0

解决以下Caliban 问题,将每条线索“忠实地”翻译成Prolog,即尽可能忠实地。

作为一个简单的抽象练习,假设四个无意义的符号 a、b、c 和 d 以一种或另一种顺序对应于同样无意义的符号 w、x、y 和 z,并进一步假设

如果 a 不是 x,则 c 不是 y。
如果 b 是 y 或 z,则 a 是 x。
如果 c 不是 w,则 b 是 z。
如果 d 是 y,那么 b 不是 x。
如果 d 不是 x,则 b 是 x。

两组符号对应的顺序是什么?

我尝试了以下代码:

vban(L) :-
       L=[[a,C1],[b,C2],[c,C3],[d,C4]],

       ( member(C1,[w,y,z]) -> member(C3,[w,x,z])),
       ( member(C2,[y,z]) -> member(C1,[x])),
       ( member(C3,[x,y,z]) -> member(C2,[z])),
       ( member(C4,[y]) -> member(C2,[w,y,z])),
       ( member(C4,[w,y,z]) -> member(C2,[x])).

但它显示失败。任何帮助将不胜感激。

4

3 回答 3

5

在 SICStus Prolog 或 SWI 中使用 CLP(B):

:- use_module(library(clpb)).
:- use_module(library(lists)).
:- use_module(library(clpfd)).

corresponding(Matrix) :-
        Matrix = [[ _,AX, _, _],
                  [ _,BX,BY,BZ],
                  [CW, _,CY, _],
                  [ _,DX,DY, _]],
        maplist(card1, Matrix),
        transpose(Matrix, TMatrix),
        maplist(card1, TMatrix),
        sat(~AX =< ~CY),
        sat(BY + BZ =< AX),
        sat(~CW =< BZ),
        sat(DY =< ~BX),
        sat(~DX =< BX).

card1(Vs) :- sat(card([1], Vs)).

示例查询:

?- corresponding(Vs), 
   pairs_keys_values(Pairs, [t,a,b,c,d], [[w,x,y,z]|Vs]),
   maplist(writeln, Pairs).

Yielding(1表示相应的元素):

t-[w,x,y,z]
a-[0,0,1,0]
b-[0,1,0,0]
c-[1,0,0,0]
d-[0,0,0,1]

Vs和和的绑定Pairs

于 2014-11-08T11:26:09.000 回答
4

我喜欢 Mat 的解决方案,但要解决问题,我们可以用“and”和“or”编写逻辑表达式。

a、b、c 和 d 可以用 [0,0]、[0,1]、[1,0] 和 [1,1] 表示。

如果(M1 = N1 和 M2 = N2),两个数 M 和 N 相等

如果 (M1 \= N1) 或 (M2 \= N2) (或 not(equals) ),则两个数字不同

暗示 u => v 被翻译成 not(u) 或 v

所以我们得到:

:- use_module(library(clpb)).
:- use_module(library(lambda)).

or(A,B,A+B).
and(A,B,A*B).

% two numbers are equal
equal(A, B, Eq) :-
    foldl(\X^Y^Z^T^and(Z, (X =:= Y), T), A, B, 1, Eq).

% two numbers are different
different(A, B, Diff) :-
    equal(A,B,Eq),
    Diff = ~Eq.
    % foldl(\X^Y^Z^T^or(Z, (X =\= Y), T), A, B, 0, Diff).


puzzle :-
    A = [0,0],
    B = [0,1],
    C = [1,0],
    D = [1,1],

    W = [_,_],
    X = [_,_],
    Y = [_,_],
    Z = [_,_],

    % If a is not x, then c is not y.
    % (a is x) or (c is not y)
    equal(A, X, Eq1),
    different(C, Y, Di1),
    or(Eq1, Di1, P1),

    % If b is either y or z, then a is x.
    % (b is not y) and (b is not z) or (a is x)
    different(B, Y, Di2),
    different(B, Z, Di3),
    equal(A, X, Eq2),
    and(Di2, Di3, P2),
    or(Eq2, P2, P3),

    % If c is not w, then b is z.
    % (c is w) or (b is z)
    equal(C, W, Eq3),
    equal(B, Z, Eq4),
    or(Eq3, Eq4, P4),

    % If d is y, then b is not x.
    % (d is not y) or (b is not x)
    different(D, Y, Di4),
    different(B, X, Di5),
    or(Di4, Di5, P5),

    % If d is not x, then b is x.
    %(d is x) or (b is x)
    equal(D, X, Eq5),
    equal(B, X, Eq6),
    or(Eq5, Eq6, P6),

    % we must express that W,X,Y,Z are differents
    % W is different from X, Y, Z
    foldl(W +\R^S^T^(different(W, R, U),
             and(S, U, T)),
             [X,Y,Z], 1, Dif1),

    % X is different from Y, Z
    foldl(X +\R^S^T^(different(X, R, U),
             and(S, U, T)),
             [Y,Z], 1, Dif2),

    % Y is different from Z
    different(Y, Z, Dif3),

    % now we join all these expressions with an and
    Expr = *([P1,P3,P4,P5,P6, Dif1,Dif2, Dif3]),

    % we ask Prolog to count the number of solutions
    sat_count(Expr, N),
    writeln(N : ' solution(s)'),

    % we ask Prolog to satisfy the expr
    sat(Expr),

    maplist(writeln, [A, B, C, D]), nl,
    maplist(writeln, [W, X, Y, Z]).

我们得到:

?- puzzle.
1: solution(s)
[0,0]
[0,1]
[1,0]
[1,1]

[1,0]
[0,1]
[0,0]
[1,1]
true.
于 2014-11-08T16:23:34.857 回答
4

使用ic_symbolic约束编程库将问题陈述直接翻译为 ECLiPSe Prolog :

:- lib(ic).
:- lib(ic_symbolic).
:- local domain(symbol(w,x,y,z)).
russel(A, B, C, D) :-
    [A, B, C, D] &:: symbol,
    (A &\= x) => (C &\= y),
    (B &= y or B &= z) => (A &= x),
    (C &\= w) => (B &= z),
    (D &= y) => (B &\= x),
    (D &\= x) => (B &= x),
    ic_symbolic:alldifferent([A, B, C, D]),
    ic_symbolic:indomain(A),
    ic_symbolic:indomain(B),
    ic_symbolic:indomain(C),
    ic_symbolic:indomain(D).

解决方案:

[eclipse]: russel(A,B,C,D).
A = y
B = x
C = w
D = z
Yes
于 2014-11-08T08:02:03.300 回答