Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. route(dublin, cork, 200, 'fct').
  2. route(cork, dublin, 200, 'fct').
  3. route(cork, corkAirport, 20, 'fc').
  4. route(corkAirport, cork, 25, 'fc').
  5. route(dublin, dublinAirport, 10, 'fc').
  6. route(dublinAirport, dublin, 20, 'fc').
  7. route(dublinAirport, corkAirport, 225, 'p').
  8. route(corkAirport, dublinAirport, 225, 'p').
  9.  
  10. % String code values for each mode of transport. I needed to use the string code values as when
  11. % the string is split into a list it converts the individual letters into
  12. % their string code counterparts
  13.  
  14. mode(102, 5).
  15. mode(99, 80).
  16. mode(116, 100).
  17. mode(112, 500).
  18.  
  19.  
  20. % Used this function to get a path from Beginning to end of the journey
  21. path(Begin, End, Path) :-
  22. path(Begin, End, [Begin], Path).
  23.  
  24.  
  25. % Upon reaching the target, the list is reversed
  26.  
  27. path(End, End, RPath, Path) :-
  28. reverse(RPath, Path).
  29.  
  30.  
  31. % take an edge and check if we have already visited before use
  32. % Use of memberchk implemented below
  33.  
  34. path(Begin, End, Visited, Path) :-
  35. route(Begin, Next, _, _),
  36. \+ memberchk(Next, Visited),
  37. path(Next, End, [Next|Visited], Path).
  38.  
  39. % Use of string_to_list to convert the mode string and placed in List_input, which is then reversed and put in Rev_input
  40.  
  41. journey(A, B, Input_mode) :- path(A, B, X),
  42. string_to_list(Input_mode, List_input),
  43. reverse(List_input, Rev_input),
  44. Y is 0,
  45. L = X,
  46. lis(X, Rev_input, Y, X, L),
  47. nl.
  48.  
  49. % Used this function to calculate time it takes for mode to reach destination
  50.  
  51. timeCalc(Velocity, Dist, TMP) :- TMP is ((Dist / Velocity) * 60).
  52.  
  53.  
  54. % Used this function to add time taken by different modes together
  55.  
  56. addTime(TMP, Y, Time) :- Time is TMP + Y.
  57.  
  58.  
  59.  
  60. % This is where I formatted my output, 'L' are the source and destinations and 'H' is the lenght of time it took in minutes.
  61.  
  62. lis([_|[]], _, _, [H|_], L) :- output(L), write(' would take '), write(H), write(' minutes to travel to.'), !.
  63. lis([H1, H2|T], Rev_input, Y, X, L) :- route(H1, H2, Dist, Mode),
  64. modeFind(H1, H2, Dist, Mode, Rev_input, Transport),
  65. mode(Transport, Velocity),
  66. timeCalc(Velocity, Dist, TMP),
  67. addTime(TMP, Y, Time),
  68. lis([H2|T], Rev_input, Time, [Time|X], L).
  69.  
  70. modeFind(_, _, _, Mode, Rev_input, Transport) :- string_to_list(Mode, Mode_L),
  71. modeCheck(Rev_input, Mode_L, Transport).
  72.  
  73. modeCheck([H|_], R_mode, Transport) :- member(H, R_mode), !, Transport is H.
  74. modeCheck([_|T], R_mode, Transport) :- modeCheck(T, R_mode, Transport).
  75.  
  76.  
  77. output([]).
  78. output([H|[]]) :-
  79. write(H), !.
  80. output([H|T]) :-
  81. write(H),
  82. write(' --> '),
  83. output(T).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement