Advertisement
Guest User

Prolog code

a guest
Apr 11th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. flight(singapore,london , 2310, 0520, ba58,1).
  2. flight(singapore,london , 2310, 0520, ba58,3).
  3. flight(singapore,london , 2310, 0520, ba58,4).
  4. flight(singapore,london , 2310, 0520, ba58,6).
  5. flight(london,singapore , 1000, 1610, ba24,1).
  6. flight(london,singapore , 1000, 1610, ba24,3).
  7. flight(london,singapore , 1000, 1610, ba24,4).
  8. flight(london,singapore , 1000, 1610, ba24,6).
  9. flight(london,edinburgh , 0940, 1050, ba4732,1).
  10. flight(london,edinburgh , 0940, 1050, ba4732,2).
  11. flight(london,edinburgh , 0940, 1050, ba4732,3).
  12. flight(london,edinburgh , 0940, 1050, ba4732,4).
  13. flight(london,edinburgh , 0940, 1050, ba4732,5).
  14. flight(london,edinburgh , 0940, 1050, ba4732,6).
  15. flight(london,edinburgh , 0940, 1050, ba4732,7).
  16.  
  17.  
  18. % check if an element belongs to a list
  19. member(Country,[Country|_]).
  20. member(Country,[_|Tail]):- member(Country,Tail).
  21.  
  22. mydiff(X,X) :- !,fail.
  23. mydiff(X,Y).
  24.  
  25. % A: boarding station, B: destination, Path: route from A to B
  26. path(A,B,Path) :-
  27. travel(A,B,[A],Q),
  28. reverse(Q,Path).
  29.  
  30. % performs depth first search, member(C,Visited) will check if a station has been visited to avoid looping
  31. travel(A,B,P,[B|P]) :- flight(A,B,_,_,_,_).
  32.  
  33. travel(A,B,Visited,Path) :-
  34. flight(A,C,_,_,_,_),
  35. mydiff(C,B),
  36. \+member(C,Visited),
  37. travel(C,B,[C|Visited],Path).
  38.  
  39. calculate :- possiblePath(X), asserta(pathInformation(X)),fail.
  40.  
  41. findpossiblePath([]).
  42. findpossiblePath([Head|Tail]) :- /*nl,write(Head),*/asserta(possiblePath(Head)),findpossiblePath(Tail).
  43.  
  44. start :- nl, write('Please Day of Flight in Numberic format (1 for Mon, 7 for Sun: '),
  45. read(Day),
  46. nl, write('Origin Country: '),
  47. read(Origin), checkOCountry(Origin, 0), nl, write('Dest Country: '),
  48. read(Dest), checkDCountry(Dest, 1)->
  49. plane(Board,0),
  50. plane(Arrive,1),
  51. computation(Board,Arrive,List), nl,write('ShortestRoute: '),write(List),
  52. retractall(board(X,Y)),!,fail.
  53.  
  54.  
  55. computation(Start,Destination,List) :- setof(Path,path(Start,Destination,Path),Set), findpossiblePath(Set),
  56. not(calculate),pathInformation(List),retractall(pathInformation(X)),retractall(possiblePath(W)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement