LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_match_fcfm.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_MATCH_FCFM_H
6#define LINE_API_QSYS_QSYS_MATCH_FCFM_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Exact analysis of the first-come-first-matched (FCFM) stochastic matching
12 * model (Comte 2022).
13 *
14 * Port of matlab/src/api/qsys/qsys_match_fcfm.m (twin of the JAR
15 * `Qsys_match_fcfm` and python `qsys_match_fcfm`). Items of class i = 0..R-1
16 * arrive as independent Poisson processes of rate lambda[i]. G is the
17 * symmetric 0/1 compatibility graph between classes, with a zero diagonal. An
18 * arriving class-i item is matched at once with the OLDEST waiting item whose
19 * class is a neighbour of i in G, and both leave. When no compatible item is
20 * waiting, the arrival joins the queue, or is lost when the queue already
21 * holds K items (K = Inf: no limit).
22 *
23 * The ordered queue (c_1..c_m), oldest first, has the order-independent
24 * product form pi(c_1..c_m) = (1/Z) prod_p lambda[c_p] / mu(V_p), with
25 * mu(A) = lambda(N(A)) and V_p = {c_1..c_p}. With K = Inf this requires
26 * NCond: lambda(A) < lambda(N(A)) for every non-empty independent set A; when
27 * it fails the result is flagged unstable (Q = W = Inf, X = NaN, Z = Inf).
28 *
29 * References: Comte (2022), Stochastic non-bipartite matching models and
30 * order-independent loss queues, Stochastic Models 38(1):1-36. Berezner,
31 * Krzesinski (1996), Order independent loss queues, Queueing Systems
32 * 23:331-335.
33 */
34
35#include <cmath>
36#include <limits>
37#include <string>
38#include <vector>
39
40#include "line/util/error.h"
41
42namespace line {
43namespace qsys {
44
45/** Return value of qsys_match_fcfm. Vectors have R entries, matrices R x R. */
47 std::vector<double> Q; ///< mean number of waiting items per class
48 std::vector<double> W; ///< mean wait per accepted arrival (0 if none accepted)
49 std::vector<double> X; ///< rate at which class-i items leave matched
50 std::vector<double> omega; ///< P(a class-i arrival finds no compatible item)
51 std::vector<double> pblock; ///< P(a class-i arrival is lost), 0 when K = Inf
52 /** pmatch[i][j]: P(a class-i arrival waits and is later matched by a class j). */
53 std::vector<std::vector<double>> pmatch;
54 /** rmatch[i][j]: rate of matches of the edge {i,j} (symmetric). */
55 std::vector<std::vector<double>> rmatch;
56 double P0 = 0.0; ///< probability of the empty queue
57 double Z = 0.0; ///< normalizing constant
58 std::vector<double> piSet; ///< P(set of waiting classes = A), indexed by bitmask A
59 bool stable = true; ///< false when K = Inf and NCond fails
60};
61
62namespace detail {
63inline int match_ctz(unsigned x) {
64 int n = 0;
65 while (!(x & 1u)) {
66 x >>= 1;
67 ++n;
68 }
69 return n;
70}
71inline int match_popcount(unsigned x) {
72 int n = 0;
73 for (; x; x &= x - 1) ++n;
74 return n;
75}
76} // namespace detail
77
78/**
79 * @brief Exact FCFM matching model.
80 *
81 * @param lam (R) arrival rates (finite, >= 0)
82 * @param Gm R x R symmetric 0/1 compatibility graph, zero diagonal
83 * @param Kcap capacity on the total number of waiting items (+Inf: none)
84 */
85inline MatchFcfmResult qsys_match_fcfm(const std::vector<double>& lam,
86 const std::vector<std::vector<double>>& Gm,
87 double Kcap = std::numeric_limits<double>::infinity()) {
88 const ErrorKind arg{"Argument"};
89 const std::size_t R = lam.size();
90 for (std::size_t i = 0; i < R; ++i)
91 if (!(lam[i] >= 0) || std::isinf(lam[i]))
92 throw InputError(arg, "Arrival rates must be finite and non-negative.");
93 bool shape = Gm.size() == R;
94 for (std::size_t i = 0; shape && i < R; ++i) shape = Gm[i].size() == R;
95 if (!shape)
96 throw InputError(arg, "The compatibility graph must be a " + std::to_string(R) + "x" +
97 std::to_string(R) + " matrix.");
98 std::vector<std::vector<bool>> G(R, std::vector<bool>(R, false));
99 for (std::size_t i = 0; i < R; ++i)
100 for (std::size_t j = 0; j < R; ++j) G[i][j] = Gm[i][j] != 0;
101 for (std::size_t i = 0; i < R; ++i) {
102 if (G[i][i]) throw InputError(arg, "The compatibility graph must be symmetric with a zero diagonal.");
103 for (std::size_t j = 0; j < R; ++j)
104 if (G[i][j] != G[j][i])
105 throw InputError(arg, "The compatibility graph must be symmetric with a zero diagonal.");
106 }
107 if (R > 24)
108 throw InputError(arg, "qsys_match_fcfm enumerates the 2^R class sets; R > 24 classes is not supported.");
109 const bool infK = std::isinf(Kcap) || std::isnan(Kcap) || Kcap >= 2147483647.0;
110 if (!infK && !(Kcap >= 0 && Kcap == std::floor(Kcap)))
111 throw InputError(arg, "The capacity K must be a non-negative integer or Inf.");
112
113 const unsigned nS = 1u << R;
114 std::vector<unsigned> nbmask(R, 0);
115 for (std::size_t i = 0; i < R; ++i)
116 for (std::size_t j = 0; j < R; ++j)
117 if (G[i][j]) nbmask[i] |= 1u << j;
118 std::vector<bool> indep(nS, false);
119 std::vector<double> muA(nS, 0.0), lamA(nS, 0.0);
120 indep[0] = true;
121 for (unsigned A = 1; A < nS; ++A) {
122 const int low = detail::match_ctz(A);
123 const unsigned Ar = A ^ (1u << low);
124 indep[A] = indep[Ar] && (Ar & nbmask[low]) == 0;
125 if (indep[A]) {
126 unsigned nbA = 0;
127 for (std::size_t i = 0; i < R; ++i)
128 if ((A >> i) & 1u) {
129 nbA |= nbmask[i];
130 lamA[A] += lam[i];
131 }
132 for (std::size_t j = 0; j < R; ++j)
133 if ((nbA >> j) & 1u) muA[A] += lam[j];
134 }
135 }
136 std::vector<unsigned> sets; // subsets precede supersets
137 for (unsigned A = 0; A < nS; ++A)
138 if (indep[A]) sets.push_back(A);
139
140 MatchFcfmResult res;
141 const double inf = std::numeric_limits<double>::infinity();
142 const double nan = std::numeric_limits<double>::quiet_NaN();
143 std::vector<double> L(R, 0.0), pblock(R, 0.0), piSet(nS, 0.0);
144 double Z = 0.0;
145 std::vector<double> Gs, F;
146 std::vector<std::vector<double>> Gk, Fk;
147 const int K = infK ? -1 : static_cast<int>(Kcap);
148
149 if (infK) {
150 std::vector<double> den(nS, 0.0);
151 for (unsigned A = 0; A < nS; ++A) den[A] = muA[A] - lamA[A];
152 for (unsigned A = 1; A < nS; ++A) {
153 if (indep[A] && den[A] <= 0) {
154 res.stable = false;
155 res.Q.assign(R, inf);
156 res.W.assign(R, inf);
157 res.X.assign(R, nan);
158 res.omega.assign(R, nan);
159 res.pblock.assign(R, 0.0);
160 res.pmatch.assign(R, std::vector<double>(R, nan));
161 res.rmatch.assign(R, std::vector<double>(R, nan));
162 res.P0 = 0.0;
163 res.Z = inf;
164 res.piSet.assign(nS, nan);
165 return res;
166 }
167 }
168 Gs.assign(nS, 0.0);
169 std::vector<std::vector<double>> H(nS, std::vector<double>(R, 0.0));
170 Gs[0] = 1.0;
171 for (std::size_t s = 1; s < sets.size(); ++s) {
172 const unsigned A = sets[s];
173 double g = 0.0;
174 std::vector<double> h(R, 0.0);
175 for (std::size_t c = 0; c < R; ++c) {
176 if (!((A >> c) & 1u)) continue;
177 const unsigned Ac = A ^ (1u << c);
178 g += lam[c] * Gs[Ac];
179 for (std::size_t i = 0; i < R; ++i) h[i] += lam[c] * H[Ac][i];
180 }
181 Gs[A] = g / den[A];
182 for (std::size_t i = 0; i < R; ++i)
183 if ((A >> i) & 1u) h[i] += lam[i] * (Gs[A] + Gs[A ^ (1u << i)]);
184 for (std::size_t i = 0; i < R; ++i) H[A][i] = h[i] / den[A];
185 }
186 F.assign(nS, 0.0);
187 for (std::size_t s = sets.size(); s-- > 0;) {
188 const unsigned B = sets[s];
189 double f = 1.0;
190 for (std::size_t c = 0; c < R; ++c) {
191 if ((B >> c) & 1u) continue;
192 const unsigned Bc = B | (1u << c);
193 if (indep[Bc]) f += lam[c] / muA[Bc] * F[Bc];
194 }
195 if (B > 0) f /= (1.0 - lamA[B] / muA[B]);
196 F[B] = f;
197 }
198 for (unsigned A = 0; A < nS; ++A) Z += Gs[A];
199 for (unsigned A = 0; A < nS; ++A) {
200 for (std::size_t i = 0; i < R; ++i) L[i] += H[A][i];
201 piSet[A] = Gs[A] / Z;
202 }
203 for (std::size_t i = 0; i < R; ++i) L[i] /= Z;
204 } else {
205 const std::size_t K1 = static_cast<std::size_t>(K) + 1;
206 Gk.assign(nS, std::vector<double>(K1, 0.0));
207 std::vector<std::vector<std::vector<double>>> Hm(
208 nS, std::vector<std::vector<double>>(K1, std::vector<double>(R, 0.0)));
209 Gk[0][0] = 1.0;
210 for (int m = 1; m <= K; ++m) {
211 for (std::size_t s = 1; s < sets.size(); ++s) {
212 const unsigned A = sets[s];
213 if (muA[A] <= 0) continue;
214 double g = lamA[A] * Gk[A][m - 1];
215 std::vector<double> h(R, 0.0);
216 for (std::size_t i = 0; i < R; ++i) h[i] = lamA[A] * Hm[A][m - 1][i];
217 for (std::size_t c = 0; c < R; ++c) {
218 if (!((A >> c) & 1u)) continue;
219 const unsigned Ac = A ^ (1u << c);
220 g += lam[c] * Gk[Ac][m - 1];
221 for (std::size_t i = 0; i < R; ++i) h[i] += lam[c] * Hm[Ac][m - 1][i];
222 h[c] += lam[c] * (Gk[A][m - 1] + Gk[Ac][m - 1]);
223 }
224 Gk[A][m] = g / muA[A];
225 for (std::size_t i = 0; i < R; ++i) Hm[A][m][i] = h[i] / muA[A];
226 }
227 }
228 for (std::size_t s = 1; s < sets.size(); ++s) {
229 const unsigned A = sets[s];
230 if (muA[A] <= 0 && K >= detail::match_popcount(A))
231 throw InputError(arg,
232 "A class with no compatible neighbour is never matched; remove it or give it a neighbour.");
233 }
234 for (unsigned A = 0; A < nS; ++A)
235 for (int m = 0; m <= K; ++m) {
236 Z += Gk[A][m];
237 piSet[A] += Gk[A][m];
238 for (std::size_t i = 0; i < R; ++i) L[i] += Hm[A][m][i];
239 }
240 for (unsigned A = 0; A < nS; ++A) piSet[A] /= Z;
241 for (std::size_t i = 0; i < R; ++i) L[i] /= Z;
242 Fk.assign(nS, std::vector<double>(K1, 0.0));
243 for (unsigned B = 0; B < nS; ++B) Fk[B][0] = 1.0;
244 for (int k = 1; k <= K; ++k) {
245 for (std::size_t s = sets.size(); s-- > 0;) {
246 const unsigned B = sets[s];
247 double f = 1.0;
248 if (B > 0) f += lamA[B] / muA[B] * Fk[B][k - 1];
249 for (std::size_t c = 0; c < R; ++c) {
250 if ((B >> c) & 1u) continue;
251 const unsigned Bc = B | (1u << c);
252 if (indep[Bc]) f += lam[c] / muA[Bc] * Fk[Bc][k - 1];
253 }
254 Fk[B][k] = f;
255 }
256 }
257 for (std::size_t i = 0; i < R; ++i) {
258 for (unsigned A : sets)
259 if ((A & nbmask[i]) == 0) pblock[i] += Gk[A][K];
260 pblock[i] /= Z;
261 }
262 }
263
264 std::vector<double> omega(R, 0.0);
265 std::vector<std::vector<double>> pmatch(R, std::vector<double>(R, 0.0));
266 for (std::size_t i = 0; i < R; ++i) {
267 for (unsigned A : sets) {
268 if ((A & nbmask[i]) != 0) continue;
269 if (infK) {
270 omega[i] += Gs[A];
271 } else {
272 for (int m = 0; m <= K; ++m) omega[i] += Gk[A][m];
273 }
274 for (std::size_t j = 0; j < R; ++j) {
275 if (!G[i][j]) continue;
276 const unsigned Aj = A | (1u << j);
277 if (!indep[Aj]) continue;
278 if (infK) {
279 pmatch[i][j] += Gs[A] * lam[j] / muA[Aj] * F[Aj];
280 } else {
281 for (int m = 0; m < K; ++m)
282 pmatch[i][j] += Gk[A][m] * lam[j] / muA[Aj] * Fk[Aj][K - m - 1];
283 }
284 }
285 }
286 }
287 for (std::size_t i = 0; i < R; ++i) {
288 omega[i] /= Z;
289 for (std::size_t j = 0; j < R; ++j) pmatch[i][j] /= Z;
290 }
291 res.rmatch.assign(R, std::vector<double>(R, 0.0));
292 res.X.assign(R, 0.0);
293 for (std::size_t i = 0; i < R; ++i)
294 for (std::size_t j = 0; j < R; ++j) {
295 const double fij = lam[i] * pmatch[i][j];
296 const double fji = lam[j] * pmatch[j][i];
297 res.rmatch[i][j] = fij + fji;
298 res.X[i] += fij + fji;
299 }
300 res.Q = L;
301 res.W.assign(R, 0.0);
302 for (std::size_t i = 0; i < R; ++i) {
303 const double acc = lam[i] * (1.0 - pblock[i]);
304 res.W[i] = acc > 0 ? L[i] / acc : 0.0;
305 }
306 res.omega = omega;
307 res.pblock = pblock;
308 res.pmatch = pmatch;
309 res.P0 = 1.0 / Z;
310 res.Z = Z;
311 res.piSet = piSet;
312 return res;
313}
314
315} // namespace qsys
316} // namespace line
317
318#endif // LINE_API_QSYS_QSYS_MATCH_FCFM_H
InputError(const std::string &what)
Definition error.h:105
The exception types the port throws.
MatchFcfmResult qsys_match_fcfm(const std::vector< double > &lam, const std::vector< std::vector< double > > &Gm, double Kcap=std::numeric_limits< double >::infinity())
Exact FCFM matching model.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
Return value of qsys_match_fcfm.
std::vector< double > X
rate at which class-i items leave matched
std::vector< double > pblock
P(a class-i arrival is lost), 0 when K = Inf.
bool stable
false when K = Inf and NCond fails
std::vector< double > omega
P(a class-i arrival finds no compatible item).
double P0
probability of the empty queue
std::vector< double > piSet
P(set of waiting classes = A), indexed by bitmask A.
std::vector< std::vector< double > > rmatch
rmatch[i][j]: rate of matches of the edge {i,j} (symmetric).
std::vector< double > Q
mean number of waiting items per class
std::vector< double > W
mean wait per accepted arrival (0 if none accepted)
double Z
normalizing constant
std::vector< std::vector< double > > pmatch
pmatch[i][j]: P(a class-i arrival waits and is later matched by a class j).