Class Solution
- java.lang.Object
-
- org.chocosolver.solver.Solution
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SolutioncopySolution()booleanexists()intgetIntVal(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.Solutionrecord()Records the current solution of the solver clears all previous recordingsvoidrestore()Restore the solution inmodel.voidsetIntVal(IntVar var, int val)Set the value of variable v in this solution.voidsetRealBounds(RealVar var, double[] val)Set the value of variable v in this solutionvoidsetSetVal(SetVar var, int[] val)Set the value of variable v in this solutionStringtoString()
-
-
-
Constructor Detail
-
Solution
public Solution(Model model, Variable... varsToStore)
Create an empty solution object able to store the value of each variable invarsToStorewhen callingrecord()Stores all variables by default, whenvarsToStoreis 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
-
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 ContradictionExceptionRestore 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
SolverExceptionwill 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
ContradictionExceptionwill be thrown. If so, the propagation engine is not flushed automatically, and a call toIPropagationEngine#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.
-
-