Implementing a M/G/k 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.

Since G in M/G/k can be any distribution with non-negative support, we will use a gamma distribution for the sake of example. A gamma distribution is the result of a sum of independent exponentially-distributed random variables, and thus for this example we have an interpretation that the servicing is implicitly a multi-step process where each step has an exponentially-distributed completion time.

A M/G/k queue as described can be implemented and simulated using Ciw (Kendall 1953) in the following way.

"""Simulate an M/G/k queue using Ciw."""

from __future__ import annotations

import ciw


def run_simulation(
    seed: int = 2018,
    arrival_time: float = 1.0,
    service_shape: float = 0.6,
    service_scale: float = 1.2,
    num_servers: int = 2,
    horizon: float = 365.0,
) -> list:
    """Run a simulation of an M/G/k queue and return all records.

    Parameters
    ----------
    seed
        Random seed for reproducibility.
    arrival_time
        Mean interarrival time for the exponential distribution.
    service_shape
        Shape parameter for the gamma service distribution.
    service_scale
        Scale parameter for the gamma service 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.Gamma(shape=service_shape, scale=service_scale)
        ],
        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.