LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
fluid_aoi.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_SOLVERS_FLUID_FLUID_AOI_H
6#define LINE_SOLVERS_FLUID_FLUID_AOI_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * Age of Information by Markovian fluid queues: a port of `solver_mfq_aoi.m`
12 * (identical to `solver_fluid_aoi.m`) with the gate `aoi_is_aoi.m`, the
13 * parameter map `aoi_extract_params.m`, and the two aoi-fluid algorithms
14 * `solveBufferless.m` and `solveSingleBuffer.m`.
15 *
16 * WHAT AGE OF INFORMATION IS. Not a delay of a job but the freshness of the
17 * information a monitor holds: at time t, the age is t minus the generation
18 * time of the most recent update DELIVERED so far. It grows at unit rate and
19 * drops on every delivery, so its mean depends on the whole delivery process
20 * and not only on the response time -- a queue that delivers late but often can
21 * beat one that delivers fast and rarely. Peak AoI (PAoI) is the value reached
22 * just before a drop.
23 *
24 * WHY A FLUID QUEUE COMPUTES IT. The age is a sawtooth: it climbs with slope +1
25 * and resets. That is exactly the sample path of a Markov-modulated fluid queue
26 * with drift +1 in every state but one, so the stationary age distribution is
27 * the stationary distribution of an MFQ level, and it comes out as a
28 * MATRIX-EXPONENTIAL triple (g, A, h): P(AoI > t) = g exp(A t) h. Both
29 * algorithms build the modulating chain of the age process, then solve one
30 * linear system for g and read the moments off A. The reference for the pair is
31 * the aoi-fluid toolbox of Dogan, Akar and Atay (BSD 2-Clause, 2020).
32 *
33 * THE TWO SYSTEMS COVERED, and no others: a BUFFERLESS PH/PH/1/1 where an
34 * arrival meeting a busy server is discarded (p = 0) or preempts it (p = 1),
35 * and a SINGLE-BUFFER M/PH/1/2 where a waiting update is kept (r = 0) or
36 * REPLACED by a fresher one (r = 1). Anything else -- more capacity, more
37 * servers, more classes, a second queue -- is refused by name.
38 *
39 * WHERE p AND r COME FROM. `aoi_extract_params.m` reads the scheduling policy:
40 * FCFS gives no preemption and no replacement, LCFS-PR preempts, and LCFS
41 * replaces in the buffered system while behaving non-preemptively in the
42 * bufferless one. `FluidOptions::aoi_preemption` overrides both, as
43 * `options.config.aoi_preemption` does in the reference.
44 *
45 * THE PH PAIR. `aoi_dist2ph` builds the PH from the (D0, D1) MAP with entry
46 * vector map_pie = theta D1 / (theta D1 e), the phase a renewal RESTARTS in.
47 * Until 2026-11-14 it used theta .* (D1 e), the phase a completion OCCURRED in,
48 * which read Erlang(2) of mean 1 as Exp(2) of mean 0.5; fixed in all four
49 * codebases.
50 *
51 * THE STANDARD METRICS ARE THOSE OF THE FINITE-CAPACITY SYSTEM ANALYZED, from
52 * the stationary vector of its explicit CTMC (`aoi_queue_metrics`): the
53 * bufferless PH/PH/1/1 with preemption p, or the single-buffer M/PH/1/2 with
54 * replacement r. Until 2026-11-14 they were the infinite-buffer M/M/1
55 * rho/(1 - rho), which gave QN = 1 at a capacity-1 station with UN = 0.5.
56 * TN is the DELIVERED throughput (completions) and RN = QN / TN, the
57 * convention `fluid_analyzer_correct` (`solver_fluid_analyzer.m`) imposes on
58 * every fluid branch after the method switch. Under FCFS every accepted update
59 * is delivered, so this is the sojourn time of an accepted update; under
60 * preemption or replacement it is the queue time per DELIVERED update, which
61 * exceeds the sojourn QN / accept of an accepted one (2026-11-14: a branch
62 * value of QN / accept was overwritten by the analyzer anyway). The source row
63 * of TN is the offered arrival rate.
64 */
65
66#include <algorithm>
67#include <cmath>
68#include <cstddef>
69#include <limits>
70#include <string>
71#include <vector>
72
76#include "line/util/eig.h"
77#include "line/util/error.h"
78#include "line/util/expm.h"
79#include "line/util/linalg.h"
80#include "line/util/lstsq.h"
81#include "line/util/lu.h"
82#include "line/util/matrix.h"
83
84namespace line {
85namespace fluid {
86
87/** What the AoI gate found, when it matches. */
89 bool ok = false;
90 std::string error;
91 std::size_t source = 0; ///< 0-based station index
92 std::size_t queue = 0;
93 std::size_t cls = 0; ///< the single open class
94 double capacity = 0.0; ///< 1 = bufferless, 2 = single buffer
96};
97
98/**
99 * Port of `aoi_is_aoi.m`.
100 *
101 * Returns the reason rather than throwing, because the reference uses this as
102 * a DISPATCH test inside the `mfq` method: a model that fails it is not an
103 * error, it is a model for the ordinary single-queue fluid branch.
104 */
105template <class T>
107 AoiTopology t;
108 const std::size_t M = sn.nstations, K = sn.nclasses;
109 std::vector<std::size_t> open_cls;
110 for (std::size_t r = 0; r < K; ++r)
111 if (!std::isfinite(sn.classes[r].population)) open_cls.push_back(r);
112 if (open_cls.empty()) {
113 t.error = "Not an open model - all classes are closed";
114 return t;
115 }
116 if (open_cls.size() > 1) {
117 t.error = "Multiple open classes found - AoI analysis requires single class";
118 return t;
119 }
120 t.cls = open_cls[0];
121
122 std::size_t nsrc = 0, nq = 0, nsink = 0;
123 for (const qn::NodeDef& nd : sn.nodes) {
124 if (nd.nodetype == qn::NodeType::Source) ++nsrc;
125 if (nd.nodetype == qn::NodeType::Sink) ++nsink;
126 if (nd.nodetype == qn::NodeType::Queue) ++nq;
127 }
128 if (nsrc != 1) {
129 t.error = nsrc == 0 ? "No source node found" : "Multiple source nodes found";
130 return t;
131 }
132 if (nsink != 1) {
133 t.error = nsink == 0 ? "No sink node found" : "Multiple sink nodes found";
134 return t;
135 }
136 if (nq != 1) {
137 t.error = nq == 0 ? "No queue node found"
138 : "Multiple queue nodes found - AoI analysis supports single queue only";
139 return t;
140 }
141 bool has_src = false, has_q = false;
142 for (std::size_t i = 0; i < M; ++i) {
143 if (sn.stations[i].nodetype == qn::NodeType::Source) {
144 t.source = i;
145 has_src = true;
146 } else if (sn.stations[i].nodetype == qn::NodeType::Queue) {
147 t.queue = i;
148 has_q = true;
149 } else if (sn.stations[i].nodetype != qn::NodeType::Sink) {
150 t.error = "AoI analysis supports Source -> Queue -> Sink only";
151 return t;
152 }
153 }
154 if (!has_src || !has_q) {
155 t.error = "The Source and the Queue must both be stations";
156 return t;
157 }
158
159 if (sn.stations[t.queue].nservers != 1.0) {
160 t.error = "Queue is not single-server - AoI analysis requires c = 1";
161 return t;
162 }
163 const double cap = (t.queue < sn.cap.size()) ? sn.cap[t.queue]
164 : std::numeric_limits<double>::infinity();
165 if (!std::isfinite(cap) || cap > 2.0 || cap < 1.0) {
166 t.error =
167 "AoI analysis requires queue capacity 1 (bufferless) or 2 (single-buffer)";
168 return t;
169 }
170 t.capacity = cap;
171
172 t.sched = sn.stations[t.queue].sched;
175 t.error = "AoI analysis supports FCFS, LCFS or LCFSPR scheduling only";
176 return t;
177 }
178
179 // A single buffer is analyzed under Poisson arrivals only.
180 if (cap == 2.0 && sn.service[t.source][t.cls].D0.rows() > 1) {
181 t.error = "Single-buffer (capacity 2) requires exponential arrivals";
182 return t;
183 }
184
185 const std::size_t rt_idx = t.queue * K + t.cls;
186 if (rt_idx < sn.rt.rows() && num_traits<T>::to_double(sn.rt(rt_idx, rt_idx)) > 0.0) {
187 t.error = "Self-loop detected at queue - violates AoI model assumptions";
188 return t;
189 }
190 t.ok = true;
191 return t;
192}
193
194/** The (tau, T) / (sigma, S) pairs and the preemption probability. */
195struct AoiParams {
197 std::vector<double> tau, sigma;
198 double p = 0.0; ///< preemption (bufferless) or replacement (single buffer)
199 double lambda = 0.0; ///< arrival rate, the single-buffer input
200};
201
202/**
203 * Port of `aoi_extract_params.m`.
204 *
205 * @param preempt_override `options.config.aoi_preemption`; negative selects the
206 * policy-driven default
207 * @param sn the refreshed network struct
208 * @param top the detected age-of-information topology
209 */
210template <class T>
212 double preempt_override) {
213 AoiParams par;
214 const lang::Distrib<T>& svc = sn.service[top.queue][top.cls];
215 const std::size_t ls = svc.D0.rows();
216 if (ls == 0) throw InputError("aoi_extract_params: the queue has no service process");
217 {
218 Matrix<double> D0(ls, ls, 0.0), D1(ls, ls, 0.0);
219 for (std::size_t a = 0; a < ls; ++a)
220 for (std::size_t b = 0; b < ls; ++b) {
221 D0(a, b) = num_traits<T>::to_double(svc.D0(a, b));
222 D1(a, b) = num_traits<T>::to_double(svc.D1(a, b));
223 }
224 const aoi::AoiPh<double> ph = aoi::aoi_dist2ph(D0, D1);
225 par.sigma = ph.alpha;
226 par.Ssvc = ph.Tmat;
227 }
228
229 par.lambda = num_traits<T>::to_double(sn.rates(top.source, top.cls));
230 if (top.capacity == 1.0) {
231 const lang::Distrib<T>& arr = sn.service[top.source][top.cls];
232 const std::size_t la = arr.D0.rows();
233 if (la == 0) throw InputError("aoi_extract_params: the source has no arrival process");
234 Matrix<double> D0(la, la, 0.0), D1(la, la, 0.0);
235 for (std::size_t a = 0; a < la; ++a)
236 for (std::size_t b = 0; b < la; ++b) {
237 D0(a, b) = num_traits<T>::to_double(arr.D0(a, b));
238 D1(a, b) = num_traits<T>::to_double(arr.D1(a, b));
239 }
240 const aoi::AoiPh<double> ph = aoi::aoi_dist2ph(D0, D1);
241 par.tau = ph.alpha;
242 par.Tarr = ph.Tmat;
243 }
244
245 if (preempt_override >= 0.0) {
246 par.p = preempt_override;
247 } else if (top.capacity == 1.0) {
248 // Bufferless: only LCFS-PR preempts the update in service.
249 par.p = (top.sched == lang::SchedStrategy::LCFSPR) ? 1.0 : 0.0;
250 } else {
251 // Single buffer: both LCFS variants replace the waiting update.
252 par.p = (top.sched == lang::SchedStrategy::FCFS) ? 0.0 : 1.0;
253 }
254 return par;
255}
256
257/** A matrix-exponential age law: P(age > t) = g exp(A t) h. */
258struct AoiMe {
259 std::vector<double> g;
261 std::vector<double> h;
262 double mean = std::numeric_limits<double>::quiet_NaN();
263 double var = std::numeric_limits<double>::quiet_NaN();
264};
265
266/** Both age laws of one system, with the policy parameter that produced them. */
269 std::string system_type; ///< "bufferless" or "singlebuffer"
270 double preemption = std::numeric_limits<double>::quiet_NaN();
271};
272
273namespace aoi_detail {
274
275/** Write B into A at (r0, c0). */
276inline void put(Matrix<double>& A, std::size_t r0, std::size_t c0, const Matrix<double>& B) {
277 for (std::size_t i = 0; i < B.rows(); ++i)
278 for (std::size_t j = 0; j < B.cols(); ++j) A(r0 + i, c0 + j) = B(i, j);
279}
280
281/** A row vector as a 1 x n matrix, and a column vector as n x 1. */
282inline Matrix<double> row(const std::vector<double>& v) {
283 Matrix<double> M(1, v.size(), 0.0);
284 for (std::size_t j = 0; j < v.size(); ++j) M(0, j) = v[j];
285 return M;
286}
287inline Matrix<double> col(const std::vector<double>& v) {
288 Matrix<double> M(v.size(), 1, 0.0);
289 for (std::size_t i = 0; i < v.size(); ++i) M(i, 0) = v[i];
290 return M;
291}
292
293/** MATLAB `g / A` for a row vector g: the x solving x A = g. */
294inline std::vector<double> rdivide(const std::vector<double>& g, const Matrix<double>& A) {
295 const std::size_t n = A.rows();
296 if (g.size() != n) throw InputError("aoi: row division size mismatch");
297 Matrix<double> At(n, n, 0.0);
298 for (std::size_t i = 0; i < n; ++i)
299 for (std::size_t j = 0; j < n; ++j) At(i, j) = A(j, i);
300 return solve(At, g);
301}
302
303/** g A^{-k} h, the moment form the reference writes as g/(A^k)*h. */
304inline double moment_form(const std::vector<double>& g, const Matrix<double>& A,
305 const std::vector<double>& h, unsigned k) {
306 std::vector<double> x = g;
307 for (unsigned i = 0; i < k; ++i) x = rdivide(x, A);
308 double s = 0.0;
309 for (std::size_t i = 0; i < x.size() && i < h.size(); ++i) s += x[i] * h[i];
310 return s;
311}
312
313/**
314 * Line 2 of Algorithm 1: the Householder reflector that maps the drift signs
315 * onto the first coordinate, used when the level-zero split is known a priori.
316 */
317inline Matrix<double> householder_p(std::size_t z) {
318 std::vector<double> u1(z, 1.0);
319 u1[z - 1] = -1.0;
320 double nrm = 0.0;
321 for (std::size_t i = 0; i < z; ++i) nrm += u1[i] * u1[i];
322 nrm = std::sqrt(nrm);
323 std::vector<double> u = u1;
324 u[0] -= nrm;
325 double uu = 0.0;
326 for (std::size_t i = 0; i < z; ++i) uu += u[i] * u[i];
327 Matrix<double> P = line::eye<double>(z);
328 if (uu > 0.0)
329 for (std::size_t i = 0; i < z; ++i)
330 for (std::size_t j = 0; j < z; ++j) P(i, j) -= 2.0 * u[i] * u[j] / uu;
331 return P;
332}
333
334/**
335 * Lines 3 and 4 of Algorithm 1, shared by both systems: split QR by P, solve
336 * the boundary system for (g, d), and return g.
337 *
338 * @param QR Q R^{-1}
339 * @param P the orthogonal splitter
340 * @param Rd the drift matrix R
341 * @param Qtil the modified generator Qtilde
342 * @param a the number of negative-drift states
343 */
344struct AlgOneSplit {
345 Matrix<double> A, H;
346 std::vector<double> g, d;
347};
348inline AlgOneSplit alg_one(const Matrix<double>& QR, const Matrix<double>& P,
349 const Matrix<double>& Rd, const Matrix<double>& Qtil, std::size_t a) {
350 const std::size_t z = QR.rows();
351 const std::size_t b = z - a;
352 AlgOneSplit s;
353 // Ae = P' QR P, then A is its trailing (z-a) block and H the trailing
354 // columns of P, transposed.
355 Matrix<double> Pt(z, z, 0.0);
356 for (std::size_t i = 0; i < z; ++i)
357 for (std::size_t j = 0; j < z; ++j) Pt(i, j) = P(j, i);
358 const Matrix<double> Ae = matmul(matmul(Pt, QR), P);
359 s.A = Matrix<double>(b, b, 0.0);
360 for (std::size_t i = 0; i < b; ++i)
361 for (std::size_t j = 0; j < b; ++j) s.A(i, j) = Ae(a + i, a + j);
362 s.H = Matrix<double>(b, z, 0.0);
363 for (std::size_t i = 0; i < b; ++i)
364 for (std::size_t j = 0; j < z; ++j) s.H(i, j) = P(j, a + i);
365
366 // EqnMatrix = [H R, -A^{-1} H e; -Qtilde(b+1:z, :), e], solved transposed.
367 const Matrix<double> HR = matmul(s.H, Rd);
368 std::vector<double> He(b, 0.0);
369 for (std::size_t i = 0; i < b; ++i)
370 for (std::size_t j = 0; j < z; ++j) He[i] += s.H(i, j);
371 const Matrix<double> Ainv = inverse(s.A);
372 std::vector<double> AinvHe(b, 0.0);
373 for (std::size_t i = 0; i < b; ++i)
374 for (std::size_t j = 0; j < b; ++j) AinvHe[i] += Ainv(i, j) * He[j];
375
376 Matrix<double> Eq(z, z + 1, 0.0);
377 for (std::size_t i = 0; i < b; ++i) {
378 for (std::size_t j = 0; j < z; ++j) Eq(i, j) = HR(i, j);
379 Eq(i, z) = -AinvHe[i];
380 }
381 for (std::size_t i = 0; i < a; ++i) {
382 for (std::size_t j = 0; j < z; ++j) Eq(b + i, j) = -Qtil(b + i, j);
383 Eq(b + i, z) = 1.0;
384 }
385 // The system is EqnMatrix' x = [0 ... 0 1]', overdetermined by one row.
386 Matrix<double> EqT(z + 1, z, 0.0);
387 for (std::size_t i = 0; i < z; ++i)
388 for (std::size_t j = 0; j < z + 1; ++j) EqT(j, i) = Eq(i, j);
389 std::vector<double> rhs(z + 1, 0.0);
390 rhs[z] = 1.0;
391 const LstsqResult<double> sol = lstsq(EqT, rhs);
392 s.g.assign(sol.x.begin(), sol.x.begin() + static_cast<std::ptrdiff_t>(b));
393 s.d.assign(sol.x.begin() + static_cast<std::ptrdiff_t>(b), sol.x.end());
394 return s;
395}
396
397/** Normalize g by -g A^{-1} h and read the first two moments. */
398inline AoiMe finish_me(const std::vector<double>& g, const Matrix<double>& A,
399 const std::vector<double>& h) {
400 AoiMe me;
401 me.A = A;
402 me.h = h;
403 const double nc = -moment_form(g, A, h, 1);
404 me.g.assign(g.size(), 0.0);
405 for (std::size_t i = 0; i < g.size(); ++i) me.g[i] = g[i] / nc;
406 me.mean = moment_form(me.g, A, h, 2);
407 me.var = -2.0 * moment_form(me.g, A, h, 3) - me.mean * me.mean;
408 return me;
409}
410
411} // namespace aoi_detail
412
413/**
414 * Port of `solveBufferless.m`: the AoI and PAoI laws of a PH/PH/1/1 system in
415 * which an arrival meeting a busy server preempts it with probability p.
416 */
417inline AoiSolution aoi_solve_bufferless(const std::vector<double>& tau, const Matrix<double>& Tm,
418 const std::vector<double>& sigma, const Matrix<double>& Sm,
419 double p) {
420 using namespace aoi_detail;
421 const std::size_t k = Tm.cols(), l = Sm.cols();
422 if (k == 0 || l == 0) throw InputError("aoi_solve_bufferless: empty representation");
423 if (tau.size() != k || sigma.size() != l)
424 throw InputError("aoi_solve_bufferless: the initial vectors do not match their generators");
425 const std::size_t z = 2 * k * l + k + 1, a = 1, b = z - 1;
426
427 std::vector<double> kappa(k, 0.0), nu(l, 0.0);
428 for (std::size_t i = 0; i < k; ++i)
429 for (std::size_t j = 0; j < k; ++j) kappa[i] -= Tm(i, j);
430 for (std::size_t i = 0; i < l; ++i)
431 for (std::size_t j = 0; j < l; ++j) nu[i] -= Sm(i, j);
432 const Matrix<double> kap = col(kappa), nuc = col(nu);
433 const Matrix<double> taur = row(tau), sigr = row(sigma);
435 const Matrix<double> ones_l(l, 1, 1.0), ones_k(k, 1, 1.0);
436
437 // Q11 and Q33 of Eqn (13): service and arrival running together, with the
438 // non-preempting and the preempting restart respectively.
439 Matrix<double> Q11 = mam::kron(Ik, Sm);
440 {
441 const Matrix<double> t2 = mam::kron(Tm, Il);
442 const Matrix<double> t3 = mam::kron(mam::kron(kap, taur), Il);
443 for (std::size_t i = 0; i < k * l; ++i)
444 for (std::size_t j = 0; j < k * l; ++j) Q11(i, j) += t2(i, j) + (1.0 - p) * t3(i, j);
445 }
446 Matrix<double> Q33 = Q11;
447 {
448 const Matrix<double> t4 = mam::kron(kap, mam::kron(ones_l, mam::kron(taur, sigr)));
449 for (std::size_t i = 0; i < k * l; ++i)
450 for (std::size_t j = 0; j < k * l; ++j) Q33(i, j) += p * t4(i, j);
451 }
452
453 Matrix<double> Q(z, z, 0.0);
454 put(Q, 0, 0, Q11);
455 put(Q, 0, k * l, mam::kron(Ik, nuc));
456 {
457 Matrix<double> lastcol = mam::kron(kap, ones_l);
458 for (std::size_t i = 0; i < k * l; ++i) Q(i, z - 1) = p * lastcol(i, 0);
459 }
460 put(Q, k * l, k * l, Tm);
461 put(Q, k * l, k * l + k, mam::kron(kap, mam::kron(taur, sigr)));
462 put(Q, k * l + k, k * l + k, Q33);
463 {
464 const Matrix<double> lastcol = mam::kron(ones_k, nuc);
465 for (std::size_t i = 0; i < k * l; ++i) Q(k * l + k + i, z - 1) = lastcol(i, 0);
466 }
467
469 Rd(z - 1, z - 1) = -1.0;
470 Matrix<double> Qtil = Q;
471 {
472 const Matrix<double> ts = mam::kron(taur, sigr);
473 for (std::size_t j = 0; j < k * l; ++j) Qtil(z - 1, j) = ts(0, j);
474 Qtil(z - 1, z - 1) = -1.0;
475 }
476 // Q / R with R diagonal +-1 is Q with its last column negated.
477 Matrix<double> QR = Q;
478 for (std::size_t i = 0; i < z; ++i) QR(i, z - 1) = -QR(i, z - 1);
479
480 const AlgOneSplit s = alg_one(QR, householder_p(z), Rd, Qtil, a);
481
482 AoiSolution out;
483 out.system_type = "bufferless";
484 out.preemption = p;
485 // AoI lives in phases 2 and 3; PAoI in phase 3, weighted by the exit rates.
486 std::vector<double> sel(z, 0.0);
487 for (std::size_t i = k * l; i < k * l + k * l + k; ++i) sel[i] = 1.0;
488 std::vector<double> h(b, 0.0);
489 for (std::size_t i = 0; i < b; ++i)
490 for (std::size_t j = 0; j < z; ++j) h[i] += s.H(i, j) * sel[j];
491 out.aoi = finish_me(s.g, s.A, h);
492
493 std::vector<double> selp(z, 0.0);
494 {
495 const Matrix<double> kn = mam::kron(ones_k, nuc);
496 for (std::size_t i = 0; i < k * l; ++i) selp[k * l + k + i] = kn(i, 0);
497 }
498 std::vector<double> hp(b, 0.0);
499 for (std::size_t i = 0; i < b; ++i)
500 for (std::size_t j = 0; j < z; ++j) hp[i] += s.H(i, j) * selp[j];
501 out.paoi = finish_me(s.g, s.A, hp);
502 return out;
503}
504
505/**
506 * Port of `solveSingleBuffer.m`: the AoI and PAoI laws of an M/PH/1/2 system in
507 * which a waiting update is replaced by a fresher arrival with probability r.
508 *
509 * Two fluid queues are solved in sequence. The FIRST gives the waiting-time law
510 * of an update that finds the server busy, and needs the SCHUR splitter rather
511 * than the Householder one: its drift has two negative states and their
512 * invariant subspace is not known a priori. Lemma 1 of the reference then
513 * turns that law into a PH pair (beta, B), which drives the SECOND queue --
514 * the age process proper -- where the split is again explicit.
515 */
516inline AoiSolution aoi_solve_singlebuffer(double lambda, const std::vector<double>& sigma,
517 const Matrix<double>& Sm, double r) {
518 using namespace aoi_detail;
519 const std::size_t l = Sm.cols();
520 if (l == 0) throw InputError("aoi_solve_singlebuffer: empty representation");
521 if (sigma.size() != l)
522 throw InputError("aoi_solve_singlebuffer: sigma does not match its generator");
523 if (!(lambda > 0.0)) throw InputError("aoi_solve_singlebuffer: the arrival rate must be positive");
524
525 std::vector<double> nu(l, 0.0);
526 for (std::size_t i = 0; i < l; ++i)
527 for (std::size_t j = 0; j < l; ++j) nu[i] -= Sm(i, j);
528 const Matrix<double> nuc = col(nu), sigr = row(sigma);
529
530 // ---- the waiting-time fluid queue of Eqn (16) --------------------------
531 std::size_t z = l + 2;
532 const std::size_t a1 = 2, b1 = l;
533 Matrix<double> Q(z, z, 0.0);
534 put(Q, 0, 0, Sm);
535 for (std::size_t i = 0; i < l; ++i) Q(i, l) = nu[i];
536 Q(l, l) = -lambda;
537 Q(l, l + 1) = lambda;
538
540 Rd(z - 2, z - 2) = -1.0;
541 Rd(z - 1, z - 1) = -1.0;
542
543 Matrix<double> Qtil(z, z, 0.0);
544 for (std::size_t j = 0; j < l; ++j) {
545 Qtil(l, j) = lambda * sigma[j];
546 Qtil(l + 1, j) = sigma[j];
547 }
548 Qtil(l, l) = -lambda;
549 Qtil(l + 1, l + 1) = -1.0;
550
551 Matrix<double> QR = Q;
552 for (std::size_t i = 0; i < z; ++i) {
553 QR(i, z - 2) = -QR(i, z - 2);
554 QR(i, z - 1) = -QR(i, z - 1);
555 }
556
557 // pik solves pik (Q + e e') = e', the reference's rank-one regularization.
558 std::vector<double> pik;
559 {
560 Matrix<double> Aug(z, z, 0.0);
561 for (std::size_t i = 0; i < z; ++i)
562 for (std::size_t j = 0; j < z; ++j) Aug(i, j) = Q(i, j) + 1.0;
563 pik = rdivide(std::vector<double>(z, 1.0), Aug);
564 }
565 // A1 shifts the singular generator so its stable subspace is separated.
566 Matrix<double> A1 = QR;
567 {
568 std::vector<double> xR(z, 0.0);
569 for (std::size_t i = 0; i < z; ++i)
570 for (std::size_t j = 0; j < z; ++j) xR[i] += Rd(i, j);
571 double den = 0.0;
572 for (std::size_t i = 0; i < z; ++i) den += pik[i] * xR[i];
573 for (std::size_t i = 0; i < z; ++i)
574 for (std::size_t j = 0; j < z; ++j) A1(i, j) += xR[i] * pik[j] / den;
575 }
576 // ordschur(..., 'rhp'): the right-half-plane eigenvalues come first.
578 {
579 const RealSchur sc = schur_decomposition(A1);
580 std::vector<double> key(z, 0.0);
581 for (std::size_t i = 0; i < z; ++i) key[i] = (sc.T(i, i) >= 0.0) ? 1.0 : 0.0;
582 P = schur_reorder(sc, key).Z;
583 }
584
585 const AlgOneSplit s1 = alg_one(QR, P, Rd, Qtil, a1);
586 const double c_0 = s1.d.empty() ? 0.0 : s1.d[0];
587
588 // Lemma 1: the waiting law as a PH pair (beta, B).
589 Matrix<double> wait_A = s1.A;
590 for (std::size_t i = 0; i < b1; ++i) wait_A(i, i) -= r * lambda;
591 std::vector<double> selw(z, 0.0);
592 selw[l] = 1.0;
593 selw[l + 1] = r;
594 std::vector<double> wait_H(b1, 0.0);
595 for (std::size_t i = 0; i < b1; ++i)
596 for (std::size_t j = 0; j < z; ++j) wait_H[i] += s1.H(i, j) * selw[j];
597 std::vector<double> wait_g = s1.g;
598 {
599 const double n1 = 1.0 / (-moment_form(wait_g, wait_A, wait_H, 1) + c_0);
600 for (std::size_t i = 0; i < b1; ++i) wait_g[i] *= n1;
601 }
602 const std::vector<double> Mdiag = [&]() {
603 std::vector<double> y = solve(wait_A, wait_H);
604 for (std::size_t i = 0; i < y.size(); ++i) y[i] = -y[i];
605 return y;
606 }();
607 Matrix<double> B(l, l, 0.0);
608 for (std::size_t i = 0; i < l; ++i)
609 for (std::size_t j = 0; j < l; ++j) B(i, j) = wait_A(i, j) * Mdiag[j] / Mdiag[i];
610 std::vector<double> beta(l, 0.0);
611 for (std::size_t i = 0; i < l; ++i) beta[i] = wait_g[i] * Mdiag[i];
612 double beta_0 = 1.0;
613 for (std::size_t i = 0; i < l; ++i) beta_0 -= beta[i];
614 std::vector<double> psi(l, 0.0);
615 for (std::size_t i = 0; i < l; ++i)
616 for (std::size_t j = 0; j < l; ++j) psi[i] -= B(i, j);
617
618 // ---- the age fluid queue of Eqn (19) ----------------------------------
619 z = 4 * l + 2;
620 const std::size_t a2 = 1, b2 = z - 1;
621 Matrix<double> Q2(z, z, 0.0);
622 put(Q2, 0, 0, B);
623 put(Q2, 0, l, mam::kron(col(psi), sigr));
624 for (std::size_t i = 0; i < l; ++i) {
625 for (std::size_t j = 0; j < l; ++j) {
626 Q2(l + i, l + j) = Sm(i, j) - (i == j ? lambda : 0.0);
627 Q2(l + i, 2 * l + j) = (i == j) ? lambda : 0.0;
628 Q2(2 * l + i, 2 * l + j) = Sm(i, j);
629 Q2(3 * l + 1 + i, 3 * l + 1 + j) = Sm(i, j);
630 }
631 Q2(l + i, 3 * l) = nu[i];
632 Q2(3 * l + 1 + i, z - 1) = nu[i];
633 }
634 put(Q2, 2 * l, 3 * l + 1, mam::kron(nuc, sigr));
635 Q2(3 * l, 3 * l) = -lambda;
636 for (std::size_t j = 0; j < l; ++j) Q2(3 * l, 3 * l + 1 + j) = lambda * sigma[j];
637
639 Rd2(z - 1, z - 1) = -1.0;
640 Matrix<double> Qtil2 = Q2;
641 for (std::size_t j = 0; j < l; ++j) {
642 Qtil2(z - 1, j) = beta[j];
643 Qtil2(z - 1, l + j) = beta_0 * sigma[j];
644 }
645 Qtil2(z - 1, z - 1) = -1.0;
646 Matrix<double> QR2 = Q2;
647 for (std::size_t i = 0; i < z; ++i) QR2(i, z - 1) = -QR2(i, z - 1);
648
649 const AlgOneSplit s2 = alg_one(QR2, householder_p(z), Rd2, Qtil2, a2);
650
651 AoiSolution out;
652 out.system_type = "singlebuffer";
653 out.preemption = r;
654 std::vector<double> sel(z, 0.0);
655 for (std::size_t i = 3 * l; i < 4 * l + 1; ++i) sel[i] = 1.0;
656 std::vector<double> h(b2, 0.0);
657 for (std::size_t i = 0; i < b2; ++i)
658 for (std::size_t j = 0; j < z; ++j) h[i] += s2.H(i, j) * sel[j];
659 out.aoi = finish_me(s2.g, s2.A, h);
660
661 std::vector<double> selp(z, 0.0);
662 for (std::size_t i = 0; i < l; ++i) selp[3 * l + 1 + i] = nu[i];
663 std::vector<double> hp(b2, 0.0);
664 for (std::size_t i = 0; i < b2; ++i)
665 for (std::size_t j = 0; j < z; ++j) hp[i] += s2.H(i, j) * selp[j];
666 out.paoi = finish_me(s2.g, s2.A, hp);
667 return out;
668}
669
670/**
671 * `getCdfAoI`: F(t) = 1 - S(t) with S the survival function of the age law.
672 *
673 * (g, A, h) IS A DENSITY TRIPLE, not a survival one: `finish_me` normalizes g by
674 * `-g A^-1 h` so that the density f(t) = g exp(A t) h integrates to 1 and the
675 * mean is `g A^-2 h`. The survival function of such a law is
676 * S(t) = -g exp(A t) A^-1 h, so F(t) = 1 + g exp(A t) A^-1 h, which is 0 at
677 * t = 0 (`g A^-1 h = -1`) and rises to 1. Subtracting the DENSITY instead --
678 * the form all four codebases carried until 2026-07-31 -- gives a curve that
679 * falls before it rises and is not a distribution function at all.
680 */
681inline double aoi_cdf(const AoiMe& me, double t) {
682 if (t <= 0.0) return 0.0;
683 const Matrix<double> E = expm(me.A, t);
684 // gE = g exp(A t), then (gE) A^-1 by the same row solve the moments use.
685 std::vector<double> gE(me.g.size(), 0.0);
686 for (std::size_t j = 0; j < gE.size(); ++j)
687 for (std::size_t i = 0; i < me.g.size(); ++i) gE[j] += me.g[i] * E(i, j);
688 const std::vector<double> y = aoi_detail::rdivide(gE, me.A);
689 double f = 1.0;
690 for (std::size_t i = 0; i < y.size() && i < me.h.size(); ++i) f += y[i] * me.h[i];
691 return std::max(0.0, std::min(1.0, f));
692}
693
694/** Stationary metrics of the finite-capacity AoI queue (at most 1 or 2 updates). */
696 double U = 0.0; ///< P(server busy)
697 double Q = 0.0; ///< mean number of updates at the queue
698 double X = 0.0; ///< delivered throughput (service completions)
699};
700
701/**
702 * Exact metrics from the explicit CTMC of the system the age laws describe.
703 *
704 * Capacity 1 (bufferless PH/PH/1/1, preemption p): states are the idle arrival
705 * phases and the busy (arrival, service) pairs, arrival-major. An arrival
706 * during service restarts it in sigma with probability p and is dropped
707 * otherwise, which is the Q11/Q33 pair of `solveBufferless.m`.
708 * Capacity 2 (single-buffer M/PH/1/2, replacement r): states are empty, one
709 * update in service, and one in service plus one waiting. A replacement swaps
710 * the waiting update for the fresh one and leaves the queue length unchanged.
711 */
712inline AoiQueueMetrics aoi_queue_metrics(const AoiParams& par, double capacity) {
713 const std::size_t l = par.Ssvc.rows();
714 std::vector<double> nu(l, 0.0);
715 for (std::size_t s = 0; s < l; ++s)
716 for (std::size_t t = 0; t < l; ++t) nu[s] -= par.Ssvc(s, t);
718 if (capacity == 1.0) {
719 const std::size_t ka = par.Tarr.rows();
720 std::vector<double> kappa(ka, 0.0);
721 for (std::size_t i = 0; i < ka; ++i)
722 for (std::size_t j = 0; j < ka; ++j) kappa[i] -= par.Tarr(i, j);
723 const std::size_t n = ka + ka * l;
724 Matrix<double> Q(n, n, 0.0);
725 const auto busy = [&](std::size_t i, std::size_t s) { return ka + i * l + s; };
726 for (std::size_t i = 0; i < ka; ++i) {
727 for (std::size_t j = 0; j < ka; ++j) {
728 Q(i, j) += par.Tarr(i, j);
729 for (std::size_t s = 0; s < l; ++s) Q(i, busy(j, s)) += kappa[i] * par.tau[j] * par.sigma[s];
730 }
731 for (std::size_t s = 0; s < l; ++s) {
732 const std::size_t r = busy(i, s);
733 Q(r, i) += nu[s];
734 for (std::size_t t = 0; t < l; ++t) Q(r, busy(i, t)) += par.Ssvc(s, t);
735 for (std::size_t j = 0; j < ka; ++j) {
736 Q(r, busy(j, s)) += par.Tarr(i, j) + (1.0 - par.p) * kappa[i] * par.tau[j];
737 for (std::size_t t = 0; t < l; ++t)
738 Q(r, busy(j, t)) += par.p * kappa[i] * par.tau[j] * par.sigma[t];
739 }
740 }
741 }
742 for (std::size_t r = 0; r < n; ++r) Q(r, 0) = 1.0;
743 std::vector<double> e0(n, 0.0);
744 e0[0] = 1.0;
745 const std::vector<double> pi = aoi_detail::rdivide(e0, Q);
746 for (std::size_t i = 0; i < ka; ++i) {
747 for (std::size_t s = 0; s < l; ++s) {
748 const double pb = pi[busy(i, s)];
749 m.U += pb;
750 m.X += pb * nu[s];
751 }
752 }
753 m.Q = m.U;
754 } else {
755 const double lam = par.lambda;
756 const std::size_t n = 1 + 2 * l;
757 Matrix<double> Q(n, n, 0.0);
758 for (std::size_t s = 0; s < l; ++s) {
759 Q(0, 1 + s) += lam * par.sigma[s];
760 Q(1 + s, 0) += nu[s];
761 Q(1 + s, 1 + l + s) += lam;
762 Q(1 + s, 1 + s) -= lam;
763 for (std::size_t t = 0; t < l; ++t) {
764 Q(1 + s, 1 + t) += par.Ssvc(s, t);
765 Q(1 + l + s, 1 + l + t) += par.Ssvc(s, t);
766 Q(1 + l + s, 1 + t) += nu[s] * par.sigma[t];
767 }
768 }
769 Q(0, 0) = -lam;
770 for (std::size_t r = 0; r < n; ++r) Q(r, 0) = 1.0;
771 std::vector<double> e0(n, 0.0);
772 e0[0] = 1.0;
773 const std::vector<double> pi = aoi_detail::rdivide(e0, Q);
774 double p1 = 0.0, p2 = 0.0;
775 for (std::size_t s = 0; s < l; ++s) {
776 p1 += pi[1 + s];
777 p2 += pi[1 + l + s];
778 m.X += (pi[1 + s] + pi[1 + l + s]) * nu[s];
779 }
780 m.U = p1 + p2;
781 m.Q = p1 + 2.0 * p2;
782 }
783 return m;
784}
785
786/** What the AoI branch of `mfq` returns: the age laws and the metrics beside them. */
789 std::vector<double> QN, UN, RN, TN; ///< per class, at the queue
790 double lambda = 0.0, mu = 0.0;
791};
792
793/** Port of `solver_mfq_aoi.m`. */
794template <class T>
796 double preempt_override) {
797 if (!top.ok)
798 throw UnsupportedError(ErrorKind{"Topology"}, "fluid aoi: " +
799 (top.error.empty() ? std::string("not an AoI topology") : top.error));
800 const AoiParams par = aoi_extract_params(sn, top, preempt_override);
801
802 FluidAoiResult out;
803 if (top.capacity == 1.0)
804 out.age = aoi_solve_bufferless(par.tau, par.Tarr, par.sigma, par.Ssvc, par.p);
805 else
806 out.age = aoi_solve_singlebuffer(par.lambda, par.sigma, par.Ssvc, par.p);
807
808 const std::size_t K = sn.nclasses;
809 out.QN.assign(K, 0.0);
810 out.UN.assign(K, 0.0);
811 out.RN.assign(K, 0.0);
812 out.TN.assign(K, 0.0);
813
814 // The mean service time of the PH pair the AoI algorithm was given.
815 const std::size_t l = par.Ssvc.rows();
816 const std::vector<double> y = aoi_detail::rdivide(par.sigma, par.Ssvc);
817 double mean_svc = 0.0;
818 for (std::size_t i = 0; i < l; ++i) mean_svc -= y[i];
819 out.lambda = par.lambda;
820 out.mu = 1.0 / mean_svc;
821 const AoiQueueMetrics qm = aoi_queue_metrics(par, top.capacity);
822 const std::size_t c = top.cls;
823 out.QN[c] = qm.Q;
824 out.UN[c] = qm.U;
825 out.TN[c] = qm.X;
826 out.RN[c] = qm.Q / qm.X;
827 return out;
828}
829
830} // namespace fluid
831} // namespace line
832
833#endif // LINE_SOLVERS_FLUID_FLUID_AOI_H
MAP to PH conversion for the age-of-information solvers.
InputError(const std::string &what)
Definition error.h:105
std::size_t cols() const
Definition matrix.h:90
std::size_t rows() const
Definition matrix.h:89
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
Eigenvalues and singular values, backed by LAPACK.
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.
Least squares for a rectangular system, exact-capable.
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
AoiPh< T > aoi_dist2ph(const Matrix< T > &D0, const Matrix< T > &D1)
Convert a MAP (D0, D1) into the PH pair (alpha, T).
Definition aoi_dist2ph.h:68
double aoi_cdf(const AoiMe &me, double t)
getCdfAoI: F(t) = 1 - S(t) with S the survival function of the age law.
Definition fluid_aoi.h:681
AoiTopology aoi_is_aoi(const qn::NetworkStruct< T > &sn)
Port of aoi_is_aoi.m.
Definition fluid_aoi.h:106
AoiQueueMetrics aoi_queue_metrics(const AoiParams &par, double capacity)
Exact metrics from the explicit CTMC of the system the age laws describe.
Definition fluid_aoi.h:712
AoiSolution aoi_solve_bufferless(const std::vector< double > &tau, const Matrix< double > &Tm, const std::vector< double > &sigma, const Matrix< double > &Sm, double p)
Port of solveBufferless.m: the AoI and PAoI laws of a PH/PH/1/1 system in which an arrival meeting a ...
Definition fluid_aoi.h:417
AoiParams aoi_extract_params(const qn::NetworkStruct< T > &sn, const AoiTopology &top, double preempt_override)
Port of aoi_extract_params.m.
Definition fluid_aoi.h:211
AoiSolution aoi_solve_singlebuffer(double lambda, const std::vector< double > &sigma, const Matrix< double > &Sm, double r)
Port of solveSingleBuffer.m: the AoI and PAoI laws of an M/PH/1/2 system in which a waiting update is...
Definition fluid_aoi.h:516
FluidAoiResult fluid_aoi(const qn::NetworkStruct< T > &sn, const AoiTopology &top, double preempt_override)
Port of solver_mfq_aoi.m.
Definition fluid_aoi.h:795
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
Definition lang_types.h:194
double z(double v)
Definition lqn_focus.h:261
Matrix< T > kron(const Matrix< T > &A, const Matrix< T > &B)
Kronecker product.
Definition mmap_lambda.h:57
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
LstsqResult< T > lstsq(const Matrix< T > &A, const std::vector< T > &b, const T &tol)
Least-squares solution of A x = b, minimum-norm when A is rank deficient.
Definition lstsq.h:152
Matrix< T > inverse(const Matrix< T > &A)
Inverse by LU with one factorization and n back substitutions.
Definition linalg.h:72
RealSchur schur_reorder(const RealSchur &s, const std::vector< double > &key)
Reorder the diagonal blocks of a real Schur form into DESCENDING key order, stably,...
Definition eig.h:235
Matrix< T > matmul(const Matrix< T > &A, const Matrix< T > &B)
Matrix product A B.
Definition linalg.h:36
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 > eye(std::size_t n)
Identity of order n.
Definition linalg.h:28
RealSchur schur_decomposition(const Matrix< double > &A)
Real Schur factorization of a general square matrix (LAPACK dgees, unsorted).
Definition eig.h:169
Matrix< T > expm(const Matrix< T > &A)
Matrix exponential exp(A).
Definition expm.h:141
A queueing network and its refreshed NetworkStruct.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
Real Schur factorization A = Z T Z^T, with Z orthogonal and T upper quasi-triangular: 1 x 1 diagonal ...
Definition eig.h:156
Matrix< double > Z
orthogonal Schur vectors
Definition eig.h:157
Matrix< double > T
upper quasi-triangular factor
Definition eig.h:158
The (alpha, T) PH pair produced by aoi_dist2ph.
Definition aoi_dist2ph.h:56
Matrix< T > Tmat
sub-generator, MATLAB's T
Definition aoi_dist2ph.h:58
std::vector< T > alpha
initial probability vector, sums to one
Definition aoi_dist2ph.h:57
A matrix-exponential age law: P(age > t) = g exp(A t) h.
Definition fluid_aoi.h:258
std::vector< double > g
Definition fluid_aoi.h:259
std::vector< double > h
Definition fluid_aoi.h:261
Matrix< double > A
Definition fluid_aoi.h:260
The (tau, T) / (sigma, S) pairs and the preemption probability.
Definition fluid_aoi.h:195
Matrix< double > Ssvc
Definition fluid_aoi.h:196
double p
preemption (bufferless) or replacement (single buffer)
Definition fluid_aoi.h:198
double lambda
arrival rate, the single-buffer input
Definition fluid_aoi.h:199
std::vector< double > tau
Definition fluid_aoi.h:197
std::vector< double > sigma
Definition fluid_aoi.h:197
Matrix< double > Tarr
Definition fluid_aoi.h:196
Stationary metrics of the finite-capacity AoI queue (at most 1 or 2 updates).
Definition fluid_aoi.h:695
double X
delivered throughput (service completions)
Definition fluid_aoi.h:698
double Q
mean number of updates at the queue
Definition fluid_aoi.h:697
double U
P(server busy).
Definition fluid_aoi.h:696
Both age laws of one system, with the policy parameter that produced them.
Definition fluid_aoi.h:267
std::string system_type
"bufferless" or "singlebuffer"
Definition fluid_aoi.h:269
What the AoI gate found, when it matches.
Definition fluid_aoi.h:88
double capacity
1 = bufferless, 2 = single buffer
Definition fluid_aoi.h:94
lang::SchedStrategy sched
Definition fluid_aoi.h:95
std::size_t cls
the single open class
Definition fluid_aoi.h:93
std::size_t source
0-based station index
Definition fluid_aoi.h:91
What the AoI branch of mfq returns: the age laws and the metrics beside them.
Definition fluid_aoi.h:787
std::vector< double > RN
Definition fluid_aoi.h:789
std::vector< double > TN
per class, at the queue
Definition fluid_aoi.h:789
std::vector< double > UN
Definition fluid_aoi.h:789
std::vector< double > QN
Definition fluid_aoi.h:789
Matrix< T > D0
The (D0,D1) pair when the type carries one directly.
Definition lang_types.h:921
A node of the network.