Skip to content

ChzCharbel/BoardGameSimulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

16 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Multi-Agent Fire Rescue Simulation

Live Demo Python Flask Mesa

A sophisticated Multi-Agent System (MAS) implementation using Python and the Mesa framework. This interactive web application demonstrates autonomous agent coordination, emergent behavior, and complex problem-solving in a dynamic environment based on the Fire Rescue board game.

๐ŸŒ Live Demo

https://fire-rescue-simulation.onrender.com/

Note: The free tier on Render may take ~30 seconds to wake up if the service has been idle.

๐Ÿค– System Overview

This simulation showcases key concepts in Agent-Based Modeling (ABM) and Multi-Agent Systems:

  • Autonomous Agents: 6 firefighter agents with individual decision-making capabilities
  • Emergent Behavior: Complex coordination patterns arise from simple interaction rules
  • Dynamic Role Assignment: Agents adapt between Rescuer and Extinguisher roles
  • Intelligent Pathfinding: Dijkstra algorithm for navigation through changing environments
  • Real-time Coordination: Agents collaborate without centralized control
  • Interactive Web Interface: Real-time visualization with Flask and WebSockets

โœจ Features

Real-time Simulation

  • Interactive Grid: Live updates of agent positions and environment
  • Multi-Agent Tracking: Individual agent status and role indicators
  • Environmental Dynamics: Fire spread, smoke generation, structural damage
  • Manual & Automatic Control: Step-by-step or continuous simulation modes
  • Performance Metrics: Rescue statistics, coordination efficiency

Advanced Agent Behaviors

  • Intelligent Navigation: Dynamic pathfinding through changing environments
  • Role Adaptation: Context-aware switching between rescue and firefighting
  • Resource Management: Action point optimization and planning
  • Collision Avoidance: Spatial coordination between multiple agents

Web Application Features

  • WebSocket Communication: Real-time multi-client synchronization
  • Session Management: Multiple concurrent simulations
  • Responsive Design: Works on desktop and mobile devices
  • Bootstrap UI: Modern, professional interface

๐Ÿ—๏ธ Technical Architecture

Core Components

FireRescueModel (Mesa.Model)
โ”œโ”€โ”€ MultiGrid Environment (8x6 spatial grid)
โ”œโ”€โ”€ RandomActivation Scheduler
โ”œโ”€โ”€ Agent Population (6 FirefighterAgents)
โ””โ”€โ”€ Environmental Systems (fire, walls, victims)

Agent Intelligence Architecture

Each FireAgent implements:

  • Role-Based Behavior: Dynamic switching between RESCUER and EXTINGUISHER roles
  • Environmental Awareness: Fire detection, POI tracking, exit navigation
  • Dijkstra Pathfinding: Optimal route calculation with wall/door costs
  • Action Point Management: 4 AP per turn with cost-aware decision making
  • Knockout Recovery: Respawn system after fire exposure (2 turn timer)

๐Ÿง  Multi-Agent Implementation

Agent Behavior Patterns

class FireAgent(Agent):
    def __init__(self, unique_id, model):
        self.action_points = 4              # 4 AP per turn
        self.role = None                    # RESCUER or EXTINGUISHER
        self.target_poi = None              # Current POI target
        self.carrying_victim = None         # Victim being carried
        self.knockout_timer = 0             # 2-turn knockout system
        self.path = []                      # Dijkstra-computed path

    def step(self):
        if self.is_knocked_out():           # Check knockout status
            self.update_knockout()          # Decrement timer, respawn
            return
        self.assign_role()                  # Based on environment state
        if self.role == FireFighterRole.RESCUER:
            self.rescuer_behavior()         # Find/carry victims to exits
        else:
            self.extinguisher_behavior()    # Find and extinguish fires

Emergent Properties

The simulation demonstrates several emergent behaviors:

  1. Dynamic Role Distribution: Agents switch between RESCUER/EXTINGUISHER based on fire and POI counts
  2. Decentralized Coordination: No central controller - agents react to local environment state
  3. Adaptive Pathfinding: Routes recalculated when environment changes (fire spread, doors opened)
  4. Resource Optimization: Agents balance fire extinguishing vs victim rescue based on AP costs

๐Ÿ”ฌ Simulation Parameters

Parameter Value Description
Grid Size 8ร—6 cells Spatial environment dimensions
Agent Count 6 firefighters Multi-agent population size
Action Points 4 per turn Resource constraint per agent
POIs 10 victims + 5 false alarms Points of Interest to discover
Victims to Win 7 rescued Win condition
Max Losses 4 victims Lose condition
Max Damage 24 structural Building collapse threshold
Knockout Timer 2 turns Recovery time after fire exposure
Fireโ†’Smoke Cost 1 AP Reduce fire to smoke
Fireโ†’Clear Cost 2 AP Fully extinguish fire
Move Cost 1 AP Basic movement
Open Door Cost 1 AP Open closed door

๐Ÿ› ๏ธ Installation & Usage

Requirements

Python 3.11+
Mesa 3.0.3
Flask 3.0.0
Flask-SocketIO 5.3.6
NumPy 1.24+
python-dotenv 1.0+
gunicorn (production)
gevent-websocket (production)

Quick Start

# Clone the repository
git clone [repository-url]
cd BoardGameSimulation

# Install dependencies
pip install -r backend/requirements.txt

# Configure environment (creates .env file)
python setup.py

# Launch simulation
cd backend
python app.py

Alternative Start Methods

# Windows
run.bat

# Linux/Mac
bash run.sh

Development Setup

# Create development environment
python -m venv .venv
source .venv/bin/activate  # Windows: .venv\Scripts\activate

# Install dependencies
pip install -r backend/requirements.txt

# Configure environment variables
cp .env.example .env
# Edit .env with your settings

# Run application
cd backend
python app.py

๐ŸŽฎ How to Use

  1. Home Page: Click "Launch Simulation" to create a new simulation

  2. Simulation Controls:

    • Step: Execute one simulation step manually
    • Auto: Start/pause automatic continuous simulation
    • Stop: Stop automatic simulation
    • Reset: Create a new simulation
  3. Game Board Interpretation:

    • Light Brown: Clean spaces
    • Gray with dots: Smoke
    • Red/Orange animated: Fire
    • Blue circles: Rescuer firefighters
    • Red circles: Extinguisher firefighters
    • Pink squares: Victims
    • Orange squares: False alarms
  4. Game Rules:

    • Objective: Rescue 7 victims before losing 4 or taking >24 structural damage
    • Agent Actions: Each firefighter has 4 action points per turn
    • Role Assignment: Agents automatically become Rescuers or Extinguishers
    • Fire Dynamics: Fire spreads automatically each round
    • Knockout System: Agents in fire are unconscious for 2 turns

๐ŸŒ Web Application Structure

BoardGameSimulation/
โ”œโ”€โ”€ backend/                    # Python Flask backend
โ”‚   โ”œโ”€โ”€ app.py                  # Flask application & WebSocket server
โ”‚   โ”œโ”€โ”€ config.py               # Configuration management
โ”‚   โ”œโ”€โ”€ requirements.txt        # Python dependencies
โ”‚   โ”œโ”€โ”€ logs/                   # Application logs
โ”‚   โ””โ”€โ”€ models/                 # Mesa agent-based models
โ”‚       โ”œโ”€โ”€ fireRescueModel.py  # Main model class
โ”‚       โ”œโ”€โ”€ fireAgent.py        # Agent implementation
โ”‚       โ”œโ”€โ”€ fireState.py        # Environment states
โ”‚       โ”œโ”€โ”€ firefighterRole.py  # Agent roles
โ”‚       โ””โ”€โ”€ poi.py              # Points of interest
โ”‚
โ”œโ”€โ”€ frontend/                   # Static frontend assets
โ”‚   โ”œโ”€โ”€ static/
โ”‚   โ”‚   โ”œโ”€โ”€ css/style.css       # Stylesheets
โ”‚   โ”‚   โ””โ”€โ”€ js/
โ”‚   โ”‚       โ”œโ”€โ”€ main.js         # Main page logic
โ”‚   โ”‚       โ””โ”€โ”€ simulation.js   # Simulation interface logic
โ”‚   โ””โ”€โ”€ templates/
โ”‚       โ”œโ”€โ”€ index.html          # Technical overview page
โ”‚       โ””โ”€โ”€ simulation.html     # Simulation interface
โ”‚
โ”œโ”€โ”€ setup.py                    # Setup script
โ”œโ”€โ”€ run.bat                     # Windows launcher
โ”œโ”€โ”€ run.sh                      # Unix launcher
โ”œโ”€โ”€ .env                        # Environment variables
โ””โ”€โ”€ DOCUMENTATION.md            # Detailed documentation

๐Ÿ”ง API Endpoints

  • POST /api/create_simulation - Create new simulation
  • GET /api/simulation/<id>/state - Get simulation state
  • POST /api/simulation/<id>/step - Execute simulation step
  • POST /api/simulation/<id>/auto_start - Start automatic mode
  • POST /api/simulation/<id>/auto_stop - Stop automatic mode
  • DELETE /api/simulation/<id>/delete - Delete simulation

๐Ÿ›ก๏ธ Configuration

The application uses environment variables for configuration:

# Flask Configuration
FLASK_ENV=development
FLASK_DEBUG=True
SECRET_KEY=your-secret-key

# Simulation Parameters
MAX_SIMULATIONS=100
DEFAULT_STEP_DELAY=2.0
MAX_FIREFIGHTERS=6
VICTIMS_TO_WIN=7

๐Ÿ“Š Technical Concepts Demonstrated

  • Agent-Based Modeling: Using Mesa framework for autonomous agent simulation
  • Dijkstra Pathfinding: Optimal route calculation with weighted edges (walls, doors)
  • Real-time WebSockets: Flask-SocketIO for live simulation updates
  • Role-Based AI: Dynamic behavior switching based on environment state
  • Multi-Agent Coordination: Decentralized decision-making without central control

๐Ÿ” Troubleshooting

Common Issues

  1. Port 5000 in use: Change FLASK_PORT in .env
  2. Dependencies missing: Run pip install -r requirements.txt
  3. Permission errors: Use virtual environment
  4. WebSocket errors: Check firewall settings

Development Tips

  • Use FLASK_DEBUG=True for development
  • Monitor console for agent behavior logs
  • Use browser developer tools for WebSocket debugging

๐Ÿ“ License

MIT License - See LICENSE file for details

๐Ÿค Contributing

Contributions welcome! Areas of interest:

  • Advanced agent learning algorithms
  • Alternative coordination mechanisms
  • New environmental challenges
  • Performance optimization
  • Visualization enhancements
  • Mobile responsiveness improvements

About

A multiagent system which simulates a game of FireRescue

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors