This page is for an old version of Hexaly Optimizer. We recommend that you update your version and read the documentation for the latest stable release.

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 initial solution as input is natural.

Such an initialization 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 variables (lists and sets), we can either initialize them by direcly including all the values in braces or use the add and clear functions on this value:

function param() {
    // x an int, y a float
    x.value = 3;
    y.value = 4.3;

    // z a list or set
    z.value = {2, 0};
    // or add elements one by one in the collection
    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 it can make sense to set the values of some of the decision variables, while relying on LocalSolver to initialize other values. It is also possible to initialize 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 through the value attribute of an expression or the set_value function of the solution. Lists are modified with add and clear.

# With sol a solution and exp an expression
sol.set_value(exp, 0)
exp.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);

Note

The actual initial solution when you solve the model can actually differ from the one you’ve just set. During its preprocessing, LocalSolver may have tightened the bounds of some decision variables. As a consequence, it will start with a projection of your initial solution on these tighter bounds.

Setting an initial solution can only be done when the model is closed. In the LSP modeler, it means it can only be done in the param function. You can also set a solution after the solve in the output function if you want to retrieve its objective value or to check its feasibility status. In the APIs, you can set up an initial solution as long as the close() function has been called on the model.