【问题标题】:OptaPlanner, how can I get the count of filtered data?OptaPlanner,我怎样才能得到过滤数据的计数?
【发布时间】:2021-07-21 18:56:18
【问题描述】:

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

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

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

【问题讨论】:

  • 如果不了解您的领域模型以及您使用的是什么类型的评分函数,就无法回答这个问题。请更新您的问题并提供更多详细信息。
  • 其实我想要的是同一天最多上3节相同的课。我正在使用 optaplanner-quickstarts-schedule 域。

标签: constraints optaplanner


【解决方案1】:

我不太清楚你为什么不能使用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));
}

【讨论】:

  • 非常感谢卢卡斯。 groupBy() 问题是我缺乏知识:)
猜你喜欢
  • 2021-05-24
  • 2022-06-13
  • 1970-01-01
  • 2011-04-11
  • 1970-01-01
  • 2020-09-11
  • 2011-10-06
  • 1970-01-01
相关资源
最近更新 更多