Skip to main content

A Discrete Event Simulation (DES) library for modeling systems where state changes occur at discrete points in time.

Project description

DeSim (Discrete Event Simulator)

A Discrete Event Simulation (DES) library for modeling systems where state changes occur at discrete points in time.

Description

DeSim is a flexible Discrete Event Simulation (DES) library built for modeling systems where state changes occur at specific points in time. At its core is a queue of events that governs how the simulation evolves, processing scheduled actions in chronological order to reflect changes in system state. This method allows DeSim to simulate dynamic environments with precision and efficiency, focusing computation only when activity occurs, and so, mirroring the complexity of real-world systems without imposing rigid structures.

Using DeSim involves defining system behavior through object-oriented design. At the core of the library is a DiscreteEvent base class, which users extend to model specific types of events. Each derived event class encapsulates the logic needed to evolve the system, such as modifying state variables, scheduling future events, or interacting with other components. This design encourages clean separation of concerns and makes simulations more maintainable and reusable. By modeling system dynamics as a series of interacting event classes, DeSim allows users to build rich, modular simulations that are both intuitive and scalable.

Features

  • 📦 OOP: Object-oriented paradigm was taken in mind in order to allow flexibility while modeling dynamic systems.
  • ⏱️ Discrete time: Time updates are based on occurrences of events, and not on while True: update(deltatime) approaches.
  • ⏩ Queue-based: Events are retrieved from a priority queue that sorts them based on their time of occurrence.
  • 🍃 Lightweight: DeSim has no external dependencies.

Quick Example

Suppose we want to model a store and the flow of customers that arrive over time. We can model this by implementing events like: the store opened, a customer arrived, a customer is ready to pay and leave the store, the store closed, etc. So, the execution of one customer arrival could lead to the next one, and also to generate an event for when that customer is done. We just need to override the _run(self, env) method from the DiscreteEvent base class to model all of those interactions.

from desim import DiscreteEvent, DiscreteEventQueue, run_to_end

#
# ...
#

class CustomerArrivalEvent(DiscreteEvent):
    def __init__(self, t):
        super().__init__(t)

    def _run(self, env):
        store, stats = env

        if store.is_open():
            store.customer_enters()
            print(f"{store.customers=}")

            next_arrival = CustomerArrivalEvent(self._after(stats.random_arrival_time()))
            customer_done = CustomerDoneEvent(self._after(stats.random_shopping_time()))
            return [next_arrival, customer_done]

        else:
            print("Oops, too late")
            return []

#
# ...
#

store = Store()
stats = Stats(mean_arrival_time=1, mean_shopping_time=5)

queue = DiscreteEventQueue()
queue.add_events(
    [
        OpenStoreEvent(0),
        ChangeArrivalTimeEvent(20, 0.5),  # rush-hour
        ChangeArrivalTimeEvent(45, 1.5),  # end of rush-hour
        CloseStoreEvent(60),
    ]
)

t = []
customers = []

def callback(_, __):
    t.append(queue.t)
    customers.append(store.customers)

run_to_end(queue, (store, stats), callback)

Installation

You can install DeSim using either PyPI or clone it from GitHub.

From PyPI (recommended):

pip install desim-python

From GitHub:

git clone https://github.com/larias95/desim-python.git
cd desim-python
pip install .

Examples

After installation you can run examples from the examples/ folder. E.g: python store_example.py.

You may need to install some dependencies before running the examples by using pip install -r requirements.txt from examples/ folder.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

desim_python-1.0.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

desim_python-1.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file desim_python-1.0.0.tar.gz.

File metadata

  • Download URL: desim_python-1.0.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for desim_python-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1e414d133c2363a8af9f9bb8ec5512e7e7e481f481d100a2b1a938a913dd69e5
MD5 aab93724eb9bba189ba04ee55ba67fe4
BLAKE2b-256 7bc4caf2c2dfeafaea262a2a19ba4016922725e09052880b164d04141cbc7c02

See more details on using hashes here.

File details

Details for the file desim_python-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: desim_python-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for desim_python-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 086523c9fc817f4731ba0757dc5e6b8d80a9665378496ee72ab7f14d7a081240
MD5 deea734f8420df476e6819274406e36e
BLAKE2b-256 0fc68a7a773b5046c8af5fb71e6f8a99f8f2f1aa7267c1c1187d899bc972f017

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page