Class DefaultSolverManager<Solution_,ProblemId_>
- Type Parameters:
Solution_
- the solution type, the class with thePlanningSolution
annotationProblemId_
- the ID type of submitted problem, such asLong
orUUID
.
- All Implemented Interfaces:
AutoCloseable
,SolverManager<Solution_,
ProblemId_>
-
Constructor Summary
ConstructorDescriptionDefaultSolverManager
(SolverFactory<Solution_> solverFactory, SolverManagerConfig solverManagerConfig) -
Method Summary
Modifier and TypeMethodDescriptionaddProblemChange
(ProblemId_ problemId, ProblemChange<Solution_> problemChange) Schedules aProblemChange
to be processed by the underlyingSolver
and returns immediately.void
close()
Terminates all solvers, cancels all solver jobs that haven't (re)started yet and discards all queuedProblemChange
s.getSolverStatus
(ProblemId_ problemId) Returns if theSolver
is scheduled to solve, actively solving or not.solve
(ProblemId_ problemId, Function<? super ProblemId_, ? extends Solution_> problemFinder, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_, ? super Throwable> exceptionHandler) As defined bySolverManager.solve(Object, Function, Consumer)
.protected SolverJob<Solution_,
ProblemId_> solve
(ProblemId_ problemId, Function<? super ProblemId_, ? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_, ? super Throwable> exceptionHandler) solveAndListen
(ProblemId_ problemId, Function<? super ProblemId_, ? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_, ? super Throwable> exceptionHandler) As defined bySolverManager.solveAndListen(Object, Function, Consumer)
.void
terminateEarly
(ProblemId_ problemId) Terminates the solver or cancels the solver job if it hasn't (re)started yet.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.optaplanner.core.api.solver.SolverManager
solve, solve, solve, solve, solveAndListen, solveAndListen
-
Constructor Details
-
DefaultSolverManager
public DefaultSolverManager(SolverFactory<Solution_> solverFactory, SolverManagerConfig solverManagerConfig)
-
-
Method Details
-
getSolverFactory
-
solve
public SolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Function<? super ProblemId_, ? extends Solution_> problemFinder, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_, ? super Throwable> exceptionHandler) Description copied from interface:SolverManager
As defined bySolverManager.solve(Object, Function, Consumer)
.- Specified by:
solve
in interfaceSolverManager<Solution_,
ProblemId_> - Parameters:
problemId
- never null, a ID for each planning problem. This must be unique. Use this problemId toterminate
the solver early,to get the status
or if the problem changes while solving.problemFinder
- never null, function that returns aPlanningSolution
, usually with uninitialized planning variablesfinalBestSolutionConsumer
- sometimes null, called only once, at the end, on a consumer threadexceptionHandler
- sometimes null, called if an exception or error occurs. If null it defaults to logging the exception as an error.- Returns:
- never null
-
solveAndListen
public SolverJob<Solution_,ProblemId_> solveAndListen(ProblemId_ problemId, Function<? super ProblemId_, ? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_, ? super Throwable> exceptionHandler) Description copied from interface:SolverManager
As defined bySolverManager.solveAndListen(Object, Function, Consumer)
.The final best solution is delivered twice: first to the
bestSolutionConsumer
when it is found and then again to thefinalBestSolutionConsumer
when the solver terminates. Do not store the solution twice. This allows for use cases that only process theScore
first (during best solution changed events) and then store the solution upon termination.- Specified by:
solveAndListen
in interfaceSolverManager<Solution_,
ProblemId_> - Parameters:
problemId
- never null, an ID for each planning problem. This must be unique. Use this problemId toterminate
the solver early,to get the status
or if the problem changes while solving.problemFinder
- never null, function that returns aPlanningSolution
, usually with uninitialized planning variablesbestSolutionConsumer
- never null, called multiple times, on a consumer threadfinalBestSolutionConsumer
- sometimes null, called only once, at the end, on a consumer thread. That final best solution is already consumed by the bestSolutionConsumer earlier.exceptionHandler
- sometimes null, called if an exception or error occurs. If null it defaults to logging the exception as an error.- Returns:
- never null
-
solve
protected SolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Function<? super ProblemId_, ? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_, ? super Throwable> exceptionHandler) -
getSolverStatus
Description copied from interface:SolverManager
Returns if theSolver
is scheduled to solve, actively solving or not.Returns
SolverStatus.NOT_SOLVING
if the solver already terminated or if the problemId was never added. To distinguish between both cases, useSolverJob.getSolverStatus()
instead. Here, that distinction is not supported because it would cause a memory leak.- Specified by:
getSolverStatus
in interfaceSolverManager<Solution_,
ProblemId_> - Parameters:
problemId
- never null, a value given toSolverManager.solve(Object, Function, Consumer)
orSolverManager.solveAndListen(Object, Function, Consumer)
- Returns:
- never null
-
addProblemChange
public CompletableFuture<Void> addProblemChange(ProblemId_ problemId, ProblemChange<Solution_> problemChange) Description copied from interface:SolverManager
Schedules aProblemChange
to be processed by the underlyingSolver
and returns immediately. If the solver already terminated or the problemId was never added, throws an exception. The same applies if the underlyingSolver
is not in theSolverStatus.SOLVING_ACTIVE
state.- Specified by:
addProblemChange
in interfaceSolverManager<Solution_,
ProblemId_> - Parameters:
problemId
- never null, a value given toSolverManager.solve(Object, Function, Consumer)
orSolverManager.solveAndListen(Object, Function, Consumer)
problemChange
- never null- Returns:
- completes after the best solution containing this change has been consumed.
-
terminateEarly
Description copied from interface:SolverManager
Terminates the solver or cancels the solver job if it hasn't (re)started yet.Does nothing if the solver already terminated or the problemId was never added. To distinguish between both cases, use
SolverJob.terminateEarly()
instead. Here, that distinction is not supported because it would cause a memory leak.Waits for the termination or cancellation to complete before returning. During termination, a
bestSolutionConsumer
could still be called. When the solver terminates, thefinalBestSolutionConsumer
is executed with the latest best solution. These consumers run on a consumer thread independently of the termination and may still run even after this method returns.- Specified by:
terminateEarly
in interfaceSolverManager<Solution_,
ProblemId_> - Parameters:
problemId
- never null, a value given toSolverManager.solve(Object, Function, Consumer)
orSolverManager.solveAndListen(Object, Function, Consumer)
-
close
public void close()Description copied from interface:SolverManager
Terminates all solvers, cancels all solver jobs that haven't (re)started yet and discards all queuedProblemChange
s. Releases all thread pool resources.No new planning problems can be submitted after calling this method.
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceSolverManager<Solution_,
ProblemId_>
-