LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Toggle main menu visibility
Loading...
Searching...
No Matches
qsys_mg1_prio.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_PRIO_H
6
#define LINE_API_QSYS_QSYS_MG1_PRIO_H
7
8
/**
9
* @file
10
* @ingroup api_qsys
11
* M/G/1 with non-preemptive head-of-line priorities: per-class mean response
12
* times from the Cobham/Kleinrock formula.
13
*
14
* Templated port of matlab/src/api/qsys/qsys_mg1_prio.m, cross-checked against
15
* jar/src/main/java/jline/api/qsys/Qsys_mg1_prio.java (identical, including
16
* the rhohat convention below).
17
*
18
* rho_i = lambda_i/mu_i
19
* B_0 = (1/2) sum_i lambda_i (1 + cs_i^2)/mu_i^2 the mean work left
20
* Wq_k = B_0 / [ (1 - sum_{i<k} rho_i)(1 - sum_{i<=k} rho_i) ]
21
* W_k = Wq_k + 1/mu_k
22
*
23
* Class 1 is the highest priority. The returned rhohat is Q/(1+Q) with
24
* Q = sum_k lambda_k W_k, the qsys family convention, not the utilization:
25
* MATLAB computes the utilization into the same output variable and then
26
* overwrites it, so the utilization is not observable from the return value.
27
*
28
* Only squares of the coefficients of variation appear, so the whole
29
* computation is a rational function of the inputs and exact for
30
* T = Rational.
31
*/
32
33
#include <cstddef>
34
#include <vector>
35
36
#include "
line/api/qsys/qsys_res.h
"
37
#include "
line/api/qsys/qsys_types.h
"
38
#include "
line/num/number.h
"
39
#include "
line/util/error.h
"
40
41
namespace
line
{
42
namespace
qsys
{
43
44
template
<
class
T>
45
struct
Mg1PrioResult
{
46
std::vector<T>
W
;
///< per-class mean response time, class 1 first
47
std::vector<T>
Wq
;
///< per-class mean waiting time W - 1/mu (MATLAB res.meanWaitingTime)
48
T
rhohat
;
///< Q/(1+Q) with Q = sum_k lambda_k W_k
49
};
50
51
/**
52
* @brief M/G/1 with non-preemptive head-of-line priorities: per-class mean
53
* response times from the Cobham/Kleinrock formula.
54
*
55
* @param lambda per-class arrival rates, highest priority first
56
* @param mu per-class service rates
57
* @param cs per-class coefficients of variation of the service time
58
*/
59
template
<
class
T>
60
Mg1PrioResult<T>
qsys_mg1_prio
(
const
std::vector<T>& lambda,
const
std::vector<T>& mu,
61
const
std::vector<T>& cs) {
62
const
std::size_t K = lambda.size();
63
if
(mu.size() != K || cs.size() != K)
64
throw
InputError
(
"qsys_mg1_prio: lambda, mu and cs must have the same length"
);
65
if
(K == 0)
throw
InputError
(
"qsys_mg1_prio: at least one class is required"
);
66
const
T zero =
num_traits<T>::from_int
(0), one =
num_traits<T>::from_int
(1);
67
const
T two =
num_traits<T>::from_int
(2);
68
for
(std::size_t i = 0; i < K; ++i)
69
if
(lambda[i] <= zero || mu[i] <= zero || cs[i] <= zero)
70
throw
InputError
(
"qsys_mg1_prio: lambda, mu and cs must all be positive"
);
71
72
std::vector<T> rho_i(K);
73
T rho = zero;
74
for
(std::size_t i = 0; i < K; ++i) {
75
rho_i[i] = lambda[i] / mu[i];
76
rho += rho_i[i];
77
}
78
if
(rho >= one)
throw
InputError
(
"qsys_mg1_prio: system is unstable, rho >= 1"
);
79
80
T B_0 = zero;
81
for
(std::size_t i = 0; i < K; ++i)
82
B_0 += lambda[i] * (one + cs[i] * cs[i]) / (mu[i] * mu[i]);
83
B_0 /= two;
84
85
Mg1PrioResult<T>
r;
86
r.
W
.resize(K);
87
r.
Wq
.resize(K);
88
T rho_prev = zero;
89
for
(std::size_t k = 0; k < K; ++k) {
90
const
T rho_curr = rho_prev + rho_i[k];
91
r.
Wq
[k] = B_0 / ((one - rho_prev) * (one - rho_curr));
92
r.
W
[k] = r.
Wq
[k] + one / mu[k];
93
rho_prev = rho_curr;
94
}
95
T Q = zero;
96
for
(std::size_t k = 0; k < K; ++k) Q += lambda[k] * r.
W
[k];
97
r.
rhohat
= Q / (one + Q);
98
return
r;
99
}
100
101
/**
102
* @brief The `res` output of qsys_mg1_prio (Cobham's HOL priority M/G/1).
103
*
104
* Port of the `nargout > 2` branch of matlab/src/api/qsys/qsys_mg1_prio.m:
105
* per-class sojourn, waiting and queue length, the total utilization, and with
106
* `ES3` (one third service moment per class; empty for MATLAB's []) the
107
* per-class sojourn-time variance (Takacs 1964).
108
*/
109
inline
QsysRes
qsys_mg1_prio_res
(
const
std::vector<double>& lambda,
const
std::vector<double>& mu,
110
const
std::vector<double>& cs,
111
const
std::vector<double>& ES3 = std::vector<double>()) {
112
const
Mg1PrioResult<double>
base =
qsys_mg1_prio
(lambda, mu, cs);
113
const
std::size_t K = lambda.size();
114
std::vector<double> rho_i(K), Q(K);
115
double
rhoTot = 0.0;
116
for
(std::size_t k = 0; k < K; ++k) {
117
rho_i[k] = lambda[k] / mu[k];
118
rhoTot += rho_i[k];
119
Q[k] = lambda[k] * base.
W
[k];
120
}
121
QsysRes
res;
122
res.
set
(
"meanSojournTime"
, base.
W
);
123
res.
set
(
"meanWaitingTime"
, base.
Wq
);
124
res.
set
(
"meanQueueLength"
, Q);
125
res.
set
(
"utilization"
, rhoTot);
126
if
(!ES3.empty()) {
127
if
(ES3.size() != K)
throw
Error
(
"qsys_mg1_prio: ES3 must have one entry per class"
);
128
std::vector<double> ES2(K), sig(K + 1, 0.0), M2(K + 1, 0.0), V(K);
129
double
lES2 = 0.0, lES3 = 0.0;
130
for
(std::size_t k = 0; k < K; ++k) {
131
ES2[k] = (1.0 + cs[k] * cs[k]) / (mu[k] * mu[k]);
132
lES2 += lambda[k] * ES2[k];
133
lES3 += lambda[k] * ES3[k];
134
sig[k + 1] = sig[k] + rho_i[k];
135
M2[k + 1] = M2[k] + lambda[k] * ES2[k];
136
}
137
for
(std::size_t k = 0; k < K; ++k) {
138
const
double
a = 1.0 - sig[k], b = 1.0 - sig[k + 1];
139
V[k] = (ES2[k] - 1.0 / (mu[k] * mu[k])) + lES3 / (3.0 * a * a * b) +
140
lES2 * (2.0 * M2[k + 1] - lES2) / (4.0 * a * a * b * b) + lES2 * M2[k] / (2.0 * a * a * a * b);
141
}
142
res.
set
(
"varSojournTime"
, V);
143
}
144
res.
analyzer
=
"exact:M/G/1-prio-hol"
;
145
return
res;
146
}
147
148
}
// namespace qsys
149
}
// namespace line
150
151
#endif
// LINE_API_QSYS_QSYS_MG1_PRIO_H
line::Error::Error
Error(const std::string &what)
Definition
error.h:75
line::InputError::InputError
InputError(const std::string &what)
Definition
error.h:105
error.h
The exception types the port throws.
line::qsys
Definition
qsys_bmapm1.h:58
line::qsys::qsys_mg1_prio_res
QsysRes qsys_mg1_prio_res(const std::vector< double > &lambda, const std::vector< double > &mu, const std::vector< double > &cs, const std::vector< double > &ES3=std::vector< double >())
The res output of qsys_mg1_prio (Cobham's HOL priority M/G/1).
Definition
qsys_mg1_prio.h:109
line::qsys::QsysRes
QsysResT< double > QsysRes
Definition
qsys_res.h:96
line::qsys::qsys_mg1_prio
Mg1PrioResult< T > qsys_mg1_prio(const std::vector< T > &lambda, const std::vector< T > &mu, const std::vector< T > &cs)
M/G/1 with non-preemptive head-of-line priorities: per-class mean response times from the Cobham/Klei...
Definition
qsys_mg1_prio.h:60
line
Conservation laws of a layered queueing network, enumerated from its structure.
Definition
aoi_dist2ph.h:51
number.h
Number-type abstraction for the templated API port.
qsys_res.h
The res struct the exact queueing-system functions return, and the numeric helpers those functions sh...
qsys_types.h
Shared return type and arithmetic helpers for the templated qsys port.
line::num_traits
Definition
number.h:123
line::qsys::Mg1PrioResult
Definition
qsys_mg1_prio.h:45
line::qsys::Mg1PrioResult::Wq
std::vector< T > Wq
per-class mean waiting time W - 1/mu (MATLAB res.meanWaitingTime)
Definition
qsys_mg1_prio.h:47
line::qsys::Mg1PrioResult::rhohat
T rhohat
Q/(1+Q) with Q = sum_k lambda_k W_k.
Definition
qsys_mg1_prio.h:48
line::qsys::Mg1PrioResult::W
std::vector< T > W
per-class mean response time, class 1 first
Definition
qsys_mg1_prio.h:46
line::qsys::QsysResT::analyzer
std::string analyzer
Definition
qsys_res.h:59
line::qsys::QsysResT::set
void set(const std::string &name, const T &v)
Sets (or overwrites) a scalar field.
Definition
qsys_res.h:62
include
line
api
qsys
qsys_mg1_prio.h
Generated by
1.18.0