Main class for solving MiniZinc instances.

This API allows you to add .mzn, .dzn, .json and .mpc files using the addFile() method, and then run MiniZinc on the files using the solve() method.

Code can also be added programmatically using the addString() (and similar) methods.

Example

const model = new MiniZinc.Model();
// Add a file with a given name and string contents
model.addFile('test.mzn', 'var 1..3: x; int: y;');
// Add model code from a string
model.addString('int: z;');
// Add data in DZN format
model.addDznString('y = 1;');
// Add data from a JSON object
model.addJSON({z: 2});

const solve = model.solve({
options: {
solver: 'gecode',
timeout: 10000,
statistics: true
}
});

// You can listen for events
solve.on('solution', solution => console.log(solution));
solve.on('statistics', stats => console.log(stats.statistics));

// And/or wait until complete
solve.then(result => {
console.log(result.solution);
console.log(result.statistics);
});

Hierarchy

  • Model

Constructors

  • Create a new model.

    Example

    const model = new MiniZinc.Model();
    

    Returns Model

Methods

  • Adds a snippet of data to the model.

    Note that each snippet is used as a complete data file.

    Example

    model.addDznString("x = 1;");
    

    Parameters

    • dzn: string

      DataZinc input as a string

    Returns string

    The filename of the snippet (may be useful to identify sources of errors)

  • Makes the given string contents available to MiniZinc using the given filename.

    Example

    /// Add this file to the MiniZinc command
    model.addFile("model.mzn", `
    include "foo.mzn";
    var 1..3: x;
    `);
    // Make this file available, but don't add it to the MiniZinc command
    model.addFile("foo.mzn", "var 1..3: y;", false);

    This method is generally only used from the browser.

    Parameters

    • filename: string

      The file name to use

    • contents: string

      The contents of the file

    • Optional use: boolean

      Whether to add this file as an argument to the MiniZinc command

    Returns void

  • Adds the given file to the model.

    Example

    model.addFile("./path/to/model.mzn");
    

    Only available using the native version of MiniZinc in NodeJS.

    Parameters

    • filename: string

      The file name to use

    Returns void

  • Adds data to the model in JSON format.

    Note that each snippet is used as a complete JSON data file.

    Example

    model.addJson({
    y: 1.5
    });

    Parameters

    • data: object

      The data as an object in MiniZinc JSON data input format

    Returns string

    The filename of the snippet (may be useful to identify sources of errors)

  • Add a snippet of code to the model.

    Note that each snippet is used as a complete model file.

    Example

    model.addString("var 1..3: x;");
    model.addString("float: y;");

    Parameters

    • model: string

      MiniZinc code as a string

    Returns string

    The filename of the snippet (may be useful to identify sources of errors)

  • Check for errors in the model using --model-check-only.

    Example

    const errors = model.check({
    options: {
    solver: 'gecode'
    }
    });
    for (const error of errors) {
    console.log(error.what, error.message);
    }

    Parameters

    • config: {
          options?: ParamConfig;
      }

      Configuration options

      • Optional options?: ParamConfig

        Options to pass to MiniZinc in parameter configuration file format

    Returns Promise<ErrorMessage[]>

    The errors in the model

  • Create a clone of this model.

    Example

    const m1 = new MiniZinc.Model();
    m1.addFile('test.mzn', `
    var 1..3: x;
    int: y;
    `);
    const m2 = m1.clone();
    // Both m1 and m2 have test.mzn

    // Add different data to each model
    m1.addJson({
    y: 1
    });
    m2.addJson({
    y: 2
    });

    Returns Model

  • Compile this model to FlatZinc.

    Example

    const compile = model.compile({
    options: {
    solver: 'gecode',
    statistics: true
    }
    });

    // Print compilation statistics when received
    compile.on('statistics', e => console.log(e.statistics));

    // Wait for completion
    compile.then(console.log);

    Parameters

    • config: {
          options?: ParamConfig;
      }

      Configuration options

      • Optional options?: ParamConfig

        Options to pass to MiniZinc in parameter configuration file format

    Returns CompilationProgress

  • Get the model interface using --model-interface-only.

    Example

    model.interface({
    options: {
    solver: 'gecode'
    }
    }).then(console.log);

    Parameters

    • config: {
          options?: ParamConfig;
      }

      Configuration options

      • Optional options?: ParamConfig

        Options to pass to MiniZinc in parameter configuration file format

    Returns Promise<ModelInterface>

    The model interface

  • Solve this model and retrieve the result.

    Example

    // Begin solving
    const solve = model.solve({
    options: {
    solver: 'gecode',
    'all-solutions': true
    }
    });

    // Print each solution as it is produced
    solve.on('solution', e => console.log(e.output));

    // Wait for completion
    solve.then(result => {
    console.log(result.status);
    });

    Parameters

    • config: {
          jsonOutput?: boolean;
          options: ParamConfig;
      }

      Configuration options

      • Optional jsonOutput?: boolean

        Whether to use --output-mode json (true by default)

      • options: ParamConfig

        Options to pass to MiniZinc in parameter configuration file format

    Returns SolveProgress

Generated using TypeDoc