I’m trying to figure out how to calculate the fastest journey between two places, given different travel modes. My factbase starts with a predicate route(Src, Dest, Distance, TravelMode) and input is via the predicate journey(Src, Dest, TravelMode) which should output the fastest travel mode to use. (Basically whichever has the shortest time.)
However, it says that TravelMode is a string and if it contains f, it means the path can be traveled on foot, c for car, t for train and p for plane. This has me confused since I don’t really understand how to search the String TravelMode and only run the corresponding time functions for the travel modes included.
Below is my code and right now, it’s only able to calculate the time between places (time_f, etc), although I believe my time predicate is wrong since I think it’s supposed to be just one general function.
I also did try coding the journey predicate however, it only seems to output true / false and no values which probably means my route / speed predicate is wrong.
Am I on the right path? I’ve been stuck on this and I’d really appreciate any help to steer me in the correct direction / help explain what i’ve gotten wrong in here.
/* Sample set of facts */
route(dublin, cork, 200, 'fct').
route(cork, dublin, 200, 'fct').
route(cork, corkAirport, 20, 'fc').
route(corkAirport, cork, 25, 'fc').
route(dublin, dublinAirport, 10, 'fc').
route(dublinAirport, dublin, 20, 'fc').
route(dublinAirport, corkAirport, 225, 'p').
route(corkAirport, dublinAirport, 225, 'p').
/* Speed of mode of transport used */
speed(foot, 5).
speed(car, 80).
speed(train, 100).
speed(plane, 500).
/* Time between 2 cities, given specified transportation mode */
time_f(City1, City2, Time) :-
route(City1, City2, Distance, _),
speed(foot, Speed),
Time is (Distance / Speed),
write('Time travelling between '), write(City1), write(' and '), write(City2), write(' via foot is: '), write(Time), nl.
time_c(City1, City2, Time) :-
route(City1, City2, Distance, _),
speed(car, Speed),
Time is (Distance / Speed),
write('Time travelling between '), write(City1), write(' and '), write(City2), write(' via car is: '), write(Time), nl.
time_t(City1, City2, Time) :-
route(City1, City2, Distance, _),
speed(train, Speed),
Time is (Distance / Speed),
write('Time travelling between '), write(City1), write(' and '), write(City2), write(' via train is: '), write(Time), nl.
time_p(City1, City2, Time) :-
route(City1, City2, Distance, _),
speed(plane, Speed),
Time is (Distance / Speed),
write('Time travelling between '), write(City1), write(' and '), write(City2), write(' via plane is: '), write(Time), nl.
/* Solve for fastest journey */
journey(City1, City2, TravelModes) :-
route(City1, City2, Distance, TravelModes),
speed('TravelModes', Speed),
Time is (Distance / Speed),
write('Time travelling between '), write(City1), write(' and '), write(City2), write(' via '), write(TravelModes),
write(' is: '), write(Time), nl.