0

このリンクに似たコードを使用してみました:

Prolog でテキスト ロジック パズルを解く - 誕生日と月を見つける

私が解決しようとしている問題はこれです(電話での会話)。 http://www.cis.upenn.edu/~matuszek/cis554-2012/Assignments/prolog-01-logic-puzzle.html

私のコード:

dated(Date):-
member(Date,[1928,1929,1932,1935]).
exchanged(Exchange):-
member(Exchange,[al,be,pe,sl]).

solve(X):-
X=[[gertie,Exchange1,Date1],
   [herbert,Exchange2,Date2],
   [miriam,Exchange3,Date3],
   [wallace,Exchange4,Date4]],

exchanged(Exchange1), exchanged(Exchange2), exchanged(Exchange3), exchanged(Exchange4),
Exchange1 \== Exchange2, Exchange1 \== Exchange3, Exchange1 \== Exchange4,
Exchange2 \== Exchange1, Exchange2 \== Exchange3, Exchange2 \== Exchange4,
Exchange3 \== Exchange1, Exchange3 \== Exchange2, Exchange3 \== Exchange4,
Exchange4 \== Exchange1, Exchange4 \== Exchange2, Exchange4 \== Exchange3,

dated(Date1), dated(Date2), dated(Date3), dated(Date4),
Date1 \== Date2, Date1 \== Date3, Date1 \== Date4,
Date2 \== Date1, Date2 \== Date3, Date2 \== Date4,
Date3 \== Date1, Date3 \== Date2, Date3 \== Date4,
Date4 \== Date1, Date4 \== Date2, Date4 \== Date3,

%Herbet's first exchange wasn't for BE
Exchange2 \== be,

%The Person whose first exchange was SL wasn't Getie or Herbert
Exchange1 \== sl,
Exchange2 \== sl,

%The person whose first exchange was BE didn't get the phone in 1935
member([_,be, \+1935], X),

%The person who got the first phone in 1932 didn't have an exchange for AL or BE
member([_, \+al, 1932], X),
member([_, \+be, 1932],X),

%The person who got the first phone in 1928 had an exchange for PE
member([_,pe,1929], X),

%Wallace first exchange was AL
Exchange4 == al.

私の問題はこれです:

?- solve(X).
 false.
4

2 に答える 2

1

あなたの問題は、solve述語が解決策を見つけられないことです。これは、ソリューションを見つけるための前提条件の 1 つが、ソリューション ツリー内のすべての可能なパスで失敗することを意味します。

実際にそれがどれであるかを検索しようとしましたか?そうでなければ、次のことに気付くでしょう。

member([_,be,\+1935],X)

常に失敗します。なんで?とは\+/1? 「\+ :Goalが証明できない場合、 Goalは true です」。\+つまり、マッチングには使えません。代わりに、次のように書くことができます。

\+ member([_,be,1935),X).

したがって、すべての修正で:

?- solve(X).
X = [[gertie, be, 1928], [herbert, pe, 1929], [miriam, sl, 1932], [wallace, al, 1935]] ;
false.

プログラムの残りの部分が正しいと仮定します。

コードをデバッグする代わりに、stackoverflow を使用するのは本当に良くありません。

于 2013-04-06T10:02:49.780 に答える