LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
solver_mva_oi.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_MVA_SOLVER_MVA_OI_H
6#define LINE_SOLVERS_MVA_SOLVER_MVA_OI_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * Exact mean-value analysis for networks with order-independent stations.
12 *
13 * Templated port of `matlab/src/solvers/MVA/solver_mva_oi_analyzer.m`.
14 *
15 * An ORDER-INDEPENDENT (OI) station is a class-dependent load-dependent server
16 * whose total service rate mu(n) is a permutation-invariant function of the
17 * per-class count vector. A closed network of delay stations, load-independent
18 * single-server queues and ANY number of OI stations is product-form, and
19 * `pfqn_mvaoi` -- the Conditional MVA that carries one rate-shift vector per OI
20 * station -- solves it exactly, with no normalizing constant and no joint
21 * marginal.
22 *
23 * References: Reiser and Lavenberg, JACM 27(2), 1980; the load-dependent
24 * extension of Bruell, Balbo and Afshari, 1984; OI stations and CMVA in
25 * Casale 2009 and Casale, Comte and Dorsman 2026.
26 *
27 * A MULTISERVER BCMP QUEUE IS PROMOTED TO AN OI STATION rather than
28 * approximated. `pfqn_mvaoi` models a load-independent queue as a SINGLE
29 * server, so a c-server station cannot take that path; but its BCMP weight
30 * satisfies the OI balance recursion exactly for the permutation-invariant rate
31 *
32 * mu(n) = (min(|n|, c) / |n|) * sum_{r : n_r > 0} n_r / D_r,
33 *
34 * so it goes down the OI path instead and the answer stays exact. Reporting
35 * still classifies it as a queue, so its utilization keeps the per-server
36 * offered-load convention.
37 *
38 * DETECTION mirrors `nc_is_oi_model`: PAS or OI scheduling, a service-rate
39 * function, and an ALL-ZERO swap graph. A nonzero swap graph is a genuine
40 * pass-and-swap station, which is not order-independent and is not product
41 * form, so it is excluded from the list -- and if that leaves no OI station at
42 * all, the analyzer refuses rather than solving a different model.
43 *
44 * Arithmetic: whatever `pfqn_mvaoi` accepts; the analyzer itself is field
45 * arithmetic.
46 */
47
48#include <cmath>
49#include <functional>
50#include <vector>
51
56
57namespace line {
58namespace mva {
59
60/**
61 * The OI stations of a model, 1-based station indices.
62 *
63 * Port of `find_oi_stations`. Empty when the model has none, which is what the
64 * dispatch tests before entering this analyzer.
65 */
66template <class T>
67std::vector<std::size_t> find_oi_stations(const qn::NetworkStruct<T>& L) {
68 std::vector<std::size_t> oi;
69 for (std::size_t i = 0; i < L.nstations; ++i) {
70 const qn::Station<T>& st = L.stations[i];
71 if (st.sched != qn::SchedStrategy::PAS && st.sched != qn::SchedStrategy::OI) continue;
72 if (!st.svc_rate_fun) continue;
73 // The graph is read through `station_swap_graph`, which applies the
74 // defaults `refreshLocalVars.m` installs: an OI station is always the
75 // zero graph, and a PAS station with no explicit graph is the complete
76 // compatibility graph, hence a genuine pass-and-swap station and NOT
77 // order-independent. MATLAB never stores an empty graph, so the
78 // `isempty(sg)` arm of `nc_is_oi_model` is unreachable there.
79 if (!qn::station_swap_graph_is_zero(L, i + 1)) continue;
80 oi.push_back(i + 1);
81 }
82 return oi;
83}
84
85/**
86 * Port of `nc_is_oi_model`: whether the model as a WHOLE is order-independent,
87 * which is a strictly stronger condition than having an OI station.
88 *
89 * The whole network must be product-form for the CMVA to be exact, so every
90 * station has to be a delay, an OI station with an all-zero swap graph, a PS or
91 * LCFS-PR queue, or an FCFS / SIRO queue whose service rate does NOT depend on
92 * the class (plus the Source of an open or mixed model). The population may be
93 * closed, open or mixed, as in the reference: the dispatch sends a closed model
94 * to the CMVA below, an open one to `nc::solver_oi_open_analyzer` (shared with
95 * SolverNC), and refuses a mixed one by name, pointing to SolverNC.
96 */
97template <class T>
100 bool hasOI = false;
101 for (std::size_t i = 0; i < L.nstations; ++i) {
102 const qn::Station<T>& st = L.stations[i];
103 if (st.sched == SchedStrategy::INF || st.nodetype == qn::NodeType::Source) continue;
104 if (st.sched == SchedStrategy::PAS || st.sched == SchedStrategy::OI) {
105 if (!st.svc_rate_fun) return false;
106 // see the note in find_oi_stations on the swap-graph defaults
107 if (!qn::station_swap_graph_is_zero(L, i + 1))
108 return false; // genuine pass-and-swap: not order-independent
109 hasOI = true;
110 } else if (st.sched == SchedStrategy::PS || st.sched == SchedStrategy::LCFSPR ||
111 st.sched == SchedStrategy::SIRO || st.sched == SchedStrategy::FCFS) {
112 if (st.sched != SchedStrategy::FCFS && st.sched != SchedStrategy::SIRO) continue;
113 // a class-dependent FCFS or SIRO rate breaks product form
114 double lo = 0.0, hi = 0.0;
115 bool any = false;
116 for (std::size_t r = 0; r < L.nclasses; ++r) {
117 if (!(L.classes[r].population > 0.0)) continue;
118 const double v = num_traits<T>::to_double(L.rates(i, r));
119 if (!std::isfinite(v)) continue;
120 if (!any) {
121 lo = hi = v;
122 any = true;
123 } else {
124 lo = std::min(lo, v);
125 hi = std::max(hi, v);
126 }
127 }
128 if (any && (hi - lo) > 1e-9 * hi) return false;
129 } else {
130 return false; // an unsupported (non-product-form) station
131 }
132 }
133 return hasOI;
134}
135
136namespace detail {
137
138/**
139 * The OI rate at a count vector, evaluated through the station's microstate
140 * function: `repelem(1:R, n)` is the canonical 1-based ordered state with n_r
141 * jobs of class r, and permutation invariance makes any other order give the
142 * same rate.
143 */
144template <class T>
145T oi_rate(const std::function<T(const std::vector<std::size_t>&)>& f, const std::vector<int>& n,
146 std::size_t R) {
147 int tot = 0;
148 for (int v : n) tot += v;
149 if (tot == 0) return num_traits<T>::from_int(0);
150 std::vector<std::size_t> micro;
151 micro.reserve(static_cast<std::size_t>(tot));
152 for (std::size_t r = 0; r < R && r < n.size(); ++r)
153 for (int a = 0; a < n[r]; ++a) micro.push_back(r + 1);
154 return f(micro);
155}
156
157/**
158 * The OI rate that reproduces a c-server BCMP queue with per-class demands Dq:
159 * `mu(n) = (min(|n|,c)/|n|) sum_{r : n_r>0} n_r/Dq_r`. At c = 1 this is the
160 * ordinary total completion rate of a multiclass single server, and for one
161 * class it reduces to min(n,c)/Dq, the M/M/c rate.
162 */
163template <class T>
164T ms_oi_rate(const std::vector<int>& n, const std::vector<T>& Dq, double c) {
165 const T zero = num_traits<T>::from_int(0);
166 int tot = 0;
167 for (int v : n) tot += v;
168 if (tot == 0) return zero;
169 T acc = zero;
170 for (std::size_t r = 0; r < n.size() && r < Dq.size(); ++r)
171 if (n[r] > 0 && Dq[r] > zero) acc += T(num_traits<T>::from_int(n[r]) / Dq[r]);
172 const double cc = (std::isfinite(c) && c > 0.0) ? c : 1.0;
173 const double m = std::min<double>(static_cast<double>(tot), cc);
174 return T(num_traits<T>::from_double(m / static_cast<double>(tot)) * acc);
175}
176
177} // namespace detail
178
179/**
180 * Port of `solver_mva_oi_analyzer.m`. A fully OPEN network goes to
181 * `nc::solver_oi_open_analyzer`, the station-by-station product form SolverNC
182 * runs too (there is no population recursion to carry); its route label
183 * ('oi', 'oi.trunc' or 'oi.loss') comes back in `method`.
184 */
185template <class T>
187 using qn::SchedStrategy;
188 {
189 bool allOpen = !L.classes.empty();
190 for (const auto& c : L.classes)
191 if (!std::isinf(c.population)) allOpen = false;
192 if (allOpen) return nc::solver_oi_open_analyzer(L, opt.tol);
193 }
194 const T zero = num_traits<T>::from_int(0);
195 const std::size_t M = L.nstations, R = L.nclasses;
196
197 const std::vector<std::size_t> oi_list = find_oi_stations(L);
198 if (oi_list.empty())
199 throw UnsupportedError(ErrorKind{"SchedStrategy_OI"},
200 "solver_mva_oi_analyzer: the OI solver requires at least one order-independent "
201 "station (PAS or OI scheduling, a service-rate function, and an all-zero swap graph)");
202
203 // The OI rank rates are indexed by RAW class, so a chain that merges several
204 // classes has no rate to evaluate: the recursion is driven by the per-class
205 // population, which class switching makes meaningless (a class appearing only
206 // mid-chain carries population 0, so the OI station reads as empty).
207 for (std::size_t c = 0; c < L.nchains; ++c)
208 if (L.inchain[c].size() > 1)
209 throw UnsupportedError(ErrorKind{"ClassSwitch"},
210 "solver_mva_oi: requires one class per chain (no class switching)");
211
212 std::vector<int> N(R, 0);
213 for (std::size_t r = 0; r < R; ++r) {
214 const double nr = L.classes[r].population;
215 if (!std::isfinite(nr))
216 throw UnsupportedError(ErrorKind{"OpenClass"},
217 "solver_mva_oi_analyzer: the CMVA recursion is over a closed lattice; this model "
218 "has an open class");
219 N[r] = static_cast<int>(std::llround(nr));
220 }
221
222 std::vector<bool> isOI(M, false), isDelay(M, false);
223 for (std::size_t i : oi_list) isOI[i - 1] = true;
224 for (std::size_t i = 0; i < M; ++i) isDelay[i] = (L.stations[i].sched == SchedStrategy::INF);
225
226 // per-class demand D = V / rate at every station
227 Matrix<T> V(M, R, zero), D(M, R, zero);
228 for (std::size_t c = 0; c < L.nchains; ++c)
229 for (std::size_t i = 0; i < M; ++i)
230 for (std::size_t r = 0; r < R; ++r)
231 V(i, r) = T(V(i, r) + L.visits[c](L.stateful_of_station(i + 1) - 1, r));
232 for (std::size_t i = 0; i < M; ++i)
233 for (std::size_t r = 0; r < R; ++r) {
234 const T mu = L.rates(i, r);
235 if (std::isfinite(num_traits<T>::to_double(mu)) && mu > zero)
236 D(i, r) = T(V(i, r) / mu);
237 }
238
239 // aggregate the delays into Z; split the rest into single-server queues and
240 // multiserver queues, the latter promoted to OI stations
241 std::vector<T> Z(R, zero);
242 std::vector<std::size_t> li_list, ms_list;
243 for (std::size_t i = 0; i < M; ++i) {
244 if (isOI[i]) continue;
245 if (isDelay[i]) {
246 for (std::size_t r = 0; r < R; ++r) Z[r] += D(i, r);
247 } else if (std::isfinite(L.stations[i].nservers) && L.stations[i].nservers > 1.0) {
248 ms_list.push_back(i + 1);
249 } else {
250 li_list.push_back(i + 1);
251 }
252 }
253 Matrix<T> Dli(li_list.size(), R, zero);
254 for (std::size_t a = 0; a < li_list.size(); ++a)
255 for (std::size_t r = 0; r < R; ++r) Dli(a, r) = D(li_list[a] - 1, r);
256
257 std::vector<std::function<T(const std::vector<int>&)>> mu;
258 mu.reserve(oi_list.size() + ms_list.size());
259 for (std::size_t o = 0; o < oi_list.size(); ++o) {
260 const std::function<T(const std::vector<std::size_t>&)> f =
261 L.stations[oi_list[o] - 1].svc_rate_fun;
262 mu.push_back([f, R](const std::vector<int>& n) { return detail::oi_rate<T>(f, n, R); });
263 }
264 for (std::size_t j = 0; j < ms_list.size(); ++j) {
265 std::vector<T> Dq(R, zero);
266 for (std::size_t r = 0; r < R; ++r) Dq[r] = D(ms_list[j] - 1, r);
267 const double c = L.stations[ms_list[j] - 1].nservers;
268 mu.push_back([Dq, c](const std::vector<int>& n) { return detail::ms_oi_rate<T>(n, Dq, c); });
269 }
270
271 const pfqn::MvaoiResult<T> res = pfqn::pfqn_mvaoi(Z, N, mu, Dli, /*want_soi=*/true);
272
273 Matrix<T> QN(M, R, zero), TN(M, R, zero), RN(M, R, zero), UN(M, R, zero);
274 for (std::size_t o = 0; o < oi_list.size(); ++o)
275 for (std::size_t r = 0; r < R; ++r) QN(oi_list[o] - 1, r) = res.Qoi(o, r);
276 for (std::size_t j = 0; j < ms_list.size(); ++j)
277 for (std::size_t r = 0; r < R; ++r)
278 QN(ms_list[j] - 1, r) = res.Qoi(oi_list.size() + j, r);
279 for (std::size_t a = 0; a < li_list.size(); ++a)
280 for (std::size_t r = 0; r < R; ++r) QN(li_list[a] - 1, r) = res.Qli(a, r);
281 // a delay station takes its exact product-form share, X * D
282 for (std::size_t i = 0; i < M; ++i)
283 if (isDelay[i])
284 for (std::size_t r = 0; r < R; ++r) QN(i, r) = T(res.X[r] * D(i, r));
285
286 // which row of Soi / Qoi holds each OI station
287 std::vector<std::size_t> oiRow(M, 0);
288 for (std::size_t o = 0; o < oi_list.size(); ++o) oiRow[oi_list[o] - 1] = o + 1;
289
290 for (std::size_t i = 0; i < M; ++i)
291 for (std::size_t r = 0; r < R; ++r) {
292 TN(i, r) = T(res.X[r] * V(i, r));
293 if (res.X[r] > zero) RN(i, r) = T(QN(i, r) / res.X[r]);
294 const double sv_raw = L.stations[i].nservers;
295 const double sv = (std::isfinite(sv_raw) && sv_raw > 0.0) ? sv_raw : 1.0;
296 if (isOI[i]) {
297 // IN-SERVICE utilization, E[sir_r]/nservers, the convention
298 // solver_nc_oi_analyzer and the exact CTMC/LDES also use: sir_r
299 // counts the class-r jobs receiving a strictly positive rank
300 // rate, which Soi holds.
301 UN(i, r) = T(res.Soi(oiRow[i] - 1, r) / num_traits<T>::from_double(sv));
302 } else if (isDelay[i]) {
303 UN(i, r) = QN(i, r);
304 } else {
305 UN(i, r) = T(res.X[r] * D(i, r) / num_traits<T>::from_double(sv));
306 }
307 }
308
309 MvaSolution<T> out;
310 out.Q = QN;
311 out.U = UN;
312 out.R = RN;
313 out.Tp = TN;
314 out.X = res.X;
315 // the reference reports CN as the per-station response time, not as a
316 // per-class system time; it returns the RN matrix itself
317 out.C.assign(R, zero);
318 for (std::size_t r = 0; r < R; ++r)
319 for (std::size_t i = 0; i < M; ++i) out.C[r] += RN(i, r);
320 out.method = "oi";
321 int iter = 0;
322 for (int v : N) iter += v;
323 out.iter = iter;
324 out.lG = 0.0;
325 return out;
326}
327
328} // namespace mva
329} // namespace line
330
331#endif // LINE_SOLVERS_MVA_SOLVER_MVA_OI_H
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
std::size_t stateful_of_station(std::size_t st) const
std::vector< JobClass > classes
std::vector< Station< T > > stations
stations[k-1] is the k-th station
Matrix< T > rates
(nstations x nclasses) service rates and SCVs, with a PARALLEL disabled flag instead of MATLAB's NaN ...
std::vector< std::vector< std::size_t > > inchain
1-based class indices per chain
std::vector< Matrix< T > > visits
(nchains) each (nstateful x nclasses)
The option and result types every MVA analyzer shares.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
Definition lang_types.h:194
std::vector< std::size_t > find_oi_stations(const qn::NetworkStruct< T > &L)
The OI stations of a model, 1-based station indices.
MvaSolution< T > solver_mva_oi_analyzer(const qn::NetworkStruct< T > &L, const MvaOptions &opt)
Port of solver_mva_oi_analyzer.m.
bool nc_is_oi_model(const qn::NetworkStruct< T > &L)
Port of nc_is_oi_model: whether the model as a WHOLE is order-independent, which is a strictly strong...
mva::MvaSolution< T > solver_oi_open_analyzer(const qn::NetworkStruct< T > &sn, double tol)
Port of solver_oi_open_analyzer.m.
MvaoiResult< T > pfqn_mvaoi(const std::vector< T > &Z, const std::vector< int > &N, const std::vector< std::function< T(const std::vector< int > &)> > &mu, const Matrix< T > &Dli, const Matrix< T > &visits, bool want_soi)
Mean-value analysis of a closed network with order-independent (OI) stations, the composition-depende...
Definition pfqn_mvaoi.h:122
bool station_swap_graph_is_zero(const NetworkStruct< T > &sn, std::size_t ist)
True when the station's materialized swap graph is entirely zero.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
Mean-value analysis of a closed network with order-independent (OI) stations, the composition-depende...
Exact analysis of an OPEN queueing network mixing order-independent (OI) stations with BCMP product-f...
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
The options SolverMVA reads.
Definition mva_types.h:32
Class-level results, the [Q,U,R,T,C,X] of the MATLAB analyzers.
Definition mva_types.h:97
std::vector< T > X
Definition mva_types.h:99
double lG
log of the normalizing constant, the reference's lG.
Definition mva_types.h:119
std::vector< T > C
Definition mva_types.h:99
Return value of pfqn_mvaoi, mirroring [X, Qoi, Qli, Qdelay, Soi].
Definition pfqn_mvaoi.h:82
Matrix< T > Qoi
(K x R) per-class queue length at each OI station
Definition pfqn_mvaoi.h:84
Matrix< T > Qli
(J x R) per-class queue length at each LI queue
Definition pfqn_mvaoi.h:85
std::vector< T > X
(R) per-class throughput
Definition pfqn_mvaoi.h:83
Matrix< T > Soi
(K x R) per-class mean in-service jobs, if requested
Definition pfqn_mvaoi.h:87
One station of the network.
std::function< T(const std::vector< std::size_t > &)> svc_rate_fun
sn.nodeparam{ind}.svcRateFun for a PAS / OI station: the TOTAL service rate as a function of the orde...
SchedStrategy sched