LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
solver_oi_open_cdfrespt.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_NC_SOLVER_OI_OPEN_CDFRESPT_H
6#define LINE_SOLVERS_NC_SOLVER_OI_OPEN_CDFRESPT_H
7
8/**
9 * @file
10 * @ingroup line_solvers
11 * Per-visit response-time distributions of an OPEN network of order-independent
12 * (OI) and BCMP stations: the getCdfRespT route of SolverNC for the models
13 * `solver_oi_open_analyzer` solves.
14 *
15 * Port of `solver_oi_open_cdfrespt.m`. Every station is quasi-reversible, so in
16 * an open network it sees Poisson traffic of rate lambda_ir and its sojourn-time
17 * law is that of the isolated station:
18 * - OI station whose rate is a NESTED collaborative pool: the phase-type law
19 * of `pfqn_oi_respt` (Gardner, Righter 2020, Thm 4.5);
20 * - INF: the service law;
21 * - c-server FCFS with a class-independent exponential rate mu: the M/M/c
22 * sojourn, Exp(mu) plus an Erlang-C-weighted Exp(c*mu - lambda), as a
23 * 2-phase PH.
24 * Anything else is refused by name (UnsupportedError, kind "Analysis", the
25 * reference's LINE:unsupported:Analysis): no response-time theorem is known for
26 * a general OI rate, PS, LCFS-PR or SIRO companions have no phase-type sojourn
27 * law of this form, and the OI loss queue and unstable stations have no
28 * stationary Poisson-fed sojourn. A model `nc_oi_refusal` rejects raises with
29 * kind "Structure", as the reference's LINE:unsupported:Structure.
30 *
31 * RD[i][r] is [F(t) t] on a common logarithmic grid of 100 points spanning two
32 * decades below to one above the largest mean sojourn; Source rows and classes
33 * with no traffic at a station are empty.
34 *
35 * ARITHMETIC. map_cdf and the grid are transcendental, so a non-transcendental
36 * backend is refused by name.
37 */
38
39#include <algorithm>
40#include <cmath>
41#include <cstddef>
42#include <limits>
43#include <string>
44#include <vector>
45
53#include "line/util/error.h"
54#include "line/util/matrix.h"
55
56namespace line {
57namespace nc {
58
59/** Return value of solver_oi_open_cdfrespt: RD{ist,r} = [F(t) t] and the shared grid. */
60template <class T>
62 std::vector<std::vector<Matrix<T> > > RD;
63 std::vector<T> tset;
64};
65
66namespace detail {
67
68/** Erlang-C probability of waiting in an M/M/c queue with offered load a < c, as `erlang_c`. */
69inline double oi_open_erlang_c(double a, double c) {
70 if (a <= 0.0) return 0.0;
71 const int ci = static_cast<int>(c);
72 std::vector<double> logterms(static_cast<std::size_t>(ci));
73 double mx = -std::numeric_limits<double>::infinity();
74 for (int k = 0; k < ci; ++k) {
75 logterms[static_cast<std::size_t>(k)] = k * std::log(a) - std::lgamma(k + 1.0);
76 mx = std::max(mx, logterms[static_cast<std::size_t>(k)]);
77 }
78 const double lastlog = c * std::log(a) - std::lgamma(c + 1.0) - std::log(1.0 - a / c);
79 mx = std::max(mx, lastlog);
80 double S = std::exp(lastlog - mx);
81 for (double lt : logterms) S += std::exp(lt - mx);
82 return std::exp(lastlog - mx) / S;
83}
84
85} // namespace detail
86
87/**
88 * Port of `solver_oi_open_cdfrespt.m`.
89 *
90 * @param sn the refreshed struct of a fully OPEN network
91 * @param tol the tolerance passed to the saturation search of pfqn_oi_respt
92 * (the solver's options.tol, as the reference passes its options)
93 */
94template <class T>
97 if constexpr (!num_traits<T>::has_transcendental) {
98 (void)sn;
99 (void)tol;
100 throw UnsupportedError(ErrorKind{"Arithmetic"},
101 "solver_oi_open_cdfrespt: the sojourn-time laws are evaluated by a matrix "
102 "exponential on a logarithmic time grid; this backend has no transcendental "
103 "arithmetic");
104 } else {
105 const T zero = num_traits<T>::from_int(0);
106 const std::size_t M = sn.nstations, K = sn.nclasses;
107 for (const qn::JobClass& c : sn.classes)
108 if (!std::isinf(c.population))
109 throw UnsupportedError(ErrorKind{"Analysis"},
110 "solver_oi_open_cdfrespt serves open networks only.");
111 const std::string oiReason = nc_oi_refusal(sn, "oi");
112 if (!oiReason.empty()) throw UnsupportedError(ErrorKind{"Structure"}, oiReason);
113 if (qn::is_oi_loss(sn))
114 throw UnsupportedError(ErrorKind{"Analysis"},
115 "getCdfRespT has no exact law for the order-independent LOSS queue; use SolverCTMC "
116 "or SolverLDES.");
117 const Matrix<T> L = oi_open_traffic(sn).L;
119 kopt.tol = tol;
120 std::vector<std::vector<mam::Map<T> > > laws(M, std::vector<mam::Map<T> >(K));
121 for (std::size_t i = 0; i < M; ++i) {
122 const qn::Station<T>& st = sn.stations[i];
123 if (st.nodetype == qn::NodeType::Source) continue;
124 const std::string name = st.name;
125 if (st.sched == SchedStrategy::OI || st.sched == SchedStrategy::PAS) {
126 std::vector<T> li(K, zero);
127 for (std::size_t r = 0; r < K; ++r) li[r] = L(i, r);
128 const pfqn::OiResptResult<T> ph =
129 pfqn::pfqn_oi_respt<T>(li, detail::oi_open_rate(st), kopt);
130 if (!ph.ok) {
131 std::string msg;
132 if (ph.why == "unstable")
133 msg = "station '" + name + "' is unstable, so it has no stationary response time.";
134 else if (ph.why == "not support-only")
135 msg = "the rate of OI station '" + name +
136 "' depends on how many jobs of a class are present; the exact "
137 "response-time law (Gardner, Righter 2020) needs a collaborative server "
138 "pool, whose rate depends only on which classes are present.";
139 else if (ph.why == "not nested")
140 msg = "two classes at OI station '" + name +
141 "' share some but not all servers; the exact response-time law "
142 "(Gardner, Righter 2020) needs a NESTED compatibility structure.";
143 else if (ph.why == "not a pool")
144 msg = "the rate of OI station '" + name +
145 "' is not the rate of a nested collaborative server pool, which the "
146 "exact response-time law (Gardner, Righter 2020) requires.";
147 else
148 msg = "OI station '" + name + "' has more than 16 classes with traffic.";
149 throw UnsupportedError(ErrorKind{"Analysis"},
150 "getCdfRespT: " + msg +
151 " Use SolverCTMC, SolverSSA or SolverLDES for this model.");
152 }
153 for (std::size_t r = 0; r < K; ++r)
154 if (L(i, r) > zero) laws[i][r] = ph.PH[r];
155 } else if (st.sched == SchedStrategy::INF) {
156 for (std::size_t r = 0; r < K; ++r)
157 if (L(i, r) > zero) laws[i][r] = lang::dist_to_map(sn.service[i][r]);
158 } else if (st.sched == SchedStrategy::FCFS) {
159 T a = zero;
160 std::size_t rpos = K; // first class with traffic
161 for (std::size_t r = 0; r < K; ++r) {
162 if (!(L(i, r) > zero)) continue;
163 if (rpos == K) rpos = r;
164 const double rate = num_traits<T>::to_double(sn.rates(i, r));
165 if (!std::isfinite(rate) || !(rate > 0.0)) continue;
166 const T rho = T(L(i, r) / sn.rates(i, r));
167 if (std::isfinite(num_traits<T>::to_double(rho))) a += rho;
168 }
169 double c = st.nservers;
170 if (!std::isfinite(c) || c <= 0.0) c = 1.0;
171 if (num_traits<T>::to_double(a) >= c)
172 throw UnsupportedError(ErrorKind{"Analysis"},
173 "getCdfRespT: station '" + name +
174 "' is unstable, so it has no stationary response time.");
175 if (rpos == K) continue;
176 const T mu = sn.rates(i, rpos); // class-independent, gated by nc_oi_refusal
177 const T PC = num_traits<T>::from_double(
178 detail::oi_open_erlang_c(num_traits<T>::to_double(a), c));
179 const T cT = num_traits<T>::from_double(c);
180 const T gap = T(cT * mu - a * mu);
181 mam::Map<T> law;
182 law.D0 = Matrix<T>(2, 2, zero);
183 law.D0(0, 0) = T(-gap);
184 law.D0(0, 1) = gap;
185 law.D0(1, 1) = T(-mu);
186 const T one = num_traits<T>::from_int(1);
187 const T alpha[2] = {PC, T(one - PC)};
188 law.D1 = Matrix<T>(2, 2, zero);
189 for (std::size_t p = 0; p < 2; ++p) {
190 const T ex = T(-(law.D0(p, 0) + law.D0(p, 1)));
191 for (std::size_t q = 0; q < 2; ++q) law.D1(p, q) = T(ex * alpha[q]);
192 }
193 for (std::size_t r = rpos; r < K; ++r)
194 if (L(i, r) > zero) laws[i][r] = law;
195 } else {
196 throw UnsupportedError(ErrorKind{"Analysis"},
197 "getCdfRespT on an open order-independent network supports OI, INF and FCFS "
198 "stations; station '" + name + "' is " + lang::sched_to_text(st.sched) +
199 ". Use SolverCTMC, SolverSSA or SolverLDES for this model.");
200 }
201 }
202 // common grid around the largest mean sojourn
203 double Tm = 0.0;
204 for (std::size_t i = 0; i < M; ++i)
205 for (std::size_t r = 0; r < K; ++r)
206 if (laws[i][r].order() > 0)
207 Tm = std::max(Tm, num_traits<T>::to_double(mam::map_mean(laws[i][r])));
209 out.RD.assign(M, std::vector<Matrix<T> >(K));
210 if (!(Tm > 0.0)) return out;
211 const std::size_t npts = 100;
212 const double lo = std::log10(Tm) - 2.0;
213 out.tset.assign(npts, zero);
214 for (std::size_t j = 0; j < npts; ++j) {
215 const double e = lo + 3.0 * static_cast<double>(j) / static_cast<double>(npts - 1);
216 out.tset[j] = num_traits<T>::from_double(std::pow(10.0, e));
217 }
218 for (std::size_t i = 0; i < M; ++i)
219 for (std::size_t r = 0; r < K; ++r) {
220 if (laws[i][r].order() == 0) continue;
221 const std::vector<T> F = mam::map_cdf(laws[i][r], out.tset);
222 Matrix<T> A(npts, 2, zero);
223 for (std::size_t j = 0; j < npts; ++j) {
224 A(j, 0) = F[j];
225 A(j, 1) = out.tset[j];
226 }
227 out.RD[i][r] = A;
228 }
229 return out;
230 }
231}
232
233} // namespace nc
234} // namespace line
235
236#endif // LINE_SOLVERS_NC_SOLVER_OI_OPEN_CDFRESPT_H
UnsupportedError(const std::string &what)
Definition error.h:119
A network plus its refreshed NetworkStruct.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
Cumulative distribution of the inter-arrival time of a MAP.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
mam::Map< T > dist_to_map(const Distrib< T > &d)
The (D0,D1) pair that reaches sn.proc.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
Definition lang_types.h:194
const char * sched_to_text(SchedStrategy s)
Definition lang_types.h:248
T map_mean(const Map< T > &m)
Mean inter-arrival time, 1/lambda.
Definition map_moment.h:101
std::vector< T > map_cdf(const Map< T > &m, const std::vector< T > &points)
Cumulative distribution of the inter-arrival time at the given points.
Definition map_cdf.h:63
OiOpenTraffic< T > oi_open_traffic(const qn::NetworkStruct< T > &sn)
Port of oi_open_traffic.m: the station traffic of an OPEN network.
std::string nc_oi_refusal(const qn::NetworkStruct< T > &sn, const std::string &route="oi")
Port of nc_oi_refusal.m: may the order-independent route ROUTE run on this model?
OiOpenCdfRespT< T > solver_oi_open_cdfrespt(const qn::NetworkStruct< T > &sn, double tol)
Port of solver_oi_open_cdfrespt.m.
OiResptResult< T > pfqn_oi_respt(const std::vector< T > &lambda, const std::function< T(const std::vector< int > &)> &mu, const OiMixedOptions &opt=OiMixedOptions())
Per-class phase-type sojourn laws of an open nested-pool OI station.
bool is_oi_loss(const NetworkStruct< T > &sn, std::size_t *qist=nullptr)
Port of sn_is_oi_loss.m: an open Source -> Queue -> Sink system (any number of classes,...
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
Exact per-class sojourn-time distribution at an OPEN order-independent (OI) station whose rate is tha...
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
A MAP as the pair of matrices (D0, D1).
Definition map_moment.h:53
Matrix< T > D1
Definition map_moment.h:55
Matrix< T > D0
Definition map_moment.h:54
Return value of solver_oi_open_cdfrespt: RD{ist,r} = [F(t) t] and the shared grid.
std::vector< std::vector< Matrix< T > > > RD
The tuning fields of pfqn_oi_mixed / pfqn_oi_open (OPTIONS in the reference).
double tol
relative tolerance of the 'oi.trunc' branch
Return value of pfqn_oi_respt, mirroring [PH, ok, why].
bool ok
false when the theorem does not apply
std::string why
the reason when ok is false
std::vector< mam::Map< T > > PH
(R) PH[i] is the sojourn time of class i as a renewal MAP (D1 = -D0 * 1 * alpha), for every class wit...
One job class of the network.
double population
infinite for an open class
One station of the network.
SchedStrategy sched
double nservers
may be infinite (a Delay, or an inf-scheduled task)