Educational Gymnasium environment modeling the classic River Crossing puzzle, plus multiple agents to interact with it: random, predefined action list, and a simple reflex strategy that solves it in exactly 15 minutes.
- Friends: Alberta (1m), Bernardo (2m), Carlos (5m), Diana (8m)
- Rules:
- The bridge holds at most 2 people per crossing
- There is a single torch that must be carried on every crossing
- Someone must return with the torch when needed
- Crossing time equals the slower person in the pair
- Goal: all four must cross in exactly 15 minutes
river_crossing_env.py: GymnasiumEnv(observation/action spaces, transitions, reward, render)agent.py: abstract agent interface (next_action(obs))input_agent.py: reads actions from the consolerandom_agent.py: generates random actions (validated by the env)action_list_agent.py: executes a predefined list of actions in ordersimple_reflex_agent.py: fixed policy that achieves the 15-minute solutionriver_crossing_utils.py: small helpers (e.g.,finish)river_crossing.ipynb: guided notebook to explore and run agents
Each action is a dictionary:
{"direction": 0|1, "person1": 0..3, "person2": 0..3}
# direction: 0 = left, 1 = right
# persons: 0=A, 1=B, 2=C, 3=D- Recommended: open
river_crossing.ipynband run cells in order.- Sections: “Input Agent”, “Random Agent”, “Action List Agent”, “Simple Reflex Agent”.
- Python 3.11+
- Gymnasium
- (Optional) Poetry
Quick install with pip:
pip install gymnasium- Random Agent: explores by sampling actions at random.
- Action List Agent: consumes a predefined sequence of actions.
- Simple Reflex Agent: deterministic policy that reaches the 15-minute goal.
Minimal script example (similar to the notebook):
from river_crossing_env import RiverCrossingEnv
from simple_reflex_agent import SimpleReflexAgent
env = RiverCrossingEnv()
agent = SimpleReflexAgent(env)
obs = env.reset()
done = False
while not done:
action = agent.next_action(obs)
obs, reward, done, info = env.step(action)
print("reward:", reward, "total_time:", info.get("time"))Gym/Gymnasium basics: Official docs