如何在CLPFD中为以下场景定义变量?

时间:2013-05-29 19:00:24

标签: prolog clpfd sicstus-prolog

我有一组变量T_1, T_2, ..., T_N,我想在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

我该怎么做?

我会使用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
...

但我相信一个更有经验的程序员不会这样写。

1 个答案:

答案 0 :(得分:4)

您可以按如下方式删除all_different和count约束:

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]).