LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
solver_ba_runner.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_RUNNER_H
6#define LINE_SOLVERS_BA_SOLVER_BA_RUNNER_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * The SolverBA class surface: `@@SolverBA/runAnalyzer.m`, `listValidMethods`,
12 * `getBounds` and `getBoundsTable`.
13 *
14 * What sits here rather than in the analyzer is everything around one bound
15 * evaluation: the closed-model gate, the method aliases, the whitelist, the
16 * arrival-rate conversion and the metric filter `@@NetworkSolver/getAvg` applies
17 * to every solver's output. The result shape is SolverMVA's `AvgResult`, so a
18 * bound and a point estimate are directly comparable -- which is what a caller
19 * checking that a bracket contains the exact answer needs.
20 *
21 * FINITE-BUFFER BLOCKING IS REFUSED, NOT BOUNDED. Needing only demands and a
22 * population is the BCMP parameterization, which presumes UNBOUNDED buffers; a
23 * buffer that binds couples the station occupancies and the resulting numbers
24 * do not bracket the blocked model. `solver_ba_run_analyzer` therefore gates on
25 * `api::sn_has_blocking` and `list_valid_methods(L)` drops every blocking-blind
26 * method. The exceptions are `qrf.bas*`/`qrf.rsrd`, which carry the blocking
27 * tables explicitly. Use SolverMVA method `sqd` for a point estimate.
28 *
29 * THE RESIDENCE TIME IS ZERO BY CONSTRUCTION. `runAnalyzer.m` sets `WN` to a
30 * zero matrix and does not derive it from `RN`: the bound families define no
31 * per-station residence, and the reference declines to invent one.
32 */
33
34#include <algorithm>
35#include <cmath>
36#include <limits>
37#include <string>
38#include <vector>
39
50
51namespace line {
52namespace ba {
53
54using lang::GlobalConstants;
55
56/**
57 * Port of `runAnalyzer`'s method aliases: `default` is the geometric upper
58 * bound, bare `auto` is the AUTO composite's upper side, and `qr`/`lr` are the
59 * friendly names of two QRF sub-methods.
60 */
61inline std::string resolve_method(const std::string& method) {
62 if (method == "default") return "gb.upper";
63 if (method == "auto") return "auto.upper";
64 if (method == "lr") return "lr.upper";
65 if (method == "qr") return "qrf.mmi";
66 return method;
67}
68
69namespace detail {
70/** Whether METHOD names a side of the 'aba'/'bjb' pair, which also has an open (transaction) form. */
71inline bool ba_is_open_aba_bjb_name(const std::string& method) {
72 const std::string m = resolve_method(method);
73 return m == "aba.upper" || m == "aba.lower" || m == "bjb.upper" || m == "bjb.lower";
74}
75} // namespace detail
76
77/**
78 * Whether METHOD bounds a model as if its buffers were unbounded.
79 *
80 * Every family but the QRF BLOCKING bounds is parameterized by demands (visits
81 * x service time) and a population alone, which is the BCMP parameterization:
82 * unbounded buffers, and an equilibrium distribution that factorizes. A finite
83 * buffer that BINDS breaks both premises, so the numbers do not bracket the
84 * blocked model -- on `cqn_bas_blocking` (Queue2 capped at 1, N = 2) `gb.upper`
85 * reports QLen 1.28 at a station that can never hold more than one job.
86 * `qrf.bas*` and `qrf.rsrd` carry the blocking tables (MM, MM1, ZZ, ZM, BB, F)
87 * explicitly and are the exceptions.
88 *
89 * METHOD must already be resolved through `resolve_method`.
90 */
91inline bool ignores_blocking(const std::string& method) {
92 // 'spnlp.*' is an exception alongside the QRF blocking bounds: its polytope
93 // is indexed by the marking itself, so a bounded place enters it as a
94 // variable upper bound and as the P-invariant equality that produced the
95 // bound. The buffer is modelled, not assumed away.
96 return !(method.rfind("qrf.bas", 0) == 0 || method.rfind("qrf.rsrd", 0) == 0 ||
97 method.rfind("spnlp", 0) == 0);
98}
99
100/**
101 * Port of `SolverBA.listValidMethods`.
102 *
103 * A LISTED NAME MUST ACTUALLY RUN, OR BE REFUSED BY NAME. The `lr` family IS
104 * listed: its bound is a pure linear program, which `lp::simplex_solve` serves
105 * exactly. The four no-blocking `qrf.*` arms and the supported BAS-blocking
106 * bounds are listed too, served by `solver_ba_qrf_analyzer`.
107 *
108 * `qrf.bas.mmi` was refused by name in the reference between 2026-08-01 and
109 * 2026-09-03, when `solver_ba_qrf_analyzer.m` began serving it again; it is
110 * listed here, as in `SolverBA.listAllMethods`, and served like the other BAS bounds.
111 */
112inline std::vector<std::string> list_valid_methods() {
113 // the names and their order are this solver's row of the method registry (doc/line-methods.json)
114 return ::line::method_registry::methods("BA");
115}
116
117/**
118 * What `default`/`auto` must mean on a model with finite buffers: the QRF BAS
119 * bound, or nothing.
120 *
121 * `default` resolves to the geometric upper bound, which is parameterized by
122 * demands and a population alone and therefore bounds a blocked model as if its
123 * buffers were unbounded. `ignores_blocking` refuses that, which is right; but
124 * refusing is not the whole answer, because `qrf.bas` DOES model the finite
125 * buffer and `sn_to_qrf_blocking` derives its tables from the model, so there
126 * is nothing left for the caller to supply. A blocked model of the right shape
127 * therefore gets `qrf.bas` as its default, exactly as SolverMVA routes a BAS
128 * model to `sqd`.
129 *
130 * The shape is the one `list_valid_methods(L)` calls "reducible" and the QRF
131 * analyzer gates on. On top of it the tables must actually derive, which is
132 * asked of `sn_to_qrf_blocking` rather than re-tested here -- it owns the
133 * single-finite-buffer rule and the size guard, and a second copy of either is
134 * how the two drift apart.
135 *
136 * Only the UPPER side is routed: the analyzer solves qrf.bas in the 'max'
137 * direction alone, so `auto.lower` has no blocking counterpart and keeps
138 * refusing rather than being answered with the wrong side.
139 *
140 * @return {method, why}; method is empty when the routing does not apply, and
141 * why then carries the reason (empty for an unblocked model).
142 */
143template <class T>
144std::pair<std::string, std::string> blocking_default(const qn::NetworkStruct<T>& L) {
145 typedef std::pair<std::string, std::string> R;
146 if (!api::sn_has_blocking(L)) return R("", "");
147 if (L.nclasses != 1)
148 return R("", "the QRF blocking bounds are derived for a single-class closed network,"
149 " which this model is not.");
150 for (std::size_t r = 0; r < L.classes.size(); ++r)
151 if (!std::isfinite(L.classes[r].population))
152 return R("", "the QRF blocking bounds are derived for a single-class closed"
153 " network, which this model is not.");
154 for (std::size_t i = 0; i < L.stations.size(); ++i) {
155 if (L.stations[i].sched == SchedStrategy::INF)
156 return R("", "the QRF blocking bounds model every station as a single server and"
157 " have no infinite-server notion, so a delay station rules them out.");
158 if (L.stations[i].nservers > 1.0)
159 return R("", "the QRF blocking bounds model every station as a single server, so a"
160 " multiserver station rules them out.");
161 }
162 // Same phase count `solver_ba_qrf_analyzer` derives, so the size guard
163 // here judges the LP the analyzer would actually build.
164 int Ktot = 0;
165 for (std::size_t i = 0; i < L.stations.size(); ++i) {
166 if (L.service[i][0].disabled) { Ktot += 1; continue; } // a Join: one phase, as in MATLAB
167 const mam::Map<T> mp = lang::dist_to_map(L.service[i][0]);
168 Ktot += mp.D0.rows() == 0 ? 1 : static_cast<int>(mp.D0.rows());
169 }
170 const sn::QrfBlocking blk = sn::sn_to_qrf_blocking(L, Ktot);
171 if (!blk.msg.empty()) return R("", blk.msg);
172 return R("qrf.bas", "");
173}
174
175/** True for the aliases whose meaning a blocked model is allowed to change. */
176inline bool is_default_request(const std::string& method) {
177 return method == "default" || method == "auto" || method == "auto.upper";
178}
179
180/**
181 * Port of `ba_resolve_model_method.m` (`SolverBA.resolveMethod`): the method
182 * METHOD runs as on THIS model. The model-free aliases of `resolve_method`, then
183 * the finite-buffer routing of `blocking_default`, under which `default`, `auto`
184 * and `auto.upper` run as `qrf.bas` on a blocked model of the right shape -- the
185 * same resolution `solver_ba_solve` dispatches on, so findSolver gates the name
186 * the run serves.
187 */
188template <class T>
189std::string resolve_model_method(const qn::NetworkStruct<T>& L, const std::string& method) {
190 std::string resolved = resolve_method(method);
191 if (is_default_request(method) && ignores_blocking(resolved) && api::sn_has_blocking(L)) {
192 const std::pair<std::string, std::string> routed = blocking_default(L);
193 if (!routed.first.empty()) resolved = routed.first;
194 }
195 return resolved;
196}
197
198/**
199 * The same list, narrowed to what THIS model can run.
200 *
201 * The QR/LR/QRF reduction bounds share one premise -- a single-class closed
202 * network of single-server stations -- which `solver_ba_qrf_analyzer` enforces
203 * and the `lr` family shares. A listed name that always throws is a method a
204 * caller is invited to ask for and cannot have, so the model-aware overload
205 * drops them; `SolverMVA` gates `sqni` the same way. Mirrors `SolverBA.m`,
206 * `SolverBA.java` and the python `list_valid_methods`.
207 */
208template <class T>
209std::vector<std::string> list_valid_methods(const qn::NetworkStruct<T>& L) {
210 std::vector<std::string> all = list_valid_methods();
211 // The STRUCTURAL premises -- single-class closed, fully closed,
212 // single-server -- come from `method_refusal`, the same predicate
213 // `solver_ba_run_analyzer` throws on. Asked first so every later narrowing
214 // works on names this model could actually run, and asked HERE because this
215 // is the list `autosolver::auto_family_methods` reads: before it, a
216 // two-class closed network was offered all 36 demand-parameterized bounds
217 // and 30 of them threw the moment they were run.
218 // ... and `method_degenerate` withholds the second kind of name: one whose
219 // premises this model MEETS but whose formula says nothing here.
220 // 'ldac.lower' at N == Qhat is the only such case: it reports the trivial
221 // X >= 0, which propagates into an all-zero table a caller cannot tell from
222 // an answer. Offering is what stops; asking for it by NAME still runs and
223 // still publishes it, since a vacuous bound is a valid one -- which is what
224 // `tests/test_ba.cpp` pins at that boundary.
225 {
226 std::vector<std::string> structural;
227 for (std::size_t i = 0; i < all.size(); ++i)
228 if (method_refusal(L, all[i]).empty() && method_degenerate(L, all[i]).empty())
229 structural.push_back(all[i]);
230 all = structural;
231 }
232 // 'spnlp.*' is the only family indexed by a MARKING rather than by demands
233 // and a population, and the split is total in both directions: on a Petri
234 // net nothing else has a representation of the model, and off one spnlp has
235 // nothing to read. Two of the gates below already half-cover this by
236 // accident -- a Place is an INF station, so `reducible` and `bptOk` are both
237 // false on any Petri net -- but the demand-parameterized families survive
238 // them and must be dropped by name. Applied first so the open/closed
239 // narrowing cannot reinstate one.
240 bool isPetri = false;
241 for (std::size_t i = 0; i < L.nodes.size() && !isPetri; ++i)
242 isPetri = L.nodes[i].nodetype == lang::NodeType::Transition;
243 {
244 std::vector<std::string> sieved;
245 for (std::size_t i = 0; i < all.size(); ++i)
246 if (is_spnlp_method(all[i]) == isPetri) sieved.push_back(all[i]);
247 all = sieved;
248 if (isPetri) return all;
249 }
250 bool reducible = (L.nclasses == 1);
251 for (std::size_t r = 0; reducible && r < L.classes.size(); ++r)
252 if (!std::isfinite(L.classes[r].population)) reducible = false;
253 bool closedSingleClass = reducible;
254 for (std::size_t i = 0; reducible && i < L.stations.size(); ++i) {
255 if (L.stations[i].sched == SchedStrategy::INF)
256 reducible = false;
257 else if (L.stations[i].nservers > 1.0)
258 reducible = false;
259 }
260 // The LOAD-DEPENDENT arms survive where the rest of the family cannot run:
261 // alpha(i,n) is the rate law of a delay (alpha = n), of a c-server station
262 // (alpha = min(n,c)) and of limited load dependence alike, so
263 // `qrf.mmi.ld` and `qrf.mmi.linear` answer those models on the model's own
264 // chain. `sn_to_qrf_alpha` owns the one restriction that survives,
265 // exponential service wherever a station serves several jobs at once.
266 // Dropping them with the rest would hide from a caller enumerating the list
267 // the only two bound methods such a model has.
268 const bool ldReducible =
269 !reducible && closedSingleClass && sn::sn_to_qrf_alpha(L).msg.empty();
270 // 'bpt', 'bgt' and 'snc' are the mirror image of the reduction bounds: all
271 // three are derived for an OPEN network of single-server exponential
272 // stations, so every closed model, every delay station and every
273 // multiserver station rules them out. Every other family rules OUT the open
274 // model, so on an open network the list narrows to those three.
275 bool fullyOpen = !L.classes.empty();
276 for (std::size_t r = 0; fullyOpen && r < L.classes.size(); ++r)
277 if (std::isfinite(L.classes[r].population)) fullyOpen = false;
278 bool bptOk = fullyOpen;
279 for (std::size_t i = 0; bptOk && i < L.stations.size(); ++i) {
280 if (L.stations[i].nodetype == qn::NodeType::Source) continue;
281 if (L.stations[i].sched == SchedStrategy::INF)
282 bptOk = false;
283 else if (L.stations[i].nservers > 1.0)
284 bptOk = false;
285 }
286 std::vector<std::string> keep;
287 if (fullyOpen) {
288 if (bptOk) {
289 // Each is re-added only if it SURVIVED the structural narrowing
290 // above. Pushing them unconditionally is what silently undid it:
291 // `snc.upper` is dropped there on non-exponential service, and a
292 // blind push put it straight back, so an Erlang-service model was
293 // still offered a bound that throws.
294 const char* const open_fams[] = {"bpt.lower",
295 // 'bgt.upper' additionally needs
296 // deterministic non-merging routes, and
297 // 'snc.upper' a feed-forward station
298 // graph; both are checked by walking the
299 // routing matrix in their own analyzers,
300 // too expensive to repeat here, so they
301 // stay listed and refuse by name.
302 "bgt.upper", "snc.upper"};
303 for (const char* nm : open_fams)
304 if (std::find(all.begin(), all.end(), std::string(nm)) != all.end())
305 keep.push_back(nm);
306 }
307 // ... and the open (transaction) forms of ABA and BJB (Lazowska et al.
308 // 1984, Tables 5.1-5.2), whose own refusal and degeneracy checks have
309 // already run above: 'aba.upper' is vacuous on an open network and the
310 // pair is withheld past saturation, so neither survives to here then.
311 const char* const open_aba_bjb[] = {"aba.upper", "aba.lower", "bjb.upper", "bjb.lower"};
312 for (const char* nm : open_aba_bjb)
313 if (std::find(all.begin(), all.end(), std::string(nm)) != all.end())
314 keep.push_back(nm);
315 // in the listing's own order, as the reference's logical mask keeps them
316 std::stable_sort(keep.begin(), keep.end(), [&all](const std::string& a, const std::string& b) {
317 return std::find(all.begin(), all.end(), a) < std::find(all.begin(), all.end(), b);
318 });
319 } else {
320 for (std::size_t i = 0; i < all.size(); ++i) {
321 const std::string& m = all[i];
322 if (m == "bpt.lower" || m == "bgt.upper" || m == "snc.upper") continue;
323 if (reducible) {
324 keep.push_back(m);
325 continue;
326 }
327 if (ldReducible && (m == "qrf.mmi.ld" || m == "qrf.mmi.linear")) {
328 keep.push_back(m);
329 continue;
330 }
331 // 'mapamva' shares that premise exactly -- single-class closed,
332 // single-server, no delay -- so it narrows with them rather than
333 // being offered on a model it would refuse on contact. It is NOT
334 // one of the load-dependent arms: its q carries no population
335 // index, so a delay or a c-server station has nowhere to go.
336 if (m == "qr" || m == "lr" || m.compare(0, 3, "lr.") == 0 ||
337 m.compare(0, 4, "qrf.") == 0 || m.compare(0, 7, "mapamva") == 0)
338 continue;
339 keep.push_back(m);
340 }
341 }
342 // A binding finite buffer rules out everything but the QRF blocking bounds:
343 // the other families presume unbounded buffers, and `solver_ba_run_analyzer` refuses
344 // them by name on such a model. The list can legitimately come back EMPTY
345 // -- a blocked model that is not single-class closed single-server has no
346 // bound method at all, and offering one would be the mis-selection this
347 // gate exists to prevent.
348 if (api::sn_has_blocking(L)) {
349 std::vector<std::string> unblocked;
350 // 'default' is offered back when it now MEANS one of the survivors:
351 // `solver_ba_run_analyzer` routes it to 'qrf.bas' on a blocked model of the right
352 // shape, so a caller enumerating the list would otherwise be told the
353 // model's own default is invalid.
354 if (!blocking_default(L).first.empty()) unblocked.push_back("default");
355 for (std::size_t i = 0; i < keep.size(); ++i)
356 if (!ignores_blocking(resolve_method(keep[i]))) unblocked.push_back(keep[i]);
357 return unblocked;
358 }
359 return keep;
360}
361
362/** Port of `runAnalyzer`'s method gate: an unlisted name is refused by name. */
363inline void check_method(const std::string& method) {
364 // `qrf.bas.mmi` used to be refused here, mirroring the reference. The
365 // reference serves it again since 2026-09-03, so nothing is refused by name
366 // beyond an unlisted one.
367 const std::string m = resolve_method(method);
368 const std::vector<std::string> valid = list_valid_methods();
369 if (std::find(valid.begin(), valid.end(), m) != valid.end()) return;
370 throw UnsupportedError(ErrorKind{"Method"}, "SolverBA: unknown bound method '" + method + "'");
371}
372
373/**
374 * Port of `@@SolverBA/runAnalyzer.m` for the `lang='matlab'` path.
375 *
376 * @param L the refreshed struct of a closed model
377 * @param opt_in the method and, for a hierarchical family, the level
378 */
379template <class T>
381 // Every bound family here is parameterized by demands and a closed
382 // population, except the three OPEN-network families 'bpt', 'bgt' and
383 // 'snc', which are refused on a closed model instead, and the 'aba' and
384 // 'bjb' pairs, which also have an open (transaction) form for a
385 // single-class open network (Lazowska et al. 1984, Tables 5.1-5.2) that
386 // `method_refusal` admits.
387 // ... and the 'spnlp' family, which is parameterized by a MARKING: whether
388 // that marking is bounded is a question about the P-invariants of the net
389 // and not about nclosedjobs, so `spn_lpbnd` decides it rather than this gate.
390 if (!(L.nclosedjobs() > 0.0) && opt_in.method.compare(0, 3, "bpt") != 0 &&
391 opt_in.method.compare(0, 3, "bgt") != 0 && opt_in.method.compare(0, 3, "snc") != 0 &&
392 !is_spnlp_method(opt_in.method) && !detail::ba_is_open_aba_bjb_name(opt_in.method))
393 throw UnsupportedError(ErrorKind{"OpenClass"}, "SolverBA: supports closed queueing networks only");
394 check_method(opt_in.method);
395 BaOptions opt = opt_in;
396 opt.method = resolve_method(opt_in.method);
397
398 // Finite-buffer BLOCKING is outside the premises of every family here
399 // except the QRF blocking bounds: the rest are parameterized by demands and
400 // a population alone, which presumes unbounded buffers and a product form
401 // that the truncation destroys. Refusing is not conservatism -- on
402 // `cqn_bas_blocking`, `gb.upper` reports QLen 1.28 at a station capped at 1
403 // job. Gated after the aliases so `default` is judged as the `gb.upper` it
404 // resolves to.
405 std::string blocking_why;
406 if (ignores_blocking(opt.method) && api::sn_has_blocking(L) &&
407 is_default_request(opt_in.method)) {
408 // A blocked model whose shape admits the QRF BAS bound gets it as the
409 // DEFAULT rather than a refusal: 'qrf.bas' models the finite buffer,
410 // and since `sn_to_qrf_blocking` derives its tables from the model
411 // there is nothing left for the caller to supply.
412 const std::pair<std::string, std::string> routed = blocking_default(L);
413 blocking_why = routed.second;
414 if (!routed.first.empty()) opt.method = routed.first;
415 }
416
417 if (ignores_blocking(opt.method) && api::sn_has_blocking(L))
418 throw UnsupportedError(ErrorKind{"FiniteCapacity"},
419 "SolverBA: method '" + opt_in.method +
420 "' does not support finite-buffer blocking: every SolverBA bound family but the"
421 " QRF blocking ones is parameterized by demands and a population alone, so it"
422 " bounds the model as if its buffers were unbounded. Use SolverMVA with method"
423 " 'sqd', an exact solver (CTMC, SSA, JMT, LDES), or the QRF blocking bounds"
424 " 'qrf.bas'/'qrf.rsrd', which model the finite buffer" +
425 (blocking_why.empty()
426 ? std::string()
427 : ". The QRF blocking bounds do not apply here either: " + blocking_why));
428
429 // THE RUN-TIME GATE, the same one the report applies (MATLAB
430 // @SolverBA/runAnalyzer, supportsModelMethod(method, true)): structural
431 // predicate FIRST, feature envelope of the RESOLVED method second, and no
432 // `method_degenerate` (a vacuous bound is withheld from the report but
433 // published when named). Until this was here the envelope was report-only:
434 // a load-dependent model was bounded by gb/kriz/sib/ibjb/aba/bjb/pb/mw on
435 // its BASE rates when the method was asked for by name.
436 //
437 // The STRUCTURAL refusal itself is still thrown by `solver_ba_analyzer`,
438 // which asks `method_refusal` on the way in, with its own prefix; asking it
439 // here only keeps the reference's order, so a model outside both gates is
440 // refused with the structural sentence rather than with a feature list.
441 // Placed after the aliases and the blocking route, so 'default' is judged
442 // as the gb.upper (or qrf.bas) it runs as and the message names it.
443 if (method_refusal(L, opt.method).empty())
444 qn::feature_gate("SolverBA", qn::ba_feature_set(opt.method), L, opt_in.method, opt.method);
445
446 const bool qrf = detail::is_qrf_noblo_method(opt.method) ||
447 detail::is_qrf_lp_method(opt.method) ||
448 detail::is_qrf_bas_nlp_method(opt.method);
449 const BaSolution<T> s = is_spnlp_method(opt.method)
452 const T zero = num_traits<T>::from_int(0);
453 const std::size_t M = L.nstations, K = L.nclasses;
454
455 std::vector<std::vector<bool>> mask(M, std::vector<bool>(K, false));
456 for (std::size_t i = 0; i < M; ++i)
457 for (std::size_t k = 0; k < K; ++k)
458 mask[i][k] = num_traits<T>::to_double(s.R(i, k)) < 10.0 * GlobalConstants::FineTol;
459
461 out.QN = mva::filter_metric(L, s.Q, mva::MetricKind::QLen, &mask);
462 out.UN = mva::filter_metric(L, s.U, mva::MetricKind::Util, &mask);
463 out.RN = mva::filter_metric(L, s.R, mva::MetricKind::RespT, nullptr);
464 out.TN = mva::filter_metric(L, s.Tp, mva::MetricKind::Tput, nullptr);
465 // The arrival rate comes from the throughputs through the same helper
466 // SolverMVA and SolverNC use. The bound goldens were recorded under those
467 // solvers before the family moved here and carry a real per station-class
468 // arrival rate; returning zeros silently emptied one column. The Source mask
469 // is getAvg's and is kept although no model that reaches here has a Source:
470 // the gates admit a mixed model, and every family then refuses it.
471 std::vector<std::vector<bool>> srcmask(M, std::vector<bool>(K, false));
472 for (std::size_t i = 0; i < M; ++i)
473 if (L.stations[i].nodetype == qn::NodeType::Source)
474 for (std::size_t k = 0; k < K; ++k) srcmask[i][k] = true;
476 &srcmask);
477 out.WN = Matrix<T>(M, K, zero);
478 out.CN = s.C;
479 out.XN = s.X;
480 out.method = opt_in.method;
481 out.actualmethod = opt.method;
482 out.iter = s.iter;
483 return out;
484}
485
486/** Port of `SolverBA.getBounds`: the {lower,upper} bracket of a family. */
487template <class T>
488struct BaBounds {
489 Matrix<T> Qlower, Qupper; ///< (M x K), all-NaN on a side the family lacks
491 bool has_lower = false, has_upper = false;
492 /**
493 * `getBoundsTable`'s row filter, (M x K): whether the (station, class) pair
494 * earns a row.
495 *
496 * This is the only part of `getBoundsTable` that is behaviour rather than
497 * formatting, so it is carried here and the label columns are not ported --
498 * no solver in `cpp/` has a table layer, `getAvgTable` included, and the
499 * CLI formats its own. The rule mirrors `getAvgTable`'s drop of disabled
500 * pairs but is NaN-SAFE: a row survives when any value that is present is
501 * nonzero, so an all-NaN side (a one-sided family) never removes it.
502 */
503 std::vector<std::vector<bool>> keep;
504};
505
506/**
507 * Port of `SolverBA.getBounds`.
508 *
509 * The family is the method's prefix before the first dot, and BOTH sides are
510 * re-run with the caller's full option set: constructing the re-run with only
511 * the method would reset `level` to its default and a hierarchical family would
512 * never tighten as the level is raised. A one-sided family (cub upper-only,
513 * mbjb/ldac lower-only) leaves its missing side as NaN, never as zero.
514 */
515template <class T>
517 const std::string m = resolve_method(opt.method);
518 const std::string fam = m.substr(0, m.find('.'));
519 // `solver_ba_run_analyzer` routes 'default'/'auto' to 'qrf.bas' on a blocked model,
520 // and ba_bounds has to say so rather than let the re-run below fail with
521 // the generic gate -- but it still cannot BRACKET, because the analyzer
522 // solves qrf.bas in the 'max' direction alone.
524 const std::pair<std::string, std::string> routed = blocking_default(L);
525 if (!routed.first.empty())
526 throw UnsupportedError(ErrorKind{"FiniteCapacity"},
527 "SolverBA: '" + opt.method + "' resolves to '" + routed.first +
528 "' on this model, which has a binding finite buffer, and that bound is"
529 " UPPER-only: there is no bracket to return. Call getAvg for the upper"
530 " bound, or SolverMVA with method 'sqd' for a point estimate");
531 }
532 const std::vector<std::string> valid = list_valid_methods();
533 auto listed = [&](const std::string& x) {
534 return std::find(valid.begin(), valid.end(), x) != valid.end();
535 };
536 const double nan = std::numeric_limits<double>::quiet_NaN();
537 BaBounds<T> b;
538 const std::size_t M = L.nstations, K = L.nclasses;
539 const T nanT = num_traits<T>::from_double(nan);
540 b.Qlower = Matrix<T>(M, K, nanT);
541 b.Qupper = Matrix<T>(M, K, nanT);
542 b.Tlower = Matrix<T>(M, K, nanT);
543 b.Tupper = Matrix<T>(M, K, nanT);
544 if (listed(fam + ".lower")) {
545 BaOptions o = opt;
546 o.method = fam + ".lower";
548 b.Qlower = r.QN;
549 b.Tlower = r.TN;
550 b.has_lower = true;
551 }
552 if (listed(fam + ".upper")) {
553 BaOptions o = opt;
554 o.method = fam + ".upper";
556 b.Qupper = r.QN;
557 b.Tupper = r.TN;
558 b.has_upper = true;
559 }
560 b.keep.assign(M, std::vector<bool>(K, true));
561 for (std::size_t i = 0; i < M; ++i)
562 for (std::size_t k = 0; k < K; ++k) {
563 const T v[4] = {b.Qlower(i, k), b.Qupper(i, k), b.Tlower(i, k), b.Tupper(i, k)};
564 bool any_present = false, any_nonzero = false;
565 for (const T& x : v) {
566 if (std::isnan(num_traits<T>::to_double(x))) continue;
567 any_present = true;
568 if (x != num_traits<T>::from_int(0)) any_nonzero = true;
569 }
570 b.keep[i][k] = !any_present || any_nonzero;
571 }
572 return b;
573}
574
575} // namespace ba
576} // namespace line
577
578#endif // LINE_SOLVERS_BA_SOLVER_BA_RUNNER_H
bool empty() const
Definition matrix.h:92
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
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< JobClass > classes
std::vector< Station< T > > stations
stations[k-1] is the k-th station
std::vector< NodeDef > nodes
every node, in creation order
double nclosedjobs() const
sn.nclosedjobs: the total population of the closed classes.
The method names of each solver, in listing order.
bool sn_has_blocking(const qn::NetworkStruct< T > &sn)
Defined below, after sn_is_mm1k_loss, which it exempts.
BaBounds< T > ba_bounds(const qn::NetworkStruct< T > &L, const BaOptions &opt)
Port of SolverBA.getBounds.
BaSolution< T > solver_ba_qrf_analyzer(const qn::NetworkStruct< T > &L, const BaOptions &opt)
Port of solver_ba_qrf_analyzer.
bool is_default_request(const std::string &method)
True for the aliases whose meaning a blocked model is allowed to change.
bool ignores_blocking(const std::string &method)
Whether METHOD bounds a model as if its buffers were unbounded.
std::string resolve_model_method(const qn::NetworkStruct< T > &L, const std::string &method)
Port of ba_resolve_model_method.m (SolverBA.resolveMethod): the method METHOD runs as on THIS model.
std::pair< std::string, std::string > blocking_default(const qn::NetworkStruct< T > &L)
What default/auto must mean on a model with finite buffers: the QRF BAS bound, or nothing.
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 check_method(const std::string &method)
Port of runAnalyzer's method gate: an unlisted name is refused by name.
BaSolution< T > solver_ba_spnlp_analyzer(const qn::NetworkStruct< T > &L, const BaOptions &opt)
Moment-relaxation LP bounds for a stochastic Petri net.
mva::AvgResult< T > solver_ba_run_analyzer(const qn::NetworkStruct< T > &L, const BaOptions &opt_in)
Port of @@SolverBA/runAnalyzer.m for the lang='matlab' path.
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.
std::vector< std::string > list_valid_methods()
Port of SolverBA.listValidMethods.
bool is_spnlp_method(const std::string &method)
Whether a resolved method name belongs to the Petri-net LP family.
std::string resolve_method(const std::string &method)
Port of runAnalyzer's method aliases: default is the geometric upper bound, bare auto is the AUTO com...
mam::Map< T > dist_to_map(const Distrib< T > &d)
The (D0,D1) pair that reaches sn.proc.
Matrix< T > filter_metric(const qn::NetworkStruct< T > &L, const Matrix< T > &metric, MetricKind kind, const std::vector< std::vector< bool > > *zero_mask)
Port of filterMetric: what @@NetworkSolver/getAvg does between the analyzer and the caller.
Matrix< T > sn_get_arvr_from_tput(const qn::NetworkStruct< T > &L, const Matrix< T > &TN)
Port of sn_get_arvr_from_tput: the arrival rate each station sees, from the throughputs and the class...
FeatureSet ba_feature_set(const std::string &method)
SolverBA.getFeatureSet, transcribed name for name.
void feature_gate(const std::string &solver, const FeatureSet &declared, const NetworkStruct< T > &sn, const std::string &requested_method="", const std::string &resolved_method="")
runAnalyzerChecks: refuse a model the solver does not declare, by name.
QrfBlocking sn_to_qrf_blocking(const qn::NetworkStruct< T > &sn, int Ktot, double max_vars=kQrfDefaultMaxVars)
The QRF BAS blocking tables (f, MR, BB, MM, ZZ, MM1), derived from an sn.
QrfAlpha sn_to_qrf_alpha(const qn::NetworkStruct< T > &L)
ld stays TRUE through a refusal: the model IS load dependent, and the caller has to tell "no arm serv...
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
Ports of the sn_has_* / sn_is_* predicate family of matlab/src/api/sn.
The QRF load-dependent rate scaling alpha(i,n), derived from an sn.
The QRF BAS blocking tables (f, MR, BB, MM, ZZ, MM1), derived from an sn.
Port of matlab/src/solvers/BA/solver_ba_analyzer.m, the bound-analysis handler behind SolverBA.
Port of matlab/src/solvers/BA/solver_ba_qrf_analyzer.m, the adapter that bridges the sn struct to the...
Linear-programming bounds on the mean marking and the throughputs of a stochastic timed Petri net.
The DECLARED side of the gate: one feature set per solver.
The SolverMVA class surface: @@SolverMVA/runAnalyzer.m and the gates around it.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
Port of SolverBA.getBounds: the {lower,upper} bracket of a family.
std::vector< std::vector< bool > > keep
getBoundsTable's row filter, (M x K): whether the (station, class) pair earns a row.
Matrix< T > Qupper
(M x K), all-NaN on a side the family lacks
The options SolverBA reads.
std::string method
Bound method; default resolves to gb.upper in the runner.
Class-level results, the [Q,U,R,T,C,X] of solver_ba_analyzer.
static constexpr double FineTol
Definition lang_types.h:823
A MAP as the pair of matrices (D0, D1).
Definition map_moment.h:53
Matrix< T > D0
Definition map_moment.h:54
The metrics getAvg returns, after filtering.
Matrix< T > TN
throughput
Matrix< T > RN
response time, per visit
Matrix< T > UN
utilization
Matrix< T > WN
residence time, per job
std::string method
the method asked for
std::string actualmethod
the algorithm that ran
Matrix< T > QN
queue length
std::vector< T > CN
system response time per class
std::vector< T > XN
system throughput per class
Matrix< T > AN
arrival rate
std::string msg
empty on success
The derived blocking tables, in the reference's 1-based queue indexing.