LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mxg1.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_MXG1_H
6#define LINE_API_QSYS_QSYS_MXG1_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * M^X/G/1: the FCFS batch-Poisson queue with general service.
12 *
13 * Templated port of matlab/src/api/qsys/qsys_mxg1.m, cross-checked against
14 * jar/src/main/java/jline/api/qsys/Qsys_mxg1.java.
15 *
16 * With E[S] = 1/mu, E[S^2] = (1+cs^2)/mu^2, lambda = lambda_batch E[X] and
17 * rho = lambda E[S] (Haverkort 1998, eq. 5.31):
18 *
19 * Wq = lambda E[S^2] / (2(1-rho)) + (E[X^2]-E[X]) E[S] / (2 E[X] (1-rho))
20 * W = Wq + E[S], U = rho, Q = lambda W
21 *
22 * cs = 1 gives qsys_mxm1 and a batch size of 1 gives qsys_mg1. Every term is
23 * rational in the inputs, so this is exact for T = Rational. As for qsys_mxm1
24 * the three MATLAB batch-law forms are the entry points qsys_mxg1,
25 * qsys_mxg1_variance and qsys_mxg1_pmf.
26 */
27
28#include <cstddef>
29#include <vector>
30
31#include "line/num/number.h"
32#include "line/util/error.h"
33
34namespace line {
35namespace qsys {
36
37/** The four outputs [W, Wq, U, Q] of qsys_mxg1, qsys_mg1_vacation and qsys_mg1_breakdown. */
38template <class T>
40 T W; ///< mean response (sojourn) time of an arbitrary job
41 T Wq; ///< mean time in the system and not in service
42 T U; ///< server utilization (fraction of time doing work)
43 T Q; ///< mean number of jobs in the system
44};
45
46/**
47 * @brief M^X/G/1 from the first two raw moments of the batch size.
48 *
49 * @param lambda_batch batch arrival rate
50 * @param mu service rate of a job
51 * @param cs coefficient of variation of the service time
52 * @param E_X mean batch size (>= 1)
53 * @param E_X2 second raw moment of the batch size
54 */
55template <class T>
56MeanValueResult<T> qsys_mxg1(const T& lambda_batch, const T& mu, const T& cs, const T& E_X,
57 const T& E_X2) {
58 const T zero = num_traits<T>::from_int(0);
59 const T one = num_traits<T>::from_int(1);
60 const T two = num_traits<T>::from_int(2);
61 const T tol = num_traits<T>::from_rational(1, 1000000000000L);
62 if (!(lambda_batch > zero) || !(mu > zero) || cs < zero)
63 throw InputError(ErrorKind("Argument"), "qsys_mxg1 needs lambda_batch > 0, 0 < mu < Inf and cs >= 0");
64 if (E_X < one || E_X2 < E_X * E_X * (one - tol))
65 throw InputError(ErrorKind("Argument"),
66 "qsys_mxg1 needs a batch size of mean >= 1 with E[X^2] >= E[X]^2");
67 const T ES = one / mu;
68 const T ES2 = (one + cs * cs) / (mu * mu);
69 const T lambda = lambda_batch * E_X;
70 const T rho = lambda * ES;
71 if (rho >= one) throw NumericError(ErrorKind("Instability"), "qsys_mxg1: system is unstable, rho >= 1");
73 r.Wq = lambda * ES2 / (two * (one - rho)) + (E_X2 - E_X) * ES / (two * E_X * (one - rho));
74 r.W = r.Wq + ES;
75 r.U = rho;
76 r.Q = lambda * r.W;
77 return r;
78}
79
80/** Variance form: MATLAB's qsys_mxg1(..., Var_X, 'variance'). */
81template <class T>
82MeanValueResult<T> qsys_mxg1_variance(const T& lambda_batch, const T& mu, const T& cs, const T& E_X,
83 const T& Var_X) {
84 return qsys_mxg1(lambda_batch, mu, cs, E_X, T(Var_X + E_X * E_X));
85}
86
87/** Support-and-pmf form: MATLAB's qsys_mxg1(..., batch_sizes, pmf); the pmf is renormalized. */
88template <class T>
89MeanValueResult<T> qsys_mxg1_pmf(const T& lambda_batch, const T& mu, const T& cs,
90 const std::vector<T>& batch_sizes, const std::vector<T>& pmf) {
91 if (batch_sizes.size() != pmf.size())
92 throw InputError(ErrorKind("Argument"), "qsys_mxg1: batch sizes and pmf must have the same length");
93 if (batch_sizes.empty()) throw InputError(ErrorKind("Argument"), "qsys_mxg1: empty batch-size support");
94 const T zero = num_traits<T>::from_int(0);
95 T tot = zero;
96 for (const T& v : pmf) {
97 if (v < zero)
98 throw InputError(ErrorKind("Argument"),
99 "qsys_mxg1: the batch-size pmf must be nonnegative with a positive sum");
100 tot += v;
101 }
102 if (!(tot > zero))
103 throw InputError(ErrorKind("Argument"),
104 "qsys_mxg1: the batch-size pmf must be nonnegative with a positive sum");
105 T E_X = zero, E_X2 = zero;
106 for (std::size_t i = 0; i < pmf.size(); ++i) {
107 const T w = pmf[i] / tot;
108 E_X += batch_sizes[i] * w;
109 E_X2 += batch_sizes[i] * batch_sizes[i] * w;
110 }
111 return qsys_mxg1(lambda_batch, mu, cs, E_X, E_X2);
112}
113
114} // namespace qsys
115} // namespace line
116
117#endif // LINE_API_QSYS_QSYS_MXG1_H
InputError(const std::string &what)
Definition error.h:105
NumericError(const std::string &what)
Definition error.h:112
The exception types the port throws.
MeanValueResult< T > qsys_mxg1_pmf(const T &lambda_batch, const T &mu, const T &cs, const std::vector< T > &batch_sizes, const std::vector< T > &pmf)
Support-and-pmf form: MATLAB's qsys_mxg1(..., batch_sizes, pmf); the pmf is renormalized.
Definition qsys_mxg1.h:89
MeanValueResult< T > qsys_mxg1(const T &lambda_batch, const T &mu, const T &cs, const T &E_X, const T &E_X2)
M^X/G/1 from the first two raw moments of the batch size.
Definition qsys_mxg1.h:56
MeanValueResult< T > qsys_mxg1_variance(const T &lambda_batch, const T &mu, const T &cs, const T &E_X, const T &Var_X)
Variance form: MATLAB's qsys_mxg1(..., Var_X, 'variance').
Definition qsys_mxg1.h:82
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Number-type abstraction for the templated API port.
ErrorKind(std::string n)
Definition error.h:39
The four outputs [W, Wq, U, Q] of qsys_mxg1, qsys_mg1_vacation and qsys_mg1_breakdown.
Definition qsys_mxg1.h:39
T W
mean response (sojourn) time of an arbitrary job
Definition qsys_mxg1.h:40
T Q
mean number of jobs in the system
Definition qsys_mxg1.h:43
T Wq
mean time in the system and not in service
Definition qsys_mxg1.h:41
T U
server utilization (fraction of time doing work)
Definition qsys_mxg1.h:42