LocalSolver logo
is now
Hexaly logo

We're excited to share that we are moving forward. We're leaving behind the LocalSolver brand and transitioning to our new identity: Hexaly. This represents a leap forward in our mission to enable every organization to make better decisions faster when faced with operational and strategic challenges.


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. Interval variables can be initialized by assigning a range to the value attribute in the LSP modeler:

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

    // 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 or an interval decision 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 a numeric decision
sol.set_value(exp, 0)
exp.value = 1

# With intervalExpr an interval decision
sol.set_value(intervalExpr, LSInterval(1, 5))
intervalExpr.value = LSInterval(1, 5)

# With listExpr a list decision
col = listExpr.get_value()
col.clear()
col.add(2)
col.add(0)
col.add(3)

Setting the value of a numeric decision is done with setIntValue or setValue for int and boolean decisions or with setDoubleValue for float decisions or with setIntervalValue for interval 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");
LSExpression intervalExpr = ls.getModel().getExpression("i");

sol.setValue(intExpr, 12ll);
intExpr.setValue(12ll);

sol.setIntValue(intExpr, 12ll);
intExpr.setIntValue(12ll);

sol.setDoubleValue(dblExpr, 4.8);
dblExpr.setDoubleValue(4.8);

sol.setIntervalValue(intervalExpr, LSInterval(1, 5));
intervalExpr.setIntervalValue(LSInterval(1, 5));

LSCollection col = listExpr.getCollectionValue();
col.clear();
col.add(2);
col.add(0);
col.add(3);

Setting the value of a numeric decision is done with SetIntValue or SetValue for int and boolean decisions or with SetDoubleValue for float decisions or with SetIntervalValue for interval 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");
LSExpression intervalExpr = ls.GetModel().GetExpression("i");

sol.SetValue(intExpr, 12);
intExpr.SetValue(12);

sol.SetIntValue(intExpr, 12);
intExpr.SetIntValue(12);

sol.SetDoubleValue(dblExpr, 4.8);
dblExpr.SetDoubleValue(4.8);

sol.SetIntervalValue(intervalExpr, new LSInterval(1, 5));
intervalExpr.SetIntervalValue(new LSInterval(1, 5));

LSCollection col = listExpr.GetCollectionValue();
col.Clear();
col.Add(2);
col.Add(0);
col.Add(3);

Setting the value of a numeric decision is done with setIntValue or setValue for int and boolean decisions or with setDoubleValue for float decisions or with setIntervalValue for interval 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");
LSExpression intervalExpr = ls.getModel().getExpression("i");

sol.setValue(intExpr, 12);
intExpr.setValue(12);

sol.setIntValue(intExpr, 12);
intExpr.setIntValue(12);

sol.setDoubleValue(dblExpr, 4.8);
dblExpr.setDoubleValue(4.8);

sol.setIntervalValue(intervalExpr, new LSInterval(1, 5));
intervalExpr.setIntervalValue(new LSInterval(1, 5));

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.