LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
pfqn_oi_respt.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_OI_RESPT_H
6#define LINE_API_PFQN_PFQN_OI_RESPT_H
7
8/**
9 * @file
10 * @ingroup api_pfqn
11 * Exact per-class sojourn-time distribution at an OPEN order-independent (OI)
12 * station whose rate is that of a NESTED collaborative server pool, as a
13 * phase-type law.
14 *
15 * Templated port of matlab/src/api/pfqn/pfqn_oi_respt.m. A collaborative pool
16 * (sn_compat_rate) has a rate that depends only on which classes are present,
17 * mu(n) = mu(S(supp n)) with S(A) the servers compatible with some class of A.
18 * It is NESTED when any two classes have server sets that are disjoint or
19 * contained one in the other. For such a station fed by Poisson traffic (in
20 * isolation, or anywhere in an open product-form network, since an OI queue is
21 * quasi-reversible), Gardner and Righter (2020, Thm 4.5) give, for every class i,
22 *
23 * T_i ~ Exp(mu(S_i) - lambda(R(S_i)))
24 * + sum_{g : S_i strictly in S_g} Bernoulli(lambda_g / muh_g) * Exp(muh_g - lambda_g),
25 *
26 * muh_g = mu(S_g) - lambda(R(S_g)) + lambda_g, all terms independent, where
27 * R(S) are the classes whose server set lies in S and classes with equal server
28 * sets are merged into one group g (they are served FCFS among themselves).
29 *
30 * Everything is read off the rate function on CLASS SETS, m(A) = mu(1_A):
31 * S_j in S_i iff m({i,j}) = m({i}),
32 * S_i, S_j disjoint iff m({i,j}) = m({i}) + m({j}),
33 * and the structure is then CHECKED on every class subset: m(A) must equal the
34 * sum of m over the most flexible classes of A.
35 *
36 * REFUSALS, with the reference's reason strings verbatim: "unstable",
37 * "not support-only" (the rate depends on how many jobs of a class are present),
38 * "not nested" (two classes share some but not all servers), "not a pool" (the
39 * subset check failed), "too many classes" (more than 16 classes with traffic).
40 *
41 * Reference: Gardner, Righter (2020), Queueing Systems 96:3-51, Sec. 4.1 (Thm 4.5).
42 *
43 * ARITHMETIC. Field arithmetic; the structural tests are tolerance tests taken
44 * in double, as the reference takes them. Stability and the saturation cutoffs
45 * come from pfqn_oi_mixed (the open station is its one-point closed lattice),
46 * which reports an unstable station as stable = false in every arithmetic, so
47 * "unstable" is returned rather than raised even under Rational.
48 */
49
50#include <algorithm>
51#include <cmath>
52#include <cstddef>
53#include <functional>
54#include <limits>
55#include <string>
56#include <vector>
57
60#include "line/num/number.h"
61#include "line/util/error.h"
62#include "line/util/matrix.h"
63
64namespace line {
65namespace pfqn {
66
67/** Return value of pfqn_oi_respt, mirroring [PH, ok, why]. */
68template <class T>
70 /**
71 * (R) PH[i] is the sojourn time of class i as a renewal MAP
72 * (D1 = -D0 * 1 * alpha), for every class with lambda_i > 0; an empty MAP
73 * (order 0) otherwise, and for every class when ok is false.
74 */
75 std::vector<mam::Map<T> > PH;
76 bool ok = false; ///< false when the theorem does not apply
77 std::string why; ///< the reason when ok is false
78};
79
80namespace detail {
81
82/**
83 * Independent sum of Bernoulli(probs[k]) * Exp(rates[k]), with probs.back() = 1,
84 * as a phase-type law in renewal MAP form {D0, D1}; `series_ph` in the reference.
85 */
86template <class T>
87mam::Map<T> oi_respt_series_ph(const std::vector<T>& rates, const std::vector<T>& probs) {
88 const std::size_t k = rates.size();
89 const T zero = num_traits<T>::from_int(0);
90 const T one = num_traits<T>::from_int(1);
91 // law of the next phase that is actually visited, starting at position from
92 const auto reach = [&](std::size_t from) -> std::vector<T> {
93 std::vector<T> v(k, zero);
94 T rem = one;
95 for (std::size_t q = from; q < k; ++q) {
96 v[q] = T(rem * probs[q]);
97 rem = T(rem * T(one - probs[q]));
98 }
99 return v;
100 };
101 const std::vector<T> alpha = reach(0);
102 Matrix<T> D0(k, k, zero);
103 for (std::size_t l = 0; l + 1 < k; ++l) {
104 const std::vector<T> nx = reach(l + 1);
105 for (std::size_t q = 0; q < k; ++q) D0(l, q) = T(rates[l] * nx[q]);
106 D0(l, l) = T(-rates[l]);
107 }
108 D0(k - 1, k - 1) = T(-rates[k - 1]);
109 Matrix<T> D1(k, k, zero);
110 for (std::size_t l = 0; l < k; ++l) {
111 T ex = zero;
112 for (std::size_t q = 0; q < k; ++q) ex -= D0(l, q);
113 for (std::size_t q = 0; q < k; ++q) D1(l, q) = T(ex * alpha[q]);
114 }
115 mam::Map<T> m;
116 m.D0 = D0;
117 m.D1 = D1;
118 return m;
119}
120
121} // namespace detail
122
123/**
124 * @brief Per-class phase-type sojourn laws of an open nested-pool OI station.
125 *
126 * @param lambda (R) class traffic offered to the station (>= 0)
127 * @param mu total service rate mu(n) on the per-class count vector
128 * @param opt passed to the stability and saturation search (pfqn_oi_mixed)
129 */
130template <class T>
131OiResptResult<T> pfqn_oi_respt(const std::vector<T>& lambda,
132 const std::function<T(const std::vector<int>&)>& mu,
133 const OiMixedOptions& opt = OiMixedOptions()) {
134 const std::size_t R = lambda.size();
135 const T zero = num_traits<T>::from_int(0);
137 out.PH.assign(R, mam::Map<T>());
138 std::vector<std::size_t> act;
139 for (std::size_t r = 0; r < R; ++r)
140 if (lambda[r] > zero) act.push_back(r);
141 if (act.empty()) {
142 out.ok = true;
143 return out;
144 }
145 if (act.size() > 16) {
146 out.why = "too many classes";
147 return out;
148 }
149 if (!mu)
150 throw InputError(ErrorKind{"Argument"},
151 "pfqn_oi_respt: mu must be a function mu(n) on the per-class count vector.");
152 {
153 // pfqn_oi_open's stable and tau, read off the one-point mixed table so
154 // that an unstable station is reported, not raised, in every arithmetic
156 lambda, std::vector<double>(R, std::numeric_limits<double>::infinity()), mu, opt);
157 if (!mx.stable) {
158 out.why = "unstable";
159 return out;
160 }
161 for (std::size_t r : act)
162 if (r < mx.tau.size() && mx.tau[r] > 1) {
163 out.why = "not support-only";
164 return out;
165 }
166 }
167 const std::size_t Ra = act.size();
168 // set function on the active classes (mask over 0..Ra-1)
169 const auto m = [&](unsigned long mask) -> T {
170 std::vector<int> n(R, 0);
171 for (std::size_t i = 0; i < Ra; ++i)
172 if (mask & (1UL << i)) n[act[i]] = 1;
173 return mu(n);
174 };
175 std::vector<T> mi(Ra, zero);
176 std::vector<double> mid(Ra, 0.0);
177 for (std::size_t i = 0; i < Ra; ++i) {
178 mi[i] = m(1UL << i);
179 mid[i] = num_traits<T>::to_double(mi[i]);
180 }
181 const double tol = 1e-10;
182 // D[i][j]: S_j is contained in S_i
183 std::vector<std::vector<bool> > D(Ra, std::vector<bool>(Ra, false));
184 for (std::size_t i = 0; i < Ra; ++i) {
185 D[i][i] = true;
186 for (std::size_t j = i + 1; j < Ra; ++j) {
187 const double mij = num_traits<T>::to_double(m((1UL << i) | (1UL << j)));
188 const double sc = tol * std::max(1.0, mij);
189 if (std::fabs(mij - mid[i]) <= sc) D[i][j] = true;
190 if (std::fabs(mij - mid[j]) <= sc) D[j][i] = true;
191 if (!D[i][j] && !D[j][i] && std::fabs(mij - mid[i] - mid[j]) > sc) {
192 out.why = "not nested";
193 return out;
194 }
195 }
196 }
197 // containment must be a partial order compatible with the rates
198 for (std::size_t i = 0; i < Ra; ++i)
199 for (std::size_t j = 0; j < Ra; ++j) {
200 if (!D[i][j]) continue;
201 for (std::size_t k = 0; k < Ra; ++k)
202 if (!D[i][k] && D[j][k]) {
203 out.why = "not nested";
204 return out;
205 }
206 }
207 // subset check: m(A) = sum of m over the maximal (most flexible) classes of A
208 for (unsigned long mask = 1; mask < (1UL << Ra); ++mask) {
209 std::vector<std::size_t> set, top;
210 for (std::size_t i = 0; i < Ra; ++i)
211 if (mask & (1UL << i)) set.push_back(i);
212 for (std::size_t i : set) {
213 bool dom = false; // some class of A strictly contains i
214 for (std::size_t j : set)
215 if (D[j][i] && !D[i][j]) dom = true;
216 bool covered = false; // first of an equal-set group only
217 for (std::size_t t : top)
218 if (D[t][i]) covered = true;
219 if (!dom && !covered) top.push_back(i);
220 }
221 const double v = num_traits<T>::to_double(m(mask));
222 double s = 0.0;
223 for (std::size_t t : top) s += mid[t];
224 if (std::fabs(v - s) > tol * std::max(1.0, v)) {
225 out.why = "not a pool";
226 return out;
227 }
228 }
229 std::vector<T> la(Ra, zero), lamR(Ra, zero);
230 for (std::size_t i = 0; i < Ra; ++i) la[i] = lambda[act[i]];
231 for (std::size_t i = 0; i < Ra; ++i)
232 for (std::size_t j = 0; j < Ra; ++j)
233 if (D[i][j]) lamR[i] += la[j]; // lambda(R(S_i))
234 for (std::size_t i = 0; i < Ra; ++i) {
235 std::vector<T> rates, probs;
236 std::vector<bool> done(Ra, false);
237 for (std::size_t g = 0; g < Ra; ++g) {
238 const bool strict = D[g][i] && !D[i][g];
239 if (!strict || done[g]) continue;
240 T lg = zero; // classes with the same server set as g
241 for (std::size_t j = 0; j < Ra; ++j)
242 if (D[g][j] && D[j][g]) {
243 done[j] = true;
244 lg += la[j];
245 }
246 const T muh = T(mi[g] - lamR[g] + lg);
247 rates.push_back(T(muh - lg));
248 probs.push_back(T(lg / muh));
249 }
250 rates.push_back(T(mi[i] - lamR[i]));
251 probs.push_back(num_traits<T>::from_int(1));
252 out.PH[act[i]] = detail::oi_respt_series_ph(rates, probs);
253 }
254 out.ok = true;
255 return out;
256}
257
258} // namespace pfqn
259} // namespace line
260
261#endif // LINE_API_PFQN_PFQN_OI_RESPT_H
InputError(const std::string &what)
Definition error.h:105
The exception types the port throws.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
OiMixedResult< T > pfqn_oi_mixed(const std::vector< T > &lambda, const std::vector< double > &N, const std::function< T(const std::vector< int > &)> &mu, const OiMixedOptions &opt=OiMixedOptions())
Closed-lattice station tables of an OI station with open classes summed out.
OiResptResult< T > pfqn_oi_respt(const std::vector< T > &lambda, const std::function< T(const std::vector< int > &)> &mu, const OiMixedOptions &opt=OiMixedOptions())
Per-class phase-type sojourn laws of an open nested-pool OI station.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Number-type abstraction for the templated API port.
Closed-lattice station tables of an order-independent (OI) station visited by CLOSED and OPEN classes...
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
The tuning fields of pfqn_oi_mixed / pfqn_oi_open (OPTIONS in the reference).
Return value of pfqn_oi_mixed, mirroring [W, H, S, stable, tau, method].
std::vector< int > tau
(R) open cutoffs used (0 for closed and idle classes)
bool stable
false when some reachable macrostate has den(m) <= 0
Return value of pfqn_oi_respt, mirroring [PH, ok, why].
bool ok
false when the theorem does not apply
std::string why
the reason when ok is false
std::vector< mam::Map< T > > PH
(R) PH[i] is the sojourn time of class i as a renewal MAP (D1 = -D0 * 1 * alpha), for every class wit...