5#ifndef LINE_API_QSYS_QSYS_MMCK_H
6#define LINE_API_QSYS_QSYS_MMCK_H
72 if (lambda <= zero)
throw InputError(
"qsys_mmck: lambda must be a positive real scalar");
73 if (mu <= zero)
throw InputError(
"qsys_mmck: mu must be a positive real scalar");
74 if (c < 1)
throw InputError(
"qsys_mmck: c must be a positive integer");
75 if (K < c)
throw InputError(
"qsys_mmck: K must be an integer >= c");
77 const T a = lambda / mu;
80 std::vector<T> p(K + 1, zero);
83 for (
unsigned n = c; n <= K; ++n) p[n] = ac_over_cfact *
num_pow_int(rho, n - c);
86 for (
const T& v : p) S += v;
87 if (S <= zero)
throw NumericError(
ErrorKind{
"Degenerate"},
"qsys_mmck: stationary distribution failed to normalize");
88 for (T& v : p) v /= S;
92 T L = zero, Lq = zero;
93 for (
unsigned n = 0; n <= K; ++n) {
100 const T lambdaEff = lambda * (one - p[K]);
103 if (lambdaEff > zero) {
111 for (
unsigned n = 0; n <= K; ++n) EN2 += num_traits<T>::from_int(
static_cast<long>(n) * n) * p[n];
114 for (
unsigned n = 0; n < K; ++n) r.
arrivalDist[n] = p[n] / (one - p[K]);
117 T probWait = zero, EW2 = zero;
118 for (
unsigned nw = c; nw + 1 <= K; ++nw) {
119 const long m =
static_cast<long>(nw - c + 1);
123 EW2 /= theta * theta;
146 std::vector<double> Piw, m;
147 for (
unsigned nw = c; nw + 1 <= K; ++nw) {
148 Piw.push_back(Pi[nw]);
149 m.push_back(
static_cast<double>(nw - c + 1));
151 const double theta = c * mu;
152 const std::function<double(
double)> Wccdf = [Piw, m, theta](
double t) {
153 if (t < 0.0)
return 1.0;
155 for (std::size_t i = 0; i < m.size(); ++i) v += Piw[i] * gammainc_upper(theta * t, m[i]);
158 const std::function<double(
double)> Tccdf = [Pi, Piw, m, theta, mu, c](
double t) {
159 if (t < 0.0)
return 1.0;
161 for (std::size_t i = 0; i < std::min<std::size_t>(c, Pi.size()); ++i) s0 += Pi[i];
162 double v = s0 * std::exp(-mu * t);
163 if (m.empty())
return v;
165 for (std::size_t i = 0; i < m.size(); ++i) v += Piw[i] * gammainc_upper(mu * t, m[i] + 1.0);
167 const double d = theta - mu;
168 for (std::size_t i = 0; i < m.size(); ++i) {
169 const double lower = gammainc_lower(d * t, m[i]);
171 lower > 0.0 ? std::exp(m[i] * std::log(theta / d) - mu * t + std::log(lower)) : 0.0;
172 v += Piw[i] * (gammainc_upper(theta * t, m[i]) + cross);
178 if (!o.tPoints.empty()) {
179 res.
set(
"tPoints", o.tPoints);
180 res.
set(
"waitingTimeCdf", cdf_of(Wccdf, o.tPoints));
181 res.
set(
"sojournTimeCdf", cdf_of(Tccdf, o.tPoints));
183 if (!o.percentiles.empty()) {
184 for (
double p : o.percentiles)
185 if (p <= 0.0 || p >= 100.0)
throw Error(
"qsys_mmck: percentiles must lie in (0, 100)");
186 const double hiT = (K + 50.0) / std::min(mu, theta);
187 res.
set(
"percentiles", o.percentiles);
188 res.
set(
"waitingTimePercentile", pct_of(Wccdf, o.percentiles, hiT));
189 res.
set(
"sojournTimePercentile", pct_of(Tccdf, o.percentiles, hiT));
191 res.
analyzer =
"exact:M/M/" + std::to_string(c) +
"/" + std::to_string(K);
Error(const std::string &what)
NumericError(const std::string &what)
The exception types the port throws.
MmckResult< T > qsys_mmck(const T &lambda, const T &mu, unsigned c, unsigned K)
Exact analysis of the M/M/c/K queue (truncated Erlang form).
QsysResT< double > QsysRes
QsysRes qsys_mmck_dist(double lambda, double mu, unsigned c, unsigned K, const QsysResOptions &o=QsysResOptions())
The distributional part of qsys_mmck: waiting/sojourn CDFs and percentiles.
Conservation laws of a layered queueing network, enumerated from its structure.
T num_factorial(unsigned n)
Factorial as a value of T.
T num_pow_int(const T &base, unsigned e)
Integer power, valid in any field (no transcendental requirement).
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 <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
std::vector< T > queueLengthDist
p_0 ... p_K
T meanWaitingTime
Wq = Lq/lambda_eff.
T meanQueueLengthQ
Lq, mean number waiting.
T varWaitingTime
Var[W] from the Erlang(m, c mu) mixture.
std::vector< T > arrivalDist
Pi_n = p_n/(1-p_K), n = 0..K-1, seen by admitted arrivals.
T varSojournTime
Var[W] + 1/mu^2.
T meanQueueLength
L, mean number in system.
T utilization
per-server utilization lambda_eff/(c mu)
T varQueueLength
variance of the number in system
T probWait
P(an admitted arrival waits).
T meanSojournTime
W = L /lambda_eff.
T throughput
lambda_eff = lambda (1 - p_K)
T condMeanWaitingTime
E[W | W > 0], 0 when no arrival waits.
The name-value options shared by the exact functions: tPoints, percentiles and nmax (a negative nmax ...
void set(const std::string &name, const T &v)
Sets (or overwrites) a scalar field.