LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mg1_setf.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_SETF_H
6#define LINE_API_QSYS_QSYS_MG1_SETF_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * M/G/1 under SETF (shortest elapsed time first), the non-preemptive
12 * counterpart of FB/LAS.
13 *
14 * Templated port of matlab/src/api/qsys/qsys_mg1_setf.m, cross-checked against
15 * jar/src/main/java/jline/api/qsys/Qsys_mg1_setf.java.
16 *
17 * SETF ranks waiting jobs by attained service and never interrupts service,
18 * so every waiting job has attained service 0 and the FCFS tie break decides
19 * the order: SETF is FCFS, and the Pollaczek-Khinchine form
20 *
21 * W_k = lambda E[X^2] / (2 (1-rho)) + 1/mu_k
22 *
23 * is exact for any size law. The former body evaluated one job of mean size
24 * x_k = 1/mu_k under FB plus a residual penalty, which read 1.6033 on M/M/1
25 * (lambda = 1, mu = 2) against the exact 1.0.
26 *
27 * ARITHMETIC. Rational operations only; the transcendental gate is kept for
28 * API stability.
29 *
30 * The returned rhohat is Q/(1+Q) with Q = sum_k lambda_k W_k, the qsys family
31 * convention; the utilization sum_k rho_k is not part of the return value
32 * because MATLAB overwrites it.
33 */
34
35#include <cstddef>
36#include <vector>
37
39#include "line/num/number.h"
40#include "line/util/error.h"
41
42namespace line {
43namespace qsys {
44
45template <class T>
47 std::vector<T> W; ///< per-class mean response time
48 T rhohat; ///< Q/(1+Q) with Q = sum_k lambda_k W_k
49};
50
51namespace detail {
52
53/** Shared argument validation for the M/G/1 discipline family. */
54template <class T>
55T mg1_discipline_check(const char* fn, const std::vector<T>& lambda, const std::vector<T>& mu,
56 const std::vector<T>& cs) {
57 const std::size_t K = lambda.size();
58 if (mu.size() != K || cs.size() != K)
59 throw InputError(std::string(fn) + ": lambda, mu and cs must have the same length");
60 if (K == 0) throw InputError(std::string(fn) + ": at least one class is required");
61 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
62 T rho = zero;
63 for (std::size_t i = 0; i < K; ++i) {
64 if (lambda[i] <= zero || mu[i] <= zero)
65 throw InputError(std::string(fn) + ": lambda and mu must be positive");
66 if (cs[i] < zero) throw InputError(std::string(fn) + ": cs must be non-negative");
67 rho += lambda[i] / mu[i];
68 }
69 if (rho >= one) throw InputError(std::string(fn) + ": system is unstable, rho >= 1");
70 return rho;
71}
72
73/** rhohat = Q/(1+Q) with Q = sum_k lambda_k W_k. */
74template <class T>
75T mg1_discipline_rhohat(const std::vector<T>& lambda, const std::vector<T>& W) {
77 for (std::size_t k = 0; k < W.size(); ++k) Q += lambda[k] * W[k];
78 return Q / (num_traits<T>::from_int(1) + Q);
79}
80
81} // namespace detail
82
83/**
84 * @brief M/G/1 under SETF (shortest elapsed time first), the non-preemptive
85 * counterpart of FB/LAS.
86 *
87 * @param lambda per-class arrival rates
88 * @param mu per-class service rates
89 * @param cs per-class coefficients of variation of the service time
90 */
91template <class T>
92Mg1DisciplineResult<T> qsys_mg1_setf(const std::vector<T>& lambda, const std::vector<T>& mu,
93 const std::vector<T>& cs) {
95 "qsys_mg1_setf requires transcendental arithmetic");
96 const T rho_total = detail::mg1_discipline_check("qsys_mg1_setf", lambda, mu, cs);
97 const std::size_t K = lambda.size();
98 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
99 const T two = num_traits<T>::from_int(2);
100
101 T lambda_total = zero;
102 for (const T& v : lambda) lambda_total += v;
103
104 // SETF never interrupts service, so a waiting job always has attained
105 // service 0 and the FCFS tie break decides: SETF is FCFS, and the
106 // Pollaczek-Khinchine form is exact for any size law. The former body
107 // added a residual penalty to the FB response of one mean-size job and
108 // read 1.6033 on M/M/1 (lambda = 1, mu = 2) against the exact 1.0.
109 T E_X2 = zero;
110 for (std::size_t i = 0; i < K; ++i)
111 E_X2 += (lambda[i] / lambda_total) * (one + cs[i] * cs[i]) / (mu[i] * mu[i]);
113 r.W.assign(K, zero);
114 for (std::size_t k = 0; k < K; ++k)
115 r.W[k] = lambda_total * E_X2 / (two * (one - rho_total)) + one / mu[k];
116 r.rhohat = detail::mg1_discipline_rhohat(lambda, r.W);
117 return r;
118}
119
120} // namespace qsys
121} // namespace line
122
123#endif // LINE_API_QSYS_QSYS_MG1_SETF_H
InputError(const std::string &what)
Definition error.h:105
The exception types the port throws.
Mg1DisciplineResult< T > qsys_mg1_setf(const std::vector< T > &lambda, const std::vector< T > &mu, const std::vector< T > &cs)
M/G/1 under SETF (shortest elapsed time first), the non-preemptive counterpart of FB/LAS.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Number-type abstraction for the templated API port.
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.