LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
solver_mva_polling.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_MVA_SOLVER_MVA_POLLING_H
6#define LINE_SOLVERS_MVA_SOLVER_MVA_POLLING_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * The multiclass open polling analyzer (ladder branch 5).
12 *
13 * Templated port of `matlab/src/solvers/MVA/solver_mva_polling_analyzer.m`.
14 *
15 * A polling system is a single server that visits each class's buffer in turn,
16 * switching over between them. Each CLASS is a queue of the polling model, and
17 * the mean waiting time of every class follows from the pseudo-conservation
18 * laws of its discipline: EXHAUSTIVE (serve until the buffer empties), GATED
19 * (serve exactly what was present at the visit), 1-limited (one job per visit)
20 * and DECREMENTING. The `polling_qsys_*` routines carry those laws; this
21 * analyzer builds their two-moment inputs from the arrival, service and
22 * switchover distributions and maps the resulting waiting times back onto the
23 * per-class [Q,U,R,T] block.
24 *
25 * SUPPORTED. One server and one polling type: EXHAUSTIVE, GATED,
26 * K-limited (the `polling_qsys_1limited` law at K = 1, Fuhrmann's relation
27 * `polling_qsys_klimited` for K > 1, default method only) and DECREMENTING
28 * (symmetric systems exactly, asymmetric ones by the pseudo-conservation-law
29 * approximation), and G-limited with K = 1 (which is 1-limited). A polling table is answered exactly by
30 * `polling_qsys_table` for EXHAUSTIVE / GATED with Poisson arrivals; a
31 * load-dependent order or a visit timer is refused. `exact` is available only for EXHAUSTIVE / GATED with
32 * Poisson arrivals on the polled classes. `polling_reason` is the one gate,
33 * read by solver_mva_run_analyzer ahead of the dispatch and again here.
34 *
35 * Arithmetic: TRANSCENDENTAL. The pseudo-conservation laws are field
36 * arithmetic, but the input SCVs come from distribution moments that are only
37 * defined at a transcendental T.
38 */
39
40#include <cmath>
41#include <string>
42#include <vector>
43
51
52namespace line {
53namespace mva {
54
55namespace detail {
56
57/**
58 * True where class r has an enabled service at queue station `q` (1-based):
59 * the buffers the server visits, in cyclic order. A disabled class (rate NaN
60 * or zero) is dropped from the cycle together with its switchover leg, as
61 * State.pollingInfo and the JAR/Python analyzers do.
62 */
63template <class T>
64std::vector<std::size_t> polling_polled_classes(const qn::NetworkStruct<T>& L, std::size_t q) {
65 std::vector<std::size_t> polled;
66 for (std::size_t r = 0; r < L.nclasses; ++r) {
67 const double mu = num_traits<T>::to_double(L.rates(q - 1, r));
68 const bool disabled = r < L.service[q - 1].size() && L.service[q - 1][r].disabled;
69 if (!disabled && !std::isnan(mu) && mu > 0.0) polled.push_back(r);
70 }
71 return polled;
72}
73
74} // namespace detail
75
76/**
77 * Why SolverMVA cannot answer a model holding a POLLING station under
78 * `method`, or "" when it can (or there is no polling station).
79 *
80 * Port of MATLAB `SolverMVA.supportsPolling` and JAR
81 * `Solver_mva_polling_analyzer.pollingReason`. A POLLING station is served by
82 * the station-time analyzer alone, for an open multiclass Source -> Queue ->
83 * Sink system; anywhere else it would reach the AMVA core, which has no polling
84 * arm and leaves its wait at zero. The analyzer answers 'default' and 'exact'
85 * (an `amva.` prefix is ignored) for one cyclic server and one polling type,
86 * K-limited only at K = 1; 'exact' further needs Poisson arrivals on the polled
87 * classes and an exhaustive or gated discipline.
88 */
89template <class T>
90std::string polling_reason(const qn::NetworkStruct<T>& L, const std::string& method) {
91 using qn::NodeType;
92 bool any = false;
93 for (const qn::Station<T>& st : L.stations)
94 if (st.sched == qn::SchedStrategy::POLLING) any = true;
95 if (!any) return "";
96 std::string base = method.empty() ? std::string("default") : method;
97 if (base.rfind("amva.", 0) == 0) base = base.substr(5);
98 int nsrc = 0, nq = 0, nsnk = 0;
99 for (const qn::NodeDef& nd : L.nodes) {
100 if (nd.nodetype == NodeType::Source) ++nsrc;
101 else if (nd.nodetype == NodeType::Queue) ++nq;
102 else if (nd.nodetype == NodeType::Sink) ++nsnk;
103 }
104 const bool shape = L.nof_nodes() == 3 && nsrc == 1 && nq == 1 && nsnk == 1 &&
105 L.nclosedjobs() == 0.0 && L.nclasses > 1;
106 if (!shape)
107 return "SolverMVA serves a POLLING station only as an open multiclass single-station "
108 "system (Source -> Queue -> Sink); use SolverLDES or SolverJMT for this model.";
109 if (base != "default" && base != "exact")
110 return "the polling analyzer answers 'default' and 'exact' only; the '" + base +
111 "' method has no polling arm";
112 std::size_t src = 0, q = 0;
113 for (std::size_t i = 0; i < L.nstations; ++i) {
114 if (L.stations[i].nodetype == NodeType::Source) src = i + 1;
115 if (L.stations[i].nodetype == NodeType::Queue) q = i + 1;
116 }
117 const qn::Station<T>& qst = L.stations[q - 1];
118 if (!(std::isfinite(qst.nservers) && std::llround(qst.nservers) == 1))
119 return "the polling analyzer models a single cyclic server; this station has more than one server.";
120 const std::vector<std::size_t> polled = detail::polling_polled_classes(L, q);
121 if (polled.empty() || qst.polling_type.empty())
122 return "the polling analyzer requires one polling type, declared for every class the station serves.";
123 const lang::PollingType pt = qst.polling_type[0];
124 for (std::size_t r : polled)
125 if (r >= qst.polling_type.size() || qst.polling_type[r] != pt)
126 return "the polling analyzer requires one polling type, declared for every class the station serves.";
128 return "the polling analyzer has no load-dependent polling order (PollingOrder); use "
129 "SolverCTMC, SolverSSA or SolverLDES.";
131 return "the polling analyzer has no time-limited visits (PollingTimer); use SolverCTMC, "
132 "SolverSSA or SolverLDES.";
133 if (!qst.polling_table.empty()) {
134 // Exact buffer-occupancy recursion over the table (Eisenberg 1972):
135 // exhaustive or gated service with Poisson arrivals.
136 bool poisson = true;
137 for (std::size_t r : polled)
138 if (std::fabs(std::sqrt(num_traits<T>::to_double(L.scv(src - 1, r))) - 1.0) > 1e-8)
139 poisson = false;
140 if (!(pt == lang::PollingType::EXHAUSTIVE || pt == lang::PollingType::GATED) || !poisson)
141 return "the polling-table analyzer requires exhaustive or gated service and Poisson arrivals.";
142 }
143 // G-limited with K=1 is 1-limited; for K>1 there is no station-time
144 // formula, and the K-limited one does not apply since arrivals during the
145 // visit are not eligible.
146 if (pt == lang::PollingType::GLIMITED && qst.polling_par != 1)
147 return "the polling analyzer has no formula for G-limited polling with K>1 (only K=1, "
148 "which is 1-limited); use SolverCTMC, SolverSSA or SolverLDES.";
149 // polling_qsys_klimited is Fuhrmann's approximation, not exact
150 if (base == "exact" && pt == lang::PollingType::KLIMITED && qst.polling_par != 1)
151 return "MVA exact method unavailable for K-limited polling with K>1 (only the "
152 "approximation in method default).";
153 if (base == "exact") {
154 bool poisson = true;
155 for (std::size_t r : polled)
156 if (num_traits<T>::to_double(L.scv(src - 1, r)) != 1.0) poisson = false;
157 if (!(poisson && (pt == lang::PollingType::EXHAUSTIVE || pt == lang::PollingType::GATED)))
158 return "MVA exact method unavailable for this model.";
159 }
160 return "";
161}
162
163/**
164 * The reference's `line_warning` for method 'default' when a polled class has a
165 * non-Poisson arrival process (SCV != 1): the station-time formulas use only
166 * the arrival rate, so the answer treats it as Poisson. Empty otherwise.
167 * Method 'exact' refuses such a model in `polling_reason` instead. This port
168 * has no warning channel, so mva_dispatch carries the text on
169 * DispatchResult::warning (see the note there).
170 */
171template <class T>
173 std::size_t src = 0, q = 0;
174 for (std::size_t i = 0; i < L.nstations; ++i) {
175 if (L.stations[i].nodetype == qn::NodeType::Source) src = i + 1;
176 if (L.stations[i].nodetype == qn::NodeType::Queue) q = i + 1;
177 }
178 if (src == 0 || q == 0) return "";
179 for (std::size_t r : detail::polling_polled_classes(L, q)) {
180 const double v = num_traits<T>::to_double(L.scv(src - 1, r));
181 if (std::isfinite(v) && std::fabs(v - 1.0) > 1e-8)
182 return "The MVA polling analyzer assumes Poisson arrivals, but some classes have a "
183 "non-exponential arrival process (SCV ~= 1). The result treats them as Poisson "
184 "with the same rate; use SolverCTMC, SolverSSA or SolverLDES for an accurate "
185 "solution.";
186 }
187 return "";
188}
189
190/**
191 * Port of `solver_mva_polling_analyzer.m`.
192 *
193 * The station-time formulas of Takagi (1988) read only the arrival rate, the
194 * first two service moments and the switchover mean and variance, so those are
195 * passed as moments. They are the moments of the LAW, not of the process
196 * representation: `convertToMAP` replaces a non-Markovian law by an Erlang fit
197 * that matches the SCV only up to its phase cap (Erlang-20 for a Det, so E[S^2]
198 * off by 5%), and a Det switchover has no MAP at all.
199 */
200template <class T>
202 using qn::NodeType;
203 if constexpr (!num_traits<T>::has_transcendental) {
204 (void)L;
205 (void)opt;
206 throw UnsupportedError(ErrorKind{"Arithmetic"},
207 "solver_mva_polling_analyzer: the two-moment polling inputs come from distribution "
208 "SCVs, which need a transcendental T");
209 } else {
210 {
211 const std::string why = polling_reason(L, opt.method);
212 if (!why.empty()) throw UnsupportedError(ErrorKind{"Structure"}, "SolverMVA: " + why);
213 }
214 const T zero = num_traits<T>::from_int(0);
215 const T one = num_traits<T>::from_int(1);
216 const std::size_t M = L.nstations, K = L.nclasses;
217
218 std::size_t src = 0, q = 0;
219 for (std::size_t i = 0; i < M; ++i) {
220 if (L.stations[i].nodetype == NodeType::Source) src = i + 1;
221 if (L.stations[i].nodetype == NodeType::Queue) q = i + 1;
222 }
223 const qn::Station<T>& qst = L.stations[q - 1];
224 const std::size_t qstateful = L.stateful_of_station(q);
225 // the queue's visit ratio per class, summed over chains: each open class
226 // sits in its own chain, so reading only chain 0 would zero every class but
227 // the first
228 std::vector<T> Vq(K, zero);
229 for (std::size_t c = 0; c < L.nchains; ++c)
230 for (std::size_t r = 0; r < K; ++r) Vq[r] += L.visits[c](qstateful - 1, r);
231
232 const std::vector<std::size_t> polled = detail::polling_polled_classes(L, q);
233 const std::size_t nP = polled.size();
235 pm.lambda.assign(nP, zero);
236 pm.b.assign(nP, zero);
237 pm.b2.assign(nP, zero);
238 pm.r.assign(nP, zero);
239 pm.delta2.assign(nP, zero);
241 for (std::size_t jj = 0; jj < nP; ++jj) {
242 const std::size_t r = polled[jj];
243 const T mu = L.rates(q - 1, r);
244 const T lr = L.rates(src - 1, r);
245 pm.lambda[jj] = std::isnan(num_traits<T>::to_double(lr)) ? zero : T(lr * Vq[r]);
246 pm.b[jj] = T(one / mu);
247 pm.b2[jj] = T(T(one + L.scv(q - 1, r)) / T(mu * mu));
248 // Takagi's r_i is the switchover OUT of buffer i; Queue refuses the
249 // pairwise (from, to) form at a polling queue, so there is one leg per
250 // buffer. An undeclared, Immediate or zero-mean leg is charged as the
251 // exponential of rate GlobalConstants.Immediate, because the
252 // station-time equations divide by the total switchover time and have
253 // no R = 0 limit in this form.
254 T so_mean = zero, so_var = zero;
255 if (r < qst.switchover.size() && !qst.switchover[r].disabled &&
256 !qst.switchover[r].is_immediate()) {
257 so_mean = qst.switchover[r].mean;
258 so_var = T(qst.switchover[r].scv * so_mean * so_mean);
259 }
260 const double m = num_traits<T>::to_double(so_mean);
261 if (!(m > 0.0) || std::isinf(m)) {
262 so_mean = imm;
263 so_var = T(imm * imm);
264 }
265 pm.r[jj] = so_mean;
266 pm.delta2[jj] = so_var;
267 }
268
269 const lang::PollingType pt = qst.polling_type[polled[0]];
270 std::vector<T> Wp;
271 if (!qst.polling_table.empty()) {
272 // Polling table (polling_reason admits it for exhaustive and gated
273 // service only): exact buffer-occupancy solution, with the legs read
274 // per table position, the table switchover of that position or else
275 // the class switchover of its entry.
276 const std::size_t Mt = qst.polling_table.size();
277 std::vector<std::size_t> tabjj(Mt);
278 std::vector<T> s_mean(Mt), s_var(Mt);
279 for (std::size_t t = 0; t < Mt; ++t) {
280 const std::size_t cls = qst.polling_table[t] - 1;
281 std::size_t jj = 0;
282 while (jj < nP && polled[jj] != cls) ++jj;
283 if (jj == nP)
284 throw InputError(ErrorKind{"Model"},
285 "solver_mva_polling_analyzer: the polling table lists a class the station does not serve");
286 tabjj[t] = jj + 1;
287 if (t < qst.polling_table_sw.size() && !qst.polling_table_sw[t].disabled) {
288 const lang::Distrib<T>& d = qst.polling_table_sw[t];
289 T m = d.is_immediate() ? zero : d.mean;
290 T v = d.is_immediate() ? zero : T(d.scv * m * m);
291 if (!(num_traits<T>::to_double(m) > 0.0)) {
292 m = imm;
293 v = T(imm * imm);
294 }
295 s_mean[t] = m;
296 s_var[t] = v;
297 } else {
298 s_mean[t] = pm.r[jj];
299 s_var[t] = pm.delta2[jj];
300 }
301 }
302 Wp = polling::polling_qsys_table(pm.lambda, pm.b, pm.b2, tabjj, s_mean, s_var,
304 } else
305 switch (pt) {
308 break;
311 break;
313 // polling_reason admits K=1 only, where G-limited and 1-limited coincide
315 break;
317 if (qst.polling_par == 1)
319 else // Fuhrmann's k-limited relation (Haverkort eq. 9.27)
320 Wp = polling::polling_qsys_klimited(pm, static_cast<long>(qst.polling_par));
321 break;
324 break;
325 default:
326 throw UnsupportedError(ErrorKind{"SchedStrategy_POLLING"},
327 "solver_mva_polling_analyzer: unsupported polling type");
328 }
329
330 // R = W + 1/mu, then scale by the queue's visit ratio, as the reference does
331 MvaSolution<T> out;
332 out.Q = Matrix<T>(M, K, zero);
333 out.U = Matrix<T>(M, K, zero);
334 out.R = Matrix<T>(M, K, zero);
335 out.Tp = Matrix<T>(M, K, zero);
336 out.C.assign(K, zero);
337 out.X.assign(K, zero);
338 for (std::size_t jj = 0; jj < nP; ++jj) {
339 const std::size_t r = polled[jj];
340 const T mu = L.rates(q - 1, r);
341 const T Rr = T(T(Wp[jj] + T(one / mu)) * Vq[r]);
342 out.R(q - 1, r) = Rr;
343 out.C[r] = Rr;
344 out.Tp(src - 1, r) = pm.lambda[jj];
345 out.Tp(q - 1, r) = pm.lambda[jj];
346 out.X[r] = pm.lambda[jj];
347 out.U(q - 1, r) = T(pm.lambda[jj] / mu);
348 out.Q(q - 1, r) = T(pm.lambda[jj] * Rr);
349 }
350 out.method = "stationtime";
351 out.lG = 0.0;
352 return out;
353 }
354}
355
356} // namespace mva
357} // namespace line
358
359#endif // LINE_SOLVERS_MVA_SOLVER_MVA_POLLING_H
InputError(const std::string &what)
Definition error.h:105
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
std::size_t stateful_of_station(std::size_t st) const
std::size_t nof_nodes() const
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< NodeDef > nodes
every node, in creation order
std::vector< Matrix< T > > visits
(nchains) each (nstateful x nclasses)
double nclosedjobs() const
sn.nclosedjobs: the total population of the closed classes.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The option and result types every MVA analyzer shares.
@ CYCLIC
fixed cyclic order (or the polling table)
Definition lang_types.h:405
@ NONE
no visit timer
Definition lang_types.h:413
PollingType
Polling service disciplines, with the values of MATLAB PollingType.
Definition lang_types.h:391
@ KLIMITED
serve at most K per visit (K in pollingPar)
Definition lang_types.h:394
@ EXHAUSTIVE
serve until the queue empties
Definition lang_types.h:393
@ GATED
serve exactly the jobs present at the polling instant
Definition lang_types.h:392
@ GLIMITED
serve at most K of the jobs present at the polling instant
Definition lang_types.h:396
@ DECREMENTING
serve until the queue is one shorter than at arrival
Definition lang_types.h:395
NodeType
Node kinds, with the values of MATLAB NodeType.
Definition lang_types.h:345
MvaSolution< T > solver_mva_polling_analyzer(const qn::NetworkStruct< T > &L, const MvaOptions &opt)
Port of solver_mva_polling_analyzer.m.
std::string polling_poisson_warning(const qn::NetworkStruct< T > &L)
The reference's line_warning for method 'default' when a polled class has a non-Poisson arrival proce...
std::string polling_reason(const qn::NetworkStruct< T > &L, const std::string &method)
Why SolverMVA cannot answer a model holding a POLLING station under method, or "" when it can (or the...
std::vector< T > polling_qsys_table(const std::vector< T > &lambda, const std::vector< T > &b, const std::vector< T > &b2, const std::vector< std::size_t > &table, const std::vector< T > &s_mean, const std::vector< T > &s_var, bool gated)
Mean waiting times only; see polling_qsys_table_full.
std::vector< T > polling_qsys_klimited(const PollingMoments< T > &m, std::vector< long > K)
std::vector< T > polling_qsys_decrementing(const PollingMoments< T > &m)
std::vector< T > polling_qsys_exhaustive(const PollingMoments< T > &m)
Exhaustive service: the server empties a queue completely before switching.
std::vector< T > polling_qsys_gated(const PollingMoments< T > &m)
Gated service: only the jobs found at the polling instant are served.
std::vector< T > polling_qsys_1limited(const PollingMoments< T > &m)
1-limited polling: one job served per visit.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
Mean waiting times in polling systems: 1-limited and decrementing service, and the pseudo-conservatio...
Mean waiting times of a polling system under exhaustive service.
Approximate mean waiting times of a cyclic k-limited polling system.
Exact mean waiting times of a polling system with a polling table.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
bool is_immediate() const
static constexpr double Immediate
Rate of an Immediate distribution; its mean is 1/Immediate = 1e-8.
Definition lang_types.h:829
The options SolverMVA reads.
Definition mva_types.h:32
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
double lG
log of the normalizing constant, the reference's lG.
Definition mva_types.h:119
std::vector< T > C
Definition mva_types.h:99
Per-queue first two moments of the arrival, service and switchover processes, the reduced form both f...
std::vector< T > b2
second raw moment of the service time
std::vector< T > lambda
arrival rate per queue
std::vector< T > delta2
variance of the switchover time per queue
std::vector< T > r
mean switchover time per queue
std::vector< T > b
mean service time per queue
A node of the network.
One station of the network.
SchedStrategy sched
std::vector< Distrib< T > > polling_table_sw
double nservers
may be infinite (a Delay, or an inf-scheduled task)
std::vector< lang::PollingType > polling_type
Polling parameters for a POLLING station, MATLAB's pollingType, switchoverTime and pollingPar on the ...
lang::PollingOrder polling_order
std::vector< std::size_t > polling_table
The polling extensions (Queue.setPollingTable, setPollingOrder, setPollingTimer).
std::vector< Distrib< T > > switchover
lang::PollingTimer polling_timer_scope