I am familiar with such approaches as
set of int: RANGE = 1..5;
enum OPTS = {A,B,C};
array[RANGE] of var OPTS = result;
but what about when I would like to specify the OPTS
for each position in RANGE
? Something that looks like
[{A,B,C},{A,B},{A,B,C,D},{B,D},{A}]
then generate result[n]
so that it picks one of the available options at n
.
I've tried with the following model but a model inconsistency is detected.
set of int: RANGE = 1..5;
enum OPTS = {A,B,C,D};
array[RANGE] of set of OPTS: t = [{A,B,C},{A,B},{A,B,C,D},{B,D},{A}];
array[RANGE] of var OPTS: result;
constraint forall(i in RANGE)(
forall(j in t[i])(
result[i] = j
)
);
output [show(result)]
with a possible result being [B,A,B,D,A]
- the last position result[5]
cannot be anything other than A
.
I try to use the Minizinc manuals but I cannot decipher how to use examples within the specifications eg https://www.minizinc.org/doc-2.4.3/en/spec.html#set-operations, which presuppose other knowledge (e.g. how to interpret the syntax) that I could spend countless hours trying to hunt down an explanation for on my level, since searching this stuff seems to be really unfruitful.
Thanks!
If I understand you correctly you can use set of OPTS
to construct the array of OPTS
, here called t
(you cannot call it output
since it's a reserved word). Note that as you presented the problem, it's not a var
but a constant array so you don't need the var
keyword when you construct the array.
set of int: RANGE = 1..5;
enum OPTS = {A,B,C,D};
array[RANGE] of set of OPTS: t = [{A,B,C},{A,B},{A,B,C,D},{B,D},{A}];
Update
Here's a model for the updated question. The trick here is that t[i]
defines the valid domain of result[i]
, and you can simply use in
for this.
set of int: RANGE = 1..5;
enum OPTS = {A,B,C,D};
array[RANGE] of set of OPTS: t = [{A,B,C},{A,B},{A,B,C,D},{B,D},{A}];
array[RANGE] of var OPTS: result;
constraint
forall(i in RANGE) (
% ensure that result[i] only takes the values in t[i]
result[i] in t[i]
)
;
output [show(result)]
There are 48 solutions to this problem, such as:
[A, A, A, B, A]
----------
[B, A, A, B, A]
----------
[C, A, A, B, A]
----------
[A, B, A, B, A]
----------
[B, B, A, B, A]
----------
[C, B, A, B, A]
----------
[A, A, B, B, A]
----------
...