MiniZinc JavaScript
    Preparing search index...

    Class Model

    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.

    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',
    'time-limit': 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);
    });
    Index

    Constructors

    • Create a new model.

      Returns Model

      const model = new MiniZinc.Model();
      

    Methods

    • Adds a snippet of data to the model.

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

      Parameters

      • dzn: string

        DataZinc input as a string

      Returns string

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

      model.addDznString("x = 1;");
      
    • Makes the given string contents available to MiniZinc using the given filename.

      Parameters

      • filename: string

        The file name to use

      • contents: string

        The contents of the file

      • Optionaluse: boolean

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

      Returns void

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

    • Adds the given file to the model.

      Parameters

      • filename: string

        The file name to use

      Returns void

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

      Only available using the native version of MiniZinc in NodeJS.

    • Adds data to the model in JSON format.

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

      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)

      model.addJson({
      y: 1.5
      });
    • Add a snippet of code to the model.

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

      Parameters

      • model: string

        MiniZinc code as a string

      Returns string

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

      model.addString("var 1..3: x;");
      model.addString("float: y;");
    • Check for errors in the model using --model-check-only.

      Parameters

      • config: { options?: ParamConfig }

        Configuration options

        • Optionaloptions?: ParamConfig

          Options to pass to MiniZinc in parameter configuration file format

      Returns Promise<ErrorMessage[]>

      The errors in the model

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

      Returns Model

      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
      });
    • Compile this model to FlatZinc.

      Parameters

      • config: { options?: ParamConfig }

        Configuration options

        • Optionaloptions?: ParamConfig

          Options to pass to MiniZinc in parameter configuration file format

      Returns CompilationProgress

      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);
    • Get the model interface using --model-interface-only.

      Parameters

      • config: { options?: ParamConfig }

        Configuration options

        • Optionaloptions?: ParamConfig

          Options to pass to MiniZinc in parameter configuration file format

      Returns Promise<ModelInterface>

      The model interface

      model.interface({
      options: {
      solver: 'gecode'
      }
      }).then(console.log);
    • Solve this model and retrieve the result.

      Parameters

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

        Configuration options

        • OptionaljsonOutput?: boolean

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

        • options: ParamConfig

          Options to pass to MiniZinc in parameter configuration file format

      Returns SolveProgress

      // 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);
      });