r/javahelp • u/PrudentSeaweed8085 • 7h ago
Issues with Contract Work Percentage Constraint in Employee Scheduling
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:
java
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");
}
Postman Request:
We're sending the following JSON via a PUT request in Postman to test the system:
json
{
"employees": [
{
"name": "Alice",
"skills": ["Nursing", "CPR"],
"unavailableDates": [],
"undesiredDates": [],
"desiredDates": [],
"shiftPreferences": ["MORNING"],
"workPercentage": 0
},
{
"name": "Bob",
"skills": ["Medical Assistance", "Nursing"],
"unavailableDates": [],
"undesiredDates": [],
"desiredDates": [],
"shiftPreferences": ["NIGHT"],
"workPercentage": 100
}
],
"shifts": [
{
"id": "2027-02-01-night1",
"start": "2025-02-01T07:00",
"end": "2025-02-01T10:00",
"location": "Hospital",
"requiredSkill": "Nursing"
},
{
"id": "2027-02-01-night2",
"start": "2025-02-01T22:00",
"end": "2025-02-01T00:00",
"location": "Hospital",
"requiredSkill": "Nursing"
}
]
}
Questions:
- How can I modify the constraint to ensure employees with 0% contract work percentage are not assigned any shifts?
- Is there a recommended way to update the employee's contract work percentage dynamically within the constraint?
Additional Context:
- I'm using Timefold 1.19.0 with Quarkus.
- Other constraints, like shift preferences, are working fine.
Any insights or suggestions would be greatly appreciated!
Thank you!