Search code examples
prolog

How to list all the trips discarding one airline


This is my flights data

#flight(city1, city2,airline,distance,time,price).
flight(dublin, london,ab,8000,4,1000).
flight(moscow,london,ab,9000,5,2000).
flight(dublin,moscow,bc,1000,6,3000).
flight(cork, moscow,ca,2000,7,4000).
flight(chicago, dublin,ab,6000,8,4500).
flight(berlin, moscow,bc,3000,9,4600).
flight(cork, newyork,ca,4000,10,4700).
flight(paris, hongkong,bc,11000,11,4890).

connected(X,Y,_,_,_,_) :- flight(X,Y,_,_,_,_) ; flight(Y,X,_,_,_,_). 

I have to get all the trips between city1 and city2 discarding one airline

I have calculated the trip something like

trips(A,B,Path) :- 
      traverse(A,B,[A],Q),
      reverse(Q,Path).
 traverse(A,B,P,[B|P]) :-
         connected(A,B,_,_,_,_).
  traverse(A,B,Visited,Path) :-
         connected(A,C,_,_,_,_),
         C \== B,
         \+member(C,Visited),
         traverse(C,B,[C|Visited],Path). 

This is the all_trip

Alltrip(C,L,T):-
      findall(Ci, trips(C,L,Ci), T).

I have to compute this

Alltrip_noairline(X,Y,T,A):- where X and Y are city ,T is the list of all trips and DISCARD all the trip containing a flight with airline A

I am stuck here ,don't know how to start ,any help would be appreciated .Thanks


Solution

  • I believe connected/6 is in fact flight/6

    You may add a Filter goal to your procedures:

    trips(A,B,Filter, Path) :-
      traverse(A,B,Filter, [A],Q),
      reverse(Q,Path).
    
    traverse(A,B, Filter, P,[B|P]) :-
      flight(A,B,Airline,_,_,_),
      call(Filter, Airline).
    traverse(A,B, Filter, Visited,Path) :-
      flight(A,C,Airline,_,_,_),
      C \== B,
      call(Filter, Airline),
       \+ member(C,Visited),
      traverse(C,B, Filter, [C|Visited],Path).
    

    Then in alltrip/3 use a filter that accepts any airline:

    alltrip(C,L,T):-
       findall(Ci, trips(C,L, any, Ci), T).
    
    any(_).
    

    and in alltrip_noairline/4 one that forbids certain airline:

    alltrip_noairline(C,L,T, A):-
       findall(Ci, trips(C,L, except(A), Ci), T).   
    
    except(X, Y):- X\=Y.