LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_gm1.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_GM1_H
6#define LINE_API_QSYS_GM1_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Exact mean response time of the G/M/1 queue.
12 *
13 * Templated port of matlab/src/api/qsys/qsys_gm1.m, cross-checked against
14 * jar/src/main/java/jline/api/qsys/Qsys_gm1.java. The two agree on W; the JAR
15 * additionally reports rhohat = 0, whereas MATLAB returns W alone. The port
16 * follows MATLAB and returns the single value.
17 *
18 * W = 1/(1-sigma)/mu
19 *
20 * sigma is the load seen at arrival instants, i.e. the root in (0,1) of
21 * sigma = A*(mu(1-sigma)) with A* the Laplace transform of the interarrival
22 * time. It is an input here, so the function itself is pure field arithmetic
23 * and exact for T = Rational.
24 */
25
28#include "line/num/number.h"
29
30namespace line {
31namespace qsys {
32
33/**
34 * @brief Exact mean response time of the G/M/1 queue.
35 *
36 * @param sigma load at arrival instants, 0 <= sigma < 1
37 * @param mu service rate
38 * @return mean response time W
39 */
40template <class T>
41T qsys_gm1(const T& sigma, const T& mu) {
42 const T one = num_traits<T>::from_int(1);
43 detail::require_no_pole(T(one - sigma), "qsys_gm1");
44 return one / (one - sigma) / mu;
45}
46
47/**
48 * @brief The `res` output of qsys_gm1: the GI/M/1 measure set from the root sigma.
49 *
50 * Port of the `nargout > 1` branch of matlab/src/api/qsys/qsys_gm1.m. The
51 * queue-length measures need the arrival rate and are set only when `lambda`
52 * is given (pass a negative value for MATLAB's []); `tPoints`/`percentiles` add
53 * the waiting/sojourn CDFs and percentiles. `o.nmax` is ignored, as the
54 * reference has no such option here.
55 */
56inline QsysRes qsys_gm1_res(double sigma, double mu, double lambda = -1.0,
57 const QsysResOptions& o = QsysResOptions()) {
58 using namespace resdetail;
59 if (sigma <= 0.0 || sigma >= 1.0) throw Error("qsys_gm1: sigma must lie in (0, 1)");
60 const double W = 1.0 / (1.0 - sigma) / mu;
61 const double theta = mu * (1.0 - sigma);
62 const long nmax = std::max(1L, static_cast<long>(std::ceil(std::log(1e-14) / std::log(sigma))));
63 QsysRes res;
64 res.set("meanWaitingTime", sigma / theta);
65 res.set("varWaitingTime", sigma * (2.0 - sigma) / (theta * theta));
66 res.set("meanSojournTime", W);
67 res.set("varSojournTime", 1.0 / (theta * theta));
68 res.set("probWait", sigma);
69 res.set("condMeanWaitingTime", 1.0 / theta);
70 res.set("arrivalDist", geometric(1.0 - sigma, sigma, nmax));
71 if (lambda >= 0.0) {
72 const double rho = lambda / mu;
73 res.set("meanQueueLength", rho / (1.0 - sigma));
74 res.set("varQueueLength", rho * (1.0 + sigma - rho) / std::pow(1.0 - sigma, 2));
75 res.set("meanQueueLengthQ", rho * sigma / (1.0 - sigma));
76 res.set("varQueueLengthQ", rho * sigma * (1.0 + sigma - rho * sigma) / std::pow(1.0 - sigma, 2));
77 res.set("throughput", lambda);
78 res.set("utilization", rho);
79 std::vector<double> ql{1.0 - rho};
80 for (long n = 0; n <= nmax - 1; ++n)
81 ql.push_back(rho * (1.0 - sigma) * std::pow(sigma, static_cast<double>(n)));
82 res.set("queueLengthDist", ql);
83 }
84 if (!o.tPoints.empty()) {
85 std::vector<double> wc(o.tPoints.size()), sc(o.tPoints.size());
86 for (std::size_t i = 0; i < wc.size(); ++i) {
87 const double tp = o.tPoints[i], t = std::max(tp, 0.0), ind = tp >= 0.0 ? 1.0 : 0.0;
88 wc[i] = ind * (1.0 - sigma * std::exp(-theta * t));
89 sc[i] = ind * (1.0 - std::exp(-theta * t));
90 }
91 res.set("tPoints", o.tPoints);
92 res.set("waitingTimeCdf", wc);
93 res.set("sojournTimeCdf", sc);
94 }
95 if (!o.percentiles.empty()) {
96 std::vector<double> wp(o.percentiles.size()), sp(o.percentiles.size());
97 for (std::size_t i = 0; i < wp.size(); ++i) {
98 const double p = o.percentiles[i];
99 wp[i] = std::max(0.0, std::log(100.0 * sigma / (100.0 - p)) / theta);
100 sp[i] = std::log(100.0 / (100.0 - p)) / theta;
101 }
102 res.set("percentiles", o.percentiles);
103 res.set("waitingTimePercentile", wp);
104 res.set("sojournTimePercentile", sp);
105 }
106 res.analyzer = "exact:GI/M/1";
107 return res;
108}
109
110} // namespace qsys
111} // namespace line
112
113#endif // LINE_API_QSYS_GM1_H
Error(const std::string &what)
Definition error.h:75
T qsys_gm1(const T &sigma, const T &mu)
Exact mean response time of the G/M/1 queue.
Definition qsys_gm1.h:41
QsysResT< double > QsysRes
Definition qsys_res.h:96
QsysRes qsys_gm1_res(double sigma, double mu, double lambda=-1.0, const QsysResOptions &o=QsysResOptions())
The res output of qsys_gm1: the GI/M/1 measure set from the root sigma.
Definition qsys_gm1.h:56
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