Search code examples
optaplanner

OptaPlanner, update multiple CustomShadowVariables in VariableListener


This question is to clarify another question from me. optaplanner-bom 7.45.0.Final. My case: the PlanningEntity is Task, it contains PlanningVariable startingTimeGrain and CustomShadowVariable duration. Yes the duration depends on the starting time. In the VariableListener(), two arguments passed in, one is the PlanningSolution object call it "s", the other is the Task object call it "task". Question, in the listener method, because all Task objects can be accessed through the "s", after changing the "task"s "duration", will there be any problem to change other task's "duration". For example, totally two tasks: taskA and taskB, in the listener method, when object taskA is passed in, may I also change the duration of the taskB?


Solution

  • Yes, you may.

    One VariableListener may affect the shadow variable (on which it is annotated) for multiple instances of the class that holds that shadow variable.

    For example, in the case below, the arrival time listener gets 1 change (amsterdam being added), but affects 3 entity instances:

    enter image description here