I'm trying to make a course assignment schedule with optaplanner.
How can I get the count of same lessons with the same class on the same day? I am using constraint management
for example
-----------------------------------------------
0.P Math Math Math --> 3 ---Pen.ofHard(5)
1.P Math Math Another --> 2 -- No Pen
2.P Another Another Another --> 3 -- Pen.ofHard(5)
(I wanted to use groupBy but I couldn't)
i want to do :
private Constraint checkMaxLesson(ConstraintFactory constraintFactory) {
return constraintFactory.fromUniquePair(Lesson.class,
Joiners.equal(Lesson::getStudentGroup),
Joiners.equal(Lesson::getSubject),
Joiners.equal(t -> t.getTimeSlot().getDayOfWeek()))
**??.filter( count() > 2)**
.penalize("Max 2 lesson some day",HardSoftScore.ofHard(5));
}
I am not quite sure why you weren't able to use groupBy()
, you should add the explanation to your question. That said, what you're trying to do should be doable like so:
private Constraint checkMaxLesson(ConstraintFactory constraintFactory) {
return constraintFactory.from(Lesson.class)
.groupBy(Lesson::getStudentGroup,
Lesson::getSubject,
lesson -> lesson.getTimeSlot().getDayOfWeek(),
ConstraintCollectors.count())
.filter((group, subject, dayOfWeek, lessonCount) -> lessonCount > 2)
.penalize("Max 2 lessons on any given day",
HardSoftScore.ofHard(5),
(group, subject, dayOfWeek, lessonCount) -> lessonCount - 2));
}