Going from one point to another

Asked

Viewed 114 times

4

With the following facts,

x(p1,d1,c,d,e,f),
y(d1,d2,i,j,k,l),
z(d2,d3,o,p,q,r),

Thinking of them as points, I would like to know how I can go from x to z , making the input of P1 and D3.

I have, for example:

write('Partida: '),nl,
read(Partida_it),
write('Destino: '),nl,
read(Destino_it),
    viagem(Partida_it,Destino_it,C,D,E,F),nl,
    write(Partida_it),write('<-->'),write(Destino_it),nl,
write('Tipo: '),write(C),nl,
write('Hora de partida: '),write(D),nl,
write('Hora de chegada: '),write(E),nl,
write('Preco: '),write(F),nl,
    fail,

In case such a journey already exists^

viagem(Partida_it,B,C,D,_,P1),nl,
viagem(B,Destino_it,F,_,E,P2),nl,
P is P1+P2,
write(Partida_it),write('<-->'),write(Destino_it),nl,
write('Tipo: '),write(C),write('e'),write(F),nl,
write('Hora de partida: '),write(D),nl,
write('Hora de chegada: '),write(E),nl,
write('Preco: '),write(P),nl,
    fail.

I’ve been stuck with this problem for a few hours now.

  • 1

    Hello, welcome to Stackoverflow! Could you provide more details on your question? What kind of problem are you trying to solve? Where did you lock?

  • Hello, thank you, thank you! It is a travel platform between various transports, I have several trips in the database, for example trip(roma,Lisboa,aviao,12:00,15:00,300). travel(Lisboa,port,train,16:00,18:00,10). Giving input write('Departure: '),nl, read(Partida_it), write('Destination: '),nl, read(Destino_it). I want the program of an output of the necessary trips to arrive from Rome to Porto and the sum of prices

1 answer

4

Your high-level logic would be as follows::

  1. If there is a journey of Partida to Destino, do it and that’s it;
  2. Otherwise travel to a point X that you haven’t visited yet, and then try to travel from X for Destino.

Check if you have visited X or it is not important, otherwise you can enter an infinite loop. This can be done by saving the points you have already passed on a list, and by choosing a destination by checking whether or not that point is on the list.

In code:

viajar(A, B, _, [viagem(A, B, C, D, E, F)]) :-
    viagem(A, B, C, D, E, F), % Se existe uma viagem direta de A a B
    !.                        % Não precisa procurar mais

viajar(A, B, JaVisitou, [viagem(A, X, C, D, E, F)|Lista]) :-
    viagem(A, X, C, D, E, F),           % Escolhe um destino qualquer
    \+ member(X, JaVisitou),            % Que você ainda não visitou
    viajar(X, B, [X|JaVisitou], Lista). % E tente ir dele até B

The result of a call passing Partida and Destino would be a list of all trips that have to be made:

?- viajar(roma, porto, [roma], Lista).
Lista = [viagem(roma, lisboa, aviao, 12:00, 15:00, 300), viagem(lisboa, porto, comboio, 16:00, 18:00, 10)]

Example in the ideone. From there you can do operations on this list, like printing all the steps:

passos([]).
passos([viagem(A,B,C,D,E,F)|R]) :-
    write(A),write('<-->'),write(B),nl,
    write('Tipo: '),write(C),nl,
    write('Hora de partida: '),write(D),nl,
    write('Hora de chegada: '),write(E),nl,
    write('Preco: '),write(F),nl,
    passo(R).

And calculate the total price:

preco([], 0).
preco([viagem(_,_,_,_,_,P)|R], Total) :-
    preco(R, X),
    Total is P + X.

Etc. Note: in the examples above, I did not use tail recursion to make the answer simpler and the logic more evident. In practice, you should use accumulators instead of doing additional operations after the recursive call.

  • That clarified everything! Thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.