Setting an initial solution¶
Hexaly Optimizer does not need a starting solution to launch its algorithms.
However in some cases you may want to force Hexaly Optimizer 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 Hexaly Optimizer by setting the value
of decision variables. For numeric decision variables (boolean, integers and
floats) it is done with the value
attribute in the Hexaly modeling language.
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 Hexaly modeling language:
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 Hexaly Optimizer
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, Hexaly Optimizer 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, HxInterval(1, 5))
intervalExpr.value = HxInterval(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 optimizer a HexalyOptimizer object
HxSolution sol = optimizer.getSolution();
HxExpression intExpr = optimizer.getModel().getExpression("x");
HxExpression dblExpr = optimizer.getModel().getExpression("y");
HxExpression listExpr = optimizer.getModel().getExpression("z");
HxExpression intervalExpr = optimizer.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, HxInterval(1, 5));
intervalExpr.setIntervalValue(HxInterval(1, 5));
HxCollection 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 optimizer a HexalyOptimizer object
HxSolution sol = optimizer.GetSolution();
HxExpression intExpr = optimizer.GetModel().GetExpression("x");
HxExpression dblExpr = optimizer.GetModel().GetExpression("y");
HxExpression listExpr = optimizer.GetModel().GetExpression("z");
HxExpression intervalExpr = optimizer.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 HxInterval(1, 5));
intervalExpr.SetIntervalValue(new HxInterval(1, 5));
HxCollection 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 optimizer a HexalyOptimizer object
HxSolution sol = optimizer.getSolution();
HxExpression intExpr = optimizer.getModel().getExpression("x");
HxExpression dblExpr = optimizer.getModel().getExpression("y");
HxExpression listExpr = optimizer.getModel().getExpression("z");
HxExpression intervalExpr = optimizer.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 HxInterval(1, 5));
intervalExpr.setIntervalValue(new HxInterval(1, 5));
HxCollection 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, Hexaly Optimizer 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
Hexaly 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.