---
title: "General Simulation Framework"
year: "2021"
motto: "Simulation Framework"
description: "Framework for the implementation of simulation tools which offers the possibility of using and extending the same components to generate simulation projects."
stack: ["python"]
website: "https://lab.rolandoandrade.me/general-simulation-framework/"
url: "https://rolandoandrade.me/projects/general-simulation-framework/"
author: "Rolando Andrade"
---

# General Simulation Framework

> Simulation Framework

Framework for the implementation of simulation tools which offers the possibility of using and extending the same components to generate simulation projects.

Year: 2021. Stack: python. Website: https://lab.rolandoandrade.me/general-simulation-framework/

Repositories:

- [github](https://github.com/RolandoAndrade/general-simulation-framework)
- [gitlab](https://gitlab.com/RolandoAndrade/general-simulation-framework)

Every simulation project needs the same pieces. It has to track time, schedule events, change state and connect parts together. Yet each new simulation rebuilds those pieces from scratch. The work gets repeated, and every version behaves a little differently.

I saw this pattern while working on discrete event simulations. In a discrete event simulation, the state only changes at specific moments, when an event happens. The math stays the same from project to project. Only the behavior of each domain changes. A queue simulation and a cellular automaton both need event scheduling and state management. They differ in how their parts interact, not in the mechanics underneath.

So I built the General Simulation Framework. It gives you the parts every simulation needs. You focus on modeling your domain. The framework handles the plumbing.

## Models, networks and an engine

The framework supports discrete event and discrete time simulations. You define models, connect them into networks, and the simulation engine moves time forward for you.

Models are the building blocks. Each model has a state, reacts to inputs and produces outputs. The framework gives you base versions that handle the common patterns. You extend them and write only the logic of your domain.

Dynamic systems group models into networks. Models connect through weighted paths. When one model produces an output, the framework sends it to the models connected to it. You get complex interactions without wiring every event by hand.

The simulation engine runs everything. It keeps a future event list, picks which models fire next, and coordinates their state changes. You can run a simulation step by step or let it run to the end.

## Built on the DEVS formalism

The framework follows DEVS, the Discrete Event System Specification. DEVS is a mathematical foundation for discrete event simulation. It defines four functions every model must have:

- **Internal state transition**: How the model changes when it fires autonomously
- **External state transition**: How the model responds to inputs from other models
- **Time advance**: When the next autonomous event should occur
- **Output**: What the model produces when it fires

The framework also handles confluent transitions. These happen when a model gets an input at the exact moment it was going to fire on its own. By default, the internal transition runs first and the external one second. A model can change that order if it needs to.

## Scheduling the next event

The scheduler keeps upcoming events in a min heap. A min heap is a tree that always keeps the smallest value on top. Adding an event costs O(log n), and finding the next one costs O(1). When several models have events at the same time, the framework fires them together.

Each entry holds a model and its firing time. As simulation time moves forward, the scheduler subtracts the elapsed time from every pending entry. This relative time approach makes models simpler to write. A model says how long until it fires, not an absolute timestamp.

## Paths with probabilities

Paths between models can have weights. A weight of 1 means the output always goes through that path. A weight below 1 makes the route probabilistic. The framework picks a path at random, based on the weights.

That lets you model branching queues or probabilistic state machines. You set the probabilities once during setup. The framework makes the random choices while the simulation runs.

## Parts you can extend

The framework uses inheritance with care. Base types define the contracts. Concrete classes add specific behavior. The dynamic system base is abstract, and you extend it to build a system for your domain.

Report generators plug into the simulation engine and receive the outputs at each step. You can write your own for logging, visualization or analysis. The framework comes with a default reporter that prints tables.

An entity manager creates and tracks short lived objects. Simulations often create and destroy entities while they run. The entity manager gives that life cycle a clean interface.

## Example: a cellular automaton

Take a linear cellular automaton. Each cell has a binary state: alive or dead. At each time step, cells affect their neighbors.

The cell model extends the discrete time base. Its state transition copies the input from its neighbor. Its output function returns the current state. Each cell schedules itself to fire at regular intervals.

The automaton dynamic system creates the cells and wires them together. Each cell connects to its right neighbor. The rightmost cell wraps around and affects the leftmost one.

When you run the simulation, patterns move through the cells. The framework coordinates all the events. The model code holds only the rules of the automaton.

## Lessons from formal methods

This framework taught me what formal methods are worth. DEVS gave me a solid theory to build on. Its mathematical definitions turned straight into the code structure. That made the code cleaner and easier to predict.

Good abstractions take several tries. Early versions showed too many internal details. Later refactoring brought cleaner interfaces. The final API hides the complexity and stays flexible.

Python type hints documented my intent. They made the framework easier to understand and use correctly. They caught errors early and improved editor support.

Publishing to PyPI made me think about packaging and documentation. Users need clear install steps and working examples. A Google Colab notebook lets people try the framework without installing anything.

The framework is on PyPI. Install it with pip and start modeling, with the runnable examples in the documentation as your guide.

[Book a call with Rolando](https://rolandoandrade.me/pricing/)
