gpt4 book ai didi

algorithm - 在列表序言中创建数字组合

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:45:31 24 4
gpt4 key购买 nike

我正在 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

最佳答案

这个查询应该满足你的要求

?- Vs = [_,_,_,_,_,_], Vs ins 1..4,
[A,B,C,D] ins 1..2, global_cardinality(Vs, [1-A,2-B,3-C,4-D]), label(Vs).
Vs = [1, 1, 2, 2, 3, 4],
A = B, B = 2,
C = D, D = 1 ;
Vs = [1, 1, 2, 2, 4, 3],
A = B, B = 2,
C = D, D = 1 ;
...

关于algorithm - 在列表序言中创建数字组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27450199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com