- mzn2fzn(+ModelFile, +InstFileOrParMap, ++SolverOrOptions, ?FznFile)
- Convert a MiniZinc model into a FlatZinc model
- mzn_load(++ModelFile, ++SolverOrOptions, ++InstFileOrParMap, +VarMap, -FznState)
- Load a MiniZinc model from a file
- mzn_load_string(++MznModel, ++SolverOrOptions, ++ParMap, +VarMap, -FznState)
- Load a MiniZinc model given as a string or list
- mzn_run
- Run a MiniZinc model using command line arguments
- mzn_run(+File, ++SolverOrOptions)
- Run a MiniZinc model from a given file
- mzn_run(+ModelFile, +InstFileOrParMap, ++SolverOrOptions)
- Run a MiniZinc model from a given model and instance file
- mzn_run_string(++MznModel, ++SolverOrOptions)
- Run a MiniZinc model given as a string or list
- reexport struct(_) from flatzinc
- export macro(of / 2, tr_of / 2, [])
This module allows to run MiniZinc models with ECLiPSe. MiniZinc models can be either read from a file or stream, or they can be embedded as strings into ECLiPSe code. The implementation relies on an external MiniZinc-to-FlatZinc converter, e.g. mzn2fzn, and on the FlatZinc interpreter lib(flatzinc). Mappings to different ECLiPSe solvers are possible via the solver mapping libraries fzn_ic, fzn_fd, fzn_eplex, etc.
You can run a MiniZinc model by first converting it to FlatZinc yourself, and then using the lib(flatzinc) library. This can be done either via an intermediate .fzn file, or by piping the resulting FlatZinc model into the ECLiPSe-FlatZinc interpreter using e.g.
% mzn2fzn --output-to-stdout model.mzn | eclipse -e "flatzinc:fzn_run(fzn_ic)"This should work as long as the mzn2fzn command is in your PATH. Note that mzn2fzn is currently not included with ECLiPSe but comes with the Melbourne MiniZinc distribution. You must also make sure that the correct specialised global constraint definitions are used, by including e.g. lib/fzn_ic in mzn2fzn's search path via its -I option. For more details see lib(flatzinc).
This library allows you to do everything from within ECLiPSe and let ECLiPSe invoke the MiniZinc to FlatZinc translator (mzn2fzn) internally with the correct arguments. The model can be contained in a file:
?- mzn_run("model.mzn", fzn_ic).or, if a data instance file is used
?- mzn_run("model.mzn", "instance.dzn", fzn_ic).Since MiniZinc models are typically small, they can also be embedded as a string into ECLiPSe code. For example:
queens8 :- mzn_run_string(" int: n = 8; array [1..n] of var 1..n: q; constraint forall (i in 1..n, j in i+1..n) ( q[i] != q[j] /\\ q[i] + i != q[j] + j /\\ q[i] - i != q[j] - j ); solve satisfy; ", fzn_ic).Note that, because of the rules for escaping characters within ECLiPSe strings, the backslashes had to be doubled!
This version is intended to to work with Minizinc 1.6 or later!
In order to be found by lib(minizinc), the Melbourne Minizinc-to-Flatzinc
converter mzn2fzn (and the corresponding output processor solns2out) must be
installed in a directory called minizinc-<version>
(or similar)
in one of the following locations (where we write <ECLIPSEDIR> for
the ECLiPSe installation directory, and <ECLIPSEARCH> for
the name for the machine architecture, e.g. i386_nt for Windows, i386_linux
for Linux):
$ECLIPSEMZN
environment variable<location of lib(minizinc)>/<ECLIPSEARCH>
<ECLIPSEDIR>/lib_public/<ECLIPSEARCH>
<ECLIPSEDIR>/lib/<ECLIPSEARCH>
<ECLIPSEDIR>
<ECLIPSEDIR>
(e.g. "C:/Program Files" on Windows)$PROGRAMFILES
environment variable
You can also set the environment variable ECLIPSEMZN (on Windows alternatively
the registry entry HKLM/SOFTWARE/IC-Parc/Eclipse/
There are several reasons why one might want to embed a MiniZinc model
into an ECLiPSe program:
Combining a MiniZinc model with Search or I/O in ECLiPSe
To pass a parameter into a MiniZinc model, a generic MiniZinc model must be provided, together with a parameter map. This map is an ECLiPSe list that corresponds to a MiniZinc (actually FlatZinc) instance file:
queens(N) :- mzn_run_string(" int: n; array [1..n] of var 1..n: q; constraint forall (i in 1..n, j in i+1..n) ( q[i] != q[j] /\\ q[i] + i != q[j] + j /\\ q[i] - i != q[j] - j ); solve satisfy; ", [n=N], % parameter map: ZincId=EclipseValue fzn_ic).Alternatively, the generic model can be kept separately in a MiniZinc file:
queens(N) :- mzn_run("n_queens.mzn", [n=N], fzn_ic).
With the above exmples, search and output are still completely specified in MiniZinc.
To add your own search routine and/or output, use mzn_load_string/5 or mzn_load/5. This has the effect of only loading the MiniZinc model (i.e. setting up the constraints), but then returning to ECLiPSe without executing any MiniZinc solve or output primitives. The rest of the work can then be done in ECLiPSe:
queens(N, Q) :- mzn_load("n_queens.mzn", fzn_ic, [n=N], [q=Q], FznState), labeling(Q), fzn_output(FznState).The [q=Q] mapping gives access to the ECLiPSe array Q corresponding to the MiniZinc array q. This is a normal ECLiPSe array of lib(ic) domain variables, and can be used for doing search, or outputting the results. In the example however, we have fallen back onto the FlatZinc output routine to display the results after search has finished.
Note that even if you do your own search in ECLiPSe, your MiniZinc model must contain a solve item to be syntactically correct (and to specify the objective, if any).
fzn_ic
in
our examples), a zn_options{}
structure can be given to
customize the behaviour further, e.g.
mzn_run(File, zn_options{solver:fzn_eplex,var_names:on}.
When using ECLiPSe with a Mini/FlatZinc model, one needs to be aware of the mapping from MiniZinc to FlatZinc (e.g. flattening of arrays), and the representation of FlatZinc data in ECLiPSe.
Note that the ECLiPSe-side representation depends in part on the chosen solver mapping. The following table shows the mapping used with fzn_ic (which employs the lib(ic) and lib(ic_sets) solver libraries):
FlatZinc Type/Syntax ECLiPSe Type/Syntax ----------------------------------------------------------- string string e.g. "abc" "abc" bool (false/true) integer (0/1) e.g. false 0 int integer e.g. 33 33 float float or breal e.g. 3.4 3.399__3.401 set of int ordered list of integer e.g. {1,5,4} [1,4,5] 1..3 [1,2,3] array[1..N] of T structure with functor []/N e.g. [23,54,0] [](23,54,0) var bool lib(ic) integer variable var int lib(ic) integer variable var float lib(ic) continuous variable var set of int lib(ic_sets) set variable