Skip to content

Birth-death process and M/M/1 queue

Independent M/M/1 queues.

  • Class: BirthDeathQueueSystem
  • Import: from skcomplex.systems.mathematics.stochastic_processes import BirthDeathQueueSystem
  • Field: mathematics/stochastic_processes
  • Topics: jump process, birth-death process, queueing
  • References:
    • Bertin 2021
    • Kendall 1953
    • Feller 1968

Use this system

The block imports the class and simulates the system with its default parameters and its tuned run settings.

from skcomplex.systems.mathematics.stochastic_processes import BirthDeathQueueSystem

trajectory = BirthDeathQueueSystem().prepare().simulate()

Parameters

The model parameters take these defaults, and set_params changes them.

Parameter Default
n_queues 100
arrival_rate 0.5
service_rate 1.0
n 0.0
solver 'auto'
backend 'auto'

Tags

The system with its default parameters has these capability tags, which solvers and analyses check.

Tag Values
Time continuous
Space none
Laws jump
Randomness stochastic
Domains integer
Structure fixed

Class

The source of the class shows how it builds its components from its model parameters.

class BirthDeathQueueSystem(BaseSystem):
    """Independent M/M/1 queues.

    The queues are birth-death processes with constant arrival and service rates.

    Customers arrive as a Poisson process of rate `lambda` and are served one at a time in exponential times of rate
    `mu`. For the load `rho = lambda / mu < 1` the length of an unbounded queue reaches the stationary geometric
    distribution `(1 - rho) rho^n`, of mean `rho / (1 - rho)` and variance `rho / (1 - rho)^2`, and for `rho > 1` it
    grows without bound. The system has the lengths `queues.n`.

    Args:
        n_queues:
            The number of independent queues.

        arrival_rate:
            The rate `lambda` of arrivals.

        service_rate:
            The rate `mu` of departures.

        n:
            The initial number of customers of every queue.

        label:
            The label of the system, which names it when it is nested in another one.

        solver:
            The solver of the problem, or `'auto'` for the one the system chooses.

        backend:
            The backend that evaluates the compiled functions, or `'auto'` for the one the system chooses.

    Examples:
        >>> from skcomplex.systems.mathematics.stochastic_processes import BirthDeathQueueSystem
        >>> BirthDeathQueueSystem(5).prepare().system_.elements[0].size
        5
    """

    field = 'mathematics/stochastic_processes'
    topics = ('jump process', 'birth-death process', 'queueing')
    references = ('Bertin 2021', 'Kendall 1953', 'Feller 1968')
    until = 200.0
    dt = 0.5

    def __init__(
        self,
        n_queues: int = 100,
        *,
        arrival_rate: float = 0.5,
        service_rate: float = 1.0,
        n: float = 0.0,
        label: str = 'system',
        solver: BaseSolver | str = 'auto',
        backend: BaseBackend | str = 'auto',
    ) -> None:
        super().__init__(label, solver=solver, backend=backend)
        self.n_queues = n_queues
        self.arrival_rate = arrival_rate
        self.service_rate = service_rate
        self.n = n

    def _prepare(self, context: Sequence[Observable] | None = None) -> None:
        """Set the system of the queues and their service."""
        super()._prepare(context)
        service = ServiceInteraction(
            'service',
            target='queues',
            arrival_rate=self.arrival_rate,
            service_rate=self.service_rate,
        )
        self.system_ = System(
            self.label,
            elements=[QueueElement('queues', n=self.n, size=self.n_queues)],
            interactions=[service],
        )

Components

The system uses these element and interaction classes, each with its nearest class of the core.

Elements:

Interactions:

  • ServiceInteraction, from Interaction
class QueueElement(Element):
    """Queues at desks, each holding the number `n` of customers waiting.

    Args:
        label:
            The label of the queues.

        n:
            The initial number of customers, one per queue or shared.

        size:
            The number of queues, or `None` for one.

    Examples:
        >>> from skcomplex.systems.mathematics.stochastic_processes import QueueElement
        >>> QueueElement('desk', n=3.0).n
        3.0
    """

    def __init__(self, label: str, *, n: Annotated[float, NN] = 0.0, size: int | None = None) -> None:
        super().__init__(label, size=size)
        self.n = n

class ServiceInteraction(Interaction):
    """The arrivals and departures of an M/M/1 queue.

    Customers arrive at rate `arrival_rate` and leave at rate `service_rate` while the queue is not empty.

    Args:
        label:
            The label of the interaction.

        target:
            The queues whose customers arrive and leave.

        arrival_rate:
            The rate of arrivals.

        service_rate:
            The rate of departures from a queue that is not empty.

    Examples:
        >>> from skcomplex.systems.mathematics.stochastic_processes import ServiceInteraction
        >>> ServiceInteraction('service', target='queues', arrival_rate=0.8).arrival_rate
        0.8
    """

    law_classes = (JumpLaw,)

    def __init__(
        self,
        label: str,
        *,
        target: Role = 'queues',
        arrival_rate: float = 0.5,
        service_rate: float = 1.0,
    ) -> None:
        super().__init__(label)
        self.target = target
        self.arrival_rate = arrival_rate
        self.service_rate = service_rate

    def _prepare(self, context: SimpleNamespace) -> None:
        """Set the arrival and the departure of a customer as the laws.

        Args:
            context:
                The namespace of the elements a binding of the roles names.
        """
        super()._prepare(context)
        n = context.target.n
        self.laws_: list[BaseLaw] = [
            JumpLaw(context.arrival_rate, {n: 1}),
            JumpLaw(where(n > 0, context.service_rate, 0), {n: -1}),
        ]