Webeks.net - freelance programming
freelance programming - php, Joomla, Zend ...
Home :: Articles :: Programming :: Prolog :: Prolog example exercise - flights

Prolog example exercise - flights

Written by Miha

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.

Data stored in predicates

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

Connections graph

Airports connections graph for prolog exercise

Prolog predicate that checks whether connection exists

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

  1. connExists(Start,Destination):-
  2. flight(Start, Destination,_,_).
  3. connExists(Start,Destination):-
  4. flight(Start, X,_,_),
  5. connExists(X,Destination).
  6.  

Prolog predicate that finds all paths between two nodes

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] ?

  1. travelTime(Start,Destination,Time,Path):-
  2. flight(Start,Destination,FlightTime,_),
  3. waiting(Start,WTimeStart),
  4. waiting(Destination,WTimeDest),
  5. Time is FlightTime + WTimeStart + WTimeDest,
  6. conc([Start],[Destination],Path).
  7.  
  8. travelTime(Start,Destination,Time,Path):-
  9. flight(Start,X,FlightTime,_),
  10. waiting(Start,WTimeStart1),
  11. travelTime(X,Destination,TimeX,PathX),
  12. Time is FlightTime + TimeX +WTimeStart1,
  13. conc([Start],PathX,Path).

Prolog predicate to build shortest paths database

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

  1. allShorthestPaths:-
  2. waiting(X,_),
  3. waiting(Y,_),
  4. X \== Y,
  5. setof(T,(X^Y^C^P^travelTime(X,Y,T,P)),L),
  6. sort(L,[M|_]),
  7. assert(shortestPath(X,Y,M)),
  8. fail.

Prolog predicate to return length of given path

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

Usage example:

?-pathLength([lju,cdg,fco],Length).
Length = 2055 ?

  1. %pathLength(Path,Length)
  2.  
  3. pathLength([],0).
  4. pathLength([_],0).
  5. pathLength([X,Y|L],Length):-
  6. flight(X,Y,_,T),
  7. pathLength([Y|L], Length1),
  8. Length is Length1+ T.

 

 


blog comments powered by Disqus