Interface SolverManager<Solution_,ProblemId_>
-
- Type Parameters:
Solution_- the solution type, the class with thePlanningSolutionannotationProblemId_- the ID type of a submitted problem, such asLongorUUID.
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
DefaultSolverManager
public interface SolverManager<Solution_,ProblemId_> extends AutoCloseable
A SolverManager solves multiple planning problems of the same domain, asynchronously without blocking the calling thread.To create a SolverManager, use
create(SolverFactory, SolverManagerConfig). To solve a planning problem, callsolve(Object, Function, Consumer)orsolveAndListen(Object, Function, Consumer).These methods are thread-safe unless explicitly stated otherwise.
Internally a SolverManager manages a thread pool of solver threads (which call
Solver.solve(Object)) and consumer threads (to handle theBestSolutionChangedEvents).
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description voidclose()Terminates all solvers, cancels all solver jobs that haven't (re)started yet and discards all queuedProblemFactChanges.static <Solution_,ProblemId_>
SolverManager<Solution_,ProblemId_>create(SolverFactory<Solution_> solverFactory, SolverManagerConfig solverManagerConfig)static <Solution_,ProblemId_>
SolverManager<Solution_,ProblemId_>create(SolverConfig solverConfig, SolverManagerConfig solverManagerConfig)SolverStatusgetSolverStatus(ProblemId_ problemId)Returns if theSolveris scheduled to solve, actively solving or not.default SolverJob<Solution_,ProblemId_>solve(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> finalBestSolutionConsumer)Submits a planning problem to solve and returns immediately.SolverJob<Solution_,ProblemId_>solve(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_,? super Throwable> exceptionHandler)As defined bysolve(Object, Function, Consumer).default SolverJob<Solution_,ProblemId_>solve(ProblemId_ problemId, Solution_ problem)Submits a planning problem to solve and returns immediately.default SolverJob<Solution_,ProblemId_>solve(ProblemId_ problemId, Solution_ problem, Consumer<? super Solution_> finalBestSolutionConsumer)As defined bysolve(Object, Object).default SolverJob<Solution_,ProblemId_>solve(ProblemId_ problemId, Solution_ problem, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_,? super Throwable> exceptionHandler)As defined bysolve(Object, Object).default SolverJob<Solution_,ProblemId_>solveAndListen(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer)Submits a planning problem to solve and returns immediately.default SolverJob<Solution_,ProblemId_>solveAndListen(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer, BiConsumer<? super ProblemId_,? super Throwable> exceptionHandler)As defined bysolveAndListen(Object, Function, Consumer).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)As defined bysolveAndListen(Object, Function, Consumer).voidterminateEarly(ProblemId_ problemId)Terminates the solver or cancels the solver job if it hasn't (re)started yet.
-
-
-
Method Detail
-
create
static <Solution_,ProblemId_> SolverManager<Solution_,ProblemId_> create(SolverConfig solverConfig, SolverManagerConfig solverManagerConfig)
Use aSolverConfigand aSolverManagerConfigto build aSolverManager.When using
ScoreManagertoo, usecreate(SolverFactory, SolverManagerConfig)instead so they reuse the sameSolverFactoryinstance.- Type Parameters:
Solution_- the solution type, the class with thePlanningSolutionannotationProblemId_- the ID type of a submitted problem, such asLongorUUID.- Parameters:
solverConfig- never nullsolverManagerConfig- never null- Returns:
- never null
-
create
static <Solution_,ProblemId_> SolverManager<Solution_,ProblemId_> create(SolverFactory<Solution_> solverFactory, SolverManagerConfig solverManagerConfig)
- Type Parameters:
Solution_- the solution type, the class with thePlanningSolutionannotationProblemId_- the ID type of a submitted problem, such asLongorUUID.- Parameters:
solverFactory- never nullsolverManagerConfig- never null- Returns:
- never null
-
solve
default SolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Solution_ problem)
Submits a planning problem to solve and returns immediately. The planning problem is solved on a solverThread, as soon as one is available. To retrieve the final best solution, useSolverJob.getFinalBestSolution().In server applications, it's recommended to use
solve(Object, Function, Consumer)instead, to avoid loading the problem going stale if solving can't start immediately. To listen to intermediate best solutions too, usesolveAndListen(Object, Function, Consumer)instead.Defaults to logging exceptions as an error.
To stop a solver job before it naturally terminates, call
SolverJob.terminateEarly().- Parameters:
problemId- never null, a ID for each planning problem. This must be unique. Use this problemId toterminatethe solver early,problem- never null, aPlanningSolutionusually with uninitialized planning variables- Returns:
- never null
-
solve
default SolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Solution_ problem, Consumer<? super Solution_> finalBestSolutionConsumer)
As defined bysolve(Object, Object).- Parameters:
problemId- never null, a ID for each planning problem. This must be unique. Use this problemId toterminatethe solver early,to get the statusor if the problem changes while solving.problem- never null, aPlanningSolutionusually with uninitialized planning variablesfinalBestSolutionConsumer- sometimes null, called only once, at the end, on a consumer thread- Returns:
- never null
-
solve
default SolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Solution_ problem, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_,? super Throwable> exceptionHandler)
As defined bysolve(Object, Object).- Parameters:
problemId- never null, a ID for each planning problem. This must be unique. Use this problemId toterminatethe solver early,to get the statusor if the problem changes while solving.problem- never null, aPlanningSolutionusually 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
-
solve
default SolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> finalBestSolutionConsumer)
Submits a planning problem to solve and returns immediately. The planning problem is solved on a solverThread, as soon as one is available.When the solver terminates, the
finalBestSolutionConsumeris called once with the final best solution, on a consumerThread, as soon as one is available. To listen to intermediate best solutions too, usesolveAndListen(Object, Function, Consumer)instead.Defaults to logging exceptions as an error.
To stop a solver job before it naturally terminates, call
terminateEarly(Object).- Parameters:
problemId- never null, a ID for each planning problem. This must be unique. Use this problemId toterminatethe solver early,to get the statusor if the problem changes while solving.problemFinder- never null, a function that returns aPlanningSolution, usually with uninitialized planning variablesfinalBestSolutionConsumer- sometimes null, called only once, at the end, on a consumer thread- Returns:
- never null
-
solve
SolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_,? super Throwable> exceptionHandler)
As defined bysolve(Object, Function, Consumer).- Parameters:
problemId- never null, a ID for each planning problem. This must be unique. Use this problemId toterminatethe solver early,to get the statusor 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
default SolverJob<Solution_,ProblemId_> solveAndListen(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer)
Submits a planning problem to solve and returns immediately. The planning problem is solved on a solverThread, as soon as one is available.When the solver finds a new best solution, the
bestSolutionConsumeris called every time, on a consumerThread, as soon as one is available (taking into account any throttling waiting time), unless a newer best solution is already available by then (in which case skip ahead discards it).Defaults to logging exceptions as an error.
To stop a solver job before it naturally terminates, call
terminateEarly(Object).- Parameters:
problemId- never null, a ID for each planning problem. This must be unique. Use this problemId toterminatethe solver early,to get the statusor if the problem changes while solving.problemFinder- never null, a function that returns aPlanningSolution, usually with uninitialized planning variablesbestSolutionConsumer- never null, called multiple times, on a consumer thread- Returns:
- never null
-
solveAndListen
default SolverJob<Solution_,ProblemId_> solveAndListen(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer, BiConsumer<? super ProblemId_,? super Throwable> exceptionHandler)
As defined bysolveAndListen(Object, Function, Consumer).- Parameters:
problemId- never null, a ID for each planning problem. This must be unique. Use this problemId toterminatethe solver early,to get the statusor 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 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
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)
As defined bysolveAndListen(Object, Function, Consumer).The final best solution is delivered twice: first to the
bestSolutionConsumerwhen it is found and then again to thefinalBestSolutionConsumerwhen the solver terminates. Do not store the solution twice. This allows for use cases that only process theScorefirst (during best solution changed events) and then store the solution upon termination.- Parameters:
problemId- never null, an ID for each planning problem. This must be unique. Use this problemId toterminatethe solver early,to get the statusor 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
-
getSolverStatus
SolverStatus getSolverStatus(ProblemId_ problemId)
Returns if theSolveris scheduled to solve, actively solving or not.Returns
SolverStatus.NOT_SOLVINGif 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.- Parameters:
problemId- never null, a value given tosolve(Object, Function, Consumer)orsolveAndListen(Object, Function, Consumer)- Returns:
- never null
-
terminateEarly
void terminateEarly(ProblemId_ problemId)
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
bestSolutionConsumercould still be called (on a consumer thread), before this method returns.- Parameters:
problemId- never null, a value given tosolve(Object, Function, Consumer)orsolveAndListen(Object, Function, Consumer)
-
close
void close()
Terminates all solvers, cancels all solver jobs that haven't (re)started yet and discards all queuedProblemFactChanges. Releases all thread pool resources.No new planning problems can be submitted after calling this method.
- Specified by:
closein interfaceAutoCloseable
-
-