60 likes | 93 Views
Monkey and Bananas Exercise. Notes on Exercise 3.10 of Bratko For CSCE 580 Sp03 Marco Valtorta. Bratko’s Solution.
E N D
Monkey and Bananas Exercise Notes on Exercise 3.10 of Bratko For CSCE 580 Sp03 Marco Valtorta
Bratko’s Solution • let canget(State,Actions) be the relation that holds if the monkey can get from State to a state in which it has the bananas by carrying out the moves described in the list Actions. • If the monkey already has the bananas, there is nothing that it needs to do. canget( state(_,_,_,has), [ ]). • Definition continues on the next slide
Bratko’s Solution (ctd.) • The monkey can get from State to a state in which it has the bananas by doing Action followed by Actions if • (a) the monkey can move from State to NewState by doing Action, and • (b) the monkey can move from NewState to a state in which it has the bananas by doing Actions. canget( State, [Action|Actions]) :- move( State, Action, NewState), canget( NewState, Actions).
Alternate Solution • canget1(State, Actions, Path) if the monkey can get the bananas from State by doing the actions in the difference list Path – Actions • If the monkey is already in a state in which it can get the bananas, then there is nothing that it needs to do: canget1( state(_,_,_,has), Actions, Actions). • Definition continues on the next slide
Alternate Solution (Ctd.) • The monkey can get the bananas from State1 by doing the Actions in Path - Actions if • (a) the monkey can move to State2 by doing Move, and • (b) the monkey can get the bananas from State2 by doing the actions in Path - (Actions + Move) canget1( State1, Actions, Path) :- move( State1, Move, State2), canget1( State2, [Move | Actions], Path).
Alternate Solution (Ctd.) • There is a procedural reading to the alternate solution: accumulate the solution path into the Actions list going down the goal tree, then save it at the bottom of the goal tree • The solution Path is built backwards! We need: canget( State, Actions) :- canget1( State, [ ], ReverseActions), rev(Actions, ReverseActions). • Actions can be thought of as an accumulator. • Accumulators and difference lists are good for efficiency, but in this exercise the simpler solution is faster too!