Skip to content

How do I model a job made of several short steps?

A bake, a wash or an inspection often runs through a fixed number of steps in a row. An Erlang time is the sum of k exponential steps. The script compares four steps of 0.3 minutes with one exponential step of 1.2 minutes.

from nagasim import build as nb


def wait_with(service):
    m = nb.Model(time_limit=480, warmup=0)
    m.add_resource(name="Oven")
    arrive = m.create(distribution=nb.Exponential(rate=0.8))
    arrive >> m.process(resource_name="Oven", distribution=service) >> m.dispose()
    return m.run(replications=20, seed=1).kpi("resource/Oven/Time in Queue Avg")


# A bake is four steps, each exponential with a mean of 0.3 minutes: Erlang(k=4, rate=1/0.3).
# The same 1.2-minute mean as one exponential step, but with a quarter of the variance.
print(f"Erlang, 4 steps:  mean wait {wait_with(nb.Erlang(k=4, rate=1 / 0.3)):.2f} minutes")
print(f"one exponential:  mean wait {wait_with(nb.Exponential(rate=1 / 1.2)):.2f} minutes")

Save it as recipe-erlang-steps.py and run it with python. The script seeds itself, so the numbers below are the same on your machine.

Erlang, 4 steps:  mean wait 9.02 minutes
one exponential:  mean wait 11.57 minutes

How it works

  1. Erlang(k, rate) adds k exponential steps that each have the given rate, so its mean is k / rate.
  2. Both services average 1.2 minutes, but the Erlang has a quarter of the variance, so the queue waits less.
  3. Raise k for a more regular job; at k = 1 it is the exponential again.

Elements used

Back to all recipes.