Optaplanner 8-学校时间表限制工厂避免在同一天提前空时隙



我正在学习Optaplanner,并在创建新的约束方面取得了巨大成功,如果涉及到课程的话。然而,如果没有分配给时间段的课程,我很难找到如何创建constraintFactory。

我会从早上开始上课,如果可能的话,不会有空位。奖励("没有空位",HardSoftScore.ONE_SOFT(。类似于:

import org.acme.kotlin.schooltimetabling.domain.Timeslot
...
fun noEmptyEarlierTimeslots(constraintFactory: ConstraintFactory): Constraint {
// try to fill the slots from the start and have not empty slots later in the day on the same day
return constraintFactory
.from(Timeslot::class.java)
.join(Timeslot::class.java)
.filter { slot1: Timeslot, slot2: Timeslot -> slot1.startTime.toLocalDate() === slot2.startTime.toLocalDate() }
slot1.startTime < slo2.startTime && ifNotExists(slot1::Lesson) && ifExists(slot2::Lesson)
.reward("no empty earlier time slots", HardSoftScore.ONE_SOFT)
}

[我将startTime和endTime更改为localDateTime,因此使用了toLocalDate((]

任何帮助都将不胜感激。

"如果没有课程分配给某个时间段">

我认为.ifNotExists()可能适用于此。基本上类似于from(Timeslot.class).ifNotExists(Lesson.class, equal(identity(), Lesson::getTimeslot)...

"我会从早上开始上课,如果可能的话,不会有空位;

看看示例中的这个约束:

Constraint teacherTimeEfficiency(ConstraintFactory constraintFactory) {
// A teacher prefers to teach sequential lessons and dislikes gaps between lessons.
return constraintFactory
.from(Lesson.class)
.join(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("Teacher time efficiency", HardSoftScore.ONE_SOFT);
}

最新更新