LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
pfqn_ldsy_bounds.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_PFQN_PFQN_LDSY_BOUNDS_H
6#define LINE_API_PFQN_PFQN_LDSY_BOUNDS_H
7
8/**
9 * @file
10 * @ingroup api_pfqn
11 * Shanthikumar-Yao (1988) throughput bounds for single-class closed networks
12 * with arbitrary queue-dependent service rates.
13 *
14 * Templated port of matlab/src/api/pfqn/pfqn_ldsy_bounds.m (native Python
15 * twin: python/line_solver/api/pfqn/throughput_bounds.py). J. G. Shanthikumar
16 * and D. D. Yao, "Throughput Bounds for Closed Queueing Networks with
17 * Queue-Dependent Service Rates", Perform. Eval. 9 (1988) 69-78. Station i at
18 * population n has demand L(i)/mu(i,n). A station whose scaling is
19 * mu(i,1)*n for every n <= N is an infinite server on the reachable range and
20 * is moved into Z exactly. Then:
21 *
22 * Thm 3.1, both sides: the fixed-rate networks at the per-station minimum
23 * and maximum rate over n = 1..N, solved exactly by MVA.
24 * Thm 3.2, lower, when every rate function is nondecreasing: the
25 * maximum-rate network with N-|a| jobs, a_i = n_i - 1, n_i the first
26 * population at which station i reaches its maximum rate.
27 * Thm 3.3, both sides (closed form): mu*_i = max_n N mu_i(n)/n (upper) and
28 * min_n N mu_i(n)/n (lower), X <= or >= 1/(sum_i L_i/mu*_i + Z/N).
29 * Thm 4.2, lower, at a c-server station (mu = mu1*min(n,c), 1 < c < N): a
30 * single server of rate c*mu1 plus an infinite server of rate
31 * c*mu1/(c-1); every other load-dependent station is first set to its
32 * minimum rate.
33 * Sec. 4.2, upper: each load-dependent station becomes K single servers of
34 * rate max_n (n+K-1) mu(n)/n; the balanced-load upper bound of the result,
35 * capped by its bottleneck, is minimized over the common K by the paper's
36 * stop-when-it-stops-improving scan.
37 *
38 * Xlo is the largest applicable lower bound, Xhi the smallest upper.
39 *
40 * ARITHMETIC: field operations and comparisons only (the integer server
41 * count c of Thm 4.2 is read by rounding, as MATLAB does). Exact under
42 * Rational.
43 */
44
45#include <cmath>
46#include <cstddef>
47#include <vector>
48
49#include "line/num/number.h"
50#include "line/util/error.h"
51#include "line/util/matrix.h"
52
53namespace line {
54namespace pfqn {
55
56/** Each bound of pfqn_ldsy_bounds, MATLAB's info struct; has_* false where MATLAB has NaN. */
57template <class T>
60 bool has_thm31_lo = false, has_thm31_hi = false, has_thm32_lo = false, has_thm33_lo = false,
61 has_thm33_hi = false, has_thm42_lo = false, has_sec42_bjb = false, has_sec42_hi = false;
62 int Kopt = 0; ///< 0 where MATLAB has NaN
63};
64
65/** Return value of pfqn_ldsy_bounds, mirroring [Xlo, Xhi, info]. */
66template <class T>
72
73namespace detail {
74
75/** Exact single-class MVA throughput, fixed-rate single servers D, think time Z. */
76template <class T>
77T ldsy_mva_x(const std::vector<T>& D, int N, const T& Z) {
78 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
79 std::vector<T> Q(D.size(), zero), R(D.size(), zero);
80 T X = zero;
81 for (int n = 1; n <= N; ++n) {
82 T s = Z;
83 for (std::size_t i = 0; i < D.size(); ++i) {
84 R[i] = T(D[i] * T(one + Q[i]));
85 s += R[i];
86 }
87 X = T(num_traits<T>::from_int(n) / s);
88 for (std::size_t i = 0; i < D.size(); ++i) Q[i] = T(X * R[i]);
89 }
90 return X;
91}
92
93} // namespace detail
94
95/**
96 * @brief Shanthikumar-Yao (1988) throughput bounds for queue-dependent rates.
97 *
98 * @param L (M) base demands
99 * @param N population, at least 1
100 * @param Z total think time of the infinite-server stations
101 * @param mu (M x Nmu) rate scaling, the pfqn_mvald convention; columns beyond
102 * Nmu repeat the last one (limited load dependence). Empty for ones.
103 */
104template <class T>
105LdsyBounds<T> pfqn_ldsy_bounds(const std::vector<T>& Lin, int N, const T& Zin, const Matrix<T>& muIn) {
106 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
107 const std::size_t M = Lin.size();
108 if (N < 1) throw InputError(ErrorKind{"Invalid"}, "pfqn_ldsy_bounds needs N >= 1.");
109 const std::size_t NN = static_cast<std::size_t>(N);
110 Matrix<T> mu0(M, NN, one);
111 if (!muIn.empty()) {
112 if (muIn.rows() != M)
113 throw InputError(ErrorKind{"Invalid"}, "pfqn_ldsy_bounds: mu must have one row per station.");
114 for (std::size_t i = 0; i < M; ++i)
115 for (std::size_t n = 0; n < NN; ++n)
116 mu0(i, n) = muIn(i, n < muIn.cols() ? n : muIn.cols() - 1);
117 }
118 for (std::size_t i = 0; i < M; ++i)
119 for (std::size_t n = 0; n < NN; ++n)
120 if (!std::isfinite(num_traits<T>::to_double(mu0(i, n))) || !(mu0(i, n) > zero))
121 throw InputError(ErrorKind{"Invalid"},
122 "pfqn_ldsy_bounds: rate scalings must be finite and positive.");
123 const T tol = num_traits<T>::from_double(1e-12);
124 const auto tabs = [](const T& x) -> T { return x < T(0) ? T(-x) : x; };
125 T Z = Zin;
126 std::vector<T> L;
127 std::vector<std::vector<T> > mu;
128 for (std::size_t i = 0; i < M; ++i) {
129 if (!(Lin[i] > zero)) continue;
130 // Exact reduction: an infinite server on 1..N is a delay.
131 bool isinfst = true;
132 for (std::size_t n = 0; n < NN && isinfst; ++n) {
133 const T lin = T(mu0(i, 0) * num_traits<T>::from_int(static_cast<long>(n + 1)));
134 if (tabs(T(mu0(i, n) - lin)) > T(tol * lin)) isinfst = false;
135 }
136 if (isinfst) {
137 Z += T(Lin[i] / mu0(i, 0));
138 continue;
139 }
140 L.push_back(Lin[i]);
141 std::vector<T> row(NN);
142 for (std::size_t n = 0; n < NN; ++n) row[n] = mu0(i, n);
143 mu.push_back(row);
144 }
145 LdsyBounds<T> out;
146 LdsyBoundsInfo<T>& info = out.info;
147 const T NT = num_traits<T>::from_int(N);
148 if (L.empty()) {
149 if (!(Z > zero))
150 throw InputError(ErrorKind{"Invalid"},
151 "pfqn_ldsy_bounds needs a station with positive demand or a positive think time.");
152 const T X = T(NT / Z);
153 info.thm31_lo = info.thm31_hi = info.thm33_lo = info.thm33_hi = X;
154 info.has_thm31_lo = info.has_thm31_hi = info.has_thm33_lo = info.has_thm33_hi = true;
155 out.Xlo = X;
156 out.Xhi = X;
157 return out;
158 }
159 const std::size_t Ms = L.size();
160 std::vector<T> muL(Ms), muU(Ms);
161 std::vector<bool> isld(Ms);
162 for (std::size_t i = 0; i < Ms; ++i) {
163 muL[i] = mu[i][0];
164 muU[i] = mu[i][0];
165 for (std::size_t n = 1; n < NN; ++n) {
166 if (mu[i][n] < muL[i]) muL[i] = mu[i][n];
167 if (mu[i][n] > muU[i]) muU[i] = mu[i][n];
168 }
169 isld[i] = T(muU[i] - muL[i]) > T(tol * muU[i]);
170 }
171 std::vector<T> lows, highs;
172
173 // Theorem 3.1
174 {
175 std::vector<T> Dlo(Ms), Dhi(Ms);
176 for (std::size_t i = 0; i < Ms; ++i) {
177 Dlo[i] = T(L[i] / muL[i]);
178 Dhi[i] = T(L[i] / muU[i]);
179 }
180 info.thm31_lo = detail::ldsy_mva_x(Dlo, N, Z);
181 info.thm31_hi = detail::ldsy_mva_x(Dhi, N, Z);
182 info.has_thm31_lo = info.has_thm31_hi = true;
183 lows.push_back(info.thm31_lo);
184 highs.push_back(info.thm31_hi);
185 }
186
187 // Theorem 3.2 (nondecreasing rates)
188 bool nondecr = true;
189 if (N != 1)
190 for (std::size_t i = 0; i < Ms && nondecr; ++i)
191 for (std::size_t n = 1; n < NN; ++n)
192 if (T(mu[i][n] - mu[i][n - 1]) < T(-(tol * mu[i][n]))) {
193 nondecr = false;
194 break;
195 }
196 if (nondecr) {
197 long A = 0;
198 for (std::size_t i = 0; i < Ms; ++i) {
199 const T thr = T(muU[i] * T(one - tol));
200 std::size_t a = 0;
201 while (a < NN && mu[i][a] < thr) ++a;
202 A += static_cast<long>(a);
203 }
204 if (A < N) {
205 std::vector<T> D(Ms);
206 for (std::size_t i = 0; i < Ms; ++i) D[i] = T(L[i] / muU[i]);
207 info.thm32_lo = detail::ldsy_mva_x(D, static_cast<int>(N - A), Z);
208 info.has_thm32_lo = true;
209 lows.push_back(info.thm32_lo);
210 }
211 }
212
213 // Theorem 3.3 (closed form)
214 {
215 T shi = T(Z / NT), slo = T(Z / NT);
216 for (std::size_t i = 0; i < Ms; ++i) {
217 T rmax = T(NT * mu[i][0]), rmin = rmax;
218 for (std::size_t n = 1; n < NN; ++n) {
219 const T rr = T(NT * mu[i][n] / num_traits<T>::from_int(static_cast<long>(n + 1)));
220 if (rr > rmax) rmax = rr;
221 if (rr < rmin) rmin = rr;
222 }
223 shi += T(L[i] / rmax);
224 slo += T(L[i] / rmin);
225 }
226 info.thm33_hi = T(one / shi);
227 info.thm33_lo = T(one / slo);
228 info.has_thm33_hi = info.has_thm33_lo = true;
229 lows.push_back(info.thm33_lo);
230 highs.push_back(info.thm33_hi);
231 }
232
233 // Theorem 4.2 (c-server stations)
234 {
235 std::vector<bool> isms(Ms, false);
236 std::vector<long> cvec(Ms, 0);
237 bool any = false;
238 for (std::size_t i = 0; i < Ms; ++i) {
239 const long c = std::lround(num_traits<T>::to_double(T(mu[i][NN - 1] / mu[i][0])));
240 if (!(c > 1 && c < N)) continue;
241 bool ok = true;
242 for (std::size_t n = 0; n < NN && ok; ++n) {
243 const T cap = num_traits<T>::from_int(static_cast<long>(n + 1) < c ? static_cast<long>(n + 1) : c);
244 const T shape = T(mu[i][n] / mu[i][0]);
245 if (tabs(T(shape - cap)) > T(tol * cap)) ok = false;
246 }
247 if (ok) {
248 isms[i] = true;
249 cvec[i] = c;
250 any = true;
251 }
252 }
253 if (any) {
254 std::vector<T> Dq(Ms);
255 for (std::size_t i = 0; i < Ms; ++i) Dq[i] = T(L[i] / muL[i]); // Thm 3.1 step at the other LD stations
256 T Zq = Z;
257 for (std::size_t i = 0; i < Ms; ++i) {
258 if (!isms[i]) continue;
259 const T m1 = mu[i][0];
260 const T cT = num_traits<T>::from_int(cvec[i]);
261 Dq[i] = T(L[i] / T(cT * m1));
262 Zq += T(L[i] * T(cT - one) / T(cT * m1));
263 }
264 info.thm42_lo = detail::ldsy_mva_x(Dq, N, Zq);
265 info.has_thm42_lo = true;
266 lows.push_back(info.thm42_lo);
267 }
268 }
269
270 // Section 4.2 upper bound over a common K, scanned from K = 1 until the
271 // balanced-load bound UB(K) stops improving; every scanned K gives a valid
272 // bound, further capped by the bottleneck of its network.
273 bool anyld = false;
274 for (std::size_t i = 0; i < Ms; ++i) anyld = anyld || isld[i];
275 if (anyld) {
276 bool havePrev = false, haveBest = false;
277 T prevUb = zero, best = zero;
278 int Kopt = 1;
279 for (int K = 1; K <= N; ++K) {
280 const T KT = num_traits<T>::from_int(K);
281 T Dtot = zero, Dmax = zero;
282 long Mtot = 0;
283 bool firstD = true;
284 for (std::size_t i = 0; i < Ms; ++i) {
285 T Di;
286 if (!isld[i]) {
287 Di = L[i];
288 Dtot += Di;
289 Mtot += 1;
290 } else {
291 T muStar = zero;
292 for (std::size_t n = 0; n < NN; ++n) {
293 const T nT = num_traits<T>::from_int(static_cast<long>(n + 1));
294 const T v = T(T(nT + KT - one) * mu[i][n] / nT);
295 if (n == 0 || v > muStar) muStar = v;
296 }
297 Di = T(L[i] / muStar); // demand of each of the K replicas
298 Dtot += T(KT * Di);
299 Mtot += K;
300 }
301 if (firstD || Di > Dmax) Dmax = Di;
302 firstD = false;
303 }
304 const T ubK = T(NT / T(Dtot + T(NT - one) * Dtot / num_traits<T>::from_int(Mtot)));
305 const T capK = T(one / Dmax);
306 const T m = ubK < capK ? ubK : capK;
307 if (!haveBest || m < best) best = m;
308 haveBest = true;
309 if (havePrev && !(ubK < prevUb)) break;
310 prevUb = ubK;
311 havePrev = true;
312 Kopt = K;
313 }
314 info.sec42_bjb = prevUb;
315 info.sec42_hi = best;
316 info.has_sec42_bjb = info.has_sec42_hi = true;
317 info.Kopt = Kopt;
318 highs.push_back(best);
319 }
320
321 out.Xlo = lows[0];
322 for (const T& v : lows)
323 if (v > out.Xlo) out.Xlo = v;
324 out.Xhi = highs[0];
325 for (const T& v : highs)
326 if (v < out.Xhi) out.Xhi = v;
327 return out;
328}
329
330} // namespace pfqn
331} // namespace line
332
333#endif // LINE_API_PFQN_PFQN_LDSY_BOUNDS_H
InputError(const std::string &what)
Definition error.h:105
std::size_t cols() const
Definition matrix.h:90
std::size_t rows() const
Definition matrix.h:89
bool empty() const
Definition matrix.h:92
The exception types the port throws.
Dense matrix and non-owning view.
LdsyBounds< T > pfqn_ldsy_bounds(const std::vector< T > &Lin, int N, const T &Zin, const Matrix< T > &muIn)
Shanthikumar-Yao (1988) throughput bounds for queue-dependent rates.
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
Each bound of pfqn_ldsy_bounds, MATLAB's info struct; has_* false where MATLAB has NaN.
int Kopt
0 where MATLAB has NaN
Return value of pfqn_ldsy_bounds, mirroring [Xlo, Xhi, info].
LdsyBoundsInfo< T > info