Write a Program to Implement Monkey Banana Problem using Python.
SOURCE CODE :
def monkey_banana_problem():
# Initial state
initial_state = ('Far-Chair', 'Chair-Not-Under-Banana', 'Off-Chair', 'Empty') # (Monkey's Location, Monkey's Position on Chair, Chair's Location, Monkey's Status)
print(f"\n Initial state is {initial_state}")
goal_state = ('Near-Chair', 'Chair-Under-Banana', 'On-Chair', 'Holding') # The goal state when the monkey has the banana
# Possible actions and their effects
actions = {
"Move to Chair": lambda state: ('Near-Chair', state[1], state[2], state[3]) if state[0] != 'Near-Chair' else None,
"Push Chair under Banana": lambda state: ('Near-Chair', 'Chair-Under-Banana', state[2], state[3]) if state[0] == 'Near-Chair' and state[1] != 'Chair-Under-Banana' else None,
"Climb Chair": lambda state: ('Near-Chair', 'Chair-Under-Banana', 'On-Chair', state[3]) if state[0] == 'Near-Chair' and state[1] == 'Chair-Under-Banana' and state[2] != 'On-Chair' else None,
"Grasp Banana": lambda state: ('Near-Chair', 'Chair-Under-Banana', 'On-Chair', 'Holding') if state[0] == 'Near-Chair' and state[1] == 'Chair-Under-Banana' and state[2] == 'On-Chair' and state[3] !='Holding' else None
}
# BFS to explore states
from collections import deque
dq = deque([(initial_state, [])]) # Each element is (current_state, actions_taken)
visited = set()
while dq:
current_state, actions_taken = dq.popleft()
# Check if we've reached the goal
if current_state == goal_state:
print("\nSolution Found!")
print("Actions to achieve goal:")
for action in actions_taken:
print(action)
print(f"Final State: {current_state}")
return
# Mark the current state as visited
if current_state in visited:
continue
visited.add(current_state)
# Try all possible actions
for action_name, action_func in actions.items():
next_state = action_func(current_state)
if next_state and (next_state not in visited):
dq.append((next_state, actions_taken + [f"Action: {action_name}, Resulting State: {next_state}"]))
print("No solution found.")
# Run the program
monkey_banana_problem()
OUTPUT :
Initial state is ('Far-Chair', 'Chair-Not-Under-Banana', 'Off-Chair', 'Empty')
Solution Found!
Actions to achieve goal:
Action: Move to Chair, Resulting State: ('Near-Chair', 'Chair-Not-Under-Banana', 'Off-Chair', 'Empty')
Action: Push Chair under Banana, Resulting State: ('Near-Chair', 'Chair-Under-Banana', 'Off-Chair', 'Empty')
Action: Climb Chair, Resulting State: ('Near-Chair', 'Chair-Under-Banana', 'On-Chair', 'Empty')
Action: Grasp Banana, Resulting State: ('Near-Chair', 'Chair-Under-Banana', 'On-Chair', 'Holding')
Final State: ('Near-Chair', 'Chair-Under-Banana', 'On-Chair', 'Holding')
๐ป Run This Python Program
Write a Program to Implement Monkey Banana Problem using Python.
The selected Python program is loaded
automatically into the editor.
Edit the program, provide input if required
and click Run.
The compiler is embedded using OneCompiler.
Your Python program runs inside the embedded
Python environment; Study Glance does not execute
Python code directly on your web server.
If the program asks for input (for example the
starting node), type the value in the
STDIN box before clicking Run.