LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mg1kk.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_MG1KK_H
6#define LINE_API_QSYS_QSYS_MG1KK_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Exact analysis of the finite-source M/G/1/K/K (machine-repair) queue.
12 *
13 * Port of matlab/src/api/qsys/qsys_mg1kk.m. K sources each operate for an
14 * Exp(alpha) time and then queue FCFS at one server of general service law B.
15 * Takacs (1962) gives the idle probability from the service LST at the
16 * points k alpha,
17 *
18 * P0 = 1 / (1 + K alpha E[S] sum_{n=0}^{K-1} C(K-1, n) prod_{k=1}^n (1 - B(k alpha))/B(k alpha)),
19 *
20 * whence X = (1 - P0)/E[S] and E[T] = K/X - 1/alpha. The queue-length law
21 * comes from the chain embedded at departures, whose transition probabilities
22 * are alternating sums of B(k alpha); MATLAB's check that its P0 agrees with
23 * Takacs' is kept: when the alternating sums cancel it warns and returns the
24 * distribution (and the variance) as NaN, the means being the Takacs sum.
25 *
26 * ARITHMETIC: double, as the reference. MATLAB's overdetermined `A \ b`
27 * (least squares on the consistent system [P' - I; 1] pi = [0; 1]) is solved
28 * here by LU on the square system that replaces one of the K dependent
29 * balance rows by the normalization; the solution of a consistent system is
30 * unique, so the two agree to rounding.
31 */
32
33#include <algorithm>
34#include <cmath>
35#include <cstdio>
36#include <functional>
37#include <iostream>
38#include <limits>
39#include <string>
40#include <vector>
41
44#include "line/util/error.h"
46#include "line/util/lu.h"
47#include "line/util/matrix.h"
48
49namespace line {
50namespace qsys {
51
52/**
53 * @brief Exact M/G/1/K/K finite-source queue, service given by its LST.
54 *
55 * @param alpha request rate of one operating source
56 * @param K number of sources
57 * @param lst service-time LST at real arguments
58 * @param ES mean service time
59 * @param means_only skip the departure-epoch chain: the means are the Takacs
60 * sum and never read it, so a caller that wants only them (the
61 * MVA finite-source analyzer) pays O(K) instead of O(K^3), and
62 * no accuracy warning; queueLengthDist, arrivalDist and
63 * varQueueLength are then NaN, as when the chain is withdrawn
64 */
65inline QsysRes qsys_mg1kk(double alpha, unsigned K, const std::function<double(double)>& lst, double ES,
66 bool means_only = false) {
68 if (!(alpha > 0.0)) throw Error("qsys_mg1kk: alpha must be a positive real scalar");
69 if (K < 1) throw Error("qsys_mg1kk: K must be a positive integer");
70 if (!lst) throw Error("qsys_mg1kk: svc must be a Distribution or an LST function handle");
71 if (!(ES > 0.0)) throw Error("qsys_mg1kk: with an LST handle the mean service time ES must be given");
72
73 std::vector<double> Bs(K + 1);
74 for (unsigned k = 0; k <= K; ++k) Bs[k] = lst(k * alpha);
75 double Bn = 1.0, S = 1.0;
76 {
77 const std::vector<double> cK = binom_row(K - 1);
78 for (unsigned nn = 1; nn + 1 <= K; ++nn) {
79 Bn = Bn * (1.0 - Bs[nn]) / Bs[nn];
80 S += cK[nn] * Bn;
81 }
82 }
83 const double P0 = 1.0 / (1.0 + K * alpha * ES * S);
84 const double X = (1.0 - P0) / ES;
85 const double ET = K / X - 1.0 / alpha;
86 const double EW = ET - ES;
87 const double nan = std::numeric_limits<double>::quiet_NaN();
88
89 // the reference's field order; the chain overwrites its three fields in place
90 QsysRes r;
91 r.set("meanQueueLength", X * ET);
92 r.set("varQueueLength", nan);
93 r.set("meanQueueLengthQ", X * EW);
94 r.set("meanWaitingTime", EW);
95 r.set("meanSojournTime", ET);
96 r.set("throughput", X);
97 r.set("utilization", 1.0 - P0);
98 r.set("meanOperating", K - X * ET);
99 r.set("queueLengthDist", std::vector<double>(K + 1, nan));
100 r.set("arrivalDist", std::vector<double>(K, nan));
101 r.analyzer = "exact:M/G/1/" + std::to_string(K) + "/" + std::to_string(K);
102 if (means_only) return r;
103
104 // binomial rows 0..K-1, once: the chain reads C(i, l) and C(nop, i) O(K^3) times
105 std::vector<std::vector<double>> C(K);
106 for (unsigned n = 0; n < K; ++n) C[n] = binom_row(n);
107 Matrix<double> P(K, K, 0.0);
108 for (unsigned j = 0; j < K; ++j) {
109 const unsigned nop = j == 0 ? K - 1 : K - j;
110 const unsigned base = j == 0 ? 0 : j - 1;
111 for (unsigned i = 0; i <= nop; ++i) {
112 double acc = 0.0;
113 for (unsigned l = 0; l <= i; ++l)
114 acc += ((l % 2) ? -1.0 : 1.0) * C[i][l] * Bs[nop - i + l];
115 P(j, base + i) += C[nop][i] * acc;
116 }
117 }
118 for (unsigned j = 0; j < K; ++j) {
119 double rs = 0.0;
120 for (unsigned i = 0; i < K; ++i) {
121 P(j, i) = std::max(P(j, i), 0.0);
122 rs += P(j, i);
123 }
124 for (unsigned i = 0; i < K; ++i) P(j, i) /= rs;
125 }
126 // [P' - I] with its last balance row replaced by the normalization
127 Matrix<double> A(K, K, 0.0);
128 std::vector<double> b(K, 0.0);
129 for (unsigned row = 0; row + 1 < K; ++row)
130 for (unsigned cc = 0; cc < K; ++cc) A(row, cc) = P(cc, row) - (row == cc ? 1.0 : 0.0);
131 for (unsigned cc = 0; cc < K; ++cc) A(K - 1, cc) = 1.0;
132 b[K - 1] = 1.0;
133 std::vector<double> Pi = solve(A, b);
134
135 std::vector<double> pn(K + 1, 0.0);
136 double mass = 0.0;
137 for (unsigned n = 0; n < K; ++n) {
138 pn[n] = X * Pi[n] / ((static_cast<double>(K) - n) * alpha);
139 mass += pn[n];
140 }
141 pn[K] = 1.0 - mass;
142 if (std::fabs(pn[0] - P0) > 1e-6 * std::max(P0, 1e-12) || pn[K] < -1e-9) {
143 char buf[384];
144 std::snprintf(buf, sizeof buf,
145 "qsys_mg1kk: the embedded-chain distribution disagrees with the Takacs P_0 (%g vs %g): "
146 "the alternating arrival sums lost accuracy at K=%u; queueLengthDist, arrivalDist "
147 "and varQueueLength are returned as NaN, the means are unaffected.",
148 pn[0], P0, K);
149 // the means above are the Takacs sum of positive terms and do not
150 // read the chain, so only the distribution is withdrawn
152 std::cerr << "[LINE] Warning: " << buf << std::endl;
153 std::fill(pn.begin(), pn.end(), nan);
154 std::fill(Pi.begin(), Pi.end(), nan);
155 } else {
156 pn[K] = std::max(pn[K], 0.0);
157 }
158 double EN = 0.0, EN2 = 0.0;
159 for (unsigned n = 0; n <= K; ++n) {
160 EN += n * pn[n];
161 EN2 += static_cast<double>(n) * n * pn[n];
162 }
163 r.set("varQueueLength", EN2 - EN * EN);
164 r.set("queueLengthDist", pn);
165 r.set("arrivalDist", Pi);
166 return r;
167}
168
169/**
170 * @brief Exact M/G/1/K/K finite-source queue, service given as a Distribution
171 * (its LST and mean, as MATLAB's svc.evalLST / svc.getMean).
172 */
173inline QsysRes qsys_mg1kk(double alpha, unsigned K, const lang::Distrib<double>& svc) {
174 const lang::Distrib<double> d = svc;
175 return qsys_mg1kk(alpha, K, [d](double s) { return lang::dist_lst(d, s); }, lang::dist_moment(d, 1));
176}
177
178} // namespace qsys
179} // namespace line
180
181#endif // LINE_API_QSYS_QSYS_MG1KK_H
Error(const std::string &what)
Definition error.h:75
static VerboseLevel get_verbose()
The session verbosity.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
Running progress log of a LINE solver run (the "solver console").
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
T dist_lst(const Distrib< T > &d, const T &s)
sn.lst: the Laplace-Stieltjes transform E[exp(-sX)].
T dist_moment(const Distrib< T > &d, unsigned k)
The k-th raw moment.
std::vector< double > binom_row(unsigned n)
The row C(n, 0..n) by the multiplicative recurrence C(n,k) = C(n,k-1)(n-k+1)/k, each entry rounded to...
Definition qsys_res.h:243
QsysResT< double > QsysRes
Definition qsys_res.h:96
QsysRes qsys_mg1kk(double alpha, unsigned K, const std::function< double(double)> &lst, double ES, bool means_only=false)
Exact M/G/1/K/K finite-source queue, service given by its LST.
Definition qsys_mg1kk.h:65
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
std::vector< T > solve(const Matrix< T > &A, const std::vector< T > &b)
Convenience: solve Ax = b, leaving A and b untouched.
Definition lu.h:158
The res struct the exact queueing-system functions return, and the numeric helpers those functions sh...
std::string analyzer
Definition qsys_res.h:59
void set(const std::string &name, const T &v)
Sets (or overwrites) a scalar field.
Definition qsys_res.h:62