Class BasicPlumbingTermination<Solution_>
- java.lang.Object
-
- org.optaplanner.core.impl.solver.event.SolverLifecycleListenerAdapter<Solution_>
-
- org.optaplanner.core.impl.phase.event.PhaseLifecycleListenerAdapter<Solution_>
-
- org.optaplanner.core.impl.solver.termination.AbstractTermination<Solution_>
-
- org.optaplanner.core.impl.solver.termination.BasicPlumbingTermination<Solution_>
-
- All Implemented Interfaces:
EventListener
,PhaseLifecycleListener<Solution_>
,SolverLifecycleListener<Solution_>
,Termination<Solution_>
public class BasicPlumbingTermination<Solution_> extends AbstractTermination<Solution_>
Concurrency notes: Condition predicate on (problemFactChangeQueue
is not empty orterminatedEarly
is true).
-
-
Field Summary
Fields Modifier and Type Field Description protected boolean
daemon
protected BlockingQueue<ProblemFactChange<Solution_>>
problemFactChangeQueue
protected boolean
problemFactChangesBeingProcessed
protected boolean
terminatedEarly
-
Fields inherited from class org.optaplanner.core.impl.solver.termination.AbstractTermination
logger
-
-
Constructor Summary
Constructors Constructor Description BasicPlumbingTermination(boolean daemon)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
addProblemFactChange(ProblemFactChange<Solution_> problemFactChange)
Concurrency note: unblockswaitForRestartSolverDecision()
.boolean
addProblemFactChanges(List<ProblemFactChange<Solution_>> problemFactChangeList)
Concurrency note: unblockswaitForRestartSolverDecision()
.double
calculatePhaseTimeGradient(AbstractPhaseScope<Solution_> phaseScope)
double
calculateSolverTimeGradient(SolverScope<Solution_> solverScope)
A timeGradient is a relative estimate of how long the search will continue.Termination<Solution_>
createChildThreadTermination(SolverScope<Solution_> solverScope, ChildThreadType childThreadType)
void
endProblemFactChangesProcessing()
boolean
isEveryProblemFactChangeProcessed()
boolean
isPhaseTerminated(AbstractPhaseScope<Solution_> phaseScope)
Called by thePhase
after every step and every move to determine if the search should stop.boolean
isSolverTerminated(SolverScope<Solution_> solverScope)
Called by theSolver
after every phase to determine if the search should stop.boolean
isTerminateEarly()
This method is thread-safe.void
resetTerminateEarly()
This method is thread-safe.BlockingQueue<ProblemFactChange<Solution_>>
startProblemFactChangesProcessing()
boolean
terminateEarly()
This method is thread-safe.String
toString()
boolean
waitForRestartSolverDecision()
If this returns true, then the problemFactChangeQueue is definitely not empty.-
Methods inherited from class org.optaplanner.core.impl.phase.event.PhaseLifecycleListenerAdapter
phaseEnded, phaseStarted, stepEnded, stepStarted
-
Methods inherited from class org.optaplanner.core.impl.solver.event.SolverLifecycleListenerAdapter
solvingEnded, solvingStarted
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.optaplanner.core.impl.phase.event.PhaseLifecycleListener
phaseEnded, phaseStarted, stepEnded, stepStarted
-
Methods inherited from interface org.optaplanner.core.impl.solver.event.SolverLifecycleListener
solvingEnded, solvingStarted
-
-
-
-
Field Detail
-
daemon
protected final boolean daemon
-
terminatedEarly
protected boolean terminatedEarly
-
problemFactChangeQueue
protected BlockingQueue<ProblemFactChange<Solution_>> problemFactChangeQueue
-
problemFactChangesBeingProcessed
protected boolean problemFactChangesBeingProcessed
-
-
Method Detail
-
resetTerminateEarly
public void resetTerminateEarly()
This method is thread-safe.
-
terminateEarly
public boolean terminateEarly()
This method is thread-safe.Concurrency note: unblocks
waitForRestartSolverDecision()
.- Returns:
- true if successful
-
isTerminateEarly
public boolean isTerminateEarly()
This method is thread-safe.
-
waitForRestartSolverDecision
public boolean waitForRestartSolverDecision()
If this returns true, then the problemFactChangeQueue is definitely not empty.Concurrency note: Blocks until
problemFactChangeQueue
is not empty orterminatedEarly
is true.- Returns:
- true if the solver needs to be restarted
-
addProblemFactChange
public boolean addProblemFactChange(ProblemFactChange<Solution_> problemFactChange)
Concurrency note: unblockswaitForRestartSolverDecision()
.- Parameters:
problemFactChange
- never null- Returns:
- as specified by
Collection.add(E)
-
addProblemFactChanges
public boolean addProblemFactChanges(List<ProblemFactChange<Solution_>> problemFactChangeList)
Concurrency note: unblockswaitForRestartSolverDecision()
.- Parameters:
problemFactChangeList
- never null- Returns:
- as specified by
Collection.add(E)
-
startProblemFactChangesProcessing
public BlockingQueue<ProblemFactChange<Solution_>> startProblemFactChangesProcessing()
-
endProblemFactChangesProcessing
public void endProblemFactChangesProcessing()
-
isEveryProblemFactChangeProcessed
public boolean isEveryProblemFactChangeProcessed()
-
isSolverTerminated
public boolean isSolverTerminated(SolverScope<Solution_> solverScope)
Description copied from interface:Termination
Called by theSolver
after every phase to determine if the search should stop.- Parameters:
solverScope
- never null- Returns:
- true if the search should terminate.
-
isPhaseTerminated
public boolean isPhaseTerminated(AbstractPhaseScope<Solution_> phaseScope)
Description copied from interface:Termination
Called by thePhase
after every step and every move to determine if the search should stop.- Parameters:
phaseScope
- never null- Returns:
- true if the search should terminate.
-
calculateSolverTimeGradient
public double calculateSolverTimeGradient(SolverScope<Solution_> solverScope)
Description copied from interface:Termination
A timeGradient is a relative estimate of how long the search will continue.Clients that use a timeGradient should cache it at the start of a single step because some implementations are not time-stable.
If a timeGradient cannot be calculated, it should return -1.0. Several implementations (such a
SimulatedAnnealingAcceptor
) require a correctly implemented timeGradient.A Termination's timeGradient can be requested after they are terminated, so implementations should be careful not to return a timeGradient above 1.0.
- Parameters:
solverScope
- never null- Returns:
- timeGradient t for which
0.0 <= t <= 1.0 or -1.0
when it is not supported. At the start of a solver t is 0.0 and at the end t would be 1.0.
-
calculatePhaseTimeGradient
public double calculatePhaseTimeGradient(AbstractPhaseScope<Solution_> phaseScope)
Description copied from interface:Termination
- Parameters:
phaseScope
- never null- Returns:
- timeGradient t for which
0.0 <= t <= 1.0 or -1.0
when it is not supported. At the start of a phase t is 0.0 and at the end t would be 1.0.
-
createChildThreadTermination
public Termination<Solution_> createChildThreadTermination(SolverScope<Solution_> solverScope, ChildThreadType childThreadType)
Description copied from interface:Termination
- Specified by:
createChildThreadTermination
in interfaceTermination<Solution_>
- Overrides:
createChildThreadTermination
in classAbstractTermination<Solution_>
- Parameters:
solverScope
- never nullchildThreadType
- never null- Returns:
- not null
-
-