Monkey Banana Problem by using
Prolog
Problem Setup:
● The monkey is initially at the door.
● The monkey is on the floor.
● The bananas are hanging from the ceiling (so the monkey cannot reach them).
● There is a block near the window.
● The monkey can perform certain actions: walk, push the block, climb on the block,
and grab the banana.
Prolog Representation:
1. States will represent the positions of the monkey, the block, and whether the monkey
is holding the banana or not.
2. Actions will represent possible moves such as walking, pushing the block, climbing
the block, and grabbing the banana.
3. The goal is for the monkey to hold the banana.
Prolog Code:
% Facts representing initial state
% state(MonkeyPosition, MonkeyState (on_floor/on_block), BlockPosition, HasBanana)
initial_state(state(at_door, on_floor, at_window, no)).
% Goal state - monkey has banana
goal_state(state(_, _, _, yes)).
% Action: Monkey can walk to a new position
move(state(MonkeyPos, on_floor, BlockPos, no),
walk(NewPos),
state(NewPos, on_floor, BlockPos, no)) :-
MonkeyPos \= NewPos.
% Action: Monkey can push the block to a new position
move(state(MonkeyPos, on_floor, MonkeyPos, no),
push_block(NewPos),
state(NewPos, on_floor, NewPos, no)) :-
MonkeyPos \= NewPos.
% Action: Monkey can climb on the block
move(state(MonkeyPos, on_floor, MonkeyPos, no),
climb_block,
state(MonkeyPos, on_block, MonkeyPos, no)).
% Action: Monkey can climb down from the block
move(state(MonkeyPos, on_block, MonkeyPos, no),
climb_down,
state(MonkeyPos, on_floor, MonkeyPos, no)).
% Action: Monkey can grab the banana if on block
move(state(at_center, on_block, at_center, no),
grab_banana,
state(at_center, on_block, at_center, yes)).
% Recursive search for solution
solve(State, []) :-
goal_state(State).
solve(State, [Action|Actions]) :-
move(State, Action, NewState),
solve(NewState, Actions).
% Query to solve the problem
solve_monkey_banana :-
initial_state(InitialState),
solve(InitialState, Actions),
write('Actions to get the banana: '), nl,
write(Actions), nl.
Explanation:
● State Representation:
○ state(MonkeyPos, MonkeyState, BlockPos, HasBanana):
■ MonkeyPos: where the monkey is (e.g., at_door, at_center,
at_window).
■ MonkeyState: whether the monkey is on the floor or on the block
(on_floor or on_block).
■ BlockPos: where the block is (e.g., at_door, at_center, at_window).
■ HasBanana: whether the monkey has the banana or not (yes or no).
● Actions:
○ walk(NewPos): The monkey can walk to a new position.
○ push_block(NewPos): The monkey can push the block to a new position if
both the monkey and block are in the same position.
○ climb_block: The monkey can climb on the block if it’s in the same position
as the block.
○ climb_down: The monkey can climb down from the block.
○ grab_banana: If the monkey is on the block at the centre of the room, it can
grab the banana.
● Goal: The goal is to reach a state where the monkey has the banana.
Example Query:
?- solve_monkey_banana.
Actions to get the banana:
[walk(at_window), push_block(at_center), climb_block, grab_banana]
This means the monkey needs to walk to the window, push the block to the centre,
climb on the block, and then grab the banana.