- java.lang.Object
-
- org.chocosolver.solver.Solution
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Solution
copySolution()
boolean
exists()
int
getIntVal(IntVar v)
Get the value of variable v in this solution.double[]
getRealBounds(RealVar r)
Get the bounds of r in this solution.int[]
getSetVal(SetVar s)
Get the value of variable s in this solution.Solution
record()
Records the current solution of the solver clears all previous recordingsvoid
restore()
Restore the solution inmodel
.java.util.List<BoolVar>
retrieveBoolVars()
Iterate over the variable ofthis
and build a list that contains theBoolVar
only.java.util.List<IntVar>
retrieveIntVars(boolean includeBoolVar)
Iterate over the variable ofthis
and build a list that contains all theIntVar
of the solution.java.util.List<RealVar>
retrieveRealVars()
Iterate over the variable ofthis
and build a list that contains theRealVar
only.java.util.List<SetVar>
retrieveSetVars()
Iterate over the variable ofthis
and build a list that contains theSetVar
only.void
setIntVal(IntVar var, int val)
Set the value of variable v in this solution.void
setRealBounds(RealVar var, double[] val)
Set the value of variable v in this solutionvoid
setSetVal(SetVar var, int[] val)
Set the value of variable v in this solutionjava.lang.String
toString()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.chocosolver.solver.ICause
explain, forEachIntVar
-
-
-
-
Constructor Detail
-
Solution
public Solution(Model model, Variable... varsToStore)
Create an empty solution object able to store the value of each variable invarsToStore
when callingrecord()
Stores all variables by default, whenvarsToStore
is empty- Parameters:
model
- model of the solutionvarsToStore
- 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
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.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
- SetVarval
- 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
- RealVarval
- its value
-
restore
public void restore() throws ContradictionException
Restore the solution inmodel
. 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 callgetIntVal(IntVar)
,getSetVal(SetVar)
and/orgetRealBounds(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 toPropagationEngine.flush()
may be needed. However, the satisfaction of the solution status is not check (seeSettings.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 java.util.List<IntVar> retrieveIntVars(boolean includeBoolVar)
-
retrieveBoolVars
public java.util.List<BoolVar> retrieveBoolVars()
Iterate over the variable ofthis
and build a list that contains theBoolVar
only.- Returns:
- array of
BoolVar
inthis
solution
-
retrieveSetVars
public java.util.List<SetVar> retrieveSetVars()
Iterate over the variable ofthis
and build a list that contains theSetVar
only. It also contains FIXED variables and VIEWS, if any.- Returns:
- array of SetVars in
this
model
-
-