LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_gimc.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_GIMC_H
6#define LINE_API_QSYS_QSYS_GIMC_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Exact analysis of the GI/M/c queue (Takacs, 1962).
12 *
13 * Port of matlab/src/api/qsys/qsys_gimc.m. With A(s) the interarrival LST,
14 * omega in (0,1) is the root of omega = A(c mu (1 - omega)). An arrival finds
15 * n >= c-1 jobs with probability D omega^(n-c), and fewer with the alternating
16 * binomial sums of the U_n built from g_j = A(j mu); D normalizes. Waiting is
17 * then exponential, P(W > t) = PW exp(-c mu (1 - omega) t), PW = D/(1-omega),
18 * and the time-average queue law follows from the arrival law by rate
19 * balance. MATLAB's refusals when omega cannot be bracketed (LINE:numerical:Root)
20 * and when the alternating sums lose accuracy (LINE:numerical:Cancellation)
21 * are kept.
22 *
23 * ARITHMETIC: double, as the reference. MATLAB's fzero on [0, 1-d] is a
24 * bracketing root search; it is reproduced here by bisection run to the
25 * resolution of the double grid, which brackets the same simple root.
26 */
27
28#include <algorithm>
29#include <cmath>
30#include <cstdio>
31#include <functional>
32#include <limits>
33#include <string>
34#include <vector>
35
38#include "line/util/error.h"
39
40namespace line {
41namespace qsys {
42
43/**
44 * @brief Exact GI/M/c queue, interarrival law given by its LST.
45 *
46 * @param lst interarrival LST at real arguments
47 * @param lambda arrival rate (MATLAB's 'lambda' option, required with a handle)
48 * @param mu service rate of one server
49 * @param c number of servers
50 * @param o tPoints, percentiles and nmax options
51 */
52namespace detail {
53/**
54 * R_j = (c(1-g_j) - j)/(c(1-omega) - j) of the Takacs boundary constants.
55 * With eps = c(1-omega) - j the numerator is eps + c(omega - g_j), and
56 * omega = LST(mu(j+eps)), g_j = LST(mu j), so R_j = 1 + c(LST(mu(j+eps)) - LST(mu j))/eps.
57 * When c(1-omega) is an integer j both factors vanish together (g_j = omega) and
58 * the quotient is 0/0; the singularity is removable, with limit 1 + c mu LST'(mu j).
59 * Near it both factors are rounding noise, so for |eps| < 1e-5 the second-order
60 * expansion 1 + c mu L' + c mu^2 eps L''/2 is used, with L' from a five-point and
61 * L'' from a three-point central difference at step 1e-3 mu j.
62 */
63inline double gimc_ratio(const std::function<double(double)>& lst, double mu, unsigned c, unsigned j,
64 double omega, double gjj) {
65 const double eps = c * (1.0 - omega) - double(j);
66 if (std::fabs(eps) >= 1e-5) return (c * (1.0 - gjj) - double(j)) / eps;
67 const double s0 = j * mu, h = 1e-3 * s0;
68 const double l1 = (lst(s0 - 2 * h) - 8 * lst(s0 - h) + 8 * lst(s0 + h) - lst(s0 + 2 * h)) / (12 * h);
69 const double l2 = (lst(s0 - h) - 2 * gjj + lst(s0 + h)) / (h * h);
70 return 1.0 + c * mu * l1 + c * mu * mu * eps * l2 / 2.0;
71}
72} // namespace detail
73
74inline QsysRes qsys_gimc(const std::function<double(double)>& lst, double lambda, double mu, unsigned c,
75 const QsysResOptions& o = QsysResOptions()) {
76 using namespace resdetail;
77 check_percentiles(o.percentiles, "qsys_gimc");
78 if (!lst) throw Error("qsys_gimc: the interarrival law must be a Distribution or an LST function handle");
79 if (!(lambda > 0.0) || !std::isfinite(lambda))
80 throw Error("qsys_gimc: with an LST handle the arrival rate must be given with 'lambda'");
81 if (!(mu > 0.0)) throw Error("qsys_gimc: mu must be a positive scalar");
82 if (c < 1) throw Error("qsys_gimc: c must be a positive integer");
83 const double A = lambda / mu;
84 if (A >= c) throw_unstable("qsys_gimc", "lambda/(c mu)", A / c);
85
86 const std::function<double(double)> g = [&](double w) { return lst(c * mu * (1.0 - w)) - w; };
87 double d = 1e-3;
88 while (g(1.0 - d) >= 0.0 && d > 1e-15) d /= 10.0;
89 if (g(1.0 - d) >= 0.0) throw NumericError(ErrorKind{"Root"}, "qsys_gimc: could not bracket the root omega in (0,1)");
90 double lo = 0.0, hi = 1.0 - d;
91 for (int it = 0; it < 2000; ++it) {
92 const double mid = 0.5 * (lo + hi);
93 if (mid <= lo || mid >= hi) break;
94 const double gm = g(mid);
95 if (gm == 0.0) {
96 lo = hi = mid;
97 break;
98 }
99 if (gm > 0.0) lo = mid;
100 else hi = mid;
101 }
102 const double omega = std::fabs(g(lo)) <= std::fabs(g(hi)) ? lo : hi;
103
104 std::vector<double> gj(c), Cj(c), terms(c);
105 for (unsigned j = 1; j <= c; ++j) gj[j - 1] = lst(j * mu);
106 double cp = 1.0;
107 for (unsigned j = 1; j <= c; ++j) {
108 cp *= gj[j - 1] / (1.0 - gj[j - 1]);
109 Cj[j - 1] = cp;
110 }
111 double tsum = 0.0;
112 for (unsigned j = 1; j <= c; ++j) {
113 terms[j - 1] = nchoosek_r(c, j) * detail::gimc_ratio(lst, mu, c, j, omega, gj[j - 1]) /
114 (Cj[j - 1] * (1.0 - gj[j - 1]));
115 tsum += terms[j - 1];
116 }
117 const double D = 1.0 / (1.0 / (1.0 - omega) + tsum);
118 std::vector<double> Cn(c + 1);
119 Cn[0] = 1.0;
120 for (unsigned j = 1; j <= c; ++j) Cn[j] = Cj[j - 1];
121 std::vector<double> Un(c);
122 for (unsigned nn = 0; nn < c; ++nn) {
123 double s = 0.0;
124 for (unsigned j = nn; j < c; ++j) s += terms[j];
125 Un[nn] = D * Cn[nn] * s;
126 }
127 long nmax = o.nmax;
128 if (nmax < 0)
129 nmax = static_cast<long>(c) +
130 static_cast<long>(std::max(1.0, std::ceil(std::log(1e-14 * (1.0 - omega)) /
131 std::log(std::max(omega, std::numeric_limits<double>::min())))));
132 nmax = std::max<long>(nmax, c);
133 std::vector<double> Pi(static_cast<std::size_t>(nmax + 1));
134 for (long nn = 0; nn <= nmax; ++nn) {
135 if (nn >= static_cast<long>(c) - 1) {
136 Pi[nn] = D * std::pow(omega, static_cast<double>(nn - static_cast<long>(c)));
137 } else {
138 double s = 0.0;
139 for (long ii = nn; ii < static_cast<long>(c); ++ii)
140 s += (((ii - nn) % 2) ? -1.0 : 1.0) * nchoosek_r(ii, nn) * Un[ii];
141 Pi[nn] = s;
142 }
143 }
144 double PiTot = D / omega / (1.0 - omega);
145 for (long nn = 0; nn + 1 < static_cast<long>(c); ++nn) PiTot += Pi[nn];
146 bool neg = false;
147 for (long nn = 0; nn < std::max<long>(static_cast<long>(c) - 1, 1); ++nn)
148 if (Pi[nn] < -1e-10) neg = true;
149 if (!(std::fabs(PiTot - 1.0) <= 1e-8) || neg) { // NaN fails too
150 char buf[256];
151 std::snprintf(buf, sizeof buf,
152 "qsys_gimc: the arrival distribution does not normalize (sum = %.12g): the alternating "
153 "sums lost accuracy at c = %u",
154 PiTot, c);
155 throw NumericError(ErrorKind{"Cancellation"}, buf);
156 }
157 for (double& v : Pi) v = std::max(v, 0.0);
158
159 std::vector<double> Pn(Pi.size(), 0.0);
160 for (long nn = 1; nn <= nmax; ++nn) Pn[nn] = A * Pi[nn - 1] / std::min<long>(nn, c);
161 double sj = 0.0;
162 for (unsigned j = 1; j + 1 <= c; ++j) sj += Pi[j - 1] * (1.0 / j - 1.0 / c);
163 Pn[0] = 1.0 - A / c - A * sj;
164
165 const double PW = D / (1.0 - omega);
166 const double theta = c * mu * (1.0 - omega);
167 const double EW = PW / theta;
168 const double EW2 = 2.0 * PW / (theta * theta);
169 const double VW = EW2 - EW * EW;
170 const double tailc = (A / c) * D;
171 const double om1 = 1.0 - omega;
172 const double EQ = tailc / (om1 * om1);
173 const double EQ2 = tailc * (1.0 + omega) / (om1 * om1 * om1);
174 const double m = c + 1.0;
175 double EN2 = 0.0;
176 for (unsigned n0 = 0; n0 <= c; ++n0) EN2 += static_cast<double>(n0) * n0 * Pn[n0];
177 EN2 += tailc * (m * m / om1 + 2.0 * m * omega / (om1 * om1) + omega * (1.0 + omega) / (om1 * om1 * om1));
178 const double EN = EQ + A;
179
180 QsysRes r;
181 r.set("meanQueueLength", EN);
182 r.set("varQueueLength", EN2 - EN * EN);
183 r.set("meanQueueLengthQ", EQ);
184 r.set("varQueueLengthQ", EQ2 - EQ * EQ);
185 r.set("meanWaitingTime", EW);
186 r.set("varWaitingTime", VW);
187 r.set("meanSojournTime", EW + 1.0 / mu);
188 r.set("varSojournTime", VW + 1.0 / (mu * mu));
189 r.set("probWait", PW);
190 r.set("condMeanWaitingTime", 1.0 / theta);
191 r.set("throughput", lambda);
192 r.set("utilization", A / c);
193 r.set("omega", omega);
194 r.set("queueLengthDist", Pn);
195 r.set("arrivalDist", Pi);
196 const std::function<double(double)> Wccdf = [PW, theta](double t) {
197 return t < 0.0 ? 1.0 : PW * std::exp(-theta * t);
198 };
199 const std::function<double(double)> Tccdf = [PW, theta, mu](double t) {
200 if (t < 0.0) return 1.0;
201 if (std::fabs(theta - mu) > 1e-12 * mu) {
202 const double G = PW * mu / (mu - theta);
203 return (1.0 - G) * std::exp(-mu * t) + G * std::exp(-theta * t);
204 }
205 return (1.0 + PW * mu * t) * std::exp(-mu * t);
206 };
207 if (!o.tPoints.empty()) {
208 r.set("tPoints", o.tPoints);
209 r.set("waitingTimeCdf", cdf_of(Wccdf, o.tPoints));
210 r.set("sojournTimeCdf", cdf_of(Tccdf, o.tPoints));
211 }
212 if (!o.percentiles.empty()) {
213 r.set("percentiles", o.percentiles);
214 std::vector<double> wp(o.percentiles.size());
215 for (std::size_t i = 0; i < wp.size(); ++i)
216 wp[i] = std::max(0.0, std::log(PW / (1.0 - o.percentiles[i] / 100.0)) / theta);
217 r.set("waitingTimePercentile", wp);
218 r.set("sojournTimePercentile", pct_of(Tccdf, o.percentiles, 50.0 / std::min(mu, theta)));
219 }
220 r.analyzer = "exact:GI/M/" + std::to_string(c);
221 return r;
222}
223
224/**
225 * @brief Exact GI/M/c queue, interarrival law given as a Distribution (its LST,
226 * and its rate 1/mean unless `lambda` > 0 is given, as MATLAB's
227 * LA.evalLST / LA.getMean).
228 */
229inline QsysRes qsys_gimc(const lang::Distrib<double>& LA, double mu, unsigned c,
230 const QsysResOptions& o = QsysResOptions(), double lambda = -1.0) {
231 const lang::Distrib<double> d = LA;
232 if (!(lambda > 0.0)) lambda = 1.0 / lang::dist_moment(d, 1);
233 return qsys_gimc([d](double s) { return lang::dist_lst(d, s); }, lambda, mu, c, o);
234}
235
236} // namespace qsys
237} // namespace line
238
239#endif // LINE_API_QSYS_QSYS_GIMC_H
Error(const std::string &what)
Definition error.h:75
NumericError(const std::string &what)
Definition error.h:112
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
T dist_lst(const Distrib< T > &d, const T &s)
sn.lst: the Laplace-Stieltjes transform E[exp(-sX)].
T dist_moment(const Distrib< T > &d, unsigned k)
The k-th raw moment.
QsysRes qsys_gimc(const std::function< double(double)> &lst, double lambda, double mu, unsigned c, const QsysResOptions &o=QsysResOptions())
Definition qsys_gimc.h:74
QsysResT< double > QsysRes
Definition qsys_res.h:96
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
The res struct the exact queueing-system functions return, and the numeric helpers those functions sh...
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
The name-value options shared by the exact functions: tPoints, percentiles and nmax (a negative nmax ...
Definition qsys_res.h:102
std::string analyzer
Definition qsys_res.h:59
void set(const std::string &name, const T &v)
Sets (or overwrites) a scalar field.
Definition qsys_res.h:62