LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
pfqn_sib.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_SIB_H
6#define LINE_API_PFQN_PFQN_SIB_H
7
8/**
9 * @file
10 * @ingroup api_pfqn
11 * Successively Improving Bounds (Srinivasan 1985/1987) on the cycle time and
12 * throughput of a single-class closed product-form network.
13 *
14 * Templated port of matlab/src/api/pfqn/pfqn_sib.m, including its local
15 * functions phi_u1, sigma and betaL. The hierarchy bounds
16 * phi(K) = sum_m rho_m Q_m(K) from both sides, with level 1 the closed form of
17 * Theorem 2.1 and higher levels the S_i power-sum forms of Theorems 3.5 and
18 * 3.6, and reads the cycle time off W(N) = sum(L) (1 + phi(N-1)).
19 *
20 * REFERENCE DEFECT ABOVE LEVEL 1, and the port does NOT reproduce it.
21 * pfqn_sib.m declares phi_u1, sigma and betaL as NESTED functions, which in
22 * MATLAB share the parent's workspace rather than getting their own. phi_u1
23 * assigns `eta = (K-1)/K` internally, and the parent has already set
24 * `eta = (N-2)/(N-1)` for the Theorem 3.5 prefactor 0.5/eta. At level 1 sigma
25 * returns before it ever calls phi_u1, so eta survives and the bound is
26 * correct; from level 2 on, sigma calls phi_u1(N-3), which OVERWRITES the
27 * parent's eta, and the prefactor 0.5/eta is then evaluated with the wrong
28 * value. On L = [1/2, 1/3, 1/5], N = 5 the parent eta is 3/4 and the clobbered
29 * one is 1/2, so phi_u_n comes out 1.5x too large -- 2.4919 instead of
30 * 1.6613 -- which exceeds the Section-2 baseline 1.7798, so `min` discards it.
31 * The net effect is that MATLAB's SIB hierarchy is INERT above level 1: levels
32 * 2, 3 and 4 all return the level-0 baseline, and the "bound" reported at
33 * level 2 (X in [1.7407, 1.9335]) is LOOSER than the one at level 1
34 * ([1.7607, 1.9335]), which contradicts the monotonicity the method is for.
35 * A C++ port has no such aliasing -- the three helpers are lambdas with their
36 * own scope -- so this port computes the intended Theorem 3.5 value and its
37 * bounds do tighten with the level: level 2 gives 1.8182, level 3 gives
38 * 1.8369, both still below the exact 1.8558. Reproducing the MATLAB value here
39 * would mean deliberately reintroducing a scoping accident, so the divergence
40 * is documented and pinned in the tests instead.
41 *
42 * THINK TIME follows the reference's Section-3.2 substitution for
43 * terminal-driven systems: eq. (3.24) has the form of eq. (2.5) once
44 * rho_m = L_m/L is replaced by rho~_m = L_m/(Z+L) and the power sums by
45 * S~_i = sum rho~_m^i, so every bound applies unchanged and the cycle time
46 * (residence plus think time) is (Z+L)(1+phi~(N-1)). A negative Z is refused,
47 * as pfqn_sib.m does (pfqn_sib:negativeThinkTime). The port keeps its
48 * N >= 2 precondition unchanged.
49 *
50 * ARITHMETIC. Both Theorem 3.5 and Theorem 2.1 solve a quadratic, so the
51 * bounds carry a square root and the routine is gated on
52 * num_traits<T>::has_transcendental. That is a real restriction rather than a
53 * formality: unlike the CBH and PBH families, SIB cannot be evaluated exactly.
54 */
55
56#include <algorithm>
57#include <cmath>
58#include <cstddef>
59#include <vector>
60
61#include "line/num/number.h"
62#include "line/util/error.h"
63
64namespace line {
65namespace pfqn {
66
67/** Return value of pfqn_sib, mirroring [Xlo, Xhi, Wlo, Whi]. */
68template <class T>
69struct SibBounds {
70 T Xlo;
71 T Xhi;
72 T Wlo;
73 T Whi;
74};
75
76/**
77 * @brief Successively Improving Bounds (Srinivasan 1985/1987) on the cycle
78 * time and throughput of a single-class closed product-form network.
79 *
80 * @param L (M) fixed-rate demands; delay demand goes in Z
81 * @param N population, at least 2
82 * @param Z total think time, nonnegative (Section-3.2 substitution)
83 * @param level bound level >= 1, default 3 at the convenience overload
84 */
85template <class T>
86SibBounds<T> pfqn_sib(const std::vector<T>& L, int N, const T& Z, int level) {
88 "pfqn_sib requires transcendental arithmetic (the bounds solve a quadratic)");
89 using std::sqrt;
90 const std::size_t M = L.size();
91 if (M == 0) throw InputError("pfqn_sib: empty demand vector");
92 if (N < 2) throw InputError("pfqn_sib: population must be at least two");
93 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
94 if (Z < zero) throw InputError("pfqn_sib needs a nonnegative think time Z.");
95 const int lv = std::max(1, level);
96
97 // Section 3.2: W(N) = (Z+L)(1+phi~(N-1)) with rho~_m = L_m/(Z+L), so Lsum
98 // below is the total loading Z+L and the S_i are the S~_i of the paper.
99 T Lsum = zero;
100 for (const T& x : L) Lsum += x;
101 if (Lsum == zero) throw InputError("pfqn_sib: all demands are zero");
102 Lsum += Z;
103 std::vector<T> rho(M);
104 T rho_u = zero;
105 for (std::size_t i = 0; i < M; ++i) {
106 rho[i] = T(L[i] / Lsum);
107 if (rho[i] > rho_u) rho_u = rho[i];
108 }
109 const int imax = lv + 3;
110 std::vector<T> S(static_cast<std::size_t>(imax) + 1, zero); // S[i], i = 1..imax
111 for (int i = 1; i <= imax; ++i) {
112 T s = zero;
113 for (std::size_t m = 0; m < M; ++m) s += num_pow_int(rho[m], static_cast<unsigned>(i));
114 S[static_cast<std::size_t>(i)] = s;
115 }
116 const T S2 = S[2];
117
118 // alpha_i, i = 0..level (eqs. 3.5-3.6).
119 std::vector<T> alpha(static_cast<std::size_t>(lv) + 1, zero);
120 alpha[0] = S2;
121 for (int i = 1; i <= lv; ++i) {
122 T acc = zero;
123 for (int j = 0; j <= i - 1; ++j)
124 acc += S[static_cast<std::size_t>(i + 1 - j)] * alpha[static_cast<std::size_t>(j)];
125 alpha[static_cast<std::size_t>(i)] = T(S[static_cast<std::size_t>(i + 2)] - acc);
126 }
127
128 // Level-1 upper bound on phi(K), eq. 3.18.
129 const auto phi_u1 = [&](int K) -> T {
130 if (K <= 0) return zero;
131 if (K == 1) return S2;
132 const T KT = num_traits<T>::from_int(K);
133 const T eta = T(T(KT - one) / KT);
134 const T T1 = T(T(KT - one) * rho_u - one);
135 const T disc = T(T1 * T1 + num_traits<T>::from_int(4) * T(KT - one) * S2);
136 return T(T(num_traits<T>::from_rational(1, 2) / eta) * T(T1 + sqrt(disc)));
137 };
138
139 // eq. (3.22c). NN plays the role of N-1.
140 const auto sigma = [&](int NN, int i) -> T {
141 T s = zero;
142 if (i <= 0) return s;
143 const T Dbar = T(one + phi_u1(NN - 2));
144 T pnum = one;
145 for (int j = 1; j <= i; ++j) {
146 pnum *= num_traits<T>::from_int(NN - 1 - (j - 1));
147 s += T(T(rho_u * S[static_cast<std::size_t>(j + 1)] - S[static_cast<std::size_t>(j + 2)]) *
148 pnum / num_pow_int(Dbar, static_cast<unsigned>(j)));
149 }
150 return s;
151 };
152
153 // eq. (3.23c). NN plays the role of N-1.
154 const auto betaL = [&](int NN, int i) -> T {
155 T b = zero;
156 if (i <= 0) return b;
157 for (int j = 1; j <= i - 1; ++j) {
158 T p = one;
159 for (int m = 2; m <= j; ++m)
160 p *= T(num_traits<T>::from_int(NN - m) / T(one + phi_u1(NN - m)));
161 b += alpha[static_cast<std::size_t>(j)] * p;
162 }
163 T p = one;
164 for (int m = 2; m <= i; ++m) p *= T(num_traits<T>::from_int(NN - m) / T(one + phi_u1(NN - m)));
165 const T Nim2 = num_traits<T>::from_int(NN - i - 2); // MATLAB Nim2 = NN-1-i-1
166 if (alpha[static_cast<std::size_t>(i) - 1] == zero)
167 throw NumericError(ErrorKind{"Degenerate"}, "pfqn_sib: zero alpha coefficient in the level correction");
168 const T corr =
169 T(one + T(alpha[static_cast<std::size_t>(i)] / alpha[static_cast<std::size_t>(i) - 1]) *
170 Nim2 / T(one + Nim2 * alpha[0]));
171 b += alpha[static_cast<std::size_t>(i)] * p * corr;
172 return b;
173 };
174
175 const int NN = N - 1;
176 T phi_lo = T(num_traits<T>::from_int(N - 1) * S2);
177 const T T1s2 = T(num_traits<T>::from_int(N - 1) * rho_u - one);
178 T phi_hi = T(num_traits<T>::from_rational(1, 2) *
179 T(T1s2 + sqrt(T(T1s2 * T1s2 + num_traits<T>::from_int(4 * (N - 1)) * S2))));
180
181 if (N >= 3) {
182 const T eta = T(num_traits<T>::from_int(N - 2) / num_traits<T>::from_int(N - 1));
183 const T T1u = T(num_traits<T>::from_int(N - 2) * rho_u - one);
184 const T su = sigma(NN, lv - 1);
185 T d = T(T1u * T1u + num_traits<T>::from_int(4 * (N - 2)) * T(S2 - su));
186 if (d < zero) d = zero;
187 const T phi_u_n = T(T(num_traits<T>::from_rational(1, 2) / eta) * T(T1u + sqrt(d)));
188 if (phi_u_n < phi_hi) phi_hi = phi_u_n;
189
190 const T T1l = T(num_traits<T>::from_int(N - 2) * S2 - one);
191 const T bl = betaL(NN, lv - 1);
192 T dl = T(T1l * T1l + num_traits<T>::from_int(4 * (N - 2)) *
193 T(S2 + num_traits<T>::from_int(N - 2) * bl));
194 if (dl < zero) dl = zero;
195 // eq (3.23) divides by 2*eta, the SAME constant eq (3.22) applies as
196 // 0.5/eta above; the Greek eta on the scan was read as the level index n
197 const T phi_l_n = T(T(T1l + sqrt(dl)) / T(num_traits<T>::from_int(2) * eta));
198 if (phi_l_n > phi_lo) phi_lo = phi_l_n;
199 }
200
201 if (phi_lo < zero) phi_lo = zero;
202 if (phi_hi < phi_lo) phi_hi = phi_lo;
203
204 SibBounds<T> r;
205 // cycle time (residence plus think time) and throughput
206 r.Wlo = T(Lsum * T(one + phi_lo));
207 r.Whi = T(Lsum * T(one + phi_hi));
208 r.Xlo = T(num_traits<T>::from_int(N) / r.Whi);
209 r.Xhi = T(num_traits<T>::from_int(N) / r.Wlo);
210 return r;
211}
212
213template <class T>
214SibBounds<T> pfqn_sib(const std::vector<T>& L, int N, const T& Z) {
215 return pfqn_sib(L, N, Z, 3);
216}
217
218} // namespace pfqn
219} // namespace line
220
221#endif // LINE_API_PFQN_PFQN_SIB_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.
SibBounds< T > pfqn_sib(const std::vector< T > &L, int N, const T &Z, int level)
Successively Improving Bounds (Srinivasan 1985/1987) on the cycle time and throughput of a single-cla...
Definition pfqn_sib.h:86
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
T num_pow_int(const T &base, unsigned e)
Integer power, valid in any field (no transcendental requirement).
Definition number.h:204
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
Return value of pfqn_sib, mirroring [Xlo, Xhi, Wlo, Whi].
Definition pfqn_sib.h:69