Basics of Discrete Event Simulation using SimPy in Python

SimPy (rhymes with "Blimpie") is a Python package for process-oriented discrete-event simulation. It allows you to model real-world systems like queues, networks, and resource allocation scenarios.

Installation

The easiest way to install SimPy is via pip ?

pip install simpy

To upgrade an existing installation ?

pip install -U simpy

Note: You need Python 2.7 or above. For Linux/Unix/MacOS, you may need root privileges to install SimPy.

To verify the installation, open a Python shell and import simpy ?

import simpy
print("SimPy installed successfully!")
SimPy installed successfully!

Basic Concepts

In SimPy, active entities are modeled as processes. A process is a Python generator that yields discrete events. When a process yields an event, it gets suspended until that event is triggered.

Understanding Generators

SimPy processes use Python generators. Here's a simple example ?

def simple_generator(x):
    y = yield x + 1
    return y

# Create and use generator
gen = simple_generator(5)
result = next(gen)
print(f"Generator yielded: {result}")
Generator yielded: 6

Basic SimPy Example

Let's create a simple conference attendee simulation where people attend talks and take breaks ?

import simpy
from random import randint

# Configuration
TALKS_PER_SESSION = 3
TALK_LENGTH = 30
BREAK_LENGTH = 15
ATTENDEES = 1

def attendee(env, name, knowledge=0, hunger=0):
    talks = 0
    
    # Attend talks
    for i in range(TALKS_PER_SESSION):
        print(f'Talk {talks+1} begins at time {env.now}')
        
        # Simulate learning and getting hungry
        knowledge += randint(0, 3) / (1 + hunger)
        hunger += randint(1, 4)
        talks += 1
        
        # Wait for talk to finish
        yield env.timeout(TALK_LENGTH)
        print(f'Talk {talks} ends at time {env.now}')
    
    print(f'Attendee {name} finished talks with knowledge {knowledge:.2f} and hunger {hunger:.2f}')
    
    # Take a break
    food = randint(3, 12)
    hunger -= min(food, hunger)
    yield env.timeout(BREAK_LENGTH)
    print(f'Attendee {name} finished break with hunger {hunger:.2f}')

# Run simulation
env = simpy.Environment()
for i in range(ATTENDEES):
    env.process(attendee(env, i))

env.run(until=150)
Talk 1 begins at time 0
Talk 1 ends at time 30
Talk 2 begins at time 30
Talk 2 ends at time 60
Talk 3 begins at time 60
Talk 3 ends at time 90
Attendee 0 finished talks with knowledge 1.25 and hunger 8
Attendee 0 finished break with hunger 2

How SimPy Works

The simulation works as follows:

  • Environment: The simulation environment tracks time and manages events
  • Processes: Generator functions that yield events to pause execution
  • Events: Objects that represent something happening at a specific time
  • Timeout: A type of event that triggers after a specified time delay

Key Components

Component Purpose Example
Environment Manages simulation time and events env = simpy.Environment()
Process Active entity in simulation env.process(attendee(env, "John"))
Timeout Wait for specified time yield env.timeout(30)
Run Execute simulation env.run(until=100)

Conclusion

SimPy provides a powerful framework for discrete-event simulation using Python generators. The key concept is yielding events to pause processes, allowing the simulation to manage time and coordinate multiple activities efficiently.

Updated on: 2026-03-25T05:13:44+05:30

847 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements