LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mm1k_transient.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_MM1K_TRANSIENT_H
6#define LINE_API_QSYS_QSYS_MM1K_TRANSIENT_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Transient queue-length distribution of the M/M/1/K queue.
12 *
13 * Port of matlab/src/api/qsys/qsys_mm1k_transient.m (native Python twin:
14 * python/line_solver/api/qsys/transient.py). K counts the job in service; the
15 * queue starts with N0 jobs and P(r, j) = P(N(t_r) = j | N(0) = N0),
16 * j = 0..K, for any load rho = lambda/mu. P(:, K) is the time-t blocking
17 * probability seen by a Poisson arrival and tends to the stationary loss
18 * probability of qsys_mm1k_loss; res.pi is the stationary law.
19 *
20 * Methods:
21 * 'spectral' the eigen-expansion of Takacs, as given in Rubino 2020 eqs.
22 * (3.5)-(3.6), for any N0 = i:
23 * P_ij(t) = pi_j + 2/(K+1) rho^{(j-i)/2} sum_{h=1}^{K}
24 * e^{g_h t} w_h(i) w_h(j)/(1 + rho - 2 sqrt(rho) cos th_h),
25 * th_h = h pi/(K+1), g_h = -(lambda+mu) + 2 sqrt(lambda mu) cos th_h,
26 * w_h(j) = sqrt(rho) sin((j+1) th_h) - sin(j th_h),
27 * valid for rho = 1 as well. kappa bounds the amplification of the
28 * rho^{(j-i)/2} prefactors; the absolute error is about kappa*eps.
29 * 'uniformization' Jensen's method on the K+1 states at rate lambda+mu
30 * (Rubino 2020 Sec. 2.2; Rubino-Krinik 2021 Sec. 2) with a rigorously
31 * bounded Poisson window: absolute error <= errBound (<= tol).
32 * 'auto' 'spectral' when kappa*eps <= tol, else 'uniformization'.
33 *
34 * ARITHMETIC: double only.
35 *
36 * References: G. Rubino, "Transient analysis of Markovian queueing systems: a
37 * survey with focus on closed-forms and uniformization", Advanced Trends in
38 * Queueing Theory 2, ISTE/Wiley, 2020; L. Takacs, Introduction to the Theory
39 * of Queues, Oxford University Press, 1962.
40 */
41
42#include <algorithm>
43#include <cctype>
44#include <cmath>
45#include <cstddef>
46#include <limits>
47#include <string>
48#include <vector>
49
51#include "line/util/error.h"
52#include "line/util/matrix.h"
53
54namespace line {
55namespace qsys {
56
57/** Return value of qsys_mm1k_transient: [P, EN, P0, res]. */
59 Matrix<double> P; ///< (nt x K+1) probabilities
60 std::vector<double> EN; ///< (nt) mean number in system
61 std::vector<double> P0; ///< (nt) empty probability
62 double rho = 0.0;
63 std::string method; ///< 'spectral' or 'uniformization'
64 double kappa = 0.0;
65 std::vector<double> errBound; ///< per time
66 std::vector<double> pi; ///< (K+1) stationary law
67 double tol = 0.0;
68};
69
70/**
71 * @brief Transient distribution of the M/M/1/K queue (Takacs; Rubino 2020).
72 *
73 * @param lambda arrival rate, finite and positive
74 * @param mu service rate, finite and positive
75 * @param K capacity, integer >= 1
76 * @param n0 initial number in system, integer in [0, K]
77 * @param t times, finite and >= 0
78 * @param tol tolerance in (0, 1)
79 * @param method 'auto', 'spectral' or 'uniformization'
80 */
81inline Mm1kTransientResult qsys_mm1k_transient(double lambda, double mu, double K, double n0,
82 const std::vector<double>& t, double tol = 1e-14,
83 const std::string& method_in = "auto") {
84 using namespace detail;
85 const char* fn = "qsys_mm1k_transient";
86 if (!std::isfinite(lambda) || lambda <= 0) transient_arg_error(fn, "lambda must be finite and positive");
87 if (!std::isfinite(mu) || mu <= 0) transient_arg_error(fn, "mu must be finite and positive");
88 if (!transient_is_int(K) || K < 1) transient_arg_error(fn, "K must be an integer >= 1");
89 if (!transient_is_int(n0) || n0 < 0) transient_arg_error(fn, "n0 must be an integer >= 0");
90 if (n0 > K) transient_arg_error(fn, "n0 must not exceed K");
91 if (t.empty()) transient_arg_error(fn, "t must be a nonempty vector of finite nonnegative times");
92 for (double tv : t)
93 if (!std::isfinite(tv) || tv < 0)
94 transient_arg_error(fn, "t must be a nonempty vector of finite nonnegative times");
95 if (!std::isfinite(tol) || tol <= 0 || tol >= 1) transient_arg_error(fn, "tol must lie in (0, 1)");
96 std::string method = method_in;
97 std::transform(method.begin(), method.end(), method.begin(),
98 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
99 if (method != "auto" && method != "spectral" && method != "uniformization")
100 transient_arg_error(fn, "method must be 'auto', 'spectral' or 'uniformization'");
101 const std::size_t Kn = static_cast<std::size_t>(K);
102 const std::size_t i = static_cast<std::size_t>(n0);
103 const std::size_t nt = t.size();
104 const double rho = lambda / mu, sr = std::sqrt(rho), logrho = std::log(rho);
105
107 res.rho = rho;
108 res.tol = tol;
109 // stationary law (the t -> inf limit), in the log domain
110 res.pi.assign(Kn + 1, 0.0);
111 if (rho == 1.0) {
112 for (double& v : res.pi) v = 1.0 / static_cast<double>(Kn + 1);
113 } else {
114 double emax = -std::numeric_limits<double>::infinity();
115 for (std::size_t j = 0; j <= Kn; ++j) emax = std::max(emax, static_cast<double>(j) * logrho);
116 double s = 0.0;
117 for (std::size_t j = 0; j <= Kn; ++j) s += (res.pi[j] = std::exp(static_cast<double>(j) * logrho - emax));
118 for (double& v : res.pi) v /= s;
119 }
120 std::vector<double> th(Kn), den(Kn);
121 for (std::size_t h = 0; h < Kn; ++h) {
122 th[h] = static_cast<double>(h + 1) * kTransientPi / static_cast<double>(Kn + 1);
123 den[h] = 1.0 + rho - 2.0 * sr * std::cos(th[h]);
124 }
125 double A = 0.0;
126 for (std::size_t j = 0; j <= Kn; ++j)
127 A = std::max(A, std::exp(0.5 * (static_cast<double>(j) - static_cast<double>(i)) * logrho));
128 double ksum = 0.0;
129 for (std::size_t h = 0; h < Kn; ++h)
130 ksum += 2.0 * (1.0 + sr) * (1.0 + sr) / (static_cast<double>(Kn + 1) * den[h]);
131 res.kappa = A * ksum + 1.0;
132 if (method == "auto") method = res.kappa * kTransientEps <= tol ? "spectral" : "uniformization";
133 res.method = method;
134 res.P = Matrix<double>(nt, Kn + 1, 0.0);
135 res.errBound.assign(nt, 0.0);
136 if (method == "spectral") {
137 std::vector<double> g(Kn);
138 for (std::size_t h = 0; h < Kn; ++h) g[h] = -(lambda + mu) + 2.0 * std::sqrt(lambda * mu) * std::cos(th[h]);
139 Matrix<double> W(Kn + 1, Kn, 0.0);
140 for (std::size_t j = 0; j <= Kn; ++j)
141 for (std::size_t h = 0; h < Kn; ++h)
142 W(j, h) = sr * std::sin(static_cast<double>(j + 1) * th[h]) - std::sin(static_cast<double>(j) * th[h]);
143 std::vector<double> pref(Kn + 1), coef(Kn);
144 for (std::size_t j = 0; j <= Kn; ++j)
145 pref[j] = std::exp(0.5 * (static_cast<double>(j) - static_cast<double>(i)) * logrho);
146 for (std::size_t h = 0; h < Kn; ++h) coef[h] = W(i, h) / den[h];
147 for (std::size_t r = 0; r < nt; ++r) {
148 std::vector<double> e(Kn);
149 for (std::size_t h = 0; h < Kn; ++h) e[h] = std::exp(g[h] * t[r]) * coef[h];
150 for (std::size_t j = 0; j <= Kn; ++j) {
151 double s = 0.0;
152 for (std::size_t h = 0; h < Kn; ++h) s += W(j, h) * e[h];
153 res.P(r, j) = res.pi[j] + 2.0 / static_cast<double>(Kn + 1) * pref[j] * s;
154 }
155 res.errBound[r] = res.kappa * kTransientEps;
156 }
157 } else {
158 const double Lam = lambda + mu, p = lambda / Lam, q = mu / Lam;
159 std::vector<PoissonWindow> ws(nt);
160 long nRmax = 0;
161 for (std::size_t r = 0; r < nt; ++r) {
162 ws[r] = transient_poiswin(Lam * t[r], tol);
163 res.errBound[r] = ws[r].bound;
164 nRmax = std::max(nRmax, ws[r].nR);
165 }
166 std::vector<double> v(Kn + 1, 0.0), vn(Kn + 1, 0.0);
167 v[i] = 1.0;
168 for (long n = 0; n <= nRmax; ++n) {
169 for (std::size_t r = 0; r < nt; ++r)
170 if (n >= ws[r].nL && n <= ws[r].nR) {
171 const double wn = ws[r].w[static_cast<std::size_t>(n - ws[r].nL)];
172 for (std::size_t j = 0; j <= Kn; ++j) res.P(r, j) += wn * v[j];
173 }
174 if (n < nRmax) {
175 vn[0] = q * v[0] + q * v[1];
176 for (std::size_t j = 1; j < Kn; ++j) vn[j] = p * v[j - 1] + q * v[j + 1];
177 vn[Kn] = p * v[Kn - 1] + p * v[Kn];
178 v.swap(vn);
179 }
180 }
181 }
182 res.EN.assign(nt, 0.0);
183 res.P0.assign(nt, 0.0);
184 for (std::size_t r = 0; r < nt; ++r) {
185 for (std::size_t j = 0; j <= Kn; ++j) res.EN[r] += res.P(r, j) * static_cast<double>(j);
186 res.P0[r] = res.P(r, 0);
187 }
188 return res;
189}
190
191} // namespace qsys
192} // namespace line
193
194#endif // LINE_API_QSYS_QSYS_MM1K_TRANSIENT_H
The exception types the port throws.
Dense matrix and non-owning view.
Mm1kTransientResult qsys_mm1k_transient(double lambda, double mu, double K, double n0, const std::vector< double > &t, double tol=1e-14, const std::string &method_in="auto")
Transient distribution of the M/M/1/K queue (Takacs; Rubino 2020).
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Transient queue-length distribution of the M/M/1 queue.
Return value of qsys_mm1k_transient: [P, EN, P0, res].
std::vector< double > EN
(nt) mean number in system
std::vector< double > P0
(nt) empty probability
std::string method
'spectral' or 'uniformization'
std::vector< double > pi
(K+1) stationary law
Matrix< double > P
(nt x K+1) probabilities
std::vector< double > errBound
per time