Skip to main content

MiniZinc

MiniZinc is a high-level constraint modelling language that allows you to easily express and solve discrete optimisation problems.

LiveCodes runs MiniZinc in the browser using WebAssembly.

Basic Demo

show code
import { createPlayground } from 'livecodes';

const options = {
"params": {
"minizinc": "% Soduku\n\ninclude \"globals.mzn\";\n\nany: board = [|\n 5, 3, <>, <>, 7, <>, <>, <>, <> |\n 6, <>, <>, 1, 9, 5, <>, <>, <> |\n <>, 9, 8, <>, <>, <>, <>, 6, <> |\n\n 8, <>, <>, <>, 6, <>, <>, <>, 3 |\n 4, <>, <>, 8, <>, 3, <>, <>, 1 |\n 7, <>, <>, <>, 2, <>, <>, <>, 6 |\n\n <>, 6, <>, <>, <>, <>, 2, 8, <> |\n <>, <>, <>, 4, 1, 9, <>, <>, 5 |\n <>, <>, <>, <>, 8, <>, <>, 7, 9\n|];\n\narray [1..9, 1..9] of var 1..9: solution;\n\n% Given numbers are fixed\nconstraint forall (i, j in 1..9) (solution[i, j] ~= board[i, j]);\n\n% Rows are all different\nconstraint forall (i in 1..9) (all_different(solution[i, ..]));\n% Columns are all different\nconstraint forall (j in 1..9) (all_different(solution[.., j]));\n\n% Subgrids are all different\nconstraint forall (i, j in 1..3) (\n all_different(solution[\n 3 * (i - 1) + 1 .. 3 * i,\n 3 * (j - 1) + 1 .. 3 * j\n ])\n);\n\nsolve satisfy;\n",
"console": "full"
}
};
createPlayground('#container', options);

See below for more advanced examples.

Usage

By default the output is logged to the integrated console. In addition, helper methods are available to access solve progress and solution from JavaScript.

Usage from JavaScript

Helper methods are available in the browser global livecodes.minizinc object. They allow interacting with the JavaScript interface for MiniZinc.

The following methods are available:

  • livecodes.minizinc.init: A method that returns a promise that resolves when the MiniZinc environment is loaded. This should be used before calling livecodes.minizinc.solve.
  • livecodes.minizinc.getSolvers: A method that returns a promise that resolves to an array of available MiniZinc solvers.
  • livecodes.minizinc.run: A method that returns a promise that resolves to the final solution/statistics/status. It optionally accepts a data object (see below) as an argument.
  • livecodes.minizinc.solve: This method should only be run after livecodes.minizinc.init() resolves. It returns a solve progress object, which can be used to listen to events during solving, and can be awaited to retrieve the final solution/statistics/status. This method also optionally accepts a data object (see below) as an argument.

Data Object

The data object can be used to pass data to the MiniZinc environment, such as dzn or json. It can also pass configuration object. It has the following type definition:

interface MiniZincData {
dzn?: string;
json?: string;
config?: {
jsonOutput?: boolean;
options?: {
solver?: string;
"time-limit"?: number;
statistics?: boolean;
"all-solutions"?: boolean;
// ... other MiniZinc options
};
};
}

Example

show code
import { createPlayground } from 'livecodes';

const options = {
"template": "minizinc"
};
createPlayground('#container', options);

Language Info

Name

minizinc

Extension

mzn

Editor

script

Compiler

The official JavaScript port for MiniZinc which uses WebAssembly.

Version

minizinc v4.4.4

Code Formatting

Using a MiniZinc plugin for the Prettier formatter.

Limitations

Currently, visualisations are not supported out-of-the-box. However, using the helper methods, and based on official implementations, it should be possible to create custom visualisations.

Example:

show code
import { createPlayground } from 'livecodes';

const options = {
"import": "id/xj98m7cfpnv"
};
createPlayground('#container', options);

Starter Template

https://livecodes.io/?template=minizinc