Hi,
I'm working on an employee scheduling system using Timefold and have encountered an issue with implementing a contract work percentage constraint. The goal is to ensure employees are scheduled according to their contract work percentage, but I'm facing a couple of challenges:
-
Employees with 0% Contract Work Percentage:
- Currently, employees with a 0% contract work percentage are still being assigned shifts. I want to ensure they are not assigned any shifts at all.
-
Updating Contract Work Percentage:
- I'm considering updating the employee's contract work percentage dynamically based on certain conditions. Any advice on best practices for this?
Here's my current constraint implementation:
public Constraint workPercentage(ConstraintFactory constraintFactory) {
return constraintFactory.forEach(Employee.class)
.join(Shift.class, equal(Employee::getName, Shift::getEmployee))
.groupBy(
(employee, shift) -> employee,
ConstraintCollectors.sumDuration((employee, shift) ->
Duration.between(shift.getStart(), shift.getEnd()))
)
.filter((employee, totalWorkedHours) -> {
double fullTimeHours = 40.0;
double desiredHours = employee.getWorkPercentage() * fullTimeHours;
return totalWorkedHours.toHours() != desiredHours;
})
.penalize(HardSoftBigDecimalScore.ONE_HARD, (employee, totalWorkedHours) -> {
return (int) totalWorkedHours.toHours() - employee.getWorkPercentage() * 40;
})
.asConstraint("Employee work percentage not matched");
}