optaplanner-约束 - 动态禁用约束

发布于 2025-02-03 08:11:53 字数 1182 浏览 3 评论 0原文

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻拂→两袖风尘 2025-02-10 08:11:53

您是正确的,该约束的一部分仍可以执行。但是,零的约束权重确保无法匹配任何约束 - 它们在绩效方面可能会有成本,但不会影响得分或理由。

除了每次生成不同的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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文