LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
pfqn_sens_ldmx_ec.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_PFQN_SENS_LDMX_EC_H
6#define LINE_API_PFQN_SENS_LDMX_EC_H
7
8/**
9 * @file
10 * @ingroup api_pfqn
11 * Effective capacity terms of the mixed load-dependent MVA of
12 * Bruell-Balbo-Afshari, together with their exact derivatives with respect to
13 * the open-class load Lo(i) of each station.
14 *
15 * Templated port of matlab/src/api/pfqn/pfqn_sens_ldmx_ec.m. The primal terms
16 * duplicate pfqn_ldmx_ec.h; they are recomputed here rather than reused
17 * because every intermediate (E1, F2, F3, F2prime) has to be carried alongside
18 * its derivative, and the reference does the same.
19 *
20 * Lo(i) = sum_r lambda(r) D(i,r) is the only channel through which a demand
21 * enters E, Eprime and EC, since the load-dependent rates mu do not depend on
22 * the demands. Station i's terms depend on Lo(i) alone, so one derivative per
23 * station suffices and the chain rule then gives the derivative with respect
24 * to any demand-scaling parameter. This is the factorization behind equations
25 * (19), (21) and (24)-(31) of the reference, reproduced here term by term.
26 *
27 * Arithmetic. Every term is a rational function of Lo and of the reciprocal
28 * rates, differentiated symbolically, so the routine stays in the field and
29 * instantiates at line::Rational with no gate.
30 *
31 * Reference: I. F. Akyildiz and J. C. Strelen, "Moment Analysis for
32 * Load-Dependent Mixed Product Form Queueing Networks", IEEE Trans.
33 * Communications 39(6):828-832, 1991.
34 */
35
36#include <cstddef>
37#include <vector>
38
39#include "line/num/number.h"
40#include "line/util/error.h"
41#include "line/util/matrix.h"
42
43namespace line {
44namespace pfqn {
45
46template <class T>
48 Matrix<T> EC; ///< (M x Nt) effective capacity, EC(i,n-1) for n = 1..Nt
49 Matrix<T> E; ///< (M x Nt+1) E-function, column n
50 Matrix<T> Eprime; ///< (M x Nt+1) Eprime-function, column n
51 std::vector<T> Lo; ///< (M) open load per station
52 Matrix<T> dEC; ///< (M x Nt) dEC(i,n)/dLo(i)
53 Matrix<T> dE; ///< (M x Nt+1) dE(i,n)/dLo(i)
54 Matrix<T> dEprime; ///< (M x Nt+1) dEprime(i,n)/dLo(i)
55 Matrix<T> d2EC; ///< (M x Nt) d2EC(i,n)/dLo(i)^2, for pfqn_sens_momldmx
56 Matrix<T> d2E; ///< (M x Nt+1) d2E(i,n)/dLo(i)^2
57 Matrix<T> d2Eprime; ///< (M x Nt+1) d2Eprime(i,n)/dLo(i)^2
58};
59
60/**
61 * @brief Effective capacity terms of the mixed load-dependent MVA of
62 * Bruell-Balbo-Afshari, together with their exact derivatives with
63 * respect to the open-class load Lo(i) of each station.
64 *
65 * @param lambda (R) arrival rates, zero on the closed classes
66 * @param D (M x R) service demands
67 * @param mu (M x Nt) load-dependent rate lattice, limited load dependence
68 */
69template <class T>
70SensLdmxEcResult<T> pfqn_sens_ldmx_ec(const std::vector<T>& lambda, const Matrix<T>& D,
71 const Matrix<T>& mu) {
72 const std::size_t M = mu.rows();
73 const std::size_t Nt = mu.cols();
74 if (M == 0 || Nt == 0) throw InputError("pfqn_sens_ldmx_ec: the rate lattice is empty");
75 if (D.rows() != M)
76 throw InputError("pfqn_sens_ldmx_ec: D and mu disagree on the station count");
77 if (lambda.size() != D.cols())
78 throw InputError("pfqn_sens_ldmx_ec: lambda and D disagree on the class count");
79
80 const T zero = num_traits<T>::from_int(0);
81 const T one = num_traits<T>::from_int(1);
82
84 res.Lo.assign(M, zero);
85 for (std::size_t i = 0; i < M; ++i)
86 for (std::size_t r = 0; r < lambda.size(); ++r) res.Lo[i] += lambda[r] * D(i, r);
87
88 std::vector<std::size_t> b(M, 1);
89 for (std::size_t i = 0; i < M; ++i) {
90 const T& last = mu(i, Nt - 1);
91 std::size_t k = 1;
92 while (k < Nt && mu(i, k - 1) != last) ++k;
93 b[i] = k;
94 }
95
96 // padding-width rationale: see _kb/03-api-layer.md (cpp port notes: pfqn)
97 const std::size_t Cw = 2 * Nt + 2;
98 Matrix<T> C(M, Cw, zero);
99 for (std::size_t i = 0; i < M; ++i)
100 for (std::size_t k = 0; k < Cw; ++k) {
101 const T& rate = k < Nt ? mu(i, k) : mu(i, Nt - 1);
102 if (rate == zero) throw NumericError(ErrorKind{"Degenerate"}, "pfqn_sens_ldmx_ec: a load-dependent rate is zero");
103 C(i, k) = one / rate;
104 }
105 // 1-based rate index of the reference.
106 const auto Cq = [&](std::size_t i, std::size_t k) -> const T& { return C(i, k - 1); };
107
108 res.EC = Matrix<T>(M, Nt, zero);
109 res.E = Matrix<T>(M, Nt + 1, zero);
110 res.Eprime = Matrix<T>(M, Nt + 1, zero);
111 res.dEC = Matrix<T>(M, Nt, zero);
112 res.dE = Matrix<T>(M, Nt + 1, zero);
113 res.dEprime = Matrix<T>(M, Nt + 1, zero);
114 res.d2EC = Matrix<T>(M, Nt, zero);
115 res.d2E = Matrix<T>(M, Nt + 1, zero);
116 res.d2Eprime = Matrix<T>(M, Nt + 1, zero);
117 const T two = num_traits<T>::from_int(2);
118
119 for (std::size_t i = 0; i < M; ++i) {
120 const std::size_t bi = b[i];
121 const T Cb = Cq(i, bi);
122 const T den = one - res.Lo[i] * Cb;
123 if (den == zero)
124 throw NumericError(ErrorKind{"Instability"},
125 "pfqn_sens_ldmx_ec: the station is saturated by the open classes (Lo * C(b) = 1)");
126 const std::size_t nhead = bi >= 2 ? bi - 1 : 0; // n0 = 0 .. bi-2
127
128 std::vector<T> E1(Nt + 1, zero), dE1(Nt + 1, zero);
129 std::vector<T> E2(Nt + 1, zero), dE2(Nt + 1, zero);
130 std::vector<T> E3(Nt + 1, zero), dE3(Nt + 1, zero);
131 std::vector<T> E2p(Nt + 1, zero), dE2p(Nt + 1, zero);
132 Matrix<T> F2(Nt + 1, nhead + 1, zero), dF2(Nt + 1, nhead + 1, zero);
133 Matrix<T> F3(Nt + 1, nhead + 1, zero), dF3(Nt + 1, nhead + 1, zero);
134 Matrix<T> F2p(Nt + 1, nhead + 1, zero), dF2p(Nt + 1, nhead + 1, zero);
135 std::vector<T> d2E1(Nt + 1, zero), d2E2(Nt + 1, zero), d2E3(Nt + 1, zero),
136 d2E2p(Nt + 1, zero);
137 Matrix<T> d2F2(Nt + 1, nhead + 1, zero), d2F3(Nt + 1, nhead + 1, zero),
138 d2F2p(Nt + 1, nhead + 1, zero);
139
140 for (std::size_t n = 0; n <= Nt; ++n) {
141 if (n >= bi) {
142 // E(n) = 1/den^(n+1) => dE/dLo = (n+1) Cb / den^(n+2)
143 res.E(i, n) = one / num_pow_int(den, static_cast<unsigned>(n + 1));
144 res.dE(i, n) = num_traits<T>::from_int(static_cast<long>(n + 1)) * Cb /
145 num_pow_int(den, static_cast<unsigned>(n + 2));
146 res.d2E(i, n) = num_traits<T>::from_int(static_cast<long>((n + 1) * (n + 2))) *
147 Cb * Cb / num_pow_int(den, static_cast<unsigned>(n + 3));
148 res.Eprime(i, n) = Cb * res.E(i, n);
149 res.dEprime(i, n) = Cb * res.dE(i, n);
150 res.d2Eprime(i, n) = Cb * res.d2E(i, n);
151 continue;
152 }
153 // ---- E1, eq. (25)-(26) -----------------------------------------
154 if (n == 0) {
155 E1[0] = one / den;
156 dE1[0] = Cb / (den * den);
157 d2E1[0] = two * Cb * Cb / (den * den * den);
158 for (std::size_t j = 1; j + 1 <= bi; ++j) {
159 E1[0] *= Cq(i, j) / Cb;
160 dE1[0] *= Cq(i, j) / Cb;
161 d2E1[0] *= Cq(i, j) / Cb;
162 }
163 } else {
164 const T fac = Cb / Cq(i, n);
165 E1[n] = one / den * fac * E1[n - 1];
166 dE1[n] = Cb / (den * den) * fac * E1[n - 1] + one / den * fac * dE1[n - 1];
167 // Leibniz rule with g = 1/den, g' = Cb/den^2, g'' = 2 Cb^2/den^3
168 d2E1[n] = fac * (two * Cb * Cb / (den * den * den) * E1[n - 1] +
169 two * Cb / (den * den) * dE1[n - 1] + one / den * d2E1[n - 1]);
170 }
171
172 // ---- F2, eq. (27)-(28) ------------------------------------------
173 for (std::size_t n0 = 0; n0 <= nhead; ++n0) {
174 if (n0 == 0) {
175 F2(n, 0) = one;
176 dF2(n, 0) = zero;
177 } else {
178 const T coef = num_traits<T>::from_int(static_cast<long>(n + n0)) /
179 num_traits<T>::from_int(static_cast<long>(n0)) * Cq(i, n + n0);
180 F2(n, n0) = coef * res.Lo[i] * F2(n, n0 - 1);
181 dF2(n, n0) = coef * (F2(n, n0 - 1) + res.Lo[i] * dF2(n, n0 - 1));
182 d2F2(n, n0) = coef * (two * dF2(n, n0 - 1) + res.Lo[i] * d2F2(n, n0 - 1));
183 }
184 }
185 E2[n] = zero;
186 dE2[n] = zero;
187 d2E2[n] = zero;
188 for (std::size_t n0 = 0; n0 < nhead; ++n0) { // n0 = 0 .. bi-2
189 E2[n] += F2(n, n0);
190 dE2[n] += dF2(n, n0);
191 d2E2[n] += d2F2(n, n0);
192 }
193
194 // ---- F3, eq. (29)-(30) ------------------------------------------
195 for (std::size_t n0 = 0; n0 <= nhead; ++n0) {
196 if (n == 0 && n0 == 0) {
197 F3(0, 0) = one;
198 for (std::size_t j = 1; j + 1 <= bi; ++j) F3(0, 0) *= Cq(i, j) / Cb;
199 dF3(0, 0) = zero;
200 } else if (n > 0 && n0 == 0) {
201 const T fac = Cb / Cq(i, n);
202 F3(n, 0) = fac * F3(n - 1, 0);
203 dF3(n, 0) = fac * dF3(n - 1, 0);
204 d2F3(n, 0) = fac * d2F3(n - 1, 0);
205 } else {
206 const T coef = num_traits<T>::from_int(static_cast<long>(n + n0)) /
207 num_traits<T>::from_int(static_cast<long>(n0)) * Cb;
208 F3(n, n0) = coef * res.Lo[i] * F3(n, n0 - 1);
209 dF3(n, n0) = coef * (F3(n, n0 - 1) + res.Lo[i] * dF3(n, n0 - 1));
210 d2F3(n, n0) = coef * (two * dF3(n, n0 - 1) + res.Lo[i] * d2F3(n, n0 - 1));
211 }
212 }
213 E3[n] = zero;
214 dE3[n] = zero;
215 d2E3[n] = zero;
216 for (std::size_t n0 = 0; n0 < nhead; ++n0) {
217 E3[n] += F3(n, n0);
218 dE3[n] += dF3(n, n0);
219 d2E3[n] += d2F3(n, n0);
220 }
221
222 // ---- F2prime -----------------------------------------------------
223 for (std::size_t n0 = 0; n0 <= nhead; ++n0) {
224 if (n0 == 0) {
225 F2p(n, 0) = Cq(i, n + 1);
226 dF2p(n, 0) = zero;
227 } else {
228 const T coef = num_traits<T>::from_int(static_cast<long>(n + n0)) /
229 num_traits<T>::from_int(static_cast<long>(n0)) *
230 Cq(i, n + n0 + 1);
231 F2p(n, n0) = coef * res.Lo[i] * F2p(n, n0 - 1);
232 dF2p(n, n0) = coef * (F2p(n, n0 - 1) + res.Lo[i] * dF2p(n, n0 - 1));
233 d2F2p(n, n0) = coef * (two * dF2p(n, n0 - 1) + res.Lo[i] * d2F2p(n, n0 - 1));
234 }
235 }
236 E2p[n] = zero;
237 dE2p[n] = zero;
238 d2E2p[n] = zero;
239 for (std::size_t n0 = 0; n0 < nhead; ++n0) {
240 E2p[n] += F2p(n, n0);
241 dE2p[n] += dF2p(n, n0);
242 d2E2p[n] += d2F2p(n, n0);
243 }
244
245 // ---- E = E1 + E2 - E3, eq. (23)-(24) ------------------------------
246 res.E(i, n) = E1[n] + E2[n] - E3[n];
247 res.dE(i, n) = dE1[n] + dE2[n] - dE3[n];
248 res.d2E(i, n) = d2E1[n] + d2E2[n] - d2E3[n];
249 if (n + 1 < bi) {
250 res.Eprime(i, n) = Cb * E1[n] + E2p[n] - Cb * E3[n];
251 res.dEprime(i, n) = Cb * dE1[n] + dE2p[n] - Cb * dE3[n];
252 res.d2Eprime(i, n) = Cb * d2E1[n] + d2E2p[n] - Cb * d2E3[n];
253 } else {
254 res.Eprime(i, n) = Cb * res.E(i, n);
255 res.dEprime(i, n) = Cb * res.dE(i, n);
256 res.d2Eprime(i, n) = Cb * res.d2E(i, n);
257 }
258 }
259
260 // EC(n) = C(n) E(n)/E(n-1), eq. (19), and the quotient rule for dEC.
261 for (std::size_t n = 1; n <= Nt; ++n) {
262 if (res.E(i, n - 1) == zero) throw NumericError(ErrorKind{"Degenerate"}, "pfqn_sens_ldmx_ec: E vanishes");
263 res.EC(i, n - 1) = Cq(i, n) * res.E(i, n) / res.E(i, n - 1);
264 res.dEC(i, n - 1) = Cq(i, n) *
265 (res.dE(i, n) * res.E(i, n - 1) - res.E(i, n) * res.dE(i, n - 1)) /
266 (res.E(i, n - 1) * res.E(i, n - 1));
267 // q = u/v: q'' = (u'' - 2 q' v' - q v'')/v
268 const T v = res.E(i, n - 1);
269 const T q = res.E(i, n) / v;
270 const T dq = (res.dE(i, n) - q * res.dE(i, n - 1)) / v;
271 const T d2q = (res.d2E(i, n) - two * dq * res.dE(i, n - 1) - q * res.d2E(i, n - 1)) / v;
272 res.d2EC(i, n - 1) = Cq(i, n) * d2q;
273 }
274 }
275
276 return res;
277}
278
279} // namespace pfqn
280} // namespace line
281
282#endif // LINE_API_PFQN_SENS_LDMX_EC_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 matrix and non-owning view.
SensLdmxEcResult< T > pfqn_sens_ldmx_ec(const std::vector< T > &lambda, const Matrix< T > &D, const Matrix< T > &mu)
Effective capacity terms of the mixed load-dependent MVA of Bruell-Balbo-Afshari, together with their...
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
T num_pow_int(const T &base, unsigned e)
Integer power, valid in any field (no transcendental requirement).
Definition number.h:204
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
std::vector< T > Lo
(M) open load per station
Matrix< T > dEprime
(M x Nt+1) dEprime(i,n)/dLo(i)
Matrix< T > d2EC
(M x Nt) d2EC(i,n)/dLo(i)^2, for pfqn_sens_momldmx
Matrix< T > dE
(M x Nt+1) dE(i,n)/dLo(i)
Matrix< T > dEC
(M x Nt) dEC(i,n)/dLo(i)
Matrix< T > E
(M x Nt+1) E-function, column n
Matrix< T > EC
(M x Nt) effective capacity, EC(i,n-1) for n = 1..Nt
Matrix< T > d2E
(M x Nt+1) d2E(i,n)/dLo(i)^2
Matrix< T > Eprime
(M x Nt+1) Eprime-function, column n
Matrix< T > d2Eprime
(M x Nt+1) d2Eprime(i,n)/dLo(i)^2