【问题标题】:Constraint programming in SWI-PrologSWI-Prolog 中的约束编程
【发布时间】:2014-04-19 17:25:52
【问题描述】:

我想要一个包含三个元素 A、B 和 C 的列表 L,并具有以下约束,

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

但是,它给出了一个错误 - Syntax error: Operator expected.

【问题讨论】:

  • 您的意思可能是“L=[A,B,C]”(缺少“=”)。

标签: prolog clpfd


【解决方案1】:

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

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.

在上面我们只说明了一个问题,包括方程 和可变范围。要枚举解决方案,必须使用 label/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.

再见

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    相关资源
    最近更新 更多