2

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)

Ojas Dubey
  • 91
  • 7

1 Answers1

3

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.

Lukáš Petrovický
  • 3,945
  • 1
  • 11
  • 20
  • I see. So that makes me wonder what this line in the documentation which was added as a part of this change https://issues.redhat.com/browse/PLANNER-2639 actually means where it says that zero weight constraints are "automatically disabled". As per Geoffrey's comments on the linked issue there would be no impact on the performance. Is that not the case ? – Ojas Dubey Jun 01 '22 at 03:55
  • 2
    That depends. I was a bit surprised when I double-checked the behavior while writing the answer to your question and found out that indeed the constraints are not disabled completely - therefore what the JIRA says is not entirely correct for the Drools-based implementation of Constraint Streams. – Lukáš Petrovický Jun 01 '22 at 08:13
  • 2
    Yes, my statement is wrong about Constraint Streams Drools. Sorry for that. We intend to fix this in a future release some day, so Constraint Streams will have constraints with zero weights that don't have an perf impact - be the same as not being there at all. – Geoffrey De Smet Jun 01 '22 at 12:16
  • Ok. Thanks Geoffrey, Lukáš for the quick response. – Ojas Dubey Jun 06 '22 at 07:39
  • I think we really need the functionality for adding / removing / disabling constraints – Orkhan Hasanli Sep 18 '22 at 16:12