clpfd - 域的约束上键是元素列表的最大值

clpfd - constraint upper bond of domain the be the maximum of a list of elements

给定以下代码:

solve(G,L) :-
    G = [A0,B0,C0,D0],
    L = [A1,B1,C1,D1,A2,B2,C2,D2,A3,B3,C3,D3,A4,B4,C4,D4],
    G ins 0..4,
    L ins 0..max(G).

我想限制 L 不包含高于 G 中包含的最大值的值,但在使用此语法时我得到 "domain error"。还有其他表达方式吗?

基本上你是在正确的轨道上。但是 L ins 0..max(G) 不起作用,因为您传递给 ins/2 的边界需要是整数或 infsup.

SWI-Prolog clpfd manual page 支持 max 有限域算术表达式,所以我们首先声明MaxGA0B0C0D0中的最大值。然后我们声明 MaxG 大于或等于列表中的每个项目 L.

综合起来:

:- use_module(library(clpfd)).

gte_than(X,Y) :-
    X #>= Y.

solve(G,L) :-
    G = [A0,B0,C0,D0],
    L = [A1,B1,C1,D1,A2,B2,C2,D2,A3,B3,C3,D3,A4,B4,C4,D4],
    G ins 0..4,
    L ins 0..sup,
    MaxG #= max(max(A0,B0),max(C0,D0)),
    maplist(gte_than(MaxG),L).

以下是一些查询:

?- solve([0,1,2,1], [0,1,2,1,1,2,1,2,1,0,1,2,1,1,1,2]).
true.

?- solve([0,1,2,1], [0,3,2,1,1,2,1,2,1,0,1,2,1,1,1,2]).
false.

?- solve([0,3,2,1], [0,3,2,1,1,2,1,2,1,0,1,2,1,1,1,2]).
true.

?- solve([0,3,2,1], [0,3,2,1,1,2,4,4,1,0,1,2,1,1,1,2]).
false.

?- solve([4,3,2,1], [0,3,2,1,1,2,4,4,1,0,1,2,1,1,1,2]).
true.

旁注:SICStus Prolog 有一个专门的 arithmetic constraint 命名为 maximum/2 我们可以在这里使用,但该约束在带有 SWI-Prolog 的 clpfd 中(还)不可用。