1

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.

MartyIX
  • 27,828
  • 29
  • 136
  • 207
  • You could use `length(Ts, 60)` to create a list of 60 variables, which might help. – Daniel Lyons May 29 '13 at 19:18
  • I generate the prolog program, therefore it is not that important if the resulting source code is compact. Moreover, my version allows me to simply write: Slot_3 + Slot_10 + Slot_34 #< 3. I guess it would be more verbose with `length(Ts, 60)`. Thank you for your comment, though. – MartyIX May 29 '13 at 19:57

1 Answers1

4

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]).
MartyIX
  • 27,828
  • 29
  • 136
  • 207
Mats Carlsson
  • 1,426
  • 7
  • 3