Search code examples
javaoptaplannertimefold

Is it recommended to check if a planning variable really changed before informing the ScoreDirector?


Is it recommended to check if a planning variable really changed before informing the ScoreDirector or does the score director is doing that check internally before it start recalculating the store? E.G. Can I omit the if clause in the following example or does it have a performance impact?

if (oldValue != newValue) {
   scoreDirector.beforeVariableChanged(holdingJob, "required");
   holdingJob.setRequired(newValue);
   scoreDirector.afterVariableChanged(holdingJob, "required");
}

Solution

  • The ScoreDirector does not check if a variable actually changed; when afterVariableChanged is called, it assumes that the variable did indeed change and incrementally update its internal score. Nothing bad will happen if the check is omitted, just a performance lost resulting from a pointless incremental update.

    So, in general, if there is a possibility that the VariableListener/Move does not actually change the entity variable (i.e. oldValue == newValue), it is recommended to have the if (oldValue != newValue) check to improve performance.