LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
polling_qsys_1limited.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_API_POLLING_POLLING_QSYS_1LIMITED_H
6#define LINE_API_POLLING_POLLING_QSYS_1LIMITED_H
7
8/**
9 * @file
10 * @ingroup api_polling
11 * Mean waiting times in polling systems: 1-limited and decrementing service,
12 * and the pseudo-conservation law.
13 *
14 * Templated port of matlab/src/api/polling/polling_qsys_1limited.m,
15 * polling_qsys_decrementing.m and polling_pcl.m. The MATLAB versions take MAP descriptors and
16 * reduce them to the first two moments of the arrival, service and switchover
17 * processes; this port takes those moments directly, so the MAP reduction stays
18 * in line::mam (map_lambda, map_mean, map_moment, map_var) and the waiting-time
19 * formula is a pure rational function of the moments. Callers holding MAPs
20 * compose the two.
21 *
22 * Both formulas stay in the field, so a polling system with rational parameters
23 * has an exactly representable mean waiting time. Worth having: the denominator
24 * 1 - rho - lambda R vanishes at the stability boundary, and near it a rounded
25 * evaluation can return a finite but meaningless number where the exact one
26 * shows the pole.
27 */
28
29#include <algorithm>
30#include <cctype>
31#include <cmath>
32#include <cstddef>
33#include <limits>
34#include <string>
35#include <vector>
36
37#include "line/num/number.h"
38#include "line/util/error.h"
39
40namespace line {
41namespace polling {
42
43/**
44 * Per-queue first two moments of the arrival, service and switchover
45 * processes, the reduced form both formulas consume.
46 */
47template <class T>
49 std::vector<T> lambda; ///< arrival rate per queue
50 std::vector<T> b; ///< mean service time per queue
51 std::vector<T> b2; ///< second raw moment of the service time
52 std::vector<T> r; ///< mean switchover time per queue
53 std::vector<T> delta2; ///< variance of the switchover time per queue
54
55 std::size_t size() const { return lambda.size(); }
56 void validate(const char* who) const {
57 if (lambda.empty()) throw InputError(std::string(who) + ": empty system");
58 if (b.size() != lambda.size() || b2.size() != lambda.size() ||
59 r.size() != lambda.size() || delta2.size() != lambda.size())
60 throw InputError(std::string(who) + ": moment vectors have different lengths");
61 }
62};
63
64/**
65 * 1-limited polling: one job served per visit.
66 *
67 * W_i = (1-rho+rho_i)/(1-rho-lambda_i R) * (1-rho)/((1-rho)rho + sum rho_j^2)
68 * * (rho/(2(1-rho)) sum_j lambda_j b2_j + rho sum_j delta2_j/(2R)
69 * + R/(2(1-rho)) sum_j rho_j (1+rho_j))
70 *
71 * Boxma-Meister approximation, exact for symmetric systems. The last term
72 * sums over ALL queues (pseudo-conservation law), not over queue i alone.
73 */
74template <class T>
75std::vector<T> polling_qsys_1limited(const PollingMoments<T>& m) {
76 m.validate("polling_qsys_1limited");
77 const std::size_t n = m.size();
78 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
79 const T two = num_traits<T>::from_int(2);
80
81 std::vector<T> rho1(n);
82 T rho = zero, R = zero, sumLb2 = zero, sumDelta2 = zero, sumRho2 = zero, sumRho1pRho = zero;
83 for (std::size_t i = 0; i < n; ++i) {
84 rho1[i] = m.lambda[i] * m.b[i];
85 rho += rho1[i];
86 R += m.r[i];
87 sumLb2 += m.lambda[i] * m.b2[i];
88 sumDelta2 += m.delta2[i];
89 sumRho2 += rho1[i] * rho1[i];
90 sumRho1pRho += rho1[i] * (one + rho1[i]);
91 }
92 if (rho >= one) throw NumericError(ErrorKind{"Instability"}, "polling_qsys_1limited: unstable system, rho >= 1");
93 if (R == zero) throw InputError("polling_qsys_1limited: zero total switchover time");
94
95 const T den2 = (one - rho) * rho + sumRho2;
96 if (den2 == zero) throw NumericError(ErrorKind{"Degenerate"}, "polling_qsys_1limited: degenerate load configuration");
97
98 std::vector<T> W(n);
99 for (std::size_t i = 0; i < n; ++i) {
100 const T den1 = one - rho - m.lambda[i] * R;
101 // AT THE POLE THE ANSWER IS INFINITE, NOT UNAVAILABLE. Queue i is stable
102 // under 1-limited service while rho + lambda_i R < 1; on the boundary its
103 // mean waiting time diverges, and polling_qsys_1limited.m divides through
104 // and returns Inf rather than erroring. Refusing here turned a model the
105 // reference answers into a failed solve (polling_klimited, whose second
106 // class sits exactly on the boundary). An exact field has no infinity, so
107 // there the pole is still reported rather than misrepresented as a number.
108 // PAST the boundary the formula turns negative while the wait diverges, so that side is the pole too.
109 if (den1 <= zero) {
110 if constexpr (num_traits<T>::is_exact) {
111 throw NumericError(ErrorKind{"Instability"},
112 "polling_qsys_1limited: queue at or past the stability boundary, "
113 "rho + lambda_i R >= 1, and the exact field has no infinity");
114 } else {
115 W[i] = num_traits<T>::from_double(std::numeric_limits<double>::infinity());
116 continue;
117 }
118 }
119 T w = (one - rho + rho1[i]) / den1;
120 w *= (one - rho) / den2;
121 w *= rho / (two * (one - rho)) * sumLb2 + rho * sumDelta2 / (two * R) +
122 R / (two * (one - rho)) * sumRho1pRho;
123 W[i] = w;
124 }
125 return W;
126}
127
128/** Weights and right-hand side of the pseudo-conservation law. */
129template <class T>
131 std::vector<T> w; ///< weight of E[W_i]
132 T rhs; ///< right-hand side
133 T pcl; ///< rhs / sum(w)
134};
135
136/**
137 * Pseudo-conservation law SUM_i w_i E[W_i] = rhs of a cyclic polling system
138 * with Poisson arrivals (Takagi 1988, eq (10); MATLAB polling_pcl.m). code[i]
139 * is 'E' (exhaustive), 'G' (gated), 'L' (1-limited) or 'D' (decrementing).
140 *
141 * w_i = rho_i (E, G); rho_i (1 - lambda_i R/(1-rho)) (L);
142 * rho_i (1 - lambda_i (1-rho_i) R/(1-rho)) (D)
143 * rhs = rho SUM lambda_i b2_i/(2(1-rho)) + rho Delta2/(2R)
144 * + R (rho - SUM rho_i^2)/(2(1-rho)) + R/(1-rho) SUM_{G,L} rho_i^2
145 * - R/(2(1-rho)) SUM_D rho_i lambda_i^2 b2_i
146 */
147template <class T>
148PollingPcl<T> polling_pcl(const PollingMoments<T>& m, const std::string& code) {
149 m.validate("polling_pcl");
150 const std::size_t n = m.size();
151 if (code.size() != n) throw InputError("polling_pcl: one discipline per queue is required");
152 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
153 const T two = num_traits<T>::from_int(2);
154 T rho = zero, R = zero, D2 = zero, sumsq = zero, sumlb2 = zero;
155 std::vector<T> rhoi(n);
156 for (std::size_t i = 0; i < n; ++i) {
157 rhoi[i] = m.lambda[i] * m.b[i];
158 rho = rho + rhoi[i];
159 R = R + m.r[i];
160 D2 = D2 + m.delta2[i];
161 sumsq = sumsq + rhoi[i] * rhoi[i];
162 sumlb2 = sumlb2 + m.lambda[i] * m.b2[i];
163 }
164 PollingPcl<T> out;
165 out.w.resize(n);
166 T sumGL = zero, sumD = zero, sumw = zero;
167 for (std::size_t i = 0; i < n; ++i) {
168 switch (std::toupper(static_cast<unsigned char>(code[i]))) {
169 case 'E': out.w[i] = rhoi[i]; break;
170 case 'G': out.w[i] = rhoi[i]; sumGL = sumGL + rhoi[i] * rhoi[i]; break;
171 case 'L':
172 out.w[i] = rhoi[i] * (one - m.lambda[i] * R / (one - rho));
173 sumGL = sumGL + rhoi[i] * rhoi[i];
174 break;
175 case 'D':
176 out.w[i] = rhoi[i] * (one - m.lambda[i] * (one - rhoi[i]) * R / (one - rho));
177 sumD = sumD + rhoi[i] * m.lambda[i] * m.lambda[i] * m.b2[i];
178 break;
179 default:
180 throw UnsupportedError(ErrorKind{"SchedStrategy_POLLING"},
181 "polling_pcl: unsupported discipline");
182 }
183 sumw = sumw + out.w[i];
184 }
185 const T resid = (R > zero) ? rho * D2 / (two * R) : zero;
186 out.rhs = rho * sumlb2 / (two * (one - rho)) + resid + R * (rho - sumsq) / (two * (one - rho)) +
187 R / (one - rho) * sumGL - R / (two * (one - rho)) * sumD;
188 out.pcl = out.rhs / sumw;
189 return out;
190}
191
192/**
193 * Decrementing service. Parameters are compared with the 1e-6 relative
194 * tolerance of the MATLAB, JAR and Python versions in floating point, and
195 * exactly in an exact field. A symmetric system has the exact closed form
196 *
197 * W = delta2/(2r) + (N lambda b2 (1 - lambda r) + (r + lambda delta2)(N - rho))
198 * / (2 (1 - rho - lambda r (N - rho)))
199 *
200 * and an asymmetric one the approximation W_i = c f_i with
201 * f_i = (1-rho_i)/(1-rho-lambda_i(1-rho_i)R), c fixed by the exact
202 * pseudo-conservation law (polling_pcl); within about 15% of the exact CTMC
203 * on two- and three-queue exponential systems.
204 */
205/** PCL-scaled approximation of an asymmetric decrementing system. */
206template <class T>
208 const std::size_t n = m.size();
209 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
210 T rho = zero, R = zero;
211 for (std::size_t i = 0; i < n; ++i) {
212 rho = rho + m.lambda[i] * m.b[i];
213 R = R + m.r[i];
214 }
215 bool unstable = (one - rho <= zero);
216 std::vector<T> f(n);
217 for (std::size_t i = 0; i < n; ++i) {
218 const T rhoi = m.lambda[i] * m.b[i];
219 const T den = one - rho - m.lambda[i] * (one - rhoi) * R;
220 if (den <= zero) {
221 unstable = true;
222 break;
223 }
224 f[i] = (one - rhoi) / den;
225 }
226 if (unstable)
227 throw NumericError(ErrorKind{"Instability"},
228 "polling_qsys_decrementing: unstable system, rho + lambda_i (1 - rho_i) R >= 1 for some queue");
229 const PollingPcl<T> p = polling_pcl(m, std::string(n, 'D'));
230 T wf = zero;
231 for (std::size_t i = 0; i < n; ++i) wf = wf + p.w[i] * f[i];
232 const T c = p.rhs / wf;
233 for (std::size_t i = 0; i < n; ++i) f[i] = c * f[i];
234 return f;
235}
236
237template <class T>
239 m.validate("polling_qsys_decrementing");
240 const std::size_t n = m.size();
241 const auto differs = [](const T& a, const T& c) {
242 if constexpr (num_traits<T>::is_exact) {
243 return a != c;
244 } else {
245 const double x = num_traits<T>::to_double(a), y = num_traits<T>::to_double(c);
246 const double scale = std::max(std::fabs(x), std::fabs(y));
247 return scale > 0.0 && std::fabs(x - y) / scale > 1e-6;
248 }
249 };
250 for (std::size_t i = 1; i < n; ++i)
251 if (differs(m.lambda[i], m.lambda[0]) || differs(m.b[i], m.b[0]) ||
252 differs(m.b2[i], m.b2[0]) || differs(m.r[i], m.r[0]) ||
253 differs(m.delta2[i], m.delta2[0]))
255
256 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
257 const T two = num_traits<T>::from_int(2);
258 const T N = num_traits<T>::from_int(static_cast<long>(n));
259 const T lam = m.lambda[0], b2s = m.b2[0], r = m.r[0], d2 = m.delta2[0];
260 const T rho = N * lam * m.b[0];
261
262 const T denom = two * (one - rho - lam * r * (N - rho));
263 if (denom <= zero)
264 throw NumericError(ErrorKind{"Instability"},
265 "polling_qsys_decrementing: unstable system, rho + lambda r (N - rho) >= 1");
266
267 const T residualSwitchover = (r > zero) ? d2 / (two * r) : zero;
268 const T w = residualSwitchover +
269 (N * lam * b2s * (one - lam * r) + (r + lam * d2) * (N - rho)) / denom;
270 return std::vector<T>(n, w);
271}
272
273} // namespace polling
274} // namespace line
275
276#endif // LINE_API_POLLING_POLLING_QSYS_1LIMITED_H
InputError(const std::string &what)
Definition error.h:105
NumericError(const std::string &what)
Definition error.h:112
UnsupportedError(const std::string &what)
Definition error.h:119
The exception types the port throws.
PollingPcl< T > polling_pcl(const PollingMoments< T > &m, const std::string &code)
Pseudo-conservation law SUM_i w_i E[W_i] = rhs of a cyclic polling system with Poisson arrivals (Taka...
std::vector< T > polling_qsys_decrementing(const PollingMoments< T > &m)
std::vector< T > polling_qsys_decrementing_asymmetric(const PollingMoments< T > &m)
Decrementing service.
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
Number-type abstraction for the templated API port.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
Per-queue first two moments of the arrival, service and switchover processes, the reduced form both f...
void validate(const char *who) const
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
Weights and right-hand side of the pseudo-conservation law.
std::vector< T > w
weight of E[W_i]