Advertisement
Guest User

Untitled

a guest
Apr 26th, 2014
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. DOMAINS
  2.   LOC   = east ; west
  3.   STATE = state(LOC farmer,LOC wolf,LOC goat,LOC cabbage)
  4.   PATH  = STATE*
  5.  
  6. PREDICATES
  7.   nondeterm go(STATE,STATE)               % Start of the algorithm
  8.   nondeterm path(STATE,STATE,PATH,PATH)   % Finds a path from one state to another
  9.   nondeterm move(STATE,STATE)   % Transfer a system from one side to another
  10.   opposite(LOC,LOC)             % Gives a location on the opposite side
  11.   nondeterm unsafe(STATE)       % Gives the unsafe states
  12.   nondeterm member(STATE,PATH)  % Checks if the state is already visited
  13.   write_path(PATH)
  14.   write_move(STATE,STATE)
  15.   addlast(STATE,PATH,PATH)
  16.  
  17. CLAUSES
  18.   go(StartState,GoalState):-
  19.         path(StartState,GoalState,[StartState],Path),
  20.         write("A Path solution is:\n"),
  21.         write_path(Path).
  22.  
  23.   path(GoalState,GoalState,Path,Path):-!.                  % The final state is reached
  24.   path(StartState,GoalState,L,P):-
  25.     move(StartState,NState),
  26.     not(unsafe(NState)),
  27.     not(member(NState,L)),
  28.     addlast(NState,L,NL),
  29.     path(NState,GoalState,NL,P).
  30.        
  31.   unsafe( state(F,X,X,_) ):- opposite(F,X),!.  % The wolf eats the goat
  32.   unsafe( state(F,_,X,X) ):- opposite(F,X),!.  % The goat eats the cabbage
  33.    
  34.   move(state(X,W,G,C),state(Y,W,G,C)):- opposite(X,Y). % Move FARMER
  35.   move(state(X,X,G,C),state(Y,Y,G,C)):- opposite(X,Y). % Move FARMER + WOLF
  36.   move(state(X,W,G,X),state(Y,W,G,Y)):- opposite(X,Y). % Move FARMER + CABBAGE
  37.   move(state(X,W,X,C),state(Y,W,Y,C)):- opposite(X,Y). % Move FARMER + GOAT
  38.  
  39.  
  40.   opposite(east,west).
  41.   opposite(west,east).
  42.  
  43.  
  44.   member(X,[X|_]):-!.
  45.   member(X,[_|L]):- member(X,L).
  46.  
  47.   addlast(X,[],[X]):-!.
  48.   addlast(X,[H|T1],[H|T2]):- addlast(X,T1,T2).
  49.  
  50.   write_path([_]).
  51.   write_path([H1,H2|T]):-
  52.         write_move(H1,H2),
  53.         write_path([H2|T]).
  54.  
  55.   write_move( state(X,W,G,C), state(Y,W,G,C) ) :-!,
  56.     write("The farmer crosses the river from ",X," to ",Y),nl.
  57.   write_move( state(X,X,G,C), state(Y,Y,G,C) ) :-!,
  58.     write("The farmer takes the Wolf from ",X," of the river to ",Y),nl.
  59.   write_move( state(X,W,X,C), state(Y,W,Y,C) ) :-!,
  60.     write("The farmer takes the Goat from ",X," of the river to ",Y),nl.
  61.   write_move( state(X,W,G,X), state(Y,W,G,Y) ) :-!,
  62.     write("The farmer takes the cabbage from ",X," of the river to ",Y),nl.
  63.    
  64. GOAL
  65. go(state(east,east,east,east),state(west,west,west,west)),
  66. write("solved\n").
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement