190 const std::size_t E = envObj.nstages();
195 for (it = 1; it <= opt.iter_max; ++it) {
196 for (std::size_t e = 0; e < E; ++e) analyze(e);
210 mam_backend = (
opt.stage_solver ==
"mam");
211 if (!mam_backend &&
opt.stage_solver !=
"ctmc")
213 "SolverENV statevec: stage solver '" +
opt.stage_solver +
214 "' is not available; the state-vector coupling needs an EXPLICIT generator over a "
215 "state space it can propagate a distribution across, which SolverCTMC exposes by "
216 "enumeration and SolverMAM by flattening its level-dependent QBD blocks");
217 if (
opt.sojourn !=
"stochastic" &&
opt.sojourn !=
"deterministic")
218 throw InputError(
"SolverENV statevec: unknown sojourn '" +
opt.sojourn +
"'");
226 envObj.reject_lqn_stages(
227 "SolverENV statevec",
228 "the state-vector coupling propagates a joint distribution over ONE stage "
229 "generator, which a layered model does not have -- it decomposes into a network "
231 envObj.refuse_semi_markov(
"statevec");
232 envObj.refuse_absorbing(
"statevec");
234 const std::size_t E = envObj.nstages();
235 M = envObj.stage(0).model.nstations;
236 K = envObj.stage(0).model.nclasses;
237 for (std::size_t e = 1; e < E; ++e)
238 if (envObj.stage(e).model.nstations != M || envObj.stage(e).model.nclasses != K)
240 "SolverENV statevec: every stage must have the same stations and classes; the "
241 "metrics are blended entrywise across them");
243 det_sojourn = (
opt.sojourn ==
"deterministic");
247 pi_enter.assign(E, std::vector<T>());
248 pi_enter_prev.assign(E, std::vector<T>());
249 pi_exit.assign(E, std::vector<std::vector<T>>());
250 pi_timeavg.assign(E, std::vector<T>());
251 dvals.assign(E, 0.0);
253 for (std::size_t e = 0; e < E; ++e)
254 dvals[e] = std::max(envObj.hold_time_mean(e),
255 std::numeric_limits<double>::epsilon());
271 const std::size_t E = envObj.nstages();
272 for (std::size_t e = 0; e < E; ++e) {
280 mo.method =
"default";
281 mo.cutoff = opt.mam_cutoff;
288 seed_entry_distributions();
289 pi_enter_prev = pi_enter;
314 void seed_entry_distributions() {
315 const std::size_t E = envObj.nstages();
324 std::vector<std::vector<T>> own(E);
325 for (std::size_t e = 0; e < E; ++e) {
329 : normalized(stages[e].pi);
332 bool sameSpace = E > 1;
333 for (std::size_t e = 1; e < E && sameSpace; ++e)
334 sameSpace = (state_count(e) == state_count(0));
336 std::vector<T> shared;
338 std::vector<double> w(E, 1.0 /
static_cast<double>(E));
340 bool usable = envObj.prob_env.size() == E;
341 for (std::size_t e = 0; e < E && usable; ++e) {
342 if (!std::isfinite(envObj.prob_env[e]) || envObj.prob_env[e] < 0.0) usable =
false;
343 else wsum += envObj.prob_env[e];
346 if (usable && wsum > 0.0)
347 for (std::size_t e = 0; e < E; ++e) w[e] = envObj.prob_env[e] / wsum;
349 const std::size_t n = state_count(0);
350 Matrix<T> Qbar(n, n, num_traits<T>::from_double(0.0));
351 for (std::size_t e = 0; e < E; ++e) {
352 const Matrix<T>& Qe = gen(e);
353 const T we = num_traits<T>::from_double(w[e]);
354 for (std::size_t i = 0; i < n; ++i)
355 for (std::size_t j = 0; j < n; ++j) Qbar(i, j) += we * Qe(i, j);
362 std::vector<T> pi0(n, num_traits<T>::from_double(0.0));
363 for (std::size_t e = 0; e < E; ++e) {
364 const T we = num_traits<T>::from_double(w[e]);
365 for (std::size_t i = 0; i < n; ++i) pi0[i] += we * own[e][i];
370 for (std::size_t e = 0; e < E; ++e) {
371 pi_enter[e] = shared.empty() ? own[e] : shared;
376 const Matrix<T>& gen(std::size_t e)
const {
377 return mam_backend ? mam_flat[e].Q : stages[e].chain.Q;
381 std::size_t state_count(std::size_t e)
const {
382 return mam_backend ? mam_flat[e].levelOf.size() : stages[e].chain.space.size();
389 void analyze(std::size_t e) {
390 const std::size_t E = envObj.nstages();
391 const Matrix<T>& Q = gen(e);
392 const std::vector<T> pi0 = pi_enter[e];
400 std::vector<T> avg, ex;
401 time_average(pi0, Q, dvals[e], avg, ex);
402 pi_exit[e].assign(E, std::vector<T>());
403 for (std::size_t h = 0; h < E; ++h)
404 if (envObj.arc(e, h).enabled) pi_exit[e][h] = ex;
410 if (exp_sojourn(e, s_e)) {
415 const std::vector<T> res = resolvent(pi0, Q, s_e);
416 pi_exit[e].assign(E, std::vector<T>());
417 for (std::size_t h = 0; h < E; ++h)
418 if (envObj.arc(e, h).enabled) pi_exit[e][h] = res;
431 sojourn_closed_form(e, pi0, Q);
439 const std::size_t E = envObj.nstages();
440 pi_enter_prev = pi_enter;
441 std::vector<std::vector<T>> next(E);
443 for (std::size_t e = 0; e < E; ++e) {
444 const std::size_t n = state_count(e);
445 std::vector<T> acc(n, num_traits<T>::from_int(0));
447 for (std::size_t h = 0; h < E; ++h) {
448 const double po = envObj.prob_orig(h, e);
449 if (!(po > 0.0))
continue;
450 if (pi_exit[h].size() <= e || pi_exit[h][e].empty())
continue;
451 const std::vector<T> pex = reset_apply(h, e, pi_exit[h][e]);
454 "SolverENV statevec: resetStateFun[" + std::to_string(h) +
"][" +
455 std::to_string(e) +
"] returned a " + std::to_string(pex.size()) +
456 "-element vector but stage " + std::to_string(e + 1) +
" has " +
458 " states; supply a reset that maps the state space of stage " +
459 std::to_string(h + 1) +
" onto that of stage " + std::to_string(e + 1));
460 const T w = num_traits<T>::from_double(po);
461 for (std::size_t s = 0; s < n; ++s) acc[s] += T(w * pex[s]);
465 const T w = num_traits<T>::from_double(wsum);
466 for (std::size_t s = 0; s < n; ++s) acc[s] = T(acc[s] / w);
470 next[e] = normalized(acc);
480 bool converged()
const {
481 const std::size_t E = envObj.nstages();
483 for (std::size_t e = 0; e < E; ++e) {
484 const std::vector<T>& a = pi_enter[e];
485 const std::vector<T>& b = pi_enter_prev[e];
486 if (a.empty() || b.empty() || a.size() != b.size())
return false;
488 for (std::size_t s = 0; s < a.size(); ++s)
489 d += std::fabs(num_traits<T>::to_double(a[s]) - num_traits<T>::to_double(b[s]));
490 l1 = std::max(l1, d);
492 if (!std::isfinite(l1))
return false;
493 return l1 < opt.iter_tol;
500 void finish(EnvStatevecSolution<T>& out) {
501 const std::size_t E = envObj.nstages();
502 const T
zero = num_traits<T>::from_int(0);
506 out.QStage.assign(E, Matrix<T>(M, K, zero));
507 out.UStage.assign(E, Matrix<T>(M, K, zero));
508 out.TStage.assign(E, Matrix<T>(M, K, zero));
510 for (std::size_t e = 0; e < E; ++e) {
511 if (pi_timeavg[e].empty())
continue;
512 Matrix<T> QNe, UNe, TNe;
516 const mam::LdqbdAvg<T> a =
526 envObj.stage(e).model, stages[e].chain, pi_timeavg[e],
false);
534 const T p = num_traits<T>::from_double(envObj.prob_env[e]);
535 for (std::size_t i = 0; i < M; ++i)
536 for (std::size_t r = 0; r < K && r < QNe.cols(); ++r) {
537 out.QN(i, r) += T(p * QNe(i, r));
538 out.UN(i, r) += T(p * UNe(i, r));
539 out.TN(i, r) += T(p * TNe(i, r));
542 out.pi_enter = pi_enter;
543 out.pi_timeavg = pi_timeavg;
556 void cache_blend(EnvStatevecSolution<T>& out)
const {
561 if (mam_backend)
return;
562 const std::size_t E = envObj.nstages();
563 const qn::NetworkStruct<T>& sn1 = envObj.stage(0).model;
564 const T
zero = num_traits<T>::from_int(0);
567 std::vector<T>(), std::vector<T>(), Matrix<T>(),
568 Matrix<T>(), std::vector<T>());
569 for (std::size_t c = 0; c < out.cache.caches.size(); ++c) {
570 const std::size_t ind = out.cache.caches[c].node;
571 const std::size_t isf = sn1.stateful_index(ind);
572 if (isf == 0)
continue;
573 std::vector<T> hitT(K, zero), missT(K, zero), dhitT(K, zero);
574 bool any_delayed =
false;
575 for (std::size_t e = 0; e < E; ++e) {
576 if (pi_timeavg[e].empty())
continue;
577 const std::vector<T> pv = normalized(pi_timeavg[e]);
578 const qn::NetworkStruct<T>& sne = envObj.stage(e).model;
579 const auto it = sne.nodeparam.find(ind);
580 if (it == sne.nodeparam.end())
continue;
581 const qn::CacheParam<T>& np = it->second;
582 const T w = num_traits<T>::from_double(envObj.prob_env[e]);
583 const auto& dr = stages[e].chain.dep_rates;
586 const std::vector<double> drate =
587 ctmc::analyzer_detail::cache_delayed_hit_rate(sne, ind, stages[e].chain, pv);
588 for (std::size_t k = 0; k < K && k < np.hitclass.size(); ++k) {
589 const std::size_t hc = np.hitclass[k];
590 const std::size_t mc = k < np.missclass.size() ? np.missclass[k] : 0;
591 if (hc == 0 || mc == 0)
continue;
592 T hit_rate =
zero, miss_rate =
zero;
593 for (std::size_t s = 0; s < pv.size(); ++s) {
594 hit_rate += T(pv[s] * dr[s][isf - 1][hc - 1]);
595 miss_rate += T(pv[s] * dr[s][isf - 1][mc - 1]);
598 if (k < drate.size())
599 dk = num_traits<T>::from_double(
600 std::min(drate[k], num_traits<T>::to_double(hit_rate)));
601 if (num_traits<T>::to_double(dk) > 0) any_delayed =
true;
602 hitT[k] += T(w * T(hit_rate - dk));
603 dhitT[k] += T(w * dk);
604 missT[k] += T(w * miss_rate);
607 const T nan = num_traits<T>::from_double(std::numeric_limits<double>::quiet_NaN());
608 std::vector<T> hp(K, nan), mp(K, nan), dp(K, nan);
609 for (std::size_t k = 0; k < K; ++k) {
610 const T
tot = T(hitT[k] + missT[k] + dhitT[k]);
611 if (num_traits<T>::to_double(tot) > 0) {
612 hp[k] = T(hitT[k] / tot);
613 mp[k] = T(missT[k] / tot);
614 dp[k] = T(dhitT[k] / tot);
617 out.cache.caches[c].hitprob = hp;
618 out.cache.caches[c].missprob = mp;
619 if (any_delayed) out.cache.caches[c].delayedprob = dp;
626 bool exp_sojourn(std::size_t e,
double& s)
const {
627 const std::size_t E = envObj.nstages();
630 for (std::size_t h = 0; h < E; ++h) {
631 const EnvArc<T>& a = envObj.arc(e, h);
632 if (!a.enabled)
continue;
634 s += num_traits<T>::to_double(a.dist.D1(0, 0));
637 return any && s > 0.0;
641 static std::vector<T> resolvent(
const std::vector<T>& pi0,
const Matrix<T>& Q,
double s) {
642 const std::size_t n = Q.rows();
643 const T sT = num_traits<T>::from_double(s);
644 Matrix<T> A(n, n, num_traits<T>::from_int(0));
645 for (std::size_t i = 0; i < n; ++i)
646 for (std::size_t j = 0; j < n; ++j)
647 A(j, i) = T((i == j ? sT : num_traits<T>::from_int(0)) - Q(i, j));
649 for (std::size_t i = 0; i < n; ++i) b[i] = T(sT * pi0[i]);
659 void sojourn_closed_form(std::size_t e,
const std::vector<T>& pi0,
const Matrix<T>& Q) {
660 const std::size_t E = envObj.nstages();
661 const mam::Mmap<double>& H = envObj.hold_time[e];
662 const std::size_t p = H.D0.rows(), d = Q.rows(), n = d * p;
663 const std::vector<double> alpha =
mam::map_pie(H.map());
664 const T
zero = num_traits<T>::from_int(0);
666 Matrix<T> At(n, n, zero);
667 for (std::size_t i = 0; i < d; ++i)
668 for (std::size_t k = 0; k < d; ++k) {
669 if (Q(i, k) == zero)
continue;
670 for (std::size_t j = 0; j < p; ++j) At(k * p + j, i * p + j) -= Q(i, k);
672 for (std::size_t i = 0; i < d; ++i)
673 for (std::size_t j = 0; j < p; ++j)
674 for (std::size_t l = 0; l < p; ++l)
675 if (H.D0(j, l) != 0.0)
676 At(i * p + l, i * p + j) -= num_traits<T>::from_double(H.D0(j, l));
678 for (std::size_t i = 0; i < d; ++i)
679 for (std::size_t j = 0; j < p; ++j) b[i * p + j] = T(pi0[i] * num_traits<T>::from_double(alpha[j]));
682 std::vector<T> avg(d, zero);
683 for (std::size_t i = 0; i < d; ++i)
684 for (std::size_t j = 0; j < p; ++j) avg[i] += x[i * p + j];
685 pi_timeavg[e] = normalized(avg);
687 pi_exit[e].assign(E, std::vector<T>());
688 for (std::size_t h = 0; h < E; ++h) {
689 if (!envObj.arc(e, h).enabled || h >= H.Dc.size())
continue;
690 std::vector<double> rate(p, 0.0);
691 for (std::size_t j = 0; j < p; ++j)
692 for (std::size_t l = 0; l < H.Dc[h].cols(); ++l) rate[j] += H.Dc[h](j, l);
693 std::vector<T> ex(d, zero);
695 for (std::size_t i = 0; i < d; ++i) {
696 for (std::size_t j = 0; j < p; ++j)
697 if (rate[j] != 0.0) ex[i] += T(x[i * p + j] * num_traits<T>::from_double(rate[j]));
698 mass += num_traits<T>::to_double(ex[i]);
700 if (mass > 0.0) pi_exit[e][h] = normalized(ex);
705 static void time_average(
const std::vector<T>& pi0,
const Matrix<T>& Q,
double d,
706 std::vector<T>& avg, std::vector<T>& ex) {
707 if constexpr (!num_traits<T>::has_transcendental) {
709 "SolverENV statevec: a deterministic sojourn needs ctmc_timeaverage, whose "
710 "uniformization carries Poisson weights exp(-qt) that are not rational; rerun "
711 "with --arith double");
713 const mc::TimeAverageResult<T> r =
722 std::vector<T> reset_apply(std::size_t h, std::size_t e,
const std::vector<T>& p)
const {
723 if (h < opt.reset_state.size() && e < opt.reset_state[h].size() && opt.reset_state[h][e])
724 return opt.reset_state[h][e](p);
731 static std::vector<T> normalized(
const std::vector<T>& p) {
732 const T
zero = num_traits<T>::from_int(0);
733 std::vector<T> q = p;
735 for (std::size_t s = 0; s < q.size(); ++s) {
736 if (num_traits<T>::to_double(q[s]) < 0) q[s] =
zero;
739 if (num_traits<T>::to_double(tot) > 0)
740 for (std::size_t s = 0; s < q.size(); ++s) q[s] = T(q[s] / tot);
744 Environment<T>& envObj;
745 EnvStatevecOptions<T> opt;
746 std::size_t M = 0, K = 0;
747 bool det_sojourn =
false;
748 std::vector<double> dvals;
749 std::vector<ctmc::CtmcSolution<T>> stages;
750 bool mam_backend =
false;
751 std::vector<mam::LdqbdBlocks<T>> mam_ld;
752 std::vector<mam::LdqbdFlat<T>> mam_flat;
753 std::vector<std::vector<T>> pi_enter, pi_enter_prev, pi_timeavg;
754 std::vector<std::vector<std::vector<T>>> pi_exit;