A beginner-friendly workshop for learning agent-based programming in Python. You build a train station simulation that manages passenger flow during high-demand events (like football matches).
- Setup: Read docs/setup.md to install dependencies.
- Learn the basics: Read docs/overview.md for an overview of the library.
- Explore notebooks: Start with the notebooks in
notebooks/to understand the simulation.
This model simulates train capacity management at a station during peak demand periods. You learn how agents (passengers), sensors (platform counters), and controllers (train dispatch logic) work together.
Train Network Route: The train follows a fixed route with five stops in this order:
- Entry Station 1 (pick up passengers)
- Entry Station 2 (pick up passengers)
- Entry Station 3 (pick up passengers)
- Exit Station 1 (drop off 75% of passengers)
- Exit Station 2 (drop off remaining 25% of passengers)
A new train departs every 5 minutes and follows this same route.
Passengers (Agents):
- Three entry stations feed passengers into the network
- At 19:00 (peak time): 300 passengers arrive every 10 minutes
- At 18:00 and 20:00: 150 passengers arrive every 10 minutes
- Off-peak hours: 50 passengers arrive every 10 minutes
- At exit station 1: 75% of passengers leave the train
- At exit station 2: remaining 25% of passengers leave the train
Trains:
- A train arrives at the first entry station every 5 minutes
- Each train holds a maximum of 300 passengers
- Base occupancy (non-event passengers): 16% capacity
- Trains only pick up passengers at the three entry stations
- Trains only drop off passengers at the two exit stations
- Each station platform has sensors that count waiting passengers
- Sensors report the number of people waiting every minute
- This data feeds into the control center
The train control center monitors real-time data:
- Tracks the number of waiting passengers
- Decision rule: If waiting passengers exceed 250, assign an extra train to that trip
- Allocates additional trains as needed during peak demand
The controller (dispatcher) responds to control center decisions:
- Deploys extra trains when passenger counts trigger the threshold
- Updates train schedules in real time
- Manages the flow of passengers through the network
Each component (passengers, trains, sensors, dispatcher) operates as an independent agent:
- Agents make decisions based on their own logic and current state
- Agents communicate through MQTT messages (see below)
- Agents run in parallel and react to events asynchronously
- You extend the simulation by creating new agents or modifying existing ones
Agents communicate through MQTT pub/sub messaging:
- Passengers publish arrival events (I arrived at platform X at time Y)
- Trains publish position and capacity updates (I am at station Z with N passengers)
- Sensors publish observations (Platform X has N waiting passengers)
- Control Center publishes dispatch decisions (Assign extra train to route A)
- Dispatcher subscribes to control center decisions and executes them
The library provides helpers in simulated_city.mqtt to simplify publishing and subscribing.
Simulation parameters are controlled through two files:
config.yaml— Non-secret settings (MQTT broker host, port, base topic, passenger arrival rates, train capacity, decision rules).env— Secrets like MQTT credentials (kept out of version control)
Edit config.yaml to change simulation behavior without modifying code.
Start your environment:
source .venv/bin/activateRun in a notebook (recommended for learning):
python -m jupyterlabOpen a notebook in notebooks/ and follow the exercises.
Run a demo script:
python scripts/demo/01_config_and_mqtt.pyRun tests:
python -m pytest- Python fundamentals:
notebooks/00_python_fundamentals.ipynb - Simulated City basics:
notebooks/01_simulated_city_basics.ipynb - Mapping with MapLibre:
notebooks/02_maplibre_city_hall_random_walk.ipynb - MQTT and real-time agents:
notebooks/03_mqtt_random_walk/ - Advanced agents and weather:
notebooks/04_advanced_random_walk/
src/simulated_city/— the library modules you importnotebooks/— workshop exercises and interactive examplesdocs/— handouts, setup guides, and exercise descriptionstests/— basic sanity checksscripts/demo/— example simulations you can run
- Read docs/setup.md to set up your environment
- Open
notebooks/00_python_fundamentals.ipynbto start learning - Check
docs/exercises.mdfor hands-on tasks