10.48.2 FlatZinc

The FlatZinc interpreter described here is based on “Specification of FlatZinc, version 1.6”, available at http://www.minizinc.org/specifications.html.

A FlatZinc program can be run directly using fzn_run_file/[1,2] and fzn_run_stream/[1,2], as well as with spfz, a simple command-line tool interface to fzn_run_file/[1,2] (for details, see too-spfz). For example, a program for solving the 4 Queens problem, located in library('zinc/examples/queen4.fzn'), can be run by the following goal:

| ?- fzn_run_file(library('zinc/examples/queen4')).

or command:

% spfz $SP_LIBRARY_DIR/zinc/examples/queen4

The following solution is then written on the current output stream:

q = array1d(1..4, [2, 4, 1, 3]);

Note the ten consecutive dashes after the solution.

The following goal can be used to find all solutions:

| ?- fzn_run_file(library('zinc/examples/queen4'), [solutions(all)]).

or command:

% spfz $SP_LIBRARY_DIR/zinc/examples/queen4 -a

The following solutions are then written on the current output stream:

q = array1d(1..4, [2, 4, 1, 3]);
q = array1d(1..4, [3, 1, 4, 2]);

Note the ten consecutive equal signs after all solutions have been found.

FlatZinc programs are not intended to be written (or read) by humans, but rather to be automatically generated. One way to generate a FlatZinc program is by using a MiniZinc-to-FlatZinc translator, such as mzn2fzn, bundles with the MiniZinc distribution. One use of this translator is to first generate a FlatZinc program from a MiniZinc program, e.g. by the following command line (queen.mzn and queen4.dat can be found in library('zinc/examples')):

mzn2fzn -G sicstus --data queen4.dat --output-to-file queen4.fzn queen.mzn

The resulting FlatZinc program queen4.fzn can then be run as described above. If a generated FlatZinc program is not desired, another use of mzn2fzn is to pipe its result directly to a SICStus process, e.g. by the following command:

mzn2fzn -G sicstus --data queen4.dat --output-to-stdout queen.mzn | sicstus --goal 'use_module(library(zinc)), fzn_run_stream(user_input), halt.'

or, simpler:

minizinc -G sicstus --data queen4.dat queen.mzn -f spfz

or, even simpler:

minizinc -G sicstus -D n=4 queen.mzn -f spfz

or, simpler still:

mzn-sicstus -D n=4 queen.mzn

Please note: The translator mzn2fzn needs to be made aware of the SICStus specific global constraint definitions, located in library('zinc/globals'). This is necessary in order for the MiniZinc-to-FlatZinc translation to be SICStus specific. If this is not done, the SICStus interpreter may run the resulting FlatZinc program significantly slower. In the commands given above, this is done by the option -G sicstus, which requires a symbolic link e.g. /opt/minizinc-2.0.10/share/minizinc/sicstus pointing at library('zinc/globals'). If you are unable to add such a link, an alternative to -G sicstus is to pass the path to library('zinc/globals') in the --search-dir option of mzn2fzn.

It is also possible to just load a FlatZinc program into SICStus by fzn_load_file/2 and fzn_load_stream/2. The loaded FlatZinc program can then be processed further from within SICStus, e.g. by retrieving some FlatZinc variables using fzn_identifier/3 and posting additional library(clpfd) constraints or applying a Prolog labeling predicate on those variables.

Finally, it is also possible to load and run MiniZinc programs directly from within SICStus by using the predicates described in MiniZinc. These predicates all rely on the availability of an external MiniZinc-to-FlatZinc translator such as mzn2fzn, as well as an external solution printer such as solns2out (see MiniZinc).

Send feedback on this subject.