OptaPlanner,如何获取过滤数据的计数?

OptaPlanner, how can I get the count of filtered data?

我正在尝试使用 optaplanner 制定课程分配时间表。

如何计算同一天相同 class 的相同课程的数量? 我正在使用约束管理

                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)

(我想使用 groupBy 但我不能)

我想做:

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));
}
  
          

我不太清楚为什么你不能使用 groupBy(),你应该在你的问题中添加解释。也就是说,您尝试做的事情应该像这样可行:

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));
}