0

i have this A* code in Prolog.

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

astar1([(_,_,Tp,[Final|R])|_],Final,L3,Tp):-
      reverse([Final|R],L3).    %write('Path = '),write(L3).
astar1([(_,_,P,[X|R1])|R2],Final,C,Tp):-
       findall((NewSum,E1,NP,[Z,X|R1]),(adjacent(X,Z,V),
               \+(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(_,_,0).

this is the base of knowledge.

line(orange, subway, [
         [jarry,2],
         [jean_talon,1],
         [beaubien,2],
         [champ_de_mars,1]
         ], [[5,24],3,[0,33]], [[5,30],3,[0,30]]
).
line(blue, subway, [
         [fabre,2],
         [jean_talon,1]
         ], [[5,30],3,[0,45]], [[5,30],3,[0,45]]
).

this is an example of the output of the A* code:

?- astar(fabre,beaubien,Path,Time).
Path = [fabre, jean_talon, beaubien],
Time = 3 ;

But I want the output to look like this. In this example, jean_talon is an intersection between 2 lines of subway. That's why this station is represented in the blue and orange line.

Path = (blue, subway, [fabre, jean_talon]),  (orange, subway, [jean_talon, beaubien])].

how to format the output in Prolog?

1
  • Note that the adjacent/3 predicate is missing, so the code doesn't run as such. Apr 8, 2020 at 11:56

1 Answer 1

0

Here is already a snippet to format the station list into a list of station pairs:

myformat([Station|Stations],Result) :- 
   myformat3(Station,Stations,Result).

myformat3(PrevStation,[C|Stations],[[PrevStation,C]|Result]) :-
   Stations \= [],!,
   myformat3(C,Stations,Result).

myformat3(PrevStation,[C],[[PrevStation,C]]).

So:

?- myformat([fabre, jean_talon, beaubien],Result).
Result = [[fabre, jean_talon], [jean_talon, beaubien]].

Now to extend this...

Note that you are using what looks like tuples (A,B,C,D) for the result, but these are used to express conjunctions. They are nasty to compose properly, because they are actually tree structures like so:

       ,
      / \
     A   ,
        / \ 
       B   ,
          / \
         C   D

I will just use a proper list instead: [A,B,C,D], which is in-style with Prolog.

       .
      / \
     A   .
        / \ 
       B   .
          / \
         C   .
            / \
           D   []

In SWI-Prolog, the . above is replaced by [|].

decorate([[A,B]|Pairs],[Decoration|Result]):-
   line(Color,Type,Details,_,_),                   % grab a line...
   append([_,[[A,_TimeA],[B,_TimeB]],_],Details),  % ...in which the sublist for A,B appears
   Decoration = (Color,Type,[A,B]),                % compose what is actually a paranthesized CONJUNCTION
   decorate(Pairs,Result).
decorate([],[]).   

And now:

rt(Result) :- myformat([fabre, jean_talon, beaubien],Pairlist),
              decorate(Pairlist,Result).


?- rt(X).
X = [(blue, subway, [fabre, jean_talon]),  (orange, subway, [jean_talon, beaubien])] ;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.