LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
solver_nc.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_NC_SOLVER_NC_H
6#define LINE_SOLVERS_NC_SOLVER_NC_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * Port of `solver_nc.m`: the load-INDEPENDENT normalizing-constant analyzer.
12 *
13 * WHAT IT COMPUTES. One evaluation of the normalizing constant G(N) settles the
14 * whole product-form network: the chain throughput is X_c = G(N - 1_c) / G(N),
15 * and the chain queue length at station i follows from a constant evaluated on
16 * the model with station i REPLICATED into a private class,
17 *
18 * Q_ic = Zms_ic X_c + Lms_ic exp(lG_ar(i,c) - lG),
19 *
20 * which is the arrival-theorem identity written in constants rather than in a
21 * recursion. That is why this file calls `pfqn_nc` once per chain and once per
22 * (station, chain) pair rather than solving anything itself: the algorithms
23 * live in `api/pfqn`, and choosing among them is `pfqn_nc`'s job.
24 *
25 * MULTISERVER IS SEIDMANN'S APPROXIMATION, NOT AN EXACT SOLVE. A station with
26 * c > 1 servers is split into a demand L/c at a single server plus a pure delay
27 * L (c-1)/c. It is exact only in the limit; the reference warns and continues,
28 * and `@@SolverNC/runAnalyzer` converts genuine multiservers to load-dependent
29 * rates BEFORE reaching here whenever the model is product-form, so this branch
30 * serves the models that have no exact load-dependent form.
31 *
32 * WHY THERE IS AN OUTER LOOP AT ALL. `SolverOptions('NC')` sets
33 * `config.highvar = 'interp'`, so a non-product-form FCFS station has its
34 * service time rescaled by `npfqn_nonexp_approx` after each pass and the
35 * analyzer re-solves until the decay rates eta stop moving. With no FCFS
36 * station the reference pins iter_max to 1 and the loop runs once.
37 *
38 * ARITHMETIC. Every measure here is formed from differences of LOGARITHMS of
39 * normalizing constants, which is the only way the ratios stay representable;
40 * there is no exact-field formulation of exp(lG' - lG), so a non-transcendental
41 * backend is refused by name rather than narrowed.
42 */
43
44#include <algorithm>
45#include <cmath>
46#include <limits>
47#include <string>
48#include <vector>
49
59#include "line/util/error.h"
61#include "line/util/matrix.h"
62
63namespace line {
64namespace nc {
65
66using lang::GlobalConstants;
68
69namespace detail {
70
71/** `sn.njobs` per chain, infinite when any class of the chain is open. */
72template <class T>
73std::vector<double> chain_population(const qn::NetworkStruct<T>& sn) {
74 std::vector<double> Nchain(sn.nchains, 0.0);
75 for (std::size_t c = 0; c < sn.nchains; ++c) {
76 double n = 0.0;
77 bool open = false;
78 for (std::size_t k : sn.inchain[c]) {
79 const double p = sn.classes[k - 1].population;
80 if (std::isinf(p)) open = true;
81 n += p;
82 }
83 Nchain[c] = open ? std::numeric_limits<double>::infinity() : n;
84 }
85 return Nchain;
86}
87
88/**
89 * The population vector `pfqn_nc` wants: integral counts, with an OPEN chain
90 * marked by a negative entry.
91 *
92 * MATLAB marks an open class by N_r = Inf and zeroes it inside pfqn_nc; the
93 * exact-capable port has no infinity and uses the sign instead, which is also
94 * the .qn interchange convention.
95 */
96inline std::vector<int> nc_population(const std::vector<double>& Nchain) {
97 std::vector<int> N(Nchain.size(), 0);
98 for (std::size_t c = 0; c < Nchain.size(); ++c)
99 N[c] = std::isinf(Nchain[c]) ? -1 : static_cast<int>(std::llround(Nchain[c]));
100 return N;
101}
102
103/**
104 * `cellsum(sn.visits)` at STATION level: the visit ratios summed over chains.
105 *
106 * The reference passes this to npfqn_nonexp_approx, which never reads it; it is
107 * built anyway so the argument list matches the reference one for one.
108 */
109template <class T>
110Matrix<T> station_visits(const qn::NetworkStruct<T>& sn) {
111 const T zero = num_traits<T>::from_int(0);
112 Matrix<T> V(sn.nstations, sn.nclasses, zero);
113 for (std::size_t c = 0; c < sn.nchains; ++c)
114 for (std::size_t i = 0; i < sn.nstations; ++i) {
115 const std::size_t sf = sn.stateful_of_station(i + 1);
116 for (std::size_t k = 0; k < sn.nclasses; ++k)
117 V(i, k) = T(V(i, k) + sn.visits[c](sf - 1, k));
118 }
119 return V;
120}
121
122/** `oner(N, r)`: one job of chain r removed. */
123inline std::vector<int> oner(std::vector<int> N, std::size_t r) {
124 if (N[r] > 0) --N[r];
125 return N;
126}
127
128/** Map `options.method` onto the pfqn dispatcher, refusing an unknown name. */
129inline pfqn::NcMethod nc_pfqn_method(const std::string& method) {
130 return pfqn::nc_method_of(method);
131}
132
133} // namespace detail
134
135/**
136 * Port of `solver_nc.m`.
137 *
138 * @param sn the refreshed struct
139 * @param opt solver controls
140 * @return the class-level measures, the log normalizing constant and the
141 * concrete algorithm the constants were computed with
142 */
143template <class T>
145 NcSolution<T> out;
146 if constexpr (!num_traits<T>::has_transcendental) {
147 (void)sn;
148 (void)opt;
149 throw UnsupportedError(ErrorKind{"Arithmetic"},
150 "solver_nc: the normalizing-constant analyzer forms X = exp(lG(N-1_c) - lG(N)) and "
151 "needs transcendental arithmetic; this backend has none");
152 } else {
153 // Krzesinski state-dependent routing: the model has its own product
154 // form (eq. 16), so it is intercepted before the convolution and MVA
155 // analyzers, which assume state-independent routing; see
156 // _kb/16-state-dependent-routing.md
157 if (!sn.sdr.empty()) return solver_nc_sdr(sn, opt);
158
159 const T zero = num_traits<T>::from_int(0);
160 const T one = num_traits<T>::from_int(1);
161 const std::size_t M = sn.nstations, K = sn.nclasses, C = sn.nchains;
162
163 std::vector<double> nservers(M, 1.0);
164 std::vector<bool> isFCFS(M, false);
165 bool anyFCFS = false;
166 for (std::size_t i = 0; i < M; ++i) {
167 nservers[i] = sn.stations[i].nservers;
168 isFCFS[i] = sn.stations[i].sched == SchedStrategy::FCFS;
169 if (isFCFS[i]) anyFCFS = true;
170 }
171
172 // LCFS paired with LCFS-PR is a two-station special case with its own
173 // closed form; nothing else in this file can represent it, since a
174 // non-preemptive LCFS station is not a BCMP station at all.
175 std::vector<std::size_t> lcfsStat, lcfsprStat;
176 for (std::size_t i = 0; i < M; ++i) {
177 if (sn.stations[i].sched == SchedStrategy::LCFS) lcfsStat.push_back(i + 1);
178 if (sn.stations[i].sched == SchedStrategy::LCFSPR) lcfsprStat.push_back(i + 1);
179 }
180 if (!lcfsStat.empty() && !lcfsprStat.empty()) {
181 if (lcfsStat.size() != 1 || lcfsprStat.size() != 1)
182 throw UnsupportedError(ErrorKind{"SchedStrategy_LCFS"},
183 "solver_nc: LCFS NC requires exactly one LCFS and one LCFS-PR station");
184 for (std::size_t c = 0; c < C; ++c) {
185 double nc_pop = 0.0;
186 for (std::size_t r : sn.inchain[c]) nc_pop += sn.classes[r - 1].population;
187 if (std::isinf(nc_pop))
188 throw UnsupportedError(ErrorKind{"OpenClass"},
189 "solver_nc: LCFS NC requires a closed queueing network");
190 }
191 // The closed form places every job on one boundary-crossing sequence,
192 // which a self-loop would let a job re-enter without crossing.
193 for (std::size_t i : {lcfsStat[0], lcfsprStat[0]}) {
194 const std::size_t sf = sn.stateful_of_station(i) - 1;
195 for (std::size_t r = 0; r < K; ++r)
196 if (sn.rt(sf * K + r, sf * K + r) > zero)
197 throw UnsupportedError(ErrorKind{"SchedStrategy_LCFS"},
198 "solver_nc: LCFS NC does not support self-loops at stations");
199 }
200 out = solver_nc_lcfsqn(sn, opt, lcfsStat[0], lcfsprStat[0]);
201 // STeff is the reference's `ST = 1./sn.rates` with NaN zeroed: an
202 // INFINITE service time (a zero rate) is kept, only a NaN is not.
203 out.STeff = Matrix<T>(M, K, zero);
204 for (std::size_t i = 0; i < M; ++i)
205 for (std::size_t r = 0; r < K; ++r) {
206 const double mu = num_traits<T>::to_double(sn.rates(i, r));
207 if (std::isnan(mu)) continue;
208 out.STeff(i, r) = mu == 0.0
210 std::numeric_limits<double>::infinity())
211 : T(one / sn.rates(i, r));
212 }
213 return out;
214 }
215 if (!lcfsStat.empty())
216 throw UnsupportedError(ErrorKind{"SchedStrategy_LCFS"},
217 "solver_nc: LCFS scheduling requires a paired LCFS-PR station");
218
219 const std::vector<double> Nchain = detail::chain_population(sn);
220 const std::vector<int> Nnc = detail::nc_population(Nchain);
221 std::vector<std::size_t> openChains, closedChains;
222 for (std::size_t c = 0; c < C; ++c)
223 (std::isinf(Nchain[c]) ? openChains : closedChains).push_back(c);
224
225 const pfqn::NcMethod pmethod = detail::nc_pfqn_method(opt.method);
226 pfqn::NcOptions popt;
227 popt.samples = opt.samples;
228 popt.seed = opt.seed;
229 popt.tol = opt.tol;
230 popt.aghq_nodes = opt.aghq_nodes;
231 popt.mcmc_batches = opt.mcmc_batches;
232 popt.mcmc_burnin = opt.mcmc_burnin;
233 const T atol = num_traits<T>::from_double(opt.tol);
234
235 // Chain aggregation, and the class-level service times the outer loop
236 // rescales. ST0 is the untouched original: npfqn_nonexp_approx is always
237 // handed the original times, never its own previous output.
239 Matrix<T> ST = d.ST, ST0 = d.ST;
240 const Matrix<T> V = detail::station_visits(sn);
241 // sn.scv with the disabled pairs marked NOT FINITE, which is what
242 // isfinite(SCV) selects on in the reference: sn.scv is NaN there, and
243 // this port carries the marker in `disabled` instead.
244 Matrix<T> SCVnan = sn.scv;
245 for (std::size_t i = 0; i < M; ++i)
246 for (std::size_t k = 0; k < K; ++k)
247 if (sn.disabled[i][k])
248 SCVnan(i, k) = num_traits<T>::from_double(
249 std::numeric_limits<double>::quiet_NaN());
250
251 std::vector<T> gamma(M, zero);
252 std::vector<T> nserv_t(M, one);
253 for (std::size_t i = 0; i < M; ++i) nserv_t[i] = num_traits<T>::from_double(nservers[i]);
254
255 std::vector<T> lambda(C, zero);
256 std::vector<T> eta(M, one), eta_1(M, zero);
257 const int iter_max = anyFCFS ? opt.iter_max : 1;
258 int it = 0;
259 double lG = 0.0;
260 std::string actualmethod = opt.method;
262
263 while (it < iter_max) {
264 // Wall-clock budget (options.timeout): the last completed pass is the
265 // interim solution, as the reference's `if it>0 && lineTimeoutExceeded`.
266 if (it > 0 && util::timeout_exceeded()) break;
267 { // the reference's max(abs(1 - eta./eta_1)) > iter_tol test
268 double dev = 0.0;
269 for (std::size_t i = 0; i < M; ++i) {
270 const double e1 = num_traits<T>::to_double(eta_1[i]);
271 const double e = num_traits<T>::to_double(eta[i]);
272 const double v = std::fabs(1.0 - e / e1);
273 if (!(v <= dev)) dev = v; // NaN and Inf both count as "not converged"
274 }
275 if (!(dev > opt.iter_tol)) break;
276 }
277 ++it;
278 eta_1 = eta;
279
280 if (it == 1) {
281 // An open chain's reference station is its Source, whose chain
282 // service time is 1 / total arrival rate.
283 for (std::size_t c = 0; c < C; ++c) {
284 if (!std::isinf(Nchain[c])) continue;
285 const std::size_t rst = sn.classes[sn.inchain[c][0] - 1].refstat;
286 if (d.STchain(rst - 1, c) != zero)
287 lambda[c] = T(one / d.STchain(rst - 1, c));
288 }
289 } else {
290 for (std::size_t c = 0; c < C; ++c)
291 for (std::size_t i = 0; i < M; ++i) {
292 T st = zero;
293 for (std::size_t k : sn.inchain[c]) st += ST(i, k - 1) * d.alpha(i, k - 1);
294 d.STchain(i, c) = st;
295 d.Lchain(i, c) = T(d.Vchain(i, c) * st);
296 }
297 }
298 for (std::size_t i = 0; i < M; ++i)
299 for (std::size_t c = 0; c < C; ++c) {
300 if (!std::isfinite(num_traits<T>::to_double(d.STchain(i, c))))
301 d.STchain(i, c) = zero;
302 if (!std::isfinite(num_traits<T>::to_double(d.Lchain(i, c))))
303 d.Lchain(i, c) = zero;
304 }
305
306 // Seidmann's approximation: a c-server station becomes a demand L/c
307 // at one server plus a pure delay L (c-1)/c.
308 Matrix<T> Lms(M, C, zero), Z(M, C, zero), Zms(M, C, zero);
309 std::vector<std::size_t> infServers;
310 for (std::size_t i = 0; i < M; ++i) {
311 if (std::isinf(nservers[i])) {
312 infServers.push_back(i);
313 for (std::size_t c = 0; c < C; ++c) Z(i, c) = d.Lchain(i, c);
314 } else {
315 const T cs = num_traits<T>::from_double(nservers[i]);
316 for (std::size_t c = 0; c < C; ++c) {
317 Lms(i, c) = T(d.Lchain(i, c) / cs);
318 Zms(i, c) = T(d.Lchain(i, c) *
319 num_traits<T>::from_double(nservers[i] - 1.0) / cs);
320 }
321 }
322 }
323 Matrix<T> Ztot(1, C, zero);
324 for (std::size_t c = 0; c < C; ++c)
325 for (std::size_t i = 0; i < M; ++i) Ztot(0, c) += Z(i, c) + Zms(i, c);
326
327 // step 1: the base constant
328 const pfqn::NcDispatchResult<T> base =
329 pfqn::pfqn_nc(lambda, Lms, Nnc, Ztot, pmethod, atol, popt);
330 if (!base.valid) {
331 // The method declined this model (the reference's empty lG).
332 // MATLAB returns every output empty here and `getAvg` renders a
333 // table of ZEROS while reporting a completed analysis; that
334 // convention is reproduced deliberately, ruled by the user on
335 // 2026-07-25 (register row N1). The consequence the ruling
336 // accepts: a caller cannot tell "no jobs" from "declined".
337 out.sol.Q = Matrix<T>(M, K, zero);
338 out.sol.U = Matrix<T>(M, K, zero);
339 out.sol.R = Matrix<T>(M, K, zero);
340 out.sol.Tp = Matrix<T>(M, K, zero);
341 out.sol.X.assign(K, zero);
342 out.sol.C.assign(K, zero);
343 out.sol.lG = 0.0;
344 out.sol.iter = 1;
345 out.sol.method = opt.method;
346 out.actualmethod = opt.method;
347 out.STeff = ST;
348 return out;
349 }
350 lG = base.lG;
351 actualmethod = base.method;
352 std::vector<T> Xchain = base.X;
353 Matrix<T> Qchain = base.Q;
354
355 // Seidmann's delay term makes the by-product measures inconsistent
356 // with the approximated model, so the reference discards them and
357 // takes the constant-ratio route below. 'mcmc' is EXEMPT: it obtains
358 // X and Q from ONE simulation of the regularized network, so
359 // discarding them would cost R + M*R further simulations to recover
360 // the same means by differencing lG. The surrogate delay is added
361 // back to Qchain in the fill branch below instead.
362 bool allZms = true;
363 for (std::size_t c = 0; c < C; ++c) {
364 T s = zero;
365 for (std::size_t i = 0; i < M; ++i) s += Zms(i, c);
366 if (!(num_traits<T>::to_double(s) > GlobalConstants::FineTol)) allZms = false;
367 }
368 if (allZms && pmethod != pfqn::NcMethod::Mcmc) {
369 Xchain.clear();
370 Qchain = Matrix<T>();
371 }
372
373 if (Xchain.empty()) {
374 Xchain = lambda;
375 Qchain = Matrix<T>(M, C, zero);
376 for (std::size_t r : closedChains) {
377 const std::vector<int> Nr = detail::oner(Nnc, r);
378 const pfqn::NcDispatchResult<T> sub =
379 pfqn::pfqn_nc(lambda, Lms, Nr, Ztot, pmethod, atol, popt);
380 if (!sub.valid) {
381 // The reference returns empty from the SUBPROBLEMS too,
382 // rather than assigning [] into a scalar slot.
383 out.sol.Q = Matrix<T>(M, K, zero);
384 out.sol.U = Matrix<T>(M, K, zero);
385 out.sol.R = Matrix<T>(M, K, zero);
386 out.sol.Tp = Matrix<T>(M, K, zero);
387 out.sol.X.assign(K, zero);
388 out.sol.C.assign(K, zero);
389 out.sol.lG = 0.0;
390 out.sol.iter = 1;
391 out.sol.method = opt.method;
392 out.actualmethod = opt.method;
393 out.STeff = ST;
394 return out;
395 }
396 const double lGr = sub.lG;
397 Xchain[r] = num_traits<T>::from_double(std::exp(lGr - lG));
398 for (std::size_t i = 0; i < M; ++i) {
399 if (!(d.Lchain(i, r) > zero)) continue;
400 if (std::isinf(nservers[i])) {
401 Qchain(i, r) = T(d.Lchain(i, r) * Xchain[r]);
402 continue;
403 }
404 // Station i replicated into a private class holding one
405 // job: the arrival theorem in normalizing-constant form.
406 Matrix<T> Lar(M, C + 1, zero);
407 std::size_t row = 0;
408 for (std::size_t i2 = 0; i2 < M; ++i2) {
409 if (i2 == i) continue;
410 for (std::size_t c = 0; c < C; ++c) Lar(row, c) = Lms(i2, c);
411 ++row;
412 }
413 for (std::size_t c = 0; c < C; ++c) Lar(row, c) = Lms(i, c);
414 Lar(row, C) = one;
415 std::vector<T> lam_ar = lambda;
416 lam_ar.push_back(zero);
417 std::vector<int> N_ar = Nr;
418 N_ar.push_back(1);
419 Matrix<T> Z_ar(1, C + 1, zero);
420 for (std::size_t c = 0; c < C; ++c) Z_ar(0, c) = Ztot(0, c);
422 pfqn::pfqn_nc(lam_ar, Lar, N_ar, Z_ar, pmethod, atol, popt);
423 // The most costly call of the whole solve, so it is the
424 // one whose algorithm is reported.
425 actualmethod = ar.method;
426 Qchain(i, r) = T(Zms(i, r) * Xchain[r] +
427 Lms(i, r) * num_traits<T>::from_double(
428 std::exp(ar.lG - lG)));
429 }
430 }
431 for (std::size_t i = 0; i < M; ++i)
432 for (std::size_t c = 0; c < C; ++c)
433 if (std::isnan(num_traits<T>::to_double(Qchain(i, c)))) Qchain(i, c) = zero;
434
435 // An open chain sees the closed population as extra load: the
436 // mixed-network queue length of Bruell and Balbo.
437 for (std::size_t r : openChains)
438 for (std::size_t i = 0; i < M; ++i) {
439 T load = zero;
440 for (std::size_t o : openChains) load += lambda[o] * d.Lchain(i, o);
441 // In an OPEN product-form network a station with c servers has the
442 // exact M/M/c marginal, split over the classes by their offered load
443 // (solver_nc.m). The mixed expression reads c only in its
444 // denominator: an open M/M/3 (lambda 1.2, mu 1) came out at 2.0
445 // against 1.294.
446 if (closedChains.empty() && std::isfinite(nservers[i]) && nservers[i] > 1.0) {
447 if (load > zero && d.Lchain(i, r) > zero) {
448 const T W = qsys::qsys_mmk(load, one, static_cast<unsigned>(nservers[i])).W;
449 Qchain(i, r) = T(W * load * lambda[r] * d.Lchain(i, r) / load);
450 } else {
451 Qchain(i, r) = zero;
452 }
453 continue;
454 }
455 const T den =
456 std::isinf(nservers[i])
457 ? one
458 : T(one - load / num_traits<T>::from_double(nservers[i]));
459 if (den == zero) continue;
460 T qc = zero;
461 for (std::size_t cc : closedChains) qc += Qchain(i, cc);
462 Qchain(i, r) = T(lambda[r] * d.Lchain(i, r) / den * (one + qc));
463 }
464 } else {
465 // The method returned the measures, so they are KEPT: only the delay
466 // stations, which it does not model, are filled in, and the population
467 // Seidmann's surrogate delay holds outside a multiserver queueing station
468 // is restored. Zms is zero at a single-server station, so the second arm
469 // is a no-op on a single-server model. Rebuilding Qchain from zero here
470 // instead would throw away the queueing-station queue lengths the method
471 // just produced, which is what the reference keeps.
472 if (Qchain.rows() != M || Qchain.cols() != C) Qchain = Matrix<T>(M, C, zero);
473 for (std::size_t c = 0; c < C; ++c)
474 for (std::size_t i = 0; i < M; ++i) {
475 if (!(d.Lchain(i, c) > zero)) continue;
476 if (std::isinf(nservers[i]))
477 Qchain(i, c) = T(d.Lchain(i, c) * Xchain[c]);
478 else if (nservers[i] > 1.0)
479 Qchain(i, c) = T(Qchain(i, c) + Zms(i, c) * Xchain[c]);
480 }
481 }
482
483 Matrix<T> Rchain(M, C, zero), Tchain(M, C, zero);
484 for (std::size_t i = 0; i < M; ++i)
485 for (std::size_t c = 0; c < C; ++c) {
486 if (Xchain[c] != zero && d.Vchain(i, c) != zero)
487 Rchain(i, c) = T(Qchain(i, c) / Xchain[c] / d.Vchain(i, c));
488 Tchain(i, c) = T(Xchain[c] * d.Vchain(i, c));
489 }
490 for (std::size_t i : infServers)
491 for (std::size_t c = 0; c < C; ++c)
492 Rchain(i, c) = d.Vchain(i, c) == zero
493 ? zero
494 : T(d.Lchain(i, c) / d.Vchain(i, c));
495
496 d.ST = ST;
498 Tchain, Xchain);
499 out.STeff = ST; // the effective service times of THIS pass
500
502 opt.highvar, isFCFS, sn.rates, ST0, V, SCVnan, cls.Tp, cls.U, gamma, nserv_t);
503 ST = na.ST;
504 gamma = na.gamma;
505 eta = na.eta;
506 }
507
508 if (it == 0)
509 throw UnsupportedError(ErrorKind{"Option"},
510 "solver_nc: the analyzer made no pass; iter_max must be at least one");
511
512 Matrix<T> Q = cls.Q, U = cls.U, R = cls.R, Tp = cls.Tp;
513 std::vector<T> X = cls.X;
514 auto sanitize = [&](Matrix<T>& A) {
515 for (std::size_t i = 0; i < A.rows(); ++i)
516 for (std::size_t j = 0; j < A.cols(); ++j) {
517 if (A(i, j) < zero) A(i, j) = T(-A(i, j));
518 if (!std::isfinite(num_traits<T>::to_double(A(i, j)))) A(i, j) = zero;
519 }
520 };
521 sanitize(Q);
522 sanitize(U);
523 sanitize(R);
524 for (T& x : X) {
525 if (x < zero) x = T(-x);
526 if (!std::isfinite(num_traits<T>::to_double(x))) x = zero;
527 }
528
529 // Renormalize: the approximations above do not conserve the closed
530 // population exactly, and a chain whose queue lengths do not add up to
531 // N would report a throughput off by the same factor.
532 for (std::size_t c = 0; c < C; ++c) {
533 if (std::isinf(Nchain[c])) continue;
534 T qden = zero;
535 for (std::size_t k : sn.inchain[c])
536 for (std::size_t i = 0; i < M; ++i) qden += Q(i, k - 1);
537 const T ratio = qden > zero
538 ? T(num_traits<T>::from_double(Nchain[c]) / qden)
539 : zero;
540 for (std::size_t k : sn.inchain[c]) {
541 X[k - 1] = T(ratio * X[k - 1]);
542 for (std::size_t i = 0; i < M; ++i) {
543 Q(i, k - 1) = T(ratio * Q(i, k - 1));
544 Tp(i, k - 1) = T(ratio * Tp(i, k - 1));
545 U(i, k - 1) = T(ratio * U(i, k - 1));
546 R(i, k - 1) = Tp(i, k - 1) == zero ? zero : T(Q(i, k - 1) / Tp(i, k - 1));
547 }
548 }
549 }
550
551 out.sol.Q = Q;
552 out.sol.U = U;
553 out.sol.R = R;
554 out.sol.Tp = Tp;
555 out.sol.X = X;
556 out.sol.C.assign(K, zero);
557 for (std::size_t k = 0; k < K; ++k) {
558 const double njobs = sn.classes[k].population;
559 if (std::isfinite(njobs) && X[k] != zero)
560 out.sol.C[k] = T(num_traits<T>::from_double(njobs) / X[k]);
561 else
562 out.sol.C[k] = cls.C[k];
563 }
564 out.sol.lG = lG;
565 out.sol.iter = it;
566 out.sol.method = actualmethod;
567 out.actualmethod = actualmethod;
568 return out;
569 }
570}
571
572} // namespace nc
573} // namespace line
574
575#endif // LINE_SOLVERS_NC_SOLVER_NC_H
std::size_t cols() const
Definition matrix.h:90
std::size_t rows() const
Definition matrix.h:89
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
The exception types the port throws.
options.timeout: one wall-clock budget per top-level solve, on a per-THREAD deadline stack.
Dense matrix and non-owning view.
The option and result types every MVA analyzer shares.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
Definition lang_types.h:194
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
NcSolution< T > solver_nc(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Port of solver_nc.m.
Definition solver_nc.h:144
NcSolution< T > solver_nc_lcfsqn(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt, std::size_t lcfsStat, std::size_t lcfsprStat)
Port of solver_nc_lcfsqn.m.
NcSolution< T > solver_nc_sdr(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Solves a network whose entry centre routes by state-dependent routing.
NonexpApproxResult< T > npfqn_nonexp_approx(const std::string &method, const std::vector< bool > &isFCFS, const Matrix< T > &rates, const Matrix< T > &ST, const Matrix< T > &V, const Matrix< T > &SCV, const Matrix< T > &Tput, const Matrix< T > &U, const std::vector< T > &gamma, const std::vector< T > &nservers)
Handler for non-exponential service and arrival processes in AMVA and NC.
NcMethod nc_method_of(const std::string &s)
Map a method name to its enum; throws UnsupportedError on an unknown one.
Definition pfqn_nc.h:177
NcMethod
The methods this port dispatches, one per compute_norm_const case.
Definition pfqn_nc.h:101
@ Mcmc
Chen-O'Cinneide regularization; supplies X and Q, never a constant.
Definition pfqn_nc.h:126
NcDispatchResult< T > pfqn_nc(const std::vector< T > &lambda, const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, NcMethod method, const T &atol, const NcOptions &nopt)
Normalizing constant of a product-form queueing network: the dispatcher.
Definition pfqn_nc.h:275
QsysResult< T > qsys_mmk(const T &lambda, const T &mu, unsigned k)
Exact mean response time of the M/M/k queue (Erlang-C).
Definition qsys_mmk.h:67
bool timeout_exceeded()
The checkpoint: true when the innermost budget has run out, marking it tripped.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Controls and result shape shared by the normalizing-constant analyzers.
A queueing network and its refreshed NetworkStruct.
Handler for non-exponential service and arrival processes in AMVA and NC.
Normalizing constant of a product-form queueing network: the dispatcher.
Exact mean response time of the M/M/k queue (Erlang-C).
Chain aggregation and de-aggregation.
The two-station LCFS + LCFS-PR closed network.
Exact product-form analysis under state-dependent routing.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
The chain-level view of a layer, as sn_get_demands_chain returns it.
Definition sn_chain.h:46
Matrix< T > ST
(M x K) class-level mean service time, 0 where disabled
Definition sn_chain.h:54
Matrix< T > alpha
(M x K) class share of its chain's visits at a station
Definition sn_chain.h:50
Matrix< T > STchain
(M x C) mean service time
Definition sn_chain.h:48
Matrix< T > Lchain
(M x C) demand
Definition sn_chain.h:47
Matrix< T > Vchain
(M x C) visits
Definition sn_chain.h:49
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
static constexpr double FineTol
Definition lang_types.h:823
The [Q,U,R,T,C,X,lG] of the reference, plus the algorithm that ran.
Definition nc_types.h:134
mva::MvaSolution< T > sol
Definition nc_types.h:135
Matrix< T > STeff
the service times of the last pass, MATLAB's STeff
Definition nc_types.h:137
std::string actualmethod
Definition nc_types.h:136
Controls, defaulting to SolverOptions('NC') in the reference.
Definition nc_types.h:35
Return value, mirroring MATLAB's [ST,gamma,nservers,rho,scva,scvs,eta].
Matrix< T > ST
(M x R) scaled service times
std::vector< T > gamma
(M) multiserver asymptotic decay rate
std::vector< T > eta
(M) diffusion decay rate
std::vector< T > X
(R) per-class throughput, empty when not produced
Definition pfqn_nc.h:244
std::string method
the algorithm actually used
Definition pfqn_nc.h:246
Matrix< T > Q
(M x R) queue lengths, empty when not produced
Definition pfqn_nc.h:245
bool valid
False when the METHOD DECLINED THE MODEL, which is the reference's lG = []: pana outside normal usage...
Definition pfqn_nc.h:259
double lG
its logarithm
Definition pfqn_nc.h:243
The options fields compute_norm_const reads beyond the method itself.
Definition pfqn_nc.h:215
double mcmc_burnin
options.config.mcmc_burnin: warm-up fraction pfqn_mcmc discards
Definition pfqn_nc.h:226
unsigned long seed
SolverOptions('NC').seed.
Definition pfqn_nc.h:217
std::size_t mcmc_batches
options.config.mcmc_batches: batches pfqn_mcmc splits its run into
Definition pfqn_nc.h:224
std::size_t aghq_nodes
options.config.aghq_nodes: nodes per simplex direction of the adaptive Gauss-Hermite rule.
Definition pfqn_nc.h:222
std::size_t samples
SolverOptions('NC').samples.
Definition pfqn_nc.h:216
double tol
handed to pfqn_comomrm
Definition pfqn_nc.h:218