[ Third Party Libraries | Reference Manual | Alphabetic Index ]

# library(minizinc)

Utilities for using MiniZinc with ECLiPSe

## Predicates

> mzn2fzn(+ModelFile, +InstFileOrParMap, ++SolverOrOptions, -FznFile)
>   Convert a MiniZinc model into a FlatZinc model
> mzn_load(++ModelFile, ++SolverOrOptions, ++ParMap, +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(+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

## Reexports

> reexport struct(_) from flatzinc

## Other Exports

> export macro(of / 2, minizinc : tr_of / 2, [])

## Description

### 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 specialised globals.mzn file is used, by including e.g. lib/fzn_ic in mzn2fzn's search path. 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.mzn", 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 0.8 or later!

In order to be found by lib(minizinc), the Melbourne Minizinc-to-Flatzinc converter mzn2fzn must be installed in a directory called `minizinc-<version>` 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):

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

For MiniZinc on Windows, it may be necessary to copy a cygwin1.dll into the MiniZinc distribution's bin/private folder.

### 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 primitive 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.
solutions (default: 1)
  The maximum number of solutions computed is using builtin search. (0 = all)
parser (default: fast)
  Whether to use a 'strict' or 'fast' parser for FlatZinc input.
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

## About

- Status: prototype
- Author: Joachim Schimpf, supported by Cisco Systems and NICTA Victoria
- Copyright © Cisco Systems Inc, licensed under CMPL
- Date: $Date: 2008/06/20 17:21:25 $

## See Also

library(flatzinc), library(fzn_ic), library(fzn_fd), library(fzn_eplex)

Generated from minizinc.eci on Sat Nov 8 06:59:48 2008