问题
I have an optimisation problem which I am trying to solve with optaplanner. The solution algorithm uses a set of rules. The engine uses an object that captures the weight of each rule. The final score of a rule is an intermediate score of the rule multiplied with the weight. The score is set in the right hand side of every rule. The left hand side of every rule evaluates if its weight is not zero and only then executes the right hand side. Setting the weight of a rule to zero is a mechanism to deactivate the rule. I have run some checks and it works.
An alternative to this mechanism is to "deactivate" a rule permanently by completely removing it from the optaplanner solver config file.
I would expect that putting weight to zero and removing the rule would lead to identical results but it is not true. Removing the rule from config file yields a better solution than putting weight to zero.
Why is it so? How can I deactivate a rule at run time?
Below is an example of a rule myrule.drl:
rule "myrule"
when
MyWeights($weight: myRuleWeight != 0)
then
scoreHolder.addHardConstraintMatch(context, $weight)
end
and here the solver config:
<solver>
<scoreDirectorFactory>
<scoreDrl>myrule.drl</scoreDrl>
</scoreDirectorFactory>
</solver>
commenting out myrule.drl from solver config gives a better solution than setting weight to zero.
ANSWER
The secondsSpentLimit in the config xml was not big enough such that the optimisation with weight set to 0 had done only 111 Local Search (LS) steps within the given time period and the optimisation without the rule had done 301 LS steps. Increasing the secondsSpentLimit solved the problem
回答1:
That's impossible if they both ran the same number of steps. Turn on DEBUG
logging for org.optaplanner
to see how many steps they both ran, as well as well as their score speed.
At any step, both runs must have the exact same step score and exact same best score. If not, you got a bug, look for reproducible in the docs for potential causes.
来源:https://stackoverflow.com/questions/43492946/deactivating-optaplanner-rule