How to avoid gaps between lessons for the same student group? #1567
MichelFonseca1024
started this conversation in
General
Replies: 1 comment 2 replies
-
There are two ways to can do this: Reward pairs of consecutive lessons (this is what done in the school timetabling quickstart in // reward consecutive lessons
Constraint studentGroupTimeEfficiency(ConstraintFactory constraintFactory) {
// A student group should have consecutive lessons
return constraintFactory
.forEach(Lesson.class)
.join(Lesson.class, Joiners.equal(Lesson::getStudentGroup),
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)
.asConstraint("Student group time efficiency");
} Penalize gaps between lessons: Constraint studentGroupTimeEfficiency(ConstraintFactory constraintFactory) {
// A student group should have consecutive lessons
return constraintFactory
.forEach(Lesson.class)
.groupBy(Lesson::getStudentGroup, lesson -> lesson.getTimeslot().getDayOfWeek(),
ConstraintCollectors.toConnectedTemporalRanges(Lesson::getStartTime, Lesson::getEndTime))
.flattenLast(ConnectedRangeChain::getGaps)
.peanalize(HardSoftScore.ONE_SOFT, gap -> (int) gap.getLength().dividedBy(Duration.ofMinutes(1L)))
.asConstraint("Student group time efficiency");
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I’m using Timefold (Java) to generate school timetables, with lesson times ranging from 7 AM to 10 PM. However, I’m facing an issue where some student groups end up with gaps between their lessons. For example, a group that should have consecutive lessons from 7 AM to 12 PM ends up with a gap between 10 AM and 11 AM.
I’d like to implement a constraint to penalize (or prohibit) gaps between lessons for the same student group on the same day, encouraging continuous blocks of lessons. I’ve tried implementing this logic as a Constraint, but haven’t had success so far.
Is there a recommended way to handle this kind of restriction in Timefold? Are there any similar examples?
Thanks in advance for your help!
Beta Was this translation helpful? Give feedback.
All reactions