Prolog 人工智能语言中文论坛---打造优质Prolog学习交流园地
Would you like to react to this message? Create an account in a few clicks or log in to continue.
Prolog 人工智能语言中文论坛---打造优质Prolog学习交流园地

一个供Prolog爱好者学习与交流的地方


您没有登录。 请登录注册

Prolog实现A*算法出问题了,且大神指导。。。急!!!

3 posters

向下  留言 [第1页/共1页]

yuxue99



astar(state([Aim|T],G,F),state([Aim|T],G,F),Open):-
Aim=t,true.

astar(X,Y,Open):-
findall(Z,rule(X,Z),L),
enter1(L,Open,[T]),
not(empty(T)),
get_first(state([A|B],G,H),T,T1),
delete(state([A|_ ],_,_),T1,T2),
astar(state([A|B],G,H),state([A|B],G,H),T1).

rule(state([W1|T],G1,H1),state([W2,W1|T],G2,H2)):-
goes(W1,W2,G0),
not(member(W2,[W1|T])),
count_g(G1,G0,G2),
count_h(W2,H2).

enter([ ],X,X):-true.
enter([X|Y],L,L1):-enter(X,L,L2),enter(Y,L2,L1).
enter1(X,[ ],[X]):-true.
enter1(X,[F|T],[X,F|T]):-le(X,F).
enter1(X,[F|T],[F|T1]):-enter1(X,T,T1).
le(state(X,G1,H1),state(Y,G2,H2)):- G1+H1=<G2+H2.
get_first(X,[X|T],T):-true.
delete(_,[ ],[ ]):-true.
delete(X,[X|L],M):-delete(X,L,M).
delete(X,[Y|L1],[Y|L2]):-delete(X,L1,L2).
empty([ ]):-true.
member(X,[X|_]):-true.
member(X,[_|Y]):-member(X,Y).
max(X,X,Y):-X>Y,true.
max(Y,X,Y):-Y>X,true.
count_g(G1,G0,G2):-G2=G1+G0.
goes(s,a,11).
goes(a,b,9).
goes(a,c,6).
goes(a,d,1).
goes(a,t,18).
goes(b,a,1).
goes(c,a,3).
goes(c,b,1).
goes(d,a,6).
goes(d,c,1).
count_h(a,1).
count_h(b,4).
count_h(c,Cool.
count_h(d,14).
count_h(s,20).
count_h(t,0).
/*astar(state([s],0,20),state(Path,G,H),[ ]).*/
/*rule(state([c|[d,s]],11,Cool,state([W,c|[d,s]],G,H)).*/


以上代码运行出了问题,SWI输入astar(state([s],0,20),state(Path,G,H),[ ]).之后没有结果显示,只有false,跪求指导!!谢了!!!

yauhsien



astar/3的第二句内,都只get_first,把其他选项丢失了,而且递归写错了。

这段更改为
代码:
astar(X,Y,Open) :-
    findall(Z,rule(X,Z),L),
    enter1(L,Open,[T]),
    member(state([A|B],G,H),T),
    astar(state([A|B],G,H),Y,T1).

yuxue99 写道:
代码:
astar(state([Aim|T],G,F),state([Aim|T],G,F),Open):-
   Aim=t,true.

astar(X,Y,Open):-
   findall(Z,rule(X,Z),L),
   enter1(L,Open,[T]),
   not(empty(T)),
   get_first(state([A|B],G,H),T,T1),
   delete(state([A|_ ],_,_),T1,T2),
   astar(state([A|B],G,H),state([A|B],G,H),T1).
......

以上代码运行出了问题,SWI输入astar(state([s],0,20),state(Path,G,H),[ ]).之后没有结果显示,只有false,跪求指导!!谢了!!!

http://yauhsien.wordpress.com

Mercury Liao


Admin

yauhsien太强大了,
比谁都有耐心看prolog代码啊……

yauhsien 写道:astar/3的第二句内,都只get_first,把其他选项丢失了,而且递归写错了。

这段更改为
代码:
astar(X,Y,Open) :-
    findall(Z,rule(X,Z),L),
    enter1(L,Open,[T]),
    member(state([A|B],G,H),T),
    astar(state([A|B],G,H),Y,T1).

yuxue99 写道:
代码:
astar(state([Aim|T],G,F),state([Aim|T],G,F),Open):-
   Aim=t,true.

astar(X,Y,Open):-
   findall(Z,rule(X,Z),L),
   enter1(L,Open,[T]),
   not(empty(T)),
   get_first(state([A|B],G,H),T,T1),
   delete(state([A|_ ],_,_),T1,T2),
   astar(state([A|B],G,H),state([A|B],G,H),T1).
......

以上代码运行出了问题,SWI输入astar(state([s],0,20),state(Path,G,H),[ ]).之后没有结果显示,只有false,跪求指导!!谢了!!!

http://prolog.longluntan.net

yuxue99



跪谢!!!
另,在SWI里面输入:“astar(state([s],0,20),state(Path,G,H),[ ]).”
输出结果为Path = [t|_G2250] .
这个输出是什么意思?以及为什么不能输出G,H的值?

我输入 astar(state([s],0,20),X,[ ]).之后,有输出:
X = state([t|_G2134], _G2130, _G2131).

新手啊~~问的弱智了不要见怪~~

yuxue99 写道:astar(state([Aim|T],G,F),state([Aim|T],G,F),Open):-
Aim=t,true.

astar(X,Y,Open):-
findall(Z,rule(X,Z),L),
enter1(L,Open,[T]),
not(empty(T)),
get_first(state([A|B],G,H),T,T1),
delete(state([A|_ ],_,_),T1,T2),
astar(state([A|B],G,H),state([A|B],G,H),T1).

rule(state([W1|T],G1,H1),state([W2,W1|T],G2,H2)):-
goes(W1,W2,G0),
not(member(W2,[W1|T])),
count_g(G1,G0,G2),
count_h(W2,H2).

enter([ ],X,X):-true.
enter([X|Y],L,L1):-enter(X,L,L2),enter(Y,L2,L1).
enter1(X,[ ],[X]):-true.
enter1(X,[F|T],[X,F|T]):-le(X,F).
enter1(X,[F|T],[F|T1]):-enter1(X,T,T1).
le(state(X,G1,H1),state(Y,G2,H2)):- G1+H1=<G2+H2.
get_first(X,[X|T],T):-true.
delete(_,[ ],[ ]):-true.
delete(X,[X|L],M):-delete(X,L,M).
delete(X,[Y|L1],[Y|L2]):-delete(X,L1,L2).
empty([ ]):-true.
member(X,[X|_]):-true.
member(X,[_|Y]):-member(X,Y).
max(X,X,Y):-X>Y,true.
max(Y,X,Y):-Y>X,true.
count_g(G1,G0,G2):-G2=G1+G0.
goes(s,a,11).
goes(a,b,9).
goes(a,c,6).
goes(a,d,1).
goes(a,t,18).
goes(b,a,1).
goes(c,a,3).
goes(c,b,1).
goes(d,a,6).
goes(d,c,1).
count_h(a,1).
count_h(b,4).
count_h(c,Cool.
count_h(d,14).
count_h(s,20).
count_h(t,0).
/*astar(state([s],0,20),state(Path,G,H),[ ]).*/
/*rule(state([c|[d,s]],11,Cool,state([W,c|[d,s]],G,H)).*/


以上代码运行出了问题,SWI输入astar(state([s],0,20),state(Path,G,H),[ ]).之后没有结果显示,只有false,跪求指导!!谢了!!!

yuxue99



我弄好了,谢谢!!

yauhsien 写道:astar/3的第二句内,都只get_first,把其他选项丢失了,而且递归写错了。

这段更改为
代码:
astar(X,Y,Open) :-
    findall(Z,rule(X,Z),L),
    enter1(L,Open,[T]),
    member(state([A|B],G,H),T),
    astar(state([A|B],G,H),Y,T1).

yuxue99 写道:
代码:
astar(state([Aim|T],G,F),state([Aim|T],G,F),Open):-
   Aim=t,true.

astar(X,Y,Open):-
   findall(Z,rule(X,Z),L),
   enter1(L,Open,[T]),
   not(empty(T)),
   get_first(state([A|B],G,H),T,T1),
   delete(state([A|_ ],_,_),T1,T2),
   astar(state([A|B],G,H),state([A|B],G,H),T1).
......

以上代码运行出了问题,SWI输入astar(state([s],0,20),state(Path,G,H),[ ]).之后没有结果显示,只有false,跪求指导!!谢了!!!

返回页首  留言 [第1页/共1页]

您在这个论坛的权限:
不能在这个论坛回复主题