SWI-Prolog中的约束编程

时间:2014-04-19 17:25:52

标签: prolog clpfd

我想要一个包含三个元素A,B和C的列表L,其中包含以下约束

:- use_module(library(clpfd)).
L[A,B,C], L ins 1..3, A#=B+C.

但是,它会出错 - Syntax error: Operator expected.

1 个答案:

答案 0 :(得分:3)

回答,以便问题从未回答的问题列表中删除: 错误也可能是(: - )/ 2,而不仅仅是缺失(=)/ 2。所以以下 会话wurks:

Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 7.1.11)
Copyright (c) 1990-2014 University of Amsterdam, VU Amsterdam

?- use_module(library(clpfd)).
%   library(occurs) compiled into occurs 0.00 sec, 14 clauses
%  library(apply_macros) compiled into apply_macros 0.01 sec, 51 clauses
%  library(assoc) compiled into assoc 0.01 sec, 103 clauses
% library(clpfd) compiled into clpfd 0.12 sec, 2,694 clauses
true.

?- L=[A,B,C], L ins 1..3, A#=B+C.
L = [A, B, C],
A in 2..3,
B+C#=A,
B in 1..2,
C in 1..2.

在上面我们只讨论了包括方程在内的问题 和变量范围。要列举必须使用的解决方案 标签/ 2谓词:

?- L=[A,B,C], L ins 1..3, A#=B+C, label(L).
L = [2, 1, 1],
A = 2,
B = C, C = 1 
L = [3, 1, 2],
A = 3,
B = 1,
C = 2 
L = [3, 2, 1],
A = 3,
B = 2,
C = 1.

再见