Up: MiniZinc   [Contents][Index]


10.55.3.1 Exported Predicates

The first two predicates can be used to run a MiniZinc program in one go.

mzn_run_file(+MznFile)
mzn_run_file(+MznFile, +Options)

MznFile is a MiniZinc program file and Options is a list of options; see see Zinc Options. Runs the MiniZinc program on MznFile, and writes the result onto the file given in the option/1 option if given, or onto the current output stream otherwise. This is done by first calling minizinc -c, and then interpreting its output with fzn_run_stream/[1,2] (see FlatZinc Exported Predicates). Fails if the constraints of the MiniZinc program are inconsistent.

mzn_run_model(+MznModel)
mzn_run_model(+MznModel, +Options)

MznModel is a MiniZinc program specified by a list of strings as explained below and Options is a list of options; see see Zinc Options. Runs the MiniZinc program MznModel, and writes the result onto the file given in the option/1 option if given, or onto the current output stream otherwise. This is done by first calling minizinc -c and interpreting its output with fzn_run_stream/[1,2] (see FlatZinc Exported Predicates). The MiniZinc program specification MznModel must be a list of strings (list of character codes) where each element must specify one line of the MiniZinc program. For example, a MiniZinc program for the N Queens problem can be specified as follows:

NQueens = ["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;",
           "output [\"A solution to the \", show(n),",
           "\" Queens problem: \", show(q), \"\\n\"];"]

Note that backslashes and double quotes must be escaped with an additional backslash.

Consider the following MiniZinc program for solving the N Queens problem located in library('zinc/examples/queen.mzn'):

queen.mzn
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;

output ["A solution to the ", show(n), " Queens problem: ", show(q), "\n"];

Consider now the following goal at the Prolog top level:

| ?- mzn_run_file(library('zinc/examples/queen'),
                  [data_file(library('zinc/examples/queen4.dzn'))]).

Since library('zinc/examples/queen4.dzn') contains the single line

n = 4;

the following is written on the current output stream:

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

The initialization n = 4 can also be passed using the parameter/1 option. So the following goal is equivalent to the one above:

| ?- mzn_run_file(library('zinc/examples/queen'), [parameters([n=4])]).

Finally, the following goal finds all solutions to the 4 Queens problem:

| ?- mzn_run_file(library('zinc/examples/queen'),
                  [parameters([n=4]), solutions(all)]).

Given this goal, the following is 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]
----------
==========

The next two predicates can be used to construct a FlatZinc state (see FlatZinc Exported Predicates).

mzn_load_file(+MznFile, -FznState)
mzn_load_file(+MznFile, +Options, -FznState)

MznFile is a MiniZinc program file and Options is a list of options; see see Zinc Options. Initializes a FlatZinc state FznState with respect to MznFile. May fail if post(true) and the constraints are inconsistent.

mzn_load_model(+MznModel, -FznState)
mzn_load_model(+MznModel, +Options, -FznState)

MznModel is a MiniZinc program specified by a list of strings as explained for mzn_run_model/[1,2] above and Options is a list of options; see see Zinc Options. Initializes a FlatZinc state FznState with respect to MznModel. May fail if post(true) and the constraints are inconsistent.

The following Prolog goal constructs a FlatZinc state representing the 4 Queens problem:

| ?- mzn_load_file(library('zinc/examples/queen'),
                   [parameters([n=4])], Queen4State).

See FlatZinc Exported Predicates for more information on FlatZinc states and how they can be queried. A very useful option to mzn_load_file/3 and mzn_load_model/3 is the variables/1 option, which can be used to unify values of MiniZinc identifiers with Prolog variables (this option can be used in place of several calls to fzn_identifier/3). For example, the following goal posts an additional symmetry breaking constraint and labels the variables using a Prolog goal that finds all remaining solutions to the 4 Queens problem:

| ?- mzn_load_file(library('zinc/examples/queen'),
                   [parameters([n=4]), variables([q=Q])],
                   Queen4State),
     Q = [Q1, Q2|_], Q1 #< Q2,
     findall(_, (labeling([], Q), fzn_output(Queen4State)), _).

Given this goal, the following is written on the current output stream:

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

Finally, the following predicate can be used to translate a MiniZinc file to a FlatZinc by a direct call to minizinc -c.

mzn_to_fzn(+MznFile, +FznFile)
mzn_to_fzn(+MznFile, +Options, +FznFile)

MznFile is a MiniZinc program file and Options is a list of options; see see Zinc Options. Calls minizinc -c, the result of which is written to FznFile.



Send feedback on this subject.