Class IncrementalScoreDirector<Solution_,Score_ extends Score<Score_>>
- java.lang.Object
-
- ai.timefold.solver.core.impl.score.director.AbstractScoreDirector<Solution_,Score_,IncrementalScoreDirectorFactory<Solution_,Score_>>
-
- ai.timefold.solver.core.impl.score.director.incremental.IncrementalScoreDirector<Solution_,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_>,AutoCloseable,Cloneable
public class IncrementalScoreDirector<Solution_,Score_ extends Score<Score_>> extends AbstractScoreDirector<Solution_,Score_,IncrementalScoreDirectorFactory<Solution_,Score_>>
Incremental java implementation ofScoreDirector, which only recalculates theScoreof the part of theworking solutionthat changed, instead of the going through the entirePlanningSolution. This is incremental calculation, which is fast.- See Also:
ScoreDirector
-
-
Field Summary
-
Fields inherited from class ai.timefold.solver.core.impl.score.director.AbstractScoreDirector
allChangesWillBeUndoneBeforeStepEnds, calculationCount, constraintMatchEnabledPreference, expectShadowVariablesInCorrectState, logger, lookUpEnabled, lookUpManager, scoreDirectorFactory, variableListenerSupport, workingEntityListRevision, workingInitScore, workingSolution
-
-
Constructor Summary
Constructors Constructor Description IncrementalScoreDirector(IncrementalScoreDirectorFactory<Solution_,Score_> scoreDirectorFactory, boolean lookUpEnabled, boolean constraintMatchEnabledPreference, boolean expectShadowVariablesInCorrectState, IncrementalScoreCalculator<Solution_,Score_> incrementalScoreCalculator)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterEntityAdded(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)Score_calculateScore()Calculates theScoreand updates theworking solutionaccordingly.Map<String,ConstraintMatchTotal<Score_>>getConstraintMatchTotalMap()IncrementalScoreCalculator<Solution_,Score_>getIncrementalScoreCalculator()Map<Object,Indictment<Score_>>getIndictmentMap()Explains the impact of each planning entity or problem fact on theScore.booleanisConstraintMatchEnabled()booleanrequiresFlushing()Some score directors (such as the Drools-based) keep a set of changes that they only apply whenInnerScoreDirector.calculateScore()is called.voidsetWorkingSolution(Solution_ workingSolution)Note: resetting the working solution does NOT substitute the calls to before/after methods of theProblemChangeDirectorduringproblem changes, as these calls are propagated tovariable listeners, which update shadow variables in theworking solutionto keep it consistent.-
Methods inherited from class ai.timefold.solver.core.impl.score.director.AbstractScoreDirector
afterEntityAdded, afterEntityRemoved, afterListVariableChanged, afterListVariableElementAssigned, afterListVariableElementUnassigned, afterVariableChanged, assertExpectedUndoMoveScore, assertExpectedWorkingScore, assertNonNullPlanningIds, assertPredictedScoreFromScratch, assertShadowVariablesAreNotStale, assertWorkingScoreFromScratch, beforeEntityAdded, beforeEntityRemoved, beforeListVariableChanged, beforeListVariableElementAssigned, beforeListVariableElementUnassigned, beforeVariableChanged, buildScoreCorruptionAnalysis, buildShadowVariableAnalysis, changeVariableFacade, clone, cloneSolution, cloneWorkingSolution, close, createChildThreadScoreDirector, doAndProcessMove, doAndProcessMove, expectShadowVariablesInCorrectState, forceTriggerVariableListeners, getCalculationCount, getScoreDefinition, getScoreDirectorFactory, getSolutionDescriptor, getSupplyManager, getWorkingEntityListRevision, getWorkingSolution, incrementCalculationCount, isConstraintConfiguration, isWorkingEntityListDirty, lookUpWorkingObject, lookUpWorkingObjectOrReturnNull, overwriteConstraintMatchEnabledPreference, resetCalculationCount, setAllChangesWillBeUndoneBeforeStepEnds, setCalculatedScore, setWorkingEntityListDirty, toString, triggerVariableListeners
-
-
-
-
Constructor Detail
-
IncrementalScoreDirector
public IncrementalScoreDirector(IncrementalScoreDirectorFactory<Solution_,Score_> scoreDirectorFactory, boolean lookUpEnabled, boolean constraintMatchEnabledPreference, boolean expectShadowVariablesInCorrectState, IncrementalScoreCalculator<Solution_,Score_> incrementalScoreCalculator)
-
-
Method Detail
-
getIncrementalScoreCalculator
public IncrementalScoreCalculator<Solution_,Score_> getIncrementalScoreCalculator()
-
setWorkingSolution
public void setWorkingSolution(Solution_ workingSolution)
Description copied from class:AbstractScoreDirectorNote: resetting the working solution does NOT substitute the calls to before/after methods of theProblemChangeDirectorduringproblem changes, as these calls are propagated tovariable listeners, which update shadow variables in theworking solutionto keep it consistent.- Specified by:
setWorkingSolutionin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>>- Overrides:
setWorkingSolutionin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>- Parameters:
workingSolution- never null
-
calculateScore
public Score_ calculateScore()
Description copied from interface:InnerScoreDirectorCalculates theScoreand updates theworking solutionaccordingly.- Returns:
- never null, the
Scoreof theworking solution
-
isConstraintMatchEnabled
public boolean isConstraintMatchEnabled()
- Returns:
- true if
InnerScoreDirector.getConstraintMatchTotalMap()andInnerScoreDirector.getIndictmentMap()can be called
-
getConstraintMatchTotalMap
public Map<String,ConstraintMatchTotal<Score_>> 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, useConstraintMatchTotal.composeConstraintId(String, String)). - See Also:
InnerScoreDirector.getIndictmentMap()
-
getIndictmentMap
public Map<Object,Indictment<Score_>> 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:
InnerScoreDirector.getConstraintMatchTotalMap()
-
requiresFlushing
public boolean requiresFlushing()
Description copied from interface:InnerScoreDirectorSome score directors (such as the Drools-based) 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
public void beforeEntityAdded(EntityDescriptor<Solution_> entityDescriptor, Object entity)
- Overrides:
beforeEntityAddedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>
-
afterEntityAdded
public void afterEntityAdded(EntityDescriptor<Solution_> entityDescriptor, Object entity)
- Overrides:
afterEntityAddedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>
-
beforeVariableChanged
public void beforeVariableChanged(VariableDescriptor variableDescriptor, Object entity)
- Specified by:
beforeVariableChangedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>>- Overrides:
beforeVariableChangedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>
-
afterVariableChanged
public void afterVariableChanged(VariableDescriptor variableDescriptor, Object entity)
- Specified by:
afterVariableChangedin interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>>- 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:InnerScoreDirectorCall 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 interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>>- 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:InnerScoreDirectorCall 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 interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>>- 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:InnerScoreDirectorCall 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 interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>>- 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:InnerScoreDirectorCall 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 interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>>- 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:InnerScoreDirectorNotify 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.
InnerScoreDirector.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 interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>>- 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:InnerScoreDirectorNotify 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.
InnerScoreDirector.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 interfaceInnerScoreDirector<Solution_,Score_ extends Score<Score_>>- 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
public void beforeEntityRemoved(EntityDescriptor<Solution_> entityDescriptor, Object entity)
- Overrides:
beforeEntityRemovedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>
-
afterEntityRemoved
public void afterEntityRemoved(EntityDescriptor<Solution_> entityDescriptor, Object entity)
- Overrides:
afterEntityRemovedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>
-
beforeProblemFactAdded
public void beforeProblemFactAdded(Object problemFact)
- Specified by:
beforeProblemFactAddedin interfaceScoreDirector<Solution_>- Overrides:
beforeProblemFactAddedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>
-
afterProblemFactAdded
public void afterProblemFactAdded(Object problemFact)
- Specified by:
afterProblemFactAddedin interfaceScoreDirector<Solution_>- Overrides:
afterProblemFactAddedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>
-
beforeProblemPropertyChanged
public void beforeProblemPropertyChanged(Object problemFactOrEntity)
- Specified by:
beforeProblemPropertyChangedin interfaceScoreDirector<Solution_>- Overrides:
beforeProblemPropertyChangedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>
-
afterProblemPropertyChanged
public void afterProblemPropertyChanged(Object problemFactOrEntity)
- Specified by:
afterProblemPropertyChangedin interfaceScoreDirector<Solution_>- Overrides:
afterProblemPropertyChangedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>
-
beforeProblemFactRemoved
public void beforeProblemFactRemoved(Object problemFact)
- Specified by:
beforeProblemFactRemovedin interfaceScoreDirector<Solution_>- Overrides:
beforeProblemFactRemovedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>
-
afterProblemFactRemoved
public void afterProblemFactRemoved(Object problemFact)
- Specified by:
afterProblemFactRemovedin interfaceScoreDirector<Solution_>- Overrides:
afterProblemFactRemovedin classAbstractScoreDirector<Solution_,Score_ extends Score<Score_>,IncrementalScoreDirectorFactory<Solution_,Score_ extends Score<Score_>>>
-
-