Setting an initial solutionΒΆ
LocalSolver does not need a starting solution to launch its algorithms.
However in some cases you may want to force LocalSolver to start from a specific solution. For instance a planning system may consist in reoptimizing every morning the current planning (inserting new tasks and taking into account updated deadlines). In such a case passing an intial solution as input is natural.
Such an intialization will be achieved in LocalSolver by setting the value of
decision variables. For numeric decision variables (boolean, integers and
floats) it is done with the value
attribute in the LSP modeler. For
collection decision variable (lists) we use the add
and clear
functions on this value:
function param() {
// with x an int, y a float and z a list
x.value = 3;
y.value = 4.3;
z.value.clear();
z.value.add(2);
z.value.add(0);
}
Note that only decision variables can be initialized: setting the value of any
other expression will throw an exception. Besides, it is not necessary to set
the values of all decision variables. On the contrary in can make sense to set
the values of some of the decision variables, while relyling on LocalSolver to
initialize other values. It is also possible to intialize values to an
infeasible solution, that is to say a solution violating some of the
constraints. In this case, LocalSolver will start from this infeasible solution
and quickly move to a feasible solution. The only requirement is that a
decision variable cannot be given a value outside of its domain. For instance
an integer decision defined as int(3,10)
cannot be given value 15 and a
list cannot be initialized to a collection with duplicated values.
In the APIs, the principle is the same.
Setting the value of a numeric expression is done with set_value
(on the solution or on the expression). Lists are modified with
add
and clear
.
# With sol a solution and exp and expression
sol.set_value(exp, 0)
exp.set_value(1)
# With listExpr a list variable
col = listExpr.get_value()
col.clear()
col.add(2)
col.add(0)
col.add(3)
Setting the value of a numeric expression is done with setIntValue
or setValue
for int and boolean decisions or with setDoubleValue
for float decisions. Lists are modified with add
and clear
.
// With ls a LocalSolver object
LSSolution sol = ls.getSolution();
LSExpression intExpr = ls.getModel().getExpression("x");
LSExpression dblExpr = ls.getModel().getExpression("y");
LSExpression listExpr = ls.getModel().getExpression("z");
sol.setValue(intExpr, 12ll);
intExpr.setValue(12ll);
sol.setIntValue(intExpr, 12ll);
intExpr.setIntValue(12ll);
sol.setDoubleValue(dblExpr, 4.8);
dblExpr.setDoubleValue(4.8);
LSCollection col = listExpr.getCollectionValue();
col.clear();
col.add(2);
col.add(0);
col.add(3);
Setting the value of a numeric expression is done with setIntValue
or setValue
for int and boolean decisions or with setDoubleValue
for float decisions. Lists are modified with add
and clear
.
// with ls a LocalSolver object
LSSolution sol = ls.getSolution();
LSExpression intExpr = ls.getModel().getExpression("x");
LSExpression dblExpr = ls.getModel().getExpression("y");
LSExpression listExpr = ls.getModel().getExpression("z");
sol.setValue(intExpr, 12);
intExpr.setValue(12);
sol.setIntValue(intExpr, 12);
intExpr.setIntValue(12);
sol.setDoubleValue(dblExpr, 4.8);
dblExpr.setDoubleValue(4.8);
LSCollection col = listExpr.getCollectionValue();
col.clear();
col.add(2);
col.add(0);
col.add(3);
Setting the value of a numeric expression is done with SetIntValue
or SetValue
for int and boolean decisions or with SetDoubleValue
for float decisions. Lists are modified with Add
and Clear
.
// With ls a LocalSolver object
LSSolution sol = ls.GetSolution();
LSExpression intExpr = ls.GetModel().GetExpression("x");
LSExpression dblExpr = ls.GetModel().GetExpression("y");
LSExpression listExpr = ls.GetModel().GetExpression("z");
sol.SetValue(intExpr, 12);
intExpr.SetValue(12);
sol.SetIntValue(intExpr, 12);
intExpr.SetIntValue(12);
sol.SetDoubleValue(dblExpr, 4.8);
dblExpr.SetDoubleValue(4.8);
LSCollection col = listExpr.GetCollectionValue();
col.Clear();
col.Add(2);
col.Add(0);
col.Add(3);