0

我需要您的帮助来解决以下问题:

有 3 个女孩(安、苏珊、爱丽丝)需要选择穿什么颜色的鞋子和衣服。鞋子和连衣裙有 3 种可能的颜色:白色、蓝色和绿色。

主要条件:

  • 安讨厌白色。
  • 苏珊穿着同色的鞋子和裙子。
  • 爱丽丝有一双白鞋。
  • 爱丽丝和安的鞋子和衣服有不同的颜色。

我的代码只满足两个条件;对于苏珊来说,我很难满足相同颜色的条件,而其他女孩需要不同颜色的衣服。

这是我想出的:

PREDICATES
   girl(symbol)
   shoes(symbol,symbol)
   skirt(symbol,symbol)
   hates(symbol,symbol)
   will_wear(symbol, symbol, symbol)


CLAUSES
   will_wear(X,Y,Z):-
      girl(X),
      shoes(X,Y),
      skirt(X,Z),
      not(hates(X,Y)),
      not(hates(X,Z)).

   girl(ann).
   girl(susan).
   girl(alice).

   hates(ann,white).

   skirt(_,white).
   skirt(_,blue).
   skirt(_,green).

   shoes(alice,white).
   shoes(_,blue).
   shoes(_,green).

GOAL
   will_wear(Name,Shoes,Dress).

上面的代码工作正常,但提供了太多的解决方案。另外,对于苏珊穿同色鞋子和衣服的条件,我想不出任何合乎逻辑的解决方案。

谢谢。

4

2 回答 2

1

在我的脑海中,我在想一些事情:

only_wears(Girl,Color):-
    shoes(Girl, Color),
    skirt(Girl, Color).

different_shoes(F, S):-
    shoes(F,F_color),
    shoes(S,S_color),
    not(equals(F_color,S_color)).

different_skirts(F, S):-
    skirt(F,F_color),
    skirt(S,S_color),
    not(equals(F_color,S_color)).

我确实想知道是否有办法将子句传递给其他子句,因为different_shoesanddifferent_skirts在结构上是相同的。

你会像这样初始化它:

only_wears(ann, white).
different_shoes(alice, ann).
different_skirt(alice, ann).
于 2011-10-06T17:29:54.623 回答
1

如果我正确理解了这些条件,那它们就不是 Shurane 所回答的。

这将确保女孩穿着相同颜色的连衣裙和鞋子:

same_color(Girl) :-
    shoes(Girl, Color),
    dress(Girl, Color).

我将保留不同颜色的一个作为练习,但暗示说两件事与你说的不一样A \= B。如果您在使用 different_color 时遇到困难,请发表评论 - 并告诉我您尝试了什么。

于 2011-10-06T17:58:18.960 回答