Replies: 3 comments 1 reply
-
After getting some help, I changed my implementation from: return constraintFactory.forEach(Employee.class)
.join(Shift.class, equal(Employee::getName, Shift::getEmployee))
.groupBy(Employee::getName,
ConstraintCollectors.sum(shift ->
Duration.between(shift.getStart(), shift.getEnd()).toHours())) to: return constraintFactory.forEach(Employee.class)
.join(Shift.class, equal(Employee::getName, Shift::getEmployee))
.groupBy(
(employee, shift) -> employee, // Changed to BiFunction
ConstraintCollectors.sum((employee, shift) ->
(int) Duration.between(shift.getStart(), shift.getEnd()).toHours())
) The key changes were:
This resolved all the type mismatch errors, but I have two questions:
public class Employee {
@PlanningId
private String name;
private int workPercentage;
// ...
} Should I be grouping by the entire
The constraint works now, but I want to make sure I'm following best practices. Any insights would be appreciated! |
Beta Was this translation helpful? Give feedback.
-
I don't think it matters all that much as you are basically passing a pointer to a Java object.
I do not know the the performance impact of Duration.toHours() and having it evaluate every time together with the constraint. I would assume you'll be fine here. You might opt to make the |
Beta Was this translation helpful? Give feedback.
-
My colleagues have already said most of what needs to be said. I'll only suggest that you ask yourself what should happen to employees with no shifts. Are you intentionally excluding them from the calculation? If so, don't change anything. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm working on an employee scheduling system (repo here) using Timefold, and I'm running into type mismatch issues with my constraint streams. Specifically, I'm trying to implement a work percentage constraint that ensures employees are scheduled according to their preferred work percentage.
Here's my current implementation:
I'm getting several type mismatch errors:
groupBy
method is expectingBiConstraintCollector<Employee,Shift,ResultContainerA_,ResultA_>
but gettingUniConstraintCollector<Object,?,Integer>
getStart()
andgetEnd()
methods because it's seeing the parameter asObject
instead ofShift
Employee::getName
My domain classes are structured as follows:
For context, other constraints in my system work fine. For example, this similar constraint for shift preferences works without type issues:
I'm using Timefold 1.19.0 with Quarkus, and my solver configuration is standard:
Has anyone encountered similar issues with constraint streams and grouping operations? What's the correct way to handle these type parameters?
Any help would be greatly appreciated!
Beta Was this translation helpful? Give feedback.
All reactions