Information Technology Reference
In-Depth Information
Figure 9.6.
The monkey and bananas problem in Prolog
monkey.pl
% This is the monkey and bananas as a planning problem.
% The bananas, monkey, and box are at different locations.
% The monkey is not on the box and has no bananas.
initial_state([loc1,loc2,loc3,n,n]).
% The goal is any state where the monkey has the bananas.
goal_state([_,_,_,_,y]).
% Climbing on the box causes the monkey to be on the box.
legal_move([B,M,M,n,H],climb_on,[B,M,M,y,H]).
% Climbing off the box causes the monkey to be off the box.
legal_move([B,M,M,y,H],climb_off,[B,M,M,n,H]).
% Grabbing the bananas causes the monkey to have the bananas.
legal_move([B,B,B,y,n],grab,[B,B,B,y,y]).
% Pushing the box changes where the monkey and the box are.
legal_move([B,M,M,n,H],push(X),[B,X,X,n,H]).
% Going to a location changes where the monkey is.
legal_move([B,_,L,n,H],go(X),[B,X,L,n,H]).
Now consider the push(X) operator. The effect will be to move to a state where the
monkey and the box are both at location X . In other words, performing push(loc2)
will result in a state where m
= loc2 . However, this operator is only legal in
states where climb_on would also be legal. This leads to
legal_move([B,M,M,n,H],push(X),[B,X,X,n,H]).
The clause for go(X) will be similar. Note how the atom push(X) is used as an argu-
ment of the predicate legal_move . Using atoms as terms avoids having to write
separate operators for each location: push_loc1 , push_loc2 , push_loc3 , ..., and
similarly for the go operators.
The complete monkey and bananas program is shown in figure 9.6. Running this
program together with the general planner will give the results shown in figure 9.7.
The planner finds the desired four-step plan, and it is unique. There are no three-step
plans for this problem, but there is a five-step plan: simply go somewhere in the first
step and then apply a four-step plan. The last query in figure 9.7 shows that in any
five-step plan, the first move must be a go action.
=
l
9.2.5 Bounding plan length
The plan queries so far have always specified how many moves a plan should have.
If a plan for the monkey did not specify the number of steps, the result would be
 
Search WWH ::




Custom Search