Giuseppe Boezio
Giuseppe Boezio

Reputation: 111

How can I write conditional expression in output of a minizinc program?

I have the following code:

output["\(w) \(l)\n\(n)\n"] ++ [if rotation[i] then "\(x[i]) \(y[i]) \(p_x[i]) \(p_y[i]) R\n" else "\(x[i]) \(y[i]) \(p_x[i]) \(p_y[i])\n" endif |i in CIRCUITS];

My purpose is to print each row and the vaue "R" whether rotation[i] is true, false otherwise. For instance:

w l
n
x[1] y[1] p_x[1] p_y[1] "R"
x[2] y[2] p_x[2] p_y[2]

In this example rotation[1] is true and rotation[2] is false

Upvotes: 0

Views: 169

Answers (1)

Dekker1
Dekker1

Reputation: 5786

Your sample MiniZinc code was almost there. The important change you will have to make if to force the "rotation" variable to take its "solution value" using the "fix" builtin function.

output ["\(w) \(l)\n\(n)\n"]
    ++ ["\(x[i]) \(y[i]) \(p_x[i]) \(p_y[i])" 
        ++ if fix(rotation[i]) then " R" else "" endif
        ++ "\n" 
       | i in index_set(rotation)];

Additionally, I would suggest trying to make the conditional part as small as possible to improve readability, as incorporated in the above code fragment.

Upvotes: 2

Related Questions