LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
solver_chain_tables.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_SOLVER_CHAIN_TABLES_H
6#define LINE_SOLVERS_SOLVER_CHAIN_TABLES_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * The CHAIN-level and SYSTEM-level views of a solved model.
12 *
13 * Ports of `@@NetworkSolver/getAvgSys.m`, `getAvgChain.m` and
14 * `getAvgNodeChain.m` with the tables built on top of them
15 * (`getAvgSysTable`, `getAvgChainTable`, `getAvgNodeChainTable`).
16 *
17 * A CHAIN IS NOT A CLASS AND THE AGGREGATION IS NOT A SUM FOR EVERY METRIC,
18 * which is the whole reason these live apart from the AvgTable. Queue lengths,
19 * utilizations, arrival rates, throughputs and residence times are ADDITIVE
20 * over the classes of a chain, so their chain value is the row sum. Response
21 * time is NOT: a chain's response time at a station is the per-visit time
22 * averaged over the classes with the VISIT SHARE alpha as the weight, because a
23 * job of the chain arrives as one class or another in proportion to how often
24 * that class visits. Summing it instead would report the total time a job would
25 * spend if it were every class at once.
26 *
27 * The system view is a third thing again. `getAvgSys` returns one response time
28 * and one throughput PER CHAIN, both measured at the chain's reference station:
29 * the throughput is the completing flow INTO that station, read off the routing
30 * matrix, and the response time is the cycle time -- Little's law on a closed
31 * chain, the visit-weighted sum of the per-class system times on an open one.
32 *
33 * INDEXING: these functions take a solved `mva::AvgResult` (station x class)
34 * and the struct it was solved from, and never re-solve. That keeps them usable
35 * from any solver whose runner returns an AvgResult, which is what the reference
36 * means by putting them on @@NetworkSolver rather than on one solver.
37 *
38 * ONE DEVIATION FROM THE REFERENCE, STATED: `getAvgSys.m` indexes `sn.rt` and,
39 * in one branch, `sn.visits{c}` with a STATION index, while both matrices are
40 * indexed by STATEFUL node. The two orders coincide on every model whose
41 * stateful nodes are all stations -- which is every model the reference is
42 * exercised on -- and differ as soon as one is not (a Cache, a Logger). This
43 * port uses `stateful_of_station`, so it agrees with the reference wherever the
44 * reference is self-consistent and is correct where it is not.
45 */
46
47#include <cmath>
48#include <limits>
49#include <cstddef>
50#include <string>
51#include <vector>
52
54#include "line/num/number.h"
57#include "line/util/error.h"
58#include "line/util/matrix.h"
59
60namespace line {
61namespace solvers {
62
63namespace chain_detail {
64
65/**
66 * Mean of the maximum of independent exponentials, by inclusion-exclusion.
67 *
68 * With one exponential of rate lambda_i per parallel branch, the expected time
69 * until ALL have finished is
70 *
71 * E[max] = sum_{k=1..n} (-1)^(k-1) sum_{|S|=k} 1 / (sum_{i in S} lambda_i),
72 *
73 * which is what `getAvgSys.m` and `pathsCS.m` both spell out with `nchoosek`.
74 * The rates come from the branch response times as lambda_i = 1/r_i, so the
75 * whole construction reads the parallel section as a race between exponentials
76 * whose means are the measured branch times. That is an ASSUMPTION about the
77 * branch laws, not a measurement of them, and it is the reference's -- a branch
78 * whose time is far from exponential is the one case where this quantity is not
79 * the synchronization delay it is reported as.
80 *
81 * Enumerated over bitmasks rather than by `nchoosek`, which materializes every
82 * combination as a matrix row. The cap is on the branch count, not on the
83 * subset count: 2^n terms alternate in sign and cancel catastrophically well
84 * before memory becomes the issue, and a fork with more than 20 parallel
85 * branches is a modelling question, not a numerical one.
86 */
87template <class T>
88T exp_max_mean(const std::vector<T>& all_branch_times) {
89 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
90 // A ZERO-TIME PATH DROPS OUT: it is a degenerate exponential at 0 and never
91 // the maximum. The reference gets the same by arithmetic, its rate 1/0 = Inf
92 // making every subset that holds it contribute 1/Inf = 0. This is exactly
93 // what an enclosing section leaves behind once it has absorbed and zeroed a
94 // nested one and the fork loop then visits the nested one again.
95 std::vector<T> branch_times;
96 for (std::size_t i = 0; i < all_branch_times.size(); ++i) {
97 const double ri = num_traits<T>::to_double(all_branch_times[i]);
98 if (!(ri >= 0.0))
99 throw NumericError(ErrorKind{"Degenerate"},
100 "getAvgSys: a path from the fork to the join has a negative or undefined total "
101 "response time, so its exponential rate is not defined");
102 if (ri > 0.0) branch_times.push_back(all_branch_times[i]);
103 }
104 const std::size_t n = branch_times.size();
105 if (n == 0) return zero;
106 if (n > 20)
107 throw UnsupportedError(ErrorKind{"Fork"},
108 "getAvgSys: the fork-join section has " + std::to_string(n) +
109 " parallel paths, and the reference's synchronization delay is an "
110 "inclusion-exclusion sum of 2^n alternating terms; past 20 paths that sum has lost "
111 "every significant digit to cancellation, so it is refused rather than reported");
112
113 std::vector<T> lambda(n);
114 for (std::size_t i = 0; i < n; ++i) lambda[i] = T(one / branch_times[i]);
115
116 T d0 = zero;
117 const unsigned long long total = 1ull << n;
118 for (unsigned long long mask = 1; mask < total; ++mask) {
119 T rate = zero;
120 std::size_t bits = 0;
121 for (std::size_t i = 0; i < n; ++i)
122 if (mask & (1ull << i)) {
123 rate = T(rate + lambda[i]);
124 ++bits;
125 }
126 const T term = T(one / rate);
127 if (bits % 2 == 1)
128 d0 = T(d0 + term);
129 else
130 d0 = T(d0 - term);
131 }
132 return d0;
133}
134
135/** What one `pathsCS` walk returns. */
136template <class T>
137struct PathsResult {
138 std::vector<T> times; ///< total response time of each fork-to-join path
139 std::vector<std::size_t> stations; ///< 1-based stations lying on those paths
140};
141
142/**
143 * Port of `ModelAdapter.pathsCS`: enumerate every path from `cur` to `stop` and
144 * total the response time along each.
145 *
146 * THE ROUTING MATRIX IS `rtnodes` HERE AND `rtorig` IN THE REFERENCE, and the
147 * indexing differs with it: `cell2mat(getLinkedRoutingMatrix)` is CLASS-major,
148 * addressed as `(class-1)*nnodes + node`, while `rtnodes` is NODE-major,
149 * `(node-1)*nclasses + class`. Only the SUPPORT of the matrix is read -- the
150 * walk follows nonzero successors and never multiplies by a probability -- and
151 * the two describe the same graph once the refresh has resolved class
152 * switching, so the enumerated path SET is the same. The order in which the
153 * paths come out differs, and nothing downstream depends on it: `exp_max_mean`
154 * is symmetric in its argument.
155 *
156 * A NESTED FORK IS COLLAPSED BEFORE THE WALK CONTINUES, which is why this
157 * mutates `RN`: the inner join's response time is set to the inner section's
158 * own synchronization delay and the inner branch stations are zeroed, so the
159 * outer walk crosses the inner section as a single station. The `RN(join) == 0`
160 * test is what makes that happen once rather than once per outer path.
161 *
162 * A CYCLE IS REFUSED, NOT WALKED. The reference has no guard and recurses until
163 * MATLAB runs out of stack; a routing loop inside a fork-join section has no
164 * finite path set, so there is nothing to enumerate and saying so is the only
165 * available answer.
166 */
167template <class T>
168PathsResult<T> paths_cs(const qn::NetworkStruct<T>& sn, const Matrix<T>& P, std::size_t cur,
169 std::size_t stop, std::size_t cls, Matrix<T>& RN, const T& elapsed,
170 const std::vector<std::size_t>& acc_stations,
171 std::vector<std::pair<std::size_t, std::size_t>>& on_path) {
172 const std::size_t K = sn.nclasses, N = sn.nodes.size();
173 const T zero = num_traits<T>::from_int(0);
174
175 PathsResult<T> out;
176 if (cur == stop) {
177 out.times.push_back(elapsed);
178 out.stations = acc_stations;
179 return out;
180 }
181 for (std::size_t i = 0; i < on_path.size(); ++i)
182 if (on_path[i].first == cur && on_path[i].second == cls)
183 throw UnsupportedError(ErrorKind{"Fork"},
184 "getAvgSys: the fork-join section of node '" + sn.nodes[cur - 1].name +
185 "' contains a routing cycle, so the set of paths from the fork to the join is "
186 "infinite and the synchronization delay has no inclusion-exclusion form");
187 on_path.push_back(std::make_pair(cur, cls));
188
189 T here = zero;
190 std::vector<std::size_t> stations = acc_stations;
191 const std::size_t ist = sn.nodes[cur - 1].station;
192 if (ist != 0) {
193 here = RN(ist - 1, cls - 1);
194 stations.push_back(ist);
195 }
196
197 const std::size_t row = (cur - 1) * K + (cls - 1);
198 for (std::size_t nxt = 1; nxt <= N; ++nxt) {
199 for (std::size_t s = 1; s <= K; ++s) {
200 if (row >= P.rows()) break;
201 const std::size_t col = (nxt - 1) * K + (s - 1);
202 if (col >= P.cols()) continue;
203 if (num_traits<T>::to_double(P(row, col)) == 0.0) continue;
204
205 std::size_t hop = nxt;
206 T entry = T(elapsed + here);
207 if (sn.nodes[nxt - 1].nodetype == lang::NodeType::Fork) {
208 std::size_t inner_join = 0;
209 for (std::size_t k = 0; k < sn.fj.size(); ++k)
210 if (sn.fj[k].first == nxt) inner_join = sn.fj[k].second;
211 if (inner_join != 0) {
212 const std::size_t jst = sn.nodes[inner_join - 1].station;
213 if (jst != 0 && num_traits<T>::to_double(RN(jst - 1, s - 1)) == 0.0) {
214 std::vector<std::pair<std::size_t, std::size_t>> inner_path;
215 const PathsResult<T> in =
216 paths_cs(sn, P, nxt, inner_join, s, RN, zero,
217 std::vector<std::size_t>(), inner_path);
218 RN(jst - 1, s - 1) = exp_max_mean(in.times);
219 for (std::size_t q = 0; q < in.stations.size(); ++q)
220 RN(in.stations[q] - 1, s - 1) = zero;
221 }
222 hop = inner_join;
223 }
224 }
225 const PathsResult<T> sub = paths_cs(sn, P, hop, stop, s, RN, entry, stations, on_path);
226 out.times.insert(out.times.end(), sub.times.begin(), sub.times.end());
227 out.stations.insert(out.stations.end(), sub.stations.begin(), sub.stations.end());
228 }
229 }
230 on_path.pop_back();
231 return out;
232}
233
234} // namespace chain_detail
235
236/** `@@NetworkSolver/getAvgSys`: one response time and one throughput per chain. */
237template <class T>
238struct SysResult {
239 std::vector<T> CN; ///< (nchains) system response time, i.e. the cycle time
240 std::vector<T> XN; ///< (nchains) system throughput at the reference station
241};
242
243/** The station- or node-level table aggregated by chain. */
244template <class T>
246 Matrix<T> QN, UN, RN, WN, AN, TN; ///< (rows x nchains), rows = stations or nodes
247};
248
249/**
250 * Port of `@@NetworkSolver/getAvgSys.m`.
251 *
252 * FORK-JOIN WITH AN OPEN CHAIN IS SERVED (2026-08-15). The reference fills the
253 * join station's response time with the order statistic of the parallel branch
254 * times -- `d0`, an inclusion-exclusion sum over every path from the fork to
255 * the join, enumerated by `ModelAdapter.pathsCS` -- and both halves of that are
256 * ported above as `chain_detail::paths_cs` and `chain_detail::exp_max_mean`.
257 * A CLOSED fork-join chain needs neither: its cycle time comes from Little's
258 * law, which reads the population and the throughput and never touches the
259 * join's response time, and the reference skips the walk there too. The JAR
260 * (`NetworkSolver.forkJoinPaths`) and Python (`_forkJoinSyncDelay`) carry the
261 * same port since 2026-11-27.
262 */
263template <class T>
265 const std::size_t M = sn.nstations, K = sn.nclasses, C = sn.nchains;
266 const T zero = num_traits<T>::from_int(0);
267 const std::vector<double> njobs = sn.njobs();
268
269 bool has_join = false, has_fork = false;
270 for (std::size_t i = 0; i < sn.nodes.size(); ++i) {
271 if (sn.nodes[i].nodetype == lang::NodeType::Join) has_join = true;
272 if (sn.nodes[i].nodetype == lang::NodeType::Fork) has_fork = true;
273 }
274
275 // `RN(join, :) = 0` of the reference, applied before anything reads RN: the
276 // join holds no service, and whatever the solver reported there is the
277 // synchronization wait, which the cycle time accounts for at the fork.
278 Matrix<T> RN = r.RN;
279 if (has_join)
280 for (std::size_t i = 0; i < M; ++i)
281 if (sn.stations[i].nodetype == lang::NodeType::Join)
282 for (std::size_t c = 0; c < K; ++c) RN(i, c) = zero;
283
284 // ---- The synchronization delay of each fork-join section, on OPEN chains
285 // only. A closed chain gets its cycle time from Little's law, which never
286 // reads the join's response time, so the reference skips the whole walk
287 // there and so does this.
288 //
289 // ORDER MATTERS: this rewrites RN -- the join gets the delay, the branch
290 // stations get zero -- and CNclass below reads RN. Computing CNclass first
291 // would total the branch times ALONG the paths, which double-counts the
292 // parallel section instead of taking its maximum.
293 if (has_fork && has_join) {
294 for (std::size_t f = 0; f < sn.fj.size(); ++f) {
295 const std::size_t fork = sn.fj[f].first, join = sn.fj[f].second;
296 if (fork == 0 || join == 0) continue;
297 const std::size_t jst = sn.nodes[join - 1].station;
298 if (jst == 0) continue;
299 for (std::size_t c = 0; c < C; ++c) {
300 double nJobsChain = 0.0;
301 for (std::size_t k = 0; k < K; ++k)
302 if (sn.chains[c][k]) nJobsChain += njobs[k];
303 if (!std::isinf(nJobsChain)) continue;
304 for (std::size_t q = 0; q < sn.inchain[c].size(); ++q) {
305 const std::size_t rr = sn.inchain[c][q];
306 if (num_traits<T>::to_double(RN(jst - 1, rr - 1)) != 0.0) continue;
307 std::vector<std::pair<std::size_t, std::size_t>> on_path;
308 const chain_detail::PathsResult<T> paths =
309 chain_detail::paths_cs(sn, sn.rtnodes, fork, join, rr, RN, zero,
310 std::vector<std::size_t>(), on_path);
311 if (paths.times.empty()) continue;
312 // d0 already accounts for the time spent on the branches,
313 // which is why they are zeroed rather than left to be added.
314 RN(jst - 1, rr - 1) = chain_detail::exp_max_mean(paths.times);
315 for (std::size_t j = 0; j < paths.stations.size(); ++j)
316 RN(paths.stations[j] - 1, rr - 1) = zero;
317 }
318 }
319 }
320 }
321
322 // ---- CNclass: the per-class system time, visits-weighted to the reference
323 // station. Computed for every model, used only by the open branch below,
324 // exactly as the reference computes it.
325 std::vector<T> CNclass(K, zero);
326 for (std::size_t c = 0; c < C; ++c) {
327 for (std::size_t j = 0; j < sn.inchain[c].size(); ++j) {
328 const std::size_t rr = sn.inchain[c][j]; // 1-based class
329 const std::size_t refst = sn.classes[rr - 1].refstat;
330 if (refst == 0) continue;
331 const std::size_t refsf = sn.stateful_of_station(refst);
332 const T vref = sn.visits[c](refsf - 1, rr - 1);
333 if (num_traits<T>::to_double(vref) == 0.0) continue;
334 for (std::size_t i = 0; i < M; ++i) {
335 // The source of an open class is not part of its system time.
336 if (std::isinf(njobs[rr - 1]) && i + 1 == refst) continue;
337 const std::size_t isf = sn.stateful_of_station(i + 1);
338 CNclass[rr - 1] =
339 T(CNclass[rr - 1] + T(T(sn.visits[c](isf - 1, rr - 1) * RN(i, rr - 1)) / vref));
340 }
341 }
342 }
343
344 // ---- alpha: the share of its chain's reference-station completions that a
345 // class accounts for, per station. `refclass` is indexed by CHAIN and
346 // `inchain` holds CLASS indices; the reference intersects the two anyway,
347 // and this port reproduces that intersection rather than repairing it,
348 // because the weights it produces are the ones every reference number was
349 // computed with.
350 Matrix<T> alpha(M, K, zero);
351 std::vector<std::size_t> refclass_nz;
352 for (std::size_t c = 0; c < C; ++c)
353 if (sn.refclass[c] != 0) refclass_nz.push_back(c + 1);
354 for (std::size_t c = 0; c < C; ++c) {
355 // The classes of the chain that COMPLETE at the reference station: a
356 // non-completing class passes through without ending a cycle, so it
357 // contributes visits but no completions to divide by.
358 std::vector<std::size_t> completing;
359 for (std::size_t k = 0; k < K; ++k)
360 if (sn.chains[c][k] && sn.classes[k].completes) completing.push_back(k + 1);
361
362 std::vector<std::size_t> ks;
363 for (std::size_t j = 0; j < refclass_nz.size(); ++j)
364 for (std::size_t q = 0; q < sn.inchain[c].size(); ++q)
365 if (refclass_nz[j] == sn.inchain[c][q]) ks.push_back(refclass_nz[j]);
366 if (sn.refclass[c] == 0) ks = sn.inchain[c];
367
368 for (std::size_t i = 0; i < M; ++i) {
369 for (std::size_t j = 0; j < ks.size(); ++j) {
370 const std::size_t k = ks[j];
371 const std::size_t refst = sn.classes[k - 1].refstat;
372 if (refst == 0) continue;
373 const std::size_t refsf = sn.stateful_of_station(refst);
374 T denom = zero;
375 for (std::size_t q = 0; q < completing.size(); ++q)
376 denom = T(denom + sn.visits[c](refsf - 1, completing[q] - 1));
377 if (num_traits<T>::to_double(denom) == 0.0) continue;
378 const std::size_t isf = sn.stateful_of_station(i + 1);
379 alpha(i, k - 1) = T(alpha(i, k - 1) + T(sn.visits[c](isf - 1, k - 1) / denom));
380 }
381 }
382 }
383 // `alpha(~isfinite(alpha)) = 0`: a class that never reaches the reference
384 // station has no share, and a non-finite weight would poison the sum.
385 for (std::size_t i = 0; i < M; ++i)
386 for (std::size_t k = 0; k < K; ++k)
387 if (!std::isfinite(num_traits<T>::to_double(alpha(i, k)))) alpha(i, k) = zero;
388
389 SysResult<T> out;
390 out.CN.assign(C, zero);
391 out.XN.assign(C, zero);
392 const std::size_t nsf = sn.nof_stateful();
393
394 for (std::size_t c = 0; c < C; ++c) {
395 const std::vector<std::size_t>& inchain = sn.inchain[c];
396 if (inchain.empty()) continue;
397 std::vector<std::size_t> completing;
398 for (std::size_t k = 0; k < K; ++k)
399 if (sn.chains[c][k] && sn.classes[k].completes) completing.push_back(k + 1);
400
401 // ---- XN: the completing flow INTO the chain's reference station.
402 // Read off the routing matrix rather than from any one station's
403 // throughput, because a chain completes wherever its routing returns to
404 // the reference station and that can be several places at once.
405 const std::size_t ref = sn.classes[inchain[0] - 1].refstat;
406 if (ref != 0 && r.TN.rows() == M) {
407 const std::size_t refsf = sn.stateful_of_station(ref);
408 std::vector<std::size_t> ss;
409 for (std::size_t j = 0; j < refclass_nz.size(); ++j)
410 for (std::size_t q = 0; q < inchain.size(); ++q)
411 if (refclass_nz[j] == inchain[q]) ss.push_back(refclass_nz[j]);
412 if (ss.empty()) ss = inchain;
413 for (std::size_t i = 0; i < M; ++i) {
414 const std::size_t isf = sn.stateful_of_station(i + 1);
415 if (isf == 0 || isf > nsf) continue;
416 for (std::size_t q = 0; q < completing.size(); ++q) {
417 const std::size_t rr = completing[q];
418 const double tn = num_traits<T>::to_double(r.TN(i, rr - 1));
419 if (std::isnan(tn)) continue;
420 for (std::size_t j = 0; j < ss.size(); ++j) {
421 const std::size_t s = ss[j];
422 out.XN[c] = T(out.XN[c] + T(sn.rt((isf - 1) * K + (rr - 1),
423 (refsf - 1) * K + (s - 1)) *
424 r.TN(i, rr - 1)));
425 }
426 }
427 }
428 }
429
430 // ---- CN: Little's law on a closed chain, the alpha-weighted sum of the
431 // per-class system times on an open one.
432 double nJobsChain = 0.0;
433 for (std::size_t k = 0; k < K; ++k)
434 if (sn.chains[c][k]) nJobsChain += njobs[k];
435
436 if (std::isinf(nJobsChain)) {
437 if (inchain.size() != completing.size())
438 throw UnsupportedError(ErrorKind{"OpenClass"},
439 "getAvgSys: edge-based chain definition is not supported for open queueing "
440 "networks -- the chain holds a non-completing class, so there is no single "
441 "flow whose reciprocal is the cycle time");
442 const std::size_t refst = sn.classes[inchain[0] - 1].refstat;
443 T acc = zero;
444 for (std::size_t j = 0; j < inchain.size(); ++j) {
445 const T v = T(alpha(refst - 1, inchain[j] - 1) * CNclass[inchain[j] - 1]);
446 // `sumfinite`: a class the solver reported no finite time for is
447 // skipped, not propagated as Inf over the whole chain.
448 if (std::isfinite(num_traits<T>::to_double(v))) acc = T(acc + v);
449 }
450 out.CN[c] = acc;
451 } else {
452 const double x = num_traits<T>::to_double(out.XN[c]);
453 if (x != 0.0) {
454 out.CN[c] = T(num_traits<T>::from_double(nJobsChain) / out.XN[c]);
455 } else if (nJobsChain > 0.0) {
456 // N/0 of the reference: a populated chain that never completes (trapped at a
457 // zero-capacity station) has an infinite cycle time, not a zero one.
458 if constexpr (num_traits<T>::is_exact)
459 throw UnsupportedError(ErrorKind{"Arithmetic"},
460 "getAvgSys: the chain holds jobs but completes none, so its cycle time is "
461 "infinite, which exact arithmetic cannot represent; rerun in double");
462 else
463 out.CN[c] = num_traits<T>::from_double(std::numeric_limits<double>::infinity());
464 }
465 }
466 }
467 return out;
468}
469
470/**
471 * Port of `@@NetworkSolver/getAvgChain.m`: the station table aggregated by chain.
472 *
473 * QLen, Util, ArvR, Tput and ResidT are row sums over the chain's classes;
474 * RespT is the alpha-weighted average, alpha being `sn_get_demands_chain`'s
475 * visit share. See the file header for why the two rules differ.
476 */
477template <class T>
479 const mva::AvgResult<T>& r) {
480 const std::size_t M = sn.nstations, C = sn.nchains;
481 const T zero = num_traits<T>::from_int(0);
483
484 ChainResult<T> out;
485 out.QN = Matrix<T>(M, C, zero);
486 out.UN = Matrix<T>(M, C, zero);
487 out.RN = Matrix<T>(M, C, zero);
488 out.WN = Matrix<T>(M, C, zero);
489 out.AN = Matrix<T>(M, C, zero);
490 out.TN = Matrix<T>(M, C, zero);
491 for (std::size_t c = 0; c < C; ++c) {
492 for (std::size_t j = 0; j < sn.inchain[c].size(); ++j) {
493 const std::size_t k = sn.inchain[c][j] - 1;
494 for (std::size_t i = 0; i < M; ++i) {
495 out.QN(i, c) = T(out.QN(i, c) + r.QN(i, k));
496 out.UN(i, c) = T(out.UN(i, c) + r.UN(i, k));
497 out.WN(i, c) = T(out.WN(i, c) + r.WN(i, k));
498 out.AN(i, c) = T(out.AN(i, c) + r.AN(i, k));
499 out.TN(i, c) = T(out.TN(i, c) + r.TN(i, k));
500 out.RN(i, c) = T(out.RN(i, c) + T(r.RN(i, k) * dem.alpha(i, k)));
501 }
502 }
503 }
504 return out;
505}
506
507/**
508 * Port of `@@NetworkSolver/getAvgNodeChain.m`: the NODE table aggregated by chain.
509 *
510 * The node-level class matrices are the caller's, because the scatter from
511 * stations to nodes and the recomputation of ArvR and Tput per node is
512 * `getAvgNodeTable`'s work and is not repeated here. Rows that are not stations
513 * carry zero response and residence time, which is the reference's construction
514 * and not a gap: a node that is not a station holds no jobs.
515 */
516template <class T>
518 const Matrix<T>& UNn, const Matrix<T>& RNn,
519 const Matrix<T>& WNn, const Matrix<T>& ANn,
520 const Matrix<T>& TNn) {
521 const std::size_t I = sn.nodes.size(), C = sn.nchains;
522 const T zero = num_traits<T>::from_int(0);
524
525 ChainResult<T> out;
526 out.QN = Matrix<T>(I, C, zero);
527 out.UN = Matrix<T>(I, C, zero);
528 out.RN = Matrix<T>(I, C, zero);
529 out.WN = Matrix<T>(I, C, zero);
530 out.AN = Matrix<T>(I, C, zero);
531 out.TN = Matrix<T>(I, C, zero);
532 // Node -> station, so the alpha weights (which are indexed by station) can
533 // be applied to the response time column.
534 std::vector<std::size_t> node_to_station(I, 0);
535 for (std::size_t ist = 0; ist < sn.nstations; ++ist) {
536 const std::size_t ind = sn.station_to_node[ist];
537 if (ind) node_to_station[ind - 1] = ist + 1;
538 }
539 for (std::size_t c = 0; c < C; ++c) {
540 for (std::size_t j = 0; j < sn.inchain[c].size(); ++j) {
541 const std::size_t k = sn.inchain[c][j] - 1;
542 for (std::size_t i = 0; i < I; ++i) {
543 out.QN(i, c) = T(out.QN(i, c) + QNn(i, k));
544 out.UN(i, c) = T(out.UN(i, c) + UNn(i, k));
545 out.AN(i, c) = T(out.AN(i, c) + ANn(i, k));
546 out.TN(i, c) = T(out.TN(i, c) + TNn(i, k));
547 const std::size_t ist = node_to_station[i];
548 if (!ist) continue;
549 out.RN(i, c) = T(out.RN(i, c) + T(RNn(i, k) * dem.alpha(ist - 1, k)));
550 // a residence time already carries the visits: the chain's is the plain sum, as in
551 // solver_get_avg_chain; weighting it by alpha counted the visits twice
552 out.WN(i, c) = T(out.WN(i, c) + WNn(i, k));
553 }
554 }
555 }
556 return out;
557}
558
559/** `Chain1`, `Chain2`, ... -- the reference's own chain labels. */
560inline std::vector<std::string> chain_names(std::size_t nchains) {
561 std::vector<std::string> out;
562 for (std::size_t c = 0; c < nchains; ++c) out.push_back("Chain" + std::to_string(c + 1));
563 return out;
564}
565
566/** `(ClassA ClassB)`, the JobClasses column: which classes a chain holds. */
567template <class T>
568std::vector<std::string> chain_class_labels(const qn::NetworkStruct<T>& sn) {
569 std::vector<std::string> out;
570 for (std::size_t c = 0; c < sn.nchains; ++c) {
571 std::string s = "(";
572 for (std::size_t j = 0; j < sn.inchain[c].size(); ++j) {
573 if (j) s += " ";
574 s += sn.classes[sn.inchain[c][j] - 1].name;
575 }
576 out.push_back(s + ")");
577 }
578 return out;
579}
580
581} // namespace solvers
582} // namespace line
583
584#endif // LINE_SOLVERS_SOLVER_CHAIN_TABLES_H
NumericError(const std::string &what)
Definition error.h:112
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
The exception types the port throws.
Dense matrix and non-owning view.
bool in(const std::string &s, std::initializer_list< const char * > set)
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::string > chain_class_labels(const qn::NetworkStruct< T > &sn)
(ClassA ClassB), the JobClasses column: which classes a chain holds.
SysResult< T > solver_get_avg_sys(const qn::NetworkStruct< T > &sn, const mva::AvgResult< T > &r)
Port of @@NetworkSolver/getAvgSys.m.
ChainResult< T > solver_get_avg_node_chain(const qn::NetworkStruct< T > &sn, const Matrix< T > &QNn, const Matrix< T > &UNn, const Matrix< T > &RNn, const Matrix< T > &WNn, const Matrix< T > &ANn, const Matrix< T > &TNn)
Port of @@NetworkSolver/getAvgNodeChain.m: the NODE table aggregated by chain.
ChainResult< T > solver_get_avg_chain(const qn::NetworkStruct< T > &sn, const mva::AvgResult< T > &r)
Port of @@NetworkSolver/getAvgChain.m: the station table aggregated by chain.
std::vector< std::string > chain_names(std::size_t nchains)
Chain1, Chain2, ... – the reference's own chain labels.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
Number-type abstraction for the templated API port.
Chain aggregation and de-aggregation.
The SolverMVA class surface: @@SolverMVA/runAnalyzer.m and the gates around it.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
The metrics getAvg returns, after filtering.
Matrix< T > TN
throughput
Matrix< T > RN
response time, per visit
Matrix< T > UN
utilization
Matrix< T > WN
residence time, per job
Matrix< T > QN
queue length
Matrix< T > AN
arrival rate
The chain-level view of a layer, as sn_get_demands_chain returns it.
Definition sn_chain.h:46
Matrix< T > alpha
(M x K) class share of its chain's visits at a station
Definition sn_chain.h:50
The station- or node-level table aggregated by chain.
Matrix< T > TN
(rows x nchains), rows = stations or nodes
@@NetworkSolver/getAvgSys: one response time and one throughput per chain.
std::vector< T > XN
(nchains) system throughput at the reference station
std::vector< T > CN
(nchains) system response time, i.e. the cycle time