10.38.2 FlatZinc

The FlatZinc interpreter described here is based on “Specification of FlatZinc, version 0.8.1”, available at http://www.g12.csse.unimelb.edu.au/minizinc/specifications.html.

A FlatZinc program can be run directly using fzn_run_file/[1,2] and fzn_run_stream/[1,2]. 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')).

The following is then written on the current output stream:

     A solution to the 4 Queens problem: [2, 4, 1, 3]
     1 solution found

The following goal can be used to find all solutions:

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

The following is then written on the current output stream:

     A solution to the 4 Queens problem: [2, 4, 1, 3]
     A solution to the 4 Queens problem: [3, 1, 4, 2]
     2 solutions 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, provided by the G12 project. One usage 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 --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 usage of mzn2fzn is to pipe its result directly to a SICStus process, e.g. by the following command line:

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

Please note: The translator mzn2fzn should be made aware of the SICStus globals.mzn file, located in library(zinc). This can be done in two ways:

This is necessary in order for the MiniZinc-to-FlatZinc translation to be SICStus specific. If this is not the case the SICStus interpreter may run the resulting FlatZinc program significantly slower.

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 (see MiniZinc).

Send feedback on this subject.