Reputation: 28666
I have a set of variables T_1, T_2, ..., T_N
and I would like to rewrite the following pseudocode in CLPFD:
T_1 in 0..59,
T_2 in 0..59,
...
T_n in 0..59,
all_different([T_1, T_2, ..., T_n]),
FOREACH x in 0 to 59 do:
IF (x \in [T_1, T_2, T_3, ..., T_n]) THEN
Slot_x = 1
ELSE
Slot_x = 0
ENDFOREACH
How can I do that?
I would use count:
T_1 in 0..59,
T_2 in 0..59,
...
count(0, [T_1, T_2, ..., T_n], #=, Slot_0) % The number 0 can be at most once in the list
count(1, [T_1, T_2, ..., T_n], #=, Slot_1) % The number 1 can be at most once in the list
...
but I believe that a more experienced programmer would not write it this way.
Upvotes: 1
Views: 135
Reputation: 1426
You can get rid of the all_different and count constraints as follows:
T_1 in 0..59,
T_2 in 0..59,
...
T_n in 0..59,
Slot_0 in 0..1,
Slot_1 in 0..1,
...
Slot_59 in 0..1,
global_cardinality([T_1, T_2, ..., T_n], [0-Slot_0, 1-Slot_1, ..., 59-Slot_59]).
Upvotes: 4