LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mm1.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_MM1_H
6#define LINE_API_QSYS_MM1_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Exact mean response time of the M/M/1 queue.
12 *
13 * Templated port of matlab/src/api/qsys/qsys_mm1.m, cross-checked against
14 * jar/src/main/java/jline/api/qsys/Qsys_mm1.java (identical).
15 *
16 * rho = lambda/mu, W = rho/(1-rho)/lambda
17 *
18 * Pure field arithmetic, so the function is exact for T = Rational.
19 */
20
23#include "line/num/number.h"
24
25namespace line {
26namespace qsys {
27
28/**
29 * @brief Exact mean response time of the M/M/1 queue.
30 *
31 * @param lambda arrival rate
32 * @param mu service rate
33 * @return W = mean response time, rhohat = rho (MATLAB returns the true rho here)
34 */
35template <class T>
36QsysResult<T> qsys_mm1(const T& lambda, const T& mu) {
37 const T one = num_traits<T>::from_int(1);
38 const T rho = lambda / mu;
39 if (detail::saturated(rho, "qsys_mm1")) return {detail::num_inf<T>(), rho};
40 const T W = rho / (one - rho) / lambda;
41 return {W, rho};
42}
43
44/**
45 * @brief The `res` output of qsys_mm1: the full M/M/1 measure set.
46 *
47 * Port of the `nargout > 2` branch of matlab/src/api/qsys/qsys_mm1.m: queue
48 * length and waiting/sojourn moments, the geometric queue-length law to
49 * `nmax`, busy and idle periods, and with `tPoints`/`percentiles` the
50 * waiting/sojourn CDFs, the busy-period density and the percentiles.
51 * Refuses an unstable queue with `LINE:input:Unstable`.
52 */
53inline QsysRes qsys_mm1_res(double lambda, double mu, const QsysResOptions& o = QsysResOptions()) {
54 using namespace resdetail;
55 const double rho = lambda / mu;
56 require_stable(rho, "qsys_mm1");
57 const double ET = 1.0 / (mu * (1.0 - rho));
58 QsysRes res;
59 res.set("meanQueueLength", rho / (1.0 - rho));
60 res.set("varQueueLength", rho / std::pow(1.0 - rho, 2));
61 res.set("meanQueueLengthQ", rho * rho / (1.0 - rho));
62 res.set("varQueueLengthQ", rho * rho * (1.0 + rho - rho * rho) / std::pow(1.0 - rho, 2));
63 res.set("meanWaitingTime", rho * ET);
64 res.set("varWaitingTime", rho * (2.0 - rho) / (mu * mu * std::pow(1.0 - rho, 2)));
65 res.set("meanSojournTime", ET);
66 res.set("varSojournTime", ET * ET);
67 res.set("probWait", rho);
68 res.set("condMeanWaitingTime", ET);
69 res.set("throughput", lambda);
70 res.set("utilization", rho);
71 long nmax = o.nmax;
72 if (nmax < 0) nmax = std::max(1L, static_cast<long>(std::ceil(std::log(1e-14) / std::log(rho))));
73 res.set("queueLengthDist", geometric(1.0 - rho, rho, nmax));
74 res.set("meanBusyPeriod", (1.0 / mu) / (1.0 - rho));
75 res.set("varBusyPeriod", (1.0 + rho) / (mu * mu * std::pow(1.0 - rho, 3)));
76 res.set("meanIdlePeriod", 1.0 / lambda);
77 if (!o.tPoints.empty()) {
78 const std::size_t n = o.tPoints.size();
79 std::vector<double> wc(n), sc(n), f(n);
80 for (std::size_t i = 0; i < n; ++i) {
81 const double tp = o.tPoints[i], t = std::max(tp, 0.0);
82 const double ind = tp >= 0.0 ? 1.0 : 0.0;
83 wc[i] = ind * (1.0 - rho * std::exp(-t / ET));
84 sc[i] = ind * (1.0 - std::exp(-t / ET));
85 const double x = 2.0 * t * std::sqrt(lambda * mu);
86 if (tp < 0.0) f[i] = 0.0;
87 else if (t == 0.0) f[i] = mu; // lim_{t->0} f(t) = mu
88 else f[i] = std::sqrt(mu / lambda) / t * besseli1_scaled(x) * std::exp(x - (lambda + mu) * t);
89 }
90 res.set("tPoints", o.tPoints);
91 res.set("waitingTimeCdf", wc);
92 res.set("sojournTimeCdf", sc);
93 res.set("busyPeriodPdf", f);
94 }
95 if (!o.percentiles.empty()) {
96 const std::size_t n = o.percentiles.size();
97 std::vector<double> sp(n), wp(n);
98 for (std::size_t i = 0; i < n; ++i) {
99 const double p = o.percentiles[i];
100 sp[i] = ET * std::log(100.0 / (100.0 - p));
101 wp[i] = std::max(0.0, ET * std::log(100.0 * rho / (100.0 - p)));
102 }
103 res.set("percentiles", o.percentiles);
104 res.set("sojournTimePercentile", sp);
105 res.set("waitingTimePercentile", wp);
106 }
107 res.analyzer = "exact:M/M/1";
108 return res;
109}
110
111} // namespace qsys
112} // namespace line
113
114#endif // LINE_API_QSYS_MM1_H
QsysResult< T > qsys_mm1(const T &lambda, const T &mu)
Exact mean response time of the M/M/1 queue.
Definition qsys_mm1.h:36
QsysResT< double > QsysRes
Definition qsys_res.h:96
QsysRes qsys_mm1_res(double lambda, double mu, const QsysResOptions &o=QsysResOptions())
The res output of qsys_mm1: the full M/M/1 measure set.
Definition qsys_mm1.h:53
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