Implementing a M/M/c Queue in Ciw

Simulation
Discrete-Event-Simulation
Queueing-Networks
Ciw
Author

Galen Seilis

Published

December 20, 2023

Ciw is a Python package for simulating queueing networks. A M/M/c queue can be implemented and simulated using Ciw (Kendall 1953) in the following way.

"""Simulate an M/M/c queue using Ciw."""

from __future__ import annotations

import ciw


def run_simulation(
    seed: int = 2018,
    arrival_time: float = 1.0,
    service_time: float = 0.5,
    num_servers: int = 2,
    horizon: float = 365.0,
) -> list:
    """Run a simulation of an M/M/c queue and return all records.

    Parameters
    ----------
    seed
        Random seed for reproducibility.
    arrival_time
        Mean interarrival time for the exponential distribution.
    service_time
        Mean service time for the exponential distribution.
    num_servers
        Number of servers.
    horizon
        Maximum simulation time.

    Returns
    -------
    list
        All simulation records.
    """
    ciw.seed(seed)

    network = ciw.create_network(
        arrival_distributions=[ciw.dists.Exponential(arrival_time)],
        service_distributions=[ciw.dists.Exponential(service_time)],
        number_of_servers=[num_servers],
    )

    simulation = ciw.Simulation(network)
    simulation.simulate_until_max_time(horizon)
    return simulation.get_all_records()


def main() -> None:
    """Execute the simulation and print results."""
    records = run_simulation()
    print(records)


if __name__ == "__main__":
    main()

References

Kendall, David G. 1953. “Stochastic Processes Occurring in the Theory of Queues and Their Analysis by the Method of the Imbedded Markov Chain.” The Annals of Mathematical Statistics 24 (3): 338–54. https://doi.org/10.1214/aoms/1177728975.