Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. %---FARMER SECTION---
  2.  
  3. change(w,e).
  4. change(e,w).
  5.  
  6. unsafe(F,X,X,C) :-
  7. change(F,X).
  8. unsafe(F,W,X,X) :-
  9. change(F,X).
  10.  
  11.  
  12. %move wolf
  13. move(state(X,X,G,C), state(Y,Y,G,C)) :-
  14. change(X,Y), not(unsafe(Y,Y,G,C)).
  15.  
  16. %move goat
  17. move(state(X,W,X,C), state(Y,W,Y,C)) :-
  18. change(X,Y), not(unsafe(Y,W,Y,C)).
  19.  
  20. %move cabbage
  21. move(state(X,W,G,X), state(Y,W,G,Y)) :-
  22. change(X,Y), not(unsafe(Y,W,G,Y)).
  23.  
  24. %move farmer alone
  25. move(state(X,W,G,C), state(Y,W,G,C)) :-
  26. change(X,Y), not(unsafe(Y,W,G,C)).
  27.  
  28. start(state(w,w,w,w)).
  29. goal(state(e,e,e,e)).
  30.  
  31.  
  32. showFarm(S) :-
  33. showFW(S), write('|~~~~~~~|'), showFE(S),nl.
  34.  
  35. showFW(state(F,W,G,C)) :-
  36. (F == w, write('F'), !; write(' ')),
  37. (W == w, write('W'), !; write(' ')),
  38. (G == w, write('G'), !; write(' ')),
  39. (C == w, write('C'), !; write(' ')).
  40.  
  41. showFE(state(F,W,G,C)) :-
  42. (F == e, write('F'), !; true),
  43. (W == e, write('W'), !; true),
  44. (G == e, write('G'), !; true),
  45. (C == e, write('C'), !; true).
  46.  
  47.  
  48. showSol([]).
  49. showSol([H|T]) :- showFarm(H), showSol(T).
  50.  
  51.  
  52. %---MISSIONARY/CANNIBAL SECTION---
  53.  
  54. % state(me,ce,mw,cw,b)
  55.  
  56. %safe(Me,Ce,MW,CW) :- Me >= Ce, MW >= CW.
  57.  
  58. unsafe(Me,Ce,MW,CW, B) :- Me > 0, Ce > Me.
  59. unsafe(Me,Ce,MW,CW, B) :- MW > 0, CW > MW.
  60.  
  61.  
  62. %%%%% East to West %%%%%%%
  63. % move 2 missionaries from east to west
  64. move(state(Me,Ce, Mw, Cw, 1), state(ME,CE,MW,CW, -1)) :-
  65. Me > 1,
  66. ME is Me-2,
  67. MW is Mw+2,
  68. CW = Cw,
  69. CE = Ce,
  70. not(unsafe(ME,CE,MW,CW,-1)).
  71.  
  72. % move 1 missionaries from east to west
  73. move(state(Me,Ce, Mw, Cw, 1), state(ME,CE,MW,CW, -1)) :-
  74. Me > 0,
  75. ME is Me-1,
  76. MW is Mw+1,
  77. CW = Cw,
  78. CE = Ce,
  79. not(unsafe(ME,CE,MW,CW,-1)).
  80.  
  81. % move 1 missionarie and 1 cannibal from east to west
  82. move(state(Me,Ce, Mw, Cw, 1), state(ME,CE,MW,CW, -1)) :-
  83. Me > 0,
  84. Ce > 0,
  85. ME is Me-1,
  86. MW is Mw+1,
  87. CW is Cw+1,
  88. CE is Ce-1,
  89. not(unsafe(ME,CE,MW,CW,-1)).
  90.  
  91. % move 2 cannibals from east to west
  92. move(state(Me,Ce, Mw, Cw, 1), state(ME,CE,MW,CW, -1)) :-
  93. Ce > 1,
  94. ME = Me,
  95. MW = Mw,
  96. CW is Cw + 2,
  97. CE is Ce - 2,
  98. not(unsafe(ME,CE,MW,CW,-1)).
  99.  
  100. % move 1 cannibals from east to west
  101. move(state(Me,Ce, Mw, Cw, 1), state(ME,CE,MW,CW, -1)) :-
  102. Ce > 0,
  103. ME is Me,
  104. MW is Mw,
  105. CW is Cw+1,
  106. CE is Ce-1,
  107. not(unsafe(ME,CE,MW,CW,-1)).
  108.  
  109. %%%%% West to East %%%%%
  110.  
  111. % move 2 missionaries from west to east
  112. move(state(Me,Ce, Mw, Cw, -1), state(ME,CE,MW,CW, 1)) :-
  113. Mw > 1,
  114. ME is Me+2,
  115. MW is Mw-2,
  116. CW = Cw,
  117. CE = Ce,
  118. not(unsafe(ME,CE,MW,CW,1)).
  119.  
  120. % move 1 missionaries from west to east
  121. move(state(Me,Ce, Mw, Cw, -1), state(ME,CE,MW,CW, 1)) :-
  122. Mw > 0,
  123. ME is Me+1,
  124. MW is Mw-1,
  125. CW = Cw,
  126. CE = Ce,
  127. not(unsafe(ME,CE,MW,CW,1)).
  128.  
  129. % move 1 missionary and 1 cannibal from west to east
  130. move(state(Me,Ce, Mw, Cw, -1), state(ME,CE,MW,CW, 1)) :-
  131. Mw > 0,
  132. Cw > 0,
  133. ME is Me+1,
  134. MW is Mw-1,
  135. CW is Cw-1,
  136. CE is Ce+1,
  137. not(unsafe(ME,CE,MW,CW,1)).
  138.  
  139. % move 2 cannibals from west to east
  140. move(state(Me,Ce, Mw, Cw, -1), state(ME,CE,MW,CW, 1)) :-
  141. Cw > 1,
  142. ME = Me,
  143. MW = Mw,
  144. CW is Cw - 2,
  145. CE is Ce + 2,
  146. not(unsafe(ME,CE,MW,CW,1)).
  147.  
  148. % move 1 cannibals from east to west
  149. move(state(Me,Ce, Mw, Cw, -1), state(ME,CE,MW,CW, 1)) :-
  150. Cw > 0,
  151. ME is Me,
  152. MW is Mw,
  153. CW is Cw-1,
  154. CE is Ce+1,
  155. not(unsafe(ME,CE,MW,CW,1)).
  156.  
  157.  
  158.  
  159. start(state(3,3,0,0,1)).
  160. goal(state(0,0,3,3,-1)).
  161.  
  162.  
  163.  
  164. showMission(S) :-
  165. showMW(S), write('|~~~~~~~|'), showME(S), nl.
  166.  
  167. showMW(state(Me, Ce, Mw, Cw, B)) :-
  168. (write(Mw), write(' '), !; write(' ')),
  169. (write(Cw), write(' '), !; write(' ')),
  170. (write('W'), !; write(' ')).
  171.  
  172. showME(state(Me, Ce, Mw, Cw, B)) :-
  173. (write(Me), write(' '), !; write(' ')),
  174. (write(Ce), write(' '), !; write(' ')),
  175. (write('E'), !; write(' ')).
  176.  
  177. showSol([]).
  178. showSol([H|T]) :- showMission(H), showSol(T).
  179.  
  180. %---STATE SEARCHING SECTION---
  181.  
  182. farm :- solve(state(w,w,w,w), X), showSol(X).
  183. mission :- solve(state(3,3,0,0,1), X), showSol(X).
  184.  
  185. solve(X,Soln):-
  186. solve(X,[],Sol),
  187. reverse(Sol,Soln).
  188.  
  189. solve(State,Path,[State|Path]):-
  190. goal(State).
  191.  
  192. solve(State,Path,Sol):-
  193. move(State,NewState),
  194. not(member(NewState,Path)),
  195. solve(NewState,[State|Path],Sol).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement