LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mmk.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_MMK_H
6#define LINE_API_QSYS_MMK_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Exact mean response time of the M/M/k queue (Erlang-C).
12 *
13 * Templated port of matlab/src/api/qsys/qsys_mmk.m, cross-checked against
14 * jar/src/main/java/jline/api/qsys/Qsys_mmk.java. The JAR accumulates the
15 * factorials incrementally and writes C = 1/(1+(1-rho)*(C*(C-1)!)/(C rho)^C*S),
16 * which is the same k! as MATLAB's factorial(k); the two agree.
17 *
18 * rho = lambda/(mu k)
19 * S = sum_{j=0}^{k-1} (k rho)^j / j!
20 * C = 1 / (1 + (1-rho) k! / (k rho)^k * S)
21 * Q = rho/(1-rho) C + k rho, W = Q/lambda
22 *
23 * Every exponent is an integer, so this stays in the field of the inputs and
24 * is exact for T = Rational.
25 */
26
29#include "line/num/number.h"
30#include "line/util/error.h"
31
32namespace line {
33namespace qsys {
34
35namespace detail {
36
37/**
38 * Erlang-C: the probability that an arriving customer finds all k servers
39 * busy. Argument rho is the per-server utilization lambda/(mu k).
40 *
41 * Computed from the Erlang B recursion B_j = A B_{j-1}/(j + A B_{j-1}),
42 * A = k rho, and C = B/(1 - rho(1-B)). The textbook sum of (k rho)^j/j!
43 * overflows to Inf/Inf = NaN in double from about k = 150; the recursion forms
44 * neither power nor factorial and is exact at T = Rational as well.
45 */
46template <class T>
47T erlang_c(unsigned k, const T& rho) {
48 const T one = num_traits<T>::from_int(1);
49 const T A = T(num_traits<T>::from_int(static_cast<long>(k)) * rho);
50 T B = one;
51 for (unsigned j = 1; j <= k; ++j)
52 B = T(A * B / T(num_traits<T>::from_int(static_cast<long>(j)) + A * B));
53 return T(B / T(one - rho * T(one - B)));
54}
55
56} // namespace detail
57
58/**
59 * @brief Exact mean response time of the M/M/k queue (Erlang-C).
60 *
61 * @param lambda arrival rate
62 * @param mu service rate of a single server
63 * @param k number of servers, k >= 1
64 * @return W = mean response time, rhohat = rho = lambda/(mu k)
65 */
66template <class T>
67QsysResult<T> qsys_mmk(const T& lambda, const T& mu, unsigned k) {
68 if (k == 0) throw InputError("qsys_mmk: k must be at least 1");
69 const T one = num_traits<T>::from_int(1);
70 const T rho = lambda / mu / num_traits<T>::from_int(static_cast<long>(k));
71 if (detail::saturated(rho, "qsys_mmk")) return {detail::num_inf<T>(), rho};
72 const T Q = rho / (one - rho) * detail::erlang_c(k, rho) +
73 num_traits<T>::from_int(static_cast<long>(k)) * rho;
74 const T W = Q / lambda;
75 return {W, rho};
76}
77
78/**
79 * @brief The `res` output of qsys_mmk: the full M/M/k measure set.
80 *
81 * Port of the `nargout > 2` branch of matlab/src/api/qsys/qsys_mmk.m: Erlang C
82 * by the stable Erlang B recursion, the queue-length and waiting moments, the
83 * log-space queue-length law to `nmax`, and with `tPoints`/`percentiles` the
84 * waiting/sojourn CDFs and percentiles (the sojourn one by bisection on the
85 * hypoexponential tail). Refuses an unstable queue with `LINE:input:Unstable`.
86 */
87inline QsysRes qsys_mmk_res(double lambda, double mu, unsigned k, const QsysResOptions& o = QsysResOptions()) {
88 using namespace resdetail;
89 const double kd = static_cast<double>(k);
90 const double rho = lambda / mu / kd;
91 require_stable(rho, "qsys_mmk");
92 const double A = lambda / mu, a = rho;
93 double B = 1.0;
94 for (unsigned j = 1; j <= k; ++j) B = A * B / (j + A * B);
95 const double C = kd * B / (kd - A * (1.0 - B));
96 const double theta = kd * mu * (1.0 - a);
97 const double EQ = C * a / (1.0 - a);
98 const double VQ = a * C * (1.0 + a - a * C) / std::pow(1.0 - a, 2);
99 const double EW = C / theta;
100 const double VW = C * (2.0 - C) / (theta * theta);
101 QsysRes res;
102 res.set("meanQueueLength", EQ + A);
103 res.set("varQueueLength", VQ + A * (1.0 + C));
104 res.set("meanQueueLengthQ", EQ);
105 res.set("varQueueLengthQ", VQ);
106 res.set("meanWaitingTime", EW);
107 res.set("varWaitingTime", VW);
108 res.set("meanSojournTime", EW + 1.0 / mu);
109 res.set("varSojournTime", VW + 1.0 / (mu * mu));
110 res.set("probWait", C);
111 res.set("condMeanWaitingTime", 1.0 / theta);
112 res.set("throughput", lambda);
113 res.set("utilization", a);
114 long nmax = o.nmax;
115 if (nmax < 0)
116 nmax = static_cast<long>(k) +
117 std::max(1L, static_cast<long>(std::ceil(std::log(1e-14 * (1.0 - a)) / std::log(a))));
118 std::vector<double> logp(static_cast<std::size_t>(nmax + 1)), lognorm;
119 for (long n = 0; n <= nmax; ++n) {
120 const double nd = static_cast<double>(n);
121 logp[static_cast<std::size_t>(n)] = nd * std::log(A) - gammaln(std::min(nd, kd) + 1.0) -
122 std::max(nd - kd, 0.0) * std::log(kd);
123 }
124 for (unsigned i = 0; i < k && i < logp.size(); ++i) lognorm.push_back(logp[i]);
125 lognorm.push_back(kd * std::log(A) - gammaln(kd + 1.0) - std::log(1.0 - a));
126 res.set("queueLengthDist", lognormalize(logp, lognorm));
127 const std::function<double(double)> Tccdf = [C, theta, mu](double t) {
128 if (t < 0.0) return 1.0;
129 if (std::fabs(theta - mu) > 1e-12 * mu) {
130 const double G = C * mu / (mu - theta);
131 return (1.0 - G) * std::exp(-mu * t) + G * std::exp(-theta * t);
132 }
133 return (1.0 + C * mu * t) * std::exp(-mu * t);
134 };
135 if (!o.tPoints.empty()) {
136 std::vector<double> wc(o.tPoints.size());
137 for (std::size_t i = 0; i < wc.size(); ++i) {
138 const double tp = o.tPoints[i];
139 wc[i] = (tp >= 0.0 ? 1.0 : 0.0) * (1.0 - C * std::exp(-theta * std::max(tp, 0.0)));
140 }
141 res.set("tPoints", o.tPoints);
142 res.set("waitingTimeCdf", wc);
143 res.set("sojournTimeCdf", cdf_of(Tccdf, o.tPoints));
144 }
145 if (!o.percentiles.empty()) {
146 std::vector<double> wp(o.percentiles.size());
147 for (std::size_t i = 0; i < wp.size(); ++i)
148 wp[i] = std::max(0.0, std::log(100.0 * C / (100.0 - o.percentiles[i])) / theta);
149 res.set("percentiles", o.percentiles);
150 res.set("waitingTimePercentile", wp);
151 res.set("sojournTimePercentile", pct_of(Tccdf, o.percentiles, 50.0 / std::min(mu, theta)));
152 }
153 res.analyzer = "exact:M/M/" + std::to_string(k);
154 return res;
155}
156
157} // namespace qsys
158} // namespace line
159
160#endif // LINE_API_QSYS_MMK_H
InputError(const std::string &what)
Definition error.h:105
The exception types the port throws.
QsysRes qsys_mmk_res(double lambda, double mu, unsigned k, const QsysResOptions &o=QsysResOptions())
The res output of qsys_mmk: the full M/M/k measure set.
Definition qsys_mmk.h:87
QsysResT< double > QsysRes
Definition qsys_res.h:96
QsysResult< T > qsys_mmk(const T &lambda, const T &mu, unsigned k)
Exact mean response time of the M/M/k queue (Erlang-C).
Definition qsys_mmk.h:67
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 res struct the exact queueing-system functions return, and the numeric helpers those functions sh...
Shared return type and arithmetic helpers for the templated qsys port.
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
Return value of the qsys family, mirroring MATLAB's [W,rhohat] and the JAR's Ret.qsys.
Definition qsys_types.h:38