|
| 1 | +# MiniZinc |
| 2 | + |
| 3 | +[MiniZinc](https://www.minizinc.org/) is a high-level constraint modelling language that allows you to easily express and solve discrete optimisation problems. |
| 4 | + |
| 5 | +LiveCodes runs MiniZinc in the browser using WebAssembly. |
| 6 | + |
| 7 | +## Basic Demo |
| 8 | + |
| 9 | +import LiveCodes from '../../src/components/LiveCodes.tsx'; |
| 10 | + |
| 11 | +export const params = { |
| 12 | + 'minizinc': `% Soduku |
| 13 | +
|
| 14 | +include "globals.mzn"; |
| 15 | +
|
| 16 | +any: board = [| |
| 17 | + 5, 3, <>, <>, 7, <>, <>, <>, <> | |
| 18 | + 6, <>, <>, 1, 9, 5, <>, <>, <> | |
| 19 | + <>, 9, 8, <>, <>, <>, <>, 6, <> | |
| 20 | +
|
| 21 | + 8, <>, <>, <>, 6, <>, <>, <>, 3 | |
| 22 | + 4, <>, <>, 8, <>, 3, <>, <>, 1 | |
| 23 | + 7, <>, <>, <>, 2, <>, <>, <>, 6 | |
| 24 | +
|
| 25 | + <>, 6, <>, <>, <>, <>, 2, 8, <> | |
| 26 | + <>, <>, <>, 4, 1, 9, <>, <>, 5 | |
| 27 | + <>, <>, <>, <>, 8, <>, <>, 7, 9 |
| 28 | +|]; |
| 29 | +
|
| 30 | +array [1..9, 1..9] of var 1..9: solution; |
| 31 | +
|
| 32 | +% Given numbers are fixed |
| 33 | +constraint forall (i, j in 1..9) (solution[i, j] ~= board[i, j]); |
| 34 | +
|
| 35 | +% Rows are all different |
| 36 | +constraint forall (i in 1..9) (all_different(solution[i, ..])); |
| 37 | +% Columns are all different |
| 38 | +constraint forall (j in 1..9) (all_different(solution[.., j])); |
| 39 | +
|
| 40 | +% Subgrids are all different |
| 41 | +constraint forall (i, j in 1..3) ( |
| 42 | + all_different(solution[ |
| 43 | + 3 * (i - 1) + 1 .. 3 * i, |
| 44 | + 3 * (j - 1) + 1 .. 3 * j |
| 45 | + ]) |
| 46 | +); |
| 47 | +
|
| 48 | +solve satisfy; |
| 49 | +`, |
| 50 | + console: 'full', |
| 51 | +}; |
| 52 | + |
| 53 | +<LiveCodes params={params}></LiveCodes> |
| 54 | + |
| 55 | +See below for more advanced examples. |
| 56 | + |
| 57 | +## Usage |
| 58 | + |
| 59 | +By default the output is logged to the integrated console. |
| 60 | +In addition, helper methods are available to access solve progress and solution from JavaScript. |
| 61 | + |
| 62 | +### Usage from JavaScript |
| 63 | + |
| 64 | +Helper methods are available in the browser global `livecodes.minizinc` object. |
| 65 | +They allow interacting with the [JavaScript interface for MiniZinc](https://js.minizinc.dev/). |
| 66 | + |
| 67 | +The following methods are available: |
| 68 | + |
| 69 | +- `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`. |
| 70 | +- `livecodes.minizinc.getSolvers`: A method that returns a promise that resolves to an array of available MiniZinc solvers. |
| 71 | +- `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. |
| 72 | +- `livecodes.minizinc.solve`: This method should only be run after `livecodes.minizinc.init()` resolves. It returns a [solve progress object](https://js.minizinc.dev/docs/stable/interfaces/SolveProgress.html), 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. |
| 73 | + |
| 74 | +#### Data Object |
| 75 | + |
| 76 | +The data object can be used to pass data to the MiniZinc environment, such as dzn or json. |
| 77 | +It can also pass configuration object. It has the following type definition: |
| 78 | + |
| 79 | +```ts |
| 80 | +interface MiniZincData { |
| 81 | + dzn?: string; |
| 82 | + json?: string; |
| 83 | + config?: { |
| 84 | + jsonOutput?: boolean; |
| 85 | + options?: { |
| 86 | + solver?: string; |
| 87 | + "time-limit"?: number; |
| 88 | + statistics?: boolean; |
| 89 | + "all-solutions"?: boolean; |
| 90 | + // ... other MiniZinc options |
| 91 | + }; |
| 92 | + }; |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +#### Example |
| 97 | + |
| 98 | +<LiveCodes template='minizinc' height="80vh"></LiveCodes> |
| 99 | + |
| 100 | +## Language Info |
| 101 | + |
| 102 | +### Name |
| 103 | + |
| 104 | +`minizinc` |
| 105 | + |
| 106 | +### Extension |
| 107 | + |
| 108 | +`mzn` |
| 109 | + |
| 110 | +### Editor |
| 111 | + |
| 112 | +`script` |
| 113 | + |
| 114 | +## Compiler |
| 115 | + |
| 116 | +The official [JavaScript port](https://js.minizinc.dev/) for MiniZinc which uses WebAssembly. |
| 117 | + |
| 118 | +### Version |
| 119 | + |
| 120 | +`minizinc` v4.4.4 |
| 121 | + |
| 122 | +## Code Formatting |
| 123 | + |
| 124 | +Using a [MiniZinc plugin](https://github.com/live-codes/prettier-plugin-minizinc) for the [Prettier](https://prettier.io/) formatter. |
| 125 | + |
| 126 | + |
| 127 | +## Limitations |
| 128 | + |
| 129 | +Currently, [visualisations](https://docs.minizinc.dev/en/stable/visualisation.html) are not supported out-of-the-box. |
| 130 | +However, using the [helper methods](#usage-from-javascript), and based on [official implementations](https://github.com/MiniZinc/libminizinc/tree/master/share/minizinc/std/ide), it should be possible to create custom visualisations. |
| 131 | + |
| 132 | +Example: |
| 133 | + |
| 134 | +<LiveCodes import='id/xj98m7cfpnv' height="80vh"></LiveCodes> |
| 135 | + |
| 136 | +## Starter Template |
| 137 | + |
| 138 | +https://livecodes.io/?template=minizinc |
| 139 | + |
| 140 | +## Links |
| 141 | + |
| 142 | +- [MiniZinc](https://www.minizinc.org/) |
| 143 | +- [MiniZinc documentation](https://docs.minizinc.dev/en/stable/) |
| 144 | +- [MiniZinc JavaScript port](https://js.minizinc.dev/) |
| 145 | +- [MiniZinc tutorial](https://docs.minizinc.dev/en/stable/part_2_tutorial.html) |
0 commit comments