【发布时间】:2015-03-31 02:03:45
【问题描述】:
我正在用 PROLOG 开发一个程序(有限制),它应该在一定的限制内输出 6 个数字的组合。
列表必须有从 1 到 4 的数字,因此会重复其他 2 个数字。从 1 到 4 的数字不可能不。
Possible examples: Wrong examples:
1,2,3,4,1,1 1,2,3,2,3,3 //Missing #4
1,3,2,1,4,4 4,3,2,4,2,3 //Missing #1
1,2,3,3,2,4
4,1,3,2,1,4
为了完成这项工作,我创建了一些限制,如下所示:
Numbers = [A1, A2, A3, A4, A5, A6]
nCr(6,4) = 15 restrictions
A1 =\= A2 =\= A3 =\= A4 OR
A1 =\= A2 =\= A3 =\= A5 OR
Etc.
这是我目前开发的代码:
make
pred(Numbers) :-
Numbers = [A1, A2, A3, A4, A5, A6],
domain(Numbers, 1, 4),
%restrictions
all_different([A1,A2,A6,A3]) #\/ %A1 =/= A2 =/= A6 =/= A3
all_different([A1,A2,A6,A4]) #\/ %A1 =/= A2 =/= A6 =/= A4
all_different([A1,A2,A6,A5]) #\/ %A1 =/= A2 =/= A6 =/= A5
all_different([A1,A2,A3,A4]) #\/ %A1 =/= A2 =/= A3 =/= A4
all_different([A1,A2,A3,A5]) #\/ %A1 =/= A2 =/= A3 =/= A5
all_different([A1,A2,A4,A5]) #\/ %A1 =/= A2 =/= A4 =/= A5
all_different([A1,A6,A3,A4]) #\/ %A1 =/= A6 =/= A3 =/= A4
all_different([A1,A6,A3,A5]) #\/ %A1 =/= A6 =/= A3 =/= A5
all_different([A1,A6,A4,A5]) #\/ %A1 =/= A6 =/= A4 =/= A5
all_different([A1,A3,A5,A4]) #\/ %A1 =/= A3 =/= A4 =/= A5
all_different([A2,A6,A3,A4]) #\/ %A2 =/= A6 =/= A3 =/= A4
all_different([A2,A6,A3,A5]) #\/ %A2 =/= A6 =/= A3 =/= A5
all_different([A2,A6,A4,A5]) #\/ %A2 =/= A6 =/= A4 =/= A5
all_different([A2,A3,A4,A5]) #\/ %A2 =/= A3 =/= A4 =/= A5
all_different([A6,A3,A4,A5]), %A6 =/= A3 =/= A4 =/= A5
labeling([], Numbers).
逻辑对我来说似乎很好,但是这个实现并没有按应有的方式工作。 没有解决方案符合输入的限制。谁能帮帮我?
| ?- pred([A1, A2, A3, A4, A5, A6]).
no
【问题讨论】:
-
尝试在更高级别表达限制。当你写它们时,它真的难以阅读......
-
这是我试图解决的子问题,以便开发更大的程序,这就是代码还不漂亮的原因。反正我做了一些改变。
-
@Khabz:当您谈到“限制”时,您的意思是指限制吗?至少这是我在查看您的程序时认为是正确的。
-
@false:是的,我就是这个意思。 “Restrições”是葡萄牙语的名字,这就是我通常说限制的原因
-
@Khabz:在英语中是约束,在法语中是约束
标签: algorithm prolog constraints clpfd