LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_oi_loss.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_OI_LOSS_H
6#define LINE_API_QSYS_QSYS_OI_LOSS_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Exact steady state of an order-independent (OI) LOSS queue.
12 *
13 * Templated port of matlab/src/api/qsys/qsys_oi_loss.m. Poisson arrivals of R
14 * classes at rates LAMBDA, a permutation-invariant total service rate mu(n) of
15 * the per-class count vector, at most K jobs in total and at most Kr(r) of class
16 * r; an arrival that would breach a limit is lost.
17 *
18 * Truncating an OI queue destroys quasi-reversibility but not the product form
19 * of the truncated chain: the stationary law is the unrestricted OI law
20 * renormalized over the admissible set A = {n : sum(n) <= K, n <= Kr}
21 * (Berezner, Kriel, Krzesinski 1995, Sec. 6, total limit; Berezner and
22 * Krzesinski 1996, per-type limits). With g(n) = Phi(n) prod_r lambda_r^{n_r},
23 *
24 * g(0) = 1, g(n) = (1/mu(n)) sum_{r: n_r>0} lambda_r g(n - e_r), n in A,
25 *
26 * and by PASTA the class-r blocking probability is the mass of the states in
27 * which a class-r arrival is refused, B_r = P(sum(n) = K or n_r = Kr(r)). The
28 * in-service means use the weight Xi_r of pfqn_oi_insvc on the same set. The
29 * queue is exact only IN ISOLATION: a loss OI queue is not quasi-reversible.
30 *
31 * ARITHMETIC. Field arithmetic; exact under Rational. The in-service indicator
32 * mu(n) > mu(n - e_r) is a tolerance test taken in double, as the reference's.
33 */
34
35#include <algorithm>
36#include <cmath>
37#include <functional>
38#include <limits>
39#include <sstream>
40#include <vector>
41
42#include "line/num/number.h"
43#include "line/util/error.h"
44
45namespace line {
46namespace qsys {
47
48/** Return value of qsys_oi_loss, mirroring [Q, Usir, B, Xadm, P0]. */
49template <class T>
51 std::vector<T> Q; ///< (R) mean number of class-r jobs
52 std::vector<T> Usir; ///< (R) mean number of class-r jobs in service
53 std::vector<T> B; ///< (R) blocking (loss) probability seen by a class-r arrival
54 std::vector<T> Xadm; ///< (R) admitted throughput lambda_r (1 - B_r)
55 T P0; ///< probability of the empty station
56};
57
58/**
59 * @brief Exact OI loss queue.
60 *
61 * @param lambda (R) arrival rates (>= 0)
62 * @param mu total service rate mu(n) on the per-class count vector
63 * @param K total limit (+Inf for none)
64 * @param Kr (R) per-class limits (+Inf for none); empty means none
65 */
66template <class T>
67OiLossResult<T> qsys_oi_loss(const std::vector<T>& lambda,
68 const std::function<T(const std::vector<int>&)>& mu, double K,
69 std::vector<double> Kr = std::vector<double>()) {
70 const std::size_t R = lambda.size();
71 const double inf = std::numeric_limits<double>::infinity();
72 if (Kr.empty()) Kr.assign(R, inf);
73 if (Kr.size() != R)
74 throw InputError(ErrorKind{"Argument"}, "qsys_oi_loss: Kr must have one entry per class.");
75 if (!mu)
76 throw InputError(ErrorKind{"Argument"},
77 "qsys_oi_loss: mu must be a function mu(n) on the per-class count vector.");
78 const T zero = num_traits<T>::from_int(0);
79 std::vector<int> ext(R, 0);
80 for (std::size_t r = 0; r < R; ++r) {
81 const double l = num_traits<T>::to_double(lambda[r]);
82 if (!std::isfinite(l) || l < 0.0)
83 throw InputError(ErrorKind{"Argument"}, "qsys_oi_loss: lambda must be finite and nonnegative.");
84 double e = std::min(Kr[r], K);
85 if (!(lambda[r] > zero)) e = 0.0;
86 if (!std::isfinite(e))
87 throw InputError(ErrorKind{"Argument"},
88 "qsys_oi_loss: the admissible set is infinite: give a finite total limit K or a "
89 "finite limit for every class with traffic.");
90 ext[r] = static_cast<int>(std::llround(e));
91 }
92 std::vector<std::size_t> dims(R), strides(R, 1);
93 for (std::size_t r = 0; r < R; ++r) dims[r] = static_cast<std::size_t>(ext[r]) + 1;
94 for (std::size_t r = 1; r < R; ++r) strides[r] = strides[r - 1] * dims[r - 1];
95 std::size_t ngrid = 1;
96 for (std::size_t d : dims) ngrid *= d;
97 std::vector<std::vector<int>> counts(ngrid, std::vector<int>(R, 0));
98 std::vector<int> tot(ngrid, 0);
99 std::vector<bool> adm(ngrid, false);
100 for (std::size_t k = 0; k < ngrid; ++k) {
101 std::size_t li = k;
102 for (std::size_t r = 0; r < R; ++r) {
103 counts[k][r] = static_cast<int>(li % dims[r]);
104 li /= dims[r];
105 tot[k] += counts[k][r];
106 }
107 adm[k] = static_cast<double>(tot[k]) <= K;
108 }
109 std::vector<std::size_t> ord(ngrid);
110 for (std::size_t k = 0; k < ngrid; ++k) ord[k] = k;
111 std::stable_sort(ord.begin(), ord.end(),
112 [&tot](std::size_t a, std::size_t b) { return tot[a] < tot[b]; });
113
114 std::vector<T> g(ngrid, zero), muv(ngrid, zero);
115 std::vector<std::vector<T>> xi(ngrid, std::vector<T>(R, zero));
116 g[0] = num_traits<T>::from_int(1);
117 for (std::size_t kk = 1; kk < ngrid; ++kk) {
118 const std::size_t k = ord[kk];
119 if (!adm[k]) continue;
120 const std::vector<int>& n = counts[k];
121 muv[k] = mu(n);
122 const double dmu = num_traits<T>::to_double(muv[k]);
123 if (!(dmu > 0.0) || !std::isfinite(dmu)) {
124 std::ostringstream os;
125 os << "qsys_oi_loss: mu(n) must be positive and finite on every admissible nonempty "
126 "state (got "
127 << dmu << " at n = [";
128 for (std::size_t r = 0; r < R; ++r) os << (r ? " " : "") << n[r];
129 os << "]).";
130 throw InputError(ErrorKind{"Argument"}, os.str());
131 }
132 T s = zero;
133 for (std::size_t r = 0; r < R; ++r)
134 if (n[r] > 0) s += T(lambda[r] * g[k - strides[r]]);
135 g[k] = T(s / muv[k]);
136 for (std::size_t j = 0; j < R; ++j) {
137 T t = zero;
138 for (std::size_t r = 0; r < R; ++r)
139 if (n[r] > 0) t += T(lambda[r] * xi[k - strides[r]][j]);
140 if (n[j] > 0) {
141 const std::size_t kj = k - strides[j];
142 if (dmu - num_traits<T>::to_double(muv[kj]) > 1e-12 * std::max(1.0, dmu))
143 t += T(lambda[j] * g[kj]);
144 }
145 xi[k][j] = T(t / muv[k]);
146 }
147 }
148 T Gt = zero;
149 for (std::size_t k = 0; k < ngrid; ++k) Gt += g[k];
150 OiLossResult<T> out;
151 out.Q.assign(R, zero);
152 out.Usir.assign(R, zero);
153 out.B.assign(R, zero);
154 out.Xadm.assign(R, zero);
155 out.P0 = T(g[0] / Gt);
156 for (std::size_t k = 0; k < ngrid; ++k) {
157 if (g[k] == zero && !adm[k]) continue;
158 const T p = T(g[k] / Gt);
159 for (std::size_t r = 0; r < R; ++r) {
160 out.Q[r] += T(num_traits<T>::from_int(counts[k][r]) * p);
161 out.Usir[r] += T(xi[k][r] / Gt);
162 if (adm[k] && (static_cast<double>(tot[k]) >= K || static_cast<double>(counts[k][r]) >= Kr[r]))
163 out.B[r] += p;
164 }
165 }
166 const T one = num_traits<T>::from_int(1);
167 for (std::size_t r = 0; r < R; ++r) out.Xadm[r] = T(lambda[r] * (one - out.B[r]));
168 return out;
169}
170
171} // namespace qsys
172} // namespace line
173
174#endif // LINE_API_QSYS_QSYS_OI_LOSS_H
InputError(const std::string &what)
Definition error.h:105
The exception types the port throws.
OiLossResult< T > qsys_oi_loss(const std::vector< T > &lambda, const std::function< T(const std::vector< int > &)> &mu, double K, std::vector< double > Kr=std::vector< double >())
Exact OI loss queue.
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_oi_loss, mirroring [Q, Usir, B, Xadm, P0].
std::vector< T > Xadm
(R) admitted throughput lambda_r (1 - B_r)
std::vector< T > Q
(R) mean number of class-r jobs
std::vector< T > B
(R) blocking (loss) probability seen by a class-r arrival
T P0
probability of the empty station
std::vector< T > Usir
(R) mean number of class-r jobs in service