Information Technology Reference
In-Depth Information
9.2.4 Solving the monkey and bananas problem
The states in the monkey and bananas problem are more complex than for the three-
coins problem. Each state must specify the location of the monkey, bananas and box,
and also whether the monkey is on the box, and whether the monkey has the bananas.
This suggests using a five-element list [ b , m , l , o , h ] , as follows:
b , m , and l are the locations of the bananas, monkey, and box, respectively.
o is either y or n according to whether the monkey is on the box. (The monkey
can be at the same location as the box but not on it.)
h is either y or n according to whether the monkey has the bananas.
For locations, the easiest thing to do is to name the initial locations of the bananas,
monkey, and box as loc1 , loc2 , and loc3 . So, for example, the initial state will be
[loc1,loc2,loc3,n,n] . No other location needs to have a name. A goal state will be
a five-element list whose last element is y .
The moves available to the monkey are the following:
climb_on (climbing on the box)
climb_off (climbing off the box)
grab (grabbing the bananas)
go(X) (going to location X )
push(X) (pushing the box to location X )
Clauses for legal_move must be written for each of these operators, just as was done
for the operators of the three-coins problem. These clauses should say how a before
state is transformed to an after state by the operator in question. The clauses should
fail if the operator in question cannot be used in the before state.
Consider climb_on . It takes a state [ b , m , l , o , h ] to another state just like it except
o
= y . But this move is only legal when the monkey is at the same location as the box
( m
=
l ) and when the monkey is not already on the box ( o
= n ):
legal_move([B,M,L,O,H],climb_on,[B1,M1,L1,O1,H1]) :-
M=L, O=n,
% Provisos on the old state
B1=B, M1=M, L1=L, O1=y, H1=H.
% Values for the new state
This can be written more succinctly as
legal_move([B,M,M,n,H],climb_on,[B,M,M,y,H]).
The climb_off and grab operators will be similar.
 
 
Search WWH ::




Custom Search