Home My formal education Article list Prolog Prolog example exercise - flights

Prolog example exercise - flights

E-mail Print
Share/Save/Bookmark
We plan a trip to Tokio. We have data about plane flights. For each flight we know duration of flight and distance between airports. Our data is being stored in predicates flight(StartAirport, DestinationAirport, Duration, Distance). On each airport we spent some time waiting (customs etc.) and this waiting time is different for each airport. Data is stored in predicates waiting(Airport, WaitingTime). International abbreviations of airports are: Paris-cdg, Rim-fco, New York-jfk, Los Angeles-lax, Singapore-sin, Ljubljana-lju, Tokyo-nrt.

flight(lju,cdg,2,955).
flight(lju,fco,1,506).
flight(cdg,fco,3,1100).
flight(cdg,jfk,7,5830).
flight(cdg,lax,10,9096).
flight(cdg,sin,12,10712).
flight(jfk,lax,5,3971).
flight(jfk,nrt,6,5486).
flight(lax,nrt,7,6230).
flight(fco,jfk,8,6861).
flight(fco,sin,11,10039).
flight(sin,nrt,11,9857).
 
waiting(cdg,4).
waiting(fco,5).
waiting(jfk,2).
waiting(lax,2).
waiting(sin,1).
waiting(lju,1).
waiting(nrt,1).
 
 

Prolog graph airport connections

Define predicate connExists(Start,Destination) that checks whether there is connection between two airports.

connExists(Start,Destination):-
 flight(Start, Destination,_,_).
connExists(Start,Destination):-
 flight(Start, X,_,_),
 connExists(X,Destination).
 

Define predicate travelTime(Start,Destination,Time,Path). Predicate should find all paths between airports Start and Destination. It should return path and travel time for each path (travel time is time of all necessary flights plus waiting time).

usage example:
  ?-travelTime(lju,fco,Time,Path).
    Time = 15, Path = [lju,cdg,fco] ?
travelTime(Start,Destination,Time,Path):-
 flight(Start,Destination,FlightTime,_),
 waiting(Start,WTimeStart),
 waiting(Destination,WTimeDest),
 Time is FlightTime + WTimeStart + WTimeDest,
 conc([Start],[Destination],Path).
 
travelTime(Start,Destination,Time,Path):-
 flight(Start,X,FlightTime,_),
 waiting(Start,WTimeStart1),
 travelTime(X,Destination,TimeX,PathX),
 Time is FlightTime + TimeX +WTimeStart1,
 conc([Start],PathX,Path).

Write predicate allShorthestPaths, that builds database of all shortest paths - shortestPath(Start,End,Time).

allShorthestPaths:-
 waiting(X,_),
 waiting(Y,_),
 X \== Y,
 setof(T,(X^Y^C^P^travelTime(X,Y,T,P)),L),
 sort(L,[M|_]),
 assert(shortestPath(X,Y,M)),
 fail.
 

Write predicate pathLength(Path,Length) that returns length of given path.

Usage example:
?-pathLength([lju,cdg,fco],Length).
        Length = 2055 ?
%pathLength(Path,Length)
 
pathLength([],0).
pathLength([_],0).
pathLength([X,Y|L],Length):-
 flight(X,Y,_,T),
 pathLength([Y|L], Length1),
 Length is Length1+ T.



















Hits: 1877
Comments (2)Add Comment
October 21, 2009     
...
Dear Sir,

I have tried out the program and it works fine. Except at the predicate travelTime, the "conc" defined in the travelTime clause has no connection with rest of the predicate and an unknown error was given in the dialogue menu of the interactive prolog compiler. Could you please help declare the conc in the given travelTime predicate. Thank you in advance!
Raymond Cheung | 202.64.37.94
October 23, 2009     
...
Conc predicate is defined here http://www.webeks.net/computer...ction.html

I've used it as a predefined because it's part of my Cheet sheet -> http://www.webeks.net/computer...e-101.html
Miha | 193.77.156.218

Write comment

busy
Last Updated ( Monday, 04 May 2009 20:51 )  

Donate

Do you find content useful? Please donate so I can cover my hosting expenses! Thanks!