LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
fluid_mfq_prio.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_SOLVERS_FLUID_FLUID_MFQ_PRIO_H
6#define LINE_SOLVERS_FLUID_FLUID_MFQ_PRIO_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * The priority branch of the `mfq` method: a port of `solver_mfq_prio.m`.
12 *
13 * WHEN IT IS REACHED. `solver_fluid_analyzer.m` sends a single-queue open model
14 * to `solver_mfq` when every class has the same priority and HERE when they do
15 * not. The model is the same one `mfq` solves -- Source -> Queue -> Sink -- but
16 * the server now drains the highest-priority fluid first, preemptively, so the
17 * per-class levels no longer follow from one fluid-fluid queue.
18 *
19 * WHAT IS SOLVED. Each class arrives as a MAP. The per-class background chains
20 * are SUPERPOSED into one joint chain by a Kronecker sum, and the joint chain
21 * modulates a K x N rate matrix, one row per class. That pair (Qjoint, Rjoint)
22 * with the constant drain rate d is exactly the input of the fluid priority
23 * queue of G. Horvath, "Efficient analysis of the MMAP[K]/PH[K]/1 priority
24 * queue", EJOR 246(1):128-139, 2015, already ported as `mam::mfq_prio_queue`.
25 * The fluid level of a class is read as its queue length and the fluid sojourn
26 * as its response time, the same reading `solver_mfq.m` makes.
27 *
28 * THE ROW ORDER MATTERS AND IS INVERTED. LINE's `classprio` is a RANK: the
29 * SMALLER the value the HIGHER the priority. FluidPrioQueue takes the opposite
30 * convention, the LAST row being the highest priority. The reference therefore
31 * sorts the open classes by `classprio` DESCENDING before building the rows,
32 * and this port keeps that sort stable so ties preserve class order.
33 *
34 * WHEN IT DECLINES. The reference falls back to the matrix fluid method, with a
35 * warning, whenever the fluid priority model degenerates: a non-MAP arrival,
36 * class-dependent or non-positive service, or a joint chain with a single state
37 * (unmodulated arrivals, where the fluid level is identically zero and the
38 * priority structure carries no information). This port reports the same
39 * decision through `MfqPrioResult::fallback` and lets the caller re-dispatch,
40 * rather than returning zeros that look like an answer.
41 *
42 * A FLUID LEVEL IS NOT A CUSTOMER COUNT. The level is zero unless the ARRIVAL
43 * RATE EXCEEDS d in some background state, and fluid spends no time in service,
44 * so the level is the BUFFER and the sojourn the WAIT. Reporting them as the
45 * station's QN and RN gave a class whose peak rate stays below the drain rate
46 * QN = RN = 0 (and, through the post-processing, UN = 0) beside a throughput
47 * lambda. The class's own service is therefore added back: QN = level +
48 * lambda/d and RN = sojourn + 1/d, as solver_mfq_prio.m does.
49 */
50
51#include <algorithm>
52#include <cmath>
53#include <cstddef>
54#include <numeric>
55#include <string>
56#include <vector>
57
62#include "line/util/error.h"
63#include "line/util/matrix.h"
64
65namespace line {
66namespace fluid {
67
68/** Per-class metrics of the priority queue, indexed by class. */
70 std::vector<double> QN, RN, TN, UN;
71 bool fallback = false; ///< true when the reference would run the matrix method instead
72 std::string reason; ///< why, for the caller's warning
73};
74
75namespace detail {
76
77/** Kronecker sum term: kron over j of (j == i ? A_i : I_{n_j}), in j order. */
78inline Matrix<double> prio_kron_term(const std::vector<Matrix<double>>& A,
79 const std::vector<std::size_t>& n, std::size_t i) {
80 Matrix<double> term(1, 1, 1.0);
81 for (std::size_t j = 0; j < A.size(); ++j) {
82 if (j == i)
83 term = mam::kron(term, A[i]);
84 else
85 term = mam::kron(term, line::eye<double>(n[j]));
86 }
87 return term;
88}
89
90} // namespace detail
91
92/**
93 * Solve the single priority fluid queue of `sn`.
94 *
95 * @param top the single-queue topology, as `mfq_is_single_queue` found it
96 * @param tol `options.tol`, passed to the Riccati iterations as their precision
97 * @param sn the refreshed network struct
98 */
99template <class T>
101 const std::size_t K = sn.nclasses;
102 if (!top.ok)
103 throw UnsupportedError(ErrorKind{"Topology"},
104 "fluid mfq: the priority fluid queue needs a model of open classes flowing "
105 "Source -> Queue -> Sink and nothing else");
106 MfqPrioResult out;
107 out.QN.assign(K, 0.0);
108 out.RN.assign(K, 0.0);
109 out.TN.assign(K, 0.0);
110 out.UN.assign(K, 0.0);
111
112 // Open classes, ordered so that the LAST row is the highest priority.
113 std::vector<std::size_t> ord = top.open_classes;
114 std::stable_sort(ord.begin(), ord.end(), [&sn](std::size_t a, std::size_t b) {
115 return sn.classes[a].prio > sn.classes[b].prio;
116 });
117 const std::size_t Kc = ord.size();
118 if (Kc == 0) {
119 out.fallback = true;
120 out.reason = "invalid service rates";
121 return out;
122 }
123
124 // The drain rate must be one number: a priority fluid queue has ONE server.
125 std::vector<double> mu(Kc, 0.0);
126 for (std::size_t i = 0; i < Kc; ++i)
127 mu[i] = num_traits<T>::to_double(sn.rates(top.queue, ord[i]));
128 for (std::size_t i = 0; i < Kc; ++i)
129 if (!std::isfinite(mu[i]) || mu[i] <= 0.0) {
130 out.fallback = true;
131 out.reason = "invalid service rates";
132 return out;
133 }
134 const double fine_tol = 1e-8; // GlobalConstants.FineTol
135 for (std::size_t i = 0; i < Kc; ++i)
136 if (std::fabs(mu[i] - mu[0]) > fine_tol * std::max(1.0, mu[0])) {
137 out.fallback = true;
138 out.reason = "class-dependent service";
139 return out;
140 }
141 const double d = mu[0];
142
143 // Per-class arrival fluid: Q_k = D0 + D1 and the rate vector R_k = D1 e.
144 std::vector<Matrix<double>> Qk(Kc), Rk(Kc);
145 std::vector<std::size_t> Nk(Kc, 0);
146 std::vector<double> lambda(Kc, 0.0);
147 for (std::size_t i = 0; i < Kc; ++i) {
148 const lang::Distrib<T>& arr = sn.service[top.source][ord[i]];
149 const std::size_t n = arr.D0.rows();
150 if (n == 0 || arr.D1.rows() != n) {
151 out.fallback = true;
152 out.reason = "non-MAP arrival";
153 return out;
154 }
155 Qk[i] = Matrix<double>(n, n, 0.0);
156 Rk[i] = Matrix<double>(n, 1, 0.0);
157 for (std::size_t a = 0; a < n; ++a)
158 for (std::size_t b = 0; b < n; ++b) {
159 Qk[i](a, b) = num_traits<T>::to_double(arr.D0(a, b)) +
160 num_traits<T>::to_double(arr.D1(a, b));
161 Rk[i](a, 0) += num_traits<T>::to_double(arr.D1(a, b));
162 }
163 Nk[i] = n;
164 lambda[i] = num_traits<T>::to_double(sn.rates(top.source, ord[i]));
165 }
166
167 std::size_t Njoint = 1;
168 for (std::size_t i = 0; i < Kc; ++i) Njoint *= Nk[i];
169 if (Njoint < 2) {
170 out.fallback = true;
171 out.reason = "non-modulated (exponential) arrivals";
172 return out;
173 }
174
175 // The joint background chain, and the per-class rate in each of its states.
176 Matrix<double> Qjoint(Njoint, Njoint, 0.0);
177 for (std::size_t i = 0; i < Kc; ++i) {
178 const Matrix<double> term = detail::prio_kron_term(Qk, Nk, i);
179 for (std::size_t a = 0; a < Njoint; ++a)
180 for (std::size_t b = 0; b < Njoint; ++b) Qjoint(a, b) += term(a, b);
181 }
182 Matrix<double> Rjoint(Kc, Njoint, 0.0);
183 std::vector<Matrix<double>> ones_col(Kc);
184 for (std::size_t j = 0; j < Kc; ++j) ones_col[j] = Matrix<double>(Nk[j], 1, 1.0);
185 for (std::size_t i = 0; i < Kc; ++i) {
186 Matrix<double> v(1, 1, 1.0);
187 for (std::size_t j = 0; j < Kc; ++j) v = mam::kron(v, (j == i) ? Rk[i] : ones_col[j]);
188 for (std::size_t a = 0; a < Njoint; ++a) Rjoint(i, a) = v(a, 0);
189 }
190
192 po.prec = (tol > 0.0) ? tol : 1e-14;
193 po.classes.clear();
194 for (std::size_t i = 1; i <= Kc; ++i) po.classes.push_back(i);
196 try {
197 po.flMoms = 1;
198 po.stMoms = 0;
199 fl = mam::mfq_prio_queue(Qjoint, Rjoint, d, po);
200 po.flMoms = 0;
201 po.stMoms = 1;
202 st = mam::mfq_prio_queue(Qjoint, Rjoint, d, po);
203 } catch (const std::exception& e) {
204 out.fallback = true;
205 out.reason = e.what();
206 return out;
207 }
208
209 for (std::size_t i = 0; i < Kc; ++i) {
210 const std::size_t k = ord[i];
211 // The fluid level is the BUFFER and the sojourn excludes service, so the
212 // class's own service is added back (solver_mfq_prio.m): QN = level +
213 // lambda/d, RN = sojourn + 1/d. Fluid Little's law keeps QN = TN*RN.
214 out.UN[k] = std::min(1.0, lambda[i] / d);
215 out.QN[k] = ((i < fl.flMoms.size() && !fl.flMoms[i].empty()) ? fl.flMoms[i][0] : 0.0) + out.UN[k];
216 out.RN[k] = ((i < st.stMoms.size() && !st.stMoms[i].empty()) ? st.stMoms[i][0] : 0.0) + 1.0 / d;
217 out.TN[k] = lambda[i];
218 }
219 return out;
220}
221
222} // namespace fluid
223} // namespace line
224
225#endif // LINE_SOLVERS_FLUID_FLUID_MFQ_PRIO_H
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
The exception types the port throws.
The mfq method: a port of solver_mfq.m and the single-queue gate fluid_is_single_queue....
Dense matrix and non-owning view.
Fluid priority queue: per-class fluid level and sojourn time of an MMAP[K]/PH[K]/1-type continuous fl...
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
MfqPrioResult fluid_mfq_prio(const qn::NetworkStruct< T > &sn, const MfqTopology &top, double tol)
Solve the single priority fluid queue of sn.
Matrix< T > kron(const Matrix< T > &A, const Matrix< T > &B)
Kronecker product.
Definition mmap_lambda.h:57
FluidPrioResult mfq_prio_queue(const Matrix< double > &Q, const Matrix< double > &R, double d, const FluidPrioOptions &opt)
Fluid priority queue.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Matrix< T > eye(std::size_t n)
Identity of order n.
Definition linalg.h:28
A queueing network and its refreshed NetworkStruct.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
Per-class metrics of the priority queue, indexed by class.
std::string reason
why, for the caller's warning
std::vector< double > RN
std::vector< double > QN
std::vector< double > UN
bool fallback
true when the reference would run the matrix method instead
std::vector< double > TN
What the single-queue gate found, when it matches.
Definition fluid_mfq.h:54
std::vector< std::size_t > open_classes
every open class, in class order
Definition fluid_mfq.h:59
std::size_t source
0-based station index
Definition fluid_mfq.h:56
Matrix< T > D0
The (D0,D1) pair when the type carries one directly.
Definition lang_types.h:921
Which measures to compute, and the numerical options.
std::size_t flMoms
number of fluid level moments, 0 = not wanted
std::size_t stMoms
number of sojourn time moments
double prec
Riccati and matrix-quadratic tolerance.
std::vector< std::size_t > classes
1-based classes to analyze, empty = all
One entry per analyzed class, in the order given by FluidPrioOptions::classes.
std::vector< std::vector< double > > flMoms
fluid level moments per class
std::vector< std::vector< double > > stMoms
sojourn time moments per class