0
這是一個水罐問題。較大的桶容納5個,較小的桶容納3個。我想在較大的桶中獲得4個。序言中的水罐
問題是,當我運行我無法得到任何答案時,它會產生一個錯誤。它看起來不是一個明顯的錯誤,算法簡單直接。
任何人都可以幫助我找到它有什麼問題嗎?
check_safe(X,Y):- X>=0,X=<3,Y>=0,Y=<5.
%empty the 3 bucket
move(state(X,Y),state(0,Y)):- X>0,check_safe(0,Y).
%empty the 5 bucket
move(state(X,Y),state(X,0)):- Y>0,check_safe(X,0).
%fill the 3 bucket
move(state(X,Y), state(3,Y)):- X<3, X>=0,check_safe(3,Y).
%fill the 5 bucket
move(state(X,Y),state(X,5)):- Y>=0, Y<5,check_safe(X,5).
%transfer from 3 to 5 until the larger bucket is full
move(state(X,Y),state(NewX,5)):- X+Y>= 5, X>0,Y>=0, NewX=X+Y-5,check_safe(NewX,5).
%transfer from 3 to 5 until the smaller bucket is empty
move(state(X,Y),state(0,NewY)):- X+Y<5, X>0,Y>=0, NewY=X+Y,check_safe(0,NewY).
%transfer from 5 to 3 until the smaller is full
move(state(X,Y),state(3,NewY)):- Y>0,X>=0,X+Y>=5, NewY=Y+X-3,check_safe(3,NewY).
%transfer from 5 to 3 until the larger is empty
move(state(X,Y),state(NewX,0)):-Y>0,X>=0, X+Y<5, NewX=Y+X,check_safe(NewX,0).
path(X,X,_,[X]).
path(X,Y,BeenStates,Path):-
move(X,Somewhere),not(member(Somewhere,BeenStates)),
path(Somewhere,Y,[Somewhere|BeenStates],Path2), Path = [X|Path2].
puzzle:- path(state(0,0),state(0,5),[state(0,0)],PathList),X>=0,X=<5,
writeOut(PathList).
% Here's an easy little predicate for printing a list.
writeOut([]).
writeOut([H|T]):-write(H),nl, writeOut(T).
我渴望有一天有人會問一個不是家庭作業的序言問題。 –
可能不會發生。 Prolog需要一種不適合絕大多數現實世界任務的編程風格。它可能對人工智能有用,但大多數人做AI似乎使用Lisp。 – cHao
現實中有很多人在使用它,他們只是被功課提問者淹沒在這裏。 –