Let's say that I have an application for generating a university timetable and I have as a constraint a preference of some teachers to teach only in certain periods of a specific day. How can I implement that type of constraint in Timefold? What would be the best practices to add such situations in the scheduling algorithm in Spring Boot/Java?
I saw an example on the Timefold's GitHub Repo for the following scenario that may be similar to what I need: A teacher prefers to teach sequential lessons and dislikes gaps between lessons. And the code for this constraint looks like this:
Constraint teacherTimeEfficiency(ConstraintFactory constraintFactory) {
// A teacher prefers to teach sequential lessons and dislikes gaps between lessons.
return constraintFactory
.forEachUniquePair(Lesson.class,
Joiners.equal(Lesson::getTeacher),
Joiners.equal((lesson) -> lesson.getTimeslot().getDayOfWeek()))
.filter((lesson1, lesson2) -> {
Duration between = Duration.between(lesson1.getTimeslot().getEndTime(),
lesson2.getTimeslot().getStartTime());
return !between.isNegative() && between.compareTo(Duration.ofMinutes(30)) <= 0;
})
.reward(HardSoftScore.ONE_SOFT)
.justifyWith((lesson1, lesson2, score) -> new TeacherTimeEfficiencyJustification(lesson1.getTeacher(), lesson1, lesson2))
.asConstraint("Teacher time efficiency");
}
Do you know the exact
Timeslotsthat are preferred? If so, I would modelTeacherlike this:And then the constraint would be
This will penalize ALL non-preferred timeslot assignments for a
Teacher(provided theTeacherhas preferred timeslots). If aTeacherhas no preferred timeslots on Monday, and you do not want to penalize all Monday assignments for that teacher, you would want to change theList<Timeslot>to aMap<DayOfWeek, List<Timeslot>>(with the corresponding change in the filter):