LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
solver_mam_bgchain.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_MAM_SOLVER_MAM_BGCHAIN_H
6#define LINE_SOLVERS_MAM_SOLVER_MAM_BGCHAIN_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * Port of `solver_mam_bgchain.m` and its three helpers: the analyzer that
12 * treats the CLOSED classes as a background modulating chain and the OPEN
13 * classes as matrix-analytic queues driven by it. A purely CLOSED model is the
14 * degenerate case of the same construction -- with no open work to take a share
15 * of the servers the chain alone answers, and it answers with the EXACT closed
16 * CTMC at chain granularity -- so only a purely OPEN model is refused.
17 *
18 * WHY IT EXISTS. `dec.source` replaces a closed chain by a Poisson surrogate at
19 * the current throughput iterate, which is exactly the part of a mixed model it
20 * has least information about; measured against SolverCTMC that costs it 10-24%
21 * on the queue lengths. But the closed population vector is the one part of a
22 * mixed model whose state space is BOUNDED, so it can be solved exactly. This
23 * method does that, and hands each open station a station-local Markovian
24 * environment read off the chain, turning it into a level-dependent QBD whose
25 * phase carries the number of closed jobs competing for its server.
26 *
27 * 1 background chain the closed population vector over the stations the
28 * closed classes visit (mam_bgchain_ctmc)
29 * 2 environment that chain lumped onto the closed occupancy of ONE
30 * station (mam_bgchain_env)
31 * 3 open station a MAP/PH/c queue modulated by that environment,
32 * solved as a level-dependent QBD (mam_bgchain_station)
33 * 4 fixed point the capacity share feeds step 1 and closes
34 *
35 * TAGGED-CLASS ITERATION. Step 1 is a population process of dimension (closed
36 * chains) x (stations), so its state space is exponential in the number of
37 * closed chains R. The method therefore keeps ONE chain free at a time: the
38 * tagged chain r is carried exactly, the other R-1 collapse into flow-equivalent
39 * aggregate classes whose population is their total and whose service time and
40 * routing at each station are their throughput-weighted means
41 * (Chandy-Herzog-Woo). Each chain takes its turn as the tagged one and reads its
42 * own metrics off the chain it is exact in; the open results are averaged over
43 * the passes.
44 *
45 * HOW MUCH TO AGGREGATE is options.config.bgaggr, the number G of aggregate
46 * classes; the background chain then carries 1 + G. G = 1 is the classic
47 * tagged/aggregate pair and the default, so the chain stays two-class whatever R
48 * is; G >= R-1 aggregates nothing, carries every closed chain exactly, and answers
49 * in ONE pass instead of solving the same chain R times. Passing R reaches that, so
50 * asking for no aggregation needs no magic value. The cost is the state space, the
51 * product over the 1 + G classes of nchoosek(N_b + Mc - 1, Mc - 1), capped by
52 * bgstates_max.
53 *
54 * WHICH CHAINS SHARE A GROUP is decided by similarity of per-station SERVICE
55 * DEMAND. An aggregate carries the flow-weighted mean of its members' service times
56 * and routing, so it is exact when they place the same demand at every station and
57 * distorts in proportion to how far apart they are; grouping the demand-similar
58 * chains together keeps the aggregation where it is harmless and away from the
59 * chains it would misrepresent.
60 *
61 * THE EXCHANGED QUANTITY IS THE SHARE, NOT THE MEAN OCCUPANCY. The two halves
62 * iterate on `cshare(i,e) = E[min(e+k,c) e/(e+k)]`, the mean number of servers
63 * of station i that its e closed jobs hold, averaged over the open occupancy k.
64 * Exchanging the mean open occupancy instead and rebuilding the share from it is
65 * what a first cut does and it is WRONG: e/(e+k) is convex in k, so Jensen
66 * biases the closed service rate down and the closed throughput with it. The
67 * environment generator inside the QBD is level-dependent for the same reason.
68 *
69 * EXACTNESS, measured against SolverCTMC and exact MVA on mixed models of two to
70 * four stations: PS or INF with ANY service law (exponential, Erlang, HyperExp,
71 * Coxian), any number of servers, Poisson or MAP arrivals and one to four closed
72 * chains agree to 4-5 significant digits, as does FCFS with class-INDEPENDENT
73 * rates. FCFS with class-DEPENDENT rates keeps the closed queue lengths within
74 * ~1% while the open queue length reads 14-20% low, the server being held here
75 * in random order rather than head-of-line.
76 *
77 * PS IS INSENSITIVE to the service law beyond its mean, and the method honours
78 * that rather than approximating it: at a PS station the open service is
79 * replaced by the exponential of the same mean before the QBD is built. This QBD
80 * tracks ONE service phase for the whole station, so carrying the phase-type
81 * there makes the open queue length inherit the SCV-sensitivity of an M/PH/1
82 * FCFS queue -- measured, a HyperExp of SCV 4 read 21% high where the exact
83 * answer is the exponential one to five digits. At an FCFS station the service
84 * law IS carried, collapsed into one phase-type process scaled by the share (the
85 * same collapse `solver_mam_ldqbd` documents), and the background chain reads
86 * only the MEAN closed service time.
87 *
88 * ARITHMETIC. The path runs `ctmc_solve` on the background chain and the
89 * level-dependent QBD recursion on each station, the latter falling back to a
90 * pseudo-inverse on a singular level, so it is gated on transcendental
91 * arithmetic exactly as `solver_mam_ldqbd` is.
92 */
93
94#include <algorithm>
95#include <cmath>
96#include <cstddef>
97#include <limits>
98#include <map>
99#include <string>
100#include <vector>
101
102#include "line/api/mam/ldqbd.h"
111#include "line/lang/qn/state.h"
115#include "line/util/error.h"
117#include "line/util/linalg.h"
118#include "line/util/matrix.h"
119
120namespace line {
121namespace mam {
122
123namespace bgchain_detail {
124
125/**
126 * Block of one background class: the ways to place N jobs over the m stations
127 * that class visits.
128 *
129 * This is `qn::space_closed_single`, the lattice primitive the CTMC solver
130 * enumerates a closed population over, so the row order and the row count are
131 * the reference's rather than this file's; only the support differs, being the
132 * class's own stations rather than all of them.
133 */
134inline std::vector<std::vector<int>> closed_block(std::size_t m, int N) {
135 // The primitive is templated on the numeric type of a state row and
136 // `num_traits` is specialised for the field types only, so it is taken at
137 // double and rounded back, as the JAR twin does off its Matrix.
138 const std::vector<std::vector<double>> rows =
139 qn::space_closed_single<double>(m, static_cast<std::size_t>(N));
140 std::vector<std::vector<int>> out(rows.size(), std::vector<int>(m, 0));
141 for (std::size_t r = 0; r < rows.size(); ++r)
142 for (std::size_t t = 0; t < m; ++t)
143 out[r][t] = static_cast<int>(std::lround(rows[r][t]));
144 return out;
145}
146
147/**
148 * Share of the server capacity that k open jobs hold when e closed jobs are also
149 * present: min(k+e,c) busy servers times the open fraction k/(k+e).
150 */
151inline double open_share(double k, double e, double c) {
152 const double tot = k + e;
153 if (!(tot > 0.0)) return 0.0;
154 return std::min(tot, c) * (k / tot);
155}
156
157/** The mirror image of open_share, for the closed jobs. */
158inline double closed_share(double e, double k, double c) {
159 const double tot = e + k;
160 if (!(tot > 0.0)) return 0.0;
161 return std::min(tot, c) * (e / tot);
162}
163
164/**
165 * Group the columns of D into G clusters by per-station SERVICE DEMAND
166 * (mam_bgchain_groups.m).
167 *
168 * WHY DEMAND IS THE RIGHT CRITERION. The aggregate that replaces a group carries
169 * the flow-weighted mean of its members' service times and routing, so the group
170 * aggregates EXACTLY when its members place the same demand at every station and
171 * distorts both quantities in proportion to how far apart they are. The distance
172 * is the symmetric relative L1 gap between the demand vectors,
173 *
174 * dist(a,b) = sum_i |D[i][a] - D[i][b]| / ((sum_i D[i][a] + sum_i D[i][b])/2),
175 *
176 * scale-relative rather than absolute: it separates two chains whose demand
177 * PROFILE across the stations differs and two whose profile agrees but whose
178 * magnitude does not, and being dimensionless it groups a model the same way
179 * whatever its time unit.
180 *
181 * WHY COMPLETE LINKAGE. Agglomerative from singletons, merging the pair of
182 * clusters whose WORST member-to-member distance is smallest. The aggregation
183 * error inside a group is driven by its worst mismatch, not its average one.
184 *
185 * DETERMINISM. Ties break on the lexicographically smallest pair of cluster
186 * indices and the groups are relabelled by their smallest member, so the same
187 * input gives the same grouping in MATLAB, the JAR, Python and C++.
188 *
189 * @param D (Mc x n) per-station demand, one column per chain
190 * @param G number of groups wanted, clamped to [1, n]
191 * @return group index in 0..G-1 of each chain
192 */
193inline std::vector<std::size_t> mam_bgchain_groups(const std::vector<std::vector<double>>& D,
194 std::size_t G) {
195 const std::size_t Mc = D.size();
196 const std::size_t n = (Mc == 0) ? 0 : D[0].size();
197 std::vector<std::size_t> grp(n, 0);
198 if (n == 0) return grp;
199 if (G < 1) G = 1;
200 if (G > n) G = n;
201
202 std::vector<double> tot(n, 0.0);
203 for (std::size_t c = 0; c < n; ++c)
204 for (std::size_t i = 0; i < Mc; ++i) tot[c] += D[i][c];
205 std::vector<std::vector<double>> dist(n, std::vector<double>(n, 0.0));
206 for (std::size_t a = 0; a < n; ++a) {
207 for (std::size_t b = a + 1; b < n; ++b) {
208 const double den = (tot[a] + tot[b]) / 2.0;
209 double d = 0.0;
210 if (den > 1e-14) {
211 double acc = 0.0;
212 for (std::size_t i = 0; i < Mc; ++i) acc += std::fabs(D[i][a] - D[i][b]);
213 d = acc / den;
214 }
215 dist[a][b] = d;
216 dist[b][a] = d;
217 }
218 }
219
220 std::vector<std::vector<std::size_t>> clusters(n);
221 for (std::size_t a = 0; a < n; ++a) clusters[a].push_back(a);
222 std::vector<bool> active(n, true);
223 std::size_t nactive = n;
224 while (nactive > G) {
225 double best = std::numeric_limits<double>::infinity();
226 long bp = -1, bq = -1;
227 for (std::size_t p = 0; p < n; ++p) {
228 if (!active[p]) continue;
229 for (std::size_t q = p + 1; q < n; ++q) {
230 if (!active[q]) continue;
231 double d = 0.0;
232 for (std::size_t x : clusters[p])
233 for (std::size_t y : clusters[q])
234 if (dist[x][y] > d) d = dist[x][y];
235 if (d < best - 1e-14) {
236 best = d;
237 bp = static_cast<long>(p);
238 bq = static_cast<long>(q);
239 }
240 }
241 }
242 if (bp < 0) break;
243 std::vector<std::size_t>& cp = clusters[static_cast<std::size_t>(bp)];
244 std::vector<std::size_t>& cq = clusters[static_cast<std::size_t>(bq)];
245 cp.insert(cp.end(), cq.begin(), cq.end());
246 std::sort(cp.begin(), cp.end());
247 cq.clear();
248 active[static_cast<std::size_t>(bq)] = false;
249 --nactive;
250 }
251
252 // Relabel by smallest member, so the group numbering is canonical
253 std::vector<std::size_t> live;
254 for (std::size_t p = 0; p < n; ++p)
255 if (active[p]) live.push_back(p);
256 std::sort(live.begin(), live.end(), [&](std::size_t a, std::size_t b) {
257 return clusters[a][0] < clusters[b][0];
258 });
259 for (std::size_t g = 0; g < live.size(); ++g)
260 for (std::size_t x : clusters[live[g]]) grp[x] = g;
261 return grp;
262}
263
264} // namespace bgchain_detail
265
266/** The solved background modulating chain. */
267template <class T>
269 /** space[s][i][b]: class-b jobs held by station i in state s. */
270 std::vector<std::vector<std::vector<int>>> space;
271 /** totocc[s][i]: closed jobs of any background class held by station i. */
272 std::vector<std::vector<int>> totocc;
273 std::vector<T> pi;
275 std::vector<std::vector<T>> QLen, Tput, Ubusy; ///< (Mc x B)
276 std::size_t nstates = 0;
277};
278
279/**
280 * Build and solve the background chain (mam_bgchain_ctmc.m).
281 *
282 * A station holding e closed jobs serves background class b at rate
283 * `n[i][b] / STb[i][b]` when it is an infinite server, and
284 * `cshare[i][e] * (n[i][b]/e) / STb[i][b]` otherwise, splitting the capacity the
285 * closed jobs hold over the background classes in proportion to their counts.
286 * That is exact under PS and is the random-order surrogate under FCFS.
287 *
288 * @param Nb population of each background class, size B in {1,2}
289 * @param STb (Mc x B) mean service time per station per background class
290 * @param Pb B row-stochastic (Mc x Mc) routing matrices
291 * @param isinf_i whether each station of the support is an infinite server
292 * @param nsrv servers of each station of the support
293 * @param cshare (Mc x (Nmax+1)) mean servers the e closed jobs hold
294 * @param supp supp[i][b]: station i is on the route of background class b.
295 * NOT an optimization -- a chain that never visits a station
296 * cannot hold jobs there, and enumerating the union of every
297 * chain's stations puts probability on unreachable configurations
298 * that also ABSORB, because the chain's routing matrix has a zero
299 * row at an unvisited station which row-normalizes to a self-loop.
300 * The generator turns reducible and population conservation
301 * silently fails
302 * @param states_max cap on the number of chain states
303 */
304template <class T>
305BgchainCtmc<T> mam_bgchain_ctmc(const std::vector<int>& Nb,
306 const std::vector<std::vector<T>>& STb,
307 const std::vector<Matrix<T>>& Pb,
308 const std::vector<bool>& isinf_i,
309 const std::vector<double>& nsrv,
310 const std::vector<std::vector<double>>& cshare,
311 const std::vector<std::vector<bool>>& supp,
312 std::size_t states_max) {
313 const T zero = num_traits<T>::from_int(0);
314 const std::size_t Mc = STb.size();
315 const std::size_t B = Nb.size();
316
317 // Each class is enumerated over ITS OWN stations only; see the supp param.
318 std::vector<std::vector<std::vector<int>>> sp(B); // expanded to Mc columns
319 std::vector<std::vector<std::vector<int>>> compb(B); // reduced to the class's stations
320 std::vector<std::vector<std::size_t>> idxb(B);
321 std::vector<std::size_t> nst(B);
322 std::size_t nstates = 1;
323 for (std::size_t b = 0; b < B; ++b) {
324 for (std::size_t i = 0; i < Mc; ++i)
325 if (supp.empty() || supp[i][b]) idxb[b].push_back(i);
326 if (idxb[b].empty()) {
327 if (Nb[b] > 0)
328 throw UnsupportedError(ErrorKind{"Model"}, "mam_bgchain_ctmc: background class " + std::to_string(b + 1) +
329 " holds " + std::to_string(Nb[b]) +
330 " jobs but visits no station");
331 idxb[b].push_back(0); // an empty class still needs one slot
332 }
333 compb[b] = bgchain_detail::closed_block(idxb[b].size(), Nb[b]);
334 sp[b].assign(compb[b].size(), std::vector<int>(Mc, 0));
335 for (std::size_t r = 0; r < compb[b].size(); ++r)
336 for (std::size_t t = 0; t < idxb[b].size(); ++t)
337 sp[b][r][idxb[b][t]] = compb[b][r][t];
338 nst[b] = compb[b].size();
339 nstates *= nst[b];
340 }
341 if (nstates > states_max)
342 throw UnsupportedError(ErrorKind{"StateSpace"},
343 "mam_bgchain_ctmc: the background chain of this model has " + std::to_string(nstates) +
344 " states, above the limit of " + std::to_string(states_max) +
345 ". The chain enumerates the closed-class population vector over the " +
346 std::to_string(Mc) +
347 " stations the closed classes visit, so its size grows as nchoosek(N+Mc-1,Mc-1) per "
348 "class, and it carries " + std::to_string(B) +
349 " classes. Lower options.config.bgaggr to aggregate more of the closed chains into "
350 "fewer classes, raise options.config.bgstates_max to solve it anyway, or reduce the "
351 "closed populations.");
352
353 // Per-class transition targets: tgt[b][s][i*Mc+j] is the class-b state
354 // reached from s when one job moves from station i to station j, or -1.
355 std::vector<std::vector<std::vector<long>>> tgt(B);
356 for (std::size_t b = 0; b < B; ++b) {
357 const std::size_t mb = idxb[b].size();
358 std::map<std::vector<int>, long> index;
359 for (std::size_t s = 0; s < nst[b]; ++s) index[compb[b][s]] = static_cast<long>(s);
360 tgt[b].assign(nst[b], std::vector<long>(Mc * Mc, -1));
361 for (std::size_t s = 0; s < nst[b]; ++s) {
362 for (std::size_t ii = 0; ii < mb; ++ii) {
363 if (compb[b][s][ii] == 0) continue;
364 for (std::size_t jj = 0; jj < mb; ++jj) {
365 if (jj == ii) continue;
366 std::vector<int> cand = compb[b][s];
367 --cand[ii];
368 ++cand[jj];
369 const auto it = index.find(cand);
370 if (it != index.end())
371 tgt[b][s][idxb[b][ii] * Mc + idxb[b][jj]] = it->second;
372 }
373 }
374 }
375 }
376
377 // Joint space, class 0 outermost.
378 std::vector<std::size_t> strideb(B, 1);
379 for (std::size_t b = 0; b < B; ++b) {
380 std::size_t s = 1;
381 for (std::size_t b2 = b + 1; b2 < B; ++b2) s *= nst[b2];
382 strideb[b] = s;
383 }
384 BgchainCtmc<T> out;
385 out.nstates = nstates;
386 out.space.assign(nstates, std::vector<std::vector<int>>(Mc, std::vector<int>(B, 0)));
387 out.totocc.assign(nstates, std::vector<int>(Mc, 0));
388 std::vector<std::vector<std::size_t>> subidx(nstates, std::vector<std::size_t>(B, 0));
389 for (std::size_t s = 0; s < nstates; ++s) {
390 std::size_t rem = s;
391 for (std::size_t bb = B; bb-- > 0;) {
392 subidx[s][bb] = rem % nst[bb];
393 rem /= nst[bb];
394 }
395 for (std::size_t b = 0; b < B; ++b) {
396 const std::vector<int>& vec = sp[b][subidx[s][b]];
397 for (std::size_t i = 0; i < Mc; ++i) {
398 out.space[s][i][b] = vec[i];
399 out.totocc[s][i] += vec[i];
400 }
401 }
402 }
403
404 std::vector<std::vector<T>> mu(Mc, std::vector<T>(B, zero));
405 for (std::size_t b = 0; b < B; ++b)
406 for (std::size_t i = 0; i < Mc; ++i)
407 if (num_traits<T>::to_double(STb[i][b]) > 0.0)
408 mu[i][b] = T(num_traits<T>::from_int(1) / STb[i][b]);
409
410 Matrix<T> Q(nstates, nstates, zero);
411 std::vector<std::vector<std::vector<T>>> rate_full(
412 nstates, std::vector<std::vector<T>>(Mc, std::vector<T>(B, zero)));
413 std::vector<std::vector<T>> cap_busy(nstates, std::vector<T>(Mc, zero));
414 for (std::size_t s = 0; s < nstates; ++s) {
415 for (std::size_t i = 0; i < Mc; ++i) {
416 const int eclosed = out.totocc[s][i];
417 if (eclosed == 0) continue;
418 double held_d;
419 if (isinf_i[i]) {
420 held_d = static_cast<double>(eclosed);
421 } else {
422 const std::size_t ecap =
423 std::min<std::size_t>(static_cast<std::size_t>(eclosed), cshare[i].size() - 1);
424 held_d = cshare[i][ecap];
425 }
426 if (!(held_d > 0.0)) continue;
427 const T held = num_traits<T>::from_double(held_d);
428 cap_busy[s][i] = held;
429 for (std::size_t b = 0; b < B; ++b) {
430 if (out.space[s][i][b] == 0 || mu[i][b] == zero) continue;
431 const T frac = num_traits<T>::from_double(static_cast<double>(out.space[s][i][b]) /
432 static_cast<double>(eclosed));
433 const T r = T(held * frac * mu[i][b]);
434 rate_full[s][i][b] = r;
435 for (std::size_t j = 0; j < Mc; ++j) {
436 if (j == i) continue;
437 if (!(num_traits<T>::to_double(Pb[b](i, j)) > 0.0)) continue;
438 const long tsub = tgt[b][subidx[s][b]][i * Mc + j];
439 if (tsub < 0) continue;
440 const std::size_t sdest =
441 s + (static_cast<std::size_t>(tsub) - subidx[s][b]) * strideb[b];
442 Q(s, sdest) += T(r * Pb[b](i, j));
443 }
444 }
445 }
446 }
447
448 out.Q = mc::ctmc_makeinfgen(Q);
449 if (nstates == 1) {
450 out.pi.assign(1, num_traits<T>::from_int(1));
451 } else {
452 out.pi = mc::ctmc_solve(out.Q);
453 }
454 T tot = zero;
455 for (T& v : out.pi) {
456 if (num_traits<T>::to_double(v) < 0.0) v = zero;
457 tot += v;
458 }
459 if (tot != zero)
460 for (T& v : out.pi) v /= tot;
461
462 out.QLen.assign(Mc, std::vector<T>(B, zero));
463 out.Tput.assign(Mc, std::vector<T>(B, zero));
464 out.Ubusy.assign(Mc, std::vector<T>(B, zero));
465 for (std::size_t s = 0; s < nstates; ++s) {
466 const T p = out.pi[s];
467 if (p == zero) continue;
468 for (std::size_t i = 0; i < Mc; ++i)
469 for (std::size_t b = 0; b < B; ++b) {
470 out.QLen[i][b] += T(p * num_traits<T>::from_int(out.space[s][i][b]));
471 out.Tput[i][b] += T(p * rate_full[s][i][b]);
472 }
473 }
474 for (std::size_t i = 0; i < Mc; ++i) {
475 if (isinf_i[i]) {
476 out.Ubusy[i] = out.QLen[i];
477 continue;
478 }
479 for (std::size_t s = 0; s < nstates; ++s) {
480 const T p = out.pi[s];
481 const int occ = out.totocc[s][i];
482 if (p == zero || occ == 0) continue;
483 for (std::size_t b = 0; b < B; ++b) {
484 const T share = num_traits<T>::from_double(
485 static_cast<double>(out.space[s][i][b]) / static_cast<double>(occ));
486 out.Ubusy[i][b] +=
487 T(p * cap_busy[s][i] * share / num_traits<T>::from_double(nsrv[i]));
488 }
489 }
490 }
491 return out;
492}
493
494/** The environment one station sees: the background chain lumped onto its occupancy. */
495template <class T>
498 std::vector<T> phi;
499 std::vector<int> esup; ///< ascending
500};
501
502/**
503 * Lump the background chain onto the closed occupancy of station i
504 * (mam_bgchain_env.m).
505 *
506 * Station i does not observe the whole closed population vector, only how many
507 * closed jobs compete with the open ones for its server. The lumped generator is
508 * the stationary-weighted aggregation of Q over the level sets
509 * {s : totocc(s,i) = e}, exact when the partition is lumpable in the
510 * Kemeny-Snell sense and the standard exact-aggregation approximation otherwise.
511 * The diagonal is rebuilt from the off-diagonal row sums, so the result is a
512 * proper generator whatever the lumping error is. Environment states of zero
513 * stationary probability are unreachable and are dropped.
514 */
515template <class T>
517 const T zero = num_traits<T>::from_int(0);
518 const std::size_t n = bg.nstates;
519
520 std::vector<int> levels;
521 for (std::size_t s = 0; s < n; ++s) levels.push_back(bg.totocc[s][i]);
522 std::sort(levels.begin(), levels.end());
523 levels.erase(std::unique(levels.begin(), levels.end()), levels.end());
524
525 std::vector<T> wAll(levels.size(), zero);
526 for (std::size_t s = 0; s < n; ++s) {
527 const std::size_t pos = static_cast<std::size_t>(
528 std::lower_bound(levels.begin(), levels.end(), bg.totocc[s][i]) - levels.begin());
529 wAll[pos] += bg.pi[s];
530 }
531
533 std::vector<T> w;
534 for (std::size_t e = 0; e < levels.size(); ++e) {
535 if (num_traits<T>::to_double(wAll[e]) > 1e-14) {
536 env.esup.push_back(levels[e]);
537 w.push_back(wAll[e]);
538 }
539 }
540 if (env.esup.empty()) {
541 // degenerate chain: the station never holds a closed job
542 env.A = Matrix<T>(1, 1, zero);
543 env.phi.assign(1, num_traits<T>::from_int(1));
544 env.esup.assign(1, 0);
545 return env;
546 }
547
548 const std::size_t me = env.esup.size();
549 std::vector<long> lvl(n, -1);
550 for (std::size_t s = 0; s < n; ++s) {
551 const auto it = std::lower_bound(env.esup.begin(), env.esup.end(), bg.totocc[s][i]);
552 if (it != env.esup.end() && *it == bg.totocc[s][i])
553 lvl[s] = static_cast<long>(it - env.esup.begin());
554 }
555
556 env.A = Matrix<T>(me, me, zero);
557 if (me > 1) {
558 for (std::size_t s = 0; s < n; ++s) {
559 if (lvl[s] < 0 || bg.pi[s] == zero) continue;
560 for (std::size_t sp = 0; sp < n; ++sp) {
561 if (sp == s || lvl[sp] < 0 || lvl[sp] == lvl[s]) continue;
562 const T q = bg.Q(s, sp);
563 if (q == zero) continue;
564 env.A(static_cast<std::size_t>(lvl[s]), static_cast<std::size_t>(lvl[sp])) +=
565 T(bg.pi[s] * q);
566 }
567 }
568 for (std::size_t e = 0; e < me; ++e) {
569 T diag = zero;
570 for (std::size_t ep = 0; ep < me; ++ep) {
571 if (ep == e) continue;
572 env.A(e, ep) /= w[e];
573 diag += env.A(e, ep);
574 }
575 env.A(e, e) = T(-diag);
576 }
577 }
578 T wsum = zero;
579 for (const T& v : w) wsum += v;
580 env.phi.assign(me, zero);
581 for (std::size_t e = 0; e < me; ++e) env.phi[e] = T(w[e] / wsum);
582 return env;
583}
584
585/** What one modulated station QBD returns. */
586template <class T>
589 std::vector<T> penv;
590 std::vector<double> cshare; ///< E[min(e+k,c) e/(e+k) | e], indexed by esup
591 std::vector<int> esup;
592};
593
594/**
595 * Solve the open classes of one station as a modulated level-dependent QBD
596 * (mam_bgchain_station.m).
597 *
598 * Level = number of open jobs held by the station, phase = (arrival MAP phase,
599 * environment state, service phase). With k open and e closed jobs present the
600 * open aggregate completes at rate `min(k+e,c) k/(k+e)` times the phase-type
601 * completion rate of one busy server: the dependence on k makes the QBD
602 * level-dependent, the dependence on e makes it modulated.
603 *
604 * The environment is level-dependent too. A lumped transition that LOWERS the
605 * closed occupancy is a closed completion here, so it carries the closed share
606 * and level k rescales it by the ratio to the averaged share `gref` the chain
607 * was built at; a transition that RAISES the occupancy is an arrival from
608 * elsewhere and is left alone. Without this the closed jobs would drain at their
609 * mean-field rate however long the open queue is, and the positive correlation
610 * between the two occupancies would be lost.
611 *
612 * The level space is truncated at Kmax. An arrival at the top level is lost but
613 * still advances the arrival phase, so the arrival process keeps its exact
614 * marginal and autocorrelation and only the queue tail is cut.
615 */
616template <class T>
618 const std::vector<T>& alpha_s, const Matrix<T>& Tsvc,
619 const Matrix<T>& Ain, const std::vector<int>& esup_in,
620 double nservers, const std::vector<double>& gref_in,
621 std::size_t Kmax) {
622 const T zero = num_traits<T>::from_int(0);
623 const T one = num_traits<T>::from_int(1);
624 const std::size_t ma = Da0.rows();
625 const std::size_t me = esup_in.size();
626 const std::size_t ms = alpha_s.size();
627 if (Kmax < 1) Kmax = 1;
628
629 // esup arrives ascending from mam_bgchain_env; gref is indexed to match.
630 const std::vector<int>& esup = esup_in;
631 const std::vector<double>& gref = gref_in;
632
633 Matrix<T> tvec(ms, 1, zero);
634 for (std::size_t i = 0; i < ms; ++i) {
635 T s = zero;
636 for (std::size_t j = 0; j < ms; ++j) s += Tsvc(i, j);
637 tvec(i, 0) = T(-s);
638 }
639 Matrix<T> alphaRow(1, ms, zero);
640 for (std::size_t j = 0; j < ms; ++j) alphaRow(0, j) = alpha_s[j];
641
642 Matrix<T> Ime(me, me, zero), Ima(ma, ma, zero), Ims(ms, ms, zero);
643 for (std::size_t i = 0; i < me; ++i) Ime(i, i) = one;
644 for (std::size_t i = 0; i < ma; ++i) Ima(i, i) = one;
645 for (std::size_t i = 0; i < ms; ++i) Ims(i, i) = one;
646
647 // Split the environment into the closed departures from this station (which
648 // the open level throttles) and the arrivals to it (which it does not).
649 Matrix<T> Adown(me, me, zero), Aup(me, me, zero);
650 for (std::size_t e = 0; e < me; ++e)
651 for (std::size_t ep = 0; ep < me; ++ep) {
652 if (ep < e) Adown(e, ep) = Ain(e, ep);
653 else if (ep > e) Aup(e, ep) = Ain(e, ep);
654 }
655
656 auto env_at_level = [&](std::size_t k) {
657 Matrix<T> Ak(me, me, zero);
658 for (std::size_t e = 0; e < me; ++e) {
659 const double g = bgchain_detail::closed_share(static_cast<double>(esup[e]),
660 static_cast<double>(k), nservers);
661 const double ratio = (gref[e] > 0.0) ? g / gref[e] : 1.0;
662 const T rt = num_traits<T>::from_double(ratio);
663 T diag = zero;
664 for (std::size_t ep = 0; ep < me; ++ep) {
665 if (ep == e) continue;
666 const T v = T(Aup(e, ep) + rt * Adown(e, ep));
667 Ak(e, ep) = v;
668 diag += v;
669 }
670 Ak(e, e) = T(-diag);
671 }
672 return Ak;
673 };
674 auto diagm = [&](const std::vector<T>& v) {
675 Matrix<T> D(v.size(), v.size(), zero);
676 for (std::size_t i = 0; i < v.size(); ++i) D(i, i) = v[i];
677 return D;
678 };
679
680 // The C++ ldqbd indexes q0/q1/q2 BY LEVEL, with q2[0] unused, unlike the
681 // reference's 1-based cell arrays.
682 std::vector<Matrix<T>> Q0(Kmax), Q1(Kmax + 1), Q2(Kmax + 1);
683 std::vector<std::vector<T>> phiae(Kmax);
684
685 Q1[0] = qbd_detail::madd(kron(Da0, Ime), kron(Ima, env_at_level(0)));
686 Q0[0] = kron(kron(Da1, Ime), alphaRow);
687
688 const Matrix<T> Da0kron = kron(kron(Da0, Ime), Ims);
689 const Matrix<T> Da1kron = kron(kron(Da1, Ime), Ims);
690 Q2[0] = Matrix<T>(1, 1, zero); // unused
691 for (std::size_t k = 1; k <= Kmax; ++k) {
692 std::vector<T> rep(ma * me, zero);
693 for (std::size_t a = 0; a < ma; ++a)
694 for (std::size_t e = 0; e < me; ++e)
695 rep[a * me + e] = num_traits<T>::from_double(bgchain_detail::open_share(
696 static_cast<double>(k), static_cast<double>(esup[e]), nservers));
697 phiae[k - 1] = rep;
698 const Matrix<T> Ak = env_at_level(k);
699 Q1[k] = qbd_detail::madd(
700 qbd_detail::madd(Da0kron, kron(kron(Ima, Ak), Ims)), kron(diagm(rep), Tsvc));
701 if (k < Kmax) Q0[k] = Da1kron;
702 if (k == 1) {
703 Q2[1] = kron(diagm(rep), tvec);
704 } else {
705 Q2[k] = kron(diagm(rep), matmul(tvec, alphaRow));
706 }
707 }
708 // truncation: an arrival at the top level is lost, its phase transition is kept
709 Q1[Kmax] = qbd_detail::madd(Q1[Kmax], Da1kron);
710
711 const LdqbdResult<T> res = ldqbd(Q0, Q1, Q2);
712 const std::vector<T>& plev = res.pi.pi;
713
715 out.esup = esup;
716 out.QLen = zero;
717 for (std::size_t k = 0; k <= Kmax; ++k)
718 out.QLen += T(num_traits<T>::from_int(static_cast<int>(k)) * plev[k]);
719 out.ploss = plev[Kmax];
720
721 std::vector<T> penv(me, zero), gacc(me, zero);
722 T util = zero, tput = zero;
723 for (std::size_t k = 0; k <= Kmax; ++k) {
724 const std::vector<T>& pk = res.pi.pi_level[k];
725 std::vector<T> marg(me, zero);
726 if (k == 0) {
727 for (std::size_t a = 0; a < ma; ++a)
728 for (std::size_t e = 0; e < me; ++e) marg[e] += pk[a * me + e];
729 } else {
730 for (std::size_t a = 0; a < ma; ++a)
731 for (std::size_t e = 0; e < me; ++e) {
732 T block = zero, dep = zero;
733 for (std::size_t s = 0; s < ms; ++s) {
734 const T v = pk[(a * me + e) * ms + s];
735 block += v;
736 dep += T(v * tvec(s, 0));
737 }
738 marg[e] += block;
739 util += T(block * phiae[k - 1][a * me + e]);
740 tput += T(dep * phiae[k - 1][a * me + e]);
741 }
742 }
743 for (std::size_t e = 0; e < me; ++e) {
744 penv[e] += marg[e];
745 gacc[e] += T(marg[e] * num_traits<T>::from_double(bgchain_detail::closed_share(
746 static_cast<double>(esup[e]), static_cast<double>(k),
747 nservers)));
748 }
749 }
750 T psum = zero;
751 for (const T& v : penv) psum += v;
752 if (psum != zero)
753 for (std::size_t e = 0; e < me; ++e) {
754 penv[e] /= psum;
755 gacc[e] /= psum;
756 }
757 out.penv = penv;
758 out.cshare.assign(me, 0.0);
759 for (std::size_t e = 0; e < me; ++e) {
760 const double pe = num_traits<T>::to_double(penv[e]);
761 out.cshare[e] = (pe > 1e-14) ? num_traits<T>::to_double(gacc[e]) / pe : 0.0;
762 }
763 out.Util = T(util / num_traits<T>::from_double(nservers));
764 out.Tput = tput;
765 return out;
766}
767
768/**
769 * Number of states of the background-chain CTMC solver_mam_bgchain would build
770 * on this model, WITHOUT building it. Mirrors `mam_bgchain_states.m`.
771 *
772 * The size is what decides whether bgchain is affordable and mam_bgchain_ctmc
773 * only discovers it after the partition is fixed, so the default-method chooser
774 * needs it up front. The count follows the partition below: a pass carries the
775 * tagged closed chain as background class 0 and the demand-similar groups of the
776 * other closed chains as classes 1..G, each enumerating the compositions of its
777 * population over the stations its members visit. Merging two chains onto the
778 * UNION of their supports can raise the count as easily as lower it, so the
779 * passes are enumerated rather than bounded and the largest returned: that is
780 * the one mam_bgchain_ctmc would refuse. Returns 0 when bgchain does not apply
781 * to the model at all.
782 */
783template <class T>
785 const std::size_t M = L.nstations, C = L.nchains;
787
788 std::vector<std::size_t> closedChains;
789 for (std::size_t c = 0; c < C; ++c) {
790 bool open = false;
791 for (std::size_t k : L.inchain[c])
792 if (std::isinf(L.classes[k - 1].population)) open = true;
793 if (!open && dem.Nchain[c] > 0.0) closedChains.push_back(c);
794 }
795 const std::size_t R = closedChains.size();
796 if (R == 0) return 0.0;
797
798 std::vector<std::size_t> cst;
799 for (std::size_t i = 0; i < M; ++i) {
800 bool visited = false;
801 for (std::size_t c : closedChains)
802 if (num_traits<T>::to_double(dem.Vchain(i, c)) > 1e-14) visited = true;
803 if (visited) cst.push_back(i);
804 }
805 const std::size_t Mc = cst.size();
806 if (Mc == 0) return 0.0;
807
808 const std::size_t bgaggr_opt = (opt.bgaggr > 0) ? opt.bgaggr : 1;
809 const std::size_t naggr =
810 std::min(std::max<std::size_t>(bgaggr_opt, 1), std::max<std::size_t>(R - 1, 1));
811 const bool noAggr = (R == 1) || (naggr >= R - 1);
812 const std::size_t npass = noAggr ? 1 : R;
813
814 // nchoosek in floating point, so a chain far above any usable size still compares
815 auto binomial = [](double n, double k) {
816 if (k < 0.0 || k > n) return 0.0;
817 const double kk = std::min(k, n - k);
818 double acc = 1.0;
819 for (double i = 1.0; i <= kk; i += 1.0) acc = acc * (n - kk + i) / i;
820 return acc;
821 };
822
823 double worst = 0.0;
824 for (std::size_t pidx = 0; pidx < npass; ++pidx) {
825 std::vector<std::vector<std::size_t>> members;
826 if (noAggr) {
827 for (std::size_t c : closedChains) members.push_back({c});
828 } else {
829 std::vector<std::size_t> others;
830 for (std::size_t oi = 0; oi < R; ++oi)
831 if (oi != pidx) others.push_back(closedChains[oi]);
832 std::vector<std::vector<double>> D(Mc, std::vector<double>(others.size(), 0.0));
833 for (std::size_t ii = 0; ii < Mc; ++ii)
834 for (std::size_t oi = 0; oi < others.size(); ++oi)
835 D[ii][oi] = num_traits<T>::to_double(dem.Lchain(cst[ii], others[oi]));
836 const std::vector<std::size_t> grp = bgchain_detail::mam_bgchain_groups(D, naggr);
837 members.push_back({closedChains[pidx]});
838 for (std::size_t g = 0; g < naggr; ++g) {
839 std::vector<std::size_t> mem;
840 for (std::size_t oi = 0; oi < others.size(); ++oi)
841 if (grp[oi] == g) mem.push_back(others[oi]);
842 members.push_back(mem);
843 }
844 }
845
846 double n = 1.0;
847 for (const std::vector<std::size_t>& mem : members) {
848 double Nb = 0.0;
849 for (std::size_t o : mem) Nb += std::llround(dem.Nchain[o]);
850 std::size_t m = 0;
851 for (std::size_t ii = 0; ii < Mc; ++ii)
852 for (std::size_t o : mem)
853 if (num_traits<T>::to_double(dem.Vchain(cst[ii], o)) > 1e-14) {
854 ++m;
855 break;
856 }
857 if (m == 0) m = 1; // an empty class still needs one slot to be indexed by
858 n *= binomial(Nb + static_cast<double>(m) - 1.0, static_cast<double>(m) - 1.0);
859 if (!std::isfinite(n)) return std::numeric_limits<double>::infinity();
860 }
861 worst = std::max(worst, n);
862 }
863 return worst;
864}
865
866/**
867 * Port of `solver_mam_bgchain.m`.
868 *
869 * @param L the refreshed struct; must be mixed (at least one open and one
870 * closed chain)
871 * @param opt the MAM options; `cutoff` bounds the open level truncation,
872 * `bgstates_max` the background chain, `qbdphases_max` each station
873 */
874template <class T>
876 if constexpr (!num_traits<T>::has_transcendental) {
877 throw UnsupportedError(ErrorKind{"Arithmetic"},
878 "solver_mam_bgchain: the level-dependent QBD recursion inverts a matrix per level and "
879 "falls back to a pseudo-inverse when a level is singular, neither of which is exact "
880 "arithmetic; rerun with --arith double or --arith real");
881 } else {
883 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
884 const std::size_t M = L.nstations, K = L.nclasses, C = L.nchains;
885
886 // ---- class-level service times ---------------------------------------
887 Matrix<T> S(M, K, zero);
888 for (std::size_t i = 0; i < M; ++i)
889 for (std::size_t k = 0; k < K; ++k) {
890 const double r = num_traits<T>::to_double(L.rates(i, k));
891 if (std::isfinite(r) && r > 0.0) S(i, k) = T(one / L.rates(i, k));
892 }
893
895 const Matrix<T>& rtst = rtv.rtst;
896 const Matrix<T>& V = rtv.Vst;
898
899 std::vector<bool> isopenchain(C, false);
900 std::vector<std::size_t> openChains, closedChains;
901 for (std::size_t c = 0; c < C; ++c) {
902 bool open = false;
903 for (std::size_t k : L.inchain[c])
904 if (std::isinf(L.classes[k - 1].population)) open = true;
905 isopenchain[c] = open;
906 if (open) openChains.push_back(c);
907 else if (dem.Nchain[c] > 0.0) closedChains.push_back(c);
908 }
909 const std::size_t R = closedChains.size();
910 if (R == 0)
911 throw UnsupportedError(ErrorKind{"OpenClass"},
912 "solver_mam_bgchain: the bgchain method requires at least one closed class: the "
913 "background chain IS the closed population vector, so a purely open model has nothing "
914 "to build it from. Use dec.source.");
915
916 // ---- the support of the background chain -----------------------------
917 std::vector<std::size_t> cst;
918 for (std::size_t i = 0; i < M; ++i) {
919 bool visited = false;
920 for (std::size_t c : closedChains)
921 if (num_traits<T>::to_double(dem.Vchain(i, c)) > 1e-14) visited = true;
922 if (visited) cst.push_back(i);
923 }
924 const std::size_t Mc = cst.size();
925 if (Mc == 0)
926 throw UnsupportedError(ErrorKind{"Model"}, "solver_mam_bgchain: the closed classes of this model visit no "
927 "station");
928
929 // ---- chain-level station routing, folding the class axis of rt --------
930 std::vector<Matrix<T>> Pchain(C, Matrix<T>(M, M, zero));
931 for (std::size_t c = 0; c < C; ++c) {
932 Matrix<T> P(M, M, zero);
933 for (std::size_t i = 0; i < M; ++i)
934 for (std::size_t k1 : L.inchain[c]) {
935 const T a = dem.alpha(i, k1 - 1);
936 if (!(num_traits<T>::to_double(a) > 0.0)) continue;
937 for (std::size_t j = 0; j < M; ++j) {
938 T acc = zero;
939 for (std::size_t k2 : L.inchain[c])
940 acc += rtst(i * K + (k1 - 1), j * K + (k2 - 1));
941 if (acc != zero) P(i, j) += T(a * acc);
942 }
943 }
944 Pchain[c] = P;
945 }
946
947 // ---- open arrival streams --------------------------------------------
948 std::vector<double> lambdaChain(C, 0.0);
949 std::map<std::size_t, Map<T>> chainArrival;
950 for (std::size_t c : openChains) {
951 const std::size_t isrc = L.classes[L.inchain[c][0] - 1].refstat; // 1-based
952 double lam = 0.0;
953 std::vector<Mmap<T>> parts;
954 for (std::size_t k : L.inchain[c]) {
955 const double rk = num_traits<T>::to_double(L.rates(isrc - 1, k - 1));
956 if (!std::isfinite(rk) || !(rk > 0.0)) continue;
957 lam += rk;
958 const Map<T> mk = lang::dist_to_map(L.service[isrc - 1][k - 1]);
959 Mmap<T> mm;
960 mm.D0 = mk.D0;
961 mm.D1 = mk.D1;
962 mm.Dc.assign(1, mk.D1);
963 parts.push_back(mm);
964 }
965 lambdaChain[c] = lam;
966 if (!parts.empty()) {
967 const Mmap<T> sup =
968 (parts.size() == 1) ? parts[0] : mmap_super_safe(parts, opt.space_max);
969 chainArrival[c] = Map<T>{sup.D0, sup.D1};
970 }
971 }
972
973 Matrix<T> lambdaOpen(M, K, zero);
974 std::vector<bool> isopenclass(K, false);
975 for (std::size_t c : openChains)
976 for (std::size_t k : L.inchain[c]) {
977 isopenclass[k - 1] = true;
978 for (std::size_t i = 0; i < M; ++i)
979 lambdaOpen(i, k - 1) = T(num_traits<T>::from_double(lambdaChain[c]) * V(i, k - 1));
980 }
981
982 // ---- fixed-point state -----------------------------------------------
984 sol.Q = Matrix<T>(M, K, zero);
985 sol.U = Matrix<T>(M, K, zero);
986 sol.R = Matrix<T>(M, K, zero);
987 sol.Tp = Matrix<T>(M, K, zero);
988 sol.C.assign(K, zero);
989 sol.X.assign(K, zero);
990
991 int Ntot = 0;
992 for (std::size_t c : closedChains) Ntot += static_cast<int>(std::llround(dem.Nchain[c]));
993
994 // cshare[i][e]: mean number of servers of station i that its e closed jobs
995 // hold once the open work has taken its share. Starts at min(e,c).
996 std::vector<std::vector<double>> cshare(M, std::vector<double>(Ntot + 1, 0.0));
997 std::vector<double> nsrvAll(M, 1.0);
998 std::vector<bool> isinfAll(M, false);
999 for (std::size_t i = 0; i < M; ++i) {
1000 nsrvAll[i] = L.stations[i].nservers;
1001 isinfAll[i] = (L.stations[i].sched == SchedStrategy::INF);
1002 for (int e = 0; e <= Ntot; ++e)
1003 cshare[i][e] = std::min(static_cast<double>(e), nsrvAll[i]);
1004 }
1005 std::vector<double> Xclosed(C, 0.0);
1006 for (std::size_t c : closedChains) {
1007 double denom = 0.0;
1008 for (std::size_t i = 0; i < M; ++i) denom += num_traits<T>::to_double(dem.Lchain(i, c));
1009 if (denom > 0.0) Xclosed[c] = dem.Nchain[c] / denom;
1010 }
1011
1012 const std::size_t states_max = (opt.bgstates_max > 0) ? opt.bgstates_max : 20000;
1013 const std::size_t phases_max = (opt.qbdphases_max > 0) ? opt.qbdphases_max : 500;
1014
1015 // How many aggregate classes the background chain carries, and which chains
1016 // share each of them. opt.bgaggr is the number of AGGREGATE classes G: G = 1
1017 // is the classic tagged/aggregate pair, G >= R-1 aggregates nothing.
1018 const std::size_t bgaggr_opt = (opt.bgaggr > 0) ? opt.bgaggr : 1;
1019 const std::size_t naggr =
1020 std::min(std::max<std::size_t>(bgaggr_opt, 1), std::max<std::size_t>(R - 1, 1));
1021 // With nothing left to aggregate ONE background chain carries every closed
1022 // chain exactly, so the tagged loop would solve the same chain R times over.
1023 const bool noAggr = (R == 1) || (naggr >= R - 1);
1024
1025 // The grouping is a property of the demands, not of the iterate, so it is
1026 // fixed once here rather than recomputed inside the fixed point.
1027 std::vector<std::vector<std::size_t>> othersOf(R), grpOf(R);
1028 for (std::size_t ridx = 0; ridx < R; ++ridx) {
1029 for (std::size_t oi = 0; oi < R; ++oi)
1030 if (oi != ridx) othersOf[ridx].push_back(closedChains[oi]);
1031 if (!noAggr && !othersOf[ridx].empty()) {
1032 std::vector<std::vector<double>> D(Mc, std::vector<double>(othersOf[ridx].size(), 0.0));
1033 for (std::size_t ii = 0; ii < Mc; ++ii)
1034 for (std::size_t oi = 0; oi < othersOf[ridx].size(); ++oi)
1035 D[ii][oi] = num_traits<T>::to_double(dem.Lchain(cst[ii], othersOf[ridx][oi]));
1036 grpOf[ridx] = bgchain_detail::mam_bgchain_groups(D, naggr);
1037 }
1038 }
1039 const std::size_t npass = noAggr ? 1 : R;
1040
1041 Matrix<T> TNprev(M, K, num_traits<T>::from_double(1e300));
1042 int totiter = 0;
1043 const double relax = 0.5;
1044
1045 auto maxdiff = [&](const Matrix<T>& a, const Matrix<T>& b) {
1046 double m = 0.0;
1047 for (std::size_t i = 0; i < a.rows(); ++i)
1048 for (std::size_t j = 0; j < a.cols(); ++j)
1049 m = std::max(m, std::fabs(num_traits<T>::to_double(a(i, j)) -
1050 num_traits<T>::to_double(b(i, j))));
1051 return m;
1052 };
1053
1054 while (maxdiff(sol.Tp, TNprev) > opt.tol && totiter < opt.iter_max) {
1055 // Wall-clock budget (options.timeout): the last completed pass is the
1056 // interim solution, as the reference's `if it>0 && lineTimeoutExceeded`.
1057 if (totiter > 0 && util::timeout_exceeded()) break;
1058 ++totiter;
1059 TNprev = sol.Tp;
1060
1061 std::vector<double> QopenAcc(M, 0.0);
1062 std::vector<std::vector<double>> cshareAcc(M, std::vector<double>(Ntot + 1, 0.0));
1063
1064 for (std::size_t pidx = 0; pidx < npass; ++pidx) {
1065 const std::size_t r = closedChains[pidx];
1066
1067 // Background classes: class 0 is the tagged chain, classes 1..G the
1068 // flow-equivalent aggregates of the demand-similar groups. With no
1069 // aggregation every closed chain is a class of its own, in chain order.
1070 std::vector<std::vector<std::size_t>> members;
1071 if (noAggr) {
1072 for (std::size_t c : closedChains) members.push_back({c});
1073 } else {
1074 members.push_back({r});
1075 for (std::size_t g = 0; g < naggr; ++g) {
1076 std::vector<std::size_t> mem;
1077 for (std::size_t oi = 0; oi < othersOf[pidx].size(); ++oi)
1078 if (grpOf[pidx][oi] == g) mem.push_back(othersOf[pidx][oi]);
1079 members.push_back(mem);
1080 }
1081 }
1082 const std::size_t B = members.size();
1083
1084 std::vector<int> Nb(B, 0);
1085 std::vector<std::vector<T>> STb(Mc, std::vector<T>(B, zero));
1086 std::vector<Matrix<T>> Pb;
1087 // A class can only hold jobs at the stations its members visit; see
1088 // mam_bgchain_ctmc on why the union makes the chain reducible.
1089 std::vector<std::vector<bool>> suppb(Mc, std::vector<bool>(B, false));
1090 for (std::size_t b = 0; b < B; ++b) {
1091 const std::vector<std::size_t>& mem = members[b];
1092 for (std::size_t o : mem) {
1093 Nb[b] += static_cast<int>(std::llround(dem.Nchain[o]));
1094 for (std::size_t ii = 0; ii < Mc; ++ii)
1095 if (num_traits<T>::to_double(dem.Vchain(cst[ii], o)) > 1e-14)
1096 suppb[ii][b] = true;
1097 }
1098 if (mem.size() == 1) {
1099 // a group of one is carried exactly: no mean to take
1100 Matrix<T> P0(Mc, Mc, zero);
1101 for (std::size_t ii = 0; ii < Mc; ++ii) {
1102 STb[ii][b] = dem.STchain(cst[ii], mem[0]);
1103 for (std::size_t jj = 0; jj < Mc; ++jj)
1104 P0(ii, jj) = Pchain[mem[0]](cst[ii], cst[jj]);
1105 }
1106 Pb.push_back(P0);
1107 } else if (mem.empty()) {
1108 Pb.push_back(Matrix<T>(Mc, Mc, zero));
1109 } else {
1110 std::vector<std::vector<double>> w(Mc, std::vector<double>(mem.size(), 0.0));
1111 for (std::size_t ii = 0; ii < Mc; ++ii) {
1112 double rowsum = 0.0;
1113 for (std::size_t oi = 0; oi < mem.size(); ++oi) {
1114 w[ii][oi] = Xclosed[mem[oi]] *
1115 num_traits<T>::to_double(dem.Vchain(cst[ii], mem[oi]));
1116 rowsum += w[ii][oi];
1117 }
1118 for (std::size_t oi = 0; oi < mem.size(); ++oi)
1119 w[ii][oi] = (rowsum > 0.0) ? w[ii][oi] / rowsum
1120 : 1.0 / static_cast<double>(mem.size());
1121 }
1122 Matrix<T> Pagg(Mc, Mc, zero);
1123 for (std::size_t ii = 0; ii < Mc; ++ii) {
1124 T st = zero;
1125 for (std::size_t oi = 0; oi < mem.size(); ++oi) {
1126 const T wv = num_traits<T>::from_double(w[ii][oi]);
1127 st += T(wv * dem.STchain(cst[ii], mem[oi]));
1128 for (std::size_t jj = 0; jj < Mc; ++jj)
1129 Pagg(ii, jj) += T(wv * Pchain[mem[oi]](cst[ii], cst[jj]));
1130 }
1131 STb[ii][b] = st;
1132 }
1133 Pb.push_back(Pagg);
1134 }
1135 }
1136 // Row-normalize, leaving an all-zero row as a self-loop so the chain
1137 // stays a proper Markov chain on its support.
1138 for (std::size_t b = 0; b < B; ++b) {
1139 for (std::size_t ii = 0; ii < Mc; ++ii) {
1140 T s = zero;
1141 for (std::size_t jj = 0; jj < Mc; ++jj) s += Pb[b](ii, jj);
1142 if (num_traits<T>::to_double(s) > 1e-14) {
1143 for (std::size_t jj = 0; jj < Mc; ++jj) Pb[b](ii, jj) /= s;
1144 } else {
1145 for (std::size_t jj = 0; jj < Mc; ++jj) Pb[b](ii, jj) = zero;
1146 Pb[b](ii, ii) = one;
1147 }
1148 }
1149 }
1150
1151 std::vector<bool> isinfC(Mc, false);
1152 std::vector<double> nsrvC(Mc, 1.0);
1153 std::vector<std::vector<double>> cshareC(Mc);
1154 for (std::size_t ii = 0; ii < Mc; ++ii) {
1155 isinfC[ii] = isinfAll[cst[ii]];
1156 nsrvC[ii] = nsrvAll[cst[ii]];
1157 cshareC[ii] = cshare[cst[ii]];
1158 }
1159
1160 const BgchainCtmc<T> bg =
1161 mam_bgchain_ctmc(Nb, STb, Pb, isinfC, nsrvC, cshareC, suppb, states_max);
1162
1163 // ---- closed-class metrics of every chain this pass carries EXACTLY:
1164 // the tagged one always, and every chain when nothing was aggregated.
1165 const std::size_t bmax = noAggr ? B : 1;
1166 for (std::size_t b = 0; b < bmax; ++b) {
1167 if (members[b].empty()) continue;
1168 const std::size_t rb = members[b][0];
1169 for (std::size_t k : L.inchain[rb])
1170 for (std::size_t i = 0; i < M; ++i) {
1171 sol.Q(i, k - 1) = zero;
1172 sol.U(i, k - 1) = zero;
1173 sol.R(i, k - 1) = zero;
1174 sol.Tp(i, k - 1) = zero;
1175 }
1176 for (std::size_t ii = 0; ii < Mc; ++ii) {
1177 const std::size_t i = cst[ii];
1178 for (std::size_t k : L.inchain[rb]) {
1179 const T a = dem.alpha(i, k - 1);
1180 if (!(num_traits<T>::to_double(a) > 0.0)) continue;
1181 // THROUGHPUT splits by VISIT share, OCCUPANCY by DEMAND
1182 // share. A chain queue divided by alpha alone gives every
1183 // class of a station the same response time, impossible
1184 // at a Delay where R must be the class service time; the
1185 // weight is alpha*ST/STchain, as sn_deaggregate_chain_
1186 // results applies it.
1187 const T stc = dem.STchain(i, rb);
1188 const T w = (num_traits<T>::to_double(stc) > 1e-14) ? T(a * S(i, k - 1) / stc) : a;
1189 const T q = T(bg.QLen[ii][b] * w);
1190 const T x = T(bg.Tput[ii][b] * a);
1191 sol.Q(i, k - 1) = q;
1192 sol.Tp(i, k - 1) = x;
1193 sol.U(i, k - 1) = isinfC[ii] ? q : T(bg.Ubusy[ii][b] * w);
1194 sol.R(i, k - 1) = (num_traits<T>::to_double(x) > 1e-14) ? T(q / x) : zero;
1195 }
1196 }
1197 const std::size_t iref = L.classes[L.inchain[rb][0] - 1].refstat; // 1-based
1198 T tputref = zero;
1199 for (std::size_t k : L.inchain[rb]) tputref += sol.Tp(iref - 1, k - 1);
1200 const double vref = num_traits<T>::to_double(dem.Vchain(iref - 1, rb));
1201 Xclosed[rb] = (vref > 1e-14) ? num_traits<T>::to_double(tputref) / vref
1202 : num_traits<T>::to_double(tputref);
1203 }
1204
1205 std::vector<double> Uclosed(M, 0.0);
1206 for (std::size_t ii = 0; ii < Mc; ++ii) {
1207 double u = 0.0;
1208 for (std::size_t b = 0; b < B; ++b) u += num_traits<T>::to_double(bg.Ubusy[ii][b]);
1209 Uclosed[cst[ii]] = u;
1210 }
1211
1212 // ---- one pass of the open side ---------------------------------
1213 for (std::size_t i = 0; i < M; ++i) {
1214 bool solved = false;
1215 const SchedStrategy sc = L.stations[i].sched;
1216 if (sc != SchedStrategy::EXT && sc != SchedStrategy::INF) {
1217 std::vector<std::size_t> kopen;
1218 for (std::size_t k = 0; k < K; ++k)
1219 if (num_traits<T>::to_double(lambdaOpen(i, k)) > 1e-14) kopen.push_back(k);
1220 if (!kopen.empty()) {
1221 // aggregate open arrival MAP at the station
1222 bool haveDa = false;
1223 Map<T> Da;
1224 for (std::size_t c : openChains) {
1225 if (!(lambdaChain[c] > 1e-14)) continue;
1226 const auto it = chainArrival.find(c);
1227 if (it == chainArrival.end()) continue;
1228 T vsum = zero;
1229 for (std::size_t k : L.inchain[c]) vsum += V(i, k - 1);
1230 const double rate_ic =
1231 lambdaChain[c] * num_traits<T>::to_double(vsum);
1232 if (!(rate_ic > 1e-14)) continue;
1233 const Map<T> scaled =
1234 map_scale(it->second, T(num_traits<T>::from_double(1.0 / rate_ic)));
1235 if (!haveDa) {
1236 Da = scaled;
1237 haveDa = true;
1238 } else {
1239 std::vector<Mmap<T>> two(2);
1240 two[0].D0 = Da.D0; two[0].D1 = Da.D1; two[0].Dc.assign(1, Da.D1);
1241 two[1].D0 = scaled.D0; two[1].D1 = scaled.D1;
1242 two[1].Dc.assign(1, scaled.D1);
1243 const Mmap<T> sup = mmap_super_safe(two, opt.space_max);
1244 Da = Map<T>{sup.D0, sup.D1};
1245 }
1246 }
1247 if (haveDa) {
1248 // arrival-weighted phase-type mixture of the open service laws
1249 T lamtot = zero, svcwork = zero;
1250 for (std::size_t k : kopen) {
1251 lamtot += lambdaOpen(i, k);
1252 svcwork += T(lambdaOpen(i, k) * S(i, k));
1253 }
1254 std::vector<std::vector<T>> pies;
1255 std::vector<Matrix<T>> subgens;
1256 std::size_t msTotal = 0;
1257 // PROCESSOR SHARING IS INSENSITIVE to the service law beyond its mean, so
1258 // carrying the phase-type representation at a PS station is not merely unnecessary,
1259 // it is WRONG. This QBD tracks ONE service phase for the whole station, which makes
1260 // the open queue length inherit the SCV-sensitivity of an M/PH/1 FCFS queue;
1261 // measured against SolverCTMC, a HyperExp of SCV 4 then read 21% high where the
1262 // exact answer is the exponential one to five digits. The exponential of the same
1263 // mean is exact here, and it shrinks the QBD's phase count as a side effect.
1264 const bool isPSstation = (sc == SchedStrategy::PS);
1265 for (std::size_t k : kopen) {
1266 Map<T> phk;
1267 if (isPSstation) {
1268 const T mu = T(one / S(i, k));
1269 phk.D0 = Matrix<T>(1, 1, T(-mu));
1270 phk.D1 = Matrix<T>(1, 1, mu);
1271 } else {
1272 phk = map_scale(lang::dist_to_map(L.service[i][k]), S(i, k));
1273 }
1274 std::vector<T> pik = map_pie(phk);
1275 const T w = T(lambdaOpen(i, k) / lamtot);
1276 for (T& v : pik) v = T(v * w);
1277 pies.push_back(pik);
1278 subgens.push_back(phk.D0);
1279 msTotal += phk.D0.rows();
1280 }
1281 std::vector<T> alphaS(msTotal, zero);
1282 Matrix<T> Tblk(msTotal, msTotal, zero);
1283 std::size_t off = 0;
1284 for (std::size_t idx = 0; idx < pies.size(); ++idx) {
1285 const std::size_t n = subgens[idx].rows();
1286 for (std::size_t a = 0; a < n; ++a) {
1287 alphaS[off + a] = pies[idx][a];
1288 for (std::size_t b = 0; b < n; ++b)
1289 Tblk(off + a, off + b) = subgens[idx](a, b);
1290 }
1291 off += n;
1292 }
1293
1294 Matrix<T> Aenv(1, 1, zero);
1295 std::vector<int> esup(1, 0);
1296 const auto pos = std::find(cst.begin(), cst.end(), i);
1297 if (pos != cst.end()) {
1299 bg, static_cast<std::size_t>(pos - cst.begin()));
1300 Aenv = env.A;
1301 esup = env.esup;
1302 }
1303
1304 const std::size_t nphases = Da.D0.rows() * esup.size() * msTotal;
1305 if (nphases > phases_max)
1306 throw UnsupportedError(ErrorKind{"StateSpace"},
1307 "solver_mam_bgchain: the modulated QBD of station " +
1308 std::to_string(i + 1) + " needs " + std::to_string(nphases) +
1309 " phases (" + std::to_string(Da.D0.rows()) + " arrival x " +
1310 std::to_string(esup.size()) + " environment x " +
1311 std::to_string(msTotal) + " service), above the limit of " +
1312 std::to_string(phases_max) +
1313 ". The environment axis is the closed population held by the "
1314 "station, so it grows with the closed population. Raise "
1315 "options.config.qbdphases_max, or reduce the closed population "
1316 "or the order of the arrival and service processes.");
1317
1318 std::vector<double> gref(esup.size(), 0.0);
1319 for (std::size_t e = 0; e < esup.size(); ++e)
1320 gref[e] = cshare[i][std::min<std::size_t>(
1321 static_cast<std::size_t>(esup[e]),
1322 cshare[i].size() - 1)];
1323
1324 // truncation level: the explicit cutoff, else enough
1325 // levels for the geometric tail left by the closed
1326 // traffic to be negligible
1327 std::size_t Kmax;
1328 if (opt.cutoff > 0) {
1329 Kmax = std::max<std::size_t>(2, opt.cutoff);
1330 } else {
1331 const double free = std::max(1e-8, 1.0 - Uclosed[i]);
1332 const double lam = num_traits<T>::to_double(lamtot);
1333 const double Smix = num_traits<T>::to_double(svcwork) / lam;
1334 double rho = lam * Smix / (nsrvAll[i] * free);
1335 rho = std::min(std::max(rho, 1e-3), 1.0 - 1e-3);
1336 const long kv =
1337 static_cast<long>(std::ceil(std::log(1e-8) / std::log(rho)));
1338 Kmax = static_cast<std::size_t>(std::min<long>(
1339 std::max<long>(kv, 20), 200));
1340 }
1341
1342 const BgchainStation<T> st =
1343 mam_bgchain_station(Da.D0, Da.D1, alphaS, Tblk, Aenv, esup,
1344 nsrvAll[i], gref, Kmax);
1345 QopenAcc[i] += num_traits<T>::to_double(st.QLen);
1346 // The QBD only saw the environment states the chain
1347 // reaches; interpolate the rest so the next chain has
1348 // a share wherever it may go, clipped to what a closed
1349 // job can physically hold.
1350 for (int e = 0; e <= Ntot; ++e) {
1351 double g;
1352 const std::size_t n = st.esup.size();
1353 if (n == 1) {
1354 g = st.cshare[0];
1355 } else if (e <= st.esup[0]) {
1356 const double slope = (st.cshare[1] - st.cshare[0]) /
1357 (st.esup[1] - st.esup[0]);
1358 g = st.cshare[0] + slope * (e - st.esup[0]);
1359 } else if (e >= st.esup[n - 1]) {
1360 const double slope = (st.cshare[n - 1] - st.cshare[n - 2]) /
1361 (st.esup[n - 1] - st.esup[n - 2]);
1362 g = st.cshare[n - 1] + slope * (e - st.esup[n - 1]);
1363 } else {
1364 std::size_t lo = 0;
1365 while (lo + 1 < n && st.esup[lo + 1] < e) ++lo;
1366 const double w = static_cast<double>(e - st.esup[lo]) /
1367 static_cast<double>(st.esup[lo + 1] - st.esup[lo]);
1368 g = st.cshare[lo] + w * (st.cshare[lo + 1] - st.cshare[lo]);
1369 }
1370 cshareAcc[i][e] +=
1371 std::min(std::max(g, 0.0),
1372 std::min(static_cast<double>(e), nsrvAll[i]));
1373 }
1374 solved = true;
1375 }
1376 }
1377 }
1378 if (!solved) {
1379 // a station with no open queue keeps the share it had
1380 for (int e = 0; e <= Ntot; ++e) cshareAcc[i][e] += cshare[i][e];
1381 }
1382 }
1383 }
1384
1385 std::vector<double> Qopen(M, 0.0);
1386 for (std::size_t i = 0; i < M; ++i) {
1387 Qopen[i] = QopenAcc[i] / static_cast<double>(npass);
1388 for (int e = 0; e <= Ntot; ++e)
1389 cshare[i][e] = (1.0 - relax) * cshare[i][e] +
1390 relax * (cshareAcc[i][e] / static_cast<double>(npass));
1391 }
1392
1393 // ---- open-class metrics from the aggregate station results --------
1394 for (std::size_t i = 0; i < M; ++i) {
1395 std::vector<std::size_t> kopen;
1396 for (std::size_t k = 0; k < K; ++k)
1397 if (isopenclass[k] && num_traits<T>::to_double(lambdaOpen(i, k)) > 1e-14)
1398 kopen.push_back(k);
1399 const SchedStrategy sc = L.stations[i].sched;
1400 if (kopen.empty()) {
1401 for (std::size_t k = 0; k < K; ++k)
1402 if (isopenclass[k]) {
1403 sol.Tp(i, k) = lambdaOpen(i, k);
1404 sol.Q(i, k) = zero;
1405 sol.U(i, k) = zero;
1406 sol.R(i, k) = zero;
1407 }
1408 continue;
1409 }
1410 T lamtot = zero, work = zero;
1411 for (std::size_t k : kopen) {
1412 lamtot += lambdaOpen(i, k);
1413 work += T(lambdaOpen(i, k) * S(i, k));
1414 }
1415 const T Smix = T(work / lamtot);
1416 for (std::size_t k : kopen) {
1417 sol.Tp(i, k) = lambdaOpen(i, k);
1418 if (sc == SchedStrategy::EXT) {
1419 sol.Q(i, k) = zero;
1420 sol.U(i, k) = zero;
1421 sol.R(i, k) = zero;
1422 } else if (sc == SchedStrategy::INF) {
1423 sol.R(i, k) = S(i, k);
1424 sol.Q(i, k) = T(lambdaOpen(i, k) * S(i, k));
1425 sol.U(i, k) = sol.Q(i, k);
1426 } else {
1427 const T Rtot = T(num_traits<T>::from_double(Qopen[i]) / lamtot);
1428 T rk;
1429 if (sc == SchedStrategy::PS) {
1430 // processor sharing: residence scales with the demand
1431 rk = T(Rtot * S(i, k) / Smix);
1432 } else {
1433 // FCFS and its variants: the wait is class-blind, the
1434 // service time is not
1435 const T cand = T(Rtot - Smix + S(i, k));
1437 ? cand
1438 : S(i, k);
1439 }
1440 sol.R(i, k) = rk;
1441 sol.Q(i, k) = T(lambdaOpen(i, k) * rk);
1442 // Utilization Law: a c-server station holds TN*S/c
1443 sol.U(i, k) =
1444 T(lambdaOpen(i, k) * S(i, k) / num_traits<T>::from_double(nsrvAll[i]));
1445 }
1446 }
1447 }
1448 }
1449
1450 for (std::size_t c = 0; c < C; ++c)
1451 for (std::size_t k : L.inchain[c])
1452 sol.X[k - 1] = num_traits<T>::from_double(isopenchain[c] ? lambdaChain[c] : Xclosed[c]);
1453 for (std::size_t k = 0; k < K; ++k) {
1454 T acc = zero;
1455 for (std::size_t i = 0; i < M; ++i) acc += sol.R(i, k);
1456 sol.C[k] = acc;
1457 }
1458 sol.iter = totiter;
1459 return sol;
1460 }
1461}
1462
1463} // namespace mam
1464} // namespace line
1465
1466#endif // LINE_SOLVERS_MAM_SOLVER_MAM_BGCHAIN_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.
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
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
Steady-state distribution of a continuous-time Markov chain.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
Level-dependent QBD processes with finitely many levels: the rate matrices R^(n) by the backward matr...
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
options.timeout: one wall-clock budget per top-level solve, on a per-THREAD deadline stack.
The option and result types SolverMAM shares with its analyzers.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
MAP constructors and structural transformations.
Dense matrix and non-owning view.
The MMAP assembly primitives solver_mam_basic.m builds its per-station arrival stream from: mmap_expo...
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
The option and result types every MVA analyzer shares.
SnRtStations< T > sn_rt_stations(const qn::NetworkStruct< T > &sn)
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
double tot(const std::vector< double > &pops)
double bgchain_states(const qn::NetworkStruct< T > &L, const MamOptions &opt)
Number of states of the background-chain CTMC solver_mam_bgchain would build on this model,...
mva::MvaSolution< T > solver_mam_bgchain(const qn::NetworkStruct< T > &L, const MamOptions &opt)
Port of solver_mam_bgchain.m.
BgchainStation< T > mam_bgchain_station(const Matrix< T > &Da0, const Matrix< T > &Da1, const std::vector< T > &alpha_s, const Matrix< T > &Tsvc, const Matrix< T > &Ain, const std::vector< int > &esup_in, double nservers, const std::vector< double > &gref_in, std::size_t Kmax)
Solve the open classes of one station as a modulated level-dependent QBD (mam_bgchain_station....
BgchainEnv< T > mam_bgchain_env(const BgchainCtmc< T > &bg, std::size_t i)
Lump the background chain onto the closed occupancy of station i (mam_bgchain_env....
Matrix< T > kron(const Matrix< T > &A, const Matrix< T > &B)
Kronecker product.
Definition mmap_lambda.h:57
BgchainCtmc< T > mam_bgchain_ctmc(const std::vector< int > &Nb, const std::vector< std::vector< T > > &STb, const std::vector< Matrix< T > > &Pb, const std::vector< bool > &isinf_i, const std::vector< double > &nsrv, const std::vector< std::vector< double > > &cshare, const std::vector< std::vector< bool > > &supp, std::size_t states_max)
Build and solve the background chain (mam_bgchain_ctmc.m).
LdqbdResult< T > ldqbd(const std::vector< Matrix< T > > &q0, const std::vector< Matrix< T > > &q1, const std::vector< Matrix< T > > &q2)
Solve a level-dependent QBD: rate matrices and stationary law (ldqbd.m).
Definition ldqbd.h:246
Map< T > map_scale(const Map< T > &in, const T &new_mean)
Rescale time so that the mean inter-arrival time becomes new_mean.
std::vector< T > map_pie(const Map< T > &m)
Phase distribution seen by an arriving job, pie = pi D1 / (pi D1 e).
Definition map_moment.h:89
Mmap< T > mmap_super_safe(const std::vector< Mmap< T > > &in, std::size_t maxorder)
Order-bounded superposition of several MMAPs (mmap_super_safe.m).
std::vector< T > ctmc_solve(const Matrix< T > &Qin, const std::string &linsolver=std::string())
Steady-state distribution of a continuous-time Markov chain.
Definition ctmc_solve.h:133
Matrix< T > ctmc_makeinfgen(const Matrix< T > &Q)
Set the diagonal so that every row sums to zero (ctmc_makeinfgen).
Definition ctmc_solve.h:61
ChainDemands< T > sn_get_demands_chain(const qn::NetworkStruct< T > &L)
Port of sn_get_demands_chain.
Definition sn_chain.h:63
std::vector< std::vector< T > > space_closed_single(std::size_t m, std::size_t n)
Port of State.spaceClosedSingle: the ways to place n jobs over m phases.
Definition state.h:514
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
Matrix< T > matmul(const Matrix< T > &A, const Matrix< T > &B)
Matrix product A B.
Definition linalg.h:36
A queueing network and its refreshed NetworkStruct.
Chain aggregation and de-aggregation.
Port of matlab/src/api/sn/sn_rt_stations.m.
Port of the MATLAB +State package: the encoding that turns a station's state row into marginal job co...
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
What sn_rt_stations returns: the complemented routing and the station visits.
Matrix< T > rtst
(nstations*nclasses) square, STATION-major rows
Matrix< T > Vst
(nstations x nclasses), cellsum(sn.visits) by station
The solved background modulating chain.
std::vector< std::vector< int > > totocc
totocc[s][i]: closed jobs of any background class held by station i.
std::vector< std::vector< std::vector< int > > > space
space[s][i][b]: class-b jobs held by station i in state s.
std::vector< std::vector< T > > QLen
std::vector< std::vector< T > > Ubusy
(Mc x B)
std::vector< std::vector< T > > Tput
The environment one station sees: the background chain lumped onto its occupancy.
std::vector< int > esup
ascending
What one modulated station QBD returns.
std::vector< double > cshare
E[min(e+k,c) e/(e+k) | e], indexed by esup.
R and the stationary distribution together (ldqbd.m).
Definition ldqbd.h:239
LdqbdPi< T > pi
Definition ldqbd.h:241
The options SolverMAM reads.
Definition mam_types.h:30
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
An MMAP: the underlying MAP plus the per-class arrival matrices.
Definition mmap_lambda.h:45
Matrix< T > D0
Definition mmap_lambda.h:46
Matrix< T > D1
Definition mmap_lambda.h:47
std::vector< Matrix< T > > Dc
per-class matrices, sum_c Dc = D1
Definition mmap_lambda.h:48
The chain-level view of a layer, as sn_get_demands_chain returns it.
Definition sn_chain.h:46
std::vector< double > Nchain
(C) population, infinite for an open chain
Definition sn_chain.h:51
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, the [Q,U,R,T,C,X] of the MATLAB analyzers.
Definition mva_types.h:97
std::vector< T > X
Definition mva_types.h:99
std::vector< T > C
Definition mva_types.h:99