Class Solution

  • All Implemented Interfaces:
    ICause

    public class Solution
    extends Object
    implements ICause
    Class which stores the value of each variable in a solution
    Since:
    05/06/2013
    Author:
    Jean-Guillaume Fages, Charles Prud'homme
    • Constructor Detail

      • Solution

        public Solution​(Model model,
                        Variable... varsToStore)
        Create an empty solution object able to store the value of each variable in varsToStore when calling record() Stores all variables by default, when varsToStore is empty
        Parameters:
        model - model of the solution
        varsToStore - variables to store in this object
    • Method Detail

      • record

        public Solution record()
        Records the current solution of the solver clears all previous recordings
        Returns:
        this object
      • copySolution

        public Solution copySolution()
      • getIntVal

        public int getIntVal​(IntVar v)
        Get the value of variable v in this solution. If v was not instantiated during solution recording, calling this method will throw an exception.
        Parameters:
        v - IntVar (or BoolVar)
        Returns:
        the value of variable v in this solution, or null if the variable is not instantiated in the solution
        Throws:
        SolverException - if v was not instantiated during solution recording.
      • setIntVal

        public void setIntVal​(IntVar var,
                              int val)
        Set the value of variable v in this solution.
        Parameters:
        var - IntVar (or BoolVar)
        val - its value
      • getSetVal

        public int[] getSetVal​(SetVar s)
        Get the value of variable s in this solution. If v was not instantiated during solution recording, calling this method will throw an exception.
        Parameters:
        s - SetVar
        Returns:
        the value of variable s in this solution, or null if the variable is not instantiated in the solution.
        Throws:
        SolverException - if v was not instantiated during solution recording.
      • setSetVal

        public void setSetVal​(SetVar var,
                              int[] val)
        Set the value of variable v in this solution
        Parameters:
        var - SetVar
        val - its value
      • getRealBounds

        public double[] getRealBounds​(RealVar r)
        Get the bounds of r in this solution. If v was not instantiated during solution recording, calling this method will throw an exception.
        Parameters:
        r - RealVar
        Returns:
        the bounds of r in this solution, or null if the variable is not instantiated in the solution
        Throws:
        SolverException - if v was not instantiated during solution recording.
      • setRealBounds

        public void setRealBounds​(RealVar var,
                                  double[] val)
        Set the value of variable v in this solution
        Parameters:
        var - RealVar
        val - its value
      • restore

        public void restore()
                     throws ContradictionException
        Restore the solution in model. Restoring a solution in a model consists in iterating over model's variables and forcing each of them to be instantiated to the value recorded in this solution.

        If a variable was not instantiated while this solution was recorded, then a SolverException will be thrown (indeed, forcing this instantiation will call getIntVal(IntVar), getSetVal(SetVar) and/or getRealBounds(RealVar).

        When instantiating all variables to their value in the solution, a propagation loop will be achieved to ensure that the correctness and completeness of the model. If the propagation detects a failure, a ContradictionException will be thrown. If so, the propagation engine is not flushed automatically, and a call to PropagationEngine.flush() may be needed. However, the satisfaction of the solution status is not check (see Settings.checkModel(Solver) to check satisfaction).

        Restoring a solution is permanent except if a backtrack occurs. Note that, for a backtrack to be feasible, it needs to be anticipated, by calling IEnvironment.worldPush():

             
             // optional: for assertion only
             int wi = model.getEnvironment().getWorldIndex();
             // prepare future backtrack, in order to forget solution
             model.getEnvironment().worldPush();
             // restore the solution in `model`
             solution.restore();
             // ... do something
             // backtrack to before solution restoration
             model.getEnvironment().worldPop();
             // optional: for assertion only
             assert wi == model.getEnvironment().getWorldIndex();
             
         

        Throws:
        SolverException - if a variable was not instantiated during solution recording.
        ContradictionException - if restoring the solution leads to failure
      • exists

        public boolean exists()
        Returns:
        true if a solution has been recorded into this, false otherwise.
      • retrieveIntVars

        public List<IntVar> retrieveIntVars​(boolean includeBoolVar)
        Iterate over the variable of this and build a list that contains all the IntVar of the solution. excludes BoolVar if includeBoolVar=false.
        Parameters:
        includeBoolVar - indicates whether or not to include BoolVar
        Returns:
        array of IntVar in this solution
      • retrieveBoolVars

        public List<BoolVar> retrieveBoolVars()
        Iterate over the variable of this and build a list that contains the BoolVar only.
        Returns:
        array of BoolVar in this solution
      • retrieveSetVars

        public List<SetVar> retrieveSetVars()
        Iterate over the variable of this and build a list that contains the SetVar only. It also contains FIXED variables and VIEWS, if any.
        Returns:
        array of SetVars in this model
      • retrieveRealVars

        public List<RealVar> retrieveRealVars()
        Iterate over the variable of this and build a list that contains the RealVar only.
        Returns:
        array of RealVar in this solution