Solution_
- the solution type, the class with the PlanningSolution
annotationProblemId_
- the ID type of a submitted problem, such as Long
or UUID
.public interface SolverManager<Solution_,ProblemId_> extends AutoCloseable
To create a SolverManager, use create(SolverFactory, SolverManagerConfig)
.
To solve a planning problem, call solve(Object, Function, Consumer)
or solveAndListen(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 the BestSolutionChangedEvent
s).
Modifier and Type | Method and Description |
---|---|
void |
close()
Terminates all solvers, cancels all solver jobs that haven't (re)started yet
and discards all queued
ProblemFactChange s. |
static <Solution_,ProblemId_> |
create(SolverConfig solverConfig,
SolverManagerConfig solverManagerConfig)
|
static <Solution_,ProblemId_> |
create(SolverFactory<Solution_> solverFactory,
SolverManagerConfig solverManagerConfig)
|
SolverStatus |
getSolverStatus(ProblemId_ problemId)
Returns if the
Solver is 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 by
solve(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 by
solve(Object, Object) . |
default SolverJob<Solution_,ProblemId_> |
solve(ProblemId_ problemId,
Solution_ problem,
Consumer<? super Solution_> finalBestSolutionConsumer,
BiConsumer<? super ProblemId_,? super Throwable> exceptionHandler)
As defined by
solve(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 by
solveAndListen(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 by
solveAndListen(Object, Function, Consumer) . |
void |
terminateEarly(ProblemId_ problemId)
Terminates the solver or cancels the solver job if it hasn't (re)started yet.
|
static <Solution_,ProblemId_> SolverManager<Solution_,ProblemId_> create(SolverConfig solverConfig, SolverManagerConfig solverManagerConfig)
SolverConfig
and a SolverManagerConfig
to build a SolverManager
.
When using ScoreManager
too, use create(SolverFactory, SolverManagerConfig)
instead
so they reuse the same SolverFactory
instance.
Solution_
- the solution type, the class with the PlanningSolution
annotationProblemId_
- the ID type of a submitted problem, such as Long
or UUID
.solverConfig
- never nullsolverManagerConfig
- never nullstatic <Solution_,ProblemId_> SolverManager<Solution_,ProblemId_> create(SolverFactory<Solution_> solverFactory, SolverManagerConfig solverManagerConfig)
Solution_
- the solution type, the class with the PlanningSolution
annotationProblemId_
- the ID type of a submitted problem, such as Long
or UUID
.solverFactory
- never nullsolverManagerConfig
- never nulldefault SolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Solution_ problem)
Thread
, as soon as one is available.
To retrieve the final best solution, use SolverJob.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, use solveAndListen(Object, Function, Consumer)
instead.
Defaults to logging exceptions as an error.
To stop a solver job before it naturally terminates, call SolverJob.terminateEarly()
.
problemId
- never null, a ID for each planning problem. This must be unique.
Use this problemId to terminate
the solver early,problem
- never null, a PlanningSolution
usually with uninitialized planning variablesdefault SolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Solution_ problem, Consumer<? super Solution_> finalBestSolutionConsumer)
solve(Object, Object)
.problemId
- never null, a ID for each planning problem. This must be unique.
Use this problemId to terminate
the solver early,
to get the status
or if the problem changes while solving.problem
- never null, a PlanningSolution
usually with uninitialized planning variablesfinalBestSolutionConsumer
- sometimes null, called only once, at the end, on a consumer threaddefault SolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Solution_ problem, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_,? super Throwable> exceptionHandler)
solve(Object, Object)
.problemId
- never null, a ID for each planning problem. This must be unique.
Use this problemId to terminate
the solver early,
to get the status
or if the problem changes while solving.problem
- never null, a PlanningSolution
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.default SolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> finalBestSolutionConsumer)
Thread
, as soon as one is available.
When the solver terminates, the finalBestSolutionConsumer
is called once with the final best solution,
on a consumer Thread
, as soon as one is available.
To listen to intermediate best solutions too, use solveAndListen(Object, Function, Consumer)
instead.
Defaults to logging exceptions as an error.
To stop a solver job before it naturally terminates, call terminateEarly(Object)
.
problemId
- never null, a ID for each planning problem. This must be unique.
Use this problemId to terminate
the solver early,
to get the status
or if the problem changes while solving.problemFinder
- never null, a function that returns a PlanningSolution
, usually with uninitialized planning
variablesfinalBestSolutionConsumer
- sometimes null, called only once, at the end, on a consumer threadSolverJob<Solution_,ProblemId_> solve(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> finalBestSolutionConsumer, BiConsumer<? super ProblemId_,? super Throwable> exceptionHandler)
solve(Object, Function, Consumer)
.problemId
- never null, a ID for each planning problem. This must be unique.
Use this problemId to terminate
the solver early,
to get the status
or if the problem changes while solving.problemFinder
- never null, function that returns a PlanningSolution
, 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.default SolverJob<Solution_,ProblemId_> solveAndListen(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer)
Thread
, as soon as one is available.
When the solver finds a new best solution, the bestSolutionConsumer
is called every time,
on a consumer Thread
, 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)
.
problemId
- never null, a ID for each planning problem. This must be unique.
Use this problemId to terminate
the solver early,
to get the status
or if the problem changes while solving.problemFinder
- never null, a function that returns a PlanningSolution
, usually with uninitialized planning
variablesbestSolutionConsumer
- never null, called multiple times, on a consumer threaddefault SolverJob<Solution_,ProblemId_> solveAndListen(ProblemId_ problemId, Function<? super ProblemId_,? extends Solution_> problemFinder, Consumer<? super Solution_> bestSolutionConsumer, BiConsumer<? super ProblemId_,? super Throwable> exceptionHandler)
solveAndListen(Object, Function, Consumer)
.problemId
- never null, a ID for each planning problem. This must be unique.
Use this problemId to terminate
the solver early,
to get the status
or if the problem changes while solving.problemFinder
- never null, function that returns a PlanningSolution
, 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.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)
solveAndListen(Object, Function, Consumer)
.
The final best solution is delivered twice:
first to the bestSolutionConsumer
when it is found
and then again to the finalBestSolutionConsumer
when the solver terminates.
Do not store the solution twice.
This allows for use cases that only process the Score
first (during best solution changed events)
and then store the solution upon termination.
problemId
- never null, an ID for each planning problem. This must be unique.
Use this problemId to terminate
the solver early,
to get the status
or if the problem changes while solving.problemFinder
- never null, function that returns a PlanningSolution
, 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.SolverStatus getSolverStatus(ProblemId_ problemId)
Solver
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, use SolverJob.getSolverStatus()
instead.
Here, that distinction is not supported because it would cause a memory leak.
problemId
- never null, a value given to solve(Object, Function, Consumer)
or solveAndListen(Object, Function, Consumer)
void terminateEarly(ProblemId_ problemId)
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 (on a consumer thread),
before this method returns.
problemId
- never null, a value given to solve(Object, Function, Consumer)
or solveAndListen(Object, Function, Consumer)
void close()
ProblemFactChange
s.
Releases all thread pool resources.
No new planning problems can be submitted after calling this method.
close
in interface AutoCloseable
Copyright © 2006–2021 JBoss by Red Hat. All rights reserved.