5#ifndef LINE_SOLVERS_NC_SOLVER_OI_OPEN_H
6#define LINE_SOLVERS_NC_SOLVER_OI_OPEN_H
63inline std::vector<std::size_t> oi_open_microstate(
const std::vector<int>& n) {
64 std::vector<std::size_t> c;
65 for (std::size_t r = 0; r < n.size(); ++r)
66 for (
int a = 0; a < n[r]; ++a) c.push_back(r + 1);
72std::function<T(
const std::vector<int>&)> oi_open_rate(
const qn::Station<T>& st) {
73 const std::function<T(
const std::vector<std::size_t>&)> f = st.svc_rate_fun;
74 return [f](
const std::vector<int>& n) {
return f(oi_open_microstate(n)); };
79Matrix<T> oi_open_visits(
const qn::NetworkStruct<T>& sn) {
80 const T
zero = num_traits<T>::from_int(0);
81 const std::size_t M = sn.nstations, K = sn.nclasses;
82 Matrix<T> V(M, K, zero);
83 for (std::size_t r = 0; r < K; ++r) {
84 std::size_t chain = 0;
85 for (std::size_t c = 0; c < sn.nchains; ++c)
86 if (sn.chains[c][r]) chain = c + 1;
87 if (chain == 0)
continue;
88 for (std::size_t i = 0; i < M; ++i)
89 V(i, r) = sn.visits[chain - 1](sn.stateful_of_station(i + 1) - 1, r);
90 const T vref = V(sn.classes[r].refstat - 1, r);
92 for (std::size_t i = 0; i < M; ++i) V(i, r) = T(V(i, r) / vref);
98inline void oi_open_erlangc_len(
double a,
double c,
double& Ltot,
double& P0) {
104 const double la = std::log(std::max(a, std::numeric_limits<double>::min()));
105 const int ci =
static_cast<int>(c);
106 std::vector<double> logterms(
static_cast<std::size_t
>(ci));
107 double mx = -std::numeric_limits<double>::infinity();
108 for (
int k = 0; k < ci; ++k) {
109 logterms[
static_cast<std::size_t
>(k)] = k * la - std::lgamma(k + 1.0);
110 mx = std::max(mx, logterms[
static_cast<std::size_t
>(k)]);
112 const double lastlog = c * la - std::lgamma(c + 1.0) - std::log(1.0 - a / c);
113 mx = std::max(mx, lastlog);
114 double S = std::exp(lastlog - mx);
115 for (
double lt : logterms) S += std::exp(lt - mx);
116 P0 = std::exp(-mx) / S;
117 const double PC = std::exp(lastlog - mx) / S;
118 Ltot = a + PC * (a / c) / (1.0 - a / c);
126void oi_open_unstable_row(
const std::vector<double>& load, Matrix<T>& QN, Matrix<T>& UN,
129 for (
double v : load) s += v;
130 const T inf = num_traits<T>::from_double(std::numeric_limits<double>::infinity());
131 for (std::size_t r = 0; r < load.size(); ++r) {
132 QN(i, r) = load[r] > 0.0 ? inf : num_traits<T>::from_int(0);
133 UN(i, r) = num_traits<T>::from_double(s > 0.0 ? load[r] / s : 0.0);
151 const bool pas = (route ==
"pas" || route ==
"PAS");
152 const std::string who = pas ?
"solver_nc_pas_is" :
"solver_nc_oi";
153 for (std::size_t c = 0; c <
sn.nchains; ++c)
154 if (
sn.inchain[c].size() > 1)
return who +
" requires one class per chain (no class switching).";
155 bool anyOpen =
false;
162 if (pas)
return who +
" requires a closed queueing network.";
169 for (std::size_t i = 0; i <
sn.nstations; ++i) {
171 if (s != SchedStrategy::OI && s != SchedStrategy::PAS)
continue;
172 bool finite = i <
sn.cap.size() && std::isfinite(
sn.cap[i]);
173 if (!finite && i <
sn.classcap.size())
174 for (std::size_t r = 0; r <
sn.nclasses && r <
sn.classcap[i].size(); ++r)
175 if (std::isinf(
sn.classes[r].population) && std::isfinite(
sn.classcap[i][r]))
178 return who +
": open OI station " + std::to_string(i + 1) +
179 " has a finite buffer, which makes it a loss queue rather than a "
180 "quasi-reversible OI station; only an isolated Source -> OI -> Sink "
181 "loss queue is solved exactly. Remove the cap or use "
182 "SolverCTMC/SolverLDES.";
186 if (nd.
nodetype == qn::NodeType::Fork)
187 return who +
" cannot serve a fork-join model: the fork-join transformation carries "
188 "the parallelism as open auxiliary classes, and the order-independent "
189 "routes are stated for a closed network.";
193 return who +
" builds every BCMP station table from the server count and reads no "
194 "load-, class- or joint-dependent rate; declare the dependence through "
195 "the OI rank rate instead.";
196 const std::size_t M =
sn.nstations;
197 for (std::size_t i = 0; i < M; ++i) {
199 if (s != SchedStrategy::OI && s != SchedStrategy::PAS)
continue;
200 if (!
sn.stations[i].svc_rate_fun)
201 return "OI station " + std::to_string(i + 1) +
202 " has no service rate function; set it via setService(@(c) ...).";
206 return who +
" models a two-station pass-and-swap tandem (got " + std::to_string(M) +
209 for (std::size_t i = 0; i < M; ++i)
210 for (std::size_t r = 0; r <
sn.nclasses; ++r) {
212 if (
sn.classes[r].population > 0.0 && std::fabs(vir - 1.0) > 1e-9)
213 return who +
" requires unit per-class visits (station " + std::to_string(i + 1) +
214 ", class " + std::to_string(r + 1) +
", V=" + std::to_string(vir) +
").";
228 const std::size_t M =
sn.nstations, K =
sn.nclasses;
230 for (std::size_t i = 0; i < M; ++i)
231 if (
sn.stations[i].nodetype == qn::NodeType::Source) src = i + 1;
232 std::vector<T> lam(K, zero);
233 for (std::size_t r = 0; r < K; ++r) {
235 if (std::isfinite(l) && l > 0.0) lam[r] =
sn.rates(src - 1, r);
237 std::vector<double> Kr(K, std::numeric_limits<double>::infinity());
238 for (std::size_t r = 0; r < K && r <
sn.classcap[qist - 1].size(); ++r)
239 Kr[r] =
sn.classcap[qist - 1][r];
242 double sv =
sn.stations[qist - 1].nservers;
243 if (!std::isfinite(sv) || sv <= 0.0) sv = 1.0;
249 out.
C.assign(K, zero);
251 for (std::size_t r = 0; r < K; ++r) {
252 out.
Q(qist - 1, r) = lr.
Q[r];
254 out.
Tp(qist - 1, r) = lr.
Xadm[r];
255 out.
Tp(src - 1, r) = lam[r];
256 if (lr.
Xadm[r] > zero) out.
R(qist - 1, r) = T(lr.
Q[r] / lr.
Xadm[r]);
257 out.
C[r] = out.
R(qist - 1, r);
280 const std::size_t M =
sn.nstations, K =
sn.nclasses;
282 out.
lam.assign(K, zero);
283 for (std::size_t r = 0; r < K; ++r) {
284 const std::size_t ref =
sn.classes[r].refstat;
286 if (std::isfinite(l) && l > 0.0) out.
lam[r] =
sn.rates(ref - 1, r);
288 out.
V = detail::oi_open_visits(
sn);
290 for (std::size_t i = 0; i < M; ++i)
291 for (std::size_t r = 0; r < K; ++r) out.
L(i, r) = T(out.
V(i, r) * out.
lam[r]);
310 "solver_oi_open_analyzer: the Erlang-C station length, lG = -sum log P0 and the Inf "
311 "of an unstable station are transcendental; this backend has no transcendental "
315 const std::size_t M =
sn.nstations, K =
sn.nclasses;
319 "solver_oi_open_analyzer serves open networks only.");
322 std::size_t qist = 0;
327 const std::vector<T>& lam =
tr.lam;
339 bool trunc =
false, anyUnstable =
false;
340 for (std::size_t i = 0; i < M; ++i) {
342 for (std::size_t r = 0; r < K; ++r) out.
Tp(i, r) = L(i, r);
343 if (st.
nodetype == qn::NodeType::Source)
continue;
345 if (st.
sched == SchedStrategy::OI || st.
sched == SchedStrategy::PAS) {
346 std::vector<T> li(K, zero);
347 for (std::size_t r = 0; r < K; ++r) li[r] = L(i, r);
350 if (res.
method ==
"oi.trunc") trunc =
true;
351 if (!std::isfinite(sv) || sv <= 0.0) sv = 1.0;
353 for (std::size_t r = 0; r < K; ++r) {
354 out.
Q(i, r) = res.
Q[r];
360 std::vector<double> load(K, 0.0);
361 for (std::size_t r = 0; r < K; ++r) load[r] = num_traits<T>::to_double(L(i, r));
362 detail::oi_open_unstable_row(load, out.
Q, out.
U, i);
364 }
else if (st.
sched == SchedStrategy::INF) {
365 for (std::size_t r = 0; r < K; ++r) {
368 if (std::isfinite(rate) && rate > 0.0) rho = T(L(i, r) /
sn.rates(i, r));
375 std::vector<double> rho(K, 0.0);
377 for (std::size_t r = 0; r < K; ++r) {
380 if (std::isfinite(rate) && rate > 0.0 && lir > 0.0) rho[r] = lir / rate;
381 if (!std::isfinite(rho[r])) rho[r] = 0.0;
384 if (!std::isfinite(sv) || sv <= 0.0) sv = 1.0;
387 detail::oi_open_unstable_row(rho, out.
Q, out.
U, i);
390 double Ltot = 0.0, P0 = 1.0;
391 detail::oi_open_erlangc_len(a, sv, Ltot, P0);
392 for (std::size_t r = 0; r < K; ++r) {
399 for (std::size_t i = 0; i < M; ++i)
400 for (std::size_t r = 0; r < K; ++r)
401 if (out.
Tp(i, r) > zero) out.
R(i, r) = T(out.
Q(i, r) / out.
Tp(i, r));
404 out.
C.assign(K, zero);
405 for (std::size_t r = 0; r < K; ++r)
406 for (std::size_t i = 0; i < M; ++i)
407 if (
sn.stations[i].nodetype != qn::NodeType::Source && V(i, r) > zero)
408 out.
C[r] += T(out.
R(i, r) * V(i, r));
410 out.
lG = anyUnstable ? std::numeric_limits<double>::infinity() : lG;
412 out.
method = trunc ?
"oi.trunc" :
"oi";
UnsupportedError(const std::string &what)
A network plus its refreshed NetworkStruct.
The exception types the port throws.
The option and result types every MVA analyzer shares.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
OiOpenTraffic< T > oi_open_traffic(const qn::NetworkStruct< T > &sn)
Port of oi_open_traffic.m: the station traffic of an OPEN network.
mva::MvaSolution< T > solver_oi_open_analyzer(const qn::NetworkStruct< T > &sn, double tol)
Port of solver_oi_open_analyzer.m.
mva::MvaSolution< T > solver_oi_loss_station(const qn::NetworkStruct< T > &sn, std::size_t qist)
Port of the oi_loss_station branch of solver_oi_open_analyzer.m: the isolated OI loss queue,...
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?
OiOpenResult< T > pfqn_oi_open(const std::vector< T > &lambda, const std::function< T(const std::vector< int > &)> &mu, const OiMixedOptions &opt=OiMixedOptions())
Exact means of an open 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,...
OiLossResult< T > qsys_oi_loss(const std::vector< T > &lambda, const std::function< T(const std::vector< int > &)> &mu, double K, std::vector< double > Kr=std::vector< double >())
Exact OI loss queue.
Conservation laws of a layered queueing network, enumerated from its structure.
A queueing network and its refreshed NetworkStruct.
Exact steady-state means of an OPEN order-independent (OI) station fed by Poisson traffic.
Exact steady state of an order-independent (OI) LOSS queue.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Class-level results, the [Q,U,R,T,C,X] of the MATLAB analyzers.
double lG
log of the normalizing constant, the reference's lG.
Return value of oi_open_traffic, mirroring [L, lam] (plus the visits it is built from).
std::vector< T > lam
(K) class-r arrival rate at its Source (0 when not finite and positive)
Matrix< T > L
(M x K) class-r traffic offered to station i, lambda_r * V(i,r)
Matrix< T > V
(M x K) visits normalized to each class's reference station
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_open, mirroring [Q, Usir, P0, stable, tau, method].
std::vector< T > Usir
(R) mean number of class-r jobs in service (Inf when unstable)
std::vector< T > Q
(R) mean number of class-r jobs E[n_r] (Inf when unstable)
std::string method
"oi" (exact) or "oi.trunc"
bool stable
false when some reachable macrostate has den(m) <= 0
T P0
probability that the station is empty (0 when unstable)
One job class of the network.
double population
infinite for an open class
One station of the network.
double nservers
may be infinite (a Delay, or an inf-scheduled task)
CdScaling< T > jdscaling
sn.jdscaling for this station: MATLAB's Station.ljdScaling, the JOINT dependence map eta_i(n),...
std::vector< T > lldscaling
sn.lldscaling for this station: the multiplier at population 1, 2, ... Empty when the station is not ...
CdScaling< T > cdscaling
sn.cdscaling for this station: the class-dependence map, empty when unset.
Return value of qsys_oi_loss, mirroring [Q, Usir, B, Xadm, P0].
std::vector< T > Xadm
(R) admitted throughput lambda_r (1 - B_r)
std::vector< T > Q
(R) mean number of class-r jobs
T P0
probability of the empty station
std::vector< T > Usir
(R) mean number of class-r jobs in service