Skip to content

davemaier/gleam-SIR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SIR Epidemic Simulation - Event-Driven Agent-Based Model

An event-driven SIR (Susceptible-Infected-Recovered) epidemic simulation implemented in Gleam using fully autonomous agents. This project demonstrates discrete event systems (DEVS) principles and agent-based modeling.

What is This?

This is a simulation of how a disease spreads through a population. Each person in the simulation is an autonomous "agent" that:

  • Contacts other people at random intervals (with individual contact rates)
  • Can become infected when contacting sick people
  • Recovers after being sick for some time
  • Reports their status changes

The simulation runs in real-time and automatically ends when everyone has recovered.

Getting Started (Windows)

Prerequisites

You need to install Gleam and Erlang. Follow these steps:

1. Install Erlang

  1. Download Erlang from: https://www.erlang.org/downloads
  2. Get the Windows installer (e.g., otp_win64_26.2.exe)
  3. Run the installer and follow the prompts
  4. Accept default installation path (usually C:\Program Files\Erlang OTP\)

2. Install Gleam

Option A: Using Scoop (Recommended)

  1. Open PowerShell as Administrator

  2. Install Scoop if you don't have it:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
    Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression
  3. Install Gleam:

    scoop install gleam

Option B: Manual Installation

  1. Download Gleam from: https://github.com/gleam-lang/gleam/releases
  2. Get the Windows binary (e.g., gleam-v1.6.2-x86_64-pc-windows-msvc.zip)
  3. Extract the ZIP file
  4. Add the extracted folder to your PATH:
    • Right-click "This PC" -> Properties -> Advanced System Settings
    • Click "Environment Variables"
    • Under "System variables", find "Path" and click "Edit"
    • Click "New" and add the path where you extracted Gleam
    • Click OK on all dialogs

3. Verify Installation

Open a new Command Prompt or PowerShell window and run:

gleam --version
erl -version

Both should show version numbers without errors.

Running the Simulation

  1. Clone or download this repository

  2. Open Command Prompt or PowerShell and navigate to the project folder:

    cd path\to\actor
  3. Run the simulation:

    gleam run
  4. Watch the epidemic spread! You'll see output like:

    ═══════════════════════════════════════
    SIR Epidemic Simulation (Event-Driven)
    ═══════════════════════════════════════
    Population: 50
    Contacts per person: 10
    Initially infected: 3
    Contact rate: 2.0 per day (mean, +/-50.0% individual variation)
    Infection probability: 0.3
    Average recovery time: 10.0 days
    ═══════════════════════════════════════
    
    t=0: Person #9 became infected (from #-1)
    t=0: Person #8 became infected (from #-1)
    t=0: Person #27 became infected (from #-1)
    t=1169: Person #3 became infected (from #8)
    t=1455: Person #26 became infected (from #27)
    ...
    t=45234: Person #19 recovered
    
    ═══════════════════════════════════════
    Epidemic ended at t=58393ms
    Total infected over time: 50/50 people
    Never infected: 0 people
    ═══════════════════════════════════════
    

Understanding the Output

  • t=XXX - Logical simulation time in milliseconds (1000ms = 1 simulated day)
  • Person #N became infected (from #M) - Person N caught the disease from Person M
  • Person #N became infected (from #-1) - Person N was initially infected (no source)
  • Person #N recovered - Person N is now immune
  • Final statistics - How many people were infected in total

Customizing the Simulation

You can adjust parameters in src/actor.gleam at the top of the file:

const population_size = 50          // Number of people
const contacts_per_person = 10      // Social network size
const initial_infected = 3          // How many start sick
const lambda = 2.0                  // Mean contacts per day
const lambda_variation = 0.5        // Individual variation (+/-50%)
const beta = 0.3                    // Infection probability (0.0-1.0)
const gamma = 0.1                   // Recovery rate per day

After changing values, run gleam run again to see the new simulation.

How It Works

This simulation uses the actor model of concurrency:

  1. Person Actors (50) - Each person is an independent agent that:

    • Has their own contact rate (varies +/-50% from the mean)
    • Schedules when to contact others (exponential random distribution)
    • Decides whether they get infected based on probability
    • Schedules their own recovery time
    • Maintains their own logical clock for time synchronization
    • Reports status changes
  2. Reporter Actor (1) - Tracks the epidemic:

    • Counts currently infected people
    • Logs all infections and recoveries
    • Ends simulation when no one is infected
  3. No Global Coordinator - Agents act independently based on events

    • No "time steps" or global clock
    • Pure message passing between agents
    • Events scheduled with process.send_after()
    • Logical time synchronized via Lamport-style clocks

Logical Time Synchronization

Since actors run independently, we use a Lamport-style logical clock to maintain causal ordering:

  • Each actor maintains its own time value
  • Messages carry timestamps
  • On receiving a message: now = max(my_time, message_time)
  • This ensures causality: if A infects B, then time(A) < time(B)

This follows DEVS (Discrete Event System Specification) principles - a formal framework for event-driven simulation.

Architecture

See ARCHITECTURE.md for detailed system design documentation including:

  • State diagrams
  • Message flow examples
  • Design decisions
  • Mathematical model parameters

For a detailed code walkthrough (especially useful if you're new to Gleam), see WALKTHROUGH.md.

Troubleshooting

"gleam: command not found" or "'gleam' is not recognized"

  • Make sure you completed the installation steps above
  • Open a new terminal window after installation
  • Verify Gleam is in your PATH (see installation steps)

"Erlang not found" error

  • Make sure Erlang is installed

  • On Windows, you may need to set ERLANG_HOME environment variable:

    ERLANG_HOME=C:\Program Files\Erlang OTP\erts-14.2.1
    

Simulation never ends

  • This is expected if the epidemic doesn't reach everyone
  • Press Ctrl+C to stop the simulation
  • Try increasing beta (infection probability) to make it spread faster

Very slow simulation

  • The simulation runs in real-time (1 second = 1 simulated day)
  • Reduce population_size for faster results
  • Increase gamma (recovery rate) to make people recover faster

Learning More

About Gleam

About Agent-Based Models

  • This is a classic SIR epidemiological model
  • Each person is an autonomous agent with individual parameters
  • Uses discrete event simulation instead of time-stepping
  • Based on DEVS (Discrete Event System Specification)

About the Actor Model

  • Actors are independent processes that communicate via messages
  • Each actor has its own state and mailbox
  • No shared memory - pure message passing
  • Erlang/OTP provides excellent actor support

Development

gleam run       # Run the simulation
gleam build     # Compile the project
gleam test      # Run tests (if any)
gleam format    # Format the code

Project Structure

actor/
├── src/
│   └── actor.gleam          # Main simulation code (~460 lines)
├── ARCHITECTURE.md          # Detailed design documentation
├── WALKTHROUGH.md           # Code walkthrough for Gleam beginners
├── README.md                # This file
└── gleam.toml               # Project configuration

Questions?

This project demonstrates:

  • Event-driven programming
  • Actor model concurrency
  • Agent-based modeling
  • Discrete event simulation (DEVS)
  • Epidemiological modeling (SIR model)
  • Logical time synchronization (Lamport clocks)

About

basic implementation of a SIR model using the gleam actor model

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages