简体   繁体   English

OptaPlanner:混合约束和基于 for-next 的分数计算

[英]OptaPlanner: Mixing constraint and for-next based score calculation

I am implementing several restrictions within my CustomizedConstraintProvider class using the streaming API. Nevertheless there is one special case, where I currently do not see how to get this properly implemented within the streaming API.我正在使用流 API 在我的 CustomizedConstraintProvider class 中实施一些限制。不过有一个特殊情况,我目前看不到如何在流 API 中正确实施它。

If I got several methods...如果我有几种方法...

private Constraint Restriction1(ConstraintFactory constraintFactory) {
   return constraintFactory
      .forEach(Class.class)
      ...
      .penalize("Restriction1", HardMediumSoftBigDecimalScore.ONE_HARD)
}

private Constraint Restriction2(ConstraintFactory constraintFactory) {
   return constraintFactory
      .forEach(Class.class)
      ...
      .penalize("Restriction2", HardMediumSoftBigDecimalScore.ONE_SOFT)
}

private Constraint Restriction3(ConstraintFactory constraintFactory) {
   return constraintFactory
      .forEach(Class.class)
      ...
      .penalizeBigDecimal("Restriction3", HardMediumSoftBigDecimalScore.ONE_MEDIUM,
       (a, b, c) -> BigDecimal.valueOf(Math.pow((b - c), a))
}

how can I implement one particular method (let's say "Restriction4" that runs with for-next loops, accessing the assignment lists and returning medium and soft scores at the end depending on the evaluation within the ConstraintFactroy approach? In the manual I only read this as an either or approach (TimeTableEasyScoreCalculator vs. TimeTableConstraintProvider in chapter 2 of the manual for the current OptaPlanner version 8.19.0). I am aware that the looping way scales way more poorly than the streaming alternative but this shall be a basis to get later into the more complex Constraint Stream Score Calculation having a working solution on hand for comparison.我如何实现一个特定的方法(假设“Restriction4”与 for-next 循环一起运行,访问分配列表并在最后返回中等和软分数取决于 ConstraintFactroy 方法中的评估?在手册中我只读了这个作为一种或多种方法(当前 OptaPlanner 版本 8.19.0 手册第 2 章中的 TimeTableEasyScoreCalculator 与 TimeTableConstraintProvider)。我知道循环方式比流式替代方式更差,但这将成为以后获得的基础进入更复杂的约束 Stream 分数计算,手头有一个工作解决方案用于比较。

Thanks in advance!提前致谢!

The easy and entirely unhelpful answer is that you can not use for-style loops in constraint streams.简单但完全无用的答案是您不能在约束流中使用 for 式循环。

The Constraint Streams API is designed to give you incremental performance, and therefore you need to think of your constraints in a certain way. Constraint Streams API 旨在为您提供增量性能,因此您需要以某种方式考虑您的约束。 This way is not always easy to learn, and it requires practice.这种方法并不总是容易学习,需要练习。 That said, we have not yet seen a constraint which we could not implement incrementally.也就是说,我们还没有看到我们无法逐步实施的约束。

For example, groupBy is a very powerful construct which allows you to transform your data in pretty much any way you want.例如, groupBy是一个非常强大的结构,它允许您以几乎任何您想要的方式转换数据。 If you implement a custom constraint collector, you can solve even very complex problems incrementally.如果您实施自定义约束收集器,您甚至可以逐步解决非常复杂的问题。

However, some users simply use groupBy() together with the toList() constraint collector, gather all their data in a single collection, and then penalize on that.然而,一些用户只是将groupBy()toList()约束收集器一起使用,将所有数据收集到一个集合中,然后对其进行惩罚。 I will not give an example of that, as it is an anti-pattern which leads to poor performance, and we generally discourage it.我不会举一个例子,因为它是一种导致性能不佳的反模式,我们通常不鼓励它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM