here's what i have so far(i'm using a Depth-First search):
dfsearch(Anspath) :- initial_state(Init), df([Init], Anspath).
df([S | Path], [S]) :- final_state(S), ! .
df([S | Path], [S | AnsPath]) :-
extend([S | Path], S1),
df([S1, S | path], AnsPath).
extend([S | Path], S1) :-
next_state(S, S1),
not(member(S1, [S | Path])).
member(S, [S | Rest]).
member(S, [_ | Rest]) :-
member(S, Rest).
.....the only undefined predicates so far are my :
next_state, initial_state, and final_state.......
if anybody knows anything about this classic problem please reply to this post
ASAP... the project is due May 1st......thank you
Here is the code!
writelist([]).
writelist([H|T]):-
write(H), writelist(T).
empty_queue([]).
enqueue(E,[],[E]).
enqueue(E,[H|T],[H|Tnew]):- enqueue(E,T,Tnew).
dequeue(E,[E|T],T).
dequeue(E,[E|T],_).
member_queue(Element,Queue):-member(Element, Queue).
add_list_to_queue(List, Queue, Newqueue):-
append(Queue, List, Newqueue).
empty_set([]).
member_set(E,S):-
member(E,S).
add_if_not_in_set(X,S,S):-
member(X,S),!.
add_if_not_in_set(X,S,[X|S]).
union([],S,S).
union([H|T],S,S_new):-
union(T,S,S2),
add_if_not_in_set(H,S2,S_new),!.
unsafe(state(X,Y,Y,C)):-
opp(X, Y).
unsafe(state(X,W,Y,Y)):-
opp(X, Y).
move(state(X,X,G,C), state(Y,Y,G,C)):-
opp(X,Y), not(unsafe(state(Y,Y,G,C))),
writelist(['try farmer takes wolf ',Y,Y,G,C]),nl.
move(state(X,W,X,C), state(Y,W,Y,C)):-
opp(X,Y), not(unsafe(state(Y,W,Y,C))),
writelist(['try farmer takes goat ',Y,W,Y,C]),nl.
move(state(X,W,G,X), state(Y,W,G,Y)):-
opp(X,Y), not(unsafe(state(Y,W,G,Y))),
writelist(['try farmer takes cabbage ',Y,W,G,Y]),nl.
move(state(X,W,G,C), state(Y,W,G,C)):-
opp(X,Y), not(unsafe(state(Y,W,G,C))),
writelist(['try farmer takes self ',Y,W,G,C]),nl.
move(state(F,W,G,C), state(F,W,G,C)):-
writelist([' BACKTRACK from: ',F,W,G,C]),nl,fail.
path(Open_queue,_,_):-
empty_queue(Open_queue),
write('Graph searched, no solution found.').
path(Open_queue, Closed_set, Goal):-
dequeue([State, Parent], Open_queue,_), State=Goal,
write('The solution path is:'), nl,
printsolution([State, Parent], Closed_set).
path(Open_queue, Closed_set, Goal):-
dequeue([State, Parent], Open_queue, Rest_open_queue),
get_children(State, Rest_open_queue, Closed_set, Children),
add_list_to_queue(Children, Rest_open_queue, New_open_queue),
union([[State, Parent]], Closed_set, New_closed_set),
path(New_open_queue, New_closed_set, Goal),!.
opp(e,w).
opp(w,e).
get_children(State, Rest_open_queue, Closed_set, Children):-
(bagof(Child, moves(State, Rest_open_queue,
Closed_set, Child), Children);
Children = []).
moves(State, Rest_open_queue, Closed_set, [Next, State]):-
move(State, Next),
not(unsafe(Next)),
not(member_queue([Next,_], Rest_open_queue)),
not(member_set([Next,_], Closed_set)).
go(Start, Goal):-
empty_queue(Empty_open_queue),
enqueue([Start, nil],Empty_open_queue, Open_queue),
empty_set(Closed_set),
path(Open_queue, Closed_set, Goal).
test:-go(state(w,w,w,w), state(e,e,e,e)).
printsolution([State, nil],_):-
write(State),nl.
printsolution([State, Parent], Closed_set):-
member_set([Parent, Grandparent], Closed_set),
printsolution([Parent, Grandparent], Closed_set),
write(State), nl.
--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=
| Brett Selleck |
| sell...@csis.gvsu.edu |
| www.csis.gvsu.edu/~selleckb |
--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=--=
here is a depth first search of the farmer wolf goat and cabbage problem
% Farmer, goat, wolf, and cabbage problem.
writelist([]).
writelist([H|T]):-
write(H), writelist(T).
empty_stack([]).
stack(Top, Stack, [Top|Stack]).
member_stack(Element, Stack):-
member(Element, Stack).
reverse_print_stack(S):-
empty_stack(S).
reverse_print_stack(S):-
stack(E, Rest, S),
reverse_print_stack(Rest),
write(E), nl.
unsafe(state(X,Y,Y,C)):-
opp(X, Y).
unsafe(state(X,W,Y,Y)):-
opp(X, Y).
move(state(X,X,G,C), state(Y,Y,G,C)):-
opp(X,Y), not(unsafe(state(Y,Y,G,C))),
writelist(['try farmer takes wolf ',Y,Y,G,C]),nl.
move(state(X,W,X,C), state(Y,W,Y,C)):-
opp(X,Y), not(unsafe(state(Y,W,Y,C))),
writelist(['try farmer takes goat ',Y,W,Y,C]),nl.
move(state(X,W,G,X), state(Y,W,G,Y)):-
opp(X,Y), not(unsafe(state(Y,W,G,Y))),
writelist(['try farmer takes cabbage ',Y,W,G,Y]),nl.
move(state(X,W,G,C), state(Y,W,G,C)):-
opp(X,Y), not(unsafe(state(Y,W,G,C))),
writelist(['try farmer takes self ',Y,W,G,C]),nl.
move(state(F,W,G,C), state(F,W,G,C)):-
writelist([' BACKTRACK from: ',F,W,G,C]),nl,fail.
path(Goal, Goal, Been_stack):-
nl, write('Solution Path is: '), nl,
reverse_print_stack(Been_stack).
path(State, Goal, Been_stack):-
move(State, Next_state),
not(member_stack(Next_state, Been_stack)),
stack(Next_state, Been_stack, New_been_stack),
path(Next_state, Goal, New_been_stack),!.
opp(e,w).
opp(w,e).
go(Start, Goal):-
empty_stack(Empty_been_stack),
stack(Start, Empty_been_stack, Been_stack),
path(Start, Goal, Been_stack).
test:-go(state(w,w,w,w), state(e,e,e,e)).
--