Export your Hexaly model

For debugging pruposes, it can be useful to export your Hexaly model or complete environment to be able run it in different situations, or to send it to someone. Using the saveEnvironment function, and depending on your needs, you can save your model and instance either :

  • in HXB format : the complete environment will be exported, containing not only the model, but also the parameters, the solution if it had any, and so on. With an HXB file, it is possible to replay a model under the same conditions, which is useful for debugging purposes. The file produced is binary, which provides good performance and saves space. This format was also known as ‘LSB’ before Hexaly version 13.0.

  • in the HXM format (Hexaly Modeler) : only the Hexaly model will be exported. This will allow you to obtain a view of the optimization model, and possibly modify it. Bear in mind, however, that this file can be heavy for large models, and that is does not ensure reproducibility due to parameter variations from one run to another. This format was also known as ‘LSP’ before Hexaly version 13.0.

The saveEnvironment function takes the name of the resulting file as a parameter. To select a file format, simply specify it with the file suffix. The only supported file suffixes are HXB and HXM, possibly followed by .gz if you wish the file to be compressed (using the deflate algorithm).

Note that to make it easier to read an exported Hexaly Modeler file, you can give a name to any HxExpression you have defined (either a decision variable or an intermediate expression). This name will then appear in the model.

To export your environment or model, you can use the function HxModel.saveEnvironment with the name and extension you want (either .hxb, .hxm, .hxb.gz or .hxm.gz).

use hexaly;

function main() {
    with(optimizer = hexaly.create()) {
        x <- int(0, 10);
        y <- int(0, 10);
        z <- int(0, 10);

        surface <- 2 * (x * y + x * z + y * z);
        surface.name = "surface";
        constraint surface <= 150;

        volume <- x * y * z;
        volume.name = "volume";
        maximize volume;

        optimizer.model.close();
        optimizer.saveEnvironment("export.hxb.gz");
    }
}

To export your Python environment or model, you can use the function HxModel.save_environment() with the name and extension you want (either .hxb, .hxm, .hxb.gz or .hxm.gz).

from hexaly.optimizer import HexalyOptimizer;

with HexalyOptimizer() as optimizer:
    model = optimizer.model

    x = model.int(0, 10)
    y = model.int(0, 10)
    z = model.int(0, 10)

    surface = 2 * (x * y + x * z + y * z)
    surface.set_name("surface")
    model.constraint(surface <= 150)

    volume = x * y * z
    volume.set_name("volume")
    model.maximize(volume)

    model.close()
    optimizer.save_environment("export.hxb.gz")

To export your C++ environment or model, you can use the function HxModel::saveEnvironment() with the name and extension you want (either .hxb, .hxm, .hxb.gz or .hxm.gz).

#include "optimizer/hexalyoptimizer.h"

using namespace hexaly;

int main(int argc, char** argv) {
    HexalyOptimizer optimizer;
    HxModel model = optimizer.getModel();

    HxExpression x = model.intVar(0, 10);
    HxExpression y = model.intVar(0, 10);
    HxExpression z = model.intVar(0, 10);

    HxExpression surface = 2 * (x * y + x * z + y * z);
    surface.setName("surface");
    model.constraint(surface <= 150);

    HxExpression volume = x * y * z;
    volume.setName("volume");
    model.maximize(volume);

    model.close();
    optimizer.saveEnvironment("export.hxb.gz");

    return 0;
}

To export your C# environment or model, you can use the function HxModel.SaveEnvironment with the name and extension you want (either .hxb, .hxm, .hxb.gz or .hxm.gz).

using System;
using Hexaly.Optimizer;

public class Test : IDisposable
{
    public void Dispose() {}

    public static void Main(string[] args)
    {
        HexalyOptimizer optimizer = new HexalyOptimizer();

        HxModel model = optimizer.GetModel();

        HxExpression x = model.Int(0, 10);
        HxExpression y = model.Int(0, 10);
        HxExpression z = model.Int(0, 10);

        HxExpression surface = 2 * (x * y + x * z + y * z);
        surface.SetName("surface");
        model.AddConstraint(surface <= 150);

        HxExpression volume = x * y * z;
        volume.SetName("volume");
        model.Maximize(volume);

        model.Close();
        optimizer.SaveEnvironment("export.hxb.gz");
    }
}

To export your Java environment or model, you can use the function HxModel.saveEnvironment with the name and extension you want (either .hxb, .hxm, .hxb.gz or .hxm.gz).

import com.hexaly.optimizer.*;

public class Test {
    public static void main(String[] args) {
        try (HexalyOptimizer optimizer = new HexalyOptimizer()) {
            HxModel model = optimizer.getModel();

            HxExpression x = model.intVar(0, 10);
            HxExpression y = model.intVar(0, 10);
            HxExpression z = model.intVar(0, 10);

            HxExpression s1 = model.prod(x, y);
            HxExpression s2 = model.prod(x, z);
            HxExpression s3 = model.prod(y, z);
            HxExpression s4 = model.sum(s1, s2, s3);
            HxExpression surface = model.prod(model.createConstant(2), s4);
            surface.setName("surface");
            model.addConstraint(model.leq(surface, 150));

            HxExpression volume = model.prod(x, y, z);
            volume.setName("volume");
            model.maximize(volume);

            model.close();
            optimizer.saveEnvironment("export.hxb.gz");

        } catch (Exception ex) {
            System.err.println(ex);
            ex.printStackTrace();
            System.exit(1);
        }
    }
}