LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
solver_ncld.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_SOLVERS_NC_SOLVER_NCLD_H
6#define LINE_SOLVERS_NC_SOLVER_NCLD_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * Port of `solver_ncld.m`: the LOAD-DEPENDENT normalizing-constant analyzer.
12 *
13 * WHAT MAKES IT A DIFFERENT SOLVER. `solver_nc.h` assumes every station serves
14 * at a constant rate, so its constants come from `pfqn_nc`. Here each station
15 * carries a rate lattice mu_i(n) and the constants come from `pfqn_ncld`. That
16 * is not a refinement of the same formula: the arrival-theorem shortcut used
17 * there does not hold, and the queue length is recovered instead from the
18 * CONDITIONAL MVA identity
19 *
20 * Q_ic = exp(log L_ic + lG(mu^i, N-1_c) - log mu_i(1) - lG(N-1_c)) X_c (1 + CQ_i)
21 *
22 * where mu^i is the lattice shifted at station i (`pfqn_mushift`) and CQ_i is
23 * assembled from the flow-equivalent complement (`pfqn_fnc`). Four constants
24 * per (station, chain) rather than one, which is the price of load dependence.
25 *
26 * WHY MULTISERVER ARRIVES HERE. `@@SolverNC/runAnalyzer` rewrites a genuine
27 * c-server station as mu(n) = min(n, c) whenever the model is product-form, and
28 * that lattice is EXACT where Seidmann's approximation in `solver_nc.h` is not.
29 * The server count is deliberately kept alongside the lattice: utilization is
30 * the fraction of the c servers busy, and c cannot be read back from
31 * min(1:Nt, c) once the population falls below it.
32 *
33 * MIXED MODELS take a different route entirely -- the Bruell-Balbo-Ashfari
34 * effective-capacity MVA (`pfqn_mvaldmx`) -- because an open chain has no
35 * finite population for the constant to be evaluated at.
36 *
37 * ARITHMETIC. As in `solver_nc.h`, every measure is a difference of logarithms
38 * of normalizing constants; a non-transcendental backend is refused by name.
39 */
40
41#include <algorithm>
42#include <cmath>
43#include <limits>
44#include <string>
45#include <vector>
46
59#include "line/util/error.h"
61#include "line/util/matrix.h"
62
63namespace line {
64namespace nc {
65
66namespace detail {
67
68/**
69 * True when every finite multi-server station already carries mu(n)=min(n,c),
70 * i.e. the multiserver is fully described by the load-dependent rates.
71 *
72 * Port of the local `lld_encodes_multiserver` of the reference.
73 *
74 * The row is checked as far as a station's queue can reach: the closed
75 * population on a closed model (min(n,c)=n below it, so a row of width N
76 * suffices even when c > N), and at least c on an open or mixed one, whose open
77 * chains take the queue past any closed population. The open case used to be
78 * refused outright (Ntot < 1 on a purely open model), so a row the 'exact' arm
79 * had already converted was folded a SECOND time by ncld_fold_servers, giving a
80 * 2-server station the rates of min(n,2)^2.
81 */
82template <class T>
83bool lld_encodes_multiserver(const qn::NetworkStruct<T>& sn) {
84 double Ntot = 0.0;
85 bool hasOpen = false;
86 for (const qn::JobClass& c : sn.classes) {
87 if (std::isfinite(c.population))
88 Ntot += c.population;
89 else
90 hasOpen = true;
91 }
92 bool anyLld = false;
93 for (std::size_t i = 0; i < sn.nstations; ++i)
94 if (!sn.stations[i].lldscaling.empty()) anyLld = true;
95 if (!anyLld) return false;
96 if (!hasOpen && Ntot < 1.0) return false;
97 const std::size_t Nt = static_cast<std::size_t>(std::llround(Ntot));
98 for (std::size_t i = 0; i < sn.nstations; ++i) {
99 const double c = sn.stations[i].nservers;
100 if (!std::isfinite(c) || c <= 1.0) continue;
101 const std::vector<T>& lld = sn.stations[i].lldscaling;
102 const std::size_t Kc =
103 hasOpen ? std::max(Nt, static_cast<std::size_t>(std::floor(c))) : Nt;
104 if (lld.size() < Kc) return false;
105 for (std::size_t n = 1; n <= Kc; ++n)
106 if (num_traits<T>::to_double(lld[n - 1]) != std::min<double>(n, c)) return false;
107 }
108 return true;
109}
110
111/**
112 * Multiplies min(n,c) into the limited load dependence row of every finite
113 * multiserver station, in place. Every declared row is first widened, holding
114 * its last column, to reach both c and the closed population, so the
115 * saturation level that `pfqn_ldmx_ec` reads off the row stays where the
116 * station actually saturates; a multiserver station that declares no row gets
117 * one of ones before the product. Port of the local `ncld_fold_servers` of the
118 * reference.
119 */
120template <class T>
121void ncld_fold_servers(qn::NetworkStruct<T>& sn) {
122 const T one = num_traits<T>::from_int(1);
123 std::size_t w = 0;
124 for (std::size_t i = 0; i < sn.nstations; ++i) w = std::max(w, sn.stations[i].lldscaling.size());
125 double Ntot = 0.0;
126 for (const qn::JobClass& c : sn.classes)
127 if (std::isfinite(c.population)) Ntot += c.population;
128 std::size_t width = std::max(w, static_cast<std::size_t>(std::llround(Ntot)));
129 for (std::size_t i = 0; i < sn.nstations; ++i) {
130 const double c = sn.stations[i].nservers;
131 if (std::isfinite(c)) width = std::max(width, static_cast<std::size_t>(std::llround(c)));
132 }
133 for (std::size_t i = 0; i < sn.nstations; ++i) {
134 std::vector<T>& lld = sn.stations[i].lldscaling;
135 const double c = sn.stations[i].nservers;
136 const bool multi = std::isfinite(c) && c > 1.0;
137 if (lld.empty() && !multi) continue;
138 if (lld.empty()) lld.assign(width, one);
139 else if (lld.size() < width) lld.resize(width, lld.back());
140 if (multi)
141 for (std::size_t n = 1; n <= width; ++n)
142 lld[n - 1] = T(lld[n - 1] *
143 num_traits<T>::from_double(std::min<double>(static_cast<double>(n), c)));
144 }
145}
146
147/**
148 * First column (1-based) of the trailing constant run of a limited
149 * load-dependence row, i.e. the level b with mu(n) = mu(b) for every n >= b; 1
150 * on a flat row. This is the level `pfqn_ldmx_ec` infers from the row it is
151 * handed, so a row cut below it is read as a different, slower station.
152 */
153template <class T>
154std::size_t lld_saturation_level(const Matrix<T>& lldscaling, std::size_t ist) {
155 std::size_t b = lldscaling.cols();
156 if (b == 0) return 1;
157 while (b > 1 && lldscaling(ist, b - 2) == lldscaling(ist, b - 1)) --b;
158 return b;
159}
160
161} // namespace detail
162
163/**
164 * Port of `solver_ncld.m`.
165 *
166 * @param sn_in the refreshed struct; its lldscaling is completed in place on a
167 * two-station multiserver model, exactly as the reference does
168 * @param opt solver controls
169 */
170template <class T>
172 NcSolution<T> out;
173 if constexpr (!num_traits<T>::has_transcendental) {
174 (void)sn_in;
175 (void)opt;
176 throw UnsupportedError(ErrorKind{"Arithmetic"},
177 "solver_ncld: the load-dependent normalizing-constant analyzer forms "
178 "X = exp(lG(N-1_c) - lG(N)) and needs transcendental arithmetic; this backend has "
179 "none");
180 } else {
181 const T zero = num_traits<T>::from_int(0);
182 const T one = num_traits<T>::from_int(1);
183 qn::NetworkStruct<T> sn = sn_in;
184 const std::size_t M = sn.nstations, K = sn.nclasses, C = sn.nchains;
185
186 std::vector<double> nservers(M, 1.0);
187 std::vector<bool> isFCFS(M, false);
188 bool anyFCFS = false, anyMulti = false;
189 for (std::size_t i = 0; i < M; ++i) {
190 nservers[i] = sn.stations[i].nservers;
191 isFCFS[i] = sn.stations[i].sched == SchedStrategy::FCFS;
192 if (isFCFS[i]) anyFCFS = true;
193 if (std::isfinite(nservers[i]) && nservers[i] > 1.0) anyMulti = true;
194 }
195
196 double Ntot_d = 0.0;
197 bool allClosed = true;
198 for (const qn::JobClass& c : sn.classes) {
199 if (std::isinf(c.population))
200 allClosed = false;
201 else
202 Ntot_d += c.population;
203 }
204 const std::size_t Nt = static_cast<std::size_t>(
205 std::max<double>(1.0, std::ceil(std::isfinite(Ntot_d) ? Ntot_d : 1.0)));
206
207 // Class-dependent rates beta_{i,r}(n) AND joint-dependent rates eta_i(n)
208 // route to the convolution analyzer BEFORE anything else and
209 // unconditionally on the method: every algorithm below reads mu(n) from
210 // lldscaling alone and has no way to apply either, so gating this on
211 // 'exact' would silently return the UNSCALED network for every other
212 // method. The test read `cdscaling` alone until 2026-09-13, so a
213 // joint-dependent station fell through to those algorithms and was
214 // answered unscaled; `solver_nc_conv` folds eta into the same handle.
215 for (std::size_t i = 0; i < M; ++i)
216 if (static_cast<bool>(sn.stations[i].cdscaling) ||
217 static_cast<bool>(sn.stations[i].jdscaling))
218 return solver_nc_conv(sn, opt);
219
220 bool anyLld = false;
221 for (std::size_t i = 0; i < M; ++i)
222 if (!sn.stations[i].lldscaling.empty()) anyLld = true;
223
224 // A finite multiserver station beside a declared limited load
225 // dependence runs at min(n,c)*lld(n), as in SolverCTMC and SolverSSA.
226 // The lld row alone (ones at a station that declares none) dropped c,
227 // so the server count is folded into the rows here, before the gate
228 // below, which then accepts them as encoding the multiserver
229 // (MATLAB solver_ncld.m, local ncld_fold_servers).
230 bool msFolded = false;
231 if (anyLld && anyMulti && !detail::lld_encodes_multiserver(sn)) {
232 detail::ncld_fold_servers(sn);
233 msFolded = true;
234 }
235
236 if (anyMulti) {
237 if (!anyLld && M == 2 && allClosed) {
238 // Two stations and no lattice yet: express every station as
239 // mu(n) = min(n, c), which is what the reference installs here.
240 for (std::size_t i = 0; i < M; ++i) {
241 std::vector<T> lld(Nt, one);
242 for (std::size_t n = 1; n <= Nt; ++n)
243 lld[n - 1] = num_traits<T>::from_double(
244 std::min<double>(static_cast<double>(n), nservers[i]));
245 sn.stations[i].lldscaling = lld;
246 }
247 anyLld = true;
248 } else if (!msFolded && !detail::lld_encodes_multiserver(sn)) {
249 throw UnsupportedError(ErrorKind{"MultiServer"},
250 "solver_ncld: the load-dependent solver does not support multi-server "
251 "stations unless they are expressed as limited load dependence mu(n) = "
252 "min(n, c)");
253 }
254 }
255
256 // The rate lattice, defaulting to a constant rate of one. Its width is the
257 // wider of the closed population and the longest row the caller gave
258 // setLoadDependence: the mixed route below reads the saturation level off
259 // the row itself, and a row cut at Nt is read as a slower station. Past a
260 // row's own end the limited load dependence holds its last rate.
261 std::size_t Wlld = Nt;
262 for (std::size_t i = 0; i < M; ++i)
263 Wlld = std::max(Wlld, sn.stations[i].lldscaling.size());
264 Matrix<T> lldscaling(M, Wlld, one);
265 for (std::size_t i = 0; i < M; ++i) {
266 const std::vector<T>& lld = sn.stations[i].lldscaling;
267 if (lld.empty()) continue;
268 for (std::size_t n = 0; n < Wlld; ++n) lldscaling(i, n) = n < lld.size() ? lld[n] : lld.back();
269 }
270
272 Matrix<T> ST = d.ST, ST0 = d.ST;
273 const Matrix<T> V = detail::station_visits(sn);
274 Matrix<T> SCVnan = sn.scv;
275 for (std::size_t i = 0; i < M; ++i)
276 for (std::size_t k = 0; k < K; ++k)
277 if (sn.disabled[i][k])
278 SCVnan(i, k) =
279 num_traits<T>::from_double(std::numeric_limits<double>::quiet_NaN());
280
281 const std::vector<double> Nchain = detail::chain_population(sn);
282 std::vector<std::size_t> openChains, closedChains;
283 for (std::size_t c = 0; c < C; ++c)
284 (std::isinf(Nchain[c]) ? openChains : closedChains).push_back(c);
285 std::vector<int> Nnc = detail::nc_population(Nchain);
286 std::size_t Ncl = 0;
287 for (std::size_t c : closedChains) Ncl += static_cast<std::size_t>(Nnc[c]);
288
289 // The method travels as a NAME, not as an enum: pfqn_ncld and
290 // pfqn_ncldmx resolve it where the reference does, past their degenerate
291 // returns, so a model that reduces to a closed form is answered rather
292 // than refused for carrying a name the load-dependent ladder does not
293 // know. See pfqn_ncld.h.
294 const std::string& pmethod = opt.method;
295 // The estimator controls the log-domain ladder reads; the exact ladder
296 // ignores them, so this is passed unconditionally.
297 pfqn::NcOptions nopt;
298 nopt.samples = opt.samples;
299 nopt.seed = opt.seed;
300 nopt.tol = opt.tol;
301 const T atol = num_traits<T>::from_double(opt.tol);
302
303 std::vector<T> gamma(M, zero), nserv_t(M, one);
304 for (std::size_t i = 0; i < M; ++i) nserv_t[i] = num_traits<T>::from_double(nservers[i]);
305 std::vector<T> eta(M, one), eta_1(M, zero);
306 const int iter_max = anyFCFS ? opt.iter_max : 1;
307 int it = 0;
308 double lG = 0.0;
309 std::string actualmethod = opt.method;
311 std::vector<T> Xchain(C, zero);
312
313 while (it < iter_max) {
314 // Wall-clock budget (options.timeout): the last completed pass is the
315 // interim solution, as the reference's `if it>0 && lineTimeoutExceeded`.
316 if (it > 0 && util::timeout_exceeded()) break;
317 {
318 double dev = 0.0;
319 for (std::size_t i = 0; i < M; ++i) {
320 const double v = std::fabs(1.0 - num_traits<T>::to_double(eta[i]) /
321 num_traits<T>::to_double(eta_1[i]));
322 if (!(v <= dev)) dev = v;
323 }
324 if (!(dev > opt.iter_tol)) break;
325 }
326 ++it;
327 eta_1 = eta;
328
329 // Chain demands from the CURRENT service times.
330 for (std::size_t c = 0; c < C; ++c) {
331 const bool open = std::isinf(Nchain[c]);
332 const std::size_t rst = sn.classes[sn.inchain[c][0] - 1].refstat;
333 for (std::size_t i = 0; i < M; ++i) {
334 T st = zero;
335 for (std::size_t k : sn.inchain[c]) st += ST(i, k - 1) * d.alpha(i, k - 1);
336 d.Lchain(i, c) = T(d.Vchain(i, c) * st);
337 if (open && i + 1 == rst) {
338 // A source row carries 1 / arrival rate, summed over the
339 // classes whose rate is defined.
340 T s = zero;
341 for (std::size_t k : sn.inchain[c])
342 if (!sn.disabled[i][k - 1] &&
343 std::isfinite(num_traits<T>::to_double(ST(i, k - 1))))
344 s += ST(i, k - 1);
345 d.STchain(i, c) = s;
346 } else {
347 d.STchain(i, c) = st;
348 }
349 }
350 }
351 for (std::size_t i = 0; i < M; ++i)
352 for (std::size_t c = 0; c < C; ++c) {
353 if (!std::isfinite(num_traits<T>::to_double(d.STchain(i, c))))
354 d.STchain(i, c) = zero;
355 if (!std::isfinite(num_traits<T>::to_double(d.Lchain(i, c))))
356 d.Lchain(i, c) = zero;
357 }
358
359 std::vector<T> lambda(C, zero);
360 for (std::size_t c : openChains) {
361 const std::size_t rst = sn.classes[sn.inchain[c][0] - 1].refstat;
362 if (d.STchain(rst - 1, c) > zero) lambda[c] = T(one / d.STchain(rst - 1, c));
363 }
364
365 Matrix<T> L(M, C, zero), Z(M, C, zero), mu(M, Nt, one);
366 std::vector<std::size_t> infServers;
367 for (std::size_t i = 0; i < M; ++i) {
368 for (std::size_t c = 0; c < C; ++c) L(i, c) = d.Lchain(i, c);
369 if (std::isinf(nservers[i])) {
370 infServers.push_back(i);
371 for (std::size_t c = 0; c < C; ++c) Z(i, c) = d.Lchain(i, c);
372 for (std::size_t n = 1; n <= Nt; ++n)
373 mu(i, n - 1) = num_traits<T>::from_double(static_cast<double>(n));
374 } else {
375 for (std::size_t n = 0; n < Nt; ++n) mu(i, n) = lldscaling(i, n);
376 }
377 }
378
379 Matrix<T> Qchain(M, C, zero);
380 Xchain.assign(C, zero);
381
382 if (!openChains.empty()) {
383 // Mixed limited load-dependent network: the chain-level
384 // normalizing constant of Bruell-Balbo-Afshari effective
385 // capacity (pfqn_ncldmx), which never enumerates the closed
386 // population lattice. The source rows carry only the 1/lambda bookkeeping
387 // demand and are excluded from the queueing set; the delay rows
388 // fold into the think-time vector.
389 std::vector<bool> isSource(M, false), isDelay(M, false);
390 for (std::size_t c : openChains)
391 isSource[sn.classes[sn.inchain[c][0] - 1].refstat - 1] = true;
392 for (std::size_t i : infServers)
393 if (!isSource[i]) isDelay[i] = true;
394 std::vector<std::size_t> queueStations;
395 for (std::size_t i = 0; i < M; ++i)
396 if (!isSource[i] && !isDelay[i]) queueStations.push_back(i);
397 const std::size_t nq = queueStations.size();
398
399 Matrix<T> Zvec(1, C, zero);
400 for (std::size_t i = 0; i < M; ++i)
401 if (isDelay[i])
402 for (std::size_t c = 0; c < C; ++c) Zvec(0, c) += d.Lchain(i, c);
403
404 // pfqn_ldmx_ec reads the limited-load-dependence level b_i off the
405 // rate row itself -- the first column equal to the LAST one -- and
406 // treats every rate past it as saturated. Cutting the row at the
407 // closed population Ncl declares a c-server station saturated at
408 // min(n,c) with n < c whenever c exceeds Ncl (and with no closed
409 // class at all it flattens the row to mu(1)), so keep every column
410 // up to the start of each row's trailing constant run.
411 std::size_t ncol = std::max<std::size_t>(1, Ncl);
412 for (std::size_t qi = 0; qi < nq; ++qi)
413 ncol = std::max(ncol, detail::lld_saturation_level(lldscaling, queueStations[qi]));
414 Matrix<T> Dq(nq, C, zero), muq(nq, ncol, one);
415 const std::size_t Wq = lldscaling.cols();
416 for (std::size_t qi = 0; qi < nq; ++qi) {
417 for (std::size_t c = 0; c < C; ++c) Dq(qi, c) = d.Lchain(queueStations[qi], c);
418 for (std::size_t n = 0; n < ncol; ++n)
419 muq(qi, n) = lldscaling(queueStations[qi], n < Wq ? n : Wq - 1);
420 }
421 std::vector<int> Nmx(C, 0);
422 for (std::size_t c = 0; c < C; ++c)
423 Nmx[c] = std::isinf(Nchain[c]) ? pfqn::OPEN_CLASS : Nnc[c];
424 const pfqn::NcldmxResult<T> mx =
425 pfqn::pfqn_ncldmx(lambda, Dq, Nmx, Zvec, muq, pmethod, atol, nopt);
426 lG = mx.lG;
427 Xchain = mx.XN;
428 for (std::size_t qi = 0; qi < nq; ++qi)
429 for (std::size_t c = 0; c < C; ++c) Qchain(queueStations[qi], c) = mx.QN(qi, c);
430 for (std::size_t i = 0; i < M; ++i)
431 if (isDelay[i])
432 for (std::size_t c = 0; c < C; ++c)
433 Qchain(i, c) = T(d.Lchain(i, c) * Xchain[c]);
434 actualmethod = "ncldmx";
435 } else {
436 Matrix<T> Zzero(1, C, zero);
437 const pfqn::NcldResult<T> base =
438 pfqn::pfqn_ncld(L, Nnc, Zzero, mu, pmethod, atol, nopt);
439 lG = base.lG;
440 actualmethod = base.method;
441
442 const bool repairman = (M == 2 && !infServers.empty());
443 std::size_t firstDelay = infServers.empty() ? 0 : infServers[0];
444 for (std::size_t r = 0; r < C; ++r) {
445 const std::vector<int> Nr = detail::oner(Nnc, r);
446 const double lGr = pfqn::pfqn_ncld(L, Nr, Zzero, mu, pmethod, atol, nopt).lG;
447 Xchain[r] = num_traits<T>::from_double(std::exp(lGr - lG));
448 if (repairman) {
449 const T qd = T(d.Lchain(firstDelay, r) * Xchain[r]);
450 Qchain(firstDelay, r) = qd;
451 for (std::size_t i = 0; i < M; ++i)
452 if (i != firstDelay)
453 Qchain(i, r) = T(num_traits<T>::from_double(Nchain[r]) - qd);
454 continue;
455 }
456 std::size_t nfinite = 0;
457 for (std::size_t i = 0; i < M; ++i)
458 if (std::isfinite(nservers[i])) ++nfinite;
459 for (std::size_t i = 0; i < M; ++i) {
460 if (!(d.Lchain(i, r) > zero)) continue;
461 if (std::isinf(nservers[i])) {
462 Qchain(i, r) = T(d.Lchain(i, r) * Xchain[r]);
463 continue;
464 }
465 if (i + 1 == M && nfinite == 1) {
466 // The only queueing station: give it the balance of
467 // the population rather than a fourth constant.
468 T acc = zero;
469 for (std::size_t i2 : infServers) acc += d.Lchain(i2, r);
470 T q = T(num_traits<T>::from_double(Nchain[r]) - acc * Xchain[r]);
471 for (std::size_t i2 = 0; i2 + 1 < M; ++i2)
472 if (std::isfinite(nservers[i2])) q -= Qchain(i2, r);
473 Qchain(i, r) = q < zero ? zero : q;
474 continue;
475 }
476 // Conditional MVA: the shifted lattice at station i, its
477 // flow-equivalent complement, and the model with station
478 // i removed.
479 const Matrix<T> muhati = pfqn::pfqn_mushift(mu, i);
480 Matrix<T> muhati_row(1, muhati.cols(), zero);
481 for (std::size_t n = 0; n < muhati.cols(); ++n) muhati_row(0, n) = muhati(i, n);
482 const pfqn::FncResult<T> fnc = pfqn::pfqn_fnc(muhati_row);
483 Matrix<T> Lhat(M + 1, C, zero), muhat(M + 1, muhati.cols(), zero);
484 for (std::size_t i2 = 0; i2 < M; ++i2) {
485 for (std::size_t c = 0; c < C; ++c) Lhat(i2, c) = L(i2, c);
486 for (std::size_t n = 0; n < muhati.cols(); ++n)
487 muhat(i2, n) = muhati(i2, n);
488 }
489 for (std::size_t c = 0; c < C; ++c) Lhat(M, c) = L(i, c);
490 for (std::size_t n = 0; n < fnc.mu.cols(); ++n) muhat(M, n) = fnc.mu(0, n);
491 Matrix<T> Lms_i(M - 1, C, zero), mu_i(M - 1, mu.cols(), zero);
492 std::size_t row = 0;
493 for (std::size_t i2 = 0; i2 < M; ++i2) {
494 if (i2 == i) continue;
495 for (std::size_t c = 0; c < C; ++c) Lms_i(row, c) = L(i2, c);
496 for (std::size_t n = 0; n < mu.cols(); ++n) mu_i(row, n) = mu(i2, n);
497 ++row;
498 }
499 const double lGhat_fnci =
500 pfqn::pfqn_ncld(Lhat, Nr, Zzero, muhat, pmethod, atol, nopt).lG;
501 const double lGhatir =
502 pfqn::pfqn_ncld(L, Nr, Zzero, muhati, pmethod, atol, nopt).lG;
503 const double lGr_i =
504 pfqn::pfqn_ncld(Lms_i, Nr, Zzero, mu_i, pmethod, atol, nopt).lG;
505 const double dlGa = lGhat_fnci - lGhatir;
506 const double dlG_i = lGr_i - lGhatir;
507 const T CQ = T(num_traits<T>::from_double(std::exp(dlGa) - 1.0) +
508 fnc.c[0] * num_traits<T>::from_double(std::exp(dlG_i) - 1.0));
509 const double ldDemand = std::log(num_traits<T>::to_double(L(i, r))) +
510 lGhatir -
511 std::log(num_traits<T>::to_double(mu(i, 0))) - lGr;
512 Qchain(i, r) = T(num_traits<T>::from_double(std::exp(ldDemand)) *
513 Xchain[r] * (one + CQ));
514 }
515 }
516 }
517
518 Matrix<T> Rchain(M, C, zero), Tchain(M, C, zero);
519 for (std::size_t i = 0; i < M; ++i)
520 for (std::size_t c = 0; c < C; ++c) {
521 if (Xchain[c] != zero && d.Vchain(i, c) != zero)
522 Rchain(i, c) = T(Qchain(i, c) / Xchain[c] / d.Vchain(i, c));
523 Tchain(i, c) = T(Xchain[c] * d.Vchain(i, c));
524 }
525 for (std::size_t i : infServers)
526 for (std::size_t c = 0; c < C; ++c)
527 Rchain(i, c) =
528 d.Vchain(i, c) == zero ? zero : T(d.Lchain(i, c) / d.Vchain(i, c));
529 for (std::size_t c = 0; c < C; ++c) {
530 if (Nnc[c] != 0) continue;
531 Xchain[c] = zero;
532 for (std::size_t i = 0; i < M; ++i) {
533 Qchain(i, c) = zero;
534 Rchain(i, c) = zero;
535 Tchain(i, c) = zero;
536 }
537 }
538 for (std::size_t i = 0; i < M; ++i)
539 for (std::size_t c = 0; c < C; ++c) {
540 if (!std::isfinite(num_traits<T>::to_double(Qchain(i, c)))) Qchain(i, c) = zero;
541 if (!std::isfinite(num_traits<T>::to_double(Rchain(i, c)))) Rchain(i, c) = zero;
542 }
543
544 d.ST = ST;
546 Tchain, Xchain);
547 out.STeff = ST;
548
549 // The FCFS correction reads sum(U) as a utilization in [0,1]. At a
550 // limited-load-dependent station the deaggregated U is T*ST at scaling 1,
551 // which reaches max(lldscaling) at saturation: fed in raw it gave rho > 1 and
552 // service times that diverged (Tput 0, RespT ~1e14 on every multi-class
553 // load-dependent FCFS model). Present the station as a server of capacity
554 // c = max(lldscaling): U/c as utilization, c as server count (solver_ncld.m).
555 Matrix<T> Uapx = cls.U;
556 auto capapx = nserv_t;
557 for (std::size_t i = 0; i < M; ++i) {
558 if (!(std::isfinite(nservers[i]) && nservers[i] <= 1.0)) continue;
559 double pk = 0.0;
560 for (std::size_t n = 0; n < lldscaling.cols(); ++n)
561 pk = std::max(pk, num_traits<T>::to_double(lldscaling(i, n)));
562 if (!(std::isfinite(pk) && pk > 0.0)) continue;
563 const T pkT = num_traits<T>::from_double(pk);
564 for (std::size_t k = 0; k < Uapx.cols(); ++k) Uapx(i, k) = T(Uapx(i, k) / pkT);
565 capapx[i] = pkT;
566 }
568 opt.highvar, isFCFS, sn.rates, ST0, V, SCVnan, cls.Tp, Uapx, gamma, capapx);
569 ST = na.ST;
570 gamma = na.gamma;
571 eta = na.eta;
572 }
573
574 Matrix<T> Q = cls.Q, U = cls.U, R = cls.R, Tp = cls.Tp;
575 std::vector<T> X = cls.X;
576 auto absify = [&](Matrix<T>& A) {
577 for (std::size_t i = 0; i < A.rows(); ++i)
578 for (std::size_t j = 0; j < A.cols(); ++j)
579 if (A(i, j) < zero) A(i, j) = T(-A(i, j));
580 };
581 absify(Q);
582 absify(R);
583 absify(U);
584 for (T& x : X)
585 if (x < zero) x = T(-x);
586
587 // Utilization is re-derived rather than taken from the deaggregation:
588 // at a load-dependent station the chain-level U carries the lattice, and
589 // what the reference reports is the fraction of the SERVERS busy.
590 for (std::size_t i = 0; i < M; ++i) {
591 const bool multi = std::isfinite(nservers[i]) && nservers[i] > 1.0;
592 if (multi || std::isinf(nservers[i])) {
593 const double div = multi ? nservers[i] : 1.0;
594 for (std::size_t k = 0; k < K; ++k) {
595 std::size_t c = C;
596 for (std::size_t cc = 0; cc < C; ++cc)
597 if (sn.chains[cc][k]) c = cc;
598 if (c == C) continue;
599 const std::size_t rs = sn.classes[k].refstat;
600 const T vref = sn.visits[c](sn.stateful_of_station(rs) - 1, k);
601 if (vref == zero) continue;
602 const T vi = sn.visits[c](sn.stateful_of_station(i + 1) - 1, k);
603 const bool open = std::isinf(sn.classes[k].population);
604 T rate = zero;
605 if (open) {
606 // the open class's own arrival rate, sn's source rate
607 const std::size_t src = sn.classes[k].refstat;
608 if (!sn.disabled[src - 1][k]) rate = sn.rates(src - 1, k);
609 } else {
610 rate = X[k];
611 }
612 if (!(rate > zero)) continue;
613 U(i, k) = T(rate * vi / vref * ST(i, k) / num_traits<T>::from_double(div));
614 }
615 } else {
616 // `solver_ncld.m:300` divides by `max(lldscaling(ist,:))` over the
617 // STATION'S OWN ROW, whose width is whatever the caller gave
618 // setLoadDependence -- not over the Nt-column lattice this file
619 // builds. Taking the max over the lattice dropped every entry past
620 // the population: on a 4-job model with scaling [1 1.6 2 2.2 2.3]
621 // the divisor came out 2.2 instead of 2.3 and Util read 0.419042
622 // against the reference's 0.400823, with QLen, RespT and Tput all
623 // exact. A station with no row of its own keeps the lattice's
624 // constant one, which is what the row would hold anyway.
625 T mx = zero;
626 const std::vector<T>& lldrow = sn.stations[i].lldscaling;
627 if (lldrow.empty()) {
628 for (std::size_t n = 0; n < Nt; ++n)
629 if (lldscaling(i, n) > mx) mx = lldscaling(i, n);
630 } else {
631 for (std::size_t n = 0; n < lldrow.size(); ++n)
632 if (lldrow[n] > mx) mx = lldrow[n];
633 }
634 if (mx > zero)
635 for (std::size_t k = 0; k < K; ++k) U(i, k) = T(U(i, k) / mx);
636 T s = zero;
637 for (std::size_t k = 0; k < K; ++k) s += U(i, k);
638 if (num_traits<T>::to_double(s) > 1.0)
639 for (std::size_t k = 0; k < K; ++k) U(i, k) = T(U(i, k) / s);
640 }
641 }
642
643 auto clear_nonfinite = [&](Matrix<T>& A) {
644 for (std::size_t i = 0; i < A.rows(); ++i)
645 for (std::size_t j = 0; j < A.cols(); ++j)
646 if (!std::isfinite(num_traits<T>::to_double(A(i, j)))) A(i, j) = zero;
647 };
648 clear_nonfinite(Q);
649 clear_nonfinite(U);
650 clear_nonfinite(R);
651 for (T& x : X)
652 if (!std::isfinite(num_traits<T>::to_double(x))) x = zero;
653
654 for (std::size_t c = 0; c < C; ++c) {
655 if (std::isinf(Nchain[c])) continue;
656 T qden = zero;
657 for (std::size_t k : sn.inchain[c])
658 for (std::size_t i = 0; i < M; ++i) qden += Q(i, k - 1);
659 const T ratio =
660 qden > zero ? T(num_traits<T>::from_double(Nchain[c]) / qden) : zero;
661 for (std::size_t k : sn.inchain[c]) {
662 X[k - 1] = T(ratio * X[k - 1]);
663 for (std::size_t i = 0; i < M; ++i) {
664 Q(i, k - 1) = T(ratio * Q(i, k - 1));
665 Tp(i, k - 1) = T(ratio * Tp(i, k - 1));
666 U(i, k - 1) = T(ratio * U(i, k - 1));
667 R(i, k - 1) = Tp(i, k - 1) == zero ? zero : T(Q(i, k - 1) / Tp(i, k - 1));
668 }
669 }
670 }
671
672 out.sol.Q = Q;
673 out.sol.U = U;
674 out.sol.R = R;
675 out.sol.Tp = Tp;
676 out.sol.X = X;
677 out.sol.C.assign(K, zero);
678 for (std::size_t k = 0; k < K; ++k) {
679 const double njobs = sn.classes[k].population;
680 out.sol.C[k] = (std::isfinite(njobs) && X[k] != zero)
681 ? T(num_traits<T>::from_double(njobs) / X[k])
682 : cls.C[k];
683 }
684 out.sol.lG = lG;
685 out.sol.iter = it;
686 out.sol.method = actualmethod;
687 out.actualmethod = actualmethod;
688 return out;
689 }
690}
691
692} // namespace nc
693} // namespace line
694
695#endif // LINE_SOLVERS_NC_SOLVER_NCLD_H
std::size_t cols() const
Definition matrix.h:90
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
The exception types the port throws.
options.timeout: one wall-clock budget per top-level solve, on a per-THREAD deadline stack.
Dense matrix and non-owning view.
The option and result types every MVA analyzer shares.
ClassResults< T > sn_deaggregate_chain_results(const qn::NetworkStruct< T > &L, const ChainDemands< T > &d, const Matrix< T > &Qchain, const Matrix< T > &Uchain, const Matrix< T > &Rchain, const Matrix< T > &Tchain, const std::vector< T > &Xchain)
Port of sn_deaggregate_chain_results.
Definition sn_chain.h:227
ChainDemands< T > sn_get_demands_chain(const qn::NetworkStruct< T > &L)
Port of sn_get_demands_chain.
Definition sn_chain.h:63
NcSolution< T > solver_nc_conv(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Port of solver_nc_conv.m.
NcSolution< T > solver_ncld(const qn::NetworkStruct< T > &sn_in, const NcSolverOptions &opt)
Port of solver_ncld.m.
NonexpApproxResult< T > npfqn_nonexp_approx(const std::string &method, const std::vector< bool > &isFCFS, const Matrix< T > &rates, const Matrix< T > &ST, const Matrix< T > &V, const Matrix< T > &SCV, const Matrix< T > &Tput, const Matrix< T > &U, const std::vector< T > &gamma, const std::vector< T > &nservers)
Handler for non-exponential service and arrival processes in AMVA and NC.
NcldmxResult< T > pfqn_ncldmx(const std::vector< T > &lambda, const Matrix< T > &D, const std::vector< int > &N, const Matrix< T > &Z, const Matrix< T > &mu, const std::string &method, const T &atol, const NcOptions &nopt)
Normalizing constant of a MIXED open/closed network with limited load dependence.
constexpr int OPEN_CLASS
Marks an open (infinite-population) class in a population vector.
Definition pfqn_mvams.h:74
NcldResult< T > pfqn_ncld(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const Matrix< T > &mu, const std::string &method_name, const T &atol, const NcOptions &nopt)
Normalizing constant of a LOAD-DEPENDENT closed network: the dispatcher.
Definition pfqn_ncld.h:167
Matrix< T > pfqn_mushift(const Matrix< T > &mu, const std::vector< std::size_t > &iset)
Shift the load-dependent service-rate lattice of selected stations.
FncResult< T > pfqn_fnc(const Matrix< T > &alpha)
Automatic offset search (the one-argument MATLAB branch).
Definition pfqn_fnc.h:151
bool timeout_exceeded()
The checkpoint: true when the innermost budget has run out, marking it tripped.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Controls and result shape shared by the normalizing-constant analyzers.
A queueing network and its refreshed NetworkStruct.
Handler for non-exponential service and arrival processes in AMVA and NC.
Load-dependent rates of the functional server f(n) = n + c.
Shift the load-dependent service-rate lattice of selected stations.
Exact Mean Value Analysis for mixed open/closed networks with multiserver stations.
Normalizing constant of a LOAD-DEPENDENT closed network: the dispatcher.
Normalizing constant of a MIXED open/closed network with limited load dependence.
Chain aggregation and de-aggregation.
Port of solver_nc.m: the load-INDEPENDENT normalizing-constant analyzer.
Exact convolution analysis of a closed network with class-dependent service rates.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
The chain-level view of a layer, as sn_get_demands_chain returns it.
Definition sn_chain.h:46
Matrix< T > ST
(M x K) class-level mean service time, 0 where disabled
Definition sn_chain.h:54
Matrix< T > alpha
(M x K) class share of its chain's visits at a station
Definition sn_chain.h:50
Matrix< T > STchain
(M x C) mean service time
Definition sn_chain.h:48
Matrix< T > Lchain
(M x C) demand
Definition sn_chain.h:47
Matrix< T > Vchain
(M x C) visits
Definition sn_chain.h:49
Class-level results, as sn_deaggregate_chain_results returns them.
Definition sn_chain.h:204
std::vector< T > C
Definition sn_chain.h:206
std::vector< T > X
Definition sn_chain.h:206
The [Q,U,R,T,C,X,lG] of the reference, plus the algorithm that ran.
Definition nc_types.h:134
mva::MvaSolution< T > sol
Definition nc_types.h:135
Matrix< T > STeff
the service times of the last pass, MATLAB's STeff
Definition nc_types.h:137
std::string actualmethod
Definition nc_types.h:136
Controls, defaulting to SolverOptions('NC') in the reference.
Definition nc_types.h:35
Return value, mirroring MATLAB's [ST,gamma,nservers,rho,scva,scvs,eta].
Matrix< T > ST
(M x R) scaled service times
std::vector< T > gamma
(M) multiserver asymptotic decay rate
std::vector< T > eta
(M) diffusion decay rate
Return value of pfqn_fnc, mirroring [mu, c].
Definition pfqn_fnc.h:61
std::vector< T > c
Definition pfqn_fnc.h:63
The options fields compute_norm_const reads beyond the method itself.
Definition pfqn_nc.h:215
unsigned long seed
SolverOptions('NC').seed.
Definition pfqn_nc.h:217
std::size_t samples
SolverOptions('NC').samples.
Definition pfqn_nc.h:216
double tol
handed to pfqn_comomrm
Definition pfqn_nc.h:218
std::string method
the algorithm actually used
Definition pfqn_ncld.h:144
double lG
its logarithm
Definition pfqn_ncld.h:143
std::vector< T > XN
(R) throughputs: G(N-e_r)/G(N) closed, lambda_r open
Definition pfqn_ncldmx.h:91
Matrix< T > QN
(M x R) mean queue lengths
Definition pfqn_ncldmx.h:92
double lG
its logarithm
Definition pfqn_ncldmx.h:87
One job class of the network.
double population
infinite for an open class