LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mm11_retrial.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_API_QSYS_QSYS_MM11_RETRIAL_H
6#define LINE_API_QSYS_QSYS_MM11_RETRIAL_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Joint stationary distribution of the M/M/1/1 retrial queue.
12 *
13 * Port of matlab/src/api/qsys/qsys_mm11_retrial.m. P0[n] and P1[n] are the
14 * probabilities that the orbit holds n customers with the server idle and
15 * busy, for n = 0..nmax.
16 *
17 * LINEAR (Falin and Templeton 1997, Sec. 1.2), a = lambda/nu, rho < 1:
18 * P0[n] = (1-rho)^(a+1) rho^n (a)_n / n!,
19 * P1[n] = (1-rho)^(a+1) rho^(n+1) (a+1)_n / n!
20 * (negative-binomial marginals, computed by a stable term recursion).
21 * CONSTANT (Fayolle 1986), r = lambda(lambda+nu)/(mu nu) < 1,
22 * g = lambda/(lambda+nu): P1[n] = rho r^n p00, P0[n] = g r^n p00 for
23 * n >= 1, P0[0] = p00, p00 = 1/(1 + g r/(1-r) + rho/(1-r)).
24 *
25 * With nmax < 0 the distribution is truncated once the remaining tail mass
26 * falls below 1e-12. N = Norb + rho. An unstable queue raises
27 * LINE:input:Unstable. The function needs pow and log, so it requires a
28 * transcendental number type.
29 */
30
31#include <cmath>
32#include <string>
33#include <vector>
34
36#include "line/num/number.h"
37#include "line/util/error.h"
38
39namespace line {
40namespace qsys {
41
42/** The outputs [P0, P1, N, Norb] of qsys_mm11_retrial. */
43template <class T>
45 std::vector<T> P0; ///< P0[n] = Pr(orbit = n, server idle)
46 std::vector<T> P1; ///< P1[n] = Pr(orbit = n, server busy)
47 T N; ///< mean number in system
48 T Norb; ///< mean orbit size
49};
50
51/**
52 * @param lambda arrival rate (> 0)
53 * @param mu service rate (> 0)
54 * @param nu retrial rate (> 0)
55 * @param policy LINEAR (default) or CONSTANT
56 * @param nmax truncation level; negative = automatic (tail mass < 1e-12)
57 */
58template <class T>
59Mm11RetrialResult<T> qsys_mm11_retrial(const T& lambda, const T& mu, const T& nu,
60 RetrialPolicy policy = RetrialPolicy::LINEAR, long nmax = -1) {
62 "qsys_mm11_retrial needs pow and log; use double or real");
63 using std::log;
64 using std::pow;
65 const T zero = num_traits<T>::from_int(0);
66 const T one = num_traits<T>::from_int(1);
67 if (!(lambda > zero) || !(mu > zero) || !(nu > zero))
68 throw InputError("qsys_mm11_retrial: Rates must be positive.");
69 const T rho = lambda / mu;
70 const T tailTol = num_traits<T>::from_double(1e-12);
72 if (policy == RetrialPolicy::LINEAR) {
73 if (!(rho < one))
74 throw InputError(ErrorKind("Unstable"),
75 "qsys_mm11_retrial: unstable retrial queue: rho = " +
76 std::to_string(num_traits<T>::to_double(rho)) + " >= 1");
77 const T a = T(lambda / nu);
78 out.Norb = T(rho * rho / (one - rho) + lambda * rho / (nu * (one - rho)));
79 const T c0 = T(pow(T(one - rho), T(a + one)));
80 out.P0.push_back(c0);
81 out.P1.push_back(T(rho * c0));
82 T t0 = one, t1 = one, mass = T(c0 + rho * c0);
83 long n = 0;
84 while ((nmax < 0 && T(one - mass) > tailTol) || (nmax >= 0 && n < nmax)) {
85 ++n;
86 const T nn = num_traits<T>::from_int(n);
87 t0 = T(t0 * rho * (a + nn - one) / nn);
88 t1 = T(t1 * rho * (a + nn) / nn);
89 out.P0.push_back(T(c0 * t0));
90 out.P1.push_back(T(rho * c0 * t1));
91 mass = T(mass + out.P0.back() + out.P1.back());
92 if (n > 10000000L)
93 throw InputError("qsys_mm11_retrial: Orbit distribution did not reach the tail tolerance within 1e7 levels.");
94 }
95 } else {
96 const T r = T(lambda * (lambda + nu) / (mu * nu));
97 if (!(r < one))
98 throw InputError(ErrorKind("Unstable"),
99 "qsys_mm11_retrial: unstable retrial queue: lambda*(lambda+nu)/(mu*nu) = " +
100 std::to_string(num_traits<T>::to_double(r)) + " >= 1");
101 const T g = T(lambda / (lambda + nu));
102 const T p00 = T(one / (one + g * r / (one - r) + rho / (one - r)));
103 if (nmax < 0) {
104 if (r > zero) {
105 const double lv = std::ceil(num_traits<T>::to_double(T(log(T(tailTol * (one - r))) / log(r))));
106 nmax = lv > 0 ? static_cast<long>(lv) : 0;
107 } else {
108 nmax = 0;
109 }
110 }
111 T rn = one;
112 for (long n = 0; n <= nmax; ++n) {
113 out.P1.push_back(T(rho * rn * p00));
114 out.P0.push_back(n == 0 ? p00 : T(g * rn * p00));
115 rn = T(rn * r);
116 }
117 out.Norb = T((g + rho) * p00 * r / ((one - r) * (one - r)));
118 }
119 out.N = T(out.Norb + rho);
120 return out;
121}
122
123} // namespace qsys
124} // namespace line
125
126#endif // LINE_API_QSYS_QSYS_MM11_RETRIAL_H
InputError(const std::string &what)
Definition error.h:105
The exception types the port throws.
RetrialPolicy
Retrial admission policy (RetrialPolicy in MATLAB).
@ LINEAR
each orbiting customer retries at rate alpha (orbit rate i*alpha)
Mm11RetrialResult< T > qsys_mm11_retrial(const T &lambda, const T &mu, const T &nu, RetrialPolicy policy=RetrialPolicy::LINEAR, long nmax=-1)
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Number-type abstraction for the templated API port.
The retrial policy shared by the qsys retrial functions (MATLAB RetrialPolicy).
ErrorKind(std::string n)
Definition error.h:39
The outputs [P0, P1, N, Norb] of qsys_mm11_retrial.
std::vector< T > P1
P1[n] = Pr(orbit = n, server busy).
std::vector< T > P0
P0[n] = Pr(orbit = n, server idle).