LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
solver_ba_analyzer.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_BA_SOLVER_BA_ANALYZER_H
6#define LINE_SOLVERS_BA_SOLVER_BA_ANALYZER_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * Port of `matlab/src/solvers/BA/solver_ba_analyzer.m`, the bound-analysis
12 * handler behind SolverBA.
13 *
14 * Each method returns ONE side of a bracket, optimistic or pessimistic, never a
15 * point estimate. The bound itself is a scalar on the chain throughput; the
16 * per-station [Q,U,R,T,C] around it is reconstructed by the utilization law and
17 * by the same optimistic/pessimistic residence convention the ABA bound uses,
18 * which is why every family agrees on the shape of the answer even where the
19 * literature leaves the per-station response time undefined.
20 *
21 * WHAT A BOUND NEEDS is demands (visits x mean service time) and populations.
22 * No service-time distribution enters, so the featset is deliberately narrow:
23 * closed, product-form-parameterized models. The single-class families reject a
24 * multiclass model by name rather than answer for one class.
25 *
26 * ARITHMETIC. The asymptotic, balanced, proportional and box families are sums,
27 * products, integer powers, minima and divisions, so they are exact in rational
28 * arithmetic. Three paths are not and are gated: the geometric family (gb) and
29 * the successively-improving bounds (sib) solve a quadratic, and the sb lower
30 * bound takes an (N-1)-st root.
31 */
32
33#include <algorithm>
34#include <cmath>
35#include <cstddef>
36#include <cstdio>
37#include <limits>
38#include <sstream>
39#include <string>
40#include <vector>
41
67
68namespace line {
69namespace ba {
70
72
73/** The options SolverBA reads. Defaults are `SolverBA.defaultOptions`. */
74struct BaOptions {
75 /** Bound method; `default` resolves to `gb.upper` in the runner. */
76 std::string method = "default";
77 /**
78 * `options.level`: the hierarchy level of pbh/bjbh/cbh/sib. MATLAB's
79 * default is 2 and is NOT the pfqn default,
80 * which is 1 for pbh and 3 for sib -- the solver overrides both.
81 */
82 int level = 2;
83 /**
84 * `options.config.qrf_alpha`, the (nstations x N) load-dependent scaling of
85 * the two load-dependent QRF arms. Empty means all ones.
86 */
88 /**
89 * `options.config.qrf_params`, the blocking tables the BAS and RS-RD arms
90 * need. `sn_to_qrf_params` in the reference refuses rather than defaulting:
91 * assuming no blocking puts the bound ~31x farther from exact.
92 */
93 struct QrfParams {
94 bool supplied = false;
95 int f = 1; ///< finite-capacity queue, 1-based as in the reference
96 int MR = 1; ///< number of blocking configurations
97 std::vector<std::vector<int> > BB; ///< (MR x M) blocking state
98 std::vector<std::vector<int> > MM; ///< (MR x 2) blocking order
99 std::vector<std::vector<int> > MM1; ///< (MR x M) extended order
100 std::vector<int> ZZ; ///< (MR) blocked count per config
101 std::vector<int> F; ///< (M) capacity; empty takes sn.cap
102 };
104};
105
106/** Class-level results, the [Q,U,R,T,C,X] of `solver_ba_analyzer`. */
107template <class T>
110 std::vector<T> C, X;
111 /** `-N log X`, the reference's approximate normalizing constant. */
112 double lG = 0.0;
113 int iter = 1;
114};
115
116namespace detail {
117
118/** Station-indexed visit vector of the single chain of a single-class model. */
119template <class T>
120std::vector<T> ba_station_visits(const qn::NetworkStruct<T>& L) {
121 // THE CONVERSION IS LOAD-BEARING, not a formality: `L.visits` is
122 // STATEFUL-indexed (nstateful x nclasses) while `L.rates`, `L.stations` and
123 // the sched are STATION-indexed. Every station is stateful, but not every
124 // stateful node is a station -- a Transition is stateful without being one,
125 // a Place is both -- so on a Petri net the two lengths differ (4 against 7
126 // on the fork-join SPN). Walking the VISIT rows and indexing the station
127 // arrays with the row is the bug the other three codebases carried into
128 // their degeneracy predicate; do not "simplify" this loop into it.
129 std::vector<T> V(L.nstations, num_traits<T>::from_int(0));
130 for (std::size_t i = 0; i < L.nstations; ++i)
131 V[i] = L.visits[0](L.stateful_of_station(i + 1) - 1, 0);
132 return V;
133}
134
135template <class T>
136bool ba_is_delay(const qn::NetworkStruct<T>& L, std::size_t i) {
137 return L.stations[i].sched == SchedStrategy::INF;
138}
139
140/**
141 * Port of the local `ba_sc_demands`: the visit vector, the aggregate think
142 * time, the per-queue demand vector and the closed population.
143 */
144template <class T>
145struct ScDemands {
146 std::vector<T> V; ///< (M) station-indexed visits
147 std::vector<T> D; ///< (Mq) demands of the queueing stations, in station order
148 T Z; ///< aggregate think time of the delay stations
149 long N; ///< closed population
150};
151
152template <class T>
153ScDemands<T> ba_sc_demands(const qn::NetworkStruct<T>& L) {
154 // No single-class test here: `method_refusal` owns that rule for every
155 // caller, and a second copy is what would let the report and the run
156 // disagree. `solver_ba_analyzer` has already asked it by the time we arrive.
157 ScDemands<T> d;
158 d.V = ba_station_visits(L);
159 d.Z = num_traits<T>::from_int(0);
160 for (std::size_t i = 0; i < L.nstations; ++i) {
161 const T di = T(d.V[i] / L.rates(i, 0));
162 if (ba_is_delay(L, i))
163 d.Z += di;
164 else
165 d.D.push_back(di);
166 }
167 d.N = static_cast<long>(L.nclosedjobs());
168 return d;
169}
170
171/**
172 * The per-station metrics implied by a scalar chain-throughput bound and a
173 * system response time, shared by every family (`ba_fill` in the reference,
174 * inlined in each noniterative branch there).
175 *
176 * The optimistic side charges every queueing station the full-contention
177 * response time N/mu and the pessimistic side the no-contention 1/mu; a delay
178 * station takes 1/mu on both, having no contention to bound.
179 */
180template <class T>
181BaSolution<T> ba_fill_xc(const qn::NetworkStruct<T>& L, const std::vector<T>& V, long N, const T& X,
182 const T& C, bool is_upper) {
183 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
184 const std::size_t M = L.nstations;
185 BaSolution<T> s;
186 s.Q = Matrix<T>(M, 1, zero);
187 s.U = Matrix<T>(M, 1, zero);
188 s.R = Matrix<T>(M, 1, zero);
189 s.Tp = Matrix<T>(M, 1, zero);
190 for (std::size_t i = 0; i < M; ++i) {
191 s.Tp(i, 0) = T(V[i] * X);
192 const T svc = T(one / L.rates(i, 0));
193 s.R(i, 0) = (is_upper && !ba_is_delay(L, i)) ? T(svc * num_traits<T>::from_int(N)) : svc;
194 s.Q(i, 0) = T(s.Tp(i, 0) * s.R(i, 0));
195 if (ba_is_delay(L, i)) {
196 s.U(i, 0) = s.Q(i, 0);
197 } else {
198 // Utilization law per SERVER: without the nservers divisor a
199 // multiserver station reports U > 1 (ssd/ldac/auto reach here)
200 const T c = num_traits<T>::from_double(std::max(1.0, L.stations[i].nservers));
201 s.U(i, 0) = T(s.Tp(i, 0) / T(c * L.rates(i, 0)));
202 }
203 }
204 s.C.assign(1, C);
205 s.X.assign(1, X);
206 s.lG = -static_cast<double>(N) * num_traits<T>::log_as_double(X);
207 return s;
208}
209
210/**
211 * Port of the local `ba_chain_qfill`, the chain-indexed queue length of the two
212 * multiclass families.
213 *
214 * NEITHER RESIDENCE THE BOUND CARRIES YIELDS A QUEUE LENGTH ON THE DECLARED
215 * SIDE: the optimistic no-contention residence understates it and the Theorem-1
216 * residence of mwrbb overstates it, so the reference builds Q from the
217 * utilizations instead. The pessimistic side takes U itself, which is the jobs
218 * in service and so a lower bound on the jobs present; the optimistic side
219 * charges the whole chain population to the station in proportion to its
220 * saturation, capped at one.
221 */
222template <class T>
223Matrix<T> ba_chain_qfill(const Matrix<T>& Uchain, const std::vector<T>& Xchain,
224 const Matrix<T>& Lchain, const std::vector<T>& Nchain,
225 const std::vector<bool>& isdelay, bool is_upper) {
226 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
227 const std::size_t M = Uchain.rows(), C = Uchain.cols();
228 std::vector<T> Utot(M, zero);
229 for (std::size_t i = 0; i < M; ++i) {
230 for (std::size_t c = 0; c < C; ++c) Utot[i] += Uchain(i, c);
231 if (Utot[i] > one) Utot[i] = one;
232 }
233 Matrix<T> Qchain(M, C, zero);
234 for (std::size_t c = 0; c < C; ++c)
235 for (std::size_t i = 0; i < M; ++i) {
236 if (isdelay[i])
237 Qchain(i, c) = T(Xchain[c] * Lchain(i, c));
238 else if (is_upper)
239 Qchain(i, c) = T(Nchain[c] * Utot[i]);
240 else
241 Qchain(i, c) = Uchain(i, c);
242 }
243 return Qchain;
244}
245
246/** Port of the local `ba_fill`, whose cycle time is the ABA one at this side. */
247template <class T>
248BaSolution<T> ba_fill(const qn::NetworkStruct<T>& L, const std::vector<T>& V, long N, const T& X,
249 const T& Z, const std::vector<T>& D, bool is_upper) {
250 T Dsum = num_traits<T>::from_int(0);
251 for (const T& d : D) Dsum += d;
252 const T C = is_upper ? T(Z + num_traits<T>::from_int(N) * Dsum) : T(Z + Dsum);
253 return ba_fill_xc(L, V, N, X, C, is_upper);
254}
255
256/**
257 * Port of the local `ba_sc_ld_demands`: the queueing stations' rate scaling
258 * for the load-dependent families, mu(k,n) = lldscaling(i,n) * min(n, c_i) for
259 * n = 1..N, with a limited lldscaling row held at its last entry, as
260 * solver_mvald reads it. Rows follow the queueing stations in station order,
261 * the order of ScDemands::D.
262 */
263template <class T>
264Matrix<T> ba_sc_ld_mu(const qn::NetworkStruct<T>& L, long N) {
265 std::vector<std::size_t> qst;
266 for (std::size_t i = 0; i < L.nstations; ++i)
267 if (!ba_is_delay(L, i)) qst.push_back(i);
268 const std::size_t NN = static_cast<std::size_t>(N > 0 ? N : 0);
269 Matrix<T> mu(qst.size(), NN, num_traits<T>::from_int(1));
270 for (std::size_t k = 0; k < qst.size(); ++k) {
271 const auto& st = L.stations[qst[k]];
272 for (std::size_t n = 1; n <= NN; ++n) {
273 T row = num_traits<T>::from_int(1);
274 if (!st.lldscaling.empty()) row = st.lldscaling[std::min(n, st.lldscaling.size()) - 1];
275 const double c = st.nservers;
276 const double cap = static_cast<double>(n) < c ? static_cast<double>(n) : c;
277 mu(k, n - 1) = T(row * num_traits<T>::from_double(cap));
278 }
279 }
280 return mu;
281}
282
283/**
284 * Port of the local `ba_ld_fill`: per-station [Q,U,R,T,C] for a throughput
285 * bound X of a network with queue-dependent rates. Station i completes jobs at
286 * rate rate_i*mu_i(n) with n jobs present, so X*V_i = rate_i*E[mu_i(n_i)],
287 * while E[n_i] = X*V_i*R_i by Little's law; E[n_i] and E[mu_i(n_i)] weigh n and
288 * mu_i(n), n >= 1, with the same probabilities, so the per-visit residence R_i
289 * lies between the smallest and the largest of n/mu_i(n) over n = 1..N, divided
290 * by rate_i, whatever X is. The queue length on the declared side follows from
291 * the throughput bound of that side (capped at N above). For a fixed-rate
292 * station this is R in [1/rate, N/rate], the ba_fill convention.
293 */
294template <class T>
295BaSolution<T> ba_ld_fill(const qn::NetworkStruct<T>& L, const std::vector<T>& V, long N, const T& X,
296 const T& Z, const Matrix<T>& mu, bool is_upper) {
297 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
298 const std::size_t M = L.nstations;
299 const T Nt = num_traits<T>::from_int(N);
300 BaSolution<T> s;
301 s.Q = Matrix<T>(M, 1, zero);
302 s.U = Matrix<T>(M, 1, zero);
303 s.R = Matrix<T>(M, 1, zero);
304 s.Tp = Matrix<T>(M, 1, zero);
305 T C = Z;
306 std::size_t k = 0;
307 for (std::size_t i = 0; i < M; ++i) {
308 const T rate = L.rates(i, 0);
309 T Ri = T(one / rate);
310 if (!ba_is_delay(L, i)) {
311 T best = zero;
312 for (std::size_t n = 0; n < mu.cols(); ++n) {
313 const T ratio = T(num_traits<T>::from_int(static_cast<long>(n + 1)) / mu(k, n));
314 if (n == 0 || (is_upper ? ratio > best : ratio < best)) best = ratio;
315 }
316 if (mu.cols() > 0) Ri = T(best / rate);
317 ++k;
318 }
319 s.R(i, 0) = Ri;
320 s.Tp(i, 0) = T(V[i] * X);
321 s.Q(i, 0) = T(s.Tp(i, 0) * Ri);
322 if (ba_is_delay(L, i)) {
323 s.U(i, 0) = s.Q(i, 0);
324 } else {
325 if (is_upper && s.Q(i, 0) > Nt) s.Q(i, 0) = Nt;
326 C += T(V[i] * Ri);
327 const T c = num_traits<T>::from_double(std::max(1.0, L.stations[i].nservers));
328 s.U(i, 0) = T(s.Tp(i, 0) / T(c * rate));
329 }
330 }
331 s.C.assign(1, C);
332 s.X.assign(1, X);
333 s.lG = -static_cast<double>(N) * num_traits<T>::log_as_double(X);
334 return s;
335}
336
337/** min, max, sum and mean of the demand vector, which every family needs. */
338template <class T>
339struct DemandStats {
340 T sum, max, mean;
341};
342
343template <class T>
344DemandStats<T> ba_demand_stats(const std::vector<T>& D) {
345 if (D.empty()) throw UnsupportedError(ErrorKind{"Topology"}, "solver_ba_analyzer: the model has no queueing station");
346 DemandStats<T> st;
347 st.sum = num_traits<T>::from_int(0);
348 st.max = D[0];
349 for (const T& d : D) {
350 st.sum += d;
351 if (d > st.max) st.max = d;
352 }
353 st.mean = T(st.sum / num_traits<T>::from_int(static_cast<long>(D.size())));
354 return st;
355}
356
357template <class T>
358T ba_min(const T& a, const T& b) {
359 return a < b ? a : b;
360}
361
362/**
363 * Port of the local `mwrbb_disc_code`.
364 *
365 * The reference additionally maps the priority-scheduling variants of PS/DPS/GPS
366 * and the preemptive-resume priority disciplines; neither family exists in this
367 * port's SchedStrategy, so no input can reach those codes and the default
368 * (ABA full-contention, code 4) covers every remaining work-conserving case.
369 */
370inline pfqn::MwrbbSched mwrbb_disc_code(SchedStrategy s) {
371 switch (s) {
372 case SchedStrategy::FCFS:
374 case SchedStrategy::PS:
375 case SchedStrategy::DPS:
376 case SchedStrategy::GPS:
378 case SchedStrategy::HOL:
380 default:
382 }
383}
384
385/** The chain-level scaffolding the two multiclass families share. */
386template <class T>
387struct ChainView {
388 mva::ChainDemands<T> d;
389 std::vector<bool> isdelay;
390 std::vector<std::size_t> qstat; ///< 0-based station index of each queueing station
391 std::vector<T> Nv, Zc;
392};
393
394template <class T>
395ChainView<T> ba_chain_view(const qn::NetworkStruct<T>& L) {
396 ChainView<T> v;
398 const std::size_t M = L.nstations, C = L.nchains;
399 v.isdelay.assign(M, false);
400 for (std::size_t i = 0; i < M; ++i) {
401 v.isdelay[i] = ba_is_delay(L, i);
402 if (!v.isdelay[i]) v.qstat.push_back(i);
403 }
404 v.Nv.resize(C);
405 v.Zc.assign(C, num_traits<T>::from_int(0));
406 for (std::size_t c = 0; c < C; ++c) {
407 v.Nv[c] = num_traits<T>::from_double(v.d.Nchain[c]);
408 for (std::size_t i = 0; i < M; ++i)
409 if (v.isdelay[i]) v.Zc[c] += v.d.Lchain(i, c);
410 }
411 return v;
412}
413
414
415/**
416 * The families whose bound is a function of the single-chain demand vector
417 * D = V/rates, the think time Z and the population N. A multiclass or open model
418 * simply does not have those, which is why the rule is total.
419 */
420inline bool ba_is_single_class_family(const std::string& fam) {
421 // 'mapamva' is single-class for a different reason from the rest -- its LP
422 // variables QN(i,k)/UN(i,k) are indexed by station and MAP phase, with no
423 // class index at all -- but the premise it fails on is the same one.
424 return fam == "auto" || fam == "aba" || fam == "bjb" || fam == "pb" || fam == "sb" ||
425 fam == "gb" || fam == "harel" || fam == "lr" || fam == "pbh" || fam == "bjbh" ||
426 fam == "cbh" || fam == "ssd" || fam == "sib" || fam == "scb" ||
427 fam == "ldac" || fam == "mapamva" || fam == "qrf" || fam == "kriz" ||
428 fam == "ibjb" || fam == "ldsy";
429}
430
431/**
432 * The multiclass families: they take a per-chain demand MATRIX and a population
433 * VECTOR, so several classes are fine and an infinite population is not.
434 */
435inline bool ba_is_fully_closed_family(const std::string& fam) {
436 return fam == "mw" || fam == "cub" || fam == "mbjb" || fam == "looping";
437}
438
439/**
440 * The single-server families whose alternative on a multiserver model IS 'ssd',
441 * which is why the reason names it. 'ssd' is the multiserver bound itself,
442 * 'ldac' is parameterized by the limiting demand of a load-dependent station,
443 * 'ldsy' (Shanthikumar-Yao) reads the whole rate function
444 * mu(n) = lldscaling(n)*min(n,c), and 'auto' composes whichever candidates
445 * survive, so all four are absent.
446 */
447inline bool ba_is_single_server_family(const std::string& fam) {
448 return fam == "aba" || fam == "bjb" || fam == "pb" || fam == "sb" || fam == "gb" ||
449 fam == "harel" || fam == "lr" || fam == "pbh" || fam == "bjbh" || fam == "cbh" ||
450 fam == "sib" || fam == "scb" || fam == "mapamva" || fam == "kriz" || fam == "ibjb";
451}
452
453/** Whether the open (transaction) ABA/BJB form applies: a named aba/bjb side on a single-class, all-open model. */
454template <class T>
455bool ba_is_open_aba_bjb(const qn::NetworkStruct<T>& L, const std::string& resolved) {
456 if (resolved != "aba.upper" && resolved != "aba.lower" && resolved != "bjb.upper" &&
457 resolved != "bjb.lower")
458 return false;
459 if (L.nclasses != 1 || L.classes.empty()) return false;
460 for (std::size_t r = 0; r < L.classes.size(); ++r)
461 if (std::isfinite(L.classes[r].population)) return false;
462 return true;
463}
464
465/**
466 * The premises of the open ABA/BJB bounds: Source, Sink, Queue, Delay and
467 * Router nodes only, exactly one Source, and, for BJB, Poisson arrivals. ABA
468 * needs no arrival law: R >= D holds for any.
469 * Mirrors `ba_open_aba_bjb_refusal` in `matlab/src/solvers/BA/ba_method_refusal.m`.
470 */
471template <class T>
472std::string ba_open_aba_bjb_refusal(const qn::NetworkStruct<T>& L, const std::string& resolved) {
473 for (std::size_t ind = 0; ind < L.nodes.size(); ++ind) {
474 switch (L.nodes[ind].nodetype) {
480 break;
481 default:
482 return "Method '" + resolved + "' on an open network supports Source, Sink, Queue, "
483 "Delay and Router nodes only; node " + std::to_string(ind + 1) + " is a " +
484 lang::node_type_to_text(L.nodes[ind].nodetype) + ".";
485 }
486 }
487 std::size_t nsrc = 0, src = 0;
488 for (std::size_t i = 0; i < L.nstations; ++i)
489 if (L.stations[i].nodetype == qn::NodeType::Source) {
490 if (nsrc == 0) src = i;
491 ++nsrc;
492 }
493 if (nsrc != 1) return "Method '" + resolved + "' on an open network needs exactly one Source.";
494 if (resolved.compare(0, 3, "bjb") == 0 && L.procid(src + 1, 1) != lang::ProcessType::EXP)
495 return "Method '" + resolved + "' on an open network assumes Poisson arrivals (BJB is derived "
496 "from the product-form residence time D_k/(1-lambda*D_k)); the Source emits " +
497 std::string(lang::process_to_text(L.procid(src + 1, 1))) +
498 " interarrivals. 'aba.lower' holds for any arrival process.";
499 // ba_method_refusal.m then applies its product-form premise to the aba and
500 // bjb families: FCFS needs exponential service. This port has no general
501 // counterpart of that check for the closed forms, so it is applied here, to
502 // the open forms only, which leaves the closed results as they were.
503 for (std::size_t i = 0; i < L.nstations; ++i) {
504 if (L.stations[i].sched != SchedStrategy::FCFS) continue;
505 for (std::size_t r = 0; r < L.nclasses; ++r) {
506 const double mu = num_traits<T>::to_double(L.rates(i, r));
507 if (!std::isfinite(mu) || mu <= 0.0) continue;
508 if (L.procid(i + 1, r + 1) != lang::ProcessType::EXP)
509 return "Method '" + resolved + "' is derived for a product-form network and reads "
510 "the service mean alone: station " + std::to_string(i + 1) + " class " +
511 std::to_string(r + 1) + " serves FCFS with " +
512 std::string(lang::process_to_text(L.procid(i + 1, r + 1))) +
513 " service, so the bound would bracket the exponential network instead. Use "
514 "PS or LCFSPR at that station, exponential service, or the QRF family ('qr', "
515 "'qrf.mmi'), whose chain carries the phase-type law.";
516 }
517 }
518 return "";
519}
520
521/** The arrival rate and queueing demands the open ABA/BJB bounds read, in double. */
522struct BaOpenDemands {
523 double lambda = 0.0; ///< Source rate (0 without a Source)
524 long src = -1; ///< 0-based Source station, -1 without one
525 std::vector<double> V, S, D; ///< per station: visits (0 at the Source), service time, demand
526 std::vector<bool> isSource, isInf, isQ;
527 double Z = 0.0, Dq = 0.0, Dmax = 0.0;
528 std::size_t Kplus = 0;
529};
530
531template <class T>
532BaOpenDemands ba_open_demands(const qn::NetworkStruct<T>& L) {
533 const std::size_t M = L.nstations;
534 BaOpenDemands o;
535 o.V.assign(M, 0.0);
536 o.S.assign(M, 0.0);
537 o.D.assign(M, 0.0);
538 o.isSource.assign(M, false);
539 o.isInf.assign(M, false);
540 o.isQ.assign(M, false);
541 for (std::size_t i = 0; i < M; ++i) {
542 o.isSource[i] = L.stations[i].nodetype == qn::NodeType::Source;
543 if (o.isSource[i] && o.src < 0) o.src = static_cast<long>(i);
544 }
545 if (o.src >= 0) o.lambda = num_traits<T>::to_double(L.rates(static_cast<std::size_t>(o.src), 0));
546 const std::vector<T> Vst = ba_station_visits(L);
547 for (std::size_t i = 0; i < M; ++i) {
548 if (o.isSource[i]) continue;
549 o.V[i] = num_traits<T>::to_double(Vst[i]);
550 const double s = 1.0 / num_traits<T>::to_double(L.rates(i, 0));
551 o.S[i] = std::isfinite(s) ? s : 0.0;
552 o.D[i] = o.V[i] * o.S[i];
553 o.isInf[i] = L.stations[i].sched == SchedStrategy::INF;
554 o.isQ[i] = !o.isInf[i];
555 if (o.isInf[i]) {
556 o.Z += o.D[i];
557 } else {
558 o.Dq += o.D[i];
559 o.Dmax = std::max(o.Dmax, o.D[i]);
560 if (o.D[i] > 0.0) ++o.Kplus;
561 }
562 }
563 return o;
564}
565
566/**
567 * Asymptotic (ABA) and balanced job (BJB) bounds for a single-class OPEN
568 * (transaction) workload, Lazowska et al. (1984), Tables 5.1 and 5.2; the
569 * balanced bounds are Zahorjan et al. (1982).
570 *
571 * throughput X = lambda, exact, for lambda < 1/Dmax
572 * ABA R >= D, and no finite pessimistic bound exists
573 * BJB D/(1-lambda*Davg) <= R <= D/(1-lambda*Dmax)
574 *
575 * Per station T = lambda*V and U = lambda*D are exact on both sides; the
576 * per-visit residence time is 1/mu <= R_k <= (1/mu)/(1-lambda*Dmax) under BJB
577 * and 1/mu <= R_k <= Inf under ABA. Delay stations add their demand Z to both
578 * sides. C is the system response time.
579 *
580 * Mirrors `matlab/src/solvers/BA/ba_open_aba_bjb.m`.
581 */
582template <class T>
583BaSolution<T> ba_open_aba_bjb(const qn::NetworkStruct<T>& L, const std::string& method) {
584 const std::size_t M = L.nstations;
585 const BaOpenDemands o = ba_open_demands(L);
586 if (o.src < 0)
587 throw UnsupportedError(ErrorKind{"ClosedClass"},
588 "ba_open_aba_bjb: Method '" + method + "' on an open network needs a Source station.");
589 const double lambda = o.lambda, Dmax = o.Dmax;
590 if (lambda * Dmax >= 1.0) {
591 char buf[256];
592 std::snprintf(buf, sizeof buf,
593 "(lambda*Dmax = %.4g >= 1, lambda_sat = 1/Dmax = %.4g)", lambda * Dmax, 1.0 / Dmax);
594 throw NumericError(ErrorKind{"Instability"},
595 "ba_open_aba_bjb: Method '" + method + "': the open workload saturates the bottleneck " +
596 buf + ", so no finite response-time bound exists.");
597 }
598 const double inf = std::numeric_limits<double>::infinity();
599 std::vector<double> Tv(M, 0.0), U(M, 0.0), R(M, 0.0), Q(M, 0.0);
600 for (std::size_t i = 0; i < M; ++i) {
601 if (o.isSource[i]) continue;
602 Tv[i] = lambda * o.V[i];
603 if (o.isQ[i]) U[i] = lambda * o.D[i];
604 if (o.isInf[i]) R[i] = o.S[i];
605 }
606 Tv[static_cast<std::size_t>(o.src)] = lambda;
607 double C = 0.0;
608 for (std::size_t i = 0; i < M; ++i) {
609 if (!o.isQ[i]) continue;
610 if (method == "aba.lower" || method == "bjb.lower") R[i] = o.S[i];
611 else if (method == "aba.upper") R[i] = inf;
612 else if (method == "bjb.upper") R[i] = o.S[i] / (1.0 - lambda * Dmax);
613 }
614 if (method == "aba.lower") {
615 C = o.Z + o.Dq;
616 } else if (method == "aba.upper") {
617 C = inf;
618 } else if (method == "bjb.lower") {
619 C = o.Kplus > 0 ? o.Z + o.Dq / (1.0 - lambda * o.Dq / static_cast<double>(o.Kplus)) : o.Z;
620 } else if (method == "bjb.upper") {
621 C = o.Z + o.Dq / (1.0 - lambda * Dmax);
622 } else {
623 throw UnsupportedError(ErrorKind{"Method"},
624 "ba_open_aba_bjb: Method '" + method + "' has no open-network form.");
625 }
626 // a station the class never visits holds nothing, whatever its bound on R
627 for (std::size_t i = 0; i < M; ++i) {
628 Q[i] = Tv[i] == 0.0 ? 0.0 : Tv[i] * R[i];
629 if (Tv[i] == 0.0 && !o.isSource[i]) R[i] = 0.0;
630 if (o.isInf[i]) U[i] = Q[i];
631 }
632 BaSolution<T> s;
633 s.Q = Matrix<T>(M, 1);
634 s.U = Matrix<T>(M, 1);
635 s.R = Matrix<T>(M, 1);
636 s.Tp = Matrix<T>(M, 1);
637 for (std::size_t i = 0; i < M; ++i) {
638 s.Q(i, 0) = num_traits<T>::from_double(Q[i]);
639 s.U(i, 0) = num_traits<T>::from_double(U[i]);
640 s.R(i, 0) = num_traits<T>::from_double(R[i]);
641 s.Tp(i, 0) = num_traits<T>::from_double(Tv[i]);
642 }
643 s.C.assign(1, num_traits<T>::from_double(C));
644 s.X.assign(1, num_traits<T>::from_double(lambda));
645 return s;
646}
647
648} // namespace detail
649
650/**
651 * The STRUCTURAL premises of the SolverBA bound families, in one place: the
652 * reason METHOD cannot bound the model L, or "" when it can.
653 *
654 * ONE PREDICATE, THREE CALLERS. `solver_ba_analyzer` asks it before dispatching
655 * and throws what it returns, `solver_ba_run_analyzer` asks it on the way in, and
656 * `list_valid_methods(L)` asks it so the name never reaches a report at all,
657 * which is the route `auto_family_methods` takes. A second copy of any rule
658 * below is how the report and the run drift apart: the report offers a pair that throws the moment it is run, which is the
659 * defect this function exists to remove.
660 *
661 * WHAT BELONGS HERE AND WHAT DOES NOT. Only the rules the feature registry
662 * cannot name. `qn::Feature` has no entry for "one class", for a server count or
663 * for a station count, so those are structural and live here. Rules of the form
664 * "this family does not accept a delay station" ARE nameable and belong in
665 * `qn::ba_feature_set`, which unsets SchedStrategy_INF for the offending method
666 * instead: a feature set can refuse a model for HAVING a construct, never for
667 * lacking one.
668 *
669 * METHOD is taken as the caller spells it and resolved through
670 * `qn::ba_resolve_method_name`, the copy of `resolve_method` that lives beside the
671 * feature set (this header is below the runner and cannot reach back into it),
672 * so 'default' is judged as the gb.upper it runs as and the reason names that.
673 * The marking-parameterized spnlp and the QRF reduction bounds carry no rule
674 * here: the QRF premise is the reducibility test `list_valid_methods(L)` already
675 * applies. Of the three OPEN families, 'bpt' and 'bgt' carry none either -- a
676 * closed model is refused by their feature set and their analyzers walk the
677 * routing matrix for the rest -- while 'snc' carries one, the SERVICE law.
678 *
679 * WHY THE SNC SERVICE LAW IS HERE AND THE bpt/bgt ONE IS NOT. All three
680 * analyzers refuse a non-exponential law at a queueing station. For bpt and bgt
681 * that rule extends to the SOURCE and is registry-expressible, so it rides in
682 * `qn::ba_feature_set` as a dropped law: both are invariant to the arrival law
683 * beyond its mean, so a non-exponential source is not something they refuse, it
684 * is something they silently bound as if it were Poisson. snc is the opposite:
685 * it CONSUMES the arrival law and its analyzer branches on a non-exponential
686 * source deliberately. Its rule is about the SERVICE only, and no feature name
687 * can say "Erlang at a Queue but not at a Source", so it is structural.
688 *
689 * Mirrors `matlab/src/solvers/BA/ba_method_refusal.m` and its JAR and native
690 * python twins.
691 */
692template <class T>
693std::string method_refusal(const qn::NetworkStruct<T>& L, const std::string& method) {
694 const std::string resolved = qn::ba_resolve_method_name(method);
695 const std::string fam = qn::ba_family_of(resolved);
696 bool anyOpen = false;
697 for (std::size_t r = 0; r < L.classes.size(); ++r)
698 if (!std::isfinite(L.classes[r].population)) anyOpen = true;
699 const bool anyClosed = L.nclosedjobs() > 0.0;
700 bool allOpen = !L.classes.empty();
701 for (std::size_t r = 0; r < L.classes.size(); ++r)
702 if (std::isfinite(L.classes[r].population)) allOpen = false;
703 if ((fam == "aba" || fam == "bjb") && L.nclasses == 1 && allOpen) {
704 // the open (transaction) forms of Lazowska et al. (1984), Tables 5.1-5.2
705 const std::string reason = detail::ba_open_aba_bjb_refusal(L, resolved);
706 if (!reason.empty()) return reason;
707 } else if (fam == "aba" || fam == "bjb") {
708 if (L.nclasses != 1 || !anyClosed)
709 return "Method '" + resolved + "' supports single-class networks only, either closed or open.";
710 } else if (detail::ba_is_single_class_family(fam)) {
711 if (L.nclasses != 1 || !anyClosed)
712 return "Method '" + resolved + "' supports single-class closed networks only.";
713 } else if (detail::ba_is_fully_closed_family(fam)) {
714 if (!anyClosed || anyOpen)
715 return "Method '" + resolved + "' supports fully closed networks only.";
716 }
717 bool multiserver = false;
718 for (std::size_t i = 0; i < L.stations.size() && !multiserver; ++i) {
719 if (L.stations[i].sched == SchedStrategy::INF) continue;
720 if (L.stations[i].nservers > 1.0) multiserver = true;
721 }
722 if (multiserver) {
723 if (detail::ba_is_single_server_family(fam))
724 return "Method '" + resolved +
725 "' does not support multi-server stations (use 'ssd').";
726 if (detail::ba_is_fully_closed_family(fam))
727 return "Method '" + resolved + "' does not support multi-server stations.";
728 }
729
730 // The SNC service law. Judged over the pairs a station COULD serve rather
731 // than over the ones that carry traffic: the analyzer restricts to the
732 // latter, which needs the traffic equations solved, and this is their
733 // conservative outer approximation -- it never admits a pair the analyzer
734 // refuses, and can only differ on a pair given a service time at a station
735 // its class never visits. A Source is skipped, which is the whole reason
736 // this is not a feature-set delta.
737 if (fam == "snc") {
738 for (std::size_t i = 0; i < L.nstations; ++i) {
739 if (L.stations[i].nodetype == qn::NodeType::Source) continue;
740 for (std::size_t r = 0; r < L.nclasses; ++r) {
741 const double mu = num_traits<T>::to_double(L.rates(i, r));
742 if (!std::isfinite(mu) || mu <= 0.0) continue;
743 if (L.procid(i + 1, r + 1) != lang::ProcessType::EXP)
744 return "Method '" + resolved + "' requires exponential service: station " +
745 std::to_string(i + 1) + " class " + std::to_string(r + 1) +
746 " is not exponential.";
747 }
748 }
749 }
750 return "";
751}
752
753/**
754 * Whether METHOD APPLIES to L but its bound carries no information there, and
755 * why. Empty when the bound is informative, and empty for every method that has
756 * no such regime.
757 *
758 * THIS IS A DIFFERENT QUESTION FROM `method_refusal`, which is why it is a
759 * different function. That one answers "is this model outside the method's
760 * domain", and its answer is what the analyzer throws. This one answers "inside
761 * the domain, does the formula still say anything", and its answer is NOT
762 * thrown: a degenerate bound is a VALID bound, just a vacuous one, so an
763 * analyzer asked for it by name is entitled to publish it -- which is also what
764 * `tests/test_ba.cpp` pins at the regime boundary. What must not happen is
765 * OFFERING it: `list_valid_methods(L)` names the pairs a caller can act on, and
766 * a table of zeros over a network with jobs circulating in it is not one.
767 *
768 * THE ONE METHOD WITH SUCH A REGIME IS 'ldac.lower'. The Anselmi-Cremonesi
769 * bound is built from the population SURPLUS a = N - Qhat, where Qhat is the
770 * occupancy the non-bottleneck stations and the think time would hold in the
771 * open network fed at the bottleneck's saturation rate. `pfqn_ldbcmp` reports
772 * `applicable = false` below the regime, which the analyzer already throws on;
773 * AT the boundary a = 0 it returns Xlo = 0, which is formally the trivial bound
774 * X >= 0 and propagates into a table whose queue lengths, utilizations and
775 * throughputs are all zero. Every entry of that table is a true lower bound and
776 * none of them is usable, and a caller cannot tell it from a real answer of
777 * zero.
778 *
779 * Mirrors `matlab/src/solvers/BA/ba_method_degenerate.m` and its JAR and native
780 * python twins.
781 */
782template <class T>
783std::string method_degenerate(const qn::NetworkStruct<T>& L, const std::string& method) {
784 const std::string resolvedMethod = qn::ba_resolve_method_name(method);
785 if (detail::ba_is_open_aba_bjb(L, resolvedMethod)) {
786 // An open model past saturation has no finite response-time bound, and
787 // the open ABA upper side has none at any load (Lazowska et al. 1984,
788 // Table 5.1): both run when named and are kept out of the listing.
789 if (!method_refusal(L, method).empty()) return "";
790 if (resolvedMethod == "aba.upper")
791 return "Method 'aba.upper' on an open network is vacuous: asymptotic bounds give no "
792 "pessimistic response time for a transaction workload, so every queue length and "
793 "response time it reports is infinite. Use 'bjb.upper'.";
794 const detail::BaOpenDemands od = detail::ba_open_demands(L);
795 if (od.Dmax > 0.0 && od.lambda * od.Dmax >= 1.0) {
796 char buf[64];
797 std::snprintf(buf, sizeof buf, "%.4g", od.lambda * od.Dmax);
798 return "Method '" + resolvedMethod + "' does not apply here: the open workload saturates the "
799 "bottleneck (lambda*Dmax = " + buf + " >= 1), so there is no finite bound.";
800 }
801 return "";
802 }
803 if (resolvedMethod != "ldac.lower") return "";
804 // The applicability rules come first and are not restated: a model this
805 // method is outside the domain of has no bound to be degenerate about.
806 if (!method_refusal(L, method).empty()) return "";
807
808 const detail::ScDemands<T> sc = detail::ba_sc_demands(L);
809 // NO QUEUEING STATION, NO BOUND. Every station is a delay (or a Place, on a
810 // Petri net, which is an INF station too), so there is no bottleneck to
811 // build Qhat on and `pfqn_ldbcmp` refuses an empty demand vector. A
812 // PREDICATE MUST NOT THROW -- this one is asked once per name by
813 // `list_valid_methods`, before the Petri sieve has had a chance to drop
814 // anything -- so the case is answered rather than propagated.
815 if (sc.D.empty())
816 return "Method 'ldac.lower' has no queueing station to bound here: every station is "
817 "an infinite server, so the bottleneck the open-network occupancy is built on "
818 "does not exist.";
819 const T zero = num_traits<T>::from_int(0);
820 const std::vector<T> cz(sc.D.size(), zero);
822 sc.D, num_traits<T>::from_int(static_cast<int>(sc.N)), sc.Z, cz,
824 if (!b.applicable)
825 return "Method 'ldac.lower' does not apply here: the population is below the "
826 "open-network occupancy Qhat the bound is built from.";
827 const double xlo = num_traits<T>::to_double(b.Xlo);
828 if (!std::isfinite(xlo) || xlo <= 0.0) {
829 std::ostringstream os;
830 os << "Method 'ldac.lower' needs a population strictly above the open-network "
831 "occupancy the bound is built from (Qhat="
832 << num_traits<T>::to_double(b.Qhat) << ", N=" << sc.N
833 << "): with no surplus it degenerates to the trivial bound X >= 0 and reports a "
834 "table of zeros.";
835 return os.str();
836 }
837 return "";
838}
839
840/**
841 * Port of `solver_ba_analyzer`.
842 *
843 * @param L the refreshed struct of a closed model
844 * @param opt the method and, for a hierarchical family, the level
845 * @return the [Q,U,R,T,C,X] of the requested bound
846 */
847template <class T>
849 const std::string& method = opt.method;
850 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
851
852 // The STRUCTURAL premises of every family -- single-class closed, fully
853 // closed, single-server -- are asked here and nowhere else, so this run and
854 // the report give one answer whichever a caller meets first. Asking once at
855 // the top is what replaced the per-branch copies: an applicability test
856 // inlined in each family is how the gate and the run drift apart.
857 //
858 // IT ALSO RETIRED `ba_out_of_domain`. The five noniterative single-class
859 // families and `mw` used to answer a model outside their CLASS domain
860 // with zeroed metrics and an empty C and X, reproducing a reference whose
861 // own branches were wrapped in an applicability test with no else. That was
862 // a deliberate alignment (user ruling 2026-07-25) and it is OVERTURNED
863 // (user ruling 2026-09-04): the reference refuses by name since
864 // `ba_method_refusal.m` landed, and a table a caller cannot tell from a
865 // real answer of zero is the defect class this gate exists to remove.
866 {
867 const std::string refusal = method_refusal(L, method);
868 if (!refusal.empty()) throw UnsupportedError(ErrorKind{"Structure"}, "solver_ba_analyzer: " + refusal);
869 }
870 // The Petri-net split, which list_valid_methods applies as a sieve: no family but spnlp has a
871 // representation of a marking. Asked here rather than in method_refusal so method_degenerate
872 // still reads a net's ldac regime.
873 if (method.compare(0, 5, "spnlp") != 0) {
874 for (std::size_t i = 0; i < L.nodes.size(); ++i)
875 if (L.nodes[i].nodetype == lang::NodeType::Transition)
876 throw UnsupportedError(ErrorKind{"Structure"},
877 "solver_ba_analyzer: Method '" + method + "' is parameterized by demands and a "
878 "population, which the marking of a Petri net is not. Use "
879 "'spnlp2.upper'/'spnlp2.lower' or the 'spnlp1.upper'/'spnlp1.lower' pair on this model.");
880 }
881
882 // The open (transaction) forms of ABA and BJB, Lazowska et al. (1984)
883 // Tables 5.1-5.2: same method names, a single-class open network.
884 if ((method == "aba.upper" || method == "aba.lower" || method == "bjb.upper" ||
885 method == "bjb.lower") && detail::ba_is_open_aba_bjb(L, method))
886 return detail::ba_open_aba_bjb(L, method);
887
888 // AUTO composite: evaluate every noniterative bound and keep the tightest
889 // side. Feasibility is probed by execution -- a candidate that rejects the
890 // model (multiserver, delay station, regime gate) throws and is skipped --
891 // so the list stays correct as families are added.
892 if (method == "auto.upper" || method == "auto.lower") {
893 const bool up = method == "auto.upper";
894 const detail::ScDemands<T> sc = detail::ba_sc_demands(L);
895 static const char* const cand_up[] = {"aba.upper", "bjb.upper", "pb.upper", "gb.upper",
896 "sb.upper", "mw.upper", "ssd.upper", "cub.upper"};
897 static const char* const cand_lo[] = {"aba.lower", "bjb.lower", "pb.lower",
898 "gb.lower", "sb.lower", "mw.lower",
899 "ssd.lower", "mbjb.lower", "ldac.lower"};
900 const char* const* cand = up ? cand_up : cand_lo;
901 const std::size_t ncand = up ? sizeof(cand_up) / sizeof(*cand_up)
902 : sizeof(cand_lo) / sizeof(*cand_lo);
903 bool have = false;
904 T Xbest = zero;
905 for (std::size_t ci = 0; ci < ncand; ++ci) {
906 BaOptions oc = opt;
907 oc.method = cand[ci];
909 try {
910 r = solver_ba_analyzer(L, oc);
911 } catch (const std::exception&) {
912 continue;
913 }
914 if (r.X.empty()) continue;
915 const T Xc = r.X[0];
916 if (!std::isfinite(num_traits<T>::to_double(Xc)) || !(Xc > zero)) continue;
917 if (!have || (up && Xc < Xbest) || (!up && Xc > Xbest)) {
918 Xbest = Xc;
919 have = true;
920 }
921 }
922 if (!have)
923 throw UnsupportedError(ErrorKind{"Infeasible"}, "solver_ba_analyzer: method '" + method +
924 "' found no feasible bound for this model");
925 return detail::ba_fill(L, sc.V, sc.N, Xbest, sc.Z, sc.D, up);
926 }
927
928 // The noniterative single-class families (aba, bjb, pb, sb, gb) share the
929 // demand extraction; the reference inlines it in every branch.
930 const bool single_class_family =
931 method == "aba.upper" || method == "aba.lower" || method == "bjb.upper" ||
932 method == "bjb.lower" || method == "pb.upper" || method == "pb.lower" ||
933 method == "sb.upper" || method == "sb.lower" || method == "gb.upper" ||
934 method == "gb.lower";
935
936 if (single_class_family) {
937 const detail::ScDemands<T> sc = detail::ba_sc_demands(L);
938 const std::vector<T>& V = sc.V;
939 const std::vector<T>& D = sc.D;
940 const T& Z = sc.Z;
941 const long N = sc.N;
942 const T Nt = num_traits<T>::from_int(N);
943 const detail::DemandStats<T> st = detail::ba_demand_stats(D);
944
945 if (method == "aba.upper") {
946 const T X = detail::ba_min(T(one / st.max), T(Nt / T(Z + st.sum)));
947 return detail::ba_fill_xc(L, V, N, X, T(Z + Nt * st.sum), true);
948 }
949 if (method == "aba.lower") {
950 const T X = T(Nt / T(Z + Nt * st.sum));
951 return detail::ba_fill_xc(L, V, N, X, T(Z + st.sum), false);
952 }
953 // The balanced and proportional families evaluate the ABA bracket at
954 // N-1: both are one exact MVA step taken from a bounded arrival-theorem
955 // queue length, so the population they see is the one a job arriving to
956 // the system leaves behind.
957 const T Nm1 = num_traits<T>::from_int(N - 1);
958 const T xup1 = detail::ba_min(T(one / st.max), T(Nm1 / T(Z + st.sum)));
959 const T xlo1 = T(Nm1 / T(Z + Nm1 * st.sum));
960 if (method == "bjb.upper") {
961 const T C = T(Z + st.sum + st.max * T(Nm1 - Z * xlo1));
962 const T X = detail::ba_min(
963 T(one / st.max), T(Nt / T(Z + st.sum + st.mean * T(Nm1 - Z * xup1))));
964 return detail::ba_fill_xc(L, V, N, X, C, true);
965 }
966 if (method == "bjb.lower") {
967 const T C = T(Z + st.sum + st.mean * T(Nm1 - Z * xup1));
968 const T X = T(Nt / T(Z + st.sum + st.max * T(Nm1 - Z * xlo1)));
969 return detail::ba_fill_xc(L, V, N, X, C, false);
970 }
971 if (method == "pb.upper" || method == "pb.lower") {
972 T d2 = zero, dN = zero, dNm1 = zero;
973 for (const T& d : D) {
974 d2 += T(d * d);
975 dN += num_pow_int(d, static_cast<unsigned>(N));
976 dNm1 += num_pow_int(d, static_cast<unsigned>(N - 1));
977 }
978 const T Dpb2 = T(d2 / st.sum);
979 const T DpbN = T(dN / dNm1);
980 if (method == "pb.upper") {
981 const T C = T(Z + st.sum + DpbN * T(Nm1 - Z * xlo1));
982 const T X = detail::ba_min(
983 T(one / st.max), T(Nt / T(Z + st.sum + Dpb2 * T(Nm1 - Z * xup1))));
984 return detail::ba_fill_xc(L, V, N, X, C, true);
985 }
986 const T C = T(Z + st.sum + Dpb2 * T(Nm1 - Z * xup1));
987 const T X = T(Nt / T(Z + st.sum + DpbN * T(Nm1 - Z * xlo1)));
988 return detail::ba_fill_xc(L, V, N, X, C, false);
989 }
990 if (method == "sb.upper" || method == "sb.lower") {
991 // The power sums of Harel-Namn-Sturm are written for a network with
992 // no terminal population, and the reference refuses a delay station
993 // rather than drop its think time.
994 for (std::size_t i = 0; i < L.nstations; ++i)
995 if (detail::ba_is_delay(L, i))
996 throw UnsupportedError(ErrorKind{"DelayStation"}, "solver_ba_analyzer: method '" + method +
997 "' does not support infinite-server stations");
998 T A1 = zero, A2 = zero, A3 = zero;
999 for (const T& d : D) {
1000 A1 += d;
1001 A2 += T(d * d);
1002 A3 += T(d * d * d);
1003 }
1004 if (method == "sb.upper") {
1005 const T C = T(Z + A1 + Nm1 * T(T(A1 * A2 + A3) / T(A1 * A1 + A2)));
1006 const T X = detail::ba_min(T(one / st.max), T(Nt / C));
1007 // The power-sum family leaves the per-station response time
1008 // undefined, and the reference pairs BOTH sides with the
1009 // no-contention residence rather than only the lower one.
1010 return detail::ba_fill_xc(L, V, N, X, C, false);
1011 }
1012 // The (N-1)-st root of the N-th power sum has no field expression.
1013 if constexpr (!num_traits<T>::has_transcendental) {
1014 throw UnsupportedError(ErrorKind{"Arithmetic"},
1015 "solver_ba_analyzer: method 'sb.lower' needs an (N-1)-st root and is "
1016 "unavailable in exact arithmetic");
1017 } else {
1018 using std::pow;
1019 T AN = zero;
1020 for (const T& d : D) AN += num_pow_int(d, static_cast<unsigned>(N));
1021 // pow is taken on T, not through double: a high-precision
1022 // backend would otherwise lose every digit past the 17th here.
1023 const T root = pow(T(AN / A1), T(one / Nm1));
1024 const T C = T(Z + A1 + Nm1 * root);
1025 return detail::ba_fill_xc(L, V, N, T(Nt / C), C, false);
1026 }
1027 }
1028 // gb: the geometric bounds solve a quadratic in the throughput.
1029 if constexpr (!num_traits<T>::has_transcendental) {
1030 throw UnsupportedError(ErrorKind{"Arithmetic"}, "solver_ba_analyzer: the geometric bounds '" + method +
1031 "' solve a quadratic and are unavailable in exact arithmetic");
1032 } else {
1033 const bool up = (method == "gb.upper");
1034 const T Xup = pfqn::pfqn_xzgsbup(D, Nt, Z);
1035 const T Xlo = pfqn::pfqn_xzgsblow(D, Nt, Z);
1036 const T X = up ? detail::ba_min(T(one / st.max), Xup) : Xlo;
1037 const T C = T(Nt / (up ? Xlo : Xup));
1038 // The queue-length bound is the geometric one, not the residence
1039 // convention of ba_fill; the response time follows from it by the
1040 // OPPOSITE side of the throughput bracket, which is what keeps
1041 // Q = X R consistent with a bound rather than with a point estimate.
1042 const T Xden = up ? Xlo : Xup;
1043 BaSolution<T> s;
1044 s.Q = Matrix<T>(L.nstations, 1, zero);
1045 s.U = Matrix<T>(L.nstations, 1, zero);
1046 s.R = Matrix<T>(L.nstations, 1, zero);
1047 s.Tp = Matrix<T>(L.nstations, 1, zero);
1048 std::size_t k = 0;
1049 for (std::size_t i = 0; i < L.nstations; ++i) {
1050 s.Tp(i, 0) = T(V[i] * X);
1051 if (detail::ba_is_delay(L, i)) {
1052 s.R(i, 0) = T(one / L.rates(i, 0));
1053 s.Q(i, 0) = T(X * s.R(i, 0));
1054 } else {
1055 s.Q(i, 0) = up ? pfqn::pfqn_qzgbup(D, Nt, Z, k) : pfqn::pfqn_qzgblow(D, Nt, Z, k);
1056 s.R(i, 0) = T(T(s.Q(i, 0) / Xden) / V[i]);
1057 ++k;
1058 }
1059 s.U(i, 0) = detail::ba_is_delay(L, i) ? s.Q(i, 0) : T(s.Tp(i, 0) / L.rates(i, 0));
1060 }
1061 s.C.assign(1, C);
1062 s.X.assign(1, X);
1063 s.lG = -static_cast<double>(N) * num_traits<T>::log_as_double(X);
1064 return s;
1065 }
1066 }
1067
1068 if (method == "lr.upper" || method == "lr.lower") {
1069 // LP linear-reduction bound: one LP per station, each minimizing or
1070 // maximizing that station's utilization over a polytope that CONTAINS
1071 // the exact stationary solution.
1072 const detail::ScDemands<T> sc = detail::ba_sc_demands(L);
1073 for (std::size_t i = 0; i < L.nstations; ++i)
1074 if (detail::ba_is_delay(L, i))
1075 throw UnsupportedError(ErrorKind{"DelayStation"}, "solver_ba_analyzer: method '" + method +
1076 "' does not support delay (infinite-server) stations");
1077 const bool up = (method == "lr.upper");
1078 const std::size_t M = L.nstations;
1079 const std::vector<T>& V = sc.V;
1080
1082 par.M = static_cast<int>(M);
1083 // MATLAB reads sn.njobs(1), the class population, which for the
1084 // single-class model this branch admits is nclosedjobs.
1085 par.N = static_cast<int>(sc.N);
1086 par.mu.resize(M);
1087 T totV = zero;
1088 for (std::size_t i = 0; i < M; ++i) {
1089 par.mu[i] = L.rates(i, 0);
1090 totV += V[i];
1091 }
1092 // Every row of r is the visit vector normalized by its total: the
1093 // reference builds it with repmat, so a job leaves any station for j
1094 // with the same probability V_j / sum V.
1095 par.r = Matrix<T>(M, M, zero);
1096 if (totV == zero) throw NumericError(ErrorKind{"Degenerate"}, "solver_ba_analyzer: the visit ratios are all zero");
1097 for (std::size_t i = 0; i < M; ++i)
1098 for (std::size_t j = 0; j < M; ++j) par.r(i, j) = T(V[j] / totV);
1099
1101 std::vector<T> U(M, zero);
1102 for (std::size_t ti = 0; ti < M; ++ti) {
1103 const mapqn::LrPfResult<T> r =
1104 mapqn::mapqn_bnd_lr_pf(par, static_cast<int>(ti) + 1, sense);
1105 if (!r.ok)
1106 throw NumericError(ErrorKind{"NonConvergence"}, "solver_ba_analyzer: the '" + method +
1107 "' linear program did not solve (" + r.status + ")");
1108 U[ti] = r.objective;
1109 }
1110 // The chain throughput implied by the bounded utilizations, U_i = X V_i / mu_i.
1111 // MATLAB drops the non-finite candidates (a station with no visit) and
1112 // takes the minimum, or zero when none survives.
1113 bool any = false;
1114 T X = zero;
1115 for (std::size_t i = 0; i < M; ++i) {
1116 if (V[i] == zero) continue; // MATLAB: the candidate is Inf and isfinite drops it
1117 const T cand = T(U[i] * L.rates(i, 0) / V[i]);
1118 if (!any || cand < X) {
1119 X = cand;
1120 any = true;
1121 }
1122 }
1123 if (!any) X = zero;
1124 // Q, R, T and C follow the ABA residence convention of ba_fill at this
1125 // side, with no think time since a delay station is refused above; only
1126 // U comes from the LP rather than from the utilization law.
1127 BaSolution<T> s = detail::ba_fill(L, V, sc.N, X, zero, sc.D, up);
1128 for (std::size_t i = 0; i < M; ++i) s.U(i, 0) = U[i];
1129 s.lG = std::numeric_limits<double>::quiet_NaN(); // the reference leaves it unset
1130 return s;
1131 }
1132
1133 if (method == "mapamva.upper" || method == "mapamva.lower") {
1134 // MAP-AMVA (Casale-Smirni, DSN 2009): the LP over the EXACT mean-value
1135 // balance equations of a closed MAP queueing network. Like the QRF
1136 // family, it consumes the CORRELATION between successive services
1137 // rather than the service mean alone -- its variables are the per-phase
1138 // queue lengths QN(i,k) and utilizations UN(i,k), so a workload whose
1139 // burstiness moves the bottleneck between stations is bounded rather
1140 // than averaged into a renewal process. That is why 'MAP' and 'MMPP2'
1141 // reach this family's feature set (and the QRF and snc ones, which also
1142 // read the (D0,D1) law).
1143 //
1144 // The LP carries phases at ONE queue and requires it to be the LAST --
1145 // q(i,j,k,h) reads the scalar muM(i) for i < M and the (D0,D1) pair
1146 // muMAP/v for i == M -- so a model whose phase-carrying station sits
1147 // elsewhere is PERMUTED rather than refused, and the results are
1148 // permuted back before they are returned.
1149 const detail::ScDemands<T> sc = detail::ba_sc_demands(L);
1150 for (std::size_t i = 0; i < L.nstations; ++i)
1151 if (detail::ba_is_delay(L, i))
1152 throw UnsupportedError(ErrorKind{"DelayStation"},
1153 "solver_ba_analyzer: method '" + method +
1154 "' does not support delay (infinite-server) stations: the MAP-AMVA program "
1155 "of Casale-Smirni (DSN 2009) is written for a network of queues and the "
1156 "paper names the delay extension as open work. Use a QRF method, which "
1157 "carries the load-dependent rate law");
1158 const bool up = (method == "mapamva.upper");
1159 const std::size_t M = L.nstations;
1160 const std::vector<T>& V = sc.V;
1161
1162 // Phase order per station. One phase is an exponential server, which
1163 // enters the LP as the scalar rate muM(i); more than one is the (D0,D1)
1164 // pair, which only queue M can hold.
1165 std::vector<std::pair<Matrix<T>, Matrix<T>>> MAPs(M);
1166 std::vector<int> kph(M, 1);
1167 for (std::size_t i = 0; i < M; ++i) {
1168 const mam::Map<T> m = lang::dist_to_map(L.service[i][0]);
1169 if (m.D0.rows() == 0) {
1170 MAPs[i] = std::make_pair(Matrix<T>(1, 1, num_traits<T>::from_int(-1)),
1172 kph[i] = 1;
1173 } else {
1174 MAPs[i] = std::make_pair(m.D0, m.D1);
1175 kph[i] = static_cast<int>(m.D0.rows());
1176 }
1177 }
1178 std::vector<std::size_t> phased;
1179 for (std::size_t i = 0; i < M; ++i)
1180 if (kph[i] > 1) phased.push_back(i);
1181 if (phased.size() > 1) {
1182 std::string names;
1183 for (std::size_t j = 0; j < phased.size(); ++j)
1184 names += (j ? ", " : "") + std::to_string(phased[j] + 1);
1185 throw UnsupportedError(ErrorKind{"NonExponential"},
1186 "solver_ba_analyzer: method '" + method +
1187 "' carries phases at ONE station: the LP gives queue M the (D0,D1) pair and "
1188 "every other queue a scalar rate. Stations " + names + " are all "
1189 "non-exponential. Use a QRF method, whose q carries a phase at every station");
1190 }
1191 // Every station exponential: the program is still the right one, it just
1192 // degenerates to K = 1, where the per-phase variables collapse and the
1193 // balances become the product-form ones of mapqn_bnd_lr_pf.
1194 const std::size_t map_idx = phased.empty() ? M - 1 : phased.front();
1195 std::vector<std::size_t> perm;
1196 perm.reserve(M);
1197 for (std::size_t i = 0; i < M; ++i)
1198 if (i != map_idx) perm.push_back(i);
1199 perm.push_back(map_idx);
1200 const int K = kph[map_idx];
1201 const std::size_t Ks = static_cast<std::size_t>(K);
1202
1204 par.M = static_cast<int>(M);
1205 par.N = static_cast<int>(sc.N);
1206 par.K = K;
1207 par.muM.resize(M - 1);
1208 for (std::size_t a = 0; a + 1 < M; ++a) par.muM[a] = L.rates(perm[a], 0);
1209 // muMAP(k,h) is the completion rate out of phase k landing in phase h,
1210 // i.e. D1(k,h); v(k,h) is the background phase change that completes no
1211 // job, i.e. D0 off the diagonal. Same (from,to) convention as
1212 // qrf_extract_mu_v -- writing either as its transpose is invisible for a
1213 // reversible D0 and silently reverses the phase order of an Erlang.
1214 par.muMAP = Matrix<T>(Ks, Ks, zero);
1215 par.v = Matrix<T>(Ks, Ks, zero);
1216 for (std::size_t a = 0; a < Ks; ++a)
1217 for (std::size_t b = 0; b < Ks; ++b) {
1218 par.muMAP(a, b) = MAPs[map_idx].second(a, b);
1219 par.v(a, b) = (a == b) ? zero : MAPs[map_idx].first(a, b);
1220 }
1221 par.r = Matrix<T>(M, M, zero);
1222 for (std::size_t a = 0; a < M; ++a)
1223 for (std::size_t b = 0; b < M; ++b) par.r(a, b) = L.rt(perm[a], perm[b]);
1224
1225 std::vector<T> Vp(M, zero), Sp(M, zero);
1226 for (std::size_t a = 0; a < M; ++a) {
1227 const std::size_t i = perm[a];
1228 Vp[a] = V[i];
1229 mam::Map<T> mi;
1230 mi.D0 = MAPs[i].first;
1231 mi.D1 = MAPs[i].second;
1232 Sp[a] = mam::map_mean(mi);
1233 }
1234
1235 // THREE SWEEPS OF THE SAME LP, and the utilization one runs in BOTH
1236 // senses on purpose. R_i = Q_i/(V_i*X) rises with Q_i and FALLS with X,
1237 // so an upper bound on the response time pairs Q_i^max with X^min;
1238 // dividing by X^max on both sides is what would report an upper R below
1239 // the exact value and break the bracket.
1241 std::vector<T> umax(M, zero), umin(M, zero), qbnd(M, zero);
1242 for (std::size_t a = 0; a < M; ++a) {
1243 const int ti = static_cast<int>(a);
1252 if (!ru.ok || !rl.ok || !rq.ok)
1253 throw NumericError(ErrorKind{"NonConvergence"}, "solver_ba_analyzer: the '" + method +
1254 "' linear program did not solve (" +
1255 (ru.ok ? (rl.ok ? rq.status : rl.status) : ru.status) + ")");
1256 umax[a] = ru.objective;
1257 umin[a] = rl.objective;
1258 qbnd[a] = rq.objective;
1259 }
1260
1261 // Utilization law U_i = X*V_i*S_i, exact at a single server under ANY
1262 // service law, so each station turns its own utilization bound into a
1263 // throughput bound and the tightest of the M survives. A station with no
1264 // visits or no service time carries no information and is skipped.
1265 bool any = false;
1266 T x_up = zero, x_lo = zero;
1267 for (std::size_t a = 0; a < M; ++a) {
1268 const T load = T(Vp[a] * Sp[a]);
1269 if (load <= zero) continue;
1270 const T cu = T(umax[a] / load), cl = T(umin[a] / load);
1271 if (!any) {
1272 x_up = cu;
1273 x_lo = cl;
1274 any = true;
1275 } else {
1276 if (cu < x_up) x_up = cu;
1277 if (cl > x_lo) x_lo = cl;
1278 }
1279 }
1280 if (!any)
1281 throw NumericError(ErrorKind{"Degenerate"}, "solver_ba_analyzer: method '" + method +
1282 "' found no station with both a positive visit ratio and a "
1283 "positive mean service time");
1284 const T xb = up ? x_up : x_lo;
1285 const T xopp = up ? x_lo : x_up;
1286
1287 // Unpermute: the LP orders the stations with the phase-carrying one last.
1288 BaSolution<T> s;
1289 s.Q = Matrix<T>(M, 1, zero);
1290 s.U = Matrix<T>(M, 1, zero);
1291 s.R = Matrix<T>(M, 1, zero);
1292 s.Tp = Matrix<T>(M, 1, zero);
1293 for (std::size_t a = 0; a < M; ++a) {
1294 const std::size_t i = perm[a];
1295 s.U(i, 0) = up ? umax[a] : umin[a];
1296 s.Q(i, 0) = qbnd[a];
1297 s.Tp(i, 0) = T(Vp[a] * xb);
1298 s.R(i, 0) = (xopp > zero && Vp[a] > zero) ? T(qbnd[a] / (Vp[a] * xopp)) : zero;
1299 }
1300 // Delay stations are refused above, so the closed-network response time
1301 // is N/X exactly and the throughput bracket transfers to it directly.
1302 // Summing the per-station R bounds instead would add M separately
1303 // attained maxima and report a looser number.
1304 s.C.assign(1, xopp > zero
1305 ? T(num_traits<T>::from_int(static_cast<long>(sc.N)) / xopp)
1306 : zero);
1307 s.X.assign(1, xb);
1308 s.lG = -static_cast<double>(sc.N) * num_traits<T>::log_as_double(xb);
1309 return s;
1310 }
1311
1312 if (method == "mw.upper" || method == "mw.lower") {
1313 // Majumdar-Woodside robust box bounds: multiclass, and the only family
1314 // that reads the scheduling discipline, since its lower bound is a
1315 // per-discipline residence guarantee.
1316 const detail::ChainView<T> v = detail::ba_chain_view(L);
1317 const std::size_t M = L.nstations, C = L.nchains, Kq = v.qstat.size();
1318 Matrix<T> Vq(Kq, C, zero), Sq(Kq, C, zero);
1319 std::vector<pfqn::MwrbbSched> schedq(Kq);
1320 for (std::size_t k = 0; k < Kq; ++k) {
1321 const std::size_t i = v.qstat[k];
1322 schedq[k] = detail::mwrbb_disc_code(L.stations[i].sched);
1323 for (std::size_t c = 0; c < C; ++c) {
1324 Vq(k, c) = v.d.Vchain(i, c);
1325 Sq(k, c) = v.d.STchain(i, c);
1326 }
1327 }
1328 // Chain priority: the reference class's when the chain has one, else the
1329 // highest priority (lowest value) any class of the chain carries.
1330 std::vector<int> prioc(C, 0);
1331 for (std::size_t c = 0; c < C; ++c) {
1332 if (L.refclass[c] > 0) {
1333 prioc[c] = L.classes[L.refclass[c] - 1].prio;
1334 } else {
1335 int p = L.classes[L.inchain[c][0] - 1].prio;
1336 for (std::size_t r : L.inchain[c]) p = std::min(p, L.classes[r - 1].prio);
1337 prioc[c] = p;
1338 }
1339 }
1340 const pfqn::MwrbbBounds<T> b = pfqn::pfqn_mwrbb(Vq, Sq, v.Nv, v.Zc, schedq, prioc);
1341 const bool up = (method == "mw.upper");
1342 const std::vector<T>& Xchain = up ? b.Xup : b.Xlo;
1343 Matrix<T> Tchain(M, C, zero), Uchain(M, C, zero), Qchain(M, C, zero);
1344 for (std::size_t c = 0; c < C; ++c)
1345 for (std::size_t i = 0; i < M; ++i) {
1346 Tchain(i, c) = T(Xchain[c] * v.d.Vchain(i, c));
1347 Uchain(i, c) = T(Xchain[c] * v.d.Lchain(i, c)); // utilization law
1348 }
1349 Qchain = detail::ba_chain_qfill(Uchain, Xchain, v.d.Lchain, v.Nv, v.isdelay, up);
1351 L, v.d, Qchain, Uchain, Matrix<T>(), Tchain, Xchain);
1352 BaSolution<T> s;
1353 s.Q = cr.Q;
1354 s.U = cr.U;
1355 s.R = cr.R;
1356 s.Tp = cr.Tp;
1357 s.C = cr.C;
1358 s.X = cr.X;
1359 s.lG = std::numeric_limits<double>::quiet_NaN();
1360 return s;
1361 }
1362
1363 // Eager Looping: the multiclass bracket that initializes the multiple-class
1364 // PBH. Pessimistic side from the heap-inflated response time, optimistic
1365 // side from the response-time lower bound.
1366 if (method == "looping.upper" || method == "looping.lower") {
1367 const detail::ChainView<T> v = detail::ba_chain_view(L);
1368 const std::size_t M = L.nstations, C = L.nchains, Kq = v.qstat.size();
1369 Matrix<T> Lq(Kq, C, zero);
1370 for (std::size_t k = 0; k < Kq; ++k)
1371 for (std::size_t c = 0; c < C; ++c) Lq(k, c) = v.d.Lchain(v.qstat[k], c);
1372 const pfqn::LoopingBounds<T> b = pfqn::pfqn_looping(Lq, v.Nv, v.Zc);
1373 const bool up = (method == "looping.upper");
1374 const std::vector<T>& Xchain = up ? b.Xup : b.Xlo;
1375 Matrix<T> Tchain(M, C, zero), Uchain(M, C, zero), Qchain(M, C, zero);
1376 for (std::size_t c = 0; c < C; ++c)
1377 for (std::size_t i = 0; i < M; ++i) {
1378 Tchain(i, c) = T(Xchain[c] * v.d.Vchain(i, c));
1379 Uchain(i, c) = T(Xchain[c] * v.d.Lchain(i, c)); // utilization law
1380 }
1381 Qchain = detail::ba_chain_qfill(Uchain, Xchain, v.d.Lchain, v.Nv, v.isdelay, up);
1383 L, v.d, Qchain, Uchain, Matrix<T>(), Tchain, Xchain);
1384 BaSolution<T> s;
1385 s.Q = cr.Q;
1386 s.U = cr.U;
1387 s.R = cr.R;
1388 s.Tp = cr.Tp;
1389 s.C = cr.C;
1390 s.X = cr.X;
1391 s.lG = std::numeric_limits<double>::quiet_NaN();
1392 return s;
1393 }
1394
1395 // Achievable-region LP relaxation (Bertsimas-Paschalidis-Tsitsiklis 1994).
1396 // The only OPEN-network family here: it lower bounds the mean response
1397 // times attainable by ANY non-idling policy, so it is refused on the closed
1398 // models every other family requires.
1399 if (method == "bpt.lower") {
1400 BaSolution<T> s;
1401 solver_ba_bpt(L, s);
1402 return s;
1403 }
1404
1405 // Piecewise-linear Lyapunov bound (Bertsimas-Gamarnik-Tsitsiklis 2001), the
1406 // OPEN-network upper side. A feasible gamma > 0 both certifies that every
1407 // work-conserving policy is stable and yields the (loose) queue-length bound.
1408 if (method == "bgt.upper") {
1409 BaSolution<T> s;
1410 solver_ba_bgt(L, s);
1411 return s;
1412 }
1413
1414 // Stochastic network calculus (Fidler-Rizk 2015), the third OPEN-network
1415 // family and the only one whose native object is a TAIL: MGF envelopes are
1416 // propagated hop by hop and the delay bound is integrated into a mean.
1417 if (method == "snc.upper") {
1418 BaSolution<T> s;
1419 solver_ba_snc(L, s);
1420 return s;
1421 }
1422
1423 if (method == "cub.upper" || method == "mbjb.lower") {
1424 const detail::ChainView<T> v = detail::ba_chain_view(L);
1425 const std::size_t M = L.nstations, C = L.nchains, Kq = v.qstat.size();
1426 Matrix<T> Lq(Kq, C, zero);
1427 for (std::size_t k = 0; k < Kq; ++k)
1428 for (std::size_t c = 0; c < C; ++c) Lq(k, c) = v.d.Lchain(v.qstat[k], c);
1429 const pfqn::McubBounds<T> b = pfqn::pfqn_mcub(Lq, v.Nv, v.Zc);
1430 const bool up = (method == "cub.upper");
1431 const std::vector<T>& Xchain = up ? b.Xub : b.Xlb;
1432 Matrix<T> Tchain(M, C, zero), Uchain(M, C, zero), Qchain(M, C, zero);
1433 for (std::size_t c = 0; c < C; ++c)
1434 for (std::size_t i = 0; i < M; ++i) {
1435 Tchain(i, c) = T(Xchain[c] * v.d.Vchain(i, c));
1436 Uchain(i, c) = T(Xchain[c] * v.d.Lchain(i, c)); // utilization law
1437 }
1438 Qchain = detail::ba_chain_qfill(Uchain, Xchain, v.d.Lchain, v.Nv, v.isdelay, up);
1440 L, v.d, Qchain, Uchain, Matrix<T>(), Tchain, Xchain);
1441 BaSolution<T> s;
1442 s.Q = cr.Q;
1443 s.U = cr.U;
1444 s.R = cr.R;
1445 s.Tp = cr.Tp;
1446 s.C = cr.C;
1447 s.X = cr.X;
1448 s.lG = std::numeric_limits<double>::quiet_NaN();
1449 return s;
1450 }
1451
1452 // Sharp bounds of Harel-Namn-Sturm, distinct from the 'sb' family of the
1453 // same paper: they extrapolate from the EXACT normalizing constant at
1454 // populations n <= 7 instead of using the first three power sums.
1455 if (method == "harel.upper" || method == "harel.lower") {
1456 const detail::ScDemands<T> sch = detail::ba_sc_demands(L);
1457 if (!(sch.Z == zero))
1458 throw UnsupportedError(ErrorKind{"DelayStation"}, "solver_ba_analyzer: method '" + method +
1459 "' does not support think times (infinite-server stations)");
1460 const int Nh = static_cast<int>(sch.N);
1461 const int maxUB = Nh < 7 ? Nh : 7;
1462 const pfqn::HarelBoundsResult<T> b = pfqn::pfqn_harel_bounds(sch.D, Nh, zero, maxUB);
1463 const bool uph = method == "harel.upper";
1464 T Xb = b.LB;
1465 if (uph) {
1466 T dmax = sch.D[0];
1467 for (const T& d : sch.D)
1468 if (d > dmax) dmax = d;
1469 const T cap = T(num_traits<T>::from_int(1) / dmax);
1470 const T ext = maxUB >= 2 ? b.UB[static_cast<std::size_t>(maxUB)] : b.TH[1];
1471 Xb = ext < cap ? ext : cap;
1472 }
1473 return detail::ba_fill(L, sch.V, sch.N, Xb, sch.Z, sch.D, uph);
1474 }
1475
1476 // The hierarchical / iterative single-class families.
1477 const bool hier = method == "pbh.upper" || method == "pbh.lower" || method == "cbh.upper" ||
1478 method == "cbh.lower" || method == "bjbh.upper" || method == "bjbh.lower" ||
1479 method == "ssd.upper" ||
1480 method == "ssd.lower" || method == "sib.upper" || method == "sib.lower" ||
1481 method == "scb.upper" || method == "scb.lower" ||
1482 method == "ldac.lower" || method == "kriz.upper" || method == "kriz.lower" ||
1483 method == "ibjb.upper" || method == "ibjb.lower" ||
1484 method == "ldsy.upper" || method == "ldsy.lower";
1485 if (!hier)
1486 throw UnsupportedError(ErrorKind{"Method"}, "solver_ba_analyzer: unknown bound method '" + method + "'");
1487
1488 const detail::ScDemands<T> sc = detail::ba_sc_demands(L);
1489 const bool up = method.size() > 6 && method.compare(method.size() - 6, 6, ".upper") == 0;
1490 const T Nt = num_traits<T>::from_int(sc.N);
1491 const int lvl = opt.level;
1492
1493 // ssd and ldac are the two families the reference does NOT gate on server
1494 // count: ssd bounds a multiserver station by disaggregating it into single
1495 // servers, and ldac reads only the limiting demand.
1496 if (method == "ssd.upper" || method == "ssd.lower") {
1497 std::vector<T> cvec;
1498 for (std::size_t i = 0; i < L.nstations; ++i)
1499 if (!detail::ba_is_delay(L, i))
1500 cvec.push_back(num_traits<T>::from_double(L.stations[i].nservers));
1501 const pfqn::SsdBounds<T> b = pfqn::pfqn_ssd(sc.D, Nt, sc.Z, cvec);
1502 return detail::ba_fill(L, sc.V, sc.N, up ? b.Xhi : b.Xlo, sc.Z, sc.D, up);
1503 }
1504 if (method == "ldac.lower") {
1505 // Fixed-rate parameterization (Heffes c = 0); the bound holds only in
1506 // the asymptotic regime N >= Qhat, and the reference errors below it
1507 // rather than return the NaN pfqn_ldbcmp produces there.
1508 const std::vector<T> cz(sc.D.size(), zero);
1509 const pfqn::LdBcmpBound<T> b =
1510 pfqn::pfqn_ldbcmp(sc.D, Nt, sc.Z, cz, num_traits<T>::from_double(1e-10));
1511 if (!b.applicable)
1512 throw UnsupportedError(ErrorKind{"Population"},
1513 "solver_ba_analyzer: method 'ldac.lower' requires the asymptotic regime N >= "
1514 "Qhat");
1515 return detail::ba_fill(L, sc.V, sc.N, b.Xlo, sc.Z, sc.D, false);
1516 }
1517 if (method == "scb.upper" || method == "scb.lower") {
1518 // Single-class bounds of Dowdy et al. (1992). THE BRACKETED OBJECT IS NOT
1519 // THIS MODEL: scb brackets the multiclass system that this single-class
1520 // model aggregates, so scb.lower is the EXACT single-class throughput and
1521 // scb.upper adds the demand-free Expression-(3) gap. That is why scb is
1522 // absent from the auto candidate list -- mixing it with families that
1523 // bracket this model's own solution would compare two different quantities.
1524 if (!(sc.Z == zero))
1525 throw UnsupportedError(ErrorKind{"DelayStation"}, "solver_ba_analyzer: method '" + method +
1526 "' supports Z=0 (no delay station) only; Theorem 3 rests on "
1527 "the delay-free balanced-network throughput");
1528 const pfqn::ScbBounds<T> b = pfqn::pfqn_scb(sc.D, sc.N);
1529 return detail::ba_fill(L, sc.V, sc.N, up ? b.Xhi : b.Xlo, sc.Z, sc.D, up);
1530 }
1531 if (method == "pbh.upper" || method == "pbh.lower") {
1532 const pfqn::PbhBounds<T> b = pfqn::pfqn_pbh(sc.D, static_cast<int>(sc.N), sc.Z, lvl);
1533 return detail::ba_fill(L, sc.V, sc.N, up ? b.Xhi : b.Xlo, sc.Z, sc.D, up);
1534 }
1535 if (method == "bjbh.upper" || method == "bjbh.lower") {
1536 const pfqn::PbhBounds<T> b = pfqn::pfqn_bjbk(sc.D, static_cast<int>(sc.N), sc.Z, lvl);
1537 return detail::ba_fill(L, sc.V, sc.N, up ? b.Xhi : b.Xlo, sc.Z, sc.D, up);
1538 }
1539 if (method == "cbh.upper" || method == "cbh.lower") {
1540 const pfqn::CbhBounds<T> b = pfqn::pfqn_cbh(sc.D, static_cast<int>(sc.N), sc.Z, lvl);
1541 return detail::ba_fill(L, sc.V, sc.N, up ? b.Xhi : b.Xlo, sc.Z, sc.D, up);
1542 }
1543 if (method == "ibjb.upper" || method == "ibjb.lower") {
1544 // Dallery (1987) improved balanced job bounds, Theorems 1 and 2.
1545 const pfqn::IbjbBounds<T> b = pfqn::pfqn_ibjb_bounds(sc.D, static_cast<int>(sc.N), sc.Z);
1546 return detail::ba_fill(L, sc.V, sc.N, up ? b.Xhi : b.Xlo, sc.Z, sc.D, up);
1547 }
1548 if (method == "ldsy.upper" || method == "ldsy.lower") {
1549 // Shanthikumar-Yao (1988): throughput bounds for arbitrary
1550 // queue-dependent rates, mu(i,n) = lldscaling(i,n) * min(n, c_i).
1551 const Matrix<T> mu = detail::ba_sc_ld_mu(L, sc.N);
1552 const pfqn::LdsyBounds<T> b = pfqn::pfqn_ldsy_bounds(sc.D, static_cast<int>(sc.N), sc.Z, mu);
1553 return detail::ba_ld_fill(L, sc.V, sc.N, up ? b.Xhi : b.Xlo, sc.Z, mu, up);
1554 }
1555 // kriz and sib solve quadratics. A delay station enters sib through the
1556 // Section-3.2 substitution for terminal-driven systems
1557 // (rho~_m = L_m/(Z+L)), inside pfqn_sib.
1558 if constexpr (!num_traits<T>::has_transcendental) {
1559 throw UnsupportedError(ErrorKind{"Arithmetic"}, "solver_ba_analyzer: method '" + method +
1560 "' solves a quadratic and is unavailable in exact arithmetic");
1561 } else {
1562 if (method == "kriz.upper" || method == "kriz.lower") {
1563 // Kriz (1984): square-root (Thm 5.1, 5.2) and exponentiation (Thm 4.2)
1564 // closed forms, plus the Theorem-3.2 balanced hierarchy at the level.
1565 const pfqn::KrizBounds<T> b = pfqn::pfqn_kriz_bounds(sc.D, static_cast<int>(sc.N), sc.Z, lvl);
1566 return detail::ba_fill(L, sc.V, sc.N, up ? b.Xhi : b.Xlo, sc.Z, sc.D, up);
1567 }
1568 const pfqn::SibBounds<T> b = pfqn::pfqn_sib(sc.D, static_cast<int>(sc.N), sc.Z, lvl);
1569 return detail::ba_fill(L, sc.V, sc.N, up ? b.Xhi : b.Xlo, sc.Z, sc.D, up);
1570 }
1571}
1572
1573} // namespace ba
1574} // namespace line
1575
1576#endif // LINE_SOLVERS_BA_SOLVER_BA_ANALYZER_H
NumericError(const std::string &what)
Definition error.h:112
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
std::size_t stateful_of_station(std::size_t st) const
std::vector< std::vector< Distrib< T > > > service
service[i][r], 0-based station and class; a disabled entry marks a pair never visited.
std::vector< std::size_t > refclass
(nchains) 1-based class, 0 = none
Matrix< T > rt
sn.rt and sn.rtnodes: the class-expanded routing.
Matrix< double > procid() const
sn.procid (M x K): the ProcessType id of each (station, class) pair, as refreshProcessTypes....
std::vector< JobClass > classes
std::vector< Station< T > > stations
stations[k-1] is the k-th station
Matrix< T > rates
(nstations x nclasses) service rates and SCVs, with a PARALLEL disabled flag instead of MATLAB's NaN ...
std::vector< std::vector< std::size_t > > inchain
1-based class indices per chain
std::vector< NodeDef > nodes
every node, in creation order
std::vector< Matrix< T > > visits
(nchains) each (nstateful x nclasses)
double nclosedjobs() const
sn.nclosedjobs: the total population of the closed classes.
MVA-shaped linear-reduction bound for a closed network of M - 1 exponential queues and ONE MAP queue.
Linear-reduction (LR) bound on the utilization of one station of a closed product-form network.
std::string method_refusal(const qn::NetworkStruct< T > &L, const std::string &method)
The STRUCTURAL premises of the SolverBA bound families, in one place: the reason METHOD cannot bound ...
BaSolution< T > solver_ba_analyzer(const qn::NetworkStruct< T > &L, const BaOptions &opt)
Port of solver_ba_analyzer.
void solver_ba_snc(const qn::NetworkStruct< T > &L, Solution &out)
void solver_ba_bgt(const qn::NetworkStruct< T > &L, Solution &out)
std::string method_degenerate(const qn::NetworkStruct< T > &L, const std::string &method)
Whether METHOD APPLIES to L but its bound carries no information there, and why.
void solver_ba_bpt(const qn::NetworkStruct< T > &L, Solution &out)
mam::Map< T > dist_to_map(const Distrib< T > &d)
The (D0,D1) pair that reaches sn.proc.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
Definition lang_types.h:194
const char * node_type_to_text(NodeType t)
Name of a node kind, for diagnostics.
Definition lang_types.h:362
const char * process_to_text(ProcessType p)
The MATLAB ProcessType name, as sn.procid prints it.
Definition lang_types.h:655
T map_mean(const Map< T > &m)
Mean inter-arrival time, 1/lambda.
Definition map_moment.h:101
MapqnBndLrMvaResult< T > mapqn_bnd_lr_mva(const LrMvaParams< T > &p, int objective_queue, int objective_level, MapqnSense sense=MapqnSense::Max, MapqnObjectiveVar objective_var=MapqnObjectiveVar::UN)
Bound UN or QN at (objective_queue, objective_level) over the MVA-shaped LR polytope.
MapqnSense
Which direction the bound is taken in.
LrPfResult< T > mapqn_bnd_lr_pf(const LrPfParams< T > &p, int objective_queue, MapqnSense sense)
Port of mapqn_bnd_lr_pf.
ClassResults< T > sn_deaggregate_chain_results(const qn::NetworkStruct< T > &L, const ChainDemands< T > &d, const Matrix< T > &Qchain, const Matrix< T > &Uchain, const Matrix< T > &Rchain, const Matrix< T > &Tchain, const std::vector< T > &Xchain)
Port of sn_deaggregate_chain_results.
Definition sn_chain.h:227
ChainDemands< T > sn_get_demands_chain(const qn::NetworkStruct< T > &L)
Port of sn_get_demands_chain.
Definition sn_chain.h:63
KrizBounds< T > pfqn_kriz_bounds(const std::vector< T > &Lin, int N, const T &Z, int level=0)
Kriz (1984) throughput bounds.
ScbBounds< T > pfqn_scb(const std::vector< T > &L, long N)
Bracket on the throughput and the per-device utilizations of the UNKNOWN multiclass system whose sing...
Definition pfqn_scb.h:67
McubBounds< T > pfqn_mcub(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z)
Kerola's multiclass composite bound (Perf.
Definition pfqn_mcub.h:55
LdsyBounds< T > pfqn_ldsy_bounds(const std::vector< T > &Lin, int N, const T &Zin, const Matrix< T > &muIn)
Shanthikumar-Yao (1988) throughput bounds for queue-dependent rates.
MwrbbSched
Station discipline codes, matching the MATLAB sched argument.
Definition pfqn_mwrbb.h:48
T pfqn_xzgsbup(const std::vector< T > &L, const T &N, const T &Z)
X = 2N / (R + sqrt(R^2 - 4 Z Lmax N)), R from the geometric queue bound.
SibBounds< T > pfqn_sib(const std::vector< T > &L, int N, const T &Z, int level)
Successively Improving Bounds (Srinivasan 1985/1987) on the cycle time and throughput of a single-cla...
Definition pfqn_sib.h:86
HarelBoundsResult< T > pfqn_harel_bounds(const std::vector< T > &rho, int N, const T &Z, int maxUB)
Both bounds, plus the exact throughputs the upper bounds extrapolate from.
T pfqn_qzgbup(const std::vector< T > &L, const T &N, const T &Z, std::size_t i)
As the lower bound, with Y from the ABA upper bound and the sigma term.
Definition pfqn_qzgbup.h:34
PbhBounds< T > pfqn_bjbk(const std::vector< T > &L, int N, const T &Z, int k)
BJB(k), the iterative Balanced Job Bound.
Definition pfqn_pbh.h:173
MwrbbBounds< T > pfqn_mwrbb(const Matrix< T > &V, const Matrix< T > &S, const std::vector< T > &N, const std::vector< T > &Z, const std::vector< MwrbbSched > &sched, const std::vector< int > &prio)
Majumdar-Woodside robust box bounds on the per-class throughput of a closed multiclass network with m...
Definition pfqn_mwrbb.h:161
PbhBounds< T > pfqn_pbh(const std::vector< T > &L, int N, const T &Z, int level)
Performance Bound Hierarchy (Eager and Sevcik 1983, ACM TOCS 1(2):99-115) for single-class closed pro...
Definition pfqn_pbh.h:114
LdBcmpBound< T > pfqn_ldbcmp(const std::vector< T > &L, const T &N, const T &Z, const std::vector< T > &c, const T &tol)
Anselmi-Cremonesi (2008) lower throughput bound for a closed single-class BCMP network with load-depe...
Definition pfqn_ldbcmp.h:64
LoopingBounds< T > pfqn_looping(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, double tol=1e-6, std::size_t maxiter=1000)
Eager Looping bounds for closed multiclass product-form networks.
T pfqn_qzgblow(const std::vector< T > &L, const T &N, const T &Z, std::size_t i)
Qgb = y/(1-y) - y^(N+1)/(1-y) with y = N L_i / (Z + sum(L) + Lmax N).
CbhBounds< T > pfqn_cbh(const std::vector< T > &L, int N, const T &Z, int level)
Convolutional Bound Hierarchy (Dowdy, Eager, Gordon and Saxton 1984) on the throughput of a single-cl...
Definition pfqn_cbh.h:118
IbjbBounds< T > pfqn_ibjb_bounds(const std::vector< T > &Lin, int N, const T &Z)
Dallery (1987) improved balanced job bounds.
T pfqn_xzgsblow(const std::vector< T > &L, const T &N, const T &Z)
X = 2N / (R + sqrt(R^2 - 4 Z Lmax (N-1))), R from the geometric queue bound.
SsdBounds< T > pfqn_ssd(const std::vector< T > &L, const T &N, const T &Z, const std::vector< T > &nservers)
Server-Station Disaggregation bounds for a multiserver closed network (Dallery and Suri,...
Definition pfqn_ssd.h:55
std::string ba_family_of(const std::string &method)
The family prefix of a bound method name: everything before the first dot.
std::string ba_resolve_method_name(const std::string &method)
The 'default'/'auto'/'qr'/'lr' aliases, duplicated from ba::resolve_method for the same reason ba_fam...
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
A queueing network and its refreshed NetworkStruct.
Convolutional Bound Hierarchy (Dowdy, Eager, Gordon and Saxton 1984) on the throughput of a single-cl...
Harel-Namn-Sturm throughput bounds for a single-class closed network.
Dallery (1987) improved balanced job bounds for single-class closed networks of single-server station...
Kriz (1984) closed-form throughput bounds for single-class closed networks of single-server stations ...
Anselmi-Cremonesi (2008) lower throughput bound for a closed single-class BCMP network with load-depe...
Shanthikumar-Yao (1988) throughput bounds for single-class closed networks with arbitrary queue-depen...
Eager Looping bounds for closed multiclass product-form networks.
Kerola's multiclass composite bound (Perf.
Majumdar-Woodside robust box bounds on the per-class throughput of a closed multiclass network with m...
Performance Bound Hierarchy (Eager and Sevcik 1983, ACM TOCS 1(2):99-115) for single-class closed pro...
Geometric-bound lower bound on the queue length at station i.
Geometric-bound upper bound on the queue length at station i.
Dowdy-Carlson-Krantz-Tripathi (1992) single-class bounds of multi-class queueing networks,...
Successively Improving Bounds (Srinivasan 1985/1987) on the cycle time and throughput of a single-cla...
Server-Station Disaggregation bounds for a multiserver closed network (Dallery and Suri,...
Geometric-square-root bound, lower bound on throughput.
Geometric-square-root bound, upper bound on throughput.
Chain aggregation and de-aggregation.
Piecewise-linear Lyapunov UPPER bound on the steady-state queue lengths of a multitype open Markovian...
Achievable-region LOWER bound on the mean response times of a multiclass open Markovian network,...
Stochastic network calculus UPPER bound on the mean response times and queue lengths of a feed-forwar...
The DECLARED side of the gate: one feature set per solver.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
options.config.qrf_params, the blocking tables the BAS and RS-RD arms need.
std::vector< std::vector< int > > MM1
(MR x M) extended order
std::vector< std::vector< int > > MM
(MR x 2) blocking order
std::vector< std::vector< int > > BB
(MR x M) blocking state
int MR
number of blocking configurations
int f
finite-capacity queue, 1-based as in the reference
std::vector< int > ZZ
(MR) blocked count per config
std::vector< int > F
(M) capacity; empty takes sn.cap
The options SolverBA reads.
int level
options.level: the hierarchy level of pbh/bjbh/cbh/sib.
std::string method
Bound method; default resolves to gb.upper in the runner.
Matrix< double > qrf_alpha
options.config.qrf_alpha, the (nstations x N) load-dependent scaling of the two load-dependent QRF ar...
Class-level results, the [Q,U,R,T,C,X] of solver_ba_analyzer.
double lG
-N log X, the reference's approximate normalizing constant.
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
Parameters of the MVA-shaped LR bound, mirroring the reference's params.
Matrix< T > r
(M x M) routing probabilities
Matrix< T > muMAP
(K x K) completion rates of the MAP queue
int K
number of levels of the MAP queue
int M
number of queues, the last of which is the MAP
Matrix< T > v
(K x K) level-change rates of the MAP queue
std::vector< T > muM
(M-1) service rates of the exponential queues
int N
closed population
Product-form parameters of the LR bound, mirroring MATLAB's params.
int N
closed population
Matrix< T > r
(M x M) routing probabilities
std::vector< T > mu
(M) service rates
Return value of mapqn_bnd_lr_pf, mirroring the MATLAB result struct.
T objective
the bounded utilization of the objective station
std::string status
simplex status name
Result of an MVA-shaped LR bound solve.
T objective
the bound on UN(objective_queue, objective_level)
std::string status
textual LP status
bool ok
the LP reached an optimal vertex
Class-level results, as sn_deaggregate_chain_results returns them.
Definition sn_chain.h:204
std::vector< T > C
Definition sn_chain.h:206
std::vector< T > X
Definition sn_chain.h:206
Return value of pfqn_cbh, mirroring [Xlo, Xhi].
Definition pfqn_cbh.h:41
Return value of pfqn_harel_bounds, mirroring Ret.pfqnHarelBounds.
std::vector< T > UB
UB[n] for n = 2..maxUB; entries 0 and 1 are unset.
T LB
throughput lower bound at population N
std::vector< T > TH
TH[n] = exact throughput at population n, n = 1..maxUB.
Return value of pfqn_ibjb_bounds, mirroring [Xlo, Xhi].
Return value of pfqn_kriz_bounds, mirroring [Xlo, Xhi, info].
Return value of pfqn_ldbcmp, mirroring [Xlo, Rhi, Qhat].
Definition pfqn_ldbcmp.h:48
bool applicable
false where MATLAB returns NaN (N < Qhat, or rho >= 1)
Definition pfqn_ldbcmp.h:52
Return value of pfqn_ldsy_bounds, mirroring [Xlo, Xhi, info].
Return value of pfqn_looping: the throughput bracket plus its queue lengths.
std::vector< T > Xup
(R) optimistic (upper) throughput bound
std::vector< T > Xlo
(R) pessimistic (lower) throughput bound
Return value of pfqn_mcub, mirroring [Xub, Xlb].
Definition pfqn_mcub.h:42
std::vector< T > Xlb
Definition pfqn_mcub.h:44
std::vector< T > Xub
Definition pfqn_mcub.h:43
Return value of pfqn_mwrbb, mirroring [Xlo, Xup, Wlo].
Definition pfqn_mwrbb.h:52
std::vector< T > Xlo
Definition pfqn_mwrbb.h:53
std::vector< T > Xup
Definition pfqn_mwrbb.h:54
Return value of pfqn_pbh, mirroring [Xlo, Xhi, Qlo, Qhi].
Definition pfqn_pbh.h:43
Return value of pfqn_scb, mirroring [Xlo, Xhi, Ulo, Uhi].
Definition pfqn_scb.h:42
Return value of pfqn_sib, mirroring [Xlo, Xhi, Wlo, Whi].
Definition pfqn_sib.h:69