prolog中的新手:简单解决

时间:2013-04-24 23:16:58

标签: prolog

我是prolog的新手,我正在努力解决这个问题:

它只是返回false!

我已经尝试多次更改代码的几个部分,因为我说我是prolog中的新手,因此我不知道如何完全调试..

livesIn(State):-
member(State,[california,georgia,delaware,iowa,kansas]).

sentGift(Gift):-
member(Gift,[rotisserie,salver,toaster,urn,vase]).

isHusband(Husband):-
member(Husband,[bill,doug,nick,tom,zack]).

solve(Z):-
Z=[[amber,Husband1,State1,Gift1],
   [emily,Husband2,State2,Gift2],
   [janet,Husband3,State3,Gift3],
   [maisie,Husband4,State4,Gift4],
   [patsy,Husband5,State5,Gift5]],

isHusband(Husband1), isHusband(Husband2), isHusband(Husband3), isHusband(Husband4), isHusband(Husband5),
Husband1 \== Husband2, Husband1 \== Husband3, Husband1 \== Husband4, Husband1 \== Husband5,
Husband2 \== Husband1, Husband2 \== Husband3, Husband2 \== Husband4, Husband2 \== Husband5,
Husband3 \== Husband1, Husband3 \== Husband2, Husband3 \== Husband4, Husband3 \== Husband5,
Husband4 \== Husband1, Husband4 \== Husband2, Husband4 \== Husband3, Husband4 \== Husband5,
Husband5 \== Husband1, Husband5 \== Husband2, Husband5 \== Husband3, Husband5 \== Husband4,

livesIn(State1), livesIn(State2), livesIn(State3), livesIn(State4), livesIn(State5),
State1 \== State2, State1 \== State3, State1 \== State4, State1 \== State5,
State2 \== State1, State2 \== State3, State2 \== State4, State2 \== State5,
State3 \== State1, State3 \== State2, State3 \== State4, State3 \== State5,
State4 \== State1, State4 \== State2, State4 \== State3, State4 \== State5,
State5 \== State1, State5 \== State2, State5 \== State3, State5 \== State4,

sentGift(Gift1), livesIn(Gift2), livesIn(Gift3), livesIn(Gift4), livesIn(Gift5),
Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5,
Gift2 \== Gift1, Gift2 \== Gift3, Gift2 \== Gift4, Gift2 \== Gift5,
Gift3 \== Gift1, Gift3 \== Gift2, Gift3 \== Gift4, Gift3 \== Gift5,
Gift4 \== Gift1, Gift4 \== Gift2, Gift4 \== Gift3, Gift4 \== Gift5,
Gift5 \== Gift1, Gift5 \== Gift2, Gift5 \== Gift3, Gift5 \== Gift4,

%Aunt Maisie's Husband is Uncle Nick
Husband4 = nick,

%Uncle Bill and Aunt Emily and their respective spouses sent the salver and the urn (in order)
Gift2 = salver,
member([_, bill, _, urn], Z),

%Aunt Amber and Uncle Doug live in Georgia
Husband1 = doug,
State1 = georgia,

%Aunt Patsy and her husband sent a toaster
Gift5 = toaster,

%The rotisserie came from the Delaware relatives
member([_, _, delaware, rotisserie], Z),

%Uncle Zack and his wife(don't live in Kansas) sent neither the salver nor the toaster
\+ member([_, zack, kansas, _], Z),
\+ member([_, zack, _, salver], Z),
\+ member([_, zack, _, toaster], Z),

%Aunt Janet and Uncle Bill live in California
Husband3 = bill,
State3 = california.

1 个答案:

答案 0 :(得分:0)

让它有效

您的代码中存在2个拼写错误,导致其无法返回正确的结果。

第一个错误

  比尔叔叔和艾米丽姨妈以及他们各自的配偶送了托盘和骨灰盒(按顺序)

所以而不是:

Gift2 = salver,
member([_, bill, _, urn], Z),

应该是:

Gift2 = urn,                       % Aunt Emily sent the urn
member([_, bill, _, salver], Z),   % Uncle Bill sent the salver

第二个错误

我不知道你怎么能犯这个错字!为什么livesIn是礼物?

sentGift(Gift1), livesIn(Gift2), livesIn(Gift3), livesIn(Gift4), livesIn(Gift5),
Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5,

应该是:

sentGift(Gift1), sentGift(Gift2), sentGift(Gift3), sentGift(Gift4), sentGift(Gift5),
Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5,

修复上面的2个拼写错误后,代码将正常运行,但需要很长时间才能达到解决方案。

加快速度

  1. 使用原子(例如Husband3 = billState1 = georgia订购直接统一 Gift Husband的成员资格统一}和State变量通过sentGiftisHusbandlivesIn谓词。

    实际上,您可以直接将它们插入Z。而不是手动声明所有这些不等式(例如Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5),而是编写谓词更清晰。

  2. 在第1部分直接统一后订购会员资格统一(例如member([_, bill, _, urn], Z)),但在成员统一GiftHusbandState之前变量

    请注意,包含\+ member([_, zack, _, toaster], Z),其声明会员资格统一无法成功。在将GiftHusbandState变量限制为一小组可能值的成员统一之前,可能会找到满足成员统一的统一,导致该条款,因此整个谓词失败。

  3. 上述两个技巧将减少搜索空间,从而减少查找解决方案所需的时间。下面是一些示例框架代码,供您在阅读上述文本时填写。

    % Aunt Maisie's Husband is Uncle Nick
    Husband4 = nick,
    
    %% TODO: Fill in the rest on your own ...
    
    % Uncle Bill and Aunt Emily and their respective spouses sent the salver and the urn (in order)
    Gift2 = urn,
    
    
    
    
    member([_, bill, _, salver], Z),
    
    % The rotisserie came from the Delaware relatives
    member([_, _, delaware, rotisserie], Z),
    
    
    
    
    sentGift(Gift1), sentGift(Gift2), sentGift(Gift3), sentGift(Gift4), sentGift(Gift5),
    Gift1 \== Gift2, Gift1 \== Gift3, Gift1 \== Gift4, Gift1 \== Gift5,
    Gift2 \== Gift1, Gift2 \== Gift3, Gift2 \== Gift4, Gift2 \== Gift5,
    
    %% TODO: Fill in the rest on your own
    
    
    
    
    
    % Uncle Zack and his wife(don't live in Kansas) sent neither the salver nor the toaster
    \+ member([_, zack, kansas, _], Z),
    \+ member([_, zack, _, salver], Z),
    \+ member([_, zack, _, toaster], Z).