LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
polling_qsys_exhaustive.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_EXHAUSTIVE_H
6#define LINE_API_POLLING_POLLING_QSYS_EXHAUSTIVE_H
7
8/**
9 * @file
10 * @ingroup api_polling
11 * Mean waiting times of a polling system under exhaustive service.
12 *
13 * Templated port of matlab/src/api/polling/polling_qsys_exhaustive.m, which is
14 * the station-time method of Ferguson and Aminetzah (1985) as reported by
15 * Takagi, ACM Computing Surveys 20(1), 1988, eq. (15). The MATLAB version takes
16 * MAP descriptors and immediately reduces them to the first two moments of the
17 * arrival, service and switchover processes; this port takes those moments
18 * directly through line::polling::PollingMoments, exactly as the 1-limited and
19 * decrementing ports do, so the MAP reduction stays in line::mam.
20 *
21 * The method solves an n^2 x n^2 linear system for the station times r_ij and
22 * then reads the waiting times off them. Everything is a rational function of
23 * the input moments plus one exact linear solve, so the whole computation stays
24 * in the field: a polling system with rational parameters has an exactly
25 * representable mean waiting time and the port is instantiable at Rational.
26 * That is worth having here because the denominators 1 - rho and 1 - rho_i both
27 * vanish at a stability boundary and appear cubed, so a rounded evaluation near
28 * one can return a finite but meaningless number.
29 *
30 * ORACLES USED IN THE TESTS.
31 * - n = 1 collapse. The formula reduces to the M/G/1 queue with multiple
32 * vacations, W = lambda b2/(2(1-rho)) + E[R^2]/(2 E[R]), which the port
33 * reproduces exactly (in Rational, digit for digit).
34 * - Pseudo-conservation law of Boxma and Groenendijk (1987),
35 * sum_i rho_i W_i = rho/(2(1-rho)) sum_i lambda_i b2_i
36 * + rho (delta2tot + R^2)/(2R)
37 * + R (rho^2 - sum_i rho_i^2)/(2(1-rho))
38 * + sum_i E[M_i],
39 * with E[M_i] = 0 under exhaustive service and E[M_i] = rho_i^2 R/(1-rho)
40 * under gated service. The port satisfies it as an identity, so exhaustive
41 * and gated are cross-checked against one common invariant.
42 *
43 * The CTMC of the same station is the independent oracle in the tests. The
44 * LDES polling server roves through empty buffers, as Takagi's server does,
45 * and agrees with these formulas within simulation error (both engines
46 * after the 2026-11 switchover and feedback fixes; see _kb/09-ldes-and-cache.md).
47 */
48
49#include <cstddef>
50#include <vector>
51
53#include "line/num/number.h"
54#include "line/util/error.h"
55#include "line/util/lu.h"
56#include "line/util/matrix.h"
57
58namespace line {
59namespace polling {
60
61namespace detail {
62
63/** Derived aggregates shared by the exhaustive and gated formulas. */
64template <class T>
65struct PollingAggregates {
66 std::vector<T> rho1; ///< per-queue load lambda_i b_i
67 T rho; ///< total load
68 T R; ///< total switchover time
69};
70
71template <class T>
72PollingAggregates<T> polling_aggregates(const PollingMoments<T>& m, const char* who) {
73 const std::size_t n = m.size();
74 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
75 PollingAggregates<T> a;
76 a.rho1.resize(n);
77 a.rho = zero;
78 a.R = zero;
79 for (std::size_t i = 0; i < n; ++i) {
80 a.rho1[i] = m.lambda[i] * m.b[i];
81 if (a.rho1[i] <= zero)
82 throw InputError(std::string(who) + ": every queue must carry a positive load");
83 a.rho += a.rho1[i];
84 a.R += m.r[i];
85 }
86 if (a.rho >= one) throw NumericError(ErrorKind{"Instability"}, std::string(who) + ": unstable system, rho >= 1");
87 if (a.R <= zero) throw InputError(std::string(who) + ": zero total switchover time");
88 return a;
89}
90
91} // namespace detail
92
93/**
94 * Exhaustive service: the server empties a queue completely before switching.
95 *
96 * Takagi (1988) eq. (15) in the station-time form. The unknowns are the n^2
97 * station times r_ij collected row-major as index (i-1) n + j, 1-based in the
98 * reference and shifted by one here.
99 *
100 * @param m per-queue first two moments of arrivals, service and switchover
101 * @return the n mean waiting times
102 */
103template <class T>
105 m.validate("polling_qsys_exhaustive");
106 const std::size_t n = m.size();
107 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
108 const T two = num_traits<T>::from_int(2);
109 const detail::PollingAggregates<T> a =
110 detail::polling_aggregates(m, "polling_qsys_exhaustive");
111 const std::vector<T>& rho1 = a.rho1;
112 const T rho = a.rho, r = a.R;
113
114 const std::size_t nn = n * n;
115 Matrix<T> A(nn, nn, zero);
116 std::vector<T> rhs(nn, zero);
117 std::size_t row = 0;
118 for (std::size_t i = 1; i <= n; ++i) {
119 for (std::size_t j = 1; j <= n; ++j, ++row) {
120 if (i > j) {
121 for (std::size_t k = i + 1; k <= n; ++k) A(row, (j - 1) * n + k - 1) -= one;
122 for (std::size_t k = 1; k + 1 <= j; ++k) A(row, (j - 1) * n + k - 1) -= one;
123 for (std::size_t k = j; k + 1 <= i; ++k) A(row, (k - 1) * n + j - 1) -= one;
124 A(row, (i - 1) * n + j - 1) += (one - rho1[i - 1]) / rho1[i - 1];
125 } else if (j > i) {
126 for (std::size_t k = i + 1; k + 1 <= j; ++k) A(row, (j - 1) * n + k - 1) -= one;
127 for (std::size_t k = j; k <= n; ++k) A(row, (k - 1) * n + j - 1) -= one;
128 for (std::size_t k = 1; k + 1 <= i; ++k) A(row, (k - 1) * n + j - 1) -= one;
129 A(row, (i - 1) * n + j - 1) += (one - rho1[i - 1]) / rho1[i - 1];
130 } else {
131 A(row, (i - 1) * n + i - 1) += one;
132 for (std::size_t k = 1; k <= n; ++k)
133 if (k != i) A(row, (i - 1) * n + k - 1) -= rho1[i - 1] / (one - rho1[i - 1]);
134 // The switchover variance charged to queue i is the one of the
135 // switchover that PRECEDES it, index i-1 with wraparound to n.
136 const T dprev = (i > 1) ? m.delta2[i - 2] : m.delta2[n - 1];
137 const T omr = one - rho1[i - 1];
138 rhs[row] = dprev / (omr * omr) +
139 m.lambda[i - 1] * m.b2[i - 1] * r * omr / ((one - rho) * omr * omr * omr);
140 }
141 }
142 }
143
144 const std::vector<T> f = solve(A, rhs);
145
146 std::vector<T> W(n);
147 for (std::size_t i = 1; i <= n; ++i) {
148 const T omr = one - rho1[i - 1];
149 T w = m.lambda[i - 1] * m.b2[i - 1] / (two * omr);
150 w += r * omr / (two * (one - rho));
151 T s = zero;
152 for (std::size_t j = 1; j <= n; ++j)
153 if (j != i) s += f[(i - 1) * n + j - 1];
154 s *= omr / rho1[i - 1];
155 s += (i > 1) ? m.delta2[i - 2] : m.delta2[n - 1];
156 s /= r * omr * two / (one - rho);
157 W[i - 1] = w + s;
158 }
159 return W;
160}
161
162/**
163 * Gated service: only the jobs found at the polling instant are served.
164 *
165 * Takagi (1988) eq. (20), same station-time unknowns and the same layout. The
166 * switchover variance enters at index i here, not i-1 as in the exhaustive
167 * form; that asymmetry is in the reference and is reproduced, and both forms
168 * satisfy the pseudo-conservation law, which is what pins them.
169 */
170template <class T>
171std::vector<T> polling_qsys_gated(const PollingMoments<T>& m) {
172 m.validate("polling_qsys_gated");
173 const std::size_t n = m.size();
174 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
175 const T two = num_traits<T>::from_int(2);
176 const detail::PollingAggregates<T> a = detail::polling_aggregates(m, "polling_qsys_gated");
177 const std::vector<T>& rho1 = a.rho1;
178 const T rho = a.rho, r = a.R;
179
180 const std::size_t nn = n * n;
181 Matrix<T> A(nn, nn, zero);
182 std::vector<T> rhs(nn, zero);
183 std::size_t row = 0;
184 for (std::size_t i = 1; i <= n; ++i) {
185 for (std::size_t j = 1; j <= n; ++j, ++row) {
186 if (i > j) {
187 for (std::size_t k = i; k <= n; ++k) A(row, (j - 1) * n + k - 1) = -one;
188 for (std::size_t k = 1; k + 1 <= j; ++k) A(row, (j - 1) * n + k - 1) = -one;
189 for (std::size_t k = j; k + 1 <= i; ++k) A(row, (k - 1) * n + j - 1) = -one;
190 A(row, (i - 1) * n + j - 1) = one / rho1[i - 1];
191 } else if (j > i) {
192 for (std::size_t k = i; k + 1 <= j; ++k) A(row, (j - 1) * n + k - 1) = -one;
193 for (std::size_t k = j; k <= n; ++k) A(row, (k - 1) * n + j - 1) = -one;
194 for (std::size_t k = 1; k + 1 <= i; ++k) A(row, (k - 1) * n + j - 1) = -one;
195 A(row, (i - 1) * n + j - 1) = one / rho1[i - 1];
196 } else {
197 A(row, (i - 1) * n + i - 1) += one;
198 for (std::size_t k = 1; k <= n; ++k)
199 if (k != i) A(row, (i - 1) * n + k - 1) = -rho1[i - 1];
200 for (std::size_t k = 1; k <= n; ++k)
201 A(row, (k - 1) * n + i - 1) -= rho1[i - 1] * rho1[i - 1];
202 rhs[row] = m.delta2[i - 1] + m.lambda[i - 1] * m.b2[i - 1] * r / (one - rho);
203 }
204 }
205 }
206
207 const std::vector<T> f = solve(A, rhs);
208
209 std::vector<T> W(n);
210 for (std::size_t i = 1; i <= n; ++i) {
211 T w = (one + rho1[i - 1]) * r / (two * (one - rho));
212 T s = zero;
213 for (std::size_t j = 1; j <= n; ++j)
214 if (j != i) s += f[(i - 1) * n + j - 1];
215 s /= rho1[i - 1];
216 for (std::size_t j = 1; j <= n; ++j) s += f[(j - 1) * n + i - 1];
217 w += (one - rho) * (one + rho1[i - 1]) * s / (two * r);
218 W[i - 1] = w;
219 }
220 return W;
221}
222
223} // namespace polling
224} // namespace line
225
226#endif // LINE_API_POLLING_POLLING_QSYS_EXHAUSTIVE_H
InputError(const std::string &what)
Definition error.h:105
NumericError(const std::string &what)
Definition error.h:112
The exception types the port throws.
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
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.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
std::vector< T > solve(const Matrix< T > &A, const std::vector< T > &b)
Convenience: solve Ax = b, leaving A and b untouched.
Definition lu.h:158
Number-type abstraction for the templated API port.
Mean waiting times in polling systems: 1-limited and decrementing service, and the pseudo-conservatio...
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