Good morning.
I'm working on Optaplanner and trying to implement a rule that maximizes consecutive shifts assigned to the same employee. Let's take this simple case for example:
starting example of the problem set
I have 10 hour long shifts and only two employees with a part time contract (therefore they can work a maximum of 4 hours a day). So, what I expect to see from the result (regardless of the order in which the two shifts in excess are left empty) is something like that:
I'm still running into some problems on the correct implementation of the rule, which I post below:
Constraint maximumConsecutiveShifts(ConstraintFactory constraintFactory) {
return constraintFactory.forEach(Shift.class)
.join(Shift.class,
equal(Shift::getEmployee),
lessThan(Shift::getEndDateTime, Shift::getStartDateTime), equal(Shift::getDay))
.ifNotExists(Shift.class,
equal((s1, s2) -> s1.getEmployee(), Shift::getEmployee),
lessThanOrEqual((s1, s2) -> s1.getEndDateTime(), Shift::getStartDateTime),
greaterThanOrEqual((s1, s2) -> s2.getStartDateTime(), Shift::getEndDateTime))
.filter((s1, s2) -> !Objects.equals(s1, s2))
.penalizeConfigurableLong(CONSTRAINT_MAXIMUM_CONSECUTIVE_SHIFTS_SAME_DAY, (s1, s2) -> {
long breakLength = s1.getEndDateTime().until(s2.getStartDateTime(), ChronoUnit.MINUTES);
return (24 * 60) - breakLength;
});
}
Could you please help me on how to proceed in an optimal way to understand where I'm wrong? Any kind of input would be of great help to me
Many thanks