LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mg1.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_MG1_H
6#define LINE_API_QSYS_MG1_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Exact mean response time of the M/G/1 queue (Pollaczek-Khinchine).
12 *
13 * Templated port of matlab/src/api/qsys/qsys_mg1.m, cross-checked against
14 * jar/src/main/java/jline/api/qsys/Qsys_mg1.java (identical).
15 *
16 * rho = lambda/mu
17 * Q = rho + rho^2/(2(1-rho)) + lambda^2 cs^2 / mu^2 / (2(1-rho))
18 * W = Q/lambda, rhohat = Q/(1+Q)
19 *
20 * Only integer powers appear, so the function is exact for T = Rational.
21 * Note that cs is the coefficient of variation, squared inside the formula.
22 */
23
26#include "line/num/number.h"
27
28namespace line {
29namespace qsys {
30
31/**
32 * @brief Exact mean response time of the M/G/1 queue (Pollaczek-Khinchine).
33 *
34 * @param lambda arrival rate
35 * @param mu service rate
36 * @param cs coefficient of variation of the service time
37 * @return W = mean response time, rhohat = Q/(1+Q)
38 */
39template <class T>
40QsysResult<T> qsys_mg1(const T& lambda, const T& mu, const T& cs) {
41 const T one = num_traits<T>::from_int(1);
42 const T two = num_traits<T>::from_int(2);
43 const T rho = lambda / mu;
44 if (detail::saturated(rho, "qsys_mg1")) return {detail::num_inf<T>(), one};
45 const T Q = rho + num_pow_int(rho, 2) / (two * (one - rho)) +
46 num_pow_int(lambda, 2) * num_pow_int(cs, 2) / num_pow_int(mu, 2) /
47 (two * (one - rho));
48 const T W = Q / lambda;
49 return {W, Q / (one + Q)};
50}
51
52/**
53 * @brief The `res` output of qsys_mg1 (Pollaczek-Khinchine and Takacs).
54 *
55 * Port of the `nargout > 2` branch of matlab/src/api/qsys/qsys_mg1.m. The
56 * variances need the third service moment and are set only when `ES3` is
57 * given (pass a negative value for MATLAB's []). Refuses an unstable queue with
58 * `LINE:input:Unstable`.
59 */
60inline QsysRes qsys_mg1_res(double lambda, double mu, double cs, double ES3 = -1.0) {
61 using namespace resdetail;
62 const double rho = lambda / mu;
63 require_stable(rho, "qsys_mg1");
64 const double ES = 1.0 / mu;
65 const double ES2 = (1.0 + cs * cs) / (mu * mu);
66 const double EW = lambda * ES2 / (2.0 * (1.0 - rho));
67 QsysRes res;
68 res.set("meanQueueLength", lambda * EW + rho);
69 res.set("meanQueueLengthQ", lambda * EW);
70 res.set("meanWaitingTime", EW);
71 res.set("meanSojournTime", EW + ES);
72 res.set("probWait", rho);
73 res.set("condMeanWaitingTime", EW / rho);
74 res.set("throughput", lambda);
75 res.set("utilization", rho);
76 if (ES3 >= 0.0) {
77 const double t3 = lambda * ES3 / (3.0 * (1.0 - rho));
78 const double VW = t3 + lambda * lambda * ES2 * ES2 / (4.0 * std::pow(1.0 - rho, 2));
79 const double h = lambda * lambda * ES2 / (2.0 * (1.0 - rho));
80 res.set("varWaitingTime", VW);
81 res.set("varSojournTime", VW + ES2 - ES * ES);
82 res.set("varQueueLengthQ", lambda * lambda * t3 + h * h + h);
83 res.set("varQueueLength", lambda * lambda * t3 + h * h +
84 lambda * lambda * (3.0 - 2.0 * rho) * ES2 / (2.0 * (1.0 - rho)) +
85 rho * (1.0 - rho));
86 }
87 res.analyzer = "exact:M/G/1";
88 return res;
89}
90
91} // namespace qsys
92} // namespace line
93
94#endif // LINE_API_QSYS_MG1_H
QsysRes qsys_mg1_res(double lambda, double mu, double cs, double ES3=-1.0)
The res output of qsys_mg1 (Pollaczek-Khinchine and Takacs).
Definition qsys_mg1.h:60
QsysResult< T > qsys_mg1(const T &lambda, const T &mu, const T &cs)
Exact mean response time of the M/G/1 queue (Pollaczek-Khinchine).
Definition qsys_mg1.h:40
QsysResT< double > QsysRes
Definition qsys_res.h:96
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
T num_pow_int(const T &base, unsigned e)
Integer power, valid in any field (no transcendental requirement).
Definition number.h:204
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.
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