LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mtmstm_dets.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_MTMSTM_DETS_H
6#define LINE_API_QSYS_QSYS_MTMSTM_DETS_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * DETS, the delay estimator under time-varying staffing of Konrad and Liu
12 * (WSC 2023): waiting-time distribution of a customer who arrives at time 0 to
13 * the FCFS Mt/M/st+M queue and finds all servers busy with nq customers waiting
14 * ahead.
15 *
16 * Port of matlab/src/api/qsys/qsys_mtmstm_dets.m, cross-checked against
17 * python/line_solver/api/qsys/dets.py.
18 *
19 * METHOD. The tagged customer's position in the queue is a transient CTMC.
20 * Between two staffing changes it moves down at rate s*mu + q*theta, and it is
21 * absorbed when the customer enters service. At a change it jumps by a matrix
22 * L(k), so
23 * P(W > tau) = beta0 prod_k exp(Q(k-1)(t_k - t_(k-1))) L(k) exp(Q(K)(tau - t_K)) e
24 * (Theorem 1). Customers behind the tagged one never overtake it, so the result
25 * holds for any arrival process (Gt) as well.
26 *
27 * POLICY says what happens to a busy server whose shift ends:
28 * "pe" preemptive: its customer goes back to the head of the queue
29 * "ec" exhaustive completion: it finishes its customer first and serves
30 * nobody else
31 * "eh" exhaustive handoff: it keeps its customer until a staying server
32 * frees up and takes the customer over, which needs a second state
33 * variable, the number r of servers still waiting to leave
34 * A server joining under "eh" first takes over a leaving server's customer.
35 * The three policies agree when the staffing only increases; "pe" and "eh"
36 * agree when mu = theta. The [added, removed] form of the changes differs from
37 * the net form only under "ec" (Remark 2).
38 *
39 * RIGHT-CONTINUITY. The change at tk[k] is applied at tau = tk[k] itself, so Fc
40 * is the right-continuous P(W > tau): a server added at tk[k] can admit the
41 * tagged customer exactly then, giving the law an atom there. The paper's
42 * Theorem 1 applies only the changes strictly before tau, which gives the left
43 * limit P(W >= tau) at the change points and the same values everywhere else.
44 *
45 * WAIT. "pwt" is the potential waiting time (an infinitely patient customer),
46 * "awt" the actual waiting time min(W, patience) (Remark 1): the AWT chain is
47 * the PWT chain killed at rate theta. The means integrate the CCDF piece by
48 * piece exactly, a finite piece through the block exponential [Q e; 0 0] and the
49 * last through (-Q)^{-1} e; meanPWT is Inf when the final staffing is 0 and
50 * theta = 0 (a customer at the head of the queue then waits forever).
51 *
52 * INDEXING. The vectors are 0-based C++ vectors; the internal state (q, r) has
53 * index r*(qbar+1) + q, as the MATLAB index minus one. No random numbers.
54 *
55 * Reference: K. Konrad, Y. Liu, "Real-time estimations for the waiting-time
56 * distribution in time-varying queues", Proc. Winter Simulation Conference
57 * 2023, 315-326.
58 */
59
60#include <algorithm>
61#include <array>
62#include <cctype>
63#include <cmath>
64#include <cstddef>
65#include <initializer_list>
66#include <limits>
67#include <string>
68#include <vector>
69
70#include "line/util/error.h"
71#include "line/util/expm.h"
72#include "line/util/linalg.h"
73#include "line/util/lu.h"
74#include "line/util/matrix.h"
75
76namespace line {
77namespace qsys {
78
79/** DETS result. */
80struct DetsResult {
81 std::vector<double> Fc; ///< P(W > tau) at each tau
82 std::vector<double> cdf; ///< 1 - Fc
83 double meanPWT = 0; ///< E[W]; Inf when the final staffing is 0 and theta = 0
84 double meanAWT = 0; ///< E[min(W, A)]
85 double poa = 0; ///< probability of abandonment, theta * meanAWT
86 std::vector<int> s; ///< staffing level after each change
87};
88
89namespace detail_dets {
90
91[[noreturn]] inline void input(const char* kind, const std::string& msg) {
92 throw InputError(ErrorKind{kind}, std::string("qsys_mtmstm_dets: ") + msg);
93}
94
95/**
96 * Transient generator over (q, r), q = 0..qbar, r = 0..rbar, index
97 * r*(qbar+1) + q. Level r = 0 moves down in q at rate s*mu + q*theta and is
98 * absorbed from q = 0. A level r > 0 (servers due to leave under EH) drops to
99 * r - 1 at rate (s + r)*mu and moves down in q by abandonment only. Qa is the
100 * same chain killed at rate theta.
101 */
102inline void dets_generator(int qbar, int rbar, int s, double mu, double theta, Matrix<double>& Qp, Matrix<double>& Qa) {
103 const std::size_t nq1 = static_cast<std::size_t>(qbar) + 1;
104 const std::size_t n = nq1 * (static_cast<std::size_t>(rbar) + 1);
105 Qp = Matrix<double>(n, n, 0.0);
106 for (int r = 0; r <= rbar; ++r) {
107 for (int q = 0; q <= qbar; ++q) {
108 const std::size_t i = static_cast<std::size_t>(r) * nq1 + static_cast<std::size_t>(q);
109 double out;
110 if (r == 0) {
111 out = s * mu + q * theta;
112 if (q > 0) Qp(i, i - 1) = out;
113 } else {
114 out = (s + r) * mu + q * theta;
115 Qp(i, i - nq1) = (s + r) * mu;
116 if (q > 0) Qp(i, i - 1) = q * theta;
117 }
118 Qp(i, i) = -out;
119 }
120 }
121 Qa = Qp;
122 for (std::size_t i = 0; i < n; ++i) Qa(i, i) = Qp(i, i) - theta;
123}
124
125/** The staffing change at t_k applied to the row vector v over (q, r); qn < 0 means every state was absorbed. */
126inline std::vector<double> dets_jump(const std::vector<double>& v, int qbar, int rbar, int add, int rem,
127 const std::string& policy, int& qn, int& rn) {
128 const int net = add - rem;
129 const int nq1 = qbar + 1;
130 if (policy == "pe") {
131 qn = qbar - net;
132 rn = 0;
133 } else if (policy == "ec") {
134 qn = qbar - add;
135 rn = 0;
136 } else if (net >= 0) { // eh
137 qn = qbar - std::max(net - rbar, 0);
138 rn = std::max(rbar - net, 0);
139 } else {
140 qn = qbar;
141 rn = rbar - net;
142 }
143 if (qn < 0) return std::vector<double>();
144 std::vector<double> vnew(static_cast<std::size_t>(qn + 1) * static_cast<std::size_t>(rn + 1), 0.0);
145 for (int r = 0; r <= rbar; ++r) {
146 for (int q = 0; q <= qbar; ++q) {
147 const double w = v[static_cast<std::size_t>(r * nq1 + q)];
148 if (w == 0.0) continue;
149 int q2, r2;
150 if (policy == "pe") {
151 q2 = q - net;
152 r2 = 0;
153 } else if (policy == "ec") {
154 q2 = q - add;
155 r2 = 0;
156 } else if (net >= 0) {
157 q2 = q - std::max(net - r, 0);
158 r2 = std::max(r - net, 0);
159 } else {
160 q2 = q;
161 r2 = r - net;
162 }
163 if (q2 >= 0) { // q2 < 0: the customer entered service
164 const std::size_t j = static_cast<std::size_t>(r2 * (qn + 1) + q2);
165 vnew[j] = vnew[j] + w;
166 }
167 }
168 }
169 return vnew;
170}
171
172inline double vsum(const std::vector<double>& x) {
173 double s = 0.0;
174 for (double v : x) s += v;
175 return s;
176}
177
178/**
179 * Integral over [0, inf) of v_k exp(Q_k (x - t_k)) e exp(-theta t_k) on each
180 * piece: a finite piece by the block exponential [Q e; 0 0], the last by
181 * (-Q)^{-1} e, Inf when the last chain has no server and no killing.
182 */
183inline double dets_mean(const std::vector<std::vector<double>>& v, const std::vector<Matrix<double>>& Qs,
184 const std::vector<double>& tb, const std::vector<char>& alive, double theta, int sK,
185 double kill) {
186 const std::size_t K = tb.size() - 1;
187 double m = 0.0;
188 for (std::size_t k = 0; k <= K; ++k) {
189 if (!alive[k]) break;
190 const Matrix<double>& Q = Qs[k];
191 const std::size_t n = Q.rows();
192 const double f = std::exp(-theta * tb[k]);
193 std::vector<double> w(v[k].size());
194 bool any = false;
195 for (std::size_t i = 0; i < w.size(); ++i) {
196 w[i] = f * v[k][i];
197 if (w[i] != 0.0) any = true;
198 }
199 if (!any) continue;
200 if (k < K) {
201 const double d = tb[k + 1] - tb[k];
202 Matrix<double> B(n + 1, n + 1, 0.0);
203 for (std::size_t i = 0; i < n; ++i) {
204 for (std::size_t j = 0; j < n; ++j) B(i, j) = Q(i, j) * d;
205 B(i, n) = 1.0 * d;
206 }
207 const Matrix<double> E = line::expm(B);
208 double acc = 0.0;
209 for (std::size_t i = 0; i < n; ++i) acc += w[i] * E(i, n);
210 m = m + acc;
211 } else if (sK == 0 && kill == 0.0) {
212 m = std::numeric_limits<double>::infinity();
213 } else {
214 Matrix<double> negQ(n, n, 0.0);
215 for (std::size_t i = 0; i < n; ++i)
216 for (std::size_t j = 0; j < n; ++j) negQ(i, j) = -Q(i, j);
217 const std::vector<double> x = line::solve(negQ, std::vector<double>(n, 1.0));
218 double acc = 0.0;
219 for (std::size_t i = 0; i < n; ++i) acc += w[i] * x[i];
220 m = m + acc;
221 }
222 }
223 return m;
224}
225
226inline std::string lower(const std::string& s) {
227 std::string o = s;
228 for (char& c : o) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
229 return o;
230}
231
232/** The common body once the changes are split into added and removed servers. */
233inline DetsResult dets_core(double mu, double theta, int s0, const std::vector<double>& tk,
234 const std::vector<int>& add, const std::vector<int>& rem, int nq,
235 const std::vector<double>& tau, const std::string& pol, const std::string& wt) {
236 const std::size_t K = tk.size();
237 std::vector<int> s(K);
238 long long cur = s0;
239 for (std::size_t k = 0; k < K; ++k)
240 if (add[k] < 0 || rem[k] < 0) input("Argument", "The staffing changes must be integers.");
241 for (std::size_t k = 0; k < K; ++k) {
242 cur += static_cast<long long>(add[k]) - rem[k];
243 if (cur < 0) input("Argument", "The staffing level becomes negative.");
244 s[k] = static_cast<int>(cur);
245 }
246 bool tok = !tau.empty();
247 for (double v : tau) tok = tok && std::isfinite(v) && v >= 0.0;
248 if (!tok) input("Argument", "TAU must be a non-empty vector of finite nonnegative times.");
249
250 // piecewise chains: piece k (0..K) runs on [t_k, t_(k+1)) with sk[k] servers
251 std::vector<int> sk(K + 1);
252 std::vector<double> tb(K + 1);
253 sk[0] = s0;
254 tb[0] = 0.0;
255 for (std::size_t k = 0; k < K; ++k) {
256 sk[k + 1] = s[k];
257 tb[k + 1] = tk[k];
258 }
259 std::vector<int> qbar(K + 1, 0), rbar(K + 1, 0);
260 qbar[0] = nq;
261 std::vector<std::vector<double>> v(K + 1);
262 v[0].assign(static_cast<std::size_t>(nq) + 1, 0.0);
263 v[0].back() = 1.0; // beta0: state (q = nq, r = 0)
264 std::vector<Matrix<double>> Qp(K + 1), Qa(K + 1);
265 dets_generator(qbar[0], rbar[0], sk[0], mu, theta, Qp[0], Qa[0]);
266 std::vector<char> alive(K + 1, 1); // 0 once every state is absorbed
267 for (std::size_t k = 0; k < K; ++k) {
268 if (!alive[k]) {
269 alive[k + 1] = 0;
270 continue;
271 }
272 // state at t_(k+1)^- under the PWT chain (the AWT chain is the PWT one
273 // killed at rate theta, so it carries the factor exp(-theta t) and the same L)
274 const std::vector<double> vend = line::vecmul(v[k], line::expm(Qp[k], tb[k + 1] - tb[k]));
275 int qn = 0, rn = 0;
276 v[k + 1] = dets_jump(vend, qbar[k], rbar[k], add[k], rem[k], pol, qn, rn);
277 qbar[k + 1] = qn;
278 rbar[k + 1] = rn;
279 if (qn < 0) {
280 alive[k + 1] = 0;
281 } else {
282 dets_generator(qn, rn, sk[k + 1], mu, theta, Qp[k + 1], Qa[k + 1]);
283 }
284 }
285
286 // CCDF at the targets; changes at t_k <= tau are applied
287 DetsResult res;
288 res.Fc.assign(tau.size(), 0.0);
289 for (std::size_t i = 0; i < tau.size(); ++i) {
290 std::size_t k = 0;
291 for (std::size_t j = 0; j <= K; ++j)
292 if (tb[j] <= tau[i]) k = j;
293 if (!alive[k]) {
294 res.Fc[i] = 0.0;
295 continue;
296 }
297 if (wt == "pwt") {
298 res.Fc[i] = vsum(line::vecmul(v[k], line::expm(Qp[k], tau[i] - tb[k])));
299 } else {
300 res.Fc[i] = std::exp(-theta * tb[k]) * vsum(line::vecmul(v[k], line::expm(Qa[k], tau[i] - tb[k])));
301 }
302 }
303
304 res.meanPWT = dets_mean(v, Qp, tb, alive, 0.0, sk[K], 0.0);
305 res.meanAWT = dets_mean(v, Qa, tb, alive, theta, sk[K], theta);
306 res.cdf.resize(res.Fc.size());
307 for (std::size_t i = 0; i < res.Fc.size(); ++i) res.cdf[i] = 1.0 - res.Fc[i];
308 res.poa = theta * res.meanAWT;
309 res.s = s;
310 return res;
311}
312
313/** The checks MATLAB runs before splitting DSK, in its order; returns {policy, wait} lower-cased. */
314inline std::array<std::string, 2> check_common(double mu, double theta, int s0, const std::vector<double>& tk,
315 int nq, const std::string& policy, const std::string& wait) {
316 const std::string wt = lower(wait);
317 if (wt != "pwt" && wt != "awt") input("Option", "options.wait must be 'pwt' or 'awt'.");
318 const std::string pol = lower(policy);
319 if (pol != "pe" && pol != "ec" && pol != "eh") input("Argument", "POLICY must be 'pe', 'ec' or 'eh'.");
320 if (!std::isfinite(mu) || !(mu > 0.0)) input("Argument", "MU must be a positive finite rate.");
321 if (!std::isfinite(theta) || theta < 0.0) input("Argument", "THETA must be a nonnegative finite rate.");
322 if (s0 < 0) input("Argument", "S0 must be a nonnegative integer.");
323 if (nq < 0) input("Argument", "NQ must be a nonnegative integer.");
324 for (std::size_t k = 0; k < tk.size(); ++k) {
325 if (!std::isfinite(tk[k]) || !(tk[k] > 0.0) || (k > 0 && !(tk[k] - tk[k - 1] > 0.0)))
326 input("Argument", "TK must be strictly increasing, finite and positive.");
327 }
328 return {{pol, wt}};
329}
330
331} // namespace detail_dets
332
333/**
334 * DETS waiting-time CCDF with the staffing changes given as net signed changes.
335 *
336 * @param mu service rate (> 0)
337 * @param theta abandonment rate of a waiting customer (>= 0)
338 * @param s0 servers at time 0 (>= 0), all busy
339 * @param tk staffing change times, strictly increasing and > 0, measured
340 * from the tagged customer's arrival (empty: constant staffing,
341 * the myopic predictor of the paper)
342 * @param dsk net change at each tk (one entry per change time)
343 * @param nq customers waiting ahead of the tagged customer (>= 0)
344 * @param tau waiting-time targets (finite, >= 0)
345 * @param policy "pe", "ec" or "eh"
346 * @param wait "pwt" (default) or "awt"
347 * @throws InputError LINE:input:Argument on a bad argument, LINE:input:Option on a bad wait
348 */
349inline DetsResult qsys_mtmstm_dets(double mu, double theta, int s0, const std::vector<double>& tk,
350 const std::vector<int>& dsk, int nq, const std::vector<double>& tau,
351 const std::string& policy, const std::string& wait = "pwt") {
352 const std::array<std::string, 2> pw = detail_dets::check_common(mu, theta, s0, tk, nq, policy, wait);
353 const std::size_t K = tk.size();
354 if (K == 0 && !dsk.empty()) detail_dets::input("Argument", "DSK must be empty when TK is.");
355 if (dsk.size() != K)
356 detail_dets::input("Argument",
357 "DSK must have one entry per change time, or be a K-by-2 [added, removed] matrix.");
358 std::vector<int> add(K), rem(K);
359 for (std::size_t k = 0; k < K; ++k) {
360 add[k] = std::max(dsk[k], 0);
361 rem[k] = std::max(-dsk[k], 0);
362 }
363 return detail_dets::dets_core(mu, theta, s0, tk, add, rem, nq, tau, pw[0], pw[1]);
364}
365
366/**
367 * DETS with the staffing changes given as [added, removed] pairs, for servers
368 * joining and leaving at the same time (differs from the net form only under
369 * "ec", Remark 2). Otherwise as the net-change overload.
370 */
371inline DetsResult qsys_mtmstm_dets(double mu, double theta, int s0, const std::vector<double>& tk,
372 const std::vector<std::array<int, 2>>& dsk, int nq,
373 const std::vector<double>& tau, const std::string& policy,
374 const std::string& wait = "pwt") {
375 const std::array<std::string, 2> pw = detail_dets::check_common(mu, theta, s0, tk, nq, policy, wait);
376 const std::size_t K = tk.size();
377 if (K == 0 && !dsk.empty()) detail_dets::input("Argument", "DSK must be empty when TK is.");
378 if (dsk.size() != K)
379 detail_dets::input("Argument",
380 "DSK must have one entry per change time, or be a K-by-2 [added, removed] matrix.");
381 std::vector<int> add(K), rem(K);
382 for (std::size_t k = 0; k < K; ++k) {
383 add[k] = dsk[k][0];
384 rem[k] = dsk[k][1];
385 }
386 return detail_dets::dets_core(mu, theta, s0, tk, add, rem, nq, tau, pw[0], pw[1]);
387}
388
389/**
390 * Brace-list spelling of the net-change form, so that qsys_mtmstm_dets(...,
391 * {}, {}, ...) and qsys_mtmstm_dets(..., {-1, -1}, ...) pick the net form
392 * rather than being ambiguous with the pair overload.
393 */
394inline DetsResult qsys_mtmstm_dets(double mu, double theta, int s0, const std::vector<double>& tk,
395 std::initializer_list<int> dsk, int nq, const std::vector<double>& tau,
396 const std::string& policy, const std::string& wait = "pwt") {
397 return qsys_mtmstm_dets(mu, theta, s0, tk, std::vector<int>(dsk), nq, tau, policy, wait);
398}
399
400} // namespace qsys
401} // namespace line
402
403#endif // LINE_API_QSYS_QSYS_MTMSTM_DETS_H
InputError(const std::string &what)
Definition error.h:105
std::size_t rows() const
Definition matrix.h:89
The exception types the port throws.
Matrix exponential by scaling and squaring with a diagonal Pade approximant.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
void dets_generator(int qbar, int rbar, int s, double mu, double theta, Matrix< double > &Qp, Matrix< double > &Qa)
Transient generator over (q, r), q = 0..qbar, r = 0..rbar, index r*(qbar+1) + q.
std::array< std::string, 2 > check_common(double mu, double theta, int s0, const std::vector< double > &tk, int nq, const std::string &policy, const std::string &wait)
The checks MATLAB runs before splitting DSK, in its order; returns {policy, wait} lower-cased.
DetsResult dets_core(double mu, double theta, int s0, const std::vector< double > &tk, const std::vector< int > &add, const std::vector< int > &rem, int nq, const std::vector< double > &tau, const std::string &pol, const std::string &wt)
The common body once the changes are split into added and removed servers.
std::string lower(const std::string &s)
void input(const char *kind, const std::string &msg)
double vsum(const std::vector< double > &x)
std::vector< double > dets_jump(const std::vector< double > &v, int qbar, int rbar, int add, int rem, const std::string &policy, int &qn, int &rn)
The staffing change at t_k applied to the row vector v over (q, r); qn < 0 means every state was abso...
double dets_mean(const std::vector< std::vector< double > > &v, const std::vector< Matrix< double > > &Qs, const std::vector< double > &tb, const std::vector< char > &alive, double theta, int sK, double kill)
Integral over [0, inf) of v_k exp(Q_k (x - t_k)) e exp(-theta t_k) on each piece: a finite piece by t...
DetsResult qsys_mtmstm_dets(double mu, double theta, int s0, const std::vector< double > &tk, const std::vector< int > &dsk, int nq, const std::vector< double > &tau, const std::string &policy, const std::string &wait="pwt")
DETS waiting-time CCDF with the staffing changes given as net signed changes.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
std::vector< T > vecmul(const std::vector< T > &v, const Matrix< T > &A)
Row vector times matrix, v A.
Definition linalg.h:50
std::vector< T > solve(const Matrix< T > &A, const std::vector< T > &b)
Convenience: solve Ax = b, leaving A and b untouched.
Definition lu.h:158
Matrix< T > expm(const Matrix< T > &A)
Matrix exponential exp(A).
Definition expm.h:141
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
std::vector< double > Fc
P(W > tau) at each tau.
double poa
probability of abandonment, theta * meanAWT
std::vector< double > cdf
1 - Fc
std::vector< int > s
staffing level after each change
double meanAWT
E[min(W, A)].
double meanPWT
E[W]; Inf when the final staffing is 0 and theta = 0.