org.optaplanner.core.impl.solver

## Class DefaultSolver<Solution_>

- java.lang.Object
- - org.optaplanner.core.impl.solver.AbstractSolver<Solution_>
  - - org.optaplanner.core.impl.solver.DefaultSolver<Solution_>

- Type Parameters:
    `Solution_` - the solution type, the class with the `PlanningSolution` annotation
  All Implemented Interfaces:
    Solver<Solution_>

      public class DefaultSolver<Solution_>
      extends AbstractSolver<Solution_>

  Default implementation for `Solver`.
  See Also:
    `Solver`, `AbstractSolver`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected BasicPlumbingTermination` | `basicPlumbingTermination`
    `protected EnvironmentMode` | `environmentMode`
    `protected RandomFactory` | `randomFactory`
    `protected DefaultSolverScope<Solution_>` | `solverScope`
    `protected AtomicBoolean` | `solving`

    - ### Fields inherited from class org.optaplanner.core.impl.solver.AbstractSolver

      `bestSolutionRecaller, logger, phaseLifecycleSupport, phaseList, solverEventSupport, termination`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `DefaultSolver(EnvironmentMode environmentMode, RandomFactory randomFactory, BestSolutionRecaller<Solution_> bestSolutionRecaller, BasicPlumbingTermination basicPlumbingTermination, Termination termination, List<Phase<Solution_>> phaseList, DefaultSolverScope<Solution_> solverScope)`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `addProblemFactChange(ProblemFactChange<Solution_> problemFactChange)` Schedules a `ProblemFactChange` to be processed.
    `boolean` | `addProblemFactChanges(List<ProblemFactChange<Solution_>> problemFactChangeList)` Schedules multiple `ProblemFactChange`s to be processed.
    `String` | `explainBestScore()` Returns a diagnostic text that explains the `Solver.getBestSolution()` through the `ConstraintMatch` API to identify which constraints or planning entities cause that `Solver.getBestScore()` quality.
    `Score` | `getBestScore()` Returns the `Score` of the `Solver.getBestSolution()`.
    `Solution_` | `getBestSolution()` The best solution is the `best solution` found during solving: it might or might not be optimal, feasible or even initialized.
    `BestSolutionRecaller<Solution_>` | `getBestSolutionRecaller()`
    `EnvironmentMode` | `getEnvironmentMode()`
    `List<Phase<Solution_>>` | `getPhaseList()`
    `RandomFactory` | `getRandomFactory()`
    `InnerScoreDirectorFactory<Solution_>` | `getScoreDirectorFactory()` Useful to reuse the `Score` calculation (for example in a UI) and to explain the `Score` to the user with the `ConstraintMatchTotal` and `Indictment` API.
    `DefaultSolverScope<Solution_>` | `getSolverScope()`
    `long` | `getTimeMillisSpent()` Returns the amount of milliseconds spent solving since the last start.
    `boolean` | `isEveryProblemFactChangeProcessed()` Checks if all scheduled `ProblemFactChange`s have been processed.
    `boolean` | `isSolving()` This method is thread-safe.
    `boolean` | `isTerminateEarly()` This method is thread-safe.
    `void` | `outerSolvingEnded(DefaultSolverScope<Solution_> solverScope)`
    `void` | `outerSolvingStarted(DefaultSolverScope<Solution_> solverScope)`
    `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).
    `void` | `solvingEnded(DefaultSolverScope<Solution_> solverScope)`
    `void` | `solvingStarted(DefaultSolverScope<Solution_> solverScope)`
    `boolean` | `terminateEarly()` 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`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### environmentMode

          protected EnvironmentMode environmentMode

    - #### randomFactory

          protected RandomFactory randomFactory

    - #### basicPlumbingTermination

          protected BasicPlumbingTermination basicPlumbingTermination

    - #### solving

          protected final AtomicBoolean solving

    - #### solverScope

          protected final DefaultSolverScope<Solution_> solverScope

  - ### Constructor Detail

    - #### DefaultSolver

          public DefaultSolver(EnvironmentMode environmentMode,
                               RandomFactory randomFactory,
                               BestSolutionRecaller<Solution_> bestSolutionRecaller,
                               BasicPlumbingTermination basicPlumbingTermination,
                               Termination termination,
                               List<Phase<Solution_>> phaseList,
                               DefaultSolverScope<Solution_> solverScope)

  - ### Method Detail

    - #### getEnvironmentMode

          public EnvironmentMode getEnvironmentMode()

    - #### getRandomFactory

          public RandomFactory getRandomFactory()

    - #### getScoreDirectorFactory

          public InnerScoreDirectorFactory<Solution_> getScoreDirectorFactory()

      Description copied from interface: `Solver`
      Useful to reuse the `Score` calculation (for example in a UI) and to explain the `Score` to the user with the `ConstraintMatchTotal` and `Indictment` API.
      Returns:
        never null

    - #### getBestSolutionRecaller

          public BestSolutionRecaller<Solution_> getBestSolutionRecaller()

    - #### getPhaseList

          public List<Phase<Solution_>> getPhaseList()

    - #### getSolverScope

          public DefaultSolverScope<Solution_> getSolverScope()

    - #### getBestSolution

          public Solution_ getBestSolution()

      Description copied from interface: `Solver`
      The best solution is the `best solution` found 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 (although `SolverEventListener.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 uninitialized `PlanningSolution` with a `Score` null.

    - #### getBestScore

          public Score getBestScore()

      Description copied from interface: `Solver`
      Returns the `Score` of the `Solver.getBestSolution()`.

      This is useful for generic code, which doesn't know the type of the `PlanningSolution` to retrieve the `Score` from the `Solver.getBestSolution()` easily.

      This method is thread-safe.

      Returns:
        null if the `PlanningSolution` is still uninitialized

    - #### explainBestScore

          public String explainBestScore()

      Description copied from interface: `Solver`
      Returns a diagnostic text that explains the `Solver.getBestSolution()` through the `ConstraintMatch` API to identify which constraints or planning entities cause that `Solver.getBestScore()` quality. In case of an `infeasible` solution, 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 retrieve `ScoreDirector.getConstraintMatchTotalMap()` and `ScoreDirector.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: `Solver`
      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 `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: `Solver`
      This method is thread-safe.
      Returns:
        true if the `Solver.solve(Solution_)` method is still running.

    - #### terminateEarly

          public boolean terminateEarly()

      Description copied from interface: `Solver`
      Notifies the solver that it should stop at its earliest convenience. This method returns immediately, but it takes an undetermined time for the `Solver.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: `Solver`
      This method is thread-safe.
      Returns:
        true if terminateEarly has been called since the `Solver` started.
      See Also:
        `Future.isCancelled()`

    - #### addProblemFactChange

          public boolean addProblemFactChange(ProblemFactChange<Solution_> problemFactChange)

      Description copied from interface: `Solver`
      Schedules a `ProblemFactChange` to be processed.

      As a side-effect, this restarts the `Solver`, effectively resetting all `Termination`s, but not `Solver.terminateEarly()`.

      This method is thread-safe. Follows specifications of `BlockingQueue.add(Object)` with by default a capacity of `Integer.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_>> problemFactChangeList)

      Description copied from interface: `Solver`
      Schedules multiple `ProblemFactChange`s to be processed.

      As a side-effect, this restarts the `Solver`, effectively resetting all `Termination`s, but not `Solver.terminateEarly()`.

      This method is thread-safe. Follows specifications of `Collection.addAll(Collection)` with by default a capacity of `Integer.MAX_VALUE`.

      Parameters:
        `problemFactChangeList` - never null
      Returns:
        true (as specified by `Collection.add(E)`)
      See Also:
        `Solver.addProblemFactChange(ProblemFactChange)`

    - #### isEveryProblemFactChangeProcessed

          public boolean isEveryProblemFactChangeProcessed()

      Description copied from interface: `Solver`
      Checks if all scheduled `ProblemFactChange`s have been processed.

      This method is thread-safe.

      Returns:
        true if there are no `ProblemFactChange`s left to do

    - #### solve

          public final Solution_ solve(Solution_ problem)

      Description copied from interface: `Solver`
      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 `Termination` configuration. To terminate a `Solver` early, call `Solver.terminateEarly()`.

      Parameters:
        `problem` - never null, a `PlanningSolution`, usually its planning variables are uninitialized
      Returns:
        never null, but it can return the original, uninitialized `PlanningSolution` with a null `Score`.
      See Also:
        `Solver.terminateEarly()`

    - #### outerSolvingStarted

          public void outerSolvingStarted(DefaultSolverScope<Solution_> solverScope)

    - #### solvingStarted

          public void solvingStarted(DefaultSolverScope<Solution_> solverScope)

      Overrides:
        `solvingStarted` in class `AbstractSolver<Solution_>`

    - #### solvingEnded

          public void solvingEnded(DefaultSolverScope<Solution_> solverScope)

      Overrides:
        `solvingEnded` in class `AbstractSolver<Solution_>`

    - #### outerSolvingEnded

          public void outerSolvingEnded(DefaultSolverScope<Solution_> solverScope)

Copyright © 2006–2021 JBoss by Red Hat. All rights reserved.