lib(minizinc)



Overview


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.



Running MiniZinc Models without using this Library


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



Running MiniZinc Models using this Library


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!



Installation


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):

Directory specified by $ECLIPSEMZN environment variable
The user's home directory, as indicated by $HOME or $HOMEPATH
<location of lib(minizinc)>/<ECLIPSEARCH>
<ECLIPSEDIR>/lib_public/<ECLIPSEARCH>
<ECLIPSEDIR>/lib/<ECLIPSEARCH>
<ECLIPSEDIR>
Parent of <ECLIPSEDIR> (e.g. "C:/Program Files" on Windows)
Directory specified by $PROGRAMFILES environment variable


You can also set the environment variable ECLIPSEMZN (on Windows alternatively
the registry entry HKLM/SOFTWARE/IC-Parc/Eclipse//ECLIPSEMZN)
to the Minizinc installation directory (or to its parent).



Combining a MiniZinc model with Search or I/O in ECLiPSe


There are several reasons why one might want to embed a MiniZinc model
into an ECLiPSe program:

Passing parameters from the ECLiPSe program to the MiniZinc model
Getting the model solutions back into ECLiPSe
Programming custom search in ECLiPSe
Doing custom output beyond what the Zinc output annotations can do


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



Options

Instead of just the name of the solver mapping (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}.


solver (default: fzn_ic)
    Determines which ECLiPSe solvers are used.  The name is the
    name of a library implementing the mapping, e.g. fzn_ic,
    fzn_fd or fzn_eplex.

solutions (default: 1)
    The maximum number of solutions computed. Only effective if using
    builtin search and not optimizing. (0 or all = all solutions)

setup_prio (default: 0)
    The priority under which the constraint setup will be executed
    (see call_priority/2 and get_priority/1). Possible values are
    the ECLiPSe priorities 1 to 12, or 0 (the default) which stands
    for the current priority of the calling code.  A sensible value
    for this option is 2, which means that the setup code is executed
    under high priority (still allowing debug/visualisation goals).
    The effect of such a setting is that no propagation occurs until
    all constraints are fully set up, possibly leading to time savings.

parser (default: fast)
    Whether to use a 'strict' or 'fast' parser for FlatZinc input.

optimize (default: on)
    If 'off', pass --no-optimize to the Minizinc-Flatzinc translator.
    This can improve performance on large models.

timeout (default: 0)
    Time limit in seconds. (0 means no timeout),

var_names (default: off)
    Use lib(var_name) to label ECLiPSe variables with their Zinc names.
    This is useful for debugging.

fzn_tmp (default: file)
    Use a 'pipe' or intermediate 'file' for FlatZinc.




Mapping between MiniZinc/FlatZinc Data and ECLiPSe Data


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




