Interface Solver<Solution_>
-
- Type Parameters:
Solution_- the solution type, the class with thePlanningSolutionannotation
- All Known Implementing Classes:
AbstractSolver,DefaultSolver,PartitionSolver
public interface Solver<Solution_>A Solver solves a planning problem and returns the best solution found. It's recommended to create a new Solver instance for each dataset.To create a Solver, use
SolverFactory.buildSolver(). To solve a planning problem, callsolve(Object). To solve a planning problem without blocking the current thread, useSolverManagerinstead.These methods are not thread-safe and should be called from the same thread, except for the methods that are explicitly marked as thread-safe. Note that despite that
solve(Solution_)is not thread-safe for clients of this class, that method is free to do multithreading inside itself.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description voidaddEventListener(SolverEventListener<Solution_> eventListener)booleanaddProblemFactChange(ProblemFactChange<Solution_> problemFactChange)Schedules aProblemFactChangeto be processed.booleanaddProblemFactChanges(List<ProblemFactChange<Solution_>> problemFactChangeList)Schedules multipleProblemFactChanges to be processed.StringexplainBestScore()Deprecated.in favor ofScoreManager.explainScore(Object)ScoregetBestScore()Deprecated.in favor ofScoreManager.updateScore(Object)Solution_getBestSolution()Deprecated.ScoreDirectorFactory<Solution_>getScoreDirectorFactory()Deprecated.in favor ofSolverFactory.getScoreDirectorFactory()Will be removed in 8.0.longgetTimeMillisSpent()Deprecated.in favor ofSolverJob.getSolvingDuration().booleanisEveryProblemFactChangeProcessed()Checks if all scheduledProblemFactChanges have been processed.booleanisSolving()This method is thread-safe.booleanisTerminateEarly()This method is thread-safe.voidremoveEventListener(SolverEventListener<Solution_> eventListener)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).booleanterminateEarly()Notifies the solver that it should stop at its earliest convenience.
-
-
-
Method Detail
-
solve
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).It can take seconds, minutes, even hours or days before this method returns, depending on the
Terminationconfiguration. To terminate aSolverearly, callterminateEarly().- 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:
terminateEarly()
-
terminateEarly
boolean terminateEarly()
Notifies the solver that it should stop at its earliest convenience. This method returns immediately, but it takes an undetermined time for thesolve(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
solve(Object).- Returns:
- true if successful, false if was already terminating or terminated
- See Also:
isTerminateEarly(),Future.cancel(boolean)
-
getBestSolution
@Deprecated Solution_ getBestSolution()
Deprecated.The best solution is thebest solutionfound during solving: it might or might not be optimal, feasible or even initialized.The
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
solve(Object)hasn't been called yet), but it can return the uninitializedPlanningSolutionwith aScorenull.
-
getBestScore
@Deprecated Score getBestScore()
Deprecated.in favor ofScoreManager.updateScore(Object)Returns theScoreof thegetBestSolution().This is useful for generic code, which doesn't know the type of the
PlanningSolutionto retrieve theScorefrom thegetBestSolution()easily.This method is thread-safe.
- Returns:
- null if the
PlanningSolutionis still uninitialized
-
explainBestScore
@Deprecated String explainBestScore()
Deprecated.in favor ofScoreManager.explainScore(Object)Returns a diagnostic text that explains thegetBestSolution()through theConstraintMatchAPI to identify which constraints or planning entities cause thatgetBestScore()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
getBestScore()returns null - See Also:
ScoreDirector.explainScore()
-
getTimeMillisSpent
@Deprecated long getTimeMillisSpent()
Deprecated.in favor ofSolverJob.getSolvingDuration().Returns 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
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
boolean isSolving()
This method is thread-safe.- Returns:
- true if the
solve(Solution_)method is still running.
-
isTerminateEarly
boolean isTerminateEarly()
This method is thread-safe.- Returns:
- true if terminateEarly has been called since the
Solverstarted. - See Also:
Future.isCancelled()
-
addProblemFactChange
boolean addProblemFactChange(ProblemFactChange<Solution_> problemFactChange)
Schedules aProblemFactChangeto be processed.As a side-effect, this restarts the
Solver, effectively resetting allTerminations, but notterminateEarly().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:
addProblemFactChanges(List)
-
addProblemFactChanges
boolean addProblemFactChanges(List<ProblemFactChange<Solution_>> problemFactChangeList)
Schedules multipleProblemFactChanges to be processed.As a side-effect, this restarts the
Solver, effectively resetting allTerminations, but notterminateEarly().This method is thread-safe. Follows specifications of
Collection.addAll(Collection)with by default a capacity ofInteger.MAX_VALUE.- Parameters:
problemFactChangeList- never null- Returns:
- true (as specified by
Collection.add(E)) - See Also:
addProblemFactChange(ProblemFactChange)
-
isEveryProblemFactChangeProcessed
boolean isEveryProblemFactChangeProcessed()
Checks if all scheduledProblemFactChanges have been processed.This method is thread-safe.
- Returns:
- true if there are no
ProblemFactChanges left to do
-
addEventListener
void addEventListener(SolverEventListener<Solution_> eventListener)
- Parameters:
eventListener- never null
-
removeEventListener
void removeEventListener(SolverEventListener<Solution_> eventListener)
- Parameters:
eventListener- never null
-
getScoreDirectorFactory
@Deprecated ScoreDirectorFactory<Solution_> getScoreDirectorFactory()
Deprecated.in favor ofSolverFactory.getScoreDirectorFactory()Will be removed in 8.0.Useful to reuse theScorecalculation (for example in a UI) and to explain theScoreto the user with theConstraintMatchTotalandIndictmentAPI.- Returns:
- never null
-
-