A*(Star) Prolog

泪湿孤枕 提交于 2021-02-08 05:56:19

问题


I have an A*(Star) Algorithm in prolog, and I need receive a list of destinations instead a single destination, and it should past for every element of the list and then back to the start. NOTE: It can pass in the same place twice. I tried it, but Swi-prolog returns false everytime, can I get any help? The code above receive a single destination, and it works, but as I said I need a list of destinations, and pass through them all. Example: astar(1,[2,2,4,6,8,9,10,13,15],C,P).

/*area(Number,PosX,PosY).*/

area(1,20,80).
area(2,50,30).
area(3,100,100).
area(4,90,70).
area(5,70,50).
area(6,110,50).
area(7,150,90).
area(8,200,90).
area(9,140,60).
area(10,160,20).
area(11,180,60).
area(12,190,20).
area(13,230,70).
area(14,240,30).
area(15,240,20).

/*street(CityA,CityB,Distance).*/
street(1,2,50).
street(1,3,100).
street(1,4,75).
street(1,5,65).
street(2,5,30).
street(3,4,40).
street(3,7,50).
street(4,5,25).
street(4,6,30).
street(4,9,65).
street(4,7,70).
street(5,6,30).
street(6,9,35).
street(6,10,60).
street(7,8,35).
street(7,9,30).
street(8,9,70).
street(8,11,40).
street(8,13,50).
street(9,10,50).
street(9,11,30).
street(10,11,50).
street(10,12,40).
street(11,12,40).
street(11,13,50).
street(11,14,60).
street(12,14,50).
street(13,14,50).

astar(Start,Final,_,Tp):-
      estimation(Start,Final,E),
      astar1([(E,E,0,[Start])],Final,_,Tp).

astar1([(_,_,Tp,[Final|R])|_],Final,[Final|R],Tp):- reverse([Final|R],L3),write('Path = '),write(L3).

astar1([(_,_,P,[X|R1])|R2],Final,C,Tp):-
       findall((NewSum,E1,NP,[Z,X|R1]),(street(X,Z,V),
               not(member(Z,R1)),
               NP is P+V,
               estimation(Z,Final,E1),
               NewSum is E1+NP),L),
append(R2,L,R3),
sort(R3,R4),
astar1(R4,Final,C,Tp).


estimation(C1,C2,Est):- area(C1,X1,Y1),area(C2,X2,Y2), DX is X1-X2,DY is Y1-Y2, 
                     Est is sqrt(DX*DX+DY*DY).

来源:https://stackoverflow.com/questions/41020596/astar-prolog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!