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)
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.