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 LSB 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 LSB 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.

  • in the LSP format : 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.

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 LSB and LSP, 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 LSP file, you can give a name to any LSExpression you have defined (either a decision variable or an intermediate expression). This name will then appear in the model.

To export your LSP environment or model, you can use the function LSModel.saveEnvironment with the name and extension you want (either .lsp, .lsb, .lsp.gz or .lsb.gz).

use localsolver;

function main() {
    with(ls = localsolver.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;

        ls.model.close();
        ls.saveEnvironment("export.lsp");
    }
}

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

import localsolver;

with localsolver.LocalSolver() as ls:
    model = ls.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()
    ls.save_environment("export.lsp")

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

#include "localsolver.h"

using namespace localsolver;

int main(int argc, char** argv) {
    LocalSolver ls;
    LSModel model = ls.getModel();

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

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

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

    model.close();
    ls.saveEnvironment("export.lsp");

    return 0;
}

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

using System;
using localsolver;

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

    public static void Main(string[] args)
    {
        LocalSolver ls = new LocalSolver();

        LSModel model = ls.GetModel();

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

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

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

        model.Close();
        ls.SaveEnvironment("export.lsp");
    }
}

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

import localsolver.*;

public class Test {
    public static void main(String[] args) {
        try (LocalSolver ls = new LocalSolver()) {
            LSModel model = ls.getModel();

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

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

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

            model.close();
            ls.saveEnvironment("export.lsp");

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