Interface InnerScoreDirector<Solution_,Score_ extends Score<Score_>>
- Type Parameters:
Solution_- the solution type, the class with thePlanningSolutionannotationScore_- the score type to go with the solution
- All Superinterfaces:
AutoCloseable,ScoreDirector<Solution_>,VariableDescriptorAwareScoreDirector<Solution_>
- All Known Implementing Classes:
AbstractScoreDirector,BavetConstraintStreamScoreDirector,EasyScoreDirector,IncrementalScoreDirector
-
Method Summary
Modifier and TypeMethodDescriptionvoidafterEntityAdded(EntityDescriptor<Solution_> entityDescriptor, Object entity) default voidafterEntityAdded(Object entity) voidafterEntityRemoved(EntityDescriptor<Solution_> entityDescriptor, Object entity) default voidafterEntityRemoved(Object entity) voidafterProblemFactAdded(Object problemFact) voidafterProblemFactRemoved(Object problemFact) voidafterProblemPropertyChanged(Object problemFactOrEntity) voidassertExpectedUndoMoveScore(Move<Solution_> move, InnerScore<Score_> beforeMoveScore, SolverLifecyclePoint executionPoint) Asserts that if theScoreis calculated for the currentworking solutionin the currentScoreDirector(with incremental calculation residue), it is equal to the parameterbeforeMoveScore.voidassertExpectedWorkingScore(InnerScore<Score_> expectedWorkingScore, Object completedAction) Asserts that if theScoreis calculated for the currentworking solutionin the currentScoreDirector(with possibly incremental calculation residue), it is equal to the parameterexpectedWorkingScore.voidassertPredictedScoreFromScratch(InnerScore<Score_> predictedScore, Object completedAction) Asserts that if theScoreis calculated for the currentworking solutionin a freshScoreDirector(with no incremental calculation residue), it is equal to the parameterpredictedScore.voidassertShadowVariablesAreNotStale(InnerScore<Score_> expectedWorkingScore, Object completedAction) Asserts that if allVariableListeners are forcibly triggered, and therefore all shadow variables are updated if needed, that none of the shadow variables of theworking solutionchange, Then also asserts that theScorecalculated for theworking solutionafterwards is equal to the parameterexpectedWorkingScore.voidassertWorkingScoreFromScratch(InnerScore<Score_> workingScore, Object completedAction) Asserts that if theScoreis calculated for the currentworking solutionin a freshScoreDirector(with no incremental calculation residue), it is equal to the parameterworkingScore.voidbeforeEntityAdded(EntityDescriptor<Solution_> entityDescriptor, Object entity) default voidbeforeEntityAdded(Object entity) voidbeforeEntityRemoved(EntityDescriptor<Solution_> entityDescriptor, Object entity) default voidbeforeEntityRemoved(Object entity) voidbeforeProblemFactAdded(Object problemFact) voidbeforeProblemFactRemoved(Object problemFact) voidbeforeProblemPropertyChanged(Object problemFactOrEntity) default ScoreAnalysis<Score_>buildScoreAnalysis(ScoreAnalysisFetchPolicy scoreAnalysisFetchPolicy) Calculates theScoreand updates theworking solutionaccordingly.cloneSolution(Solution_ originalSolution) Returns a planning clone of the solution, which is not a shallow clone nor a deep clone nor a partition clone.default Solution_Returns a planning clone of the solution, which is not a shallow clone nor a deep clone nor a partition clone.voidclose()Needs to be called after use because some implementations need to clean up their resources.createChildThreadScoreDirector(ChildThreadType childThreadType) voidexecuteMove(Move<Solution_> move) executeTemporaryMove(Move<Solution_> move, boolean assertMoveScoreFromScratch) Executes a move, finds out its score, and immediately undoes it.booleanInverse shadow variables have a fail-fast for cases where the shadow variable doesn't actually point to its correct inverse.voidUnlikeScoreDirector.triggerVariableListeners()which only triggers notifications already in the queue, this triggers every variable listener on every genuine variable.longstatic <Score_ extends Score<Score_>>
ConstraintAnalysis<Score_>getConstraintAnalysis(ConstraintMatchTotal<Score_> constraintMatchTotal, ScoreAnalysisFetchPolicy scoreAnalysisFetchPolicy) Explains the impact of each planning entity or problem fact on theScore.getListVariableStateSupply(ListVariableDescriptor<Solution_> variableDescriptor) longintintvoiddefault booleanA derived score director is created from a root score director.booleanisWorkingEntityListDirty(long expectedWorkingEntityListRevision) booleanbooleanSome score directors keep a set of changes that they only apply whencalculateScore()is called.voidvoidsetAllChangesWillBeUndoneBeforeStepEnds(boolean allChangesWillBeUndoneBeforeStepEnds) Do not waste performance by propagating changes to step (or higher) mechanisms.voidsetMoveRepository(@Nullable MoveRepository<Solution_> moveRepository) Different phases may need different move repositories, as they may be based on different sets of moves.voidsetWorkingSolution(Solution_ workingSolution) Theworking solutionmust never be the same instance as thebest solution, it should be a (un)changed clone.Methods inherited from interface ai.timefold.solver.core.api.score.director.ScoreDirector
getWorkingSolution, lookUpWorkingObject, lookUpWorkingObjectOrReturnNull, triggerVariableListenersMethods inherited from interface ai.timefold.solver.core.impl.score.director.VariableDescriptorAwareScoreDirector
afterListVariableChanged, afterListVariableChanged, afterListVariableElementAssigned, afterListVariableElementAssigned, afterListVariableElementUnassigned, afterListVariableElementUnassigned, afterVariableChanged, afterVariableChanged, beforeListVariableChanged, beforeListVariableChanged, beforeListVariableElementAssigned, beforeListVariableElementAssigned, beforeListVariableElementUnassigned, beforeListVariableElementUnassigned, beforeVariableChanged, beforeVariableChanged, changeVariableFacade, getVariableDescriptorCache
-
Method Details
-
getConstraintAnalysis
static <Score_ extends Score<Score_>> ConstraintAnalysis<Score_> getConstraintAnalysis(ConstraintMatchTotal<Score_> constraintMatchTotal, ScoreAnalysisFetchPolicy scoreAnalysisFetchPolicy) -
setWorkingSolution
Theworking solutionmust never be the same instance as thebest solution, it should be a (un)changed clone.- Parameters:
workingSolution- never null
-
setMoveRepository
Different phases may need different move repositories, as they may be based on different sets of moves. Therefore move repository cannot be injected at score director construction time. A phase may not need a move repository at all, such as construction heuristics, which currently does not support Move Streams. Each phase is responsible for calling this method to set its repository, and also calling it again at the end to null it out. -
calculateScore
InnerScore<Score_> calculateScore()Calculates theScoreand updates theworking solutionaccordingly.- Returns:
- never null, the
Scoreof theworking solution
-
getConstraintMatchPolicy
ConstraintMatchPolicy getConstraintMatchPolicy()- Returns:
ConstraintMatchPolicy.ENABLEDifgetConstraintMatchTotalMap()andgetIndictmentMap()can be called.ConstraintMatchPolicy.ENABLED_WITHOUT_JUSTIFICATIONSif only the former can be called.ConstraintMatchPolicy.DISABLEDif neither can be called.
-
getConstraintMatchTotalMap
Map<String,ConstraintMatchTotal<Score_>> getConstraintMatchTotalMap()Explains theScoreofcalculateScore()by splitting it up perConstraint.The sum of
ConstraintMatchTotal.getScore()equalscalculateScore().Call
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. - Throws:
IllegalStateException- ifgetConstraintMatchPolicy()returnsConstraintMatchPolicy.DISABLED.- See Also:
-
getIndictmentMap
Map<Object,Indictment<Score_>> getIndictmentMap()Explains 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 fromcalculateScore()because eachConstraintMatch.getScore()is counted for eachconstraint justification.Call
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 - Throws:
IllegalStateException- unlessgetConstraintMatchPolicy()returnsConstraintMatchPolicy.ENABLED.- See Also:
-
getWorkingEntityListRevision
long getWorkingEntityListRevision()- Returns:
- used to check
isWorkingEntityListDirty(long)later on
-
getWorkingGenuineEntityCount
int getWorkingGenuineEntityCount() -
getWorkingInitScore
int getWorkingInitScore() -
executeMove
-
executeTemporaryMove
Executes a move, finds out its score, and immediately undoes it.- Parameters:
move- never nullassertMoveScoreFromScratch- true will hurt performance- Returns:
- never null
-
isWorkingEntityListDirty
boolean isWorkingEntityListDirty(long expectedWorkingEntityListRevision) - Parameters:
expectedWorkingEntityListRevision- an- Returns:
- true if the entityList might have a different set of instances now
-
isWorkingSolutionInitialized
boolean isWorkingSolutionInitialized() -
requiresFlushing
boolean requiresFlushing()Some score directors keep a set of changes that they only apply whencalculateScore()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.
-
expectShadowVariablesInCorrectState
boolean expectShadowVariablesInCorrectState()Inverse shadow variables have a fail-fast for cases where the shadow variable doesn't actually point to its correct inverse. This is very useful to pinpoint improperly initialized solutions.However,
SolutionManager.update(Object)exists precisely for the purpose of initializing solutions. And when this API is used, the fail-fast must not be triggered as it is guaranteed and expected that the inverse relationships will be wrong. In fact, they will be null.For this case and this case only, this method is allowed to return false. All other cases must return true, otherwise a very valuable fail-fast is lost.
- Returns:
- false if the fail-fast on shadow variables should not be triggered
-
getScoreDirectorFactory
ScoreDirectorFactory<Solution_,Score_> getScoreDirectorFactory()- Returns:
- never null
-
getSolutionDescriptor
SolutionDescriptor<Solution_> getSolutionDescriptor()- Specified by:
getSolutionDescriptorin interfaceVariableDescriptorAwareScoreDirector<Solution_>- Returns:
- never null
-
getScoreDefinition
ScoreDefinition<Score_> getScoreDefinition()- Returns:
- never null
-
cloneWorkingSolution
Returns a planning clone of the solution, which is not a shallow clone nor a deep clone nor a partition clone.- Returns:
- never null, planning clone
-
cloneSolution
Returns a planning clone of the solution, which is not a shallow clone nor a deep clone nor a partition clone.- Parameters:
originalSolution- never null- Returns:
- never null, planning clone
-
getCalculationCount
long getCalculationCount()- Returns:
- at least 0L
-
resetCalculationCount
void resetCalculationCount() -
incrementCalculationCount
void incrementCalculationCount() -
getSupplyManager
SupplyManager getSupplyManager()- Returns:
- never null
-
getMoveDirector
MoveDirector<Solution_,Score_> getMoveDirector() -
getListVariableStateSupply
ListVariableStateSupply<Solution_> getListVariableStateSupply(ListVariableDescriptor<Solution_> variableDescriptor) -
createChildThreadScoreDirector
InnerScoreDirector<Solution_,Score_> createChildThreadScoreDirector(ChildThreadType childThreadType) -
setAllChangesWillBeUndoneBeforeStepEnds
void setAllChangesWillBeUndoneBeforeStepEnds(boolean allChangesWillBeUndoneBeforeStepEnds) Do not waste performance by propagating changes to step (or higher) mechanisms.- Parameters:
allChangesWillBeUndoneBeforeStepEnds- true if all changes will be undone
-
assertExpectedWorkingScore
Asserts that if theScoreis calculated for the currentworking solutionin the currentScoreDirector(with possibly incremental calculation residue), it is equal to the parameterexpectedWorkingScore.Used to assert that skipping
calculateScore()(when the score is otherwise determined) is correct.- Parameters:
expectedWorkingScore- never nullcompletedAction- sometimes null, when assertion fails then the completedAction'sObject.toString()is included in the exception message
-
assertShadowVariablesAreNotStale
void assertShadowVariablesAreNotStale(InnerScore<Score_> expectedWorkingScore, Object completedAction) Asserts that if allVariableListeners are forcibly triggered, and therefore all shadow variables are updated if needed, that none of the shadow variables of theworking solutionchange, Then also asserts that theScorecalculated for theworking solutionafterwards is equal to the parameterexpectedWorkingScore.Used to assert that the shadow variables' state is consistent with the genuine variables' state.
- Parameters:
expectedWorkingScore- never nullcompletedAction- sometimes null, when assertion fails then the completedAction'sObject.toString()is included in the exception message
-
assertWorkingScoreFromScratch
Asserts that if theScoreis calculated for the currentworking solutionin a freshScoreDirector(with no incremental calculation residue), it is equal to the parameterworkingScore.Furthermore, if the assert fails, a score corruption analysis might be included in the exception message.
- Parameters:
workingScore- never nullcompletedAction- sometimes null, when assertion fails then the completedAction'sObject.toString()is included in the exception message- See Also:
-
assertPredictedScoreFromScratch
Asserts that if theScoreis calculated for the currentworking solutionin a freshScoreDirector(with no incremental calculation residue), it is equal to the parameterpredictedScore.Furthermore, if the assert fails, a score corruption analysis might be included in the exception message.
- Parameters:
predictedScore- never nullcompletedAction- sometimes null, when assertion fails then the completedAction'sObject.toString()is included in the exception message- See Also:
-
assertExpectedUndoMoveScore
void assertExpectedUndoMoveScore(Move<Solution_> move, InnerScore<Score_> beforeMoveScore, SolverLifecyclePoint executionPoint) Asserts that if theScoreis calculated for the currentworking solutionin the currentScoreDirector(with incremental calculation residue), it is equal to the parameterbeforeMoveScore.Furthermore, if the assert fails, a score corruption analysis might be included in the exception message.
- Parameters:
move- never nullbeforeMoveScore- never null
-
close
void close()Needs to be called after use because some implementations need to clean up their resources.- Specified by:
closein interfaceAutoCloseable
-
forceTriggerVariableListeners
void forceTriggerVariableListeners()UnlikeScoreDirector.triggerVariableListeners()which only triggers notifications already in the queue, this triggers every variable listener on every genuine variable. This is useful inSolutionManager.update(Object)to fill in shadow variable values. -
isDerived
default boolean isDerived()A derived score director is created from a root score director. The derived score director can be used to create separate* instances for use cases like multithreaded solving. -
buildScoreAnalysis
-
beforeEntityAdded
- Specified by:
beforeEntityAddedin interfaceScoreDirector<Solution_>
-
beforeEntityAdded
-
afterEntityAdded
- Specified by:
afterEntityAddedin interfaceScoreDirector<Solution_>
-
afterEntityAdded
-
beforeEntityRemoved
- Specified by:
beforeEntityRemovedin interfaceScoreDirector<Solution_>
-
beforeEntityRemoved
-
afterEntityRemoved
- Specified by:
afterEntityRemovedin interfaceScoreDirector<Solution_>
-
afterEntityRemoved
-
beforeProblemFactAdded
- Specified by:
beforeProblemFactAddedin interfaceScoreDirector<Solution_>
-
afterProblemFactAdded
- Specified by:
afterProblemFactAddedin interfaceScoreDirector<Solution_>
-
beforeProblemPropertyChanged
- Specified by:
beforeProblemPropertyChangedin interfaceScoreDirector<Solution_>
-
afterProblemPropertyChanged
- Specified by:
afterProblemPropertyChangedin interfaceScoreDirector<Solution_>
-
beforeProblemFactRemoved
- Specified by:
beforeProblemFactRemovedin interfaceScoreDirector<Solution_>
-
afterProblemFactRemoved
- Specified by:
afterProblemFactRemovedin interfaceScoreDirector<Solution_>
-