LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
ldes_getavg.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_LDES_LDES_GETAVG_H
6#define LINE_SOLVERS_LDES_LDES_GETAVG_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * What `NetworkSolver.getAvg` does to an LDES engine result before anyone reads it.
12 *
13 * The reference's `LdesCLI` writes its document after `solver.getAvg()`, so the
14 * station tables a caller sees are not the engine's raw sample-path means: WN is
15 * the per-JOB residence time and saturated open stations are capped. Every C++
16 * caller that reports LDES means applies the same post-processing here, so that
17 * `ldes -s ...` and a SolverUQ design point solved by LDES agree column for
18 * column (moved out of `ldes_cli.cpp` on 2026-11-29 for the UQ stage).
19 */
20
21#include <cmath>
22#include <cstddef>
23#include <limits>
24#include <vector>
25
30#include "line/util/matrix.h"
31
32namespace line {
33namespace ldes {
34
35/**
36 * The instability cap the reference document carries: `LdesCLI` writes after `solver.getAvg()`, and
37 * `NetworkSolver.getAvg` caps every finite-server open station whose offered load
38 * rho_open = sum over open r of TN(i,r) / (c * lldpeak * rate(i,r)) reaches 1. Its UN becomes rho_r / rho_open for
39 * the open classes and 0 for the closed ones (station total 1), and QN, RN and WN of its open classes become Inf
40 * (WN since 2026-11-29); TN and AN of the open classes are left alone. A closed chain visiting a saturated station
41 * gets zero capacity, as `NetworkSolver.saturateClosedChains` gives it (chaos I21/I22, 2026-11-29).
42 * Exempt, as there: delays and the Source, a binding buffer (cap, classcap or a region), reneging, and a bulk
43 * (batch-service) station, whose `rates` is per firing rather than per job. It restates the Java rule rather than
44 * calling `mva::cap_unstable_open_stations`, which differs in the bulk exemption and would pull the MVA runner
45 * into the LDES CLI. Before 2026-11-14 the engine reported the raw simulated backlog of a saturated station (he_arr:
46 * QN 222.9 against the reference's Inf).
47 */
49 const std::size_t M = L.nstations, K = L.nclasses;
50 if (r.UN.rows() != M || r.TN.rows() != M || r.UN.cols() != K || r.TN.cols() != K) return;
51 bool any_open = false;
52 for (std::size_t k = 0; k < K; ++k)
53 if (std::isinf(L.classes[k].population)) any_open = true;
54 if (!any_open) return;
55 const double dinf = std::numeric_limits<double>::infinity();
56 std::vector<bool> sat(M, false);
57 for (std::size_t i = 0; i < M; ++i) {
58 const double c = L.stations[i].nservers;
59 if (std::isinf(c) || c <= 0.0) continue;
60 if (L.stations[i].nodetype == line::qn::NodeType::Source) continue;
61 bool exempt = (i < L.cap.size() && std::isfinite(L.cap[i]) && L.cap[i] >= 0.0);
62 // Only an OPEN class's classcap is a buffer: a closed class's is its
63 // chain population at every station (chaos I21).
64 if (!exempt && i < L.classcap.size())
65 for (std::size_t k = 0; k < L.classcap[i].size() && k < K; ++k)
66 if (std::isinf(L.classes[k].population) && std::isfinite(L.classcap[i][k]) &&
67 L.classcap[i][k] >= 0.0) {
68 exempt = true;
69 break;
70 }
71 if (!exempt)
72 for (const auto& rg : L.regions)
73 if (i < rg.members.size() && rg.members[i]) { exempt = true; break; }
74 if (!exempt)
75 for (line::lang::ImpatienceType it : L.stations[i].impatience)
76 if (it == line::lang::ImpatienceType::RENEGING) { exempt = true; break; }
77 if (!exempt)
78 for (std::size_t k = 0; k < K; ++k)
79 if (line::lang::process_is_batch(L.procid(i + 1, k + 1))) { exempt = true; break; }
80 if (exempt) continue;
81 double lldpeak = 1.0;
82 for (double v : L.stations[i].lldscaling)
83 if (!std::isnan(v) && !std::isinf(v) && v > lldpeak) lldpeak = v;
84 std::vector<double> rho(K, 0.0);
85 double rho_open = 0.0, rho_tot = 0.0;
86 bool has_open = false;
87 for (std::size_t k = 0; k < K; ++k) {
88 const double rate = L.rates(i, k), t = r.TN(i, k);
89 if (rate > 0.0 && t > 0.0) {
90 rho[k] = t / (c * lldpeak * rate);
91 rho_tot += rho[k];
92 if (std::isinf(L.classes[k].population)) {
93 rho_open += rho[k];
94 has_open = true;
95 }
96 }
97 }
98 if (!(has_open && rho_open >= 1.0 && rho_tot > 0.0)) continue;
99 sat[i] = true;
100 for (std::size_t k = 0; k < K; ++k) {
101 const bool open = std::isinf(L.classes[k].population);
102 // the open classes carry the whole capped total; a closed class gets none
103 r.UN(i, k) = open ? rho[k] / rho_open : 0.0;
104 if (open) {
105 if (r.RN.rows() == M && r.RN.cols() == K) r.RN(i, k) = dinf;
106 if (r.QN.rows() == M && r.QN.cols() == K) r.QN(i, k) = dinf;
107 if (r.WN.rows() == M && r.WN.cols() == K) r.WN(i, k) = dinf;
108 }
109 }
110 }
111 // A CLOSED CHAIN GETS NO CAPACITY AT A SATURATED STATION (chaos I21/I22),
112 // as NetworkSolver.saturateClosedChains does to the reference document:
113 // T, U and A 0 everywhere, the population split by visits over the
114 // saturated stations it visits, R and W Inf there and 0 elsewhere.
115 for (std::size_t ch = 0; ch < L.nchains; ++ch) {
116 std::vector<std::size_t> inchain;
117 bool open = false;
118 double pop = 0.0;
119 for (std::size_t k = 0; k < K; ++k) {
120 if (!L.chains[ch][k]) continue;
121 inchain.push_back(k);
122 if (std::isinf(L.classes[k].population)) open = true;
123 else pop += L.classes[k].population;
124 }
125 if (open || inchain.empty() || ch >= L.visits.size()) continue;
126 std::vector<std::vector<double>> vsat(M, std::vector<double>(inchain.size(), 0.0));
127 double vtot = 0.0;
128 for (std::size_t i = 0; i < M; ++i) {
129 if (!sat[i]) continue;
130 const std::size_t isf = L.stateful_of_station(i + 1) - 1;
131 for (std::size_t j = 0; j < inchain.size(); ++j) {
132 vsat[i][j] = L.visits[ch](isf, inchain[j]);
133 vtot += vsat[i][j];
134 }
135 }
136 if (!(vtot > 0.0)) continue;
137 for (std::size_t i = 0; i < M; ++i)
138 for (std::size_t j = 0; j < inchain.size(); ++j) {
139 const std::size_t k = inchain[j];
140 const double t = vsat[i][j] > 0.0 ? dinf : 0.0;
141 r.TN(i, k) = 0.0;
142 r.UN(i, k) = 0.0;
143 if (r.AN.rows() == M && r.AN.cols() == K) r.AN(i, k) = 0.0;
144 if (r.QN.rows() == M && r.QN.cols() == K) r.QN(i, k) = pop * vsat[i][j] / vtot;
145 if (r.RN.rows() == M && r.RN.cols() == K) r.RN(i, k) = t;
146 if (r.WN.rows() == M && r.WN.cols() == K) r.WN(i, k) = t;
147 }
148 }
149}
150
151/**
152 * The per-JOB residence time the reference document carries as WN: `snGetResidTFromRespT`, which `LdesCLI` applies
153 * through `solver.getAvg()` before writing. RN is per visit, so it is scaled by this station's visits over the
154 * reference station's visits of the chain's reference class (or of every class in the chain when the chain names
155 * none); an Immediate class (RN below FineTol) passes through and values below 10*FineTol read as zero. It restates
156 * `mva::sn_get_residt_from_respt` rule for rule, because that header pulls the whole MVA runner into `ldes_cli.cpp`,
157 * which otherwise compiles the engine alone (the build went past ten minutes with it), and must change with it.
158 */
160 const std::size_t M = L.nstations, K = L.nclasses;
161 line::Matrix<double> V(M, K, 0.0);
162 for (std::size_t c = 0; c < L.nchains; ++c)
163 for (std::size_t i = 0; i < M; ++i) {
164 const std::size_t sf = L.stateful_of_station(i + 1);
165 for (std::size_t k = 0; k < K; ++k) V(i, k) += L.visits[c](sf - 1, k);
166 }
167 const double fine = line::lang::GlobalConstants::FineTol;
168 line::Matrix<double> WN(M, K, 0.0);
169 for (std::size_t i = 0; i < M; ++i)
170 for (std::size_t k = 0; k < K; ++k) {
171 const bool is_place = L.stations[i].nodetype == line::qn::NodeType::Place;
172 if ((L.disabled[i][k] && !is_place) || !(RN(i, k) > 0.0)) continue;
173 if (RN(i, k) < fine) {
174 WN(i, k) = RN(i, k);
175 continue;
176 }
177 std::size_t c = L.nchains;
178 for (std::size_t cc = 0; cc < L.nchains; ++cc)
179 if (L.chains[cc][k]) c = cc;
180 if (c == L.nchains) continue;
181 const std::size_t rs = L.classes[k].refstat;
182 double den = 0.0;
183 if (L.refclass[c] > 0) {
184 den = V(rs - 1, L.refclass[c] - 1);
185 } else {
186 for (std::size_t r : L.inchain[c]) den += V(rs - 1, r - 1);
187 }
188 if (den > 0.0) WN(i, k) = RN(i, k) * V(i, k) / den;
189 }
190 for (std::size_t i = 0; i < M; ++i)
191 for (std::size_t k = 0; k < K; ++k)
192 if (WN(i, k) < 10.0 * fine) WN(i, k) = 0.0;
193 return WN;
194}
195
196/**
197 * `getAvg` applied to an engine result in place: WN becomes the per-job
198 * residence time and AN/WN are re-derived at the stations a JSQ/SQ dispatcher
199 * feeds, WNCI is scaled with WN, and the instability cap is applied last.
200 *
201 * WN IS THE PER-JOB RESIDENCE TIME, not the per-visit response time: the
202 * reference's `snGetResidTFromRespT` scales RN by V(i,r)/V(ref) and its
203 * `SnArvRStateDependentBalance` then re-derives AN and WN. The engine's
204 * single-visit WN = RN reported half the reference on a Bernoulli feedback loop
205 * with V = 2 (imf_off: 4.90676 against 9.81351 at seed 23000; closed 2026-11-14).
206 */
208 if (r.RN.rows() == sn.nstations && r.RN.cols() == sn.nclasses) {
210 if (r.AN.rows() == sn.nstations && r.TN.rows() == sn.nstations)
212 }
213 // WNCI: WN is RN times the visit ratio, so its half-width scales by the same
214 // WN/RN. It used to be left at 0; the JAR copies RNCI, which is the same
215 // number at a single-visit station and too narrow under feedback (closed 2026-11-14).
216 if (r.RNCI.rows() == r.RN.rows() && r.RNCI.cols() == r.RN.cols() &&
217 r.WN.rows() == r.RN.rows() && r.WN.cols() == r.RN.cols()) {
218 r.WNCI = r.RNCI;
219 for (std::size_t i = 0; i < r.RN.rows(); ++i)
220 for (std::size_t k = 0; k < r.RN.cols(); ++k) {
221 const double rn = r.RN(i, k), wn = r.WN(i, k);
222 if (rn > 0.0 && std::isfinite(rn) && std::isfinite(wn)) r.WNCI(i, k) = r.RNCI(i, k) * (wn / rn);
223 }
224 }
226}
227
228} // namespace ldes
229} // namespace line
230
231#endif // LINE_SOLVERS_LDES_LDES_GETAVG_H
std::size_t cols() const
Definition matrix.h:90
std::size_t rows() const
Definition matrix.h:89
A network plus its refreshed NetworkStruct.
std::size_t stateful_of_station(std::size_t st) const
std::vector< std::vector< bool > > chains
(nchains x nclasses)
std::vector< std::size_t > refclass
(nchains) 1-based class, 0 = none
std::vector< std::vector< bool > > disabled
Matrix< double > procid() const
sn.procid (M x K): the ProcessType id of each (station, class) pair, as refreshProcessTypes....
std::vector< double > cap
sn.cap and sn.classcap: the total and per-class buffers.
std::vector< JobClass > classes
std::vector< Station< T > > stations
stations[k-1] is the k-th station
Matrix< T > rates
(nstations x nclasses) service rates and SCVs, with a PARALLEL disabled flag instead of MATLAB's NaN ...
std::vector< std::vector< std::size_t > > inchain
1-based class indices per chain
std::vector< std::vector< double > > classcap
std::vector< Matrix< T > > visits
(nchains) each (nstateful x nclasses)
std::vector< Region > regions
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
The option and result records of SolverLDES, the discrete-event simulator.
Dense matrix and non-owning view.
bool process_is_batch(ProcessType p)
True when an EVENT of this process releases (or, as a service process, completes) a BATCH of jobs who...
Definition lang_types.h:776
ImpatienceType
Impatience kinds, with the values of MATLAB ImpatienceType.
Definition lang_types.h:488
line::Matrix< double > ldes_residt_from_respt(const line::qn::NetworkStruct< double > &L, const line::Matrix< double > &RN)
The per-JOB residence time the reference document carries as WN: snGetResidTFromRespT,...
void ldes_apply_getavg(const line::qn::NetworkStruct< double > &sn, LdesResult &r)
getAvg applied to an engine result in place: WN becomes the per-job residence time and AN/WN are re-d...
void ldes_cap_unstable_open_stations(const line::qn::NetworkStruct< double > &L, LdesResult &r)
The instability cap the reference document carries: LdesCLI writes after solver.getAvg(),...
Definition ldes_getavg.h:48
void sn_arvr_state_dependent_balance(const qn::NetworkStruct< T > &L, Matrix< T > &AN, const Matrix< T > &TN, Matrix< T > *WN)
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
Arrival rates at stations fed by a JSQ or SQ(d) dispatcher, from flow balance.
static constexpr double FineTol
Definition lang_types.h:828
One ldes-result document, parsed.
Matrix< double > UN
Matrix< double > TN
Matrix< double > RN
Matrix< double > WN
Matrix< double > AN
Matrix< double > RNCI
Matrix< double > WNCI
Matrix< double > QN