LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mm1_ps.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_MM1_PS_H
6#define LINE_API_QSYS_QSYS_MM1_PS_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Exact sojourn-time moments of the multiclass M/M/1-PS queue.
12 *
13 * Templated port of matlab/src/api/qsys/qsys_mm1_ps.m. No JAR counterpart.
14 * Class j arrives Poisson at rate lambda(j) and needs Exp(mu(j)) service on a
15 * processor shared equally by every job present, so the class of a job affects
16 * its sojourn time both through its own rate and through the MIX of rates it
17 * shares the processor with. With alpha = 1 - sum_j lambda_j/mu_j,
18 *
19 * E[W_r] = 1/(alpha mu_r)
20 * E[W_r^2] = 2/(alpha mu_r)^2 * [1 - sum_j lambda_j (mu_j-mu_r)/(mu_j(mu_j+mu_r))]
21 * / [1 - sum_j lambda_j/(mu_j+mu_r)]
22 *
23 * which is equation (7) of Mitra and Morrison (1983). Both are EXACT rather
24 * than asymptotic: the open system is the N -> infinity limit of the closed
25 * terminal-driven system whose moments that paper expands in 1/N, and the
26 * leading term of the expansion is exact in the limit. For a single class the
27 * second moment reduces to the classical 4/(mu^2 (1-rho)^2 (2-rho)) of
28 * Coffman, Muntz and Trotter (1970), which is the identity the test checks.
29 *
30 * Reference: D. Mitra, J. A. Morrison, "Asymptotic Expansions of Moments of
31 * the Waiting Time in Closed and Open Processor-Sharing Systems with Multiple
32 * Job Classes", Adv. Appl. Prob. 15(4), 1983, equation (7).
33 *
34 * ARITHMETIC: rational in lambda and mu throughout, so the exact
35 * instantiation returns both moments with no rounding; there is no
36 * transcendental step and no static_assert.
37 */
38
39#include <cstddef>
40#include <vector>
41
42#include "line/num/number.h"
43#include "line/util/error.h"
44
45namespace line {
46namespace qsys {
47
48/** Mirrors MATLAB's [W, W2, alpha] return list. */
49template <class T>
51 std::vector<T> W; ///< per-class mean sojourn times
52 std::vector<T> W2; ///< per-class second moments of the sojourn time
53 T alpha; ///< unutilized processor fraction, 1 - sum_j lambda_j/mu_j
54};
55
56/**
57 * @brief Exact sojourn-time moments of the multiclass M/M/1-PS queue.
58 *
59 * @param lambda per-class Poisson arrival rates, nonnegative
60 * @param mu per-class exponential service rates, positive
61 */
62template <class T>
63Mm1PsResult<T> qsys_mm1_ps(const std::vector<T>& lambda, const std::vector<T>& mu) {
64 const T zero = num_traits<T>::from_int(0);
65 const T one = num_traits<T>::from_int(1);
66 const T two = num_traits<T>::from_int(2);
67 const std::size_t R = lambda.size();
68 if (mu.size() != R)
69 throw InputError("qsys_mm1_ps: lambda and mu must have the same number of classes");
70 for (std::size_t j = 0; j < R; ++j) {
71 if (lambda[j] < zero) throw InputError("qsys_mm1_ps: lambda must be non-negative");
72 if (mu[j] <= zero) throw InputError("qsys_mm1_ps: mu must be positive");
73 }
74
76 r.alpha = one;
77 for (std::size_t j = 0; j < R; ++j) r.alpha -= lambda[j] / mu[j];
78 if (r.alpha <= zero) throw InputError("qsys_mm1_ps: the system is unstable, utilization >= 1");
79
80 r.W.assign(R, zero);
81 r.W2.assign(R, zero);
82 for (std::size_t rr = 0; rr < R; ++rr) {
83 const T mur = mu[rr];
84 T num = one, den = one;
85 for (std::size_t j = 0; j < R; ++j) {
86 num -= lambda[j] * (mu[j] - mur) / (mu[j] * (mu[j] + mur));
87 den -= lambda[j] / (mu[j] + mur);
88 }
89 const T am = r.alpha * mur;
90 r.W[rr] = one / am;
91 r.W2[rr] = two / (am * am) * num / den;
92 }
93 return r;
94}
95
96} // namespace qsys
97} // namespace line
98
99#endif // LINE_API_QSYS_QSYS_MM1_PS_H
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
Mm1PsResult< T > qsys_mm1_ps(const std::vector< T > &lambda, const std::vector< T > &mu)
Exact sojourn-time moments of the multiclass M/M/1-PS queue.
Definition qsys_mm1_ps.h:63
Number-type abstraction for the templated API port.
Mirrors MATLAB's [W, W2, alpha] return list.
Definition qsys_mm1_ps.h:50
std::vector< T > W2
per-class second moments of the sojourn time
Definition qsys_mm1_ps.h:52
T alpha
unutilized processor fraction, 1 - sum_j lambda_j/mu_j
Definition qsys_mm1_ps.h:53
std::vector< T > W
per-class mean sojourn times
Definition qsys_mm1_ps.h:51