LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_fcfs_alis.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_QSYS_QSYS_FCFS_ALIS_H
6#define LINE_API_QSYS_QSYS_FCFS_ALIS_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Exact steady-state means of an isolated FCFS parallel-server system with
12 * job-server compatibilities under Assign Longest Idle Server (ALIS).
13 *
14 * Templated port of matlab/src/api/qsys/qsys_fcfs_alis.m. C job classes arrive
15 * as Poisson streams at rates LAMBDA, M servers serve at rates MU that depend on
16 * the server only (exponential, noncollaborative: one server per job), a
17 * class-c job may only be served by the servers m with COMPAT(c,m) true, a
18 * server that frees up takes the OLDEST waiting job it is compatible with, and
19 * an arriving job that finds compatible idle servers starts on the one that has
20 * been idle longest.
21 *
22 * PRODUCT FORM. With the partially aggregated state of Adan and Weiss
23 * (Gardner, Righter 2020, Thm 5.2): the idle servers s in the order they became
24 * idle, the busy servers b in the arrival order of the jobs they serve, and
25 * n_j the number of waiting jobs between the j-th and (j+1)-th job in service,
26 *
27 * pi(s, b, n) = G * prod_j alpha_j^{n_j} / mu(b_1..b_j) * prod_j 1/lambda(C(s_1..s_j)),
28 *
29 * alpha_j = lambda(R(b_1..b_j)) / mu(b_1..b_j), where R(B) are the classes
30 * whose compatible servers all lie in B and C(S) the classes compatible with
31 * some server of S. Given (s, b) the n_j are independent geometric, and each
32 * job of segment j is class c in R(b_1..b_j) with probability
33 * lambda_c / lambda(R(b_1..b_j)), which gives the mean waiting counts Qw.
34 *
35 * PER-CLASS MEANS. The classes of the jobs in service are not part of that
36 * state, so the in-service means come from the matching rates X(c,m), the rate
37 * at which class-c jobs start service at server m: a completion at b_j hands
38 * the server to the first waiting job it is compatible with (scanning the
39 * segments i >= j in order, segment i holds none with probability
40 * (1 - alpha_i) / (1 - alpha_i q_i), q_i the probability that one of its jobs
41 * is incompatible with b_j), and an arrival of class c that finds compatible
42 * idle servers starts at the first of them in s. Then E[in service, class c] =
43 * sum_m X(c,m) / mu_m and Q = Qw plus that.
44 *
45 * The system is exact only IN ISOLATION: the noncollaborative ALIS pool has no
46 * balance function independent of lambda, so it does not compose into a
47 * product-form network. It is the textbook ALIS, which orders individual
48 * SERVERS by idle time, NOT HeteroSchedPolicy::ALIS, so no solver route uses it.
49 *
50 * Cost: two depth-first passes, one over the ordered busy sets and one over the
51 * ordered idle sets (about e * M! visits each), joined by complementary bit
52 * masks; at most 9 servers with traffic.
53 *
54 * References: Adan, Weiss (2014), Stochastic Systems 4(1):250-299; Gardner,
55 * Righter (2020), Queueing Systems 96:3-51, Sec. 3.2 and 5.1.
56 *
57 * ARITHMETIC. Field arithmetic, exact under Rational. An UNSTABLE pool reports
58 * Q = Qw = Inf and X, U, P0 = NaN, which exist only in floating point: under
59 * exact arithmetic that case raises instead.
60 */
61
62#include <cmath>
63#include <cstddef>
64#include <limits>
65#include <string>
66#include <vector>
67
68#include "line/num/number.h"
69#include "line/util/error.h"
70#include "line/util/matrix.h"
71
72namespace line {
73namespace qsys {
74
75/** Return value of qsys_fcfs_alis, mirroring [Q, Qw, X, U, P0, stable]. */
76template <class T>
78 std::vector<T> Q; ///< (C) mean number of class-c jobs in the system
79 std::vector<T> Qw; ///< (C) mean number of class-c jobs waiting (not in service)
80 Matrix<T> X; ///< (C x M) matching rates: class-c service starts per unit time at server m
81 std::vector<T> U; ///< (M) probability that server m is busy
82 T P0; ///< probability that the system is empty
83 bool stable = true; ///< false when lambda(A) >= mu(S(A)) for some class subset A
84};
85
86namespace detail {
87
88/** The two depth-first passes of qsys_fcfs_alis over the reduced (traffic-only) system. */
89template <class T>
90struct FcfsAlisDfs {
91 struct Seg {
92 std::vector<std::size_t> R; // classes needing only servers in b_1..b_j
93 T lr; // lambda(R)
94 T a; // lambda(R) / mu(b_1..b_j)
95 };
96
97 std::size_t Cr = 0, Mr = 0, nmask = 0;
98 std::vector<T> lam, mus;
99 std::vector<std::vector<bool> > A; // Cr x Mr
100 std::vector<std::size_t> smask; // class c's server set as a bit mask
101 T zero, one, thr;
102 bool stable = true;
103
104 std::vector<T> WB; // nmask: sum over orderings of the busy weight
105 std::vector<std::vector<T> > WBq; // nmask x Cr: ... times the mean waiting count
106 std::vector<std::vector<T> > RBq; // nmask x Cr*Mr: ... times the queue-start rate
107 std::vector<T> WI; // nmask: sum over orderings of the idle weight
108 std::vector<std::vector<T> > RIa; // nmask x Cr*Mr: ... times the arrival-start rate
109
110 T mu_of(std::size_t mask) const {
111 T s = zero;
112 for (std::size_t m = 0; m < Mr; ++m)
113 if (mask & (std::size_t(1) << m)) s += mus[m];
114 return s;
115 }
116
117 void busy(const std::vector<std::size_t>& b, std::size_t bmask, const T& wb,
118 const std::vector<Seg>& seg) {
119 const std::size_t l = b.size();
120 if (l > 0) {
121 WB[bmask] += wb;
122 std::vector<T> qv(Cr, zero);
123 for (std::size_t j = 0; j < l; ++j) {
124 if (!(seg[j].lr > zero)) continue;
125 for (std::size_t c : seg[j].R)
126 qv[c] += T(lam[c] / seg[j].lr * seg[j].a / T(one - seg[j].a));
127 }
128 for (std::size_t c = 0; c < Cr; ++c) WBq[bmask][c] += T(wb * qv[c]);
129 std::vector<T> rv(Cr * Mr, zero);
130 for (std::size_t j = 0; j < l; ++j) {
131 const std::size_t m = b[j];
132 T surv = one;
133 for (std::size_t i = j; i < l; ++i) {
134 if (!(seg[i].lr > zero)) continue;
135 std::vector<std::size_t> comp;
136 for (std::size_t c : seg[i].R)
137 if (A[c][m]) comp.push_back(c);
138 if (comp.empty()) continue;
139 T lc = zero;
140 for (std::size_t c : comp) lc += lam[c];
141 const T q = T(one - lc / seg[i].lr);
142 const T pnone = T(T(one - seg[i].a) / T(one - seg[i].a * q));
143 for (std::size_t c : comp)
144 rv[c + Cr * m] += T(mus[m] * surv * T(one - pnone) * lam[c] / lc);
145 surv = T(surv * pnone);
146 }
147 }
148 for (std::size_t k = 0; k < Cr * Mr; ++k) RBq[bmask][k] += T(wb * rv[k]);
149 }
150 for (std::size_t m = 0; m < Mr; ++m) {
151 const std::size_t bit = std::size_t(1) << m;
152 if (bmask & bit) continue;
153 const std::size_t nm = bmask + bit;
154 Seg s;
155 s.lr = zero;
156 for (std::size_t c = 0; c < Cr; ++c)
157 if ((smask[c] & nm) == smask[c]) {
158 s.R.push_back(c);
159 s.lr += lam[c];
160 }
161 const T muj = mu_of(nm);
162 s.a = T(s.lr / muj);
163 if (s.a >= thr) {
164 stable = false;
165 return;
166 }
167 std::vector<Seg> sg = seg;
168 sg.push_back(s);
169 std::vector<std::size_t> bb = b;
170 bb.push_back(m);
171 busy(bb, nm, T(wb / T(muj * T(one - s.a))), sg);
172 if (!stable) return;
173 }
174 }
175
176 void idle(const std::vector<std::size_t>& s, std::size_t smk, const T& wi) {
177 if (!s.empty()) {
178 WI[smk] += wi;
179 for (std::size_t c = 0; c < Cr; ++c)
180 for (std::size_t k = 0; k < s.size(); ++k)
181 if (A[c][s[k]]) {
182 RIa[smk][c + Cr * s[k]] += T(wi * lam[c]);
183 break;
184 }
185 }
186 for (std::size_t m = 0; m < Mr; ++m) {
187 const std::size_t bit = std::size_t(1) << m;
188 if (smk & bit) continue;
189 const std::size_t nm = smk + bit;
190 T cover = zero;
191 for (std::size_t c = 0; c < Cr; ++c)
192 if (smask[c] & nm) cover += lam[c];
193 std::vector<std::size_t> ss = s;
194 ss.push_back(m);
195 idle(ss, nm, T(wi / cover));
196 }
197 }
198};
199
200} // namespace detail
201
202/**
203 * @brief Exact means of the isolated FCFS-ALIS pool.
204 *
205 * @param lambda (C) arrival rates (>= 0)
206 * @param mu (M) server rates (> 0)
207 * @param compat (C x M) compatibility matrix (nonzero = compatible)
208 *
209 * A class with lambda_c = 0 and a server no class with traffic can use are left
210 * out (the server stays idle).
211 */
212template <class T>
213FcfsAlisResult<T> qsys_fcfs_alis(const std::vector<T>& lambda, const std::vector<T>& mu,
214 const std::vector<std::vector<bool> >& compat) {
215 const std::size_t C = lambda.size(), M = mu.size();
216 const T zero = num_traits<T>::from_int(0);
217 const T one = num_traits<T>::from_int(1);
218 bool shape = compat.size() == C;
219 for (std::size_t c = 0; shape && c < C; ++c) shape = compat[c].size() == M;
220 if (!shape)
221 throw InputError(ErrorKind{"Argument"}, "qsys_fcfs_alis: compat must be " + std::to_string(C) +
222 " x " + std::to_string(M) + " (classes x servers).");
223 for (const T& l : lambda)
224 if (!std::isfinite(num_traits<T>::to_double(l)) || l < zero)
225 throw InputError(ErrorKind{"Argument"}, "qsys_fcfs_alis: lambda must be finite and nonnegative.");
226 for (const T& m : mu)
227 if (!std::isfinite(num_traits<T>::to_double(m)) || !(m > zero))
228 throw InputError(ErrorKind{"Argument"}, "qsys_fcfs_alis: mu must be finite and positive.");
229
231 out.Q.assign(C, zero);
232 out.Qw.assign(C, zero);
233 out.X = Matrix<T>(C, M, zero);
234 out.U.assign(M, zero);
235 out.P0 = one;
236 out.stable = true;
237
238 std::vector<std::size_t> cls, srv;
239 for (std::size_t c = 0; c < C; ++c)
240 if (lambda[c] > zero) cls.push_back(c);
241 if (cls.empty()) return out;
242 for (std::size_t c : cls) {
243 bool any = false;
244 for (std::size_t m = 0; m < M; ++m) any = any || compat[c][m];
245 if (!any)
246 throw InputError(ErrorKind{"Argument"},
247 "qsys_fcfs_alis: every class with traffic needs at least one compatible server.");
248 }
249 for (std::size_t m = 0; m < M; ++m) {
250 bool any = false;
251 for (std::size_t c : cls) any = any || compat[c][m];
252 if (any) srv.push_back(m);
253 }
254 if (srv.size() > 9)
255 throw InputError(ErrorKind{"Argument"},
256 "qsys_fcfs_alis: at most 9 servers with traffic are supported (got " +
257 std::to_string(srv.size()) + "): the cost grows as M!.");
258
259 detail::FcfsAlisDfs<T> d;
260 d.Cr = cls.size();
261 d.Mr = srv.size();
262 d.nmask = std::size_t(1) << d.Mr;
263 d.zero = zero;
264 d.one = one;
265 d.thr = T(one - num_traits<T>::from_double(1e-14));
266 for (std::size_t c : cls) d.lam.push_back(lambda[c]);
267 for (std::size_t m : srv) d.mus.push_back(mu[m]);
268 d.A.assign(d.Cr, std::vector<bool>(d.Mr, false));
269 d.smask.assign(d.Cr, 0);
270 for (std::size_t c = 0; c < d.Cr; ++c)
271 for (std::size_t m = 0; m < d.Mr; ++m)
272 if (compat[cls[c]][srv[m]]) {
273 d.A[c][m] = true;
274 d.smask[c] |= std::size_t(1) << m;
275 }
276 const std::size_t CM = d.Cr * d.Mr;
277
278 // ---- busy orderings ----
279 d.WB.assign(d.nmask, zero);
280 d.WBq.assign(d.nmask, std::vector<T>(d.Cr, zero));
281 d.RBq.assign(d.nmask, std::vector<T>(CM, zero));
282 d.WB[0] = one;
283 d.busy(std::vector<std::size_t>(), 0, one, std::vector<typename detail::FcfsAlisDfs<T>::Seg>());
284 if (!d.stable) {
285 out.stable = false;
286 if constexpr (!num_traits<T>::has_transcendental) {
287 throw NumericError(ErrorKind{"Instability"},
288 "qsys_fcfs_alis: the pool is unstable (lambda(A) >= mu(S(A)) for some class "
289 "subset A) and its mean queue lengths are infinite, which exact arithmetic "
290 "cannot represent");
291 } else {
292 const T inf = num_traits<T>::from_double(std::numeric_limits<double>::infinity());
293 const T nan = num_traits<T>::from_double(std::numeric_limits<double>::quiet_NaN());
294 for (std::size_t c = 0; c < C; ++c) {
295 out.Q[c] = lambda[c] > zero ? inf : zero;
296 out.Qw[c] = out.Q[c];
297 }
298 out.X = Matrix<T>(C, M, nan);
299 out.U.assign(M, nan);
300 out.P0 = nan;
301 return out;
302 }
303 }
304
305 // ---- idle orderings ----
306 d.WI.assign(d.nmask, zero);
307 d.RIa.assign(d.nmask, std::vector<T>(CM, zero));
308 d.WI[0] = one;
309 d.idle(std::vector<std::size_t>(), 0, one);
310
311 // ---- join the complementary halves ----
312 const std::size_t full = d.nmask - 1;
313 T W = zero;
314 std::vector<T> Wq(d.Cr, zero), Rm(CM, zero);
315 for (std::size_t bm = 0; bm <= full; ++bm) {
316 const std::size_t im = full - bm;
317 if (d.WB[bm] == zero || d.WI[im] == zero) continue;
318 W += T(d.WB[bm] * d.WI[im]);
319 for (std::size_t c = 0; c < d.Cr; ++c) Wq[c] += T(d.WBq[bm][c] * d.WI[im]);
320 for (std::size_t k = 0; k < CM; ++k)
321 Rm[k] += T(d.RBq[bm][k] * d.WI[im] + d.WB[bm] * d.RIa[im][k]);
322 }
323 for (std::size_t c = 0; c < d.Cr; ++c) {
324 const T qw = T(Wq[c] / W);
325 T insvc = zero;
326 for (std::size_t m = 0; m < d.Mr; ++m) {
327 const T x = T(Rm[c + d.Cr * m] / W);
328 out.X(cls[c], srv[m]) = x;
329 insvc += T(x / d.mus[m]);
330 }
331 out.Qw[cls[c]] = qw;
332 out.Q[cls[c]] = T(qw + insvc);
333 }
334 for (std::size_t m = 0; m < d.Mr; ++m) {
335 T s = zero;
336 for (std::size_t c = 0; c < d.Cr; ++c) s += out.X(cls[c], srv[m]);
337 out.U[srv[m]] = T(s / d.mus[m]);
338 }
339 out.P0 = T(d.WI[full] / W); // every server idle (busy part is the empty ordering)
340 return out;
341}
342
343} // namespace qsys
344} // namespace line
345
346#endif // LINE_API_QSYS_QSYS_FCFS_ALIS_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.
Dense matrix and non-owning view.
FcfsAlisResult< T > qsys_fcfs_alis(const std::vector< T > &lambda, const std::vector< T > &mu, const std::vector< std::vector< bool > > &compat)
Exact means of the isolated FCFS-ALIS pool.
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
Return value of qsys_fcfs_alis, mirroring [Q, Qw, X, U, P0, stable].
Matrix< T > X
(C x M) matching rates: class-c service starts per unit time at server m
T P0
probability that the system is empty
std::vector< T > U
(M) probability that server m is busy
std::vector< T > Q
(C) mean number of class-c jobs in the system
bool stable
false when lambda(A) >= mu(S(A)) for some class subset A
std::vector< T > Qw
(C) mean number of class-c jobs waiting (not in service)