Search code examples
optaplanner

OptaPlanner, set the other shadow variable in the VariableListener


In the example taskassignment (and also in other examples), in the VariableListener(), only the shadow variable of the passed in PlanningEntity object is being updated. Like in

@Override
public void afterVariableChanged(ScoreDirector<TaskAssigningSolution> scoreDirector, Task task) {
    updateStartTime(scoreDirector, task);
}

the shadow variable of the argument task object is updated. I think a solution object will be kept updating until all its PlanningEntity/PlanningVariable are filled. And whenever a PlanningVariable is changed afterVariableChanged() will be called, and then the rule will be calculated to update the score. My question is, will there be any problem if I update another entity's shadow variable in the afterVariableChanged()? Not only the shadow variable of the passed in object. I can get the other entity by calling scoreDirector.getWorkingSolution().


Solution

  • One shadow variable can update another shadow variable.

    For example, when an instance shadow variable C (sourced on A) changes, it might also change 1 or more instances of shadow variable E (sourced on C and B):

    enter image description here

    Do call the before/after methods as needed.