LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mg1_fb.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_FB_H
6#define LINE_API_QSYS_QSYS_MG1_FB_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * M/G/1 under FB (feedback), also called LAS (least attained service).
12 *
13 * Templated port of matlab/src/api/qsys/qsys_mg1_fb.m, cross-checked against
14 * jar/src/main/java/jline/api/qsys/Qsys_mg1_fb.java.
15 *
16 * The job with the least attained service holds the server, so priority
17 * depends on age, not on the original or remaining size. For a job of size x
18 * (Wierman and Harchol-Balter, SIGMETRICS 2003, Sec. 3.3)
19 *
20 * rho_x = lambda int_0^x Fbar(t) dt
21 * num(x) = lambda int_0^x t Fbar(t) dt
22 * E[T(x)] = num(x)/(1-rho_x)^2 + x/(1-rho_x)
23 *
24 * with Fbar the tail of the mixture job-size law. MATLAB takes two paths:
25 *
26 * all cs_i = 1 (within 1e-6): the mixture is a mixture of exponentials, the
27 * two truncated integrals are closed forms, and the class mean is
28 * E[T_k] = int_0^{20/mu_k} E[T(x)] mu_k e^{-mu_k x} dx by quadrature;
29 * otherwise: the exact integral over the two-moment fit of each class size
30 * law (qsys_sizequad.h). The former path collapsed each class onto its
31 * mean size x = 1/mu_k and treated non-exponential classes as
32 * deterministic, which is not FB.
33 *
34 * The exponential path is reproduced verbatim, including the truncation of the outer
35 * integral at 20 mean service times, which is not an implementation detail:
36 * it biases the class mean low by the tail beyond 20 e-foldings, of relative
37 * order 1e-8, and a port that integrated to infinity would not reproduce the
38 * reference.
39 *
40 * ARITHMETIC. exp and the adaptive quadrature make this transcendental.
41 */
42
43#include <cstddef>
44#include <vector>
45
50#include "line/num/number.h"
51#include "line/util/error.h"
52
53namespace line {
54namespace qsys {
55
56namespace detail {
57
58/** E[T(x)] under FB for a mixture-of-exponentials job-size law. */
59template <class T>
60T mg1_fb_response(const T& x, const std::vector<T>& mu, const std::vector<T>& p,
61 const T& lambda_total) {
62 const T one = num_traits<T>::from_int(1);
63 T rho_x = num_traits<T>::from_int(0), numer = num_traits<T>::from_int(0);
64 for (std::size_t i = 0; i < mu.size(); ++i) {
65 const T e = num_exp(T(-mu[i] * x));
66 rho_x += p[i] * lambda_total * ((one - e) / mu[i]);
67 numer += p[i] * lambda_total * ((one - e * (one + mu[i] * x)) / (mu[i] * mu[i]));
68 }
69 if (rho_x >= one)
70 throw NumericError(ErrorKind{"Instability"}, "qsys_mg1_fb: truncated load reaches one, the mean is infinite");
71 return numer / ((one - rho_x) * (one - rho_x)) + x / (one - rho_x);
72}
73
74} // namespace detail
75
76/**
77 * @brief M/G/1 under FB (feedback), also called LAS (least attained service).
78 *
79 * @param lambda per-class arrival rates
80 * @param mu per-class service rates
81 * @param cs per-class coefficients of variation of the service time
82 */
83template <class T>
84Mg1DisciplineResult<T> qsys_mg1_fb(const std::vector<T>& lambda, const std::vector<T>& mu,
85 const std::vector<T>& cs) {
87 "qsys_mg1_fb requires transcendental arithmetic");
88 detail::mg1_discipline_check("qsys_mg1_fb", lambda, mu, cs);
89 const std::size_t K = lambda.size();
90 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
91 const T two = num_traits<T>::from_int(2);
92 const T cs_tol = T(num_traits<T>::from_double(1e-6));
93
94 bool all_exp = true;
95 for (std::size_t i = 0; i < K; ++i)
96 if (!(num_abs(T(cs[i] - one)) < cs_tol)) all_exp = false;
97
99 r.W.assign(K, zero);
100
101 if (all_exp) {
102 T lambda_total = zero;
103 for (const T& v : lambda) lambda_total += v;
104 std::vector<T> p(K);
105 for (std::size_t i = 0; i < K; ++i) p[i] = lambda[i] / lambda_total;
106 const T reltol = T(num_traits<T>::from_double(1e-8));
107 const T abstol = T(num_traits<T>::from_double(1e-10));
108 for (std::size_t k = 0; k < K; ++k) {
109 const T mu_k = mu[k];
110 const T x_max = num_traits<T>::from_int(20) / mu_k;
111 r.W[k] = detail::num_integral<T>(
112 [&](const T& x) {
113 return detail::mg1_fb_response(x, mu, p, lambda_total) * mu_k *
114 detail::num_exp(T(-mu_k * x));
115 },
116 zero, x_max, reltol, abstol);
117 }
118 } else {
119 // General service: exact over the two-moment fit of each class size
120 // law (MATLAB private/qsys_mg1_sizequad.m), evaluated in double.
121 std::vector<double> l(K), m(K), c(K);
122 for (std::size_t i = 0; i < K; ++i) {
123 l[i] = num_traits<T>::to_double(lambda[i]);
124 m[i] = num_traits<T>::to_double(mu[i]);
125 c[i] = num_traits<T>::to_double(cs[i]);
126 }
127 const std::vector<double> Wd = sizequad::solve(l, m, c, true);
128 for (std::size_t k = 0; k < K; ++k) r.W[k] = T(num_traits<T>::from_double(Wd[k]));
129 }
130 r.rhohat = detail::mg1_discipline_rhohat(lambda, r.W);
131 return r;
132}
133
134} // namespace qsys
135} // namespace line
136
137#endif // LINE_API_QSYS_QSYS_MG1_FB_H
NumericError(const std::string &what)
Definition error.h:112
The exception types the port throws.
std::vector< double > solve(const std::vector< double > &lambda, const std::vector< double > &mu, const std::vector< double > &cs, Mode mode)
Mg1DisciplineResult< T > qsys_mg1_fb(const std::vector< T > &lambda, const std::vector< T > &mu, const std::vector< T > &cs)
M/G/1 under FB (feedback), also called LAS (least attained service).
Definition qsys_mg1_fb.h:84
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
T num_abs(const T &v)
Definition number.h:184
Number-type abstraction for the templated API port.
M/G/1 under SETF (shortest elapsed time first), the non-preemptive counterpart of FB/LAS.
Adaptive quadrature for the qsys functions whose MATLAB originals call integral(),...
Per-class mean response time of an M/G/1 queue under FB, PSJF or SRPT for general job sizes.
Shared return type and arithmetic helpers for the templated qsys port.
std::vector< T > W
per-class mean response time
T rhohat
Q/(1+Q) with Q = sum_k lambda_k W_k.