AI Lab 4
AI Lab 4
Prolog
Problem Setup:
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)).
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.