Jinxed
Jinxed

Reputation: 366

Optional variables and set membership in MiniZinc

I have a variable, that should only have a value under certain conditions, so it needs to be an optional variable, I suppose. If that condition is met, the optional variable should be restricted to elements of a set.

The problem is, that MiniZinc does not seem to like optional variables and sets.

How can this be rewritten, so that MiniZinc does not complain?

enum TYPES = { TYPE1, TYPE2 };
enum SUBTYPES = { SUBTYPE1, SUBTYPE2, SUBTYPE3, SUBTYPE4 };
var TYPES: mytype;
var opt SUBTYPES: subtype; % if "opt" is removed, it works

constraint mytype=TYPE1 -> subtype in { SUBTYPE1, SUBTYPE3 };

Upvotes: 3

Views: 145

Answers (1)

Dekker1
Dekker1

Reputation: 5786

Your model is almost correct, but is missing the handling of the optional part of subtype in your constraint. Because subtype is not guaranteed to exists, we cannot directly say that it has to be in the set {SUBTYPE1, SUBTYPE3}. Instead, we have to (1) force the value of subtype to exist and (2) then ensure that its value is within the given set.

We can force an optional value to exist by using the occurs intrinsic. Its value, on the other hand can be influenced using the deopt intrinsic. The constraint thus becomes:

constraint mytype=TYPE1 -> (occurs(subtype) /\ deopt(subtype) in { SUBTYPE1, SUBTYPE3 });

Upvotes: 5

Related Questions