LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
matching_station.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_LANG_QN_MATCHING_STATION_H
6#define LINE_LANG_QN_MATCHING_STATION_H
7
8/**
9 * State-space helpers for a Matching (SchedStrategy::FCFM) station, the FCFM
10 * stochastic matching model of Comte (2022). Port of `MatchingStation.java`
11 * (MATLAB `State.matchingSpace`, `State.matchingPhases`,
12 * `State.afterEventStationMatching`, `solver_ctmc_matching_tput`).
13 *
14 * THE LOCAL STATE is the ordered word of waiting jobs, oldest first, as
15 * (class, patience phase) pairs (both 1-based), zero-padded to the capacity W,
16 * followed by the node's routing variables: [c_1 k_1 ... c_W k_W | vars]. The
17 * waiting classes always form an independent set of the compatibility graph
18 * (`Station::compatibility`).
19 *
20 * - ARV (passive): a class-r arrival is matched with the OLDEST waiting job of
21 * a class s with G(r,s) = 1, which leaves the word; the arrival never enters.
22 * Both leave in the same transition, so no vanishing state arises. With no
23 * compatible job waiting the arrival is appended in a patience phase drawn
24 * from the initial vector of its patience law, or lost when the word holds W
25 * jobs.
26 * - PHASE: a waiting class-r job moves between the transient phases of its
27 * phase-type patience (D0 off-diagonal).
28 * - RENEGE: a waiting class-r job in phase k abandons at rate sum(D1(k,:)).
29 * - DEP: none. Matched jobs leave inside the ARV transition; their throughput
30 * is recovered from the stationary distribution by `matching_tput`.
31 *
32 * The successor rows are returned through plain vectors so this header does
33 * not depend on state_events.h (state.h includes it for the marginals).
34 */
35
36#include <algorithm>
37#include <cmath>
38#include <cstddef>
39#include <limits>
40#include <string>
41#include <utility>
42#include <vector>
43
48#include "line/util/error.h"
49
50namespace line {
51namespace qn {
52namespace matching {
53
54/** True when station `ist` (1-based) is a Matching (FCFM) station. */
55template <class T>
56bool is_matching(const NetworkStruct<T>& sn, std::size_t ist) {
57 return ist >= 1 && ist <= sn.stations.size() &&
58 sn.stations[ist - 1].sched == lang::SchedStrategy::FCFM;
59}
60
61/** True when node `ind` (1-based) is a Matching (FCFM) station. */
62template <class T>
63bool is_matching_node(const NetworkStruct<T>& sn, std::size_t ind) {
64 if (ind == 0 || ind > sn.nodes.size()) return false;
65 return is_matching(sn, sn.nodes[ind - 1].station);
66}
67
68/** Compatibility graph of Matching station `ist` (1-based), R x R. */
69template <class T>
70std::vector<std::vector<bool>> compatibility(const NetworkStruct<T>& sn, std::size_t ist) {
71 const std::size_t R = sn.nclasses;
72 std::vector<std::vector<bool>> G(R, std::vector<bool>(R, false));
73 const std::vector<std::vector<bool>>& C = sn.stations[ist - 1].compatibility;
74 for (std::size_t r = 0; r < R && r < C.size(); ++r)
75 for (std::size_t s = 0; s < R && s < C[r].size(); ++s) G[r][s] = C[r][s];
76 return G;
77}
78
79/** Capacity of station `ist` (1-based), or -1 when the station is uncapped. */
80template <class T>
81long capacity(const NetworkStruct<T>& sn, std::size_t ist) {
82 const double cap = ist <= sn.cap.size() ? sn.cap[ist - 1] : std::numeric_limits<double>::infinity();
83 if (!std::isfinite(cap) || cap >= static_cast<double>(std::numeric_limits<int>::max())) return -1;
84 return static_cast<long>(cap);
85}
86
87/** Whether class `r` (0-based) reneges at station `ist` (1-based). */
88template <class T>
89bool has_patience(const NetworkStruct<T>& sn, std::size_t ist, std::size_t r) {
90 const Station<T>& st = sn.stations[ist - 1];
91 if (r >= st.impatience.size() || st.impatience[r] != lang::ImpatienceType::RENEGING) return false;
92 return r < st.patience.size() && !st.patience[r].disabled &&
94}
95
96/** The (D0, D1) representation of the class-r patience at station `ist`. */
97template <class T>
98mam::Map<T> patience_map(const NetworkStruct<T>& sn, std::size_t ist, std::size_t r) {
99 try {
100 return lang::dist_to_map(sn.stations[ist - 1].patience[r]);
101 } catch (const Error&) {
102 throw UnsupportedError(ErrorKind{"Patience"},
103 "A Matching node supports only Markovian (phase-type) patience distributions.");
104 }
105}
106
107/**
108 * Patience phases (1-based) a waiting class-r job can be in: {1} without
109 * patience, otherwise 1..K, or only the phases with positive initial
110 * probability when `initial_only` is true.
111 */
112template <class T>
113std::vector<std::size_t> phases(const NetworkStruct<T>& sn, std::size_t ist, std::size_t r,
114 bool initial_only) {
115 if (!has_patience(sn, ist, r)) return std::vector<std::size_t>(1, 1);
116 const mam::Map<T> p = patience_map(sn, ist, r);
117 const std::size_t K = p.D0.rows();
118 std::vector<std::size_t> ph;
119 if (!initial_only) {
120 for (std::size_t k = 0; k < K; ++k) ph.push_back(k + 1);
121 return ph;
122 }
123 const std::vector<T> alpha = mam::map_pie(p);
124 for (std::size_t k = 0; k < K; ++k)
125 if (num_traits<T>::to_double(alpha[k]) > 0) ph.push_back(k + 1);
126 return ph;
127}
128
129/** Initial patience phases (1-based) of a new class-r job and their probabilities. */
130template <class T>
131std::pair<std::vector<std::size_t>, std::vector<T>> entry(const NetworkStruct<T>& sn,
132 std::size_t ist, std::size_t r) {
133 std::pair<std::vector<std::size_t>, std::vector<T>> out;
134 if (!has_patience(sn, ist, r)) {
135 out.first.push_back(1);
136 out.second.push_back(num_traits<T>::from_int(1));
137 return out;
138 }
139 const mam::Map<T> p = patience_map(sn, ist, r);
140 const std::vector<T> alpha = mam::map_pie(p);
141 T tot = num_traits<T>::from_int(0);
142 for (std::size_t k = 0; k < alpha.size(); ++k)
143 if (num_traits<T>::to_double(alpha[k]) > 0) tot += alpha[k];
144 for (std::size_t k = 0; k < alpha.size(); ++k)
145 if (num_traits<T>::to_double(alpha[k]) > 0) {
146 out.first.push_back(k + 1);
147 out.second.push_back(T(alpha[k] / tot));
148 }
149 return out;
150}
151
152/**
153 * Local states of Matching station `ist` (1-based) with per-class waiting
154 * counts `n`, WITHOUT the trailing routing variables. A count vector whose
155 * support is not an independent set of G, or that exceeds the capacity, has no
156 * state. With `initial_only` only the patience phases a new job can start in
157 * are listed. An uncapped node has only the empty state (one zero pair), which
158 * is all SolverNC needs; any non-empty marginal there is refused.
159 */
160template <class T>
161std::vector<std::vector<T>> space(const NetworkStruct<T>& sn, std::size_t ist,
162 const std::vector<std::size_t>& n, bool initial_only) {
163 const std::size_t R = sn.nclasses;
164 long W = capacity(sn, ist);
165 std::size_t total = 0;
166 for (std::size_t r = 0; r < R && r < n.size(); ++r) total += n[r];
167 if (W < 0) {
168 if (total > 0)
169 throw InputError(
170 "A Matching node requires a finite capacity (setCap) for state-space generation.");
171 W = 1;
172 }
173 const std::size_t w2 = 2 * static_cast<std::size_t>(W);
174 std::vector<std::vector<T>> out;
175 if (total == 0) {
176 out.push_back(std::vector<T>(w2, num_traits<T>::from_int(0)));
177 return out;
178 }
179 if (total > static_cast<std::size_t>(W)) return out;
180 const std::vector<std::vector<bool>> G = compatibility(sn, ist);
181 std::vector<std::size_t> supp;
182 for (std::size_t r = 0; r < R; ++r)
183 if (n[r] > 0) supp.push_back(r);
184 for (std::size_t a : supp)
185 for (std::size_t b : supp)
186 if (G[a][b]) return out; // compatible jobs never wait together
187 std::vector<std::size_t> word;
188 for (std::size_t r : supp)
189 for (std::size_t j = 0; j < n[r]; ++j) word.push_back(r + 1);
190 std::vector<std::vector<std::size_t>> ph(R);
191 for (std::size_t r : supp) ph[r] = phases(sn, ist, r, initial_only);
192 // `word` is built class-major, hence sorted: next_permutation walks every
193 // distinct ordering of the multiset exactly once.
194 do {
195 std::vector<std::vector<T>> partial(1, std::vector<T>(w2, num_traits<T>::from_int(0)));
196 for (std::size_t p = 0; p < total; ++p) {
197 const std::size_t cls = word[p];
198 std::vector<std::vector<T>> next;
199 for (const std::vector<T>& base : partial)
200 for (std::size_t k : ph[cls - 1]) {
201 std::vector<T> row = base;
202 row[2 * p] = num_traits<T>::from_int(static_cast<long>(cls));
203 row[2 * p + 1] = num_traits<T>::from_int(static_cast<long>(k));
204 next.push_back(row);
205 }
206 partial.swap(next);
207 }
208 out.insert(out.end(), partial.begin(), partial.end());
209 } while (std::next_permutation(word.begin(), word.end()));
210 return out;
211}
212
213/** Per-class waiting counts of a Matching station's local state row (node `ind`, 1-based). */
214template <class T>
215std::vector<T> count_waiting(const NetworkStruct<T>& sn, std::size_t ind, const std::vector<T>& row) {
216 const std::size_t R = sn.nclasses;
217 const std::size_t V = sn.nvars_of(ind);
218 const std::size_t w2 = row.size() > V ? row.size() - V : 0;
219 std::vector<T> nir(R, num_traits<T>::from_int(0));
220 for (std::size_t c = 0; c < w2; c += 2) {
221 const long cls = static_cast<long>(num_traits<T>::to_double(row[c]) + 0.5);
222 if (cls >= 1 && static_cast<std::size_t>(cls) <= R) nir[cls - 1] += num_traits<T>::from_int(1);
223 }
224 return nir;
225}
226
227/** The (class, phase) pairs of a word, both 1-based, oldest first. */
228template <class T>
229std::vector<std::pair<std::size_t, std::size_t>> word_of(const std::vector<T>& row, std::size_t w2) {
230 std::vector<std::pair<std::size_t, std::size_t>> w;
231 for (std::size_t q = 0; q + 1 < w2 && q + 1 < row.size(); q += 2) {
232 const long cls = static_cast<long>(num_traits<T>::to_double(row[q]) + 0.5);
233 if (cls > 0)
234 w.push_back(std::make_pair(static_cast<std::size_t>(cls),
235 static_cast<std::size_t>(num_traits<T>::to_double(row[q + 1]) + 0.5)));
236 }
237 return w;
238}
239
240/** Pack a word back into a row of the width of `row`, keeping its trailing variables. */
241template <class T>
242std::vector<T> pack(const std::vector<std::pair<std::size_t, std::size_t>>& w, const std::vector<T>& row,
243 std::size_t w2) {
244 std::vector<T> out(row.size(), num_traits<T>::from_int(0));
245 for (std::size_t q = 0; q < w.size(); ++q) {
246 out[2 * q] = num_traits<T>::from_int(static_cast<long>(w[q].first));
247 out[2 * q + 1] = num_traits<T>::from_int(static_cast<long>(w[q].second));
248 }
249 for (std::size_t c = w2; c < row.size(); ++c) out[c] = row[c];
250 return out;
251}
252
253/**
254 * Successors of a Matching station (node `ind`, 1-based) after `event` on
255 * class `cls` (1-based): the rows, their rates (-1 on the passive ARV) and
256 * their probabilities.
257 */
258template <class T>
259void after_event(const NetworkStruct<T>& sn, std::size_t ind, const std::vector<T>& inspace,
260 lang::EventType event, std::size_t cls, std::vector<std::vector<T>>& rows,
261 std::vector<T>& rates, std::vector<T>& probs) {
262 const std::size_t ist = sn.nodes[ind - 1].station;
263 const long cap = capacity(sn, ist);
264 if (cap < 0)
265 throw InputError("A Matching node requires a finite capacity (setCap) for state-space generation.");
266 const T one = num_traits<T>::from_int(1);
267 const T passive = num_traits<T>::from_int(-1);
268 const std::size_t V = sn.nvars_of(ind);
269 const std::size_t w2 = inspace.size() > V ? inspace.size() - V : 0;
270 const std::size_t r = cls - 1;
271 const std::vector<std::pair<std::size_t, std::size_t>> w = word_of(inspace, w2);
272 const std::size_t m = w.size();
273 if (event == lang::EventType::ARV) {
274 const std::vector<std::vector<bool>> G = compatibility(sn, ist);
275 std::size_t nb = m;
276 for (std::size_t q = 0; q < m; ++q)
277 if (G[r][w[q].first - 1]) {
278 nb = q;
279 break;
280 }
281 if (nb < m) {
282 std::vector<std::pair<std::size_t, std::size_t>> neww = w;
283 neww.erase(neww.begin() + static_cast<long>(nb));
284 rows.push_back(pack(neww, inspace, w2));
285 rates.push_back(passive);
286 probs.push_back(one);
287 } else if (static_cast<long>(m) < cap) {
288 const std::pair<std::vector<std::size_t>, std::vector<T>> e = entry(sn, ist, r);
289 for (std::size_t i = 0; i < e.first.size(); ++i) {
290 std::vector<std::pair<std::size_t, std::size_t>> neww = w;
291 neww.push_back(std::make_pair(cls, e.first[i]));
292 rows.push_back(pack(neww, inspace, w2));
293 rates.push_back(passive);
294 probs.push_back(e.second[i]);
295 }
296 }
297 } else if (event == lang::EventType::PHASE) {
298 if (!has_patience(sn, ist, r)) return;
299 const mam::Map<T> p = patience_map(sn, ist, r);
300 for (std::size_t q = 0; q < m; ++q) {
301 if (w[q].first != cls) continue;
302 const std::size_t k = w[q].second - 1;
303 for (std::size_t j = 0; j < p.D0.cols(); ++j) {
304 if (j == k || !(num_traits<T>::to_double(p.D0(k, j)) > 0)) continue;
305 std::vector<std::pair<std::size_t, std::size_t>> neww = w;
306 neww[q].second = j + 1;
307 rows.push_back(pack(neww, inspace, w2));
308 rates.push_back(p.D0(k, j));
309 probs.push_back(one);
310 }
311 }
312 } else if (event == lang::EventType::RENEGE) {
313 if (!has_patience(sn, ist, r)) return;
314 const mam::Map<T> p = patience_map(sn, ist, r);
315 for (std::size_t q = 0; q < m; ++q) {
316 if (w[q].first != cls) continue;
317 const std::size_t k = w[q].second - 1;
319 for (std::size_t j = 0; j < p.D1.cols(); ++j) a += p.D1(k, j);
320 if (!(num_traits<T>::to_double(a) > 0)) continue;
321 std::vector<std::pair<std::size_t, std::size_t>> neww = w;
322 neww.erase(neww.begin() + static_cast<long>(q));
323 rows.push_back(pack(neww, inspace, w2));
324 rates.push_back(a);
325 probs.push_back(one);
326 }
327 }
328}
329
330/** Arrival rate of each class at the Source (0 when the class has none). */
331template <class T>
332std::vector<double> source_rates(const NetworkStruct<T>& sn) {
333 const std::size_t R = sn.nclasses;
334 std::vector<double> lam(R, 0.0);
335 for (std::size_t i = 1; i <= sn.stations.size(); ++i) {
336 const std::size_t ind = sn.node_of_station(i);
337 if (ind == 0 || sn.nodes[ind - 1].nodetype != NodeType::Source) continue;
338 for (std::size_t r = 0; r < R; ++r) {
339 const double v = num_traits<T>::to_double(sn.rates(i - 1, r));
340 lam[r] = (std::isfinite(v) && v > 0) ? v : 0.0;
341 }
342 break;
343 }
344 return lam;
345}
346
347/** Per-class rates at a Matching station: matched, accepted and reneging. */
348struct Tput {
349 std::vector<double> X; ///< rate at which class-r jobs leave the node matched
350 std::vector<double> acc; ///< accepted class-r arrival rate (matched or admitted)
351 std::vector<double> reneg; ///< class-r reneging rate
352};
353
354/**
355 * Per-class rates at Matching station `ist` (1-based) from the probabilities
356 * `prob` of its local states `rows` (each [c_1 k_1 ... c_W k_W | vars]). A
357 * class-r arrival finding a compatible job waiting adds its rate to the
358 * matched throughput of both classes.
359 */
360template <class T>
361Tput matching_tput(const NetworkStruct<T>& sn, std::size_t ist, const std::vector<const std::vector<T>*>& rows,
362 const std::vector<double>& prob) {
363 const std::size_t R = sn.nclasses;
364 const std::size_t ind = sn.node_of_station(ist);
365 const std::vector<std::vector<bool>> G = compatibility(sn, ist);
366 const std::vector<double> lam = source_rates(sn);
367 const long cap = capacity(sn, ist);
368 const std::size_t V = sn.nvars_of(ind);
369 std::vector<bool> has_pat(R, false);
370 std::vector<std::vector<double>> renrate(R); // per patience phase: sum(D1(k,:))
371 for (std::size_t r = 0; r < R; ++r) {
372 has_pat[r] = has_patience(sn, ist, r);
373 if (!has_pat[r]) continue;
374 const mam::Map<T> p = patience_map(sn, ist, r);
375 renrate[r].assign(p.D1.rows(), 0.0);
376 for (std::size_t k = 0; k < p.D1.rows(); ++k)
377 for (std::size_t j = 0; j < p.D1.cols(); ++j) renrate[r][k] += num_traits<T>::to_double(p.D1(k, j));
378 }
379 Tput t;
380 t.X.assign(R, 0.0);
381 t.acc.assign(R, 0.0);
382 t.reneg.assign(R, 0.0);
383 for (std::size_t s = 0; s < rows.size(); ++s) {
384 const double p = prob[s];
385 if (!(p > 0)) continue;
386 const std::vector<T>& row = *rows[s];
387 const std::size_t w2 = row.size() > V ? row.size() - V : 0;
388 const std::vector<std::pair<std::size_t, std::size_t>> w = word_of(row, w2);
389 const std::size_t m = w.size();
390 for (std::size_t r = 0; r < R; ++r) {
391 if (!(lam[r] > 0)) continue;
392 std::size_t nb = m;
393 for (std::size_t q = 0; q < m; ++q)
394 if (G[r][w[q].first - 1]) {
395 nb = q;
396 break;
397 }
398 if (nb < m) {
399 t.X[r] += p * lam[r];
400 t.X[w[nb].first - 1] += p * lam[r];
401 t.acc[r] += p * lam[r];
402 } else if (cap < 0 || static_cast<long>(m) < cap) {
403 t.acc[r] += p * lam[r];
404 }
405 }
406 for (const std::pair<std::size_t, std::size_t>& job : w) {
407 const std::size_t c = job.first - 1;
408 if (has_pat[c] && job.second >= 1 && job.second <= renrate[c].size())
409 t.reneg[c] += p * renrate[c][job.second - 1];
410 }
411 }
412 return t;
413}
414
415} // namespace matching
416} // namespace qn
417} // namespace line
418
419#endif // LINE_LANG_QN_MATCHING_STATION_H
Base error for the multiprecision C++ port.
Definition error.h:73
InputError(const std::string &what)
Definition error.h:105
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
mam::Map< T > dist_to_map(const Distrib< T > &d)
The (D0,D1) pair that reaches sn.proc.
EventType
The events a state can undergo, with the values of MATLAB EventType.
Definition lang_types.h:116
@ PHASE
service advances a phase WITHOUT departing
Definition lang_types.h:121
@ RENEGE
a waiting job abandons the queue (impatience)
Definition lang_types.h:128
@ ARV
a job arrives
Definition lang_types.h:119
std::vector< T > map_pie(const Map< T > &m)
Phase distribution seen by an arriving job, pie = pi D1 / (pi D1 e).
Definition map_moment.h:89
long capacity(const NetworkStruct< T > &sn, std::size_t ist)
Capacity of station ist (1-based), or -1 when the station is uncapped.
std::vector< T > count_waiting(const NetworkStruct< T > &sn, std::size_t ind, const std::vector< T > &row)
Per-class waiting counts of a Matching station's local state row (node ind, 1-based).
std::vector< double > source_rates(const NetworkStruct< T > &sn)
Arrival rate of each class at the Source (0 when the class has none).
std::vector< std::size_t > phases(const NetworkStruct< T > &sn, std::size_t ist, std::size_t r, bool initial_only)
Patience phases (1-based) a waiting class-r job can be in: {1} without patience, otherwise 1....
bool is_matching_node(const NetworkStruct< T > &sn, std::size_t ind)
True when node ind (1-based) is a Matching (FCFM) station.
void after_event(const NetworkStruct< T > &sn, std::size_t ind, const std::vector< T > &inspace, lang::EventType event, std::size_t cls, std::vector< std::vector< T > > &rows, std::vector< T > &rates, std::vector< T > &probs)
Successors of a Matching station (node ind, 1-based) after event on class cls (1-based): the rows,...
mam::Map< T > patience_map(const NetworkStruct< T > &sn, std::size_t ist, std::size_t r)
The (D0, D1) representation of the class-r patience at station ist.
std::vector< T > pack(const std::vector< std::pair< std::size_t, std::size_t > > &w, const std::vector< T > &row, std::size_t w2)
Pack a word back into a row of the width of row, keeping its trailing variables.
std::vector< std::pair< std::size_t, std::size_t > > word_of(const std::vector< T > &row, std::size_t w2)
The (class, phase) pairs of a word, both 1-based, oldest first.
std::vector< std::vector< T > > space(const NetworkStruct< T > &sn, std::size_t ist, const std::vector< std::size_t > &n, bool initial_only)
Local states of Matching station ist (1-based) with per-class waiting counts n, WITHOUT the trailing ...
bool has_patience(const NetworkStruct< T > &sn, std::size_t ist, std::size_t r)
Whether class r (0-based) reneges at station ist (1-based).
bool is_matching(const NetworkStruct< T > &sn, std::size_t ist)
True when station ist (1-based) is a Matching (FCFM) station.
std::vector< std::vector< bool > > compatibility(const NetworkStruct< T > &sn, std::size_t ist)
Compatibility graph of Matching station ist (1-based), R x R.
std::pair< std::vector< std::size_t >, std::vector< T > > entry(const NetworkStruct< T > &sn, std::size_t ist, std::size_t r)
Initial patience phases (1-based) of a new class-r job and their probabilities.
Tput matching_tput(const NetworkStruct< T > &sn, std::size_t ist, const std::vector< const std::vector< T > * > &rows, const std::vector< double > &prob)
Per-class rates at Matching station ist (1-based) from the probabilities prob of its local states row...
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
A MAP as the pair of matrices (D0, D1).
Definition map_moment.h:53
Matrix< T > D1
Definition map_moment.h:55
Matrix< T > D0
Definition map_moment.h:54
One station of the network.
std::vector< Distrib< T > > patience
Queue.setPatience(class, dist): the abandonment timer of a WAITING job, with impatience[r] naming whi...
std::vector< lang::ImpatienceType > impatience
Per-class rates at a Matching station: matched, accepted and reneging.
std::vector< double > reneg
class-r reneging rate
std::vector< double > acc
accepted class-r arrival rate (matched or admitted)
std::vector< double > X
rate at which class-r jobs leave the node matched