Next: , Previous: , Up: lib-zinc   [Contents][Index]


10.55.2 FlatZinc

The FlatZinc interpreter described here is based on “Specification of FlatZinc, available at https://www.minizinc.org/doc-latest/en/fzn-spec.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 can be generated by the minizinc command-line tool with the -c option, for example (queen.mzn and queen4.dzn can be found in library('zinc/examples')):

minizinc --solver sicstus -c queen.mzn queen4.dzn

The resulting FlatZinc program queen4.fzn can then be run as described above. minizinc without the -c option first generates a temporary FlatZinc file, then runs it, then deletes it:

minizinc --solver sicstus queen.mzn queen4.dzn

or, simplest of all:

minizinc --solver sicstus -D n=4 queen.mzn

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 possible to load and run MiniZinc programs directly from within SICStus by using the predicates described in MiniZinc.



Send feedback on this subject.