LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mg1_busy.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_QSYS_MG1_BUSY_H
6#define LINE_API_QSYS_QSYS_MG1_BUSY_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Busy-period measures of the M/G/1 queue.
12 *
13 * Port of matlab/src/api/qsys/qsys_mg1_busy.m. The busy period BP has
14 *
15 * E[BP] = E[S]/(1-rho), E[BP^2] = E[S^2]/(1-rho)^3,
16 * E[BP^3] = E[S^3]/(1-rho)^4 + 3 lambda E[S^2]^2/(1-rho)^5,
17 *
18 * the number served in it has mean 1/(1-rho) and variance
19 * (rho(1-rho) + lambda^2 E[S^2])/(1-rho)^3, and given the service LST its own
20 * LST solves Takacs' equation b(s) = B(s + lambda - lambda b(s)), here by the
21 * reference's fixed-point iteration from b = 0.
22 *
23 * ARITHMETIC: double, as the reference.
24 */
25
26#include <cmath>
27#include <functional>
28#include <vector>
29
31#include "line/util/error.h"
32
33namespace line {
34namespace qsys {
35
36/** MATLAB's name-value options of qsys_mg1_busy; a negative ES3 means not given. */
38 double ES3 = -1.0; ///< third moment of the service time
39 std::function<double(double)> lst; ///< the service LST at real arguments
40 std::vector<double> sPoints; ///< where to evaluate the busy-period LST
41};
42
43/**
44 * @brief Busy-period measures of the M/G/1 queue.
45 *
46 * @param lambda arrival rate
47 * @param mu service rate
48 * @param cs coefficient of variation of the service time
49 * @param o ES3, lst and sPoints options
50 */
51inline QsysRes qsys_mg1_busy(double lambda, double mu, double cs, const Mg1BusyOptions& o = Mg1BusyOptions()) {
52 if (!(lambda > 0.0) || !(mu > 0.0) || !(cs >= 0.0))
53 throw InputError(ErrorKind("Argument"),
54 "qsys_mg1_busy: lambda and mu must be positive scalars and cs non-negative");
55 const double rho = lambda / mu;
56 if (rho >= 1.0) resdetail::throw_unstable("qsys_mg1_busy", "rho", rho);
57 const double ES = 1.0 / mu;
58 const double ES2 = (1.0 + cs * cs) / (mu * mu);
59 const double EBP = ES / (1.0 - rho);
60 const double EBP2 = ES2 / std::pow(1.0 - rho, 3);
61 QsysRes r;
62 r.set("meanBusyPeriod", EBP);
63 r.set("varBusyPeriod", EBP2 - EBP * EBP);
64 if (o.ES3 >= 0.0)
65 r.set("thirdMomentBusyPeriod", o.ES3 / std::pow(1.0 - rho, 4) + 3.0 * lambda * ES2 * ES2 / std::pow(1.0 - rho, 5));
66 r.set("meanServed", 1.0 / (1.0 - rho));
67 r.set("varServed", (rho * (1.0 - rho) + lambda * lambda * ES2) / std::pow(1.0 - rho, 3));
68 r.set("meanIdlePeriod", 1.0 / lambda);
69 r.set("busyFraction", rho);
70 r.set("busyPeriodRate", lambda * (1.0 - rho));
71 if (!o.sPoints.empty()) {
72 if (!o.lst)
73 throw InputError(ErrorKind("Argument"), "qsys_mg1_busy: sPoints needs the service LST (option 'lst')");
74 for (double s : o.sPoints)
75 if (s < 0.0) throw InputError(ErrorKind("Argument"), "qsys_mg1_busy: sPoints must be non-negative");
76 std::vector<double> v(o.sPoints.size());
77 for (std::size_t k = 0; k < o.sPoints.size(); ++k) {
78 const double s = o.sPoints[k];
79 double b = 0.0;
80 for (int it = 1; it <= 100000; ++it) {
81 const double bn = o.lst(s + lambda - lambda * b);
82 if (std::fabs(bn - b) <= 1e-14) {
83 b = bn;
84 break;
85 }
86 b = bn;
87 }
88 v[k] = b;
89 }
90 r.set("sPoints", o.sPoints);
91 r.set("busyPeriodLst", v);
92 }
93 r.analyzer = "exact:M/G/1-busy";
94 return r;
95}
96
97} // namespace qsys
98} // namespace line
99
100#endif // LINE_API_QSYS_QSYS_MG1_BUSY_H
InputError(const std::string &what)
Definition error.h:105
The exception types the port throws.
void throw_unstable(const char *fn, const char *what, double v)
A LINE:input:Unstable refusal "fn: unstable system: <what> = %g >= 1".
Definition qsys_res.h:213
QsysResT< double > QsysRes
Definition qsys_res.h:96
QsysRes qsys_mg1_busy(double lambda, double mu, double cs, const Mg1BusyOptions &o=Mg1BusyOptions())
Busy-period measures of the M/G/1 queue.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:52
The res struct the exact queueing-system functions return, and the numeric helpers those functions sh...
ErrorKind(std::string n)
Definition error.h:39
MATLAB's name-value options of qsys_mg1_busy; a negative ES3 means not given.
std::function< double(double)> lst
the service LST at real arguments
double ES3
third moment of the service time
std::vector< double > sPoints
where to evaluate the busy-period LST
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