org.cpsolver.ifs.solution

## Class Solution<V extends Variable<V,T>,T extends Value<V,T>>

- java.lang.Object
- - org.cpsolver.ifs.solution.Solution<V,T>

- Type Parameters:
    `V` - Variable
    `T` - Value

      public class Solution<V extends Variable<V,T>,T extends Value<V,T>>
      extends Object

  Generic solution.
  It consist from the model and information about current iteration and solution time.
  Version:
    IFS 1.3 (Iterative Forward Search)
    Copyright (C) 2006 - 2014 Tomáš Müller
    muller@unitime.org
    http://muller.unitime.org
    This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
    This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    You should have received a copy of the GNU Lesser General Public License along with this library; if not see http://www.gnu.org/licenses/.
  See Also:
    `Model`, `Solver`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Solution(Model<V,T> model)` Deprecated.
    `Solution(Model<V,T> model, Assignment<V,T> assignment)` Constructor
    `Solution(Model<V,T> model, Assignment<V,T> assignment, long iteration, double time)` Constructor

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addSolutionListener(SolutionListener<V,T> listener)` Adds solution listener
    `void` | `clearBest()` Clear the best ever found solution
    `Assignment<V,T>` | `getAssignment()` The assignment associated with this solution
    `long` | `getBestFailedIterations()` Number of failed iterations (i.e., number of calls `update(double, boolean)` with false success) in the best solution
    `int` | `getBestIndex()` Index of the best assignment.
    `Map<String,String>` | `getBestInfo()` Info of the best ever found solution
    `long` | `getBestIteration()` Iteration when the best ever found solution was found
    `double` | `getBestPerturbationsPenalty()` Perturbation penalty of the best ever found solution (see `PerturbationsCounter`)
    `double` | `getBestTime()` Solution time when the best ever found solution was found
    `double` | `getBestValue()` Total value of the best ever found solution -- sum of all assigned values (see `Value.toDouble(Assignment)`).
    `Map<String,String>` | `getExtendedInfo()` Extended solution information.
    `long` | `getFailedIterations()` Number of failed iterations (i.e., number of calls `update(double, boolean)` with false success)
    `Map<String,String>` | `getInfo()` Solution information.
    `Map<String,String>` | `getInfo(Collection<V> variables)` Solution information.
    `long` | `getIteration()` Current iteration
    `ReadWriteLock` | `getLock()` Return solution lock
    `Model<V,T>` | `getModel()` The model associated with the solution
    `PerturbationsCounter<V,T>` | `getPerturbationsCounter()` Returns perturbation counter
    `List<SolutionListener<V,T>>` | `getSolutionListeners()` Registered of solution listeners
    `double` | `getTime()` Current solution time (time in seconds from the start of the solver)
    `void` | `init(Solver<V,T> solver)` Initialization
    `boolean` | `isBestComplete()` Returns true, if all variables of the best ever solution found are assigned
    `boolean` | `isComplete()` True if the solution is complete, i.e., all the variables are assigned
    `void` | `removeSolutionListener(SolutionListener<V,T> listener)` Removes solution listener
    `void` | `restoreBest()` Restore the best ever found solution into the current solution (it also calls `Model.restoreBest(Assignment)`)
    `void` | `saveBest()` Save the current solution as the best ever found solution (it also calls `Model.saveBest(Assignment)`)
    `void` | `saveBest(Solution<V,T> master)` Save the current solution as the best ever found solution (it also calls `Model.saveBest(Assignment)`)
    `boolean` | `saveBestIfImproving(Solution<V,T> master, SolutionComparator<V,T> comparator)`
    `void` | `setAssignment(Assignment<V,T> assignment)` Set a new assignment
    `void` | `setBestValue(double bestValue)` Set total value of the best ever found solution
    `String` | `toString()` String representation -- returns a list of values of objective criteria
    `void` | `update(double time)` Update time, increment current iteration
    `void` | `update(double time, boolean success)` Update time, increment current iteration
    `void` | `update(double time, boolean success, Solution<V,T> master)` Update time, increment current iteration
    `void` | `update(double time, Solution<V,T> master)` Update time, increment current iteration

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

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

- - ### Constructor Detail

    - #### Solution

          @Deprecated
          public Solution(Model<V,T> model)

      Deprecated.
      Constructor
      Parameters:
        `model` - problem model

    - #### Solution

          public Solution(Model<V,T> model,
                          Assignment<V,T> assignment)

      Constructor
      Parameters:
        `model` - problem model
        `assignment` - current assignment

    - #### Solution

          public Solution(Model<V,T> model,
                          Assignment<V,T> assignment,
                          long iteration,
                          double time)

      Constructor
      Parameters:
        `model` - problem model
        `assignment` - current assignment
        `iteration` - current iteration
        `time` - current solver time

  - ### Method Detail

    - #### getIteration

          public long getIteration()

      Current iteration
      Returns:
        current iteration

    - #### getFailedIterations

          public long getFailedIterations()

      Number of failed iterations (i.e., number of calls `update(double, boolean)` with false success)
      Returns:
        number of failed iterations

    - #### getBestFailedIterations

          public long getBestFailedIterations()

      Number of failed iterations (i.e., number of calls `update(double, boolean)` with false success) in the best solution
      Returns:
        number of failed iterations in the best solution

    - #### getModel

          public Model<V,T> getModel()

      The model associated with the solution
      Returns:
        problem model

    - #### getAssignment

          public Assignment<V,T> getAssignment()

      The assignment associated with this solution
      Returns:
        current assignment

    - #### setAssignment

          public void setAssignment(Assignment<V,T> assignment)

      Set a new assignment
      Parameters:
        `assignment` - current assignment

    - #### getTime

          public double getTime()

      Current solution time (time in seconds from the start of the solver)
      Returns:
        solver time

    - #### update

          public void update(double time,
                             boolean success,
                             Solution<V,T> master)

      Update time, increment current iteration
      Parameters:
        `time` - updated solver time
        `success` - true if the last iteration was successful
        `master` - master solution

    - #### update

          public void update(double time,
                             boolean success)

      Update time, increment current iteration
      Parameters:
        `time` - updated solver time
        `success` - true if the last iteration was successful

    - #### update

          public void update(double time,
                             Solution<V,T> master)

      Update time, increment current iteration
      Parameters:
        `time` - updated solver time
        `master` - master solution

    - #### update

          public void update(double time)

      Update time, increment current iteration
      Parameters:
        `time` - updated solver time

    - #### init

          public void init(Solver<V,T> solver)

      Initialization
      Parameters:
        `solver` - current solver

    - #### toString

          public String toString()

      String representation -- returns a list of values of objective criteria
      Overrides:
        `toString` in class `Object`
      Returns:
        comma separated string of `AbstractCriterion.toString(Assignment)`

    - #### getInfo

          public Map<String,String> getInfo()

      Solution information. It consists from info from the model which is associated with the solution, time, iteration, speed and infos from all solution listeners.
      Returns:
        info table

    - #### getExtendedInfo

          public Map<String,String> getExtendedInfo()

      Extended solution information. Similar to `getInfo()`, but some more information (that is more expensive to compute) might be added. Also extended model information is added (see `Model.getExtendedInfo(Assignment)`) into the resultant table.
      Returns:
        extended info table

    - #### getInfo

          public Map<String,String> getInfo(Collection<V> variables)

      Solution information. It consists from info from the model which is associated with the solution, time, iteration, speed and infos from all solution listeners. Only variables from the given set are included.
      Parameters:
        `variables` - sub-problem
      Returns:
        info table

    - #### getBestInfo

          public Map<String,String> getBestInfo()

      Info of the best ever found solution
      Returns:
        info table of the best solution

    - #### getBestIteration

          public long getBestIteration()

      Iteration when the best ever found solution was found
      Returns:
        iteration of the best solution

    - #### getBestTime

          public double getBestTime()

      Solution time when the best ever found solution was found
      Returns:
        solver time of the best solution

    - #### isBestComplete

          public boolean isBestComplete()

      Returns true, if all variables of the best ever solution found are assigned
      Returns:
        true if the best solution has all the variables assigned

    - #### getBestIndex

          public int getBestIndex()

      Index of the best assignment.
      Returns:
        `Assignment.getIndex()` of the best saved solution

    - #### getBestValue

          public double getBestValue()

      Total value of the best ever found solution -- sum of all assigned values (see `Value.toDouble(Assignment)`).
      Returns:
        value of the best solution

    - #### setBestValue

          public void setBestValue(double bestValue)

      Set total value of the best ever found solution
      Parameters:
        `bestValue` - value of the best solution

    - #### getBestPerturbationsPenalty

          public double getBestPerturbationsPenalty()

      Perturbation penalty of the best ever found solution (see `PerturbationsCounter`)
      Returns:
        perturbation penalty of the best solution

    - #### getPerturbationsCounter

          public PerturbationsCounter<V,T> getPerturbationsCounter()

      Returns perturbation counter
      Returns:
        perturbations counter

    - #### clearBest

          public void clearBest()

      Clear the best ever found solution

    - #### isComplete

          public boolean isComplete()

      True if the solution is complete, i.e., all the variables are assigned
      Returns:
        true if all the variables are assigned

    - #### saveBest

          public void saveBest(Solution<V,T> master)

      Save the current solution as the best ever found solution (it also calls `Model.saveBest(Assignment)`)
      Parameters:
        `master` - master solution into which information about the best solution are to be copied as well

    - #### saveBestIfImproving

          public boolean saveBestIfImproving(Solution<V,T> master,
                                             SolutionComparator<V,T> comparator)

    - #### saveBest

          public void saveBest()

      Save the current solution as the best ever found solution (it also calls `Model.saveBest(Assignment)`)

    - #### restoreBest

          public void restoreBest()

      Restore the best ever found solution into the current solution (it also calls `Model.restoreBest(Assignment)`)

    - #### addSolutionListener

          public void addSolutionListener(SolutionListener<V,T> listener)

      Adds solution listener
      Parameters:
        `listener` - a solution listener

    - #### removeSolutionListener

          public void removeSolutionListener(SolutionListener<V,T> listener)

      Removes solution listener
      Parameters:
        `listener` - a solution listener

    - #### getSolutionListeners

          public List<SolutionListener<V,T>> getSolutionListeners()

      Registered of solution listeners
      Returns:
        list of registered solution listener

    - #### getLock

          public ReadWriteLock getLock()

      Return solution lock
      Returns:
        read-write lock used to lock the solution during a change