Class IncrementalScoreDirector<Solution_,Score_ extends Score<Score_>>
- Type Parameters:
Solution_
- the solution type, the class with thePlanningSolution
annotationScore_
- the score type to go with the solution
- All Implemented Interfaces:
AutoCloseable
,Cloneable
,ScoreDirector<Solution_>
,InnerScoreDirector<Solution_,
Score_>
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:
-
Field Summary
Fields inherited from class org.optaplanner.core.impl.score.director.AbstractScoreDirector
allChangesWillBeUndoneBeforeStepEnds, calculationCount, constraintMatchEnabledPreference, logger, lookUpEnabled, lookUpManager, scoreDirectorFactory, variableListenerSupport, workingEntityListRevision, workingInitScore, workingSolution
-
Constructor Summary
ConstructorDescriptionIncrementalScoreDirector
(IncrementalScoreDirectorFactory<Solution_, Score_> scoreDirectorFactory, boolean lookUpEnabled, boolean constraintMatchEnabledPreference, IncrementalScoreCalculator<Solution_, Score_> incrementalScoreCalculator) -
Method Summary
Modifier and TypeMethodDescriptionvoid
afterEntityAdded
(EntityDescriptor<Solution_> entityDescriptor, Object entity) void
afterEntityRemoved
(EntityDescriptor<Solution_> entityDescriptor, Object entity) void
afterListVariableChanged
(ListVariableDescriptor<Solution_> variableDescriptor, Object entity, int fromIndex, int toIndex) Notify the score director after a list variable changes.void
afterListVariableElementAssigned
(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).void
afterListVariableElementUnassigned
(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).void
afterProblemFactAdded
(Object problemFact) void
afterProblemFactRemoved
(Object problemFact) void
afterProblemPropertyChanged
(Object problemFactOrEntity) void
afterVariableChanged
(VariableDescriptor variableDescriptor, Object entity) void
beforeEntityAdded
(EntityDescriptor<Solution_> entityDescriptor, Object entity) void
beforeEntityRemoved
(EntityDescriptor<Solution_> entityDescriptor, Object entity) void
beforeListVariableChanged
(ListVariableDescriptor<Solution_> variableDescriptor, Object entity, int fromIndex, int toIndex) Notify the score director before a list variable changes.void
beforeListVariableElementAssigned
(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).void
beforeListVariableElementUnassigned
(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).void
beforeProblemFactAdded
(Object problemFact) void
beforeProblemFactRemoved
(Object problemFact) void
beforeProblemPropertyChanged
(Object problemFactOrEntity) void
beforeVariableChanged
(VariableDescriptor variableDescriptor, Object entity) Calculates theScore
and updates theworking solution
accordingly.Explains the impact of each planning entity or problem fact on theScore
.boolean
boolean
Some score directors (such as the Drools-based) keep a set of changes that they only apply whenInnerScoreDirector.calculateScore()
is called.void
setWorkingSolution
(Solution_ workingSolution) Note: resetting the working solution does NOT substitute the calls to before/after methods of theProblemChangeDirector
duringproblem changes
, as these calls are propagated tovariable listeners
, which update shadow variables in theworking solution
to keep it consistent.Methods inherited from class org.optaplanner.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, forceTriggerVariableListeners, getCalculationCount, getScoreDefinition, getScoreDirectorFactory, getSolutionDescriptor, getSupplyManager, getWorkingEntityListRevision, getWorkingSolution, incrementCalculationCount, isConstraintConfiguration, isWorkingEntityListDirty, lookUpWorkingObject, lookUpWorkingObjectOrReturnNull, overwriteConstraintMatchEnabledPreference, resetCalculationCount, setAllChangesWillBeUndoneBeforeStepEnds, setCalculatedScore, setWorkingEntityListDirty, toString, triggerVariableListeners
-
Constructor Details
-
IncrementalScoreDirector
public IncrementalScoreDirector(IncrementalScoreDirectorFactory<Solution_, Score_> scoreDirectorFactory, boolean lookUpEnabled, boolean constraintMatchEnabledPreference, IncrementalScoreCalculator<Solution_, Score_> incrementalScoreCalculator)
-
-
Method Details
-
getIncrementalScoreCalculator
-
setWorkingSolution
Description copied from class:AbstractScoreDirector
Note: resetting the working solution does NOT substitute the calls to before/after methods of theProblemChangeDirector
duringproblem changes
, as these calls are propagated tovariable listeners
, which update shadow variables in theworking solution
to keep it consistent.- Specified by:
setWorkingSolution
in interfaceInnerScoreDirector<Solution_,
Score_ extends Score<Score_>> - Overrides:
setWorkingSolution
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>> - Parameters:
workingSolution
- never null
-
calculateScore
Description copied from interface:InnerScoreDirector
Calculates theScore
and updates theworking solution
accordingly.- Returns:
- never null, the
Score
of theworking solution
-
isConstraintMatchEnabled
public boolean isConstraintMatchEnabled()- Returns:
- true if
InnerScoreDirector.getConstraintMatchTotalMap()
andInnerScoreDirector.getIndictmentMap()
can be called
-
getConstraintMatchTotalMap
Description copied from interface:InnerScoreDirector
Explains theScore
ofInnerScoreDirector.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 lastPlanningVariable
changes.- Returns:
- never null, the key is the
constraintId
(to create one, useConstraintMatchTotal.composeConstraintId(String, String)
). - See Also:
-
getIndictmentMap
Description copied from interface:InnerScoreDirector
Explains the impact of each planning entity or problem fact on theScore
. AnIndictment
is basically the inverse of aConstraintMatchTotal
: it is aScore
total 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 lastPlanningVariable
changes.- Returns:
- never null, the key is a
problem fact
or aplanning entity
- See Also:
-
requiresFlushing
public boolean requiresFlushing()Description copied from interface:InnerScoreDirector
Some 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
- Overrides:
beforeEntityAdded
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterEntityAdded
- Overrides:
afterEntityAdded
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
beforeVariableChanged
- Specified by:
beforeVariableChanged
in interfaceInnerScoreDirector<Solution_,
Score_ extends Score<Score_>> - Overrides:
beforeVariableChanged
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterVariableChanged
- Specified by:
afterVariableChanged
in interfaceInnerScoreDirector<Solution_,
Score_ extends Score<Score_>> - Overrides:
afterVariableChanged
in 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:InnerScoreDirector
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).- Specified by:
beforeListVariableElementAssigned
in interfaceInnerScoreDirector<Solution_,
Score_ extends Score<Score_>> - Overrides:
beforeListVariableElementAssigned
in 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:InnerScoreDirector
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).- Specified by:
afterListVariableElementAssigned
in interfaceInnerScoreDirector<Solution_,
Score_ extends Score<Score_>> - Overrides:
afterListVariableElementAssigned
in 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:InnerScoreDirector
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).- Specified by:
beforeListVariableElementUnassigned
in interfaceInnerScoreDirector<Solution_,
Score_ extends Score<Score_>> - Overrides:
beforeListVariableElementUnassigned
in 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:InnerScoreDirector
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).- Specified by:
afterListVariableElementUnassigned
in interfaceInnerScoreDirector<Solution_,
Score_ extends Score<Score_>> - Overrides:
afterListVariableElementUnassigned
in 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:InnerScoreDirector
Notify 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:
fromIndex
must be greater than or equal to 0;toIndex
must be less than or equal to the list variable size.toIndex
must 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 (
fromIndex
equalstoIndex
) 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:
beforeListVariableChanged
in interfaceInnerScoreDirector<Solution_,
Score_ extends Score<Score_>> - Overrides:
beforeListVariableChanged
in 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:InnerScoreDirector
Notify 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:
fromIndex
must be greater than or equal to 0;toIndex
must be less than or equal to the list variable size.toIndex
must 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 (
fromIndex
equalstoIndex
) 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:
afterListVariableChanged
in interfaceInnerScoreDirector<Solution_,
Score_ extends Score<Score_>> - Overrides:
afterListVariableChanged
in 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
- Overrides:
beforeEntityRemoved
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterEntityRemoved
- Overrides:
afterEntityRemoved
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
beforeProblemFactAdded
- Specified by:
beforeProblemFactAdded
in interfaceScoreDirector<Solution_>
- Overrides:
beforeProblemFactAdded
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterProblemFactAdded
- Specified by:
afterProblemFactAdded
in interfaceScoreDirector<Solution_>
- Overrides:
afterProblemFactAdded
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
beforeProblemPropertyChanged
- Specified by:
beforeProblemPropertyChanged
in interfaceScoreDirector<Solution_>
- Overrides:
beforeProblemPropertyChanged
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterProblemPropertyChanged
- Specified by:
afterProblemPropertyChanged
in interfaceScoreDirector<Solution_>
- Overrides:
afterProblemPropertyChanged
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
beforeProblemFactRemoved
- Specified by:
beforeProblemFactRemoved
in interfaceScoreDirector<Solution_>
- Overrides:
beforeProblemFactRemoved
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-
afterProblemFactRemoved
- Specified by:
afterProblemFactRemoved
in interfaceScoreDirector<Solution_>
- Overrides:
afterProblemFactRemoved
in classAbstractScoreDirector<Solution_,
Score_ extends Score<Score_>, IncrementalScoreDirectorFactory<Solution_, Score_ extends Score<Score_>>>
-