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).
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.
Email this
Hits: 1877
Comments (2)

Write comment



