Implementing a G/M/1 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 G/M/1 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 arrivals are implicitly the result of a multi-step process where each step has an exponentially-distributed completion time.

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

"""G/M/1 queue simulation logic."""
import ciw


def run_simulation(
    arrival_shape=6 / 10,
    arrival_scale=12 / 10,
    service_time=1,
    horizon=365,
    seed=2018,
):
    """Run G/M/1 queue simulation and return simulation object and records."""
    ciw.seed(seed)

    network = ciw.create_network(
        arrival_distributions=[ciw.dists.Gamma(shape=arrival_shape, scale=arrival_scale)],
        service_distributions=[ciw.dists.Exponential(service_time)],
        number_of_servers=[1],
    )

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


def main():
    """Main entry point for the simulation."""
    _, records = run_simulation()
    # Output to match what the post might expect; print records summary if needed
    for record in records[:10]:  # Print first few records to show output
        print(record)


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.