optaplanner-约束 - 动态禁用约束
我正在使用Java ConstraintStreams进行约束实现,需要动态启用/禁用约束。
遵循此答案中建议的方法 optaplanner”> optaplanner:addaplanner:add/explaints添加/删除约束似乎按预期工作(分数与启用约束时的分数不同)),但是当我在约束实现中登录时,打印了日志,这表明即使将分数设置为零,也表明即使分数也实际上正在评估。
private Constraint minimizeCost(ConstraintFactory constraintFactory) {
return constraintFactory.forEach(CloudProcess.class)
.filter(process -> {
System.out.println("Minimize computer cost");
return process.getComputer() != null;
})
.penalizeConfigurable("computerCost",
node -> (Integer) process.getComputer.getCost());
}
为了禁用我正在使用的约束,
new CloudBalance().setConstraintConfiguration(new CloudBalanceConstraintConfiguration()
.computerCost(HardMediumSoftScore.ZERO))
我已经修改了Cloudbalancing示例,以使其类似于我试图实现的示例。
那么,在理解/实施方面,我是否缺少一些东西? 禁用约束是否仍会执行定期的流操作,例如过滤器和跳过optaplanner特定操作,例如IFEXISTS,JOINS等? 有什么方法可以防止这种行为? (目前,我正在使用8.16.0. -final版本)
I am using Java ConstraintStreams for constraint implementation and need to enable/disable the constraints dynamically.
Following the suggested approach in this answer Optaplanner : Add / remove constraints dynamically using zero constraint weight seems to work as expected (the score is different compared to when the constraint is enabled) but when I put a log in the constraint implementation the log was printed which suggests that the constraint is actually being evaluated even if the score is set to zero.
private Constraint minimizeCost(ConstraintFactory constraintFactory) {
return constraintFactory.forEach(CloudProcess.class)
.filter(process -> {
System.out.println("Minimize computer cost");
return process.getComputer() != null;
})
.penalizeConfigurable("computerCost",
node -> (Integer) process.getComputer.getCost());
}
For Disabling the constraint I am using
new CloudBalance().setConstraintConfiguration(new CloudBalanceConstraintConfiguration()
.computerCost(HardMediumSoftScore.ZERO))
I have modified the CloudBalancing example to make it similar to what I am trying to implement.
So is there something that I am missing in terms of understanding/implementation ?
Will the disabled constraint still execute regular stream operations like filter and skip OptaPlanner specific operations like ifExists, joins etc.?
Is there any way to prevent this behavior ? (currently I am working with version 8.16.0.Final)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是正确的,该约束的一部分仍可以执行。但是,零的约束权重确保无法匹配任何约束 - 它们在绩效方面可能会有成本,但不会影响得分或理由。
除了每次生成不同的
CondraintProvider
外,您无能为力。将来,我们可能会改善约束禁用,以使约束完全被禁用。You are correct that part of the constraint may still be executed. But constraint weight of zero makes sure that no constraint will ever match - they may have a cost in terms of performance, but they will not affect the score nor justifications.
There is nothing else you could do, other than generating a different
ConstraintProvider
every time. In the future, we may improve constraint disabling so that the constraints are disabled entirely.