LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
pfqn_mmint2.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_PFQN_MMINT2_H
6#define LINE_API_PFQN_PFQN_MMINT2_H
7
8/**
9 * @file
10 * @ingroup api_pfqn
11 * McKenna-Mitra integral form of the normalizing constant of a repairman
12 * model (one queueing station, R classes, per-class think time), in its three
13 * MATLAB quadratures.
14 *
15 * Templated port of matlab/src/api/pfqn/pfqn_mmint2.m,
16 * pfqn_mmint2_gausslegendre.m and pfqn_mmint2_gausslaguerre.m. All three
17 * evaluate
18 *
19 * G = 1/prod_r N_r! * int_0^inf u^{m-1} e^{-u} prod_r (Z_r + L_r u)^{N_r} du
20 *
21 * and differ only in the rule: adaptive Gauss-Kronrod up to a mode-relative cutoff,
22 * a fixed Gauss-Legendre rule on [0, 1e6], or Gauss-Laguerre on [0, inf).
23 * The integrand is a polynomial times e^{-u}, so Gauss-Laguerre with enough
24 * nodes is exact up to rounding, which makes it the natural cross-check on the
25 * other two and on pfqn_ca.
26 *
27 * NODES. MATLAB loads a Julia-generated table (gausslegendre-data.mat,
28 * gausslaguerre-data.mat). A table cannot be carried across arithmetics -- it
29 * would pin every instantiation to the precision it was generated at -- so the
30 * rules are regenerated in T by pfqn_asympt_common.h.
31 *
32 * The Legendre form needs care. MATLAB's node count is
33 * n = max(300, min(tablesize, 2(sum N + m - 1) - 1)) and it takes the FIRST n
34 * entries of a 20000-point rule on [0, 1e6], which is not the same thing as a
35 * fresh n-point rule: the 20000-point prefix spans [0.0036, 557.8] and
36 * resolves the e^{-u} factor, whereas a genuine 300-point rule on [0, 1e6] has
37 * its first node at u = 13.7 and misses the mass entirely (it returns
38 * log G = -3.44 where the answer is 1.63). The port therefore generates the
39 * 20000-point rule and takes the same prefix, computing only the prefix since
40 * each Newton iteration is independent of the other nodes. `nodecap` is that
41 * table length and defaults to 20000, the length of MATLAB's
42 * gausslegendre-nodes.txt.
43 *
44 * TRUNCATION of the adaptive form. The integrand exp(g(u)) is log-concave,
45 * so the cutoff is placed where g has fallen K = 12 log(10) + log(2) below its
46 * peak at the mode u*, which bounds the dropped tail by 1e-12 of the integral
47 * at every population. The former fixed cutoff, the 1 - 1e-12 quantile of the
48 * unit exponential (u = 27.63), lost the mass at large populations (log G
49 * -2.66 against 0.405 at N = [20, 20]) and was removed in all codebases.
50 *
51 * ARITHMETIC. Quadrature, so all three are gated on
52 * num_traits<T>::has_transcendental.
53 */
54
55#include <algorithm>
56#include <cmath>
57#include <cstddef>
58#include <vector>
59
62#include "line/num/number.h"
63#include "line/util/error.h"
64
65namespace line {
66namespace pfqn {
67
68/** Return value of the McKenna-Mitra quadratures, mirroring [G, lG]. */
69template <class T>
71 T G;
72 T lG;
73};
74
75/**
76 * Adaptive form (MATLAB pfqn_mmint2): Gauss-Kronrod on [0, u_hi], u_hi past
77 * the mode where log f has fallen by 12 log(10) + log(2), tolerance 1e-12.
78 *
79 * @param L (R) demand at the station, @param N (R) population,
80 * @param Z (R) think times
81 */
82template <class T>
83MmintResult<T> pfqn_mmint2(const std::vector<T>& L, const std::vector<T>& N,
84 const std::vector<T>& Z) {
86 "pfqn_mmint2 requires transcendental arithmetic (quadrature of e^{-u} p(u))");
87 using std::exp;
88 using std::log;
89 const std::size_t R = L.size();
90 if (N.size() != R || Z.size() != R)
91 throw InputError("pfqn_mmint2: L, N and Z must have the same length");
92 const T zero = num_traits<T>::from_int(0);
93
94 // The reference restricts the product to the classes with N_r > 0.
95 std::vector<std::size_t> nz;
96 for (std::size_t r = 0; r < R; ++r)
97 if (N[r] != zero) nz.push_back(r);
98
99 // Integrate over [0, inf). g(u) = -u + sum_r N_r log(Z_r + L_r u) is
100 // concave, so the integrand exp(g) is unimodal with its mass around the
101 // mode u*, which grows with sum(N). The cutoff sits where g has fallen K
102 // below its peak: by concavity the dropped tail is then at most
103 // exp(-K)/(1-exp(-K)) of the integral, and K = order*log(10)+log(2) keeps
104 // it below 10^-order. The integrand is scaled by exp(-g(u*)).
105 const auto g = [&](const T& u) {
106 using std::log;
107 T v = T(-u);
108 for (std::size_t k = 0; k < nz.size(); ++k) {
109 const std::size_t r = nz[k];
110 v += N[r] * log(T(Z[r] + L[r] * u));
111 }
112 return v;
113 };
114 const auto dg = [&](const T& u) {
115 T v = num_traits<T>::from_int(-1);
116 for (std::size_t k = 0; k < nz.size(); ++k) {
117 const std::size_t r = nz[k];
118 v += N[r] * L[r] / T(Z[r] + L[r] * u);
119 }
120 return v;
121 };
122 const T one = num_traits<T>::from_int(1);
123 const T half = num_traits<T>::from_double(0.5);
124 T ustar = zero;
125 if (!nz.empty() && dg(zero) > zero) {
126 T a = zero, b = zero;
127 for (std::size_t k = 0; k < nz.size(); ++k) b += N[nz[k]];
128 for (int it = 0; it < 200; ++it) {
129 const T c = T((a + b) * half);
130 if (dg(c) > zero) a = c; else b = c;
131 if (T(b - a) <= T(num_traits<T>::from_double(1e-14) * (b > one ? b : one))) break;
132 }
133 ustar = T((a + b) * half);
134 }
135 const T gstar = g(ustar);
136 const int order = 12;
137 const T K = num_traits<T>::from_double(order * std::log(10.0) + std::log(2.0));
138 T width = ustar > one ? ustar : one;
139 while (g(T(ustar + width)) > T(gstar - K)) width = T(width + width);
140 T lo = ustar, hi = T(ustar + width);
141 for (int it = 0; it < 200; ++it) {
142 const T c = T((lo + hi) * half);
143 if (g(c) > T(gstar - K)) lo = c; else hi = c;
144 if (T(hi - lo) <= T(num_traits<T>::from_double(1e-12) * (hi > one ? hi : one))) break;
145 }
146 const auto f = [&](const T& u) {
147 using std::exp;
148 return T(exp(T(g(u) - gstar)));
149 };
150 const T tol = num_traits<T>::from_double(std::pow(10.0, -order));
151 const T I = qsys::detail::num_integral<T>(f, zero, hi, tol, tol);
152 if (I <= zero) throw NumericError(ErrorKind{"Degenerate"}, "pfqn_mmint2: non-positive integral");
153
154 MmintResult<T> res;
155 T lG = T(gstar + log(I));
156 for (std::size_t r = 0; r < R; ++r) lG -= detail::num_factln<T>(N[r]);
157 res.lG = lG;
158 res.G = exp(lG);
159 return res;
160}
161
162/**
163 * Gauss-Legendre form on [0, 1e6] (MATLAB pfqn_mmint2_gausslegendre).
164 *
165 * @param m station multiplicity, contributing the u^{m-1} factor
166 * @param nodecap size of the underlying tabulated rule, i.e. MATLAB's table
167 * length; the routine uses its first n nodes
168 * @param L (M) service demands
169 * @param N (1) population, single class
170 * @param Z (1) think time
171 */
172template <class T>
173MmintResult<T> pfqn_mmint2_gausslegendre(const std::vector<T>& L, const std::vector<T>& N,
174 const std::vector<T>& Z, int m, std::size_t nodecap) {
176 "pfqn_mmint2_gausslegendre requires transcendental arithmetic (quadrature)");
177 using std::exp;
178 using std::log;
179 const std::size_t R = L.size();
180 if (N.size() != R || Z.size() != R)
181 throw InputError("pfqn_mmint2_gausslegendre: L, N and Z must have the same length");
182 if (m < 1) throw InputError("pfqn_mmint2_gausslegendre: multiplicity must be at least one");
183 const T zero = num_traits<T>::from_int(0);
184 T Ntot = zero;
185 for (const T& v : N) Ntot += v;
186
187 const long want = 2 * (static_cast<long>(num_traits<T>::to_double(Ntot)) + m - 1) - 1;
188 std::size_t n = 300;
189 const std::size_t capped = std::min<std::size_t>(nodecap, want > 0 ? static_cast<std::size_t>(want) : 1);
190 if (capped > n) n = capped;
191 if (n > nodecap) n = nodecap;
192
193 // 20000-point table prefix rationale: see _kb/03-api-layer.md (cpp port notes: pfqn)
194 std::vector<T> x, w;
195 detail::gauss_legendre<T>(nodecap, zero, num_traits<T>::from_double(1e6), x, w, n);
196
197 std::vector<T> g(n);
198 for (std::size_t i = 0; i < n; ++i) {
199 T y = zero;
200 for (std::size_t r = 0; r < R; ++r) {
201 if (N[r] == zero) continue;
202 y += N[r] * log(T(Z[r] + L[r] * x[i]));
203 }
204 g[i] = T(log(w[i]) - x[i] + y);
205 if (m > 1) g[i] += num_traits<T>::from_int(m - 1) * log(x[i]);
206 }
207 T coeff = zero;
208 for (std::size_t r = 0; r < R; ++r) coeff -= detail::num_factln<T>(N[r]);
209 coeff -= detail::num_factln<T>(num_traits<T>::from_int(m - 1));
210
211 MmintResult<T> res;
212 // stable logsumexp rationale: see _kb/03-api-layer.md (cpp port notes: pfqn)
213 res.lG = T(detail::logsumexp(g) + coeff);
214 res.G = exp(res.lG);
215 return res;
216}
217
218template <class T>
219MmintResult<T> pfqn_mmint2_gausslegendre(const std::vector<T>& L, const std::vector<T>& N,
220 const std::vector<T>& Z) {
221 // 20000 is the length of MATLAB's gausslegendre-nodes.txt.
222 return pfqn_mmint2_gausslegendre(L, N, Z, 1, 20000);
223}
224
225/**
226 * Gauss-Laguerre form (MATLAB pfqn_mmint2_gausslaguerre).
227 *
228 * @param npts node count; MATLAB uses the length of its tabulated rule
229 * @param L (M) service demands
230 * @param N (1) population, single class
231 * @param Z (1) think time
232 * @param m multiplicity of the queueing station
233 */
234template <class T>
235MmintResult<T> pfqn_mmint2_gausslaguerre(const std::vector<T>& L, const std::vector<T>& N,
236 const std::vector<T>& Z, int m, std::size_t npts) {
238 "pfqn_mmint2_gausslaguerre requires transcendental arithmetic (quadrature)");
239 using std::exp;
240 using std::log;
241 const std::size_t R = L.size();
242 if (N.size() != R || Z.size() != R)
243 throw InputError("pfqn_mmint2_gausslaguerre: L, N and Z must have the same length");
244 if (m < 1) throw InputError("pfqn_mmint2_gausslaguerre: multiplicity must be at least one");
245 if (npts < 2) throw InputError("pfqn_mmint2_gausslaguerre: at least two nodes are required");
246 const T zero = num_traits<T>::from_int(0);
247
248 std::vector<T> x, w;
249 detail::gauss_laguerre<T>(npts, x, w);
250 std::vector<T> g(npts);
251 for (std::size_t i = 0; i < npts; ++i) {
252 T F = zero;
253 if (m > 1) F += num_traits<T>::from_int(m - 1) * log(x[i]);
254 for (std::size_t r = 0; r < R; ++r) {
255 if (N[r] == zero) continue;
256 F += N[r] * log(T(Z[r] + L[r] * x[i]));
257 }
258 g[i] = T(log(w[i]) + F);
259 }
260 T coeff = zero;
261 for (std::size_t r = 0; r < R; ++r) coeff -= detail::num_factln<T>(N[r]);
262 coeff -= detail::num_factln<T>(num_traits<T>::from_int(m - 1));
263
264 MmintResult<T> res;
265 res.lG = T(detail::logsumexp(g) + coeff);
266 res.G = exp(res.lG);
267 return res;
268}
269
270template <class T>
271MmintResult<T> pfqn_mmint2_gausslaguerre(const std::vector<T>& L, const std::vector<T>& N,
272 const std::vector<T>& Z) {
273 return pfqn_mmint2_gausslaguerre(L, N, Z, 1, 90);
274}
275
276} // namespace pfqn
277} // namespace line
278
279#endif // LINE_API_PFQN_PFQN_MMINT2_H
InputError(const std::string &what)
Definition error.h:105
NumericError(const std::string &what)
Definition error.h:112
The exception types the port throws.
MmintResult< T > pfqn_mmint2_gausslegendre(const std::vector< T > &L, const std::vector< T > &N, const std::vector< T > &Z, int m, std::size_t nodecap)
Gauss-Legendre form on [0, 1e6] (MATLAB pfqn_mmint2_gausslegendre).
MmintResult< T > pfqn_mmint2(const std::vector< T > &L, const std::vector< T > &N, const std::vector< T > &Z)
Adaptive form (MATLAB pfqn_mmint2): Gauss-Kronrod on [0, u_hi], u_hi past the mode where log f has fa...
Definition pfqn_mmint2.h:83
MmintResult< T > pfqn_mmint2_gausslaguerre(const std::vector< T > &L, const std::vector< T > &N, const std::vector< T > &Z, int m, std::size_t npts)
Gauss-Laguerre form (MATLAB pfqn_mmint2_gausslaguerre).
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Number-type abstraction for the templated API port.
Shared scalar machinery for the integration / asymptotic members of the pfqn family (pfqn_le,...
Adaptive quadrature for the qsys functions whose MATLAB originals call integral(),...
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
Return value of the McKenna-Mitra quadratures, mirroring [G, lG].
Definition pfqn_mmint2.h:70