5#ifndef LINE_SOLVERS_MAM_SOLVER_MAM_BASIC_H
6#define LINE_SOLVERS_MAM_SOLVER_MAM_BASIC_H
109using lang::GlobalConstants;
113namespace basic_detail {
129 case ProcessType::EXP:
130 case ProcessType::ERLANG:
131 case ProcessType::HYPEREXP:
132 case ProcessType::PH:
133 case ProcessType::APH:
134 case ProcessType::ME:
135 case ProcessType::RAP:
136 case ProcessType::MAP:
137 case ProcessType::MMAP:
138 case ProcessType::MPH:
139 case ProcessType::COXIAN:
140 case ProcessType::COX2:
141 case ProcessType::MMPP2:
142 case ProcessType::IMMEDIATE:
143 case ProcessType::DISABLED:
165const lang::Distrib<T>& mam_declared_law(
const lang::Distrib<T>& d) {
169 return d.declared ? *d.declared : d;
173bool mam_gk1_applicable(
const qn::NetworkStruct<T>& L, std::size_t i0, std::size_t K) {
174 for (std::size_t r = 0; r < K; ++r) {
192bool is_renewal_map(
const Map<T>& m) {
193 const std::size_t n = m.D0.rows();
194 if (n == 1)
return true;
196 for (std::size_t i = 0; i < n; ++i)
197 for (std::size_t j = 0; j < n; ++j)
198 scale = std::max(scale, std::fabs(num_traits<T>::to_double(m.D1(i, j))));
199 const double tol = 1e-9 * scale;
200 std::vector<double> exitrate(n, 0.0), sigma(n, 0.0);
202 for (std::size_t i = 0; i < n; ++i) {
203 for (std::size_t j = 0; j < n; ++j) exitrate[i] += num_traits<T>::to_double(m.D1(i, j));
206 if (tot <= 0.0)
return true;
209 for (std::size_t i = 0; i < n; ++i)
210 if (exitrate[i] > tol) {
214 if (ref == n)
return true;
215 for (std::size_t j = 0; j < n; ++j)
216 sigma[j] = num_traits<T>::to_double(m.D1(ref, j)) / exitrate[ref];
217 for (std::size_t i = 0; i < n; ++i)
218 for (std::size_t j = 0; j < n; ++j)
219 if (std::fabs(num_traits<T>::to_double(m.D1(i, j)) - exitrate[i] * sigma[j]) > tol)
232std::vector<bool> corr_service_classes(
const qn::NetworkStruct<T>& L, std::size_t i0,
233 const std::vector<Map<T>>& PHi,
234 const std::vector<bool>& known) {
235 const std::size_t K = L.nclasses;
236 std::vector<bool> corr(K,
false);
237 for (std::size_t r = 0; r < K; ++r) {
239 if (pt != ProcessType::MAP && pt != ProcessType::MMPP2)
continue;
240 if (r >= PHi.size() || !known[r] || PHi[r].D0.rows() == 0)
continue;
241 corr[r] = !is_renewal_map(PHi[r]);
248std::string class_names(
const qn::NetworkStruct<T>& L,
const std::vector<bool>& mask) {
250 for (std::size_t r = 0; r < mask.size(); ++r)
251 if (mask[r]) s += (s.empty() ?
"" :
", ") + L.classes[r].name;
262void warn_carried(
const qn::NetworkStruct<T>& L, std::size_t i0,
const std::vector<bool>& corr) {
263 const std::size_t K = L.nclasses;
264 const double ns = L.stations[i0].nservers;
265 const std::string
name = L.stations[i0].name, corrNames = class_names(L, corr);
266 if (ns > 1.0 && K == 1)
267 std::cerr <<
"[LINE] Warning: Station " <<
name <<
" has a correlated MAP service (class "
268 << corrNames <<
") at " << ns <<
" servers, and its exact MAP/MAP/c chain is "
269 <<
"too large. MAM carries the service phase on the single-fast-server "
270 <<
"surrogate, which is an approximation.\n";
272 std::cerr <<
"[LINE] Warning: Station " <<
name <<
" has a correlated MAP service (class "
273 << corrNames <<
") at " << ns <<
" servers with " << K <<
" classes. No exact "
274 <<
"multiclass multiserver analysis exists, so MAM carries each class's service "
275 <<
"phase across its services on the single-fast-server surrogate, which is an "
276 <<
"approximation.\n";
277 std::vector<bool> fitted(K,
false);
278 bool anyFitted =
false;
279 for (std::size_t r = 0; r < K; ++r) {
281 fitted[r] = !corr[r] &&
282 (pt == ProcessType::DET || pt == ProcessType::UNIFORM ||
283 pt == ProcessType::GAMMA || pt == ProcessType::PARETO ||
284 pt == ProcessType::WEIBULL || pt == ProcessType::LOGNORMAL ||
285 pt == ProcessType::LOGLOGISTIC || pt == ProcessType::MASSATORIGIN ||
286 pt == ProcessType::ME);
287 anyFitted = anyFitted || fitted[r];
290 std::cerr <<
"[LINE] Warning: Station " <<
name <<
" mixes a correlated MAP service (class "
291 << corrNames <<
") with the non-phase-type service of class "
292 << class_names(L, fitted) <<
", which is read through its phase-type fit: "
293 <<
"MMAP[K]/G[K]/1 admits only independent service times.\n";
315bool mam_source_feeds_station(
const qn::NetworkStruct<T>& L, std::size_t jst, std::size_t ist) {
316 const std::size_t M = L.nstations, K = L.nclasses;
317 if (jst < 1 || jst > M || ist < 1 || ist > M || jst == ist)
return false;
318 if (L.nodes[L.node_of_station(jst) - 1].nodetype != qn::NodeType::Source)
return false;
321 if (rtst.rows() < M * K)
return false;
325 double emitted = 0.0;
326 for (std::size_t r = 0; r < K; ++r) {
327 const std::size_t row = (jst - 1) * K + r;
328 double into = 0.0, total = 0.0;
329 for (std::size_t c = 0; c < K; ++c)
330 into += num_traits<T>::to_double(rtst(row, (ist - 1) * K + c));
331 for (std::size_t c = 0; c < rtst.cols(); ++c)
332 total += num_traits<T>::to_double(rtst(row, c));
333 if (std::fabs(into - total) > tol)
return false;
336 if (emitted <= tol)
return false;
339 for (std::size_t kst = 1; kst <= M; ++kst) {
340 if (kst == jst)
continue;
342 for (std::size_t r = 0; r < K; ++r)
343 for (std::size_t c = 0; c < K; ++c)
344 into += num_traits<T>::to_double(rtst((kst - 1) * K + r, (ist - 1) * K + c));
345 if (into > tol)
return false;
352bool is_markovian_map(
const Map<T>& m) {
353 const std::size_t n = m.D0.rows();
355 for (std::size_t i = 0; i < n; ++i)
356 for (std::size_t j = 0; j < n; ++j) {
357 scale = std::max(scale, std::fabs(num_traits<T>::to_double(m.D0(i, j))));
358 scale = std::max(scale, std::fabs(num_traits<T>::to_double(m.D1(i, j))));
360 const double tol = 1e-9 * scale;
361 for (std::size_t i = 0; i < n; ++i) {
363 for (std::size_t j = 0; j < n; ++j) {
364 const double a = num_traits<T>::to_double(m.D0(i, j));
365 const double b = num_traits<T>::to_double(m.D1(i, j));
366 if (i != j && a < -tol)
return false;
367 if (b < -tol)
return false;
370 if (std::fabs(rowsum) > tol)
return false;
377qsys::ServiceLaw<T> svc_mixture(
const Mmap<T>& arv,
const std::vector<PhService<T>>& svc) {
378 const T
zero = num_traits<T>::from_int(0);
379 const std::size_t K = svc.size();
380 Matrix<T> Q = arv.D0;
381 for (std::size_t k = 0; k < arv.classes(); ++k)
382 for (std::size_t i = 0; i < Q.rows(); ++i)
383 for (std::size_t j = 0; j < Q.cols(); ++j) Q(i, j) += arv.Dc[k](i, j);
385 std::vector<T> lam(K, zero);
387 for (std::size_t k = 0; k < K && k < arv.classes(); ++k) {
388 const std::vector<T> t =
vecmul(theta, arv.Dc[k]);
389 for (
const T& v : t) lam[k] += v;
392 std::vector<T> w(K, T(num_traits<T>::from_int(1) / num_traits<T>::from_int((
int)K)));
394 for (std::size_t k = 0; k < K; ++k) w[k] = T(lam[k] / sumL);
396 std::size_t ntot = 0;
397 for (
const PhService<T>& s : svc) ntot += s.S.rows();
398 std::vector<T> alpha(ntot, zero);
399 Matrix<T> Tm(ntot, ntot, zero);
401 for (std::size_t k = 0; k < K; ++k) {
402 for (std::size_t i = 0; i < svc[k].S.rows(); ++i) {
403 alpha[off + i] = T(w[k] * svc[k].sigma[i]);
404 for (std::size_t j = 0; j < svc[k].S.cols(); ++j) Tm(off + i, off + j) = svc[k].S(i, j);
406 off += svc[k].S.rows();
422Matrix<T> station_visits(
const qn::NetworkStruct<T>& L) {
423 Matrix<T> V(L.nstations, L.nclasses, num_traits<T>::from_int(0));
424 for (std::size_t c = 0; c < L.nchains; ++c)
425 for (std::size_t i = 0; i < L.nstations; ++i) {
426 const std::size_t sf = L.stateful_of_station(i + 1) - 1;
427 for (std::size_t k = 0; k < L.nclasses; ++k) V(i, k) += L.visits[c](sf, k);
434void zero_nans(Matrix<T>& A) {
435 for (std::size_t i = 0; i < A.rows(); ++i)
436 for (std::size_t j = 0; j < A.cols(); ++j)
437 if (std::isnan(num_traits<T>::to_double(A(i, j)))) A(i, j) = num_traits<T>::from_int(0);
458TruncRenorm<T> truncate_renorm(
const Mmap<T>& arv,
const std::vector<PhService<T>>& svc,
460 const T
zero = num_traits<T>::from_int(0);
462 std::vector<PhService<T>> scall = svc;
463 if (svc.size() > 1) {
464 const qsys::ServiceLaw<T> mix = svc_mixture(arv, svc);
465 Matrix<T> Dsum(arv.order(), arv.order(), zero);
466 for (std::size_t k = 0; k < arv.classes(); ++k)
467 for (std::size_t i = 0; i < Dsum.rows(); ++i)
468 for (std::size_t j = 0; j < Dsum.cols(); ++j) Dsum(i, j) += arv.Dc[k](i, j);
471 call.Dc.assign(1, Dsum);
472 scall.assign(1, PhService<T>{mix.ph_alpha, mix.ph_T});
476 out.p.assign(capK + 1, zero);
478 for (std::size_t n = 0; n <= capK; ++n) {
482 if (!(mass > zero)) {
483 out.p.assign(capK + 1, zero);
484 out.p[0] = num_traits<T>::from_int(1);
486 for (std::size_t n = 0; n <= capK; ++n) out.p[n] /= mass;
489 for (std::size_t n = 0; n <= capK; ++n) m += num_traits<T>::from_int((int)n) * out.p[n];
490 if (m < zero) m = zero;
491 if (num_traits<T>::to_double(m) >
static_cast<double>(capK))
492 m = num_traits<T>::from_int((
int)capK);
494 out.lossProb = out.p[capK];
506bool station_setup_pair(
const qn::NetworkStruct<T>& L, std::size_t ist, lang::Distrib<T>& setup,
507 lang::Distrib<T>& delayoff) {
508 typename std::map<std::size_t, qn::SetupDelayOffParam<T>>::const_iterator it =
509 L.setupparam.find(ist);
510 if (it == L.setupparam.end())
return false;
511 return it->second.last(setup, delayoff);
524void mam_setup_qbd(
const qn::NetworkStruct<T>& L, std::size_t ist,
525 const std::vector<T>& aggrLambda,
526 const std::vector<std::vector<PhService<T>>>& svc,
const Matrix<T>& S,
527 const std::vector<std::vector<T>>& rates, std::size_t R,
double ns,
528 const lang::Distrib<T>& setup,
const lang::Distrib<T>& delayoff,
529 std::vector<T>& Qret) {
530 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
531 const std::size_t i0 = ist - 1, K = L.nclasses;
532 for (std::size_t r = 0; r < K; ++r) Qret[r] = zero;
533 if constexpr (!num_traits<T>::has_transcendental) {
535 "solver_mam_basic: a setup/delay-off station is solved by a QBD whose G matrix needs "
536 "a logarithm; rerun with --arith double or real");
542 std::vector<T> rho(K, zero);
544 for (std::size_t r = 0; r < K; ++r) {
545 if (L.disabled[i0][r])
continue;
547 const Matrix<T>& Sr = svc[i0][r].S;
548 if (Sr.rows() == 0)
continue;
550 for (std::size_t a = 0; a < negS.rows(); ++a)
551 for (std::size_t b = 0; b < negS.cols(); ++b) negS(a, b) = -negS(a, b);
552 const Matrix<T> negSinv =
inverse(negS);
554 for (std::size_t a = 0; a < negSinv.rows() && a < svc[i0][r].sigma.size(); ++a)
555 for (std::size_t b = 0; b < negSinv.cols(); ++b)
556 mean += svc[i0][r].sigma[a] * negSinv(a, b);
557 if (!(mean > zero))
continue;
558 std::size_t c = L.nchains;
559 for (std::size_t cc = 0; cc < L.nchains; ++cc)
560 if (L.chains[cc][r]) c = cc;
561 if (c == L.nchains)
continue;
563 rho[r] = T(rates[c][r] * mean);
566 if (!(rho_total > zero))
return;
569 for (std::size_t r = 0; r < K && r < aggrLambda.size(); ++r) lam_total += aggrLambda[r];
570 if (R == 1 && !aggrLambda.empty()) lam_total = aggrLambda[0];
571 if (!(lam_total > zero))
return;
572 const T aggrRate = T(lam_total / rho_total);
575 for (std::size_t r = 0; r < K; ++r)
576 if (rho[r] > zero) Qret[r] = T(qtot * rho[r] / rho_total);
597void mam_setup_closed(
const qn::NetworkStruct<T>& L, std::size_t ist,
598 const std::vector<std::vector<PhService<T>>>& svc,
const Matrix<T>& S,
599 const std::vector<std::vector<T>>& rates,
const std::vector<T>& ztchain,
600 const Matrix<T>& V,
double ns,
const lang::Distrib<T>& setup,
601 const lang::Distrib<T>& delayoff,
const Matrix<T>& QNprev,
602 std::vector<T>& Qret) {
603 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
604 const std::size_t i0 = ist - 1, K = L.nclasses;
605 for (std::size_t r = 0; r < K; ++r) Qret[r] = zero;
606 if constexpr (!num_traits<T>::has_transcendental) {
608 "solver_mam_basic: the closed setup/delay-off chain fits a phase-type setup and "
609 "delay-off, which needs a square root; rerun with --arith double or real");
616 for (std::size_t c = 0; c < L.nchains; ++c) {
618 bool finiteNc =
true, anyActive =
false;
619 for (std::size_t r = 0; r < K; ++r) {
620 if (!L.chains[c][r])
continue;
621 if (!std::isfinite(L.classes[r].population)) { finiteNc =
false;
break; }
622 Nc += num_traits<T>::from_double(L.classes[r].population);
623 if (rates[c][r] > zero && std::isfinite(num_traits<T>::to_double(S(i0, r))))
626 if (!finiteNc || !anyActive || !(Nc > zero))
continue;
629 for (std::size_t j = 0; j < K; ++j)
630 if (L.chains[c][j]) vtot += V(i0, j);
631 const T ZT = T(ztchain[c] / (vtot > ft ? vtot : ft));
642 std::size_t svcAnyCount = 0;
643 for (std::size_t r = 0; r < K; ++r) {
644 if (!L.chains[c][r])
continue;
645 T lamK = rates[c][r];
646 if (!std::isfinite(num_traits<T>::to_double(lamK))) lamK =
zero;
648 if (!std::isfinite(num_traits<T>::to_double(sk))) sk =
zero;
650 if (i0 < QNprev.rows() && r < QNprev.cols() &&
651 std::isfinite(num_traits<T>::to_double(QNprev(i0, r))))
652 qnHere += QNprev(i0, r);
655 if (sk > zero) { svcAny += sk; ++svcAnyCount; }
658 if (lamHere > ft && T(Nc - qnHere) > zero) {
659 const T zalt = T(T(Nc - qnHere) / lamHere);
660 Zc = zalt > ZT ? zalt : ZT;
664 T Sbar = tnTot > ft ? T(tnS / tnTot)
666 ? T(svcAny / num_traits<T>::from_int(
667 static_cast<long>(svcAnyCount)))
669 if (!(Sbar > ft))
continue;
671 Nc, Zc, T(one / Sbar), alpharate, alphascv, betarate, betascv);
672 if (!std::isfinite(num_traits<T>::to_double(cr.QN)) || !(cr.XN > zero))
continue;
673 T Wq = T(T(cr.QN / cr.XN) - Sbar);
674 if (!(Wq > zero)) Wq =
zero;
675 for (std::size_t r = 0; r < K; ++r) {
676 if (!L.chains[c][r])
continue;
677 if (L.disabled[i0][r])
continue;
683 const T cserv = (std::isfinite(ns) && ns >= 1.0)
684 ? num_traits<T>::from_double(ns)
686 if (rates[c][r] > zero && std::isfinite(num_traits<T>::to_double(S(i0, r))))
687 Qret[r] = T(rates[c][r] * T(Wq + T(S(i0, r) / cserv)));
702Mmap<T> station_arrival(
const qn::NetworkStruct<T>& L,
const MamOptions& opt, std::size_t i0,
703 const std::vector<T>& lambda,
const Matrix<T>& V,
704 const std::vector<Mmap<T>>& chainSysArrivals,
705 std::vector<std::vector<T>>& rates) {
706 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
707 const std::size_t K = L.nclasses, C = L.nchains;
709 rates.assign(C, std::vector<T>(K, zero));
710 for (std::size_t c = 0; c < C; ++c)
711 for (std::size_t r = 0; r < K; ++r) rates[c][r] = T(V(i0, r) * lambda[c]);
714 for (std::size_t c = 0; c < C; ++c) {
715 const std::vector<std::size_t>& ic = L.inchain[c];
717 for (std::size_t r : ic)
tot += rates[c][r - 1];
718 Matrix<T> markProb(1, ic.size(), zero);
719 for (std::size_t j = 0; j < ic.size(); ++j)
720 markProb(0, j) = (
tot >
zero) ? T(rates[c][ic[j] - 1] / tot) :
zero;
727 for (std::size_t j = 0; j < ic.size(); ++j)
728 if (rates[c][ic[j] - 1] > zero) anypos =
true;
730 std::vector<T> tgt(ic.size(), zero);
731 for (std::size_t j = 0; j < ic.size(); ++j)
732 tgt[j] = (rates[c][ic[j] - 1] > zero)
733 ? T(one / rates[c][ic[j] - 1])
734 : num_traits<T>::from_double(1.0 / GlobalConstants::Zero);
742 aggr =
mmap_super_safe(std::vector<Mmap<T>>{aggr, cur}, opt.space_max);
749 std::vector<std::size_t> markorder;
750 for (std::size_t c = 0; c < C; ++c)
751 for (std::size_t r : L.inchain[c]) markorder.push_back(r);
752 if (markorder.size() == aggr.Dc.size() &&
753 !std::is_sorted(markorder.begin(), markorder.end())) {
754 std::vector<std::size_t> perm(markorder.size());
755 for (std::size_t j = 0; j < perm.size(); ++j) perm[j] = j;
756 std::stable_sort(perm.begin(), perm.end(),
757 [&markorder](std::size_t a, std::size_t b) {
758 return markorder[a] < markorder[b];
760 std::vector<Matrix<T>> reordered(perm.size());
761 for (std::size_t j = 0; j < perm.size(); ++j) reordered[j] = aggr.Dc[perm[j]];
762 aggr.Dc.swap(reordered);
780void solve_lcfspr_station(
const qn::NetworkStruct<T>& L,
const MamOptions& opt, std::size_t ist,
781 const std::vector<T>& lambda,
const Matrix<T>& V,
const Matrix<T>& S,
782 const std::vector<std::vector<bool>>& Sknown,
783 const std::vector<std::vector<Map<T>>>& PH,
784 const std::vector<std::vector<bool>>& PHset,
785 const std::vector<Mmap<T>>& chainSysArrivals, Matrix<T>& QN,
786 Matrix<T>& UN, Matrix<T>& RN, Matrix<T>& TN,
787 std::vector<bool>& exact_station, std::vector<bool>& car_warned) {
788 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
789 const std::size_t K = L.nclasses, C = L.nchains, i0 = ist - 1;
790 const std::string stname = L.stations[i0].name;
791 if (L.stations[i0].nservers != 1.0)
793 "solver_mam_basic: Station " + stname +
" is LCFS-PR with " +
794 std::to_string(L.stations[i0].nservers) +
" servers. The MAM analysis of LCFS-PR "
795 "(Takine 2002) is a single-server result; use SolverCTMC or SolverJMT.");
798 "solver_mam_basic: Station " + stname +
" is LCFS-PR with a finite capacity, which "
799 "the MAM analysis of LCFS-PR does not cover; use SolverCTMC or SolverJMT.");
800 lang::Distrib<T> setup_dist, delayoff_dist;
801 if (station_setup_pair(L, ist, setup_dist, delayoff_dist))
803 "solver_mam_basic: Station " + stname +
" is LCFS-PR with setup times, which the "
804 "MAM analysis of LCFS-PR does not cover; use SolverCTMC or SolverJMT.");
806 std::vector<std::vector<T>> rates;
807 const Mmap<T> aggr = station_arrival(L, opt, i0, lambda, V, chainSysArrivals, rates);
808 if (aggr.classes() != K)
810 "solver_mam_basic: The arrival process at station " + stname +
" carries " +
811 std::to_string(aggr.classes()) +
" marks for " + std::to_string(K) +
" classes.");
813 std::vector<T> lamK(K, zero);
814 for (std::size_t r = 0; r < K; ++r) {
815 for (std::size_t c = 0; c < C; ++c)
816 if (L.chains[c][r]) {
817 lamK[r] = rates[c][r];
820 if (!std::isfinite(num_traits<T>::to_double(lamK[r]))) lamK[r] =
zero;
823 const std::vector<bool> corrSvc = corr_service_classes(L, i0, PH[i0], PHset[i0]);
824 if (std::find(corrSvc.begin(), corrSvc.end(),
true) != corrSvc.end() && !car_warned[i0]) {
825 car_warned[i0] =
true;
826 std::cerr <<
"[LINE] Warning: Station " << stname <<
" has a correlated MAP service (class "
827 << class_names(L, corrSvc) <<
") under LCFS-PR. The matrix product form reads "
828 <<
"it through its renewal marginal, which discards the autocorrelation; "
829 <<
"SolverCTMC carries it exactly.\n";
838 typedef qsys::MmapGk1Service<T> Svc;
839 qsys::MmapGk1Transform<T> noLoadT;
840 noLoadT.lst = [](std::complex<double>) {
return std::complex<double>(1.0, 0.0); };
842 const Svc noLoad(noLoadT);
843 std::vector<Svc> laws;
844 for (std::size_t r = 0; r < K; ++r) {
845 const double Sr = num_traits<T>::to_double(S(i0, r));
846 if (!(lamK[r] > zero) || !Sknown[i0][r] || !std::isfinite(Sr) || Sr <= 0.0) {
847 laws.push_back(noLoad);
848 if (lamK[r] < zero) lamK[r] =
zero;
851 const lang::Distrib<T>& decl = mam_declared_law(L.service[i0][r]);
860 double dm = std::numeric_limits<double>::quiet_NaN();
863 }
catch (
const std::exception&) {
865 if (std::isfinite(dm) && std::fabs(dm / Sr - 1.0) < 1e-9) {
866 laws.push_back(Svc(decl));
869 if (std::isfinite(dm) && dm > 0.0) {
870 const double a = Sr / dm;
871 qsys::MmapGk1Transform<T> scaled;
872 scaled.lst = [decl, a](std::complex<double> s) {
return lang::dist_lst(decl, a * s); };
873 scaled.moments = {S(i0, r)};
874 laws.push_back(Svc(scaled));
878 if (!PHset[i0][r] || PH[i0][r].D0.rows() == 0)
881 laws.push_back(Svc(PH[i0][r]));
885 for (std::size_t r = 0; r < K; ++r)
886 if (Sknown[i0][r]) rho += lamK[r] * S(i0, r);
887 std::vector<T> Qk(K, zero), Rk(K, zero);
888 bool anyOpen =
false;
890 for (std::size_t r = 0; r < K; ++r) {
891 const double n = L.classes[r].population;
892 if (std::isinf(n)) anyOpen =
true;
893 else nMax = std::max(nMax,
static_cast<long>(std::llround(n)));
897 for (std::size_t r = 0; r < K; ++r) {
898 Qk[r] = num_traits<T>::from_double(L.classes[r].population);
899 if (lamK[r] > zero) Rk[r] = T(Qk[r] / lamK[r]);
902 std::vector<Matrix<T>> MM;
903 MM.push_back(aggr.D0);
904 Matrix<T> D1sum(aggr.D0.rows(), aggr.D0.cols(), zero);
905 for (std::size_t r = 0; r < K; ++r)
906 for (std::size_t a = 0; a < D1sum.rows(); ++a)
907 for (std::size_t b = 0; b < D1sum.cols(); ++b) D1sum(a, b) += aggr.Dc[r](a, b);
909 for (std::size_t r = 0; r < K; ++r) MM.push_back(aggr.Dc[r]);
910 qsys::MmapGk1LcfsprOptions<T> lopt;
911 if (!anyOpen) lopt.nMax = nMax;
913 for (std::size_t r = 0; r < K; ++r) {
915 Qk[r] = res.classMeanQueueLength[r];
916 Rk[r] = res.meanSojournTime[r];
919 if (!(lamK[r] > zero))
continue;
920 const std::size_t Nk =
static_cast<std::size_t
>(std::llround(L.classes[r].population));
921 std::vector<T> p(Nk + 1, zero);
923 for (std::size_t n = 0; n < Nk; ++n) {
924 p[n] = res.classQueueLengthDistr[r][n];
929 for (std::size_t n = 0; n <= Nk; ++n) mass += p[n];
932 for (std::size_t n = 0; n <= Nk; ++n)
933 m += num_traits<T>::from_int((
int)n) * T(p[n] / mass);
934 if (m < zero) m =
zero;
935 if (num_traits<T>::to_double(m) >
static_cast<double>(Nk))
936 m = num_traits<T>::from_int((
int)Nk);
938 Rk[r] = T(m / lamK[r]);
941 for (std::size_t r = 0; r < K; ++r) {
943 UN(i0, r) = Sknown[i0][r] ? T(TN(i0, r) * S(i0, r)) :
zero;
947 exact_station[i0] =
true;
967void solve_fcfs_station(
const qn::NetworkStruct<T>& L,
const MamOptions& opt, std::size_t ist,
968 const std::vector<T>& lambda,
const Matrix<T>& V,
const Matrix<T>& S,
969 const std::vector<std::vector<bool>>& Sknown,
970 const std::vector<std::vector<Map<T>>>& PH,
971 const std::vector<std::vector<bool>>& PHset,
972 const std::vector<std::vector<PhService<T>>>& svc,
973 const std::vector<Mmap<T>>& chainSysArrivals,
974 const std::vector<T>& ztchain, Matrix<T>& QN, Matrix<T>& UN,
975 Matrix<T>& RN, Matrix<T>& TN, std::vector<bool>& exact_station,
976 std::vector<bool>& car_warned) {
977 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
978 const std::size_t M = L.nstations, K = L.nclasses, C = L.nchains;
979 const double ns = L.stations[ist - 1].nservers;
980 const std::size_t i0 = ist - 1;
985 bool prio_distinct =
false;
986 if (sched_i == SchedStrategy::HOL || sched_i == SchedStrategy::FCFSPRPRIO)
987 for (std::size_t r = 1; r < K; ++r)
988 if (L.classes[r].prio != L.classes[0].prio) prio_distinct =
true;
990 std::vector<std::vector<T>> rates;
991 Mmap<T> aggr = station_arrival(L, opt, i0, lambda, V, chainSysArrivals, rates);
993 const std::size_t R = aggr.classes();
994 lang::Distrib<T> setup_dist, delayoff_dist;
995 const bool has_setup = station_setup_pair(L, ist, setup_dist, delayoff_dist);
1019 const std::string mark_refusal =
1020 std::string(
"solver_mam_basic: the assembled arrival stream at station '") +
1021 L.stations[i0].name +
"' carries " + std::to_string(R) +
1022 " marked classes against the model's " + std::to_string(K) +
1023 "; the reference collapses chain 1 to a single mark, so this analyzer has no arrival "
1024 "stream to pair with each service law. A class-switching chain at an FCFS station is "
1025 "refused rather than solved with a mismatched marking";
1027 const std::vector<T> aggrLambda =
mmap_lambda(aggr);
1028 double aggrUtil = 0.0;
1029 for (std::size_t r = 0; r < K; ++r) {
1032 if (L.disabled[i0][r])
continue;
1033 const double lam = num_traits<T>::to_double(aggrLambda[R == 1 ? 0 : r]);
1034 const double mu = num_traits<T>::to_double(L.rates(i0, r));
1036 if (den > 0.0 && std::isfinite(den)) aggrUtil += lam / den;
1039 std::vector<T> Qret(K, zero);
1040 bool exact_here =
false;
1041 T exactRespT =
zero;
1042 bool finiteCapUsed =
false;
1043 T finiteCapMeanQ =
zero, finiteCapLossProb =
zero;
1044 std::vector<T> finiteCapLossPerClass;
1046 bool anyopen =
false;
1047 for (std::size_t r = 0; r < K; ++r)
1048 if (std::isinf(L.classes[r].population)) anyopen =
true;
1050 if (prio_distinct && !car_warned[i0]) {
1051 const std::vector<bool> corrP = corr_service_classes(L, i0, PH[i0], PHset[i0]);
1052 if (std::find(corrP.begin(), corrP.end(),
true) != corrP.end()) {
1053 car_warned[i0] =
true;
1054 std::cerr <<
"[LINE] Warning: Station " << L.stations[i0].name
1055 <<
" has a correlated MAP service (class " << class_names(L, corrP)
1056 <<
") under class priorities. The priority analysis reads it through its "
1057 <<
"renewal marginal, which discards the autocorrelation; SolverCTMC "
1058 <<
"carries it exactly.\n";
1062 if (prio_distinct) {
1067 if (R != K)
throw UnsupportedError(ErrorKind{
"ClassSwitch"}, mark_refusal);
1068 std::vector<std::size_t> iK(K);
1069 for (std::size_t r = 0; r < K; ++r) iK[r] = r;
1070 std::stable_sort(iK.begin(), iK.end(), [&L](std::size_t a, std::size_t b) {
1071 return L.classes[a].prio > L.classes[b].prio;
1073 for (std::size_t j = 1; j < K; ++j)
1074 if (L.classes[iK[j]].prio == L.classes[iK[j - 1]].prio)
1076 "solver_mam_basic: Solver MAM requires either identical priorities or all "
1077 "distinct priorities");
1081 std::vector<PhService<T>> sl;
1082 for (std::size_t j = 0; j < K; ++j) {
1083 pa.Dc.push_back(aggr.Dc[iK[j]]);
1084 sl.push_back(svc[i0][iK[j]]);
1086 const std::vector<std::vector<T>> m = (sched_i == SchedStrategy::FCFSPRPRIO)
1089 for (std::size_t j = 0; j < K; ++j) Qret[iK[j]] = m[j][0];
1091 for (std::size_t r = 0; r < K; ++r)
1092 Qret[r] = num_traits<T>::from_double(L.classes[r].population);
1093 }
else if (anyopen) {
1094 bool isMapDc = (K == 1) && (L.service[i0][0].type == ProcessType::DET);
1099 std::size_t dmcSource = 0;
1100 if (K == 1 && !isMapDc && L.service[i0][0].type == ProcessType::EXP) {
1101 for (std::size_t j = 1; j <= M; ++j)
1102 if (j != ist && L.service[j - 1][0].type == ProcessType::DET &&
1103 mam_source_feeds_station(L, j, ist)) {
1109 bool isPhM1 =
false;
1110 std::size_t phSource = 0;
1111 if (K == 1 && !isMapDc && !isDMc && L.service[i0][0].type == ProcessType::EXP &&
1112 std::isfinite(ns) && ns >= 1.0) {
1113 for (std::size_t j = 1; j <= M; ++j) {
1114 if (j == ist)
continue;
1116 if (sp != ProcessType::EXP && sp != ProcessType::DET &&
1117 sp != ProcessType::IMMEDIATE && sp != ProcessType::DISABLED) {
1125 if (!mam_source_feeds_station(L, j, ist))
continue;
1129 }
else if (sp == ProcessType::EXP && ns > 1.0 && M == 2 &&
1130 L.nodes[L.node_of_station(j) - 1].nodetype == qn::NodeType::Source) {
1163 const T muQ = T(one / S(i0, 0));
1166 map_pie(src), src.D0, muQ,
static_cast<unsigned>(std::llround(ns)));
1167 Qret[0] = r.meanQueueLength;
1168 exactRespT = r.meanSojournTime;
1170 }
catch (
const std::exception&) {
1174 if (!exact_here && isDMc) {
1176 const T muQ = T(one / S(i0, 0));
1178 L.rates(dmcSource - 1, 0), muQ,
static_cast<unsigned>(std::llround(ns)));
1179 Qret[0] = r.meanQueueLength;
1180 exactRespT = r.meanSojournTime;
1182 }
catch (
const std::exception&) {
1186 if (!exact_here && !isFiniteCap && isMapDc) {
1191 const qsys::MapDcResult<T> r =
1193 Qret[0] = r.meanQueueLength;
1194 exactRespT = r.meanSojournTime;
1196 }
catch (
const std::exception&) {
1205 bool isMapMc = !exact_here && !isFiniteCap && (K == 1) && !isMapDc && !isDMc &&
1206 !isPhM1 && L.service[i0][0].type == ProcessType::EXP &&
1207 std::isfinite(ns) && ns > 1.0;
1213 const T muQ = T(one / S(i0, 0));
1214 const qsys::MapMcResult<T> r =
1216 Qret[0] = r.meanQueueLength;
1217 exactRespT = r.meanSojournTime;
1219 }
catch (
const std::exception&) {
1229 bool isMapPhc =
false, isMapMapc =
false;
1230 if (!exact_here && !isFiniteCap && K == 1 && std::isfinite(ns) && ns > 1.0) {
1232 if (st != ProcessType::EXP && st != ProcessType::DET && st != ProcessType::ME &&
1233 st != ProcessType::RAP && st != ProcessType::IMMEDIATE &&
1234 st != ProcessType::DISABLED) {
1239 isMapMapc = !isMapPhc && Sknown[i0][0];
1248 const qsys::MapPhcResult<T> r =
1250 static_cast<std::size_t
>(500),
static_cast<std::size_t
>(1));
1251 Qret[0] = r.meanQueueLength;
1252 exactRespT = r.meanSojournTime;
1254 }
catch (
const std::exception&) {
1267 arv,
map_pie(svc0), svc0.D0,
static_cast<unsigned>(std::llround(ns)),
1268 static_cast<std::size_t
>(500),
static_cast<std::size_t
>(1), std::vector<T>());
1269 Qret[0] = r.meanQueueLength;
1270 exactRespT = r.meanSojournTime;
1272 }
catch (
const std::exception&) {
1278 }
else if (isFiniteCap) {
1281 if (R != K && !(aggr.order() == 1))
throw UnsupportedError(ErrorKind{
"ClassSwitch"}, mark_refusal);
1282 const std::size_t capK =
static_cast<std::size_t
>(std::llround(L.cap[i0]));
1285 bool isMmck = (aggr.order() == 1);
1289 double lo = 0.0, hi = 0.0;
1290 for (std::size_t r = 0; r < K; ++r) {
1291 if (L.disabled[i0][r])
continue;
1292 if (L.service[i0][r].type != ProcessType::EXP) {
1296 const double v = num_traits<T>::to_double(L.rates(i0, r));
1297 if (!(v > 0.0))
continue;
1301 muMmck = L.rates(i0, r);
1303 lo = std::min(lo, v);
1304 hi = std::max(hi, v);
1307 if (!any) isMmck =
false;
1308 if (isMmck && hi - lo > 1e-9 * std::max(1.0, hi)) isMmck =
false;
1312 for (
const T& v : aggrLambda) lamTot += v;
1313 const qsys::MmckResult<T> r =
1314 qsys::qsys_mmck(lamTot, muMmck,
static_cast<unsigned>(std::llround(ns)),
1315 static_cast<unsigned>(capK));
1316 finiteCapMeanQ = r.meanQueueLength;
1317 finiteCapLossProb = r.lossProbability;
1318 }
else if (ns == 1.0) {
1322 std::vector<PhService<T>> sl;
1323 for (std::size_t r = 0; r < K; ++r) sl.push_back(svc[i0][r]);
1324 const qsys::ServiceLaw<T> mix = svc_mixture(aggr, sl);
1326 finiteCapMeanQ = r.meanQueueLength;
1327 finiteCapLossProb = r.lossAggregate;
1328 finiteCapLossPerClass = r.lossRatio;
1330 std::vector<PhService<T>> sl;
1331 for (std::size_t r = 0; r < K; ++r) sl.push_back(svc[i0][r]);
1332 const TruncRenorm<T> r = truncate_renorm(aggr, sl, capK);
1333 finiteCapMeanQ = r.meanQ;
1334 finiteCapLossProb = r.lossProb;
1336 finiteCapUsed =
true;
1337 exact_station[i0] =
true;
1338 }
else if (has_setup) {
1344 mam_setup_qbd(L, ist, aggrLambda, svc, S, rates, R, ns, setup_dist, delayoff_dist,
1352 (K == 1) && (ns == 1.0) && PHset[i0][0] &&
1353 std::fabs(num_traits<T>::to_double(
map_acf(PH[i0][0], std::vector<unsigned>{1})[0])) >
1357 std::vector<bool> corrSvc(K,
false);
1358 if (!corr) corrSvc = corr_service_classes(L, i0, PH[i0], PHset[i0]);
1359 const bool useCarried =
1360 std::find(corrSvc.begin(), corrSvc.end(),
true) != corrSvc.end();
1361 if (useCarried && !car_warned[i0]) {
1362 car_warned[i0] =
true;
1363 warn_carried(L, i0, corrSvc);
1368 arv.D1 = aggr.Dc[0];
1369 const QbdMapMap1Result<T> r =
qbd_mapmap1(arv, PH[i0][0]);
1371 }
else if (useCarried) {
1375 if (R != K)
throw UnsupportedError(ErrorKind{
"ClassSwitch"}, mark_refusal);
1376 std::vector<Map<T>> svcK(PH[i0].begin(), PH[i0].begin() + K);
1378 for (std::size_t r = 0; r < K; ++r) Qret[r] = cr.meanQueueLength[r];
1382 if (R != K)
throw UnsupportedError(ErrorKind{
"ClassSwitch"}, mark_refusal);
1388 bool gk_done =
false;
1389 if (ns == 1.0 && mam_gk1_applicable(L, i0, K)) {
1391 std::vector<Matrix<T>> MM;
1392 MM.push_back(aggr.D0);
1393 Matrix<T> D1sum(aggr.D0.rows(), aggr.D0.cols(),
1394 num_traits<T>::from_int(0));
1395 for (std::size_t r = 0; r < K; ++r)
1396 for (std::size_t a = 0; a < D1sum.rows(); ++a)
1397 for (std::size_t b = 0; b < D1sum.cols(); ++b)
1398 D1sum(a, b) += aggr.Dc[r](a, b);
1399 MM.push_back(D1sum);
1400 for (std::size_t r = 0; r < K; ++r) MM.push_back(aggr.Dc[r]);
1401 std::vector<lang::Distrib<T>> laws;
1402 for (std::size_t r = 0; r < K; ++r)
1403 laws.push_back(mam_declared_law(L.service[i0][r]));
1405 MM, laws, std::vector<T>(),
static_cast<std::size_t
>(1), 1e-12,
1406 static_cast<std::size_t
>(10000));
1407 for (std::size_t r = 0; r < K; ++r)
1408 Qret[r] = gk.lambdas[r] * gk.meanSojournTime[r];
1415 }
catch (
const std::exception&) {
1420 std::vector<PhService<T>> sl;
1421 for (std::size_t r = 0; r < R; ++r) sl.push_back(svc[i0][r]);
1423 for (std::size_t r = 0; r < K; ++r) Qret[r] = m[r];
1431 std::size_t maxLevel = 1;
1432 for (std::size_t r = 0; r < K; ++r)
1433 if (std::isfinite(L.classes[r].population))
1434 maxLevel +=
static_cast<std::size_t
>(std::llround(L.classes[r].population));
1439 const Map<T> probe = aggr.map();
1441 for (std::size_t r = 0; r < K; ++r)
1442 Qret[r] = (L.rates(i0, 0) > zero)
1446 }
else if (has_setup) {
1453 mam_setup_closed(L, ist, svc, S, rates, ztchain, V, ns, setup_dist, delayoff_dist,
1457 if (R != K)
throw UnsupportedError(ErrorKind{
"ClassSwitch"}, mark_refusal);
1461 const std::vector<bool> corrC = corr_service_classes(L, i0, PH[i0], PHset[i0]);
1462 if (std::find(corrC.begin(), corrC.end(),
true) != corrC.end() && !car_warned[i0]) {
1463 car_warned[i0] =
true;
1464 std::cerr <<
"[LINE] Warning: Station " << L.stations[i0].name
1465 <<
" has a correlated MAP service (class " << class_names(L, corrC)
1466 <<
") in a closed chain. The closed decomposition reads it through "
1467 <<
"its renewal marginal, which discards the autocorrelation; method "
1468 <<
"'ldqbd' carries it exactly on a single-class Delay+Queue model, "
1469 <<
"and SolverCTMC on any model.\n";
1471 std::vector<PhService<T>> sl;
1472 for (std::size_t r = 0; r < R; ++r) sl.push_back(svc[i0][r]);
1474 for (std::size_t r = 0; r < K; ++r) {
1475 const std::size_t Nk =
1476 static_cast<std::size_t
>(std::llround(L.classes[r].population));
1477 std::vector<T> p(Nk + 1, zero);
1478 const std::vector<T>& src = pd[r];
1480 for (std::size_t n = 0; n < Nk; ++n) {
1486 p[Nk] =
num_abs(T(one - acc));
1488 for (std::size_t n = 0; n <= Nk; ++n) mass += p[n];
1491 for (std::size_t n = 0; n <= Nk; ++n)
1492 m += num_traits<T>::from_int((
int)n) * T(p[n] / mass);
1493 if (m < zero) m =
zero;
1494 if (num_traits<T>::to_double(m) >
static_cast<double>(Nk))
1495 m = num_traits<T>::from_int((
int)Nk);
1502 if (finiteCapUsed) {
1505 std::vector<T> inflow(K, zero), eff(K, zero);
1506 for (std::size_t r = 0; r < K; ++r) {
1508 for (std::size_t cc = 0; cc < C; ++cc)
1509 if (L.chains[cc][r]) {
1513 inflow[r] = rates[c][r];
1514 const T loss = finiteCapLossPerClass.empty() ? finiteCapLossProb
1515 : finiteCapLossPerClass[r];
1516 eff[r] = T(inflow[r] * T(one - loss));
1519 for (std::size_t r = 0; r < K; ++r) sumTN += eff[r];
1523 for (std::size_t r = 0; r < K; ++r)
1524 if (Sknown[i0][r]) sw += eff[r] * S(i0, r);
1525 const T w = T(T(finiteCapMeanQ / sumTN) - T(sw / sumTN));
1528 for (std::size_t r = 0; r < K; ++r) {
1530 UN(i0, r) = T(TN(i0, r) * S(i0, r) / num_traits<T>::from_double(ns));
1531 if (TN(i0, r) > zero) {
1532 RN(i0, r) = T(Wq + S(i0, r));
1533 QN(i0, r) = T(TN(i0, r) * RN(i0, r));
1540 for (std::size_t r = 0; r < K; ++r) {
1542 for (std::size_t cc = 0; cc < C; ++cc)
1543 if (L.chains[cc][r]) {
1547 TN(i0, r) = rates[c][r];
1548 UN(i0, r) = T(TN(i0, r) * S(i0, r) / num_traits<T>::from_double(ns));
1549 QN(i0, r) = Qret[r];
1551 RN(i0, r) = exactRespT;
1552 exact_station[i0] =
true;
1556 if (Sknown[i0][r] && std::isfinite(ns))
1557 QN(i0, r) = T(
QN(i0, r) + TN(i0, r) * S(i0, r) *
1558 num_traits<T>::from_double((ns - 1.0) / ns));
1559 RN(i0, r) = (TN(i0, r) >
zero) ? T(
QN(i0, r) / TN(i0, r)) :
zero;
1578 "solver_mam_basic: the matrix-analytic station solves run tolerance-terminated "
1579 "iterations (the Riccati doubling behind MMAP[K]/PH[K]/1, the QBD cyclic reduction) "
1580 "and need transcendental arithmetic; rerun this model with --arith double or "
1583 using namespace basic_detail;
1586 const double tol =
opt.tol;
1590 std::vector<std::vector<bool>> Sknown(M, std::vector<bool>(K,
false));
1591 for (std::size_t i = 0; i < M; ++i)
1592 for (std::size_t r = 0; r < K; ++r)
1594 S(i, r) = T(one / L.
rates(i, r));
1595 Sknown[i][r] =
true;
1603 std::vector<T> ztchain(C, zero);
1604 for (std::size_t c = 0; c < C; ++c)
1605 for (std::size_t i = 0; i < M; ++i)
1606 if (std::isinf(L.
stations[i].nservers)) ztchain[c] += dem.
Lchain(i, c);
1608 Matrix<T> QN(M, K, zero), UN(M, K, zero), RN(M, K, zero), TN(M, K, zero);
1609 std::vector<T> CN(K, zero), XN(K, zero);
1610 std::vector<bool> exact_station(M,
false);
1611 std::vector<bool> car_warned(M,
false);
1615 std::vector<std::vector<Map<T>>> PH(M, std::vector<
Map<T>>(K));
1616 std::vector<std::vector<bool>> PHset(M, std::vector<bool>(K,
false));
1617 std::vector<std::vector<PhService<T>>> svc(M, std::vector<
PhService<T>>(K));
1618 for (std::size_t i = 0; i < M; ++i) {
1620 if (!(sc == SchedStrategy::FCFS || sc == SchedStrategy::HOL ||
1621 sc == SchedStrategy::FCFSPRPRIO || sc == SchedStrategy::PS ||
1622 sc == SchedStrategy::LCFSPR))
1624 for (std::size_t r = 0; r < K; ++r) {
1625 if (L.
service[i][r].type == ProcessType::DET &&
opt.preserve_det_resolved())
continue;
1626 const double ns = L.
stations[i].nservers;
1629 if (L.
disabled[i][r] || !Sknown[i][r] || !(target > zero)) {
1638 svc[i][r].sigma.assign(1, one);
1647 PH[i][r] = (pt == ProcessType::ME || pt == ProcessType::RAP)
1651 svc[i][r].sigma =
map_pie(PH[i][r]);
1652 svc[i][r].S = PH[i][r].D0;
1657 std::vector<bool> isopenchain(C,
false), isclosedchain(C,
false);
1658 std::vector<T> lambda(C, zero);
1659 std::vector<Mmap<T>> chainSysArrivals(C);
1660 for (std::size_t c = 0; c < C; ++c) {
1663 for (std::size_t r : L.
inchain[c]) {
1664 const double n = L.
classes[r - 1].population;
1665 if (std::isinf(n)) inf =
true;
1669 isopenchain[c] = inf;
1670 isclosedchain[c] = !inf;
1675 std::vector<Mmap<T>> parts;
1676 for (std::size_t r : L.
inchain[c]) {
1678 if (L.
disabled[ist - 1][r - 1] || !(L.
rates(ist - 1, r - 1) > zero)) {
1688 m.
Dc.assign(1, src.
D1);
1689 lambda[c] += L.
rates(ist - 1, r - 1);
1693 chainSysArrivals[c] = parts[0];
1694 for (std::size_t p = 1; p < parts.size(); ++p)
1696 std::vector<
Mmap<T>>{chainSysArrivals[c], parts[p]},
opt.space_max);
1697 for (std::size_t r : L.
inchain[c])
1698 TN(ist - 1, r - 1) = L.
disabled[ist - 1][r - 1] ? zero : L.
rates(ist - 1, r - 1);
1701 std::vector<bool> finite_srv(M,
false);
1702 for (std::size_t i = 0; i < M; ++i) finite_srv[i] = std::isfinite(L.
stations[i].nservers);
1704 bool ismixed =
false;
1706 bool anyc =
false, anyo =
false;
1707 for (std::size_t c = 0; c < C; ++c) {
1708 anyc = anyc || isclosedchain[c];
1709 anyo = anyo || isopenchain[c];
1711 ismixed = anyc && anyo;
1716 bool closedCap =
false;
1717 for (std::size_t c = 0; c < C; ++c) closedCap = closedCap || isclosedchain[c];
1720 std::vector<std::size_t> all_classes(K);
1721 for (std::size_t r = 0; r < K; ++r) all_classes[r] = r + 1;
1722 auto resptime_floor = [&](
const std::vector<std::size_t>& rlist) {
1723 for (std::size_t i = 0; i < M; ++i) {
1724 if (exact_station[i])
continue;
1725 for (std::size_t r : rlist) {
1726 const std::size_t k = r - 1;
1727 if (V(i, k) > zero) {
1728 if (!finite_srv[i]) {
1731 const T byLittle = (TN(i, k) > zero) ? T(QN(i, k) / TN(i, k)) : zero;
1732 RN(i, k) = (S(i, k) > byLittle) ? S(i, k) : byLittle;
1737 QN(i, k) = T(RN(i, k) * TN(i, k));
1744 double delta = std::numeric_limits<double>::infinity();
1746 while (delta > tol && it <=
opt.iter_max) {
1753 for (std::size_t i = 0; i < M; ++i) {
1754 if (!finite_srv[i])
continue;
1756 for (std::size_t r = 0; r < K; ++r) s += num_traits<T>::to_double(UN(i, r));
1757 if (s > Umax) Umax = s;
1759 if (ismixed || Umax < 1.0) {
1760 for (std::size_t c = 0; c < C; ++c) {
1761 if (!isclosedchain[c])
continue;
1763 for (std::size_t r : L.
inchain[c]) Nc += L.
classes[r - 1].population;
1765 for (std::size_t i = 0; i < M; ++i)
1767 QNc = std::max(tol, QNc);
1769 for (std::size_t i = 0; i < M; ++i) Dsum += dem.
Lchain(i, c);
1776 const double w =
static_cast<double>(it) /
opt.iter_max;
1785 bool binding =
false;
1786 for (std::size_t i = 0; i < M; ++i) {
1787 if (!finite_srv[i])
continue;
1788 double Uopen = 0.0, Uclosed = 0.0;
1789 for (std::size_t c = 0; c < C; ++c) {
1792 if (isclosedchain[c]) Uclosed += u;
1795 if (Uclosed > tol) {
1797 theta = std::min(theta, (Ulim - Uopen) / Uclosed);
1800 if (binding && theta < 1.0)
1801 for (std::size_t c = 0; c < C; ++c)
1802 if (isclosedchain[c])
1804 }
else if (closedCap) {
1810 for (std::size_t i = 0; i < M; ++i) {
1811 if (!finite_srv[i])
continue;
1813 for (std::size_t c = 0; c < C; ++c)
1815 Uimpl = std::max(Uimpl, u / L.
stations[i].nservers);
1818 for (std::size_t c = 0; c < C; ++c)
1820 }
else if (Umax >= 1.0) {
1821 for (std::size_t c = 0; c < C; ++c)
1825 for (std::size_t c = 0; c < C; ++c) {
1826 if (isclosedchain[c]) {
1830 chainSysArrivals[c] =
1833 for (std::size_t i = 0; i < M; ++i)
1834 for (std::size_t r : L.
inchain[c]) TN(i, r - 1) = T(V(i, r - 1) * lambda[c]);
1837 for (std::size_t ind = 1; ind <= I; ++ind) {
1839 if (nd.
station == 0)
continue;
1840 const std::size_t ist = nd.
station;
1841 if (nd.
nodetype == qn::NodeType::Join) {
1842 for (std::size_t c = 0; c < C; ++c)
1843 for (std::size_t r : L.
inchain[c]) {
1844 std::size_t fanin = 0;
1846 for (std::size_t row = 0; row < L.
rtnodes.rows(); ++row)
1847 if (L.
rtnodes(row, (ind - 1) * K + (r - 1)) != zero) ++fanin;
1848 if (fanin == 0) fanin = 1;
1849 TN(ist - 1, r - 1) =
1851 UN(ist - 1, r - 1) = zero;
1852 QN(ist - 1, r - 1) = zero;
1853 RN(ist - 1, r - 1) = zero;
1858 const double ns = L.
stations[ist - 1].nservers;
1859 if (sched == SchedStrategy::INF) {
1860 for (std::size_t c = 0; c < C; ++c)
1861 for (std::size_t r : L.
inchain[c]) {
1862 const std::size_t k = r - 1;
1863 if (!(V(ist - 1, k) > zero)) {
1864 TN(ist - 1, k) = UN(ist - 1, k) = QN(ist - 1, k) = RN(ist - 1, k) = zero;
1867 TN(ist - 1, k) = T(lambda[c] * V(ist - 1, k));
1870 UN(ist - 1, k) = T(S(ist - 1, k) * TN(ist - 1, k));
1871 QN(ist - 1, k) = T(TN(ist - 1, k) * S(ist - 1, k));
1872 RN(ist - 1, k) = (TN(ist - 1, k) > zero)
1873 ? T(QN(ist - 1, k) / TN(ist - 1, k))
1876 }
else if (sched == SchedStrategy::PS) {
1877 for (std::size_t c = 0; c < C; ++c) {
1878 for (std::size_t r : L.
inchain[c]) {
1879 const std::size_t k = r - 1;
1880 if (!(V(ist - 1, k) > zero)) {
1881 TN(ist - 1, k) = UN(ist - 1, k) = zero;
1884 TN(ist - 1, k) = T(lambda[c] * V(ist - 1, k));
1885 UN(ist - 1, k) = T(S(ist - 1, k) * TN(ist - 1, k) /
1889 for (std::size_t k = 0; k < K; ++k) Uden += num_traits<T>::to_double(UN(ist - 1, k));
1891 for (std::size_t r : L.
inchain[c]) {
1892 const std::size_t k = r - 1;
1893 if (!(V(ist - 1, k) > zero)) {
1894 QN(ist - 1, k) = RN(ist - 1, k) = zero;
1899 RN(ist - 1, k) = (TN(ist - 1, k) > zero)
1900 ? T(QN(ist - 1, k) / TN(ist - 1, k))
1904 }
else if (sched == SchedStrategy::FCFS || sched == SchedStrategy::HOL ||
1905 sched == SchedStrategy::FCFSPRPRIO) {
1906 solve_fcfs_station(L,
opt, ist, lambda, V, S, Sknown, PH, PHset, svc,
1907 chainSysArrivals, ztchain, QN, UN, RN, TN, exact_station,
1909 }
else if (sched == SchedStrategy::LCFSPR) {
1910 solve_lcfspr_station(L,
opt, ist, lambda, V, S, Sknown, PH, PHset,
1911 chainSysArrivals, QN, UN, RN, TN, exact_station, car_warned);
1912 }
else if (sched != SchedStrategy::EXT) {
1919 std::string(
"solver_mam_basic: station '") + L.
stations[ist - 1].name +
1921 " discipline, which the dec.source decomposition does not model (it solves "
1922 "INF, PS, FCFS, HOL and LCFS-PR stations only)");
1927 resptime_floor(all_classes);
1930 for (std::size_t i = 0; i < M; ++i)
1931 for (std::size_t r = 0; r < K; ++r)
1937 for (std::size_t i = 0; i < M; ++i)
1938 for (std::size_t r = 0; r < K; ++r) QN(i, r) =
num_abs(QN(i, r));
1940 for (
int pass = 0; pass < 2; ++pass) {
1941 for (std::size_t c = 0; c < C; ++c) {
1944 for (std::size_t r : L.
inchain[c]) {
1945 if (std::isinf(L.
classes[r - 1].population)) closed =
false;
1946 else Nc += L.
classes[r - 1].population;
1950 for (std::size_t i = 0; i < M; ++i)
1951 for (std::size_t r : L.
inchain[c]) QNc += QN(i, r - 1);
1954 for (std::size_t i = 0; i < M; ++i)
1955 for (std::size_t r : L.
inchain[c]) QN(i, r - 1) *= f;
1959 if (closed && Nc == 0.0) {
1960 for (std::size_t i = 0; i < M; ++i)
1961 for (std::size_t r : L.
inchain[c]) {
1962 QN(i, r - 1) = UN(i, r - 1) = RN(i, r - 1) = TN(i, r - 1) = zero;
1964 for (std::size_t r : L.
inchain[c]) CN[r - 1] = XN[r - 1] = zero;
1979 std::vector<double> Qcl(M, 0.0);
1980 for (std::size_t c = 0; c < C; ++c) {
1981 if (!isclosedchain[c])
continue;
1982 std::vector<double> qc(M, 0.0);
1984 for (std::size_t r : L.
inchain[c]) {
1985 const std::size_t k = r - 1;
1986 Nc += L.
classes[k].population;
1987 for (std::size_t i = 0; i < M; ++i) {
1989 if (std::isnan(q) || !(V(i, k) > zero))
continue;
1993 std::vector<std::size_t> ord(M);
1994 for (std::size_t i = 0; i < M; ++i) ord[i] = i;
1995 std::stable_sort(ord.begin(), ord.end(),
1996 [&](std::size_t a, std::size_t b) { return qc[a] < qc[b]; });
1998 for (std::size_t j = 0; j < M; ++j) {
1999 const double take = std::min(qc[ord[j]], std::max(0.0, left));
2000 Qcl[ord[j]] += take;
2004 for (std::size_t i = 0; i < M; ++i) {
2005 if (L.
stations[i].sched != SchedStrategy::PS)
continue;
2007 for (std::size_t c = 0; c < C; ++c)
2009 for (std::size_t r : L.
inchain[c])
2012 for (std::size_t c = 0; c < C; ++c) {
2013 if (!isopenchain[c])
continue;
2014 for (std::size_t r : L.
inchain[c]) {
2015 const std::size_t k = r - 1;
2016 if (!(V(i, k) > zero))
continue;
2018 RN(i, k) = (TN(i, k) > zero) ? T(QN(i, k) / TN(i, k)) : zero;
2024 for (std::size_t r = 0; r < K; ++r) {
2026 for (std::size_t i = 0; i < M; ++i) s += RN(i, r);
2029 for (std::size_t c = 0; c < C; ++c)
2030 for (std::size_t r : L.
inchain[c]) XN[r - 1] = TN(L.
classes[r - 1].refstat - 1, r - 1);
Acyclic phase-type fitters from the first two moments.
UnsupportedError(const std::string &what)
A network plus its refreshed NetworkStruct.
std::size_t nof_nodes() const
std::vector< std::vector< Distrib< T > > > service
service[i][r], 0-based station and class; a disabled entry marks a pair never visited.
std::vector< std::vector< bool > > disabled
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< NodeDef > nodes
every node, in creation order
Steady-state distribution of a continuous-time Markov chain.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
options.timeout: one wall-clock budget per top-level solve, on a per-THREAD deadline stack.
The option and result types SolverMAM shares with its analyzers.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
The MMAP assembly primitives solver_mam_basic.m builds its per-station arrival stream from: mmap_expo...
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
The MMAP[K]/PH[K]/1 FCFS queue: per-class mean number in system and per-class queue-length distributi...
The MMAP[K]/PH[K]/1 priority queue, preemptive resume (mmapph1prpr_*) and non-preemptive (mmapph1nppr...
SnRtStations< T > sn_rt_stations(const qn::NetworkStruct< T > &sn)
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.
T dist_lst(const Distrib< T > &d, const T &s)
sn.lst: the Laplace-Stieltjes transform E[exp(-sX)].
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
@ LOGLOGISTIC
Three reliability laws of Trivedi and Bobbio, "Reliability andAvailability Engineering" (2017),...
const char * sched_to_text(SchedStrategy s)
T dist_moment(const Distrib< T > &d, unsigned k)
The k-th raw moment.
double tot(const std::vector< double > &pops)
std::string name(const Element *el, const std::string &key="name")
std::vector< T > map_acf(const Map< T > &m, const std::vector< unsigned > &lags)
Autocorrelation coefficients of the inter-arrival times at the given lags,.
Mmap< T > mmap_normalize(const Mmap< T > &in)
Clamp negative off-diagonal and per-class entries to zero and rebuild D1 and the diagonal of D0 from ...
std::vector< T > mmapph1fcfs_ncmean(const Mmap< T > &arrival, const std::vector< PhService< T > > &svc)
Per-class mean number of customers in the system, BUTools' 'ncMoms', 1.
std::vector< T > mmap_lambda(const Mmap< T > &m)
Alias kept for parity with the MATLAB name.
Mmap< T > mmap_mark_probs(const Mmap< T > &in, const Matrix< T > &prob)
Re-mark an MMAP by a (K x R) probability matrix (mmap_mark.m): a type-k arrival is reported as class ...
Mmap< T > mmap_scale_perclass(const Mmap< T > &in, const std::vector< T > &M)
Retarget the per-class MEAN inter-arrival times (mmap_scale.m, vector form).
QbdMapMap1Result< T > qbd_mapmap1(const Map< T > &arrival, const Map< T > &service_in, const T &util, std::size_t max_levels)
MAP/MAP/1 queue (qbd_mapmap1.m).
T qbd_setupdelayoff(const T &lambda, const T &mu, const T &alpharate, const T &alphascv, const T &betarate, const T &betascv)
Mean queue length of the M/M/1 queue with setup delay and delay-off (qbd_setupdelayoff....
Mmap< T > mmap_exponential_vec(const std::vector< T > &lambda, std::size_t n=1)
Order-n MMAP with the given per-class arrival rates (mmap_exponential.m).
mva::MvaSolution< T > solver_mam_basic(const qn::NetworkStruct< T > &L, const MamOptions &opt)
Port of solver_mam_basic.m.
std::vector< std::vector< T > > mmapph1prpr_ncmoms(const Mmap< T > &arrival, const std::vector< PhService< T > > &svc, std::size_t n, const PrioQueueOptions &opt=PrioQueueOptions())
Per-class moments 1..n of the number of jobs, MMAP[K]/PH[K]/1 preemptive resume priority.
std::vector< std::vector< T > > mmapph1fcfs_ncdistr(const Mmap< T > &arrival, const std::vector< PhService< T > > &svc, std::size_t levels)
Per-class queue-length distribution, BUTools' 'ncDistr', n: P(N_k = 0..n-1).
Map< T > map_exponential_mean(const T &mean)
Poisson process with the given mean inter-arrival time (map_exponential.m).
Map< T > map_scale_rate(const Map< T > &in, const T &new_mean)
Rescale to a target mean WITHOUT the feasibility repair, for a matrix exponential.
Map< T > map_scale(const Map< T > &in, const T &new_mean)
Rescale time so that the mean inter-arrival time becomes new_mean.
std::vector< T > map_pie(const Map< T > &m)
Phase distribution seen by an arriving job, pie = pi D1 / (pi D1 e).
T map_scv(const Map< T > &m)
Squared coefficient of variation.
SetupDelayoffClosed< T > qbd_setupdelayoff_closed(const T &N, const T &Z, const T &mu, const T &alpharate, const T &alphascv, const T &betarate, const T &betascv)
Mean queue length and throughput of a FINITE-POPULATION queue with setup delay and delay-off,...
T map_lambda(const Map< T > &m)
Stationary arrival rate, lambda = pi D1 e.
std::vector< std::vector< T > > mmapph1nppr_ncmoms(const Mmap< T > &arrival, const std::vector< PhService< T > > &svc, std::size_t n, const PrioQueueOptions &opt=PrioQueueOptions())
Per-class moments 1..n of the number of jobs, MMAP[K]/PH[K]/1 non-preemptive priority.
Mmap< T > mmap_super_safe(const std::vector< Mmap< T > > &in, std::size_t maxorder)
Order-bounded superposition of several MMAPs (mmap_super_safe.m).
std::vector< T > ctmc_solve(const Matrix< T > &Qin, const std::string &linsolver=std::string())
Steady-state distribution of a continuous-time Markov chain.
ChainDemands< T > sn_get_demands_chain(const qn::NetworkStruct< T > &L)
Port of sn_get_demands_chain.
MapMcResult< T > qsys_mapmc(const mam::Map< T > &arrival, const T &mu, unsigned c, std::size_t dist_size)
MAP/M/c by the matrix-geometric solution.
MapPhcResult< T > qsys_mapmapc(const mam::Map< T > &arrival, const mam::Map< T > &service, unsigned c, std::size_t dist_size=500, std::size_t num_w_moms=3)
MAP/MAP/c FCFS with the service phase carried in start order, exactly.
MmapGk1Result< T > qsys_mmapgk1(const std::vector< Matrix< T > > &MMAP, const std::vector< MmapGk1Service< T > > &svc, const std::vector< T > &w_points, std::size_t num_w_moms, double tol, std::size_t iter_max, std::size_t quad_nodes)
MMAP[K]/G[K]/1 FCFS, per type.
MmckResult< T > qsys_mmck(const T &lambda, const T &mu, unsigned c, unsigned K)
Exact analysis of the M/M/c/K queue (truncated Erlang form).
MapPhcResult< T > qsys_mapphc(const mam::Map< T > &arrival, const std::vector< T > &alpha, const Matrix< T > &S, unsigned c, std::size_t dist_size, std::size_t num_w_moms, const std::vector< T > &w_points)
MAP/PH/c FCFS, exactly.
MmapMap1Result< T > qsys_mmapmap1(const mam::Mmap< T > &arrival, const std::vector< mam::Map< T > > &svc, std::vector< bool > carry=std::vector< bool >())
MMAP[K]/MAP[K]/1 FCFS with per-class carried service phases, exactly.
MmapG1kResult< T > qsys_mmapg1k(const Matrix< T > &D0, const std::vector< Matrix< T > > &D1c, const ServiceLaw< T > &svc, std::size_t K, const T &tol, std::size_t nmaxCap)
MMAP[K]/G/1/K with tail drop.
MmapGk1LcfsprResult< T > qsys_mmapgk1_lcfspr(const std::vector< Matrix< T > > &MMAP, const std::vector< MmapGk1Service< T > > &svc, const MmapGk1LcfsprOptions< T > &opt)
MMAP[K]/G[K]/1 LCFS-PR, per type, by Takine's matrix product form.
DmcResult< T > qsys_dmc(const T &lambda, const T &mu, unsigned c, unsigned truncation, unsigned quadSteps)
D/M/c: deterministic interarrival times, exponential service.
PhMcResult< T > qsys_phmc(const std::vector< T > &alpha, const Matrix< T > &Tm, const T &mu, unsigned c, unsigned maxIter, const T &tol)
Exact PH/M/c by Neuts' matrix-geometric method.
MapDcResult< T > qsys_mapdc(const mam::Map< T > &arrival, const T &s, unsigned c, std::size_t dist_size, unsigned max_arrivals, std::size_t max_levels, const T &tol, std::size_t num_steps)
MAP/D/c by Crommelin's exact embedded lattice chain.
double sn_get_buffer_size(const qn::NetworkStruct< T > &sn, std::size_t ist)
Physical buffer size of a station, in jobs, the one in service included.
bool timeout_exceeded()
The checkpoint: true when the innermost budget has run out, marking it tripped.
Conservation laws of a layered queueing network, enumerated from its structure.
std::vector< T > vecmul(const std::vector< T > &v, const Matrix< T > &A)
Row vector times matrix, v A.
Matrix< T > inverse(const Matrix< T > &A)
Inverse by LU with one factorization and n back substitutions.
A queueing network and its refreshed NetworkStruct.
The MAP/MAP/1 queue solved as a quasi-birth-death process.
Mean queue length of an M/M/1 queue with a setup delay and a delayed-off period, solved as a QBD.
D/M/c: deterministic interarrival times, exponential service.
The MAP/D/c FCFS queue: c servers, deterministic service of length s, fed by a Markovian arrival proc...
The MAP/MAP/c FCFS queue whose service MAP is CARRIED in service-start order.
The MAP/M/c FCFS queue: c identical exponential servers of rate mu fed by a Markovian arrival process...
The MAP/PH/c FCFS queue, solved exactly.
Exact per-class throughput and loss ratio of an MMAP[K]/G/1/K tail-drop queue.
The MMAP[K]/G[K]/1 FCFS queue: K customer types with class-dependent GENERAL service,...
The MMAP[K]/G[K]/1 LCFS-PR queue: K customer types with class-dependent GENERAL service under preempt...
The MMAP[K]/MAP[K]/1 FCFS queue in which each class's service phase is CARRIED.
Exact analysis of the M/M/c/K queue (truncated Erlang form).
Exact PH/M/c by Neuts' matrix-geometric method.
Chain aggregation and de-aggregation.
Physical buffer size of a station, in jobs, the one in service included.
Port of matlab/src/api/sn/sn_rt_stations.m.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
static constexpr double Immediate
Rate of an Immediate distribution; its mean is 1/Immediate = 1e-8.
static constexpr double FineTol
static constexpr double CoarseTol
The options SolverMAM reads.
A MAP as the pair of matrices (D0, D1).
An MMAP: the underlying MAP plus the per-class arrival matrices.
std::vector< Matrix< T > > Dc
per-class matrices, sum_c Dc = D1
One class's phase-type service law, He's (sigma_k, S_k).
The chain-level view of a layer, as sn_get_demands_chain returns it.
Matrix< T > Lchain
(M x C) demand
Class-level results, the [Q,U,R,T,C,X] of the MATLAB analyzers.
static ServiceLaw phase_type(const std::vector< T > &alpha, const Matrix< T > &Tmat)
Phase type (alpha, Tmat).