Constructor and Description |
---|
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 |
Modifier and Type | Method and Description |
---|---|
Solution |
copySolution() |
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 recordings
|
void |
restore()
Restore the solution in
model . |
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 solution
|
void |
setSetVal(SetVar var,
int[] val)
Set the value of variable v in this solution
|
String |
toString() |
public Solution(Model model, Variable... varsToStore)
varsToStore
when calling record()
Stores all variables by default, when varsToStore
is emptymodel
- model of the solutionvarsToStore
- variables to store in this objectpublic Solution record()
public Solution copySolution()
public int getIntVal(IntVar v)
v
- IntVar (or BoolVar)SolverException
- if v was not instantiated during solution recording.public void setIntVal(IntVar var, int val)
var
- IntVar (or BoolVar)val
- its valuepublic int[] getSetVal(SetVar s)
s
- SetVarSolverException
- if v was not instantiated during solution recording.public void setSetVal(SetVar var, int[] val)
var
- SetVarval
- its valuepublic double[] getRealBounds(RealVar r)
r
- RealVarSolverException
- if v was not instantiated during solution recording.public void setRealBounds(RealVar var, double[] val)
var
- RealVarval
- its valuepublic void restore() throws ContradictionException
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 IPropagationEngine#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();
SolverException
- if a variable was not instantiated during solution recording.ContradictionException
- if restoring the solution leads to failureCopyright © 2018. All rights reserved.