LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_mapmap1.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_MAPMAP1_H
6#define LINE_API_QSYS_QSYS_MAPMAP1_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * The MAP/MAP/1 FCFS queue: mean number in system, waiting time, sojourn time,
12 * utilization and the queue-length distribution.
13 *
14 * ALGORITHM. matlab/src/api/qsys/qsys_mapmap1.m calls BUTools' MAPMAP1, which
15 * solves the level-independent QBD whose level is the number in system and
16 * whose phase is the pair (arrival phase, service phase), with the service
17 * phase frozen while the server is idle. This port solves the same QBD with
18 * its own machinery (line/api/mam/qbd_mapmap1.h) and takes the queue-length
19 * moments in closed form from pi_0 and R (qbd_qlen_moment). The sojourn-time
20 * moments come from BUTools' matrix-exponential law of the age at departure,
21 * U = L + R B, Rh = (-U)^-1 F, T = I_na (x) D0 + Rh B,
22 * eta = pi_0 F (I - Rh)^-1 / (pi_0 F (I - Rh)^-1 e),
23 * E[W^k] = k! eta (-T)^-k e.
24 *
25 * THE SERVICE MAP IS USED AS A MAP. Until 2026-11 every codebase replaced the
26 * service MAP (D0,D1) by its stationary marginal PH, sigma = theta D1 /
27 * (theta D1 e), S = D0 (the old mapToPh), and solved MAP/PH/1 through
28 * MMAPPH1FCFS. That keeps the service-time marginal but discards the serial
29 * correlation between consecutive service times, so the function named
30 * MAP/MAP/1 answered a different queue: on the correlated case below it gave
31 * 2.0305355320573 against the true 2.5475575. A renewal service (any PH, as
32 * used by qsys_mapph1, qsys_phph1 and qsys_mapg1) is unaffected, since its
33 * MAP and its marginal PH are the same process.
34 *
35 * MEASURED AGREEMENT (T = double). Metric order is meanQueueLength /
36 * meanWaitingTime / meanSojournTime / utilization. Arrival MAPs used below:
37 * P(2) = Poisson, D0 = [-2], D1 = [2]
38 * C = correlated MMPP2, D0 = [-2.5 0.2; 0.1 -0.7], D1 = diag(2.3, 0.6),
39 * lambda = 7/6
40 *
41 * 1. M/M/1, qsys_mapmap1(P(2), service D0 = [-3], D1 = [3]): 2 / 2/3 / 1 / 2/3,
42 * the textbook values.
43 *
44 * 2. Renewal service. Arrival C, service the Erlang-2 renewal MAP
45 * D0 = [-6 6; 0 -6], D1 = [0 0; 6 0]: 0.836463752964917 /
46 * 0.383635597779453 / 0.716968931112786 / 0.388888888888889, unchanged
47 * from the MAP/PH/1 route.
48 *
49 * 3. Correlated service. Arrival C, service MAP D0 = [-5 0.4; 0.2 -1.4],
50 * D1 = diag(4.6, 1.2) (lambda_s = 7/3, rho = 1/2). meanQueueLength
51 * 2.5475575348, meanSojournTime 2.1836207441 (= QN / lambda by Little),
52 * the values of BUTools MAPMAP1 and of a truncated CTMC of the queue
53 * (3000 levels, 2.5475575).
54 * meanWaitingTime is meanSojournTime minus the mean service time 1/lambda_s,
55 * and utilization is lambda_a/lambda_s.
56 *
57 * ARITHMETIC. The function is gated on num_traits<T>::has_transcendental
58 * because qbd_mapmap1 reaches R by cyclic reduction, a fixed-point iteration
59 * driven to a tolerance that does not terminate in a finite number of field
60 * operations; running it at exact rational arithmetic would produce a rational
61 * with a denominator doubling every step and still not the exact R. See
62 * qbd_r.h. Everything downstream of R here -- the mean, the level
63 * probabilities, the Little's-law conversions -- is finite exact matrix
64 * algebra and carries no additional error.
65 */
66
67#include <cstddef>
68#include <vector>
69
74#include "line/api/mam/qbd_r.h"
76#include "line/num/number.h"
77#include "line/util/error.h"
78#include "line/util/matrix.h"
79
80namespace line {
81namespace qsys {
82
83/**
84 * Return value of the MAP/MAP/1 family (qsys_mapmap1, qsys_mapph1, qsys_phph1),
85 * carrying the same quantities as the MATLAB result struct.
86 */
87template <class T>
89 T meanQueueLength; ///< E[N], number in system
90 T meanWaitingTime; ///< E[Wq], time in queue
91 T meanSojournTime; ///< E[W] = E[Wq] + E[S]
92 T utilization; ///< rho = lambda_a / lambda_s
93 std::vector<T> queueLengthDist; ///< P(N = n), n = 0, 1, ...
94 std::vector<T> queueLengthMoments; ///< raw moments E[N^k], k = 1..numQLMoms
95 std::vector<T> sojournTimeMoments; ///< raw moments E[W^k], k = 1..numSTMoms
96};
97
98namespace detail {
99
100/** Raw moments k! alpha (-A)^-k e, k = 1..n, of the PH (alpha, A). */
101template <class T>
102std::vector<T> mapmap1_ph_moments(const std::vector<T>& alpha, const Matrix<T>& A, std::size_t n) {
103 std::vector<T> out;
104 if (n == 0) return out;
105 const std::size_t m = A.rows();
106 Matrix<T> negA(m, m);
107 for (std::size_t i = 0; i < m; ++i)
108 for (std::size_t j = 0; j < m; ++j) negA(i, j) = -A(i, j);
109 const Matrix<T> iA = inverse(negA);
110 std::vector<T> v = alpha;
111 for (std::size_t k = 1; k <= n; ++k) {
112 v = vecmul(v, iA);
114 for (std::size_t j = 0; j < m; ++j) s += v[j];
115 out.push_back(T(num_factorial<T>(static_cast<unsigned>(k)) * s));
116 }
117 return out;
118}
119
120} // namespace detail
121
122/**
123 * MAP/MAP/1 FCFS queue with the service MAP honoured, correlation included.
124 * @param arrival arrival MAP (C0, C1)
125 * @param service service MAP (D0, D1)
126 * @param dist_size how many entries of queueLengthDist to materialize
127 * (the reference's numQLProbs)
128 * @param numQLMoms how many raw queue-length moments to return
129 * @param numSTMoms how many raw sojourn-time moments to return
130 */
131template <class T>
133 std::size_t dist_size, std::size_t numQLMoms,
134 std::size_t numSTMoms) {
136 "qsys_mapmap1 requires transcendental arithmetic");
137 if (dist_size == 0) throw InputError("qsys_mapmap1: dist_size must be positive");
138 const T zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
139
140 const mam::QbdMapMap1Result<T> q = mam::qbd_mapmap1(arrival, service, T(zero), dist_size);
141
142 // utilization from the arrival and service rates, rho = lambda / mu
143 const T lambda_a = mam::map_lambda(arrival);
144 const T mu = mam::map_lambda(service);
145 const T mean_service = one / mu;
146
148 r.queueLengthMoments.clear();
149 for (std::size_t k = 1; k <= numQLMoms; ++k)
150 r.queueLengthMoments.push_back(mam::qbd_qlen_moment(q.pi0, q.R, static_cast<unsigned>(k)));
151 r.sojournTimeMoments.clear();
152 if (numSTMoms > 0) {
153 // BUTools MAPMAP1: matrix-exponential law (eta, Tst) of the sojourn time
154 const std::size_t N = q.R.rows();
155 const std::size_t na = arrival.order();
156 Matrix<T> negU(N, N);
157 for (std::size_t i = 0; i < N; ++i)
158 for (std::size_t j = 0; j < N; ++j) negU(i, j) = -q.U(i, j);
159 const Matrix<T> Rh = matmul(inverse(negU), q.F);
160 const Matrix<T> RhB = matmul(Rh, q.B);
161 const Matrix<T> K = mam::kron(eye<T>(na), service.D0);
162 Matrix<T> Tst(N, N);
163 Matrix<T> ImRh(N, N);
164 for (std::size_t i = 0; i < N; ++i)
165 for (std::size_t j = 0; j < N; ++j) {
166 Tst(i, j) = K(i, j) + RhB(i, j);
167 ImRh(i, j) = (i == j ? one : zero) - Rh(i, j);
168 }
169 std::vector<T> eta = vecmul(vecmul(q.pi0, q.F), inverse(ImRh));
170 T etaSum = zero;
171 for (std::size_t j = 0; j < N; ++j) etaSum += eta[j];
172 for (std::size_t j = 0; j < N; ++j) eta[j] = eta[j] / etaSum;
173 r.sojournTimeMoments = detail::mapmap1_ph_moments(eta, Tst, numSTMoms);
174 }
175
176 // meanQL = ncMoms(1), meanST = stMoms(1), each 0 when not requested
177 r.meanQueueLength = r.queueLengthMoments.empty() ? zero : r.queueLengthMoments[0];
178 r.meanSojournTime = r.sojournTimeMoments.empty() ? zero : r.sojournTimeMoments[0];
179 const T wait = T(r.meanSojournTime - mean_service);
180 r.meanWaitingTime = wait < zero ? zero : wait;
181 r.utilization = lambda_a / mu;
182 r.queueLengthDist.assign(q.pqueue.rows(), zero);
183 for (std::size_t k = 0; k < q.pqueue.rows(); ++k) {
184 T s = zero;
185 for (std::size_t j = 0; j < q.pqueue.cols(); ++j) s += q.pqueue(k, j);
186 r.queueLengthDist[k] = s;
187 }
188 return r;
189}
190
191/** qsys_mapmap1 with the reference's defaults numQLMoms = 3, numSTMoms = 3. */
192template <class T>
194 std::size_t dist_size) {
195 return qsys_mapmap1(arrival, service, dist_size, static_cast<std::size_t>(3),
196 static_cast<std::size_t>(3));
197}
198
199/** qsys_mapmap1 with 100 materialized levels, the reference's numQLProbs. */
200template <class T>
201MapMap1Result<T> qsys_mapmap1(const mam::Map<T>& arrival, const mam::Map<T>& service) {
202 return qsys_mapmap1(arrival, service, static_cast<std::size_t>(100));
203}
204
205} // namespace qsys
206} // namespace line
207
208#endif // LINE_API_QSYS_QSYS_MAPMAP1_H
InputError(const std::string &what)
Definition error.h:105
std::size_t rows() const
Definition matrix.h:89
Steady-state distribution of a continuous-time Markov chain.
The exception types the port throws.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
The MMAP[K]/PH[K]/1 FCFS queue: per-class mean number in system and per-class queue-length distributi...
QbdMapMap1Result< T > qbd_mapmap1(const Map< T > &arrival, const Map< T > &service_in, const T &util, std::size_t max_levels)
MAP/MAP/1 queue (qbd_mapmap1.m).
T qbd_qlen_moment(const std::vector< T > &pi0, const Matrix< T > &R, unsigned m)
Raw moment of order m of the number in system, E[N^m], assembled from the factorial moments with the ...
Matrix< T > kron(const Matrix< T > &A, const Matrix< T > &B)
Kronecker product.
Definition mmap_lambda.h:57
T map_lambda(const Map< T > &m)
Stationary arrival rate, lambda = pi D1 e.
Definition map_moment.h:79
MapMap1Result< T > qsys_mapmap1(const mam::Map< T > &arrival, const mam::Map< T > &service, std::size_t dist_size, std::size_t numQLMoms, std::size_t numSTMoms)
MAP/MAP/1 FCFS queue with the service MAP honoured, correlation included.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
T num_factorial(unsigned n)
Factorial as a value of T.
Definition number.h:196
std::vector< T > vecmul(const std::vector< T > &v, const Matrix< T > &A)
Row vector times matrix, v A.
Definition linalg.h:50
Matrix< T > inverse(const Matrix< T > &A)
Inverse by LU with one factorization and n back substitutions.
Definition linalg.h:72
Matrix< T > matmul(const Matrix< T > &A, const Matrix< T > &B)
Matrix product A B.
Definition linalg.h:36
Matrix< T > eye(std::size_t n)
Identity of order n.
Definition linalg.h:28
Number-type abstraction for the templated API port.
The MAP/MAP/1 queue solved as a quasi-birth-death process.
Quasi-birth-death processes: the rate matrix R, the fundamental matrix G, the caudal characteristic,...
A MAP as the pair of matrices (D0, D1).
Definition map_moment.h:53
Matrix< T > D0
Definition map_moment.h:54
std::size_t order() const
Definition map_moment.h:57
Result of qbd_mapmap1, mirroring the MATLAB return list.
std::vector< T > pi0
boundary vector, normalized
Matrix< T > pqueue
level distribution, row k = pi_k
Return value of the MAP/MAP/1 family (qsys_mapmap1, qsys_mapph1, qsys_phph1), carrying the same quant...
std::vector< T > sojournTimeMoments
raw moments E[W^k], k = 1..numSTMoms
T utilization
rho = lambda_a / lambda_s
std::vector< T > queueLengthMoments
raw moments E[N^k], k = 1..numQLMoms
T meanQueueLength
E[N], number in system.
T meanSojournTime
E[W] = E[Wq] + E[S].
std::vector< T > queueLengthDist
P(N = n), n = 0, 1, ...
T meanWaitingTime
E[Wq], time in queue.