Sunday 18 January 2009

Monkey and Banana

A monkey is in a room. Suspended from the ceiling is a bunch of bananas, beyond the monkey's reach. In the corner of the room is a box.
How can the monkey get the bananas?
The solution is that the monkey must push the box under the bananas, then stand on the box, and then grab the bananas.
In other variants of the problem, the bananas are in a chest and the monkey first has to open the chest using a key.


move( state(middle, onbox, middle, hasnot),grasp, % Grasp banana
state(middle, onbox, middle, has) ).

move( state(P, onfloor, P, H), climb, % Climb box
state(P, onbox, P, H) ).

move( state(P1, onfloor, P1, H), push(P1, P2), % Push box from P1 to P2
state(P2, onfloor, P2, H) ).

move( state(P1, onfloor, B, H), walk(P1, P2), % Walk from P1 to P2
state(P2, onfloor, B, H) ).

% canget(State,Moves): monkey in State can get banana by performing Moves

canget( state(_,_,_,has), [] ).
canget(State1,[M|List]) :- move(State1,M,State2), canget(State2,List).
solve(Moves) :- canget(state(atdoor,onfloor,atwindow,hasnot),Moves).


For help, use ?- help(Topic). or ?- apropos(Word).

?- [mb].
% mb compiled 0.00 sec, 2,280 bytes
Yes

?- solve(Moves).
Moves = [walk(atdoor, atwindow), push(atwindow, middle), climb, grasp]
Yes

No comments:

Post a Comment