Solving your first model with Hexaly Modeler¶
In this section, we show you how to model and solve your first problem: the optimization of the shape of a bucket. With a limited surface of material (S=π), we try to build a bucket that holds the largest volume.
This small example is more precisely described in our example tour. Here our main goal is to learn how to write and launch a model.
Writing the model¶
Below is a Hexaly program which models this non linear problem (see examples/optimal_bucket).
use io;
/* Declare the optimization model */
function model() {
PI = 3.14159265359;
// Numerical decisions
R <- float(0, 1);
r <- float(0, 1);
h <- float(0, 1);
// Surface must not exceed the surface of the plain disc
surface <- PI * pow(r, 2) + PI * (R + r) * sqrt(pow(R - r, 2) + pow(h, 2));
constraint surface <= PI;
// Maximize the volume
volume <- PI * h / 3 * (pow(R, 2) + R * r + pow(r, 2));
maximize volume;
}
/* Parametrize the solver */
function param() {
if (hxTimeLimit == nil) hxTimeLimit = 2;
}
/* Write the solution in a file with the following format:
* - surface and volume of the bucket
* - values of R, r and h */
function output() {
if (solFileName == nil) return;
local solFile = io.openWrite(solFileName);
solFile.println(surface.value, " ", volume.value);
solFile.println(R.value, " ", r.value, " ", h.value);
}
All the variables of the model, called expressions, are declared using left
arrows <-
. Decision variables are introduced using the built-in function
float()
(or also bool()
, int()
, set()
, list()
). Intermediate
expressions can be built upon these decision variables by using other operators
or functions. For example, in the model above: power (pow
), square root
(sqrt
), less than or equal to (<=
). Many other mathematical operators
are available, allowing you to model and solve highly-nonlinear combinatorial
optimization problems. The keywords constraint
or maximize
are used for
tagging expressions as constrained or maximized.
Launching the model¶
To solve this model, call Hexaly Optimizer with the HXM file as argument.
On Windows:
hexaly %HX_HOME%\examples\optimal_bucket\optimal_bucket.hxm
Note that on Windows, in a PowerShell window you would use the following line:
hexaly $env:HX_HOME\examples\optimal_bucket\optimal_bucket.hxm
On Linux or Mac OS the command line looks like this:
hexaly /opt/hexaly_|major|_|minor|/examples/optimal_bucket/optimal_bucket.hxm
Then, the following trace will appear in your console:
Hexaly Optimizer 12.5.20240516-Win64. All rights reserved.
Load optimal_bucket.hxm...
Run model...
Run param...
Run solver...
Model: expressions = 26, decisions = 3, constraints = 1, objectives = 1
Param: time limit = 2 sec, no iteration limit
[objective direction ]: maximize
[ 0 sec, 0 itr]: 0
[ optimality gap ]: 100%
[ 0 sec, 42898 itr]: 0.68709
[ optimality gap ]: < 0.01%
42898 iterations performed in 0 seconds
Optimal solution:
obj = 0.68709
gap = < 0.01%
bounds = 0.687189
If no time limit is set, the search will continue until optimality is proven
(Optimal solution
message) or until you force the stop of the program by
pressing Ctrl+C
. The trace in console starts with the key
figures of the model: number of expressions, decisions, constraints
and objectives.
Once the search is finished, the total number of iterations and the elapsed time
are displayed, as well as the status and the value of the best solution
found. The solution status can be Inconsistent
, Infeasible
,
Feasible
or Optimal
.