Class IncrementalScoreDirector<Solution_,Score_ extends Score<Score_>>
- Type Parameters:
Solution_- the solution type, the class with thePlanningSolutionannotationScore_- the score type to go with the solution
- All Implemented Interfaces:
ScoreDirector<Solution_>,InnerScoreDirector<Solution_,,Score_> VariableDescriptorAwareScoreDirector<Solution_>,AutoCloseable,Cloneable
ScoreDirector, which only recalculates the Score
of the part of the working solution that changed,
instead of the going through the entire PlanningSolution. This is incremental calculation, which is fast.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classNested classes/interfaces inherited from class ai.timefold.solver.core.impl.score.director.AbstractScoreDirector
AbstractScoreDirector.AbstractScoreDirectorBuilder<Solution_,Score_ extends Score<Score_>, Factory_ extends AbstractScoreDirectorFactory<Solution_, Score_, Factory_>, Builder_ extends AbstractScoreDirector.AbstractScoreDirectorBuilder<Solution_, Score_, Factory_, Builder_>> -
Field Summary
Fields inherited from class ai.timefold.solver.core.impl.score.director.AbstractScoreDirector
constraintMatchPolicy, logger, scoreDirectorFactory, variableListenerSupport, workingSolution -
Method Summary
Modifier and TypeMethodDescriptionvoidafterEntityAdded(EntityDescriptor<Solution_> entityDescriptor, Object entity) voidafterEntityRemoved(EntityDescriptor<Solution_> entityDescriptor, Object entity) voidafterListVariableChanged(ListVariableDescriptor<Solution_> variableDescriptor, Object entity, int fromIndex, int toIndex) Notify the score director after a list variable changes.voidafterListVariableElementAssigned(ListVariableDescriptor<Solution_> variableDescriptor, Object element) Call this for each element that was assigned (added to a list variable of one entity without being removed from a list variable of another entity).voidafterListVariableElementUnassigned(ListVariableDescriptor<Solution_> variableDescriptor, Object element) Call this for each element that was unassigned (removed from a list variable of one entity without being added to a list variable of another entity).voidafterProblemFactAdded(Object problemFact) voidafterProblemFactRemoved(Object problemFact) voidafterProblemPropertyChanged(Object problemFactOrEntity) voidafterVariableChanged(VariableDescriptor variableDescriptor, Object entity) voidbeforeEntityAdded(EntityDescriptor<Solution_> entityDescriptor, Object entity) voidbeforeEntityRemoved(EntityDescriptor<Solution_> entityDescriptor, Object entity) voidbeforeListVariableChanged(ListVariableDescriptor<Solution_> variableDescriptor, Object entity, int fromIndex, int toIndex) Notify the score director before a list variable changes.voidbeforeListVariableElementAssigned(ListVariableDescriptor<Solution_> variableDescriptor, Object element) Call this for each element that will be assigned (added to a list variable of one entity without being removed from a list variable of another entity).voidbeforeListVariableElementUnassigned(ListVariableDescriptor<Solution_> variableDescriptor, Object element) Call this for each element that will be unassigned (removed from a list variable of one entity without being added to a list variable of another entity).voidbeforeProblemFactAdded(Object problemFact) voidbeforeProblemFactRemoved(Object problemFact) voidbeforeProblemPropertyChanged(Object problemFactOrEntity) voidbeforeVariableChanged(VariableDescriptor variableDescriptor, Object entity) Calculates theScoreand updates theworking solutionaccordingly.Explains the impact of each planning entity or problem fact on theScore.booleanSome score directors keep a set of changes that they only apply whenInnerScoreDirector.calculateScore()is called.voidsetWorkingSolution(Solution_ workingSolution) Theworking solutionmust never be the same instance as thebest solution, it should be a (un)changed clone.Methods inherited from class ai.timefold.solver.core.impl.score.director.AbstractScoreDirector
assertExpectedUndoMoveScore, assertExpectedWorkingScore, assertPredictedScoreFromScratch, assertShadowVariablesAreNotStale, assertWorkingScoreFromScratch, buildScoreCorruptionAnalysis, buildShadowVariableAnalysis, clearVariableListenerEvents, clone, cloneSolution, close, createChildThreadScoreDirector, executeMove, executeTemporaryMove, expectShadowVariablesInCorrectState, forceTriggerVariableListeners, getCalculationCount, getConstraintMatchPolicy, getListVariableStateSupply, getMoveDirector, getScoreDefinition, getScoreDirectorFactory, getSolutionCorruptionAfterUndo, getSolutionDescriptor, getSupplyManager, getVariableDescriptorCache, getWorkingEntityListRevision, getWorkingGenuineEntityCount, getWorkingInitScore, getWorkingSolution, incrementCalculationCount, isConstraintConfiguration, isWorkingEntityListDirty, isWorkingSolutionInitialized, lookUpWorkingObject, lookUpWorkingObjectOrReturnNull, resetCalculationCount, setAllChangesWillBeUndoneBeforeStepEnds, setCalculatedScore, setMoveRepository, setWorkingEntityListDirty, setWorkingSolution, toString, triggerVariableListenersMethods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface ai.timefold.solver.core.impl.score.director.InnerScoreDirector
afterEntityAdded, afterEntityRemoved, beforeEntityAdded, beforeEntityRemoved, buildScoreAnalysis, cloneWorkingSolution, isDerivedMethods inherited from interface ai.timefold.solver.core.impl.score.director.VariableDescriptorAwareScoreDirector
afterListVariableChanged, afterListVariableElementAssigned, afterListVariableElementUnassigned, afterVariableChanged, beforeListVariableChanged, beforeListVariableElementAssigned, beforeListVariableElementUnassigned, beforeVariableChanged, changeVariableFacade
-
Method Details
-
getIncrementalScoreCalculator
-
setWorkingSolution
Description copied from interface:InnerScoreDirectorTheworking solutionmust never be the same instance as thebest solution, it should be a (un)changed clone.- Parameters:
workingSolution- never null
-
calculateScore
Description copied from interface:InnerScoreDirectorCalculates theScoreand updates theworking solutionaccordingly.- Returns:
- never null, the
Scoreof theworking solution
-
getConstraintMatchTotalMap
Description copied from interface:InnerScoreDirectorExplains theScoreofInnerScoreDirector.calculateScore()by splitting it up perConstraint.The sum of
ConstraintMatchTotal.getScore()equalsInnerScoreDirector.calculateScore().Call
InnerScoreDirector.calculateScore()before calling this method, unless that method has already been called since the lastPlanningVariablechanges.- Returns:
- never null, the key is the constraintId
(to create one, use
ConstraintRef.composeConstraintId(String, String)). If a constraint is present in the problem but resulted in no matches, it will still be in the map with aConstraintMatchTotal.getConstraintMatchSet()size of 0. - See Also:
-
getIndictmentMap
Description copied from interface:InnerScoreDirectorExplains the impact of each planning entity or problem fact on theScore. AnIndictmentis basically the inverse of aConstraintMatchTotal: it is aScoretotal for eachconstraint justification.The sum of
ConstraintMatchTotal.getScore()differs fromInnerScoreDirector.calculateScore()because eachConstraintMatch.getScore()is counted for eachconstraint justification.Call
InnerScoreDirector.calculateScore()before calling this method, unless that method has already been called since the lastPlanningVariablechanges.- Returns:
- never null, the key is a
problem factor aplanning entity - See Also:
-
requiresFlushing
public boolean requiresFlushing()Description copied from interface:InnerScoreDirectorSome score directors keep a set of changes that they only apply whenInnerScoreDirector.calculateScore()is called. Until that happens, this set accumulates and could possibly act as a memory leak.- Returns:
- true if the score director can potentially cause a memory leak due to unflushed changes.
-
beforeEntityAdded
- Specified by:
beforeEntityAddedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>> - Overrides:
beforeEntityAddedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterEntityAdded
- Specified by:
afterEntityAddedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>> - Overrides:
afterEntityAddedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
beforeVariableChanged
- Specified by:
beforeVariableChangedin interfaceVariableDescriptorAwareScoreDirector<Solution_>- Overrides:
beforeVariableChangedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterVariableChanged
- Specified by:
afterVariableChangedin interfaceVariableDescriptorAwareScoreDirector<Solution_>- Overrides:
afterVariableChangedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
beforeListVariableElementAssigned
public void beforeListVariableElementAssigned(ListVariableDescriptor<Solution_> variableDescriptor, Object element) Description copied from interface:VariableDescriptorAwareScoreDirectorCall this for each element that will be assigned (added to a list variable of one entity without being removed from a list variable of another entity).- Specified by:
beforeListVariableElementAssignedin interfaceVariableDescriptorAwareScoreDirector<Solution_>- Overrides:
beforeListVariableElementAssignedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>> - Parameters:
variableDescriptor- the list variable descriptorelement- the assigned element
-
afterListVariableElementAssigned
public void afterListVariableElementAssigned(ListVariableDescriptor<Solution_> variableDescriptor, Object element) Description copied from interface:VariableDescriptorAwareScoreDirectorCall this for each element that was assigned (added to a list variable of one entity without being removed from a list variable of another entity).- Specified by:
afterListVariableElementAssignedin interfaceVariableDescriptorAwareScoreDirector<Solution_>- Overrides:
afterListVariableElementAssignedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>> - Parameters:
variableDescriptor- the list variable descriptorelement- the assigned element
-
beforeListVariableElementUnassigned
public void beforeListVariableElementUnassigned(ListVariableDescriptor<Solution_> variableDescriptor, Object element) Description copied from interface:VariableDescriptorAwareScoreDirectorCall this for each element that will be unassigned (removed from a list variable of one entity without being added to a list variable of another entity).- Specified by:
beforeListVariableElementUnassignedin interfaceVariableDescriptorAwareScoreDirector<Solution_>- Overrides:
beforeListVariableElementUnassignedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>> - Parameters:
variableDescriptor- the list variable descriptorelement- the unassigned element
-
afterListVariableElementUnassigned
public void afterListVariableElementUnassigned(ListVariableDescriptor<Solution_> variableDescriptor, Object element) Description copied from interface:VariableDescriptorAwareScoreDirectorCall this for each element that was unassigned (removed from a list variable of one entity without being added to a list variable of another entity).- Specified by:
afterListVariableElementUnassignedin interfaceVariableDescriptorAwareScoreDirector<Solution_>- Overrides:
afterListVariableElementUnassignedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>> - Parameters:
variableDescriptor- the list variable descriptorelement- the unassigned element
-
beforeListVariableChanged
public void beforeListVariableChanged(ListVariableDescriptor<Solution_> variableDescriptor, Object entity, int fromIndex, int toIndex) Description copied from interface:VariableDescriptorAwareScoreDirectorNotify the score director before a list variable changes.The list variable change includes:
- Changing position (index) of one or more elements.
- Removing one or more elements from the list variable.
- Adding one or more elements to the list variable.
- Any mix of the above.
fromIndex(inclusive) and ends attoIndex(exclusive).The range has to comply with the following contract:
fromIndexmust be greater than or equal to 0;toIndexmust be less than or equal to the list variable size.toIndexmust be greater than or equal tofromIndex.- The range must contain all elements that are going to be changed.
- The range is allowed to contain elements that are not going to be changed.
- The range may be empty (
fromIndexequalstoIndex) if none of the existing list variable elements are going to be changed.
VariableDescriptorAwareScoreDirector.beforeListVariableElementUnassigned(ListVariableDescriptor, Object)must be called for each element that will be unassigned (removed from a list variable of one entity without being added to a list variable of another entity).- Specified by:
beforeListVariableChangedin interfaceVariableDescriptorAwareScoreDirector<Solution_>- Overrides:
beforeListVariableChangedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>> - Parameters:
variableDescriptor- descriptor of the list variable being changedentity- the entity owning the list variable being changedfromIndex- low endpoint (inclusive) of the changed rangetoIndex- high endpoint (exclusive) of the changed range
-
afterListVariableChanged
public void afterListVariableChanged(ListVariableDescriptor<Solution_> variableDescriptor, Object entity, int fromIndex, int toIndex) Description copied from interface:VariableDescriptorAwareScoreDirectorNotify the score director after a list variable changes.The list variable change includes:
- Changing position (index) of one or more elements.
- Removing one or more elements from the list variable.
- Adding one or more elements to the list variable.
- Any mix of the above.
fromIndex(inclusive) and ends attoIndex(exclusive).The range has to comply with the following contract:
fromIndexmust be greater than or equal to 0;toIndexmust be less than or equal to the list variable size.toIndexmust be greater than or equal tofromIndex.- The range must contain all elements that have changed.
- The range is allowed to contain elements that have not changed.
- The range may be empty (
fromIndexequalstoIndex) if none of the existing list variable elements have changed.
VariableDescriptorAwareScoreDirector.afterListVariableElementUnassigned(ListVariableDescriptor, Object)must be called for each element that was unassigned (removed from a list variable of one entity without being added to a list variable of another entity).- Specified by:
afterListVariableChangedin interfaceVariableDescriptorAwareScoreDirector<Solution_>- Overrides:
afterListVariableChangedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>> - Parameters:
variableDescriptor- descriptor of the list variable being changedentity- the entity owning the list variable being changedfromIndex- low endpoint (inclusive) of the changed rangetoIndex- high endpoint (exclusive) of the changed range
-
beforeEntityRemoved
- Specified by:
beforeEntityRemovedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>> - Overrides:
beforeEntityRemovedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterEntityRemoved
- Specified by:
afterEntityRemovedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>> - Overrides:
afterEntityRemovedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
beforeProblemFactAdded
- Specified by:
beforeProblemFactAddedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>> - Specified by:
beforeProblemFactAddedin interfaceScoreDirector<Solution_>- Overrides:
beforeProblemFactAddedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterProblemFactAdded
- Specified by:
afterProblemFactAddedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>> - Specified by:
afterProblemFactAddedin interfaceScoreDirector<Solution_>- Overrides:
afterProblemFactAddedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
beforeProblemPropertyChanged
- Specified by:
beforeProblemPropertyChangedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>> - Specified by:
beforeProblemPropertyChangedin interfaceScoreDirector<Solution_>- Overrides:
beforeProblemPropertyChangedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterProblemPropertyChanged
- Specified by:
afterProblemPropertyChangedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>> - Specified by:
afterProblemPropertyChangedin interfaceScoreDirector<Solution_>- Overrides:
afterProblemPropertyChangedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
beforeProblemFactRemoved
- Specified by:
beforeProblemFactRemovedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>> - Specified by:
beforeProblemFactRemovedin interfaceScoreDirector<Solution_>- Overrides:
beforeProblemFactRemovedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterProblemFactRemoved
- Specified by:
afterProblemFactRemovedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>> - Specified by:
afterProblemFactRemovedin interfaceScoreDirector<Solution_>- Overrides:
afterProblemFactRemovedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-