Search code examples
minizinc

Printing part of an array in MiniZinc


I have a MiniZinc model for wolf-goat-cabbage in which I store the locations of each entity in its own array, e.g., array[1..max] of Loc: wolf where Loc is defined as an enum: enum Loc = {left, rght}; and max is the maximum possible number of steps needed, e.g., 20..

To find a shortest plan I define a variable var 1..max: len; and constrain the end state to occur at step len.

constraint farmer[len] == left /\ wolf[len] == left /\ goat[len] == left /\ cabbage[len] == left

Then I ask for

solve minimize len

I get all the right answers.

I'd like to display the arrays from 1..len, but I can't find a way to do it. When I try, for example, to include in the output:

 [ "\(wolf[n]), " | n in 1..max where n <= len ]

I get an error message saying that I can't display an array of opt string.

Is there a way to display only an initial portion of an array, where the length of the initial portion is determined by the model?

Thanks.


Solution

  • Did you try to fix the len variable in the output statement like n <= fix(len)?. See also What is the use of minizinc fix function?