3

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 };
Jinxed
  • 356
  • 2
  • 16

1 Answers1

5

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 });
Dekker1
  • 5,565
  • 25
  • 33