LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
aoi_dist2ph.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_AOI_AOI_DIST2PH_H
6#define LINE_API_AOI_AOI_DIST2PH_H
7
8/**
9 * @file
10 * @ingroup api_aoi
11 * MAP to PH conversion for the age-of-information solvers.
12 *
13 * Templated port of matlab/src/api/aoi/aoi_dist2ph.m. The (D0, D1) pair of a
14 * MAP is turned into the (alpha, T) pair the aoi_* algorithms consume:
15 *
16 * T = D0, the sub-generator, unchanged
17 * theta solves theta (D0 + D1) = 0, sum theta = 1
18 * alpha = theta D1 / (theta D1 e), i.e. map_pie, the phase distribution
19 * just after a completion
20 *
21 * Until 2026-11-14 every codebase used theta .* (D1 e) normalized, which is
22 * the phase in which a completion HAPPENS (the exit phase), not the one the
23 * next service starts in. For an Erlang-2 of mean 1 that gave alpha = [0 1]
24 * and a PH of mean 0.5, which then fed every AoI solver through
25 * aoi_extract_params.
26 *
27 * ARITHMETIC. One linear solve, one normalization and sign checks, so this is
28 * exact at T = Rational and is instantiated there. MATLAB obtains theta from
29 * the overdetermined [Q'; e'] \ [0; 1], which its backslash resolves by QR;
30 * on a consistent system that is the exact solution, and the port reaches the
31 * same solution through the normal equations, which is a square solve the
32 * templated LU can carry exactly.
33 *
34 * The generator repair of the reference is kept: when the rows of D0 + D1 do
35 * not sum to zero to within 1e-10, the diagonal is corrected so that they do.
36 * That test is a comparison against a constant and needs no transcendental
37 * function, so it carries over verbatim to the exact instantiation, where it
38 * is essentially never triggered.
39 */
40
41#include <cstddef>
42#include <vector>
43
45#include "line/num/number.h"
46#include "line/util/error.h"
47#include "line/util/linalg.h"
48#include "line/util/lu.h"
49#include "line/util/matrix.h"
50
51namespace line {
52namespace aoi {
53
54/** The (alpha, T) PH pair produced by aoi_dist2ph. */
55template <class T>
56struct AoiPh {
57 std::vector<T> alpha; ///< initial probability vector, sums to one
58 Matrix<T> Tmat; ///< sub-generator, MATLAB's T
59};
60
61/**
62 * Convert a MAP (D0, D1) into the PH pair (alpha, T).
63 *
64 * @param D0 hidden generator
65 * @param D1 arrival matrix
66 */
67template <class T>
68AoiPh<T> aoi_dist2ph(const Matrix<T>& D0, const Matrix<T>& D1) {
69 const std::size_t n = D0.rows();
70 if (n == 0) throw InputError("aoi_dist2ph: empty process");
71 if (D0.cols() != n || D1.rows() != n || D1.cols() != n)
72 throw InputError("aoi_dist2ph: D0 and D1 must be square matrices of the same size");
73
74 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
75 const T genTol = num_traits<T>::from_double(1e-10);
76
77 AoiPh<T> out;
78 out.Tmat = D0;
79
80 Matrix<T> Q(n, n);
81 for (std::size_t i = 0; i < n; ++i)
82 for (std::size_t j = 0; j < n; ++j) Q(i, j) = D0(i, j) + D1(i, j);
83
84 // Repair the generator when the row sums do not vanish (reference behaviour).
85 T worst = zero;
86 std::vector<T> rowSum(n, zero);
87 for (std::size_t i = 0; i < n; ++i) {
88 for (std::size_t j = 0; j < n; ++j) rowSum[i] += Q(i, j);
89 const T a = rowSum[i] < zero ? T(-rowSum[i]) : rowSum[i];
90 if (a > worst) worst = a;
91 }
92 if (worst > genTol)
93 for (std::size_t i = 0; i < n; ++i) Q(i, i) = Q(i, i) - rowSum[i];
94
95 // theta Q = 0, sum theta = 1, through the normal equations of
96 // A theta' = b with A = [Q'; e'] and b = [0; 1].
97 Matrix<T> AtA(n, n, zero);
98 std::vector<T> Atb(n, one); // (A' b)_i = 1, the last row of A being e'
99 for (std::size_t i = 0; i < n; ++i)
100 for (std::size_t j = 0; j < n; ++j) {
101 T s = one; // contribution of the normalization row
102 for (std::size_t k = 0; k < n; ++k) s += Q(i, k) * Q(j, k);
103 AtA(i, j) = s;
104 }
105 std::vector<T> theta = solve(AtA, Atb);
106
107 T mass = zero;
108 for (std::size_t i = 0; i < n; ++i) {
109 if (theta[i] < zero) theta[i] = zero;
110 mass += theta[i];
111 }
112 if (mass == zero) throw NumericError(ErrorKind{"Instability"}, "aoi_dist2ph: the phase process has no stationary law");
113 for (std::size_t i = 0; i < n; ++i) theta[i] = theta[i] / mass;
114
115 out.alpha.assign(n, zero);
116 T num = zero;
117 for (std::size_t j = 0; j < n; ++j) {
118 for (std::size_t i = 0; i < n; ++i) out.alpha[j] += theta[i] * D1(i, j);
119 num += out.alpha[j];
120 }
121 if (num > zero) {
122 for (std::size_t i = 0; i < n; ++i) out.alpha[i] = out.alpha[i] / num;
123 } else {
124 out.alpha = theta; // no completions anywhere: fall back on theta
125 T s = zero;
126 for (std::size_t i = 0; i < n; ++i) s += out.alpha[i];
127 if (s == zero) throw NumericError(ErrorKind{"Degenerate"}, "aoi_dist2ph: alpha cannot be normalized");
128 for (std::size_t i = 0; i < n; ++i) out.alpha[i] = out.alpha[i] / s;
129 }
130
131 for (std::size_t i = 0; i < n; ++i)
132 if (out.Tmat(i, i) > zero)
133 throw InputError("aoi_dist2ph: T has a positive diagonal entry, which is not a "
134 "sub-generator");
135 return out;
136}
137
138/** Convenience overload taking the MAP as line::mam::Map. */
139template <class T>
141 return aoi_dist2ph(m.D0, m.D1);
142}
143
144} // namespace aoi
145} // namespace line
146
147#endif // LINE_API_AOI_AOI_DIST2PH_H
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
NumericError(const std::string &what)
Definition error.h:112
The exception types the port throws.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
LU factorization with partial pivoting, templated on the number type.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
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
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
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
Number-type abstraction for the templated API port.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
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 MAP as the pair of matrices (D0, D1).
Definition map_moment.h:53
Matrix< T > D1
Definition map_moment.h:55
Matrix< T > D0
Definition map_moment.h:54