5#ifndef LINE_SOLVERS_NC_SOLVER_NC_LOSSN_H
6#define LINE_SOLVERS_NC_SOLVER_NC_LOSSN_H
144 bool anyFlag =
false;
146 if (b) anyFlag =
true;
148 std::vector<std::size_t> out;
149 for (std::size_t i = 0; i <
sn.nstations; ++i) {
153 }
else if (i < rg.
cap.size()) {
154 for (
double c : rg.
cap[i])
155 if (c >= 0.0) in =
true;
156 if (i < rg.
maxmem.size() && rg.
maxmem[i] >= 0.0) in =
true;
158 if (in) out.push_back(i + 1);
172 std::size_t K,
Matrix<T>& A, std::vector<T>& Cvec,
173 std::vector<std::string>* names =
nullptr) {
176 const std::vector<double>& caps = rg.
cap[st - 1];
178 std::vector<std::vector<T>> rows;
180 std::vector<std::string> nm;
183 if (K < caps.size() && caps[K] >= 0.0) {
184 rows.push_back(std::vector<T>(K, one));
186 nm.push_back(
"GlobalMaxJobs");
191 if (st - 1 < rg.
maxmem.size() && rg.
maxmem[st - 1] >= 0.0) {
192 std::vector<T> row(K, one);
193 for (std::size_t r = 0; r < K && r < rg.
size.size(); ++r) row[r] = rg.
size[r];
195 rhs.push_back(num_traits<T>::from_double(rg.
maxmem[st - 1]));
196 nm.push_back(
"GlobalMaxMemory");
201 for (std::size_t r = 0; r < K && r < caps.size(); ++r) {
202 if (caps[r] < 0.0)
continue;
203 std::vector<T> row(K, zero);
206 rhs.push_back(num_traits<T>::from_double(caps[r]));
207 nm.push_back(
"ClassMaxJobs:" + (r < sn.
classes.size() ? sn.
classes[r].name : std::string()));
211 for (std::size_t k = 0; k < rg.
lincon_A.rows(); ++k) {
212 std::vector<T> row(K, zero);
213 for (std::size_t r = 0; r < K && r < rg.
lincon_A.cols(); ++r) row[r] = rg.
lincon_A(k, r);
216 nm.push_back(
"Constraint" + std::to_string(k + 1));
221 "solver_nc_lossn_analyzer: the finite capacity region declares no bounded constraint, "
222 "so it admits every arrival and is not a loss network; give it a global job cap, a "
223 "memory budget, a per-class cap or an explicit linear constraint");
226 for (std::size_t j = 0; j < rows.size(); ++j)
227 for (std::size_t r = 0; r < K; ++r) A(j, r) = rows[j][r];
229 if (names) *names = nm;
233inline std::vector<std::string> lossn_tokens(
const std::string& method) {
234 std::vector<std::string> toks;
236 for (
char ch : method) {
237 if (ch ==
'.' || ch ==
'/') {
241 tok +=
static_cast<char>(std::tolower(
static_cast<unsigned char>(ch)));
248inline bool lossn_has_token(
const std::vector<std::string>& toks,
const char* what) {
249 for (
const std::string& t : toks)
250 if (t == what)
return true;
274 if (
sn.regions.size() != 1)
return false;
275 const std::vector<std::size_t> mem = detail::lossn_region_members(
sn, 0);
276 if (mem.size() != 1)
return false;
277 const std::size_t st = mem[0];
278 if (st == 0 || st >
sn.stations.size())
return false;
279 if (std::isfinite(
sn.stations[st - 1].nservers))
return false;
304 if (
sn.regions[0].rule.size() <
sn.nclasses)
return false;
305 for (std::size_t r = 0; r <
sn.nclasses; ++r)
322 const std::size_t K =
sn.nclasses, M =
sn.nstations;
325 const std::vector<std::size_t> mem = detail::lossn_region_members(
sn, 0);
326 if (
sn.regions.empty())
328 "solver_nc_lossn_analyzer: the model declares no finite capacity region, so it is "
329 "not a loss network");
332 "solver_nc_lossn_analyzer: the finite capacity region holds " +
333 std::to_string(mem.size()) +
334 " stations; the admission rule can only see the occupancy of the region as a "
335 "whole, so the carried load could not be attributed to a station");
336 const std::size_t delayIdx = mem[0];
337 if (
sn.stations[delayIdx - 1].nodetype != qn::NodeType::Delay)
339 "solver_nc_lossn_analyzer: the station inside the finite capacity region is not a "
340 "Delay; a loss network holds admitted jobs at an infinite server, and a queueing "
341 "station would make the response time depend on the population");
347 out.
nu.assign(K, zero);
348 std::vector<T> lambda(K, zero), mu(K, zero);
349 const std::size_t dsf =
sn.stateful_of_station(delayIdx);
350 for (std::size_t r = 0; r < K; ++r) {
351 const std::size_t src =
sn.classes[r].refstat;
352 if (src == 0 || src > M)
continue;
353 if (!
sn.disabled[src - 1][r]) lambda[r] =
sn.rates(src - 1, r);
357 if (
sn.disabled[delayIdx - 1][r])
continue;
358 mu[r] =
sn.rates(delayIdx - 1, r);
361 "solver_nc_lossn_analyzer: class " + std::to_string(r + 1) +
362 " is enabled at the delay with a zero service rate, so its mean holding time "
363 "in the region is unbounded and its offered load is undefined");
366 const std::size_t rsf =
sn.stateful_of_station(src);
367 for (std::size_t c = 0; c <
sn.nchains; ++c) {
368 if (!
sn.chains[c][r])
continue;
369 const T vref =
sn.visits[c](rsf - 1, r);
370 if (vref > zero) V = T(
sn.visits[c](dsf - 1, r) / vref);
373 out.
nu[r] = T(lambda[r] * V / mu[r]);
377 std::vector<std::string> linknames;
378 detail::lossn_region_constraints(
sn, 0, delayIdx, K, out.
A, out.
Cvec, &linknames);
379 const std::size_t J = out.
Cvec.size();
384 const std::vector<std::string> toks = detail::lossn_tokens(
opt.method);
385 bool integral =
true;
386 for (std::size_t j = 0; j < J; ++j) {
388 if (std::fabs(c - std::round(c)) > 1e-9) integral =
false;
389 for (std::size_t r = 0; r < K; ++r) {
391 if (std::fabs(a - std::round(a)) > 1e-9) integral =
false;
396 if (detail::lossn_has_token(toks,
"mci")) {
398 }
else if (detail::lossn_has_token(toks,
"erlangfp")) {
400 }
else if (detail::lossn_has_token(toks,
"asy")) {
402 }
else if (detail::lossn_has_token(toks,
"rec")) {
404 }
else if (detail::lossn_has_token(toks,
"exact") ||
405 detail::lossn_has_token(toks,
"manjunath") ||
406 detail::lossn_has_token(toks,
"ms")) {
416 chosen = integral ?
"exact" :
"rec";
419 double lG = std::numeric_limits<double>::quiet_NaN();
420 std::vector<T> QLen(K, zero);
421 out.
Loss.assign(K, zero);
425 if (chosen ==
"rec") {
431 method =
"lossn.rec";
432 }
else if (chosen ==
"exact") {
441 method =
"lossn.exact";
442 }
else if (chosen ==
"asy") {
445 "solver_nc_lossn_analyzer: the 'asy' method solves the Kelly limit by Newton "
446 "steps on exponentials that stop on a tolerance, so it is unavailable under "
447 "exact arithmetic and would not be exact there anyway. Use 'exact', the "
448 "Manjunath-Sikdar transform");
456 method =
"lossn.asy";
458 }
else if (chosen ==
"mci") {
461 "solver_nc_lossn_analyzer: the 'mci' method forms its importance weights in "
462 "log space and reports a confidence interval, so it is unavailable under exact "
463 "arithmetic -- and meaningless there in any case, since the estimate is a "
464 "random variable. Use 'exact', the Manjunath-Sikdar transform, which is "
465 "rational throughout");
471 static_cast<std::uint64_t
>(
opt.seed));
479 niter =
static_cast<int>(mr.
nsamples);
480 method =
"lossn.mci";
485 "solver_nc_lossn_analyzer: the 'erlangfp' method stops on a tolerance, so it "
486 "is unavailable under "
487 "exact arithmetic and would not be exact there anyway. Use 'exact', the "
488 "Manjunath-Sikdar transform");
495 "solver_nc_lossn_analyzer: the 'erlangfp' method is not offered on a "
496 "region with fractional circuit requirements or capacity (a memory budget "
497 "with fractional class sizes, or an explicit linear constraint). Use "
498 "'mci', which compares in real arithmetic, or 'rec', which is exact there");
506 niter =
static_cast<int>(fr.iterations);
507 method =
"lossn.erlangfp";
517 out.
sol.sol.C.assign(K, zero);
518 out.
sol.sol.X.assign(K, zero);
519 for (std::size_t r = 0; r < K; ++r) {
520 const std::size_t src =
sn.classes[r].refstat;
521 const T Xc = T(lambda[r] * (one - out.
Loss[r]));
522 out.
sol.sol.X[r] = Xc;
523 out.
sol.sol.Tp(delayIdx - 1, r) = Xc;
528 if (src >= 1 && src <= M) out.
sol.sol.Tp(src - 1, r) = Xc;
529 out.
sol.sol.Q(delayIdx - 1, r) = QLen[r];
533 if (mu[r] != zero) out.
sol.sol.R(delayIdx - 1, r) = T(one / mu[r]);
534 out.
sol.sol.U(delayIdx - 1, r) = QLen[r];
537 out.
sol.sol.iter = niter;
538 out.
sol.sol.method = method;
539 out.
sol.actualmethod = method;
546 info.
nu.assign(K, 0.0);
549 info.
C.assign(J, 0.0);
550 for (std::size_t j = 0; j < J; ++j) {
554 info.
links = linknames;
556 if (chosen ==
"mci") {
558 info.
seed =
static_cast<std::uint64_t
>(
opt.seed);
UnsupportedError(const std::string &what)
A network plus its refreshed NetworkStruct.
std::vector< JobClass > classes
Damped fixed-point iteration, the shared driver of the decomposition algorithms.
The exception types the port throws.
Large-capacity (Kelly limit) approximation of loss networks.
Erlang fixed-point (reduced-load) approximation for a loss network.
Exact analysis of a loss network by the Manjunath-Sikdar transform.
Monte Carlo importance-sampling summation for product-form loss networks.
Exact analysis of a loss network by MDD-rec.
Dense matrix and non-owning view.
LossnAsyResult< T > lossn_asy(const std::vector< T > &nu, const Matrix< T > &A, const std::vector< T > &C)
Fluid (Kelly) limit of a loss network by convex duality.
LossnManjunathResult< T > lossn_manjunath(const std::vector< T > &nu, const Matrix< T > &A, const std::vector< T > &C, const LossnManjunathOptions &options=LossnManjunathOptions())
Exact normalizing constant, carried load and blocking of a loss network.
ErlangFpResult< T > lossn_erlangfp(const std::vector< T > &nu, const Matrix< T > &A, const std::vector< T > &C, const da::FpiOptions &options=lossn_erlangfp_options())
Erlang fixed-point (reduced-load) approximation for a loss network.
LossnMciResult< T > lossn_mci(const std::vector< T > &nu, const Matrix< T > &A, const std::vector< T > &C, const LossnMciOptions< T > &opt, Rng &gen)
Estimate the normalizing constant and the blocking probabilities.
LossnRecResult< T > lossn_rec(const std::vector< T > &nu, const Matrix< T > &A, const std::vector< T > &C)
Exact loss-network analysis by MDD-rec.
da::FpiOptions lossn_erlangfp_options()
The reference's fixed-point settings: iter_max 10000, iter_tol 1e-10, the max-norm of the increment (...
bool nc_is_lossn_model(const qn::NetworkStruct< T > &sn)
True when the model is a loss network: the shape above, with EVERY class dropped at the region.
bool nc_has_lossn_shape(const qn::NetworkStruct< T > &sn)
True when the model has the SHAPE of a loss network – open, one region, one member station,...
NcLossnSolution< T > solver_nc_lossn_analyzer(const qn::NetworkStruct< T > &sn, const NcSolverOptions &opt)
Port of solver_nc_lossn_analyzer.m.
Conservation laws of a layered queueing network, enumerated from its structure.
Controls and result shape shared by the normalizing-constant analyzers.
A queueing network and its refreshed NetworkStruct.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
std::vector< T > QLen
mean number of route-r calls in progress, nu_r (1 - Loss_r) (R)
std::vector< T > Loss
limiting loss probability of route r (R)
int niter
number of coordinate-descent sweeps
Controls of lossn_manjunath.
Result of lossn_manjunath.
double lG
log of the EXACT normalizing constant g(C)
std::vector< T > QLen
mean carried load E[n_r] per route
std::vector< T > Loss
blocking probability per route
std::size_t iterations
Always 1: the transform is direct, and the field exists for the shared analyzer contract that the ite...
Options of lossn_mci, MATLAB's options struct.
std::vector< T > QLen
mean carried load per route
std::vector< T > Loss
blocking probability per route
double lG
log of the estimated normalizing constant
Carried load, blocking, log normalising constant and walk count.
double lG
log G(C), a double diagnostic.
std::vector< T > Loss
Blocking probability per class.
std::vector< T > QLen
Mean number of class-r calls in progress, the carried load.
int iterations
Number of diagram walks performed, K + 1.
What the loss-network analyzer returns beyond the usual metric table.
std::vector< T > Loss
(K) blocking probability per class
Matrix< T > E
(J x K) blocking of class k on row j, empty outside the fixed point
Matrix< T > A
(J x K) rows of the admission rule A n <= C
std::vector< T > nu
(K) offered load per class
std::vector< T > Cvec
(J) the right-hand side
The [Q,U,R,T,C,X,lG] of the reference, plus the algorithm that ran.
Controls, defaulting to SolverOptions('NC') in the reference.
One job class of the network.
double population
infinite for an open class
FINITE CAPACITY REGIONS, MATLAB's refreshRegions output.
std::vector< T > lincon_b
Matrix< T > lincon_A
optional linear constraint A n <= b
std::vector< std::vector< double > > cap
(nstations x nclasses+1), -1 = unbounded
std::vector< double > maxmem
per member station, -1 = unbounded
std::vector< T > size
per class; size is the memory footprint
std::vector< bool > members
membership, independent of the caps
bool valid
false outside a loss network
std::string method
chosen method: exact, rec, erlangfp, asy or mci
std::uint64_t seed
mci seed of the run
std::vector< std::string > links
(J) link names, in row order
Matrix< double > A
(J x K) rows of the admission rule
std::vector< double > C
(J) capacities
std::size_t samples
mci sample count of the run (0 = not mci)
std::vector< double > nu
(K) offered load per class