Class PartitionSolver<Solution_>
- java.lang.Object
-
- org.optaplanner.core.impl.solver.AbstractSolver<Solution_>
-
- org.optaplanner.core.impl.partitionedsearch.PartitionSolver<Solution_>
-
- Type Parameters:
Solution_- the solution type, the class with thePlanningSolutionannotation
- All Implemented Interfaces:
Solver<Solution_>
public class PartitionSolver<Solution_> extends AbstractSolver<Solution_>
-
-
Field Summary
Fields Modifier and Type Field Description protected DefaultSolverScope<Solution_>solverScope-
Fields inherited from class org.optaplanner.core.impl.solver.AbstractSolver
bestSolutionRecaller, logger, phaseLifecycleSupport, phaseList, solverEventSupport, termination
-
-
Constructor Summary
Constructors Constructor Description PartitionSolver(BestSolutionRecaller<Solution_> bestSolutionRecaller, Termination termination, List<Phase<Solution_>> phaseList, DefaultSolverScope<Solution_> solverScope)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaddProblemFactChange(ProblemFactChange<Solution_> problemFactChange)Schedules aProblemFactChangeto be processed.booleanaddProblemFactChanges(List<ProblemFactChange<Solution_>> problemFactChanges)Schedules multipleProblemFactChanges to be processed.StringexplainBestScore()Returns a diagnostic text that explains theSolver.getBestSolution()through theConstraintMatchAPI to identify which constraints or planning entities cause thatSolver.getBestScore()quality.ScoregetBestScore()Returns theScoreof theSolver.getBestSolution().Solution_getBestSolution()The best solution is thebest solutionfound during solving: it might or might not be optimal, feasible or even initialized.longgetScoreCalculationCount()InnerScoreDirectorFactory<Solution_>getScoreDirectorFactory()Useful to reuse theScorecalculation (for example in a UI) and to explain theScoreto the user with theConstraintMatchTotalandIndictmentAPI.longgetTimeMillisSpent()Returns the amount of milliseconds spent solving since the last start.booleanisEveryProblemFactChangeProcessed()Checks if all scheduledProblemFactChanges have been processed.booleanisSolving()This method is thread-safe.booleanisTerminateEarly()This method is thread-safe.Solution_solve(Solution_ problem)Solves the planning problem and returns the best solution encountered (which might or might not be optimal, feasible or even initialized).voidsolvingEnded(DefaultSolverScope<Solution_> solverScope)voidsolvingStarted(DefaultSolverScope<Solution_> solverScope)booleanterminateEarly()Notifies the solver that it should stop at its earliest convenience.-
Methods inherited from class org.optaplanner.core.impl.solver.AbstractSolver
addEventListener, addPhaseLifecycleListener, removeEventListener, removePhaseLifecycleListener, runPhases
-
-
-
-
Field Detail
-
solverScope
protected final DefaultSolverScope<Solution_> solverScope
-
-
Constructor Detail
-
PartitionSolver
public PartitionSolver(BestSolutionRecaller<Solution_> bestSolutionRecaller, Termination termination, List<Phase<Solution_>> phaseList, DefaultSolverScope<Solution_> solverScope)
-
-
Method Detail
-
getScoreDirectorFactory
public InnerScoreDirectorFactory<Solution_> getScoreDirectorFactory()
Description copied from interface:SolverUseful to reuse theScorecalculation (for example in a UI) and to explain theScoreto the user with theConstraintMatchTotalandIndictmentAPI.- Returns:
- never null
-
getBestSolution
public Solution_ getBestSolution()
Description copied from interface:SolverThe best solution is thebest solutionfound during solving: it might or might not be optimal, feasible or even initialized.The
Solver.solve(Solution_)method also returns the best solution, but this method is useful in rare asynchronous situations (althoughSolverEventListener.bestSolutionChanged(BestSolutionChangedEvent)is often more appropriate).This method is thread-safe.
- Returns:
- never null (unless
Solver.solve(Object)hasn't been called yet), but it can return the uninitializedPlanningSolutionwith aScorenull.
-
getBestScore
public Score getBestScore()
Description copied from interface:SolverReturns theScoreof theSolver.getBestSolution().This is useful for generic code, which doesn't know the type of the
PlanningSolutionto retrieve theScorefrom theSolver.getBestSolution()easily.This method is thread-safe.
- Returns:
- null if the
PlanningSolutionis still uninitialized
-
explainBestScore
public String explainBestScore()
Description copied from interface:SolverReturns a diagnostic text that explains theSolver.getBestSolution()through theConstraintMatchAPI to identify which constraints or planning entities cause thatSolver.getBestScore()quality. In case of aninfeasiblesolution, this can help diagnose the cause of that.Do not parse this string. Instead, to provide this information in a UI or a service, use
SolverFactory.getScoreDirectorFactory()to retrieveScoreDirector.getConstraintMatchTotalMap()andScoreDirector.getIndictmentMap()and convert those into a domain specific API.This method is thread-safe.
- Returns:
- null if
Solver.getBestScore()returns null - See Also:
ScoreDirector.explainScore()
-
getTimeMillisSpent
public long getTimeMillisSpent()
Description copied from interface:SolverReturns the amount of milliseconds spent solving since the last start. If it hasn't started it yet, it returns 0. If it hasn't ended yet, it returns the time between the last start and now. If it has ended already, it returns the time between the last start and the ending.A
Solver.addProblemFactChange(ProblemFactChange)triggers a restart which resets this time.This method is thread-safe.
- Returns:
- the amount of milliseconds spent solving since the last (re)start, at least 0
-
isSolving
public boolean isSolving()
Description copied from interface:SolverThis method is thread-safe.- Returns:
- true if the
Solver.solve(Solution_)method is still running.
-
terminateEarly
public boolean terminateEarly()
Description copied from interface:SolverNotifies the solver that it should stop at its earliest convenience. This method returns immediately, but it takes an undetermined time for theSolver.solve(Solution_)to actually return.If the solver is running in daemon mode, this is the only way to terminate it normally.
This method is thread-safe. It can only be called from a different thread because the original thread is still calling
Solver.solve(Object).- Returns:
- true if successful, false if was already terminating or terminated
- See Also:
Solver.isTerminateEarly(),Future.cancel(boolean)
-
isTerminateEarly
public boolean isTerminateEarly()
Description copied from interface:SolverThis method is thread-safe.- Returns:
- true if terminateEarly has been called since the
Solverstarted. - See Also:
Future.isCancelled()
-
addProblemFactChange
public boolean addProblemFactChange(ProblemFactChange<Solution_> problemFactChange)
Description copied from interface:SolverSchedules aProblemFactChangeto be processed.As a side-effect, this restarts the
Solver, effectively resetting allTerminations, but notSolver.terminateEarly().This method is thread-safe. Follows specifications of
BlockingQueue.add(Object)with by default a capacity ofInteger.MAX_VALUE.- Parameters:
problemFactChange- never null- Returns:
- true (as specified by
Collection.add(E)) - See Also:
Solver.addProblemFactChanges(List)
-
addProblemFactChanges
public boolean addProblemFactChanges(List<ProblemFactChange<Solution_>> problemFactChanges)
Description copied from interface:SolverSchedules multipleProblemFactChanges to be processed.As a side-effect, this restarts the
Solver, effectively resetting allTerminations, but notSolver.terminateEarly().This method is thread-safe. Follows specifications of
Collection.addAll(Collection)with by default a capacity ofInteger.MAX_VALUE.- Parameters:
problemFactChanges- never null- Returns:
- true (as specified by
Collection.add(E)) - See Also:
Solver.addProblemFactChange(ProblemFactChange)
-
isEveryProblemFactChangeProcessed
public boolean isEveryProblemFactChangeProcessed()
Description copied from interface:SolverChecks if all scheduledProblemFactChanges have been processed.This method is thread-safe.
- Returns:
- true if there are no
ProblemFactChanges left to do
-
solve
public Solution_ solve(Solution_ problem)
Description copied from interface:SolverSolves the planning problem and returns the best solution encountered (which might or might not be optimal, feasible or even initialized).It can take seconds, minutes, even hours or days before this method returns, depending on the
Terminationconfiguration. To terminate aSolverearly, callSolver.terminateEarly().- Parameters:
problem- never null, aPlanningSolution, usually its planning variables are uninitialized- Returns:
- never null, but it can return the original, uninitialized
PlanningSolutionwith a nullScore. - See Also:
Solver.terminateEarly()
-
solvingStarted
public void solvingStarted(DefaultSolverScope<Solution_> solverScope)
- Overrides:
solvingStartedin classAbstractSolver<Solution_>
-
solvingEnded
public void solvingEnded(DefaultSolverScope<Solution_> solverScope)
- Overrides:
solvingEndedin classAbstractSolver<Solution_>
-
getScoreCalculationCount
public long getScoreCalculationCount()
-
-