LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
nc_dispatch.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_NC_DISPATCH_H
6#define LINE_SOLVERS_NC_NC_DISPATCH_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * Port of `solver_nc_analyzer.m`, `solver_ncld_analyzer.m` and
12 * `@@SolverNC/ncDispatch.m`: one inner solve, choosing the analyzer that fits.
13 *
14 * THE ORDER IS THE CONTRACT. As in `mva_dispatch.h`, the branches are NOT
15 * disjoint -- an order-independent station is also a multiserver, a
16 * pass-and-swap tandem is also a closed network -- so the first match wins and
17 * reordering silently changes which algorithm a model gets. The sequence is
18 * the reference's, top to bottom:
19 *
20 * 0 discrete-time (slotted) model, on an explicit options.slotted or
21 * timescale 'discrete' (nc_is_slotted)
22 * 1 order-independent closed network, on 'default' or 'exact'
23 * 2 pass-and-swap importance sampling, on 'default', 'is' or 'sampling'
24 * 3 'is' on an open or mixed model -> rejected by name
25 * 4 Maximum Entropy Method, on an explicit 'mem'
26 * 5 'exact' on an open or mixed multiserver model -> rejected by name
27 * 6 fractional closed populations, by interpolation
28 * 7 everything else -> solver_nc / solver_ncld
29 *
30 * Branch 1 is the EXACT balanced-fairness analyzer and branch 2 the sampler, so
31 * a pure-OI tandem on 'default' never reaches branch 2; only a genuine swap
32 * graph, or an explicit 'is' / 'sampling', does. A station carrying a service
33 * rate function that satisfies NEITHER predicate is refused by name rather than
34 * sent down the ordinary normalizing-constant path, where its rate function
35 * would simply be ignored.
36 *
37 * `ncDispatch` (the fork-join inner solve) is the load-dependence test alone:
38 * the MMT transformation returns a plain mixed queueing network, so none of the
39 * specialised routes can apply to it.
40 */
41
43#include <cmath>
44#include <string>
45#include <vector>
46
55#include "line/util/error.h"
56
57namespace line {
58namespace nc {
59
60namespace detail {
61
62/**
63 * Does any station carry a service rate function, and does any of them have a
64 * non-zero swap graph (which makes it a genuine pass-and-swap station)?
65 *
66 * Kept as one pass because both callers need both answers, and the swap graph is
67 * read through `station_swap_graph` so the `refreshLocalVars` defaults apply.
68 */
69template <class T>
70bool has_svc_rate_fun(const qn::NetworkStruct<T>& sn, bool& pas) {
71 pas = false;
72 bool any = false;
73 for (std::size_t i = 0; i < sn.nstations; ++i) {
74 if (!sn.stations[i].svc_rate_fun) continue;
75 any = true;
76 if (!qn::station_swap_graph_is_zero(sn, i + 1)) pas = true;
77 }
78 return any;
79}
80
81/**
82 * True when any station carries a load-, class- or joint-dependent rate.
83 *
84 * THE TEST MUST NAME ALL THREE (`ncDispatch.m:22`). It read `lldscaling` and
85 * `cdscaling` only until 2026-09-13, so a CLOSED model whose only declared
86 * dependence was `jdscaling` never reached `solver_ncld_analyzer`: it went to
87 * the load-INDEPENDENT analyzer and was answered by CoMoM at its unscaled rate
88 * (QLen 3.015385 where the eta = 0.8 model's exact answer is 3.206144).
89 */
90template <class T>
91bool has_scaling(const qn::NetworkStruct<T>& sn) {
92 for (const qn::Station<T>& st : sn.stations)
93 if (!st.lldscaling.empty() || static_cast<bool>(st.cdscaling) ||
94 static_cast<bool>(st.jdscaling))
95 return true;
96 return false;
97}
98
99} // namespace detail
100
101/**
102 * Port of `solver_ncld_analyzer.m`: the load-dependent analyzer, which is
103 * `solver_ncld` plus the same fractional-population interpolation the
104 * load-independent one applies.
105 *
106 * No open-multiserver refusal here (solver_ncld_analyzer.m): a model that
107 * reaches this analyzer carries its server counts in lldscaling, either
108 * converted by the runner's 'exact' arm or folded by `solver_ncld`
109 * (ncld_fold_servers), and the mixed route (`pfqn_ncldmx`) is exact on both.
110 * `solver_nc_analyzer` keeps the refusal (branch 5), since the Seidmann
111 * surrogate it would use is not.
112 */
113template <class T>
117
118/**
119 * Port of `solver_nc_analyzer.m`.
120 *
121 * @param sn the refreshed struct
122 * @param opt solver controls
123 */
124template <class T>
126 // Discrete time first: the slot lattice is a property of the MODEL, not of
127 // a method, so it precedes every method-keyed branch below and refuses a
128 // model outside the discrete-time product form instead of falling through.
129 if (nc_is_slotted(opt)) return solver_nc_dt(sn, opt);
130
131 // Closed think+DPS network: Morrison's heavy-usage expansion of the generating
132 // function (npfqn_dps_morrison), the default for that shape. Not a product-form
133 // route: lG is NaN. The runner intercepts this shape before reaching here; the
134 // arm is repeated for the inner entry points (fork-join, ncld) that call the
135 // analyzer directly.
136 if (nc_is_dps_model(sn) && (opt.method == "default" || opt.method == "morrison"))
138 // Named on a model that is not the shape. The runner refuses this before reaching
139 // here; the arm is repeated because the inner entry points (fork-join, ncld) call
140 // this analyzer directly, and without it the method falls through to the ordinary
141 // normalizing-constant path and answers under the caller's label.
142 if (opt.method == "morrison")
143 throw UnsupportedError(ErrorKind{"Topology"},
144 "solver_nc_analyzer: method 'morrison' requires a CLOSED network of exactly two "
145 "stations, one infinite-server (think) station and one single-server DPS station with "
146 "exponential service, which this model is not.");
147
148 // Exact balanced fairness first: a pure-OI network on 'default' or 'exact'
149 // never reaches the sampler below.
150 if (nc_is_oi_model(sn) && (opt.method == "default" || opt.method == "exact"))
152
153 // The importance sampler, on an explicit request and on 'default' for a P&S
154 // tandem with a genuine swap graph, which is reducible and has no exact path.
155 if (nc_is_pas_model(sn) &&
156 (opt.method == "default" || opt.method == "is" || opt.method == "sampling"))
158
159 bool pas = false;
160 if (detail::has_svc_rate_fun(sn, pas))
161 throw UnsupportedError(ErrorKind{"SchedStrategy_OI"},
162 "solver_nc_analyzer: a station with a service rate function (OI / PAS) has no "
163 "product-form demand and cannot be solved by the ordinary normalizing-constant path");
164
165 bool anyOpen = false;
166 for (const qn::JobClass& c : sn.classes)
167 if (std::isinf(c.population)) anyOpen = true;
168
169 if (opt.method == "is" && anyOpen)
170 throw UnsupportedError(ErrorKind{"OpenClass"},
171 "solver_nc_analyzer: the 'is' importance-sampling method requires a closed queueing "
172 "network. Use 'sampling' for an open or mixed model");
173
174 // The Maximum Entropy Method, on an explicit request only: 'default' keeps
175 // the normalizing-constant path it had before MEM existed.
176 if (opt.method == "mem") return solver_nc_mem(sn, opt);
177
178 bool multiserver = false;
179 for (const qn::Station<T>& st : sn.stations)
180 if (std::isfinite(st.nservers) && st.nservers > 1.0) multiserver = true;
181 if (multiserver && anyOpen && opt.method == "exact")
182 throw UnsupportedError(ErrorKind{"MultiServer"},
183 "solver_nc_analyzer: the NC solver cannot provide exact solutions for open or mixed "
184 "multiserver queueing networks. Remove the 'exact' option");
185
186 // Fractional closed populations: solve at floor and at ceiling and
187 // interpolate, which is the only meaning a non-integral population has for
188 // a constant defined on the population lattice.
189 double eta_max = 0.0;
190 for (const qn::JobClass& c : sn.classes)
191 if (std::isfinite(c.population)) {
192 const double e = std::fabs(c.population - std::floor(c.population));
193 if (e > eta_max) eta_max = e;
194 }
195 if (eta_max > GlobalConstants::FineTol) {
196 // solver_ncld_analyzer.m: the load-dependent analyzer refuses 'exact' on
197 // a fractional population, since the interpolation below is approximate.
198 // nc_method_refusal states the same rule before the multiserver rewrite;
199 // this is the analyzer's own copy, reached by an inner fork-join solve.
200 if (opt.method == "exact")
201 for (const qn::Station<T>& st : sn.stations)
202 if (!st.lldscaling.empty() || static_cast<bool>(st.cdscaling) ||
203 static_cast<bool>(st.jdscaling) ||
204 (std::isfinite(st.nservers) && st.nservers > 1.0))
205 throw UnsupportedError(ErrorKind{"Structure"},
206 "SolverNC: NC load-dependent solver cannot provide exact solutions for "
207 "fractional populations.");
208 qn::NetworkStruct<T> lo = sn, hi = sn;
209 for (std::size_t k = 0; k < sn.nclasses; ++k) {
210 const double p = sn.classes[k].population;
211 if (!std::isfinite(p)) continue;
212 lo.classes[k].population = std::floor(p);
213 hi.classes[k].population = std::ceil(p);
214 }
215 const NcSolution<T> f = solver_nc(lo, opt);
216 const NcSolution<T> c = solver_nc(hi, opt);
217 NcSolution<T> out = c;
218 const T e = num_traits<T>::from_double(eta_max);
219 auto blend = [&](Matrix<T>& A, const Matrix<T>& Af, const Matrix<T>& Ac) {
220 A = Af;
221 for (std::size_t i = 0; i < A.rows(); ++i)
222 for (std::size_t j = 0; j < A.cols(); ++j)
223 A(i, j) = T(Af(i, j) + e * (Ac(i, j) - Af(i, j)));
224 };
225 blend(out.sol.Q, f.sol.Q, c.sol.Q);
226 blend(out.sol.U, f.sol.U, c.sol.U);
227 blend(out.sol.R, f.sol.R, c.sol.R);
228 blend(out.sol.Tp, f.sol.Tp, c.sol.Tp);
229 for (std::size_t k = 0; k < out.sol.X.size(); ++k) {
230 out.sol.X[k] = T(f.sol.X[k] + e * (c.sol.X[k] - f.sol.X[k]));
231 out.sol.C[k] = T(f.sol.C[k] + e * (c.sol.C[k] - f.sol.C[k]));
232 }
233 // REFERENCE DEFECT, reproduced: the interpolation of lG reads
234 // lGf + eta*(lGf - lGc), which extrapolates AWAY from the ceiling
235 // solve instead of towards it. It is kept because lG is a reported
236 // quantity and silently changing it would move every caller's
237 // getProbNormConstAggr on fractional populations.
238 out.sol.lG = f.sol.lG + eta_max * (f.sol.lG - c.sol.lG);
239 out.sol.iter = f.sol.iter + c.sol.iter;
240 return out;
241 }
242 return solver_nc(sn, opt);
243}
244
245/**
246 * Port of `@@SolverNC/ncDispatch.m`: the inner solve of the fork-join fixed
247 * point, which is the load-dependence test and nothing else.
248 */
249template <class T>
252 : detail::has_scaling(sn) ? solver_ncld_analyzer(sn, opt)
254 const double lg = num_traits<T>::to_double(out.sol.lG);
255 if (std::isfinite(lg))
256 line::util::LineConsole::step("normalizing constant obtained: log G = %.6g", lg);
257 return out;
258}
259
260} // namespace nc
261} // namespace line
262
263#endif // LINE_SOLVERS_NC_NC_DISPATCH_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.
static void step(const char *fmt,...)
Write one progress line.
The exception types the port throws.
Running progress log of a LINE solver run (the "solver console").
bool nc_is_oi_model(const qn::NetworkStruct< T > &sn)
Port of nc_is_oi_model.m: a closed, open or mixed network with at least one OI station and nothing bu...
NcSolution< T > solver_nc_pas_is_analyzer(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Port of solver_nc_pas_is_analyzer.m.
NcSolution< T > solver_nc_dt(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Exact discrete-time analysis of sn.
NcSolution< T > solver_nc_dps_analyzer(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Analyzes the closed think+DPS network.
bool nc_is_slotted(const NcSolverOptions &opt)
True when the caller asked for the discrete-time route (slotted or timescale "discrete").
Definition nc_types.h:128
NcSolution< T > solver_ncld(const qn::NetworkStruct< T > &sn_in, const NcSolverOptions &opt)
Port of solver_ncld.m.
NcSolution< T > solver_nc_mem(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Port of solver_nc_mem.m.
NcSolution< T > solver_nc(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Port of solver_nc.m.
Definition solver_nc.h:144
bool nc_is_pas_model(const qn::NetworkStruct< T > &sn)
Port of nc_is_pas_model.m: a closed two-station OI / P&S tandem.
NcSolution< T > solver_nc_oi_analyzer(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Port of solver_nc_oi_analyzer.m.
NcSolution< T > solver_nc_analyzer(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Port of solver_nc_analyzer.m.
NcSolution< T > solver_ncld_analyzer(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Port of solver_ncld_analyzer.m: the load-dependent analyzer, which is solver_ncld plus the same fract...
bool nc_is_dps_model(const qn::NetworkStruct< T > &sn)
True when the model is the closed two-station network Morrison's expansion is derived for: one infini...
NcSolution< T > nc_dispatch(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Port of @@SolverNC/ncDispatch.m: the inner solve of the fork-join fixed point, which is the load-depe...
bool station_swap_graph_is_zero(const NetworkStruct< T > &sn, std::size_t ist)
True when the station's materialized swap graph is entirely zero.
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.
Port of solver_nc.m: the load-INDEPENDENT normalizing-constant analyzer.
Heavy-usage asymptotic analysis of the closed two-station network with one think (infinite-server) st...
Exact normalizing-constant analysis of a discrete-time (slotted) model.
Port of solver_nc_mem.m and solver_nc_mem_supports.m: the Maximum Entropy Method of Kouvatsos (1994).
Order-independent (OI) and pass-and-swap (P&S) normalizing-constant analysis.
Port of solver_ncld.m: the LOAD-DEPENDENT normalizing-constant analyzer.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
static constexpr double FineTol
Definition lang_types.h:823
The [Q,U,R,T,C,X,lG] of the reference, plus the algorithm that ran.
Definition nc_types.h:134
mva::MvaSolution< T > sol
Definition nc_types.h:135
Controls, defaulting to SolverOptions('NC') in the reference.
Definition nc_types.h:35
One job class of the network.
double population
infinite for an open class
One station of the network.
double nservers
may be infinite (a Delay, or an inf-scheduled task)
CdScaling< T > jdscaling
sn.jdscaling for this station: MATLAB's Station.ljdScaling, the JOINT dependence map eta_i(n),...
std::vector< T > lldscaling
sn.lldscaling for this station: the multiplier at population 1, 2, ... Empty when the station is not ...
CdScaling< T > cdscaling
sn.cdscaling for this station: the class-dependence map, empty when unset.