LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_gig1_rq.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_GIG1_RQ_H
6#define LINE_API_QSYS_QSYS_GIG1_RQ_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Robust Queueing (RQ) approximation of a G/GI/1 queue characterized by its
12 * arrival index of dispersion and the first two service moments.
13 *
14 * Templated port of matlab/src/api/qsys/qsys_gig1_rq.m, cross-checked against
15 * jar/src/main/java/jline/api/qsys/Qsys_gig1_rq.java.
16 *
17 * The mean steady-state workload is the value of a one-dimensional variational
18 * problem (Whitt and You 2018, eqs. (13), (16)-(18)):
19 *
20 * Zstar = sup_{x>=0} [ -(1-rho) x + sqrt( 2 rho x (I_a(x) + c_s^2)/mu ) ]
21 * W = max(0, Zstar/rho - (c_s^2+1)/(2 mu))
22 * Q = lambda W the mean number waiting
23 * X = Q + rho the mean number in system
24 *
25 * The objective is unimodal in practice but not guaranteed to be, so MATLAB
26 * brackets it with a 200-point log-spaced scan over [1e-6, 1e8] and refines
27 * the best bracket with fminbnd at TolX 1e-10, keeping the better of the scan
28 * value and the refined value. The port keeps the same scan and refines with
29 * golden-section search on the same bracket at the same TolX. fminbnd is
30 * golden section with parabolic acceleration, so on a bracket containing a
31 * single interior maximum the two locate the same point to within TolX; and
32 * because MATLAB and the port both return max(scan, refined), a refinement
33 * that lands short can never fall below the scan value. Where the objective
34 * really is multimodal both are equally at the mercy of the scan, and neither
35 * claims a global optimum.
36 *
37 * ARITHMETIC. The square root and the tolerance-driven search make this
38 * transcendental.
39 *
40 * The arrival process enters only through the callable I_a(x), so the caller
41 * supplies whatever index-of-dispersion model applies; for a renewal arrival
42 * stream I_a is the constant c_a^2.
43 *
44 * At rho <= 0 all four measures are zero, as in MATLAB. At rho >= 1 MATLAB
45 * returns Inf, and so does every instantiation with an IEEE infinity (double,
46 * Real50); a type without one (Rational) raises instead.
47 */
48
49#include <cstddef>
50#include <limits>
51#include <vector>
52
54#include "line/num/number.h"
55#include "line/util/error.h"
56
57namespace line {
58namespace qsys {
59
60template <class T>
62 T Z; ///< mean steady-state workload E[Z]
63 T W; ///< mean steady-state waiting time E[W]
64 T Q; ///< mean number waiting, lambda W
65 T X; ///< mean number in system, Q + rho
66};
67
68/**
69 * @brief Robust Queueing (RQ) approximation of a G/GI/1 queue characterized
70 * by its arrival index of dispersion and the first two service moments.
71 *
72 * @param rho traffic intensity lambda/mu
73 * @param mu service rate
74 * @param cs2 squared coefficient of variation of the service time
75 * @param IaFun_ callable, IaFun_(x) -> the arrival IDC I_a(x) at x > 0
76 */
77template <class T, class IaFun>
78Gig1RqResult<T> qsys_gig1_rq(const T& rho, const T& mu, const T& cs2, IaFun&& IaFun_) {
80 "qsys_gig1_rq requires transcendental arithmetic");
81 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
82 const T two = num_traits<T>::from_int(2);
84 if (rho <= zero) {
85 r.Z = r.W = r.Q = r.X = zero;
86 return r;
87 }
88 if (rho >= one) {
89 if constexpr (std::numeric_limits<T>::has_infinity) {
90 r.Z = r.W = r.Q = r.X = std::numeric_limits<T>::infinity();
91 return r;
92 } else {
93 throw InputError("qsys_gig1_rq: rho >= 1 has an infinite workload, which this type cannot represent");
94 }
95 }
96 const T lambda = rho * mu;
97
98 // f(x) = -(1-rho) x + sqrt(2 rho x (I_a(x) + c_s^2)/mu), and f(x<=0) = 0.
99 auto f = [&](const T& x) -> T {
100 if (x <= zero) return zero;
101 const T ia = IaFun_(x);
102 T inner = two * rho * x * (ia + cs2) / mu;
103 if (inner < zero) inner = zero;
104 return -(one - rho) * x + detail::num_sqrt(inner);
105 };
106
107 // Coarse log-spaced scan over [1e-6, 1e8], 200 points, as in MATLAB.
108 const std::size_t NS = 200;
109 std::vector<T> xs(NS);
110 for (std::size_t i = 0; i < NS; ++i) {
111 const double e = -6.0 + 14.0 * static_cast<double>(i) / static_cast<double>(NS - 1);
112 xs[i] = T(num_traits<T>::from_double(std::pow(10.0, e)));
113 }
114 std::size_t imax = 0;
115 T best = f(xs[0]);
116 for (std::size_t i = 1; i < NS; ++i) {
117 const T v = f(xs[i]);
118 if (v > best) {
119 best = v;
120 imax = i;
121 }
122 }
123 T lo = xs[imax > 0 ? imax - 1 : 0];
124 T hi = xs[imax + 1 < NS ? imax + 1 : NS - 1];
125
126 // Golden-section refinement on the bracket at TolX 1e-10.
127 const T tolx = T(num_traits<T>::from_double(1e-10));
128 const T invphi = T(num_traits<T>::from_double(0.6180339887498949));
129 T c = hi - (hi - lo) * invphi;
130 T d = lo + (hi - lo) * invphi;
131 T fc = f(c), fd = f(d);
132 for (unsigned it = 0; it < 500u && hi - lo > tolx; ++it) {
133 if (fc > fd) {
134 hi = d;
135 d = c;
136 fd = fc;
137 c = hi - (hi - lo) * invphi;
138 fc = f(c);
139 } else {
140 lo = c;
141 c = d;
142 fc = fd;
143 d = lo + (hi - lo) * invphi;
144 fd = f(d);
145 }
146 }
147 const T refined = fc > fd ? fc : fd;
148 T Z = best > refined ? best : refined;
149 if (Z < zero) Z = zero;
150
151 r.Z = Z;
152 T W = Z / rho - (cs2 + one) / (two * mu);
153 if (W < zero) W = zero;
154 r.W = W;
155 r.Q = lambda * W;
156 r.X = r.Q + rho;
157 return r;
158}
159
160} // namespace qsys
161} // namespace line
162
163#endif // LINE_API_QSYS_QSYS_GIG1_RQ_H
InputError(const std::string &what)
Definition error.h:105
The exception types the port throws.
Gig1RqResult< T > qsys_gig1_rq(const T &rho, const T &mu, const T &cs2, IaFun &&IaFun_)
Robust Queueing (RQ) approximation of a G/GI/1 queue characterized by its arrival index of dispersion...
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Number-type abstraction for the templated API port.
Shared return type and arithmetic helpers for the templated qsys port.
T Q
mean number waiting, lambda W
T Z
mean steady-state workload E[Z]
T X
mean number in system, Q + rho
T W
mean steady-state waiting time E[W]