5#ifndef LINE_SOLVERS_AG_SOLVER_AG_H
6#define LINE_SOLVERS_AG_SOLVER_AG_H
120 std::size_t from_station = 0, from_class = 0;
121 std::size_t to_station = 0, to_class = 0;
123 bool is_negative =
false;
124 bool is_catastrophe =
false;
125 std::size_t removal_class = 0;
126 bool has_removal_dist =
false;
132 std::size_t num_processes = 0;
134 std::vector<std::vector<std::size_t>> process_map;
135 std::vector<RcatAction> actions;
136 std::vector<std::size_t> N;
137 std::vector<Matrix<T>> Aa, Pb;
138 std::vector<Matrix<T>> L;
139 std::vector<std::size_t> act, psv;
140 std::vector<bool> is_open_proc;
141 std::vector<std::size_t> nlev;
142 std::vector<std::size_t> mph;
143 std::vector<std::vector<std::size_t>> level;
145 std::vector<std::vector<T>> svcrate;
147 std::vector<std::vector<T>> svcdown;
153 std::vector<bool> extsig;
162struct RcatComponent {
163 std::size_t ist = 0, r = 0;
167 std::size_t na = 1, ns = 1, mph = 1, nlev = 0, N = 0;
168 T lam_neg = num_traits<T>::from_int(0);
169 T lam_cat = num_traits<T>::from_int(0);
170 std::vector<std::pair<T, std::size_t>> batch;
172 T prob_void = num_traits<T>::from_int(0);
176inline std::size_t blk(std::size_t n, std::size_t mph) {
return n * mph; }
180Matrix<T> madd_local(
const Matrix<T>& A,
const Matrix<T>& B) {
181 Matrix<T> C(A.rows(), A.cols(), num_traits<T>::from_int(0));
182 for (std::size_t i = 0; i < A.rows(); ++i)
183 for (std::size_t j = 0; j < A.cols(); ++j) C(i, j) = T(A(i, j) + B(i, j));
189void add_block(Matrix<T>& target, std::size_t li, std::size_t lj, std::size_t mph,
190 const Matrix<T>& block,
const T& scale) {
191 const std::size_t r0 = blk(li, mph), c0 = blk(lj, mph);
192 for (std::size_t i = 0; i < mph; ++i)
193 for (std::size_t j = 0; j < mph; ++j) target(r0 + i, c0 + j) += T(scale * block(i, j));
198void add_identity(Matrix<T>& target, std::size_t li, std::size_t lj, std::size_t mph,
200 const std::size_t r0 = blk(li, mph), c0 = blk(lj, mph);
201 for (std::size_t i = 0; i < mph; ++i) target(r0 + i, c0 + i) += scale;
206void set_identity(Matrix<T>& target, std::size_t li, std::size_t lj, std::size_t mph) {
207 const std::size_t r0 = blk(li, mph), c0 = blk(lj, mph);
208 const T one = num_traits<T>::from_int(1);
209 for (std::size_t i = 0; i < mph; ++i) target(r0 + i, c0 + i) = one;
214Matrix<T> level_block(
const Matrix<T>& Q, std::size_t li, std::size_t lj, std::size_t mph) {
215 Matrix<T> out(mph, mph, num_traits<T>::from_int(0));
216 const std::size_t r0 = blk(li, mph), c0 = blk(lj, mph);
217 for (std::size_t i = 0; i < mph; ++i)
218 for (std::size_t j = 0; j < mph; ++j) out(i, j) = Q(r0 + i, c0 + j);
229bool is_markovian_map(
const Matrix<T>& D0,
const Matrix<T>& D1) {
230 const std::size_t n = D0.rows();
231 if (n == 0 || D0.cols() != n || D1.rows() != n || D1.cols() != n)
return false;
233 for (std::size_t i = 0; i < n; ++i)
234 for (std::size_t j = 0; j < n; ++j) {
235 const double a = num_traits<T>::to_double(D0(i, j));
236 const double b = num_traits<T>::to_double(D1(i, j));
237 if (!std::isfinite(a) || !std::isfinite(b))
return false;
238 scale = std::max(scale, std::max(std::fabs(a), std::fabs(b)));
240 const double tol = 1e-9 * scale;
241 for (std::size_t i = 0; i < n; ++i) {
243 for (std::size_t j = 0; j < n; ++j) {
244 const double a = num_traits<T>::to_double(D0(i, j));
245 const double b = num_traits<T>::to_double(D1(i, j));
246 if (i != j && a < -tol)
return false;
247 if (b < -tol)
return false;
250 if (std::fabs(row) > tol)
return false;
263std::pair<Matrix<T>, Matrix<T>> proc_map(
const qn::NetworkStruct<T>& L, std::size_t ist,
265 const T
zero = num_traits<T>::from_int(0);
266 if (L.has_service_law(ist, r)) {
269 if (is_markovian_map(m.D0, m.D1))
return std::make_pair(m.D0, m.D1);
270 }
catch (
const std::exception&) {
274 T rate = L.disabled[ist][r] ?
zero : L.rates(ist, r);
275 if (!(rate > zero) || !std::isfinite(num_traits<T>::to_double(rate))) rate =
zero;
276 Matrix<T> D0(1, 1, zero), D1(1, 1, zero);
277 D0(0, 0) = T(zero - rate);
279 return std::make_pair(D0, D1);
283bool is_tridiagonal(
const Matrix<T>& Q) {
284 const std::size_t n = Q.rows();
285 for (std::size_t i = 0; i < n; ++i)
286 for (std::size_t j = 0; j < n; ++j) {
287 const std::size_t d = (i > j) ? (i - j) : (j - i);
288 if (d > 1 && std::fabs(num_traits<T>::to_double(Q(i, j))) > 1e-14)
return false;
298bool is_block_tridiagonal(
const Matrix<T>& Q,
const std::vector<std::size_t>& lvl) {
299 const std::size_t n = Q.rows();
300 for (std::size_t i = 0; i < n; ++i)
301 for (std::size_t j = 0; j < n; ++j) {
302 const std::size_t d = (lvl[i] > lvl[j]) ? (lvl[i] - lvl[j]) : (lvl[j] - lvl[i]);
303 if (d > 1 && std::fabs(num_traits<T>::to_double(Q(i, j))) > 1e-14)
return false;
317std::vector<T> birth_death_solve(
const Matrix<T>& Q) {
318 const std::size_t n = Q.rows();
319 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
320 if (n <= 1)
return std::vector<T>(1, one);
322 std::vector<T> pi(n, zero);
324 for (std::size_t i = 1; i < n; ++i) {
325 const T birth = Q(i - 1, i);
326 const T death = Q(i, i - 1);
327 pi[i] = (death >
zero) ? T(pi[i - 1] * birth / death) :
zero;
330 for (
const T& v : pi) total += v;
332 for (T& v : pi) v /= total;
334 const T u = one / num_traits<T>::from_int(
static_cast<long>(n));
335 for (T& v : pi) v = u;
352std::vector<T> stat_vector(
const Matrix<T>& C) {
353 const std::size_t m = C.rows();
354 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
356 Matrix<T> At(m, m, zero);
357 for (std::size_t i = 0; i < m; ++i)
358 for (std::size_t j = 0; j < m; ++j) At(j, i) = (j == 0) ? one : C(i, j);
359 std::vector<T> rhs(m, zero);
361 const std::vector<std::size_t> piv =
lu_factor(At);
377std::vector<T> qbd_finite_solve(
const Matrix<T>& Q, std::size_t m, std::size_t nlev) {
378 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
381 std::vector<Matrix<T>> C(nlev);
382 C[nlev - 1] = level_block(Q, nlev - 1, nlev - 1, m);
383 for (std::size_t t = nlev - 1; t-- > 0;) {
384 const Matrix<T> F = level_block(Q, t, t + 1, m);
385 const Matrix<T> D = level_block(Q, t + 1, t, m);
386 Matrix<T> negC = C[t + 1];
387 for (std::size_t i = 0; i < m; ++i)
388 for (std::size_t j = 0; j < m; ++j) negC(i, j) = T(zero - negC(i, j));
390 Matrix<T> Cn = level_block(Q, t, t, m);
391 const Matrix<T> corr =
matmul(step, D);
392 for (std::size_t i = 0; i < m; ++i)
393 for (std::size_t j = 0; j < m; ++j) Cn(i, j) += corr(i, j);
397 std::vector<T> pi(nlev * m, zero);
398 const std::vector<T> p0 = stat_vector(C[0]);
399 for (std::size_t i = 0; i < m; ++i) pi[i] = p0[i];
400 for (std::size_t n = 0; n + 1 < nlev; ++n) {
401 Matrix<T> negC = C[n + 1];
402 for (std::size_t i = 0; i < m; ++i)
403 for (std::size_t j = 0; j < m; ++j) negC(i, j) = T(zero - negC(i, j));
404 const Matrix<T> step =
matmul(level_block(Q, n, n + 1, m),
inverse(negC));
405 for (std::size_t j = 0; j < m; ++j) {
407 for (std::size_t i = 0; i < m; ++i) acc += T(pi[blk(n, m) + i] * step(i, j));
408 pi[blk(n + 1, m) + j] = acc;
413 for (
const T& v : pi) total += v;
415 for (T& v : pi) v /= total;
417 const T u = one / num_traits<T>::from_int(
static_cast<long>(nlev * m));
418 for (T& v : pi) v = u;
435std::vector<T> solve_component(
const Matrix<T>& Q, std::size_t mph, std::size_t nlev,
436 const std::vector<std::size_t>& lvl) {
438 if (is_tridiagonal(Q))
return birth_death_solve(Q);
439 }
else if (is_block_tridiagonal(Q, lvl)) {
440 return qbd_finite_solve(Q, mph, nlev);
449 std::vector<T> pi0, pi1;
451 T qlen = num_traits<T>::from_int(0);
470QbdTail<T> qbd_matrix_tail(
const Matrix<T>& Qk,
const Matrix<T>& A0,
const Matrix<T>& A1,
471 const Matrix<T>& A2, std::size_t mph) {
473 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
476 R =
qbd_R_logred(A2, A1, A0, 500u, T(num_traits<T>::from_double(1e-14)));
477 }
catch (
const std::exception&) {
480 for (std::size_t i = 0; i < mph; ++i)
481 for (std::size_t j = 0; j < mph; ++j) {
482 const double v = num_traits<T>::to_double(R(i, j));
485 if (!std::isfinite(v) || v < -1e-12)
return out;
488 const Matrix<T> B00 = level_block(Qk, 0, 0, mph);
489 Matrix<T> IR =
eye<T>(mph);
490 for (std::size_t i = 0; i < mph; ++i)
491 for (std::size_t j = 0; j < mph; ++j) IR(i, j) = T(IR(i, j) - R(i, j));
492 std::vector<T> tail_mass;
495 }
catch (
const std::exception&) {
504 for (std::size_t i = 0; i < mph; ++i) {
505 const double w = num_traits<T>::to_double(tail_mass[i]);
506 if (!std::isfinite(w) || w < 1.0 - 1e-9)
return out;
510 const Matrix<T> lower_right = madd_local(A1,
matmul(R, A2));
511 const std::size_t n2 = 2 * mph;
512 Matrix<T> SysT(n2, n2, zero);
513 for (std::size_t i = 0; i < mph; ++i)
514 for (std::size_t j = 0; j < mph; ++j) {
515 SysT(j, i) = B00(i, j);
516 SysT(mph + j, i) = A0(i, j);
517 SysT(j, mph + i) = A2(i, j);
518 SysT(mph + j, mph + i) = lower_right(i, j);
520 for (std::size_t i = 0; i < mph; ++i) {
522 SysT(0, mph + i) = tail_mass[i];
524 std::vector<T> v(n2, zero);
528 const std::vector<std::size_t> piv =
lu_factor(lu);
530 }
catch (
const std::exception&) {
533 for (std::size_t i = 0; i < n2; ++i)
534 if (!std::isfinite(num_traits<T>::to_double(v[i])))
return out;
536 out.pi0.assign(v.begin(), v.begin() +
static_cast<long>(mph));
537 out.pi1.assign(v.begin() +
static_cast<long>(mph), v.end());
538 const Matrix<T> IRinv =
inverse(IR);
539 out.busy =
vecmul(out.pi1, IRinv);
540 const std::vector<T> qv =
vecmul(out.busy, IRinv);
542 for (
const T& t : qv) q += t;
555std::vector<T> qbd_tail_expand(
const QbdTail<T>& g, std::size_t nlev, std::size_t mph) {
556 std::vector<T> pi(nlev * mph, num_traits<T>::from_int(0));
557 for (std::size_t i = 0; i < mph; ++i) pi[i] = g.pi0[i];
558 std::vector<T> v = g.pi1;
559 for (std::size_t n = 1; n < nlev; ++n) {
560 for (std::size_t i = 0; i < mph; ++i) pi[blk(n, mph) + i] = v[i];
568T pmf_at(
const std::vector<T>& pmf, std::size_t k) {
569 return k < pmf.size() ? pmf[k] : num_traits<T>::from_int(0);
579void add_batch_removal(Matrix<T>& B,
const std::vector<T>& pmf,
const T& rate,
580 std::size_t nlev, std::size_t mph) {
581 const T one = num_traits<T>::from_int(1),
zero = num_traits<T>::from_int(0);
582 for (std::size_t n = 1; n < nlev; ++n) {
583 for (std::size_t m = 1; m <= n; ++m) {
584 const T p = pmf_at(pmf, n - m);
585 if (p > zero) add_identity(B, n, m, mph, T(rate * p));
588 for (std::size_t j = 0; j + 1 <= n; ++j) cdf += pmf_at(pmf, j);
589 const T tail = T(one - cdf);
590 if (tail > zero) add_identity(B, n, 0, mph, T(rate * tail));
595inline double qbd_scalar_rho(
double f,
double b,
double g) {
596 const double inf = std::numeric_limits<double>::infinity();
600 if (f + g <= 0.0)
return inf;
603 const double c1 = -(f + b + g);
604 const double disc = c1 * c1 - 4.0 * b * f;
605 if (disc < 0.0)
return inf;
606 const double sq = std::sqrt(disc);
607 double r1 = (-c1 - sq) / (2.0 * b), r2 = (-c1 + sq) / (2.0 * b);
608 if (r1 > r2) std::swap(r1, r2);
609 return (r1 > 0.0) ? r1 : r2;
628bool is_signal_class(
const qn::NetworkStruct<T>& L, std::size_t r) {
629 return r < L.issignal.size() && L.issignal[r];
639lang::SignalType signal_type_of(
const qn::NetworkStruct<T>& L, std::size_t r) {
654void arrival_map(
const qn::NetworkStruct<T>& L, RcatComponent<T>& c,
655 const std::vector<std::size_t>& source_stations) {
657 const std::size_t K = L.nclasses;
658 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
659 bool have_arrival =
false;
660 Matrix<T> Da0(1, 1, zero), Da1(1, 1, zero);
662 for (std::size_t isrc : source_stations)
663 for (std::size_t s = 0; s < K; ++s) {
664 const bool is_signal = is_signal_class(L, s);
669 for (std::size_t sd = 0; sd < K; ++sd)
670 prob_src += L.rt(isrc * K + s, c.ist * K + sd);
672 prob_src = L.rt(isrc * K + s, c.ist * K + c.r);
674 if (!(prob_src > zero) || L.disabled[isrc][s])
continue;
675 const T src_rate = L.rates(isrc, s);
676 const bool removal_signal =
677 is_signal && (signal_type_of(L, s) == SignalType::NEGATIVE ||
678 signal_type_of(L, s) == SignalType::CATASTROPHE);
679 if (removal_signal) {
680 if (signal_type_of(L, s) == SignalType::CATASTROPHE) {
681 c.lam_cat += T(src_rate * prob_src);
682 }
else if (s < L.signalremdist.size() && !L.signalremdist[s].empty()) {
683 c.batch.emplace_back(T(src_rate * prob_src), s);
685 c.lam_neg += T(src_rate * prob_src);
689 if (!(src_rate > zero))
continue;
691 std::pair<Matrix<T>, Matrix<T>> sm = proc_map(L, isrc, s);
692 Matrix<T> S0 = sm.first, S1 = sm.second;
693 if (prob_src < one) {
694 const T keep = prob_src, drop = T(one - prob_src);
695 for (std::size_t i = 0; i < S0.rows(); ++i)
696 for (std::size_t j = 0; j < S0.cols(); ++j) S0(i, j) += T(drop * S1(i, j));
697 for (std::size_t i = 0; i < S1.rows(); ++i)
698 for (std::size_t j = 0; j < S1.cols(); ++j) S1(i, j) = T(keep * S1(i, j));
701 Da0 =
krons(Da0, S0);
702 Da1 =
krons(Da1, S1);
715Matrix<T> build_local_rates(
const qn::NetworkStruct<T>& L,
const RcatComponent<T>& c,
716 const std::vector<std::size_t>& sink_nodes) {
717 const std::size_t K = L.nclasses;
718 const std::size_t mph = c.mph, nlev = c.nlev;
719 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
720 Matrix<T> Lm(c.N, c.N, zero);
726 add_block(Lm, 0, 0, mph,
kron(c.Da0,
eye<T>(c.ns)), one);
727 const Matrix<T> Lbusy =
krons(c.Da0, c.Ds0);
728 for (std::size_t n = 1; n < nlev; ++n) add_block(Lm, n, n, mph, Lbusy, one);
731 const Matrix<T> Aup =
kron(c.Da1,
eye<T>(c.ns));
732 for (std::size_t n = 0; n + 1 < nlev; ++n) add_block(Lm, n, n + 1, mph, Aup, one);
736 add_block(Lm, nlev - 1, nlev - 1, mph, Aup, one);
739 if (c.lam_cat > zero)
740 for (std::size_t n = 1; n < nlev; ++n) add_identity(Lm, n, 0, mph, c.lam_cat);
741 for (
const std::pair<T, std::size_t>& ba : c.batch)
742 add_batch_removal(Lm, L.signalremdist[ba.second], ba.first, nlev, mph);
744 if (c.lam_neg > zero)
745 for (std::size_t n = 1; n < nlev; ++n) add_identity(Lm, n, n - 1, mph, c.lam_neg);
749 if (L.disabled[c.ist][c.r])
return Lm;
750 const T mu = L.rates(c.ist, c.r);
751 if (!(mu > zero))
return Lm;
757 const std::size_t node = L.node_of_station(c.ist + 1);
760 for (std::size_t jsnk : sink_nodes)
761 for (std::size_t s = 0; s < K; ++s) {
762 const std::size_t from = (node - 1) * K + c.r, to = (jsnk - 1) * K + s;
763 if (from < L.rtnodes.rows() && to < L.rtnodes.cols())
764 prob_sink += L.rtnodes(from, to);
768 prob_sink += c.prob_void;
769 const T prob_self = L.rt(c.ist * K + c.r, c.ist * K + c.r);
771 if (prob_sink > zero)
772 for (std::size_t n = 1; n < nlev; ++n) add_block(Lm, n, n - 1, mph, c.Dsvc, prob_sink);
776 if (prob_self > zero)
777 for (std::size_t n = 1; n < nlev; ++n) add_block(Lm, n, n, mph, c.Dsvc, prob_self);
783RcatModel<T> build_rcat(
const qn::NetworkStruct<T>& L, std::size_t max_states) {
785 const std::size_t M = L.nstations, K = L.nclasses;
786 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
788 m.process_map.assign(M, std::vector<std::size_t>(K, 0));
790 std::vector<std::size_t> source_stations, queue_stations, sink_nodes;
791 for (std::size_t i = 0; i < L.nof_nodes(); ++i)
792 if (L.nodes[i].nodetype == qn::NodeType::Sink) sink_nodes.push_back(i + 1);
793 for (std::size_t ist = 0; ist < M; ++ist) {
800 if (ty == qn::NodeType::Source) source_stations.push_back(ist);
801 else if (ty != qn::NodeType::Sink) queue_stations.push_back(ist);
806 std::size_t pidx = 0;
807 for (std::size_t ist : queue_stations)
808 for (std::size_t r = 0; r < K; ++r) {
809 if (is_signal_class(L, r) || L.disabled[ist][r])
continue;
810 if (!(L.rates(ist, r) > zero))
continue;
811 m.process_map[ist][r] = ++pidx;
813 m.num_processes = pidx;
814 if (m.num_processes == 0)
return m;
818 const std::vector<double> njobs = L.njobs();
819 std::vector<RcatComponent<T>> comp(m.num_processes);
820 m.N.assign(m.num_processes, 0);
821 m.nlev.assign(m.num_processes, 0);
822 m.mph.assign(m.num_processes, 1);
823 m.level.assign(m.num_processes, std::vector<std::size_t>());
824 m.svcrate.assign(m.num_processes, std::vector<T>());
825 m.svcdown.assign(m.num_processes, std::vector<T>());
826 m.is_open_proc.assign(m.num_processes,
false);
827 for (std::size_t ist : queue_stations)
828 for (std::size_t r = 0; r < K; ++r) {
829 const std::size_t p = m.process_map[ist][r];
830 if (p == 0 || m.N[p - 1] != 0)
continue;
831 RcatComponent<T>& c = comp[p - 1];
834 const std::pair<Matrix<T>, Matrix<T>> svc = proc_map(L, ist, r);
837 arrival_map(L, c, source_stations);
841 if (std::isfinite(njobs[r])) {
846 for (
const std::vector<bool>& ch : L.chains)
847 if (r < ch.size() && ch[r]) {
848 for (std::size_t s = 0; s < K; ++s)
849 if (ch[s]) popc += njobs[s];
852 c.nlev =
static_cast<std::size_t
>(popc) + 1;
855 m.is_open_proc[p - 1] =
true;
859 c.N = c.nlev * c.mph;
862 m.nlev[p - 1] = c.nlev;
863 m.mph[p - 1] = c.mph;
864 m.level[p - 1].assign(c.N, 0);
865 for (std::size_t n = 0; n < c.nlev; ++n)
866 for (std::size_t j = 0; j < c.mph; ++j) m.level[p - 1][n * c.mph + j] = n;
867 m.svcdown[p - 1].assign(c.mph, zero);
868 for (std::size_t i = 0; i < c.mph; ++i) {
870 for (std::size_t j = 0; j < c.mph; ++j) acc += c.Dsvc(i, j);
871 m.svcdown[p - 1][i] = acc;
873 m.svcrate[p - 1].assign(c.N, zero);
874 for (std::size_t n = 1; n < c.nlev; ++n)
875 for (std::size_t j = 0; j < c.mph; ++j)
876 m.svcrate[p - 1][n * c.mph + j] = m.svcdown[p - 1][j];
879 for (std::size_t ist : queue_stations)
880 for (std::size_t r = 0; r < K; ++r) {
881 if (m.process_map[ist][r] == 0)
continue;
882 const bool is_removal =
883 is_signal_class(L, r) && (signal_type_of(L, r) == SignalType::NEGATIVE ||
884 signal_type_of(L, r) == SignalType::CATASTROPHE);
885 const bool is_cat = is_removal && signal_type_of(L, r) == SignalType::CATASTROPHE;
886 const bool has_dist =
887 is_removal && r < L.signalremdist.size() && !L.signalremdist[r].empty();
888 for (std::size_t jst : queue_stations)
889 for (std::size_t s = 0; s < K; ++s) {
890 if (m.process_map[jst][s] == 0)
continue;
891 if (ist == jst && r == s)
continue;
892 const T pr = L.rt(ist * K + r, jst * K + s);
893 if (!(pr > zero))
continue;
895 a.from_station = ist;
899 a.prob = num_traits<T>::to_double(pr);
900 a.is_negative = is_removal;
901 a.is_catastrophe = is_cat;
902 a.has_removal_dist = has_dist;
904 m.actions.push_back(a);
913 for (std::size_t jst : queue_stations)
914 for (std::size_t s = 0; s < K; ++s) {
915 if (!is_signal_class(L, s))
continue;
916 const T pr = L.rt(ist * K + r, jst * K + s);
917 if (!(pr > zero))
continue;
919 const bool s_cat = st == SignalType::CATASTROPHE;
920 if (!s_cat && st != SignalType::NEGATIVE)
922 "solver_ag: class '" + L.classes[r].name +
"' switches into signal class '" +
923 L.classes[s].name +
"' at station '" + L.stations[jst].name +
924 "', but only a NEGATIVE or CATASTROPHE signal has a G-network effect RCAT "
926 std::vector<std::size_t> targets;
927 for (std::size_t t = 0; t < K; ++t)
928 if (m.process_map[jst][t] != 0) targets.push_back(t);
929 if (targets.size() > 1)
931 "solver_ag: signal class '" + L.classes[s].name +
932 "', produced by departures of class '" + L.classes[r].name +
933 "', reaches station '" + L.stations[jst].name +
"', which serves " +
934 std::to_string(targets.size()) +
935 " positive classes. A departure synchronizes with ONE component in RCAT, "
936 "so the removal cannot be split across classes");
937 if (targets.empty()) {
939 comp[m.process_map[ist][r] - 1].prob_void += pr;
943 a.from_station = ist;
946 a.to_class = targets[0];
947 a.prob = num_traits<T>::to_double(pr);
948 a.is_negative =
true;
949 a.is_catastrophe = s_cat;
951 !s_cat && s < L.signalremdist.size() && !L.signalremdist[s].empty();
953 m.actions.push_back(a);
957 m.L.reserve(m.num_processes);
958 for (std::size_t p = 0; p < m.num_processes; ++p)
959 m.L.push_back(build_local_rates(L, comp[p], sink_nodes));
960 m.extsig.assign(m.num_processes,
false);
961 for (std::size_t p = 0; p < m.num_processes; ++p)
962 m.extsig[p] = comp[p].lam_neg > zero || comp[p].lam_cat > zero ||
963 !comp[p].batch.empty() || comp[p].prob_void >
zero;
965 const std::size_t A = m.actions.size();
970 for (
const RcatAction& a : m.actions) {
971 const std::size_t pa = m.process_map[a.from_station][a.from_class] - 1;
972 const std::size_t pp = m.process_map[a.to_station][a.to_class] - 1;
976 const RcatComponent<T>& ca = comp[pa];
977 const T prob = num_traits<T>::from_double(a.prob);
978 Matrix<T> Am(ca.N, ca.N, zero);
979 for (std::size_t n = 1; n < ca.nlev; ++n)
980 add_block(Am, n, n - 1, ca.mph, ca.Dsvc, prob);
987 if (std::isfinite(njobs[a.from_class])) {
988 const std::size_t off = blk(ca.nlev - 1, ca.mph);
989 for (std::size_t i = 0; i < ca.mph; ++i)
990 Am(off + i, off + i) += T(m.svcdown[pa][i] * prob);
994 const RcatComponent<T>& cp = comp[pp];
995 Matrix<T> Bm(cp.N, cp.N, zero);
997 if (a.is_catastrophe) {
998 for (std::size_t n = 0; n < cp.nlev; ++n) set_identity(Bm, n, 0, cp.mph);
999 }
else if (a.has_removal_dist) {
1000 add_batch_removal(Bm, L.signalremdist[a.removal_class], one, cp.nlev, cp.mph);
1001 set_identity(Bm, 0, 0, cp.mph);
1003 set_identity(Bm, 0, 0, cp.mph);
1004 for (std::size_t n = 1; n < cp.nlev; ++n) set_identity(Bm, n, n - 1, cp.mph);
1011 for (std::size_t n = 0; n + 1 < cp.nlev; ++n) set_identity(Bm, n, n + 1, cp.mph);
1012 set_identity(Bm, cp.nlev - 1, cp.nlev - 1, cp.mph);
1021Matrix<T> assemble_generator(
const RcatModel<T>& m,
const std::vector<T>& x, std::size_t k) {
1022 Matrix<T> Qk = m.L[k];
1023 for (std::size_t c = 0; c < m.actions.size(); ++c) {
1024 if (m.psv[c] == k) {
1025 for (std::size_t i = 0; i < Qk.rows(); ++i)
1026 for (std::size_t j = 0; j < Qk.cols(); ++j) Qk(i, j) += T(x[c] * m.Pb[c](i, j));
1027 }
else if (m.act[c] == k) {
1028 for (std::size_t i = 0; i < Qk.rows(); ++i)
1029 for (std::size_t j = 0; j < Qk.cols(); ++j) Qk(i, j) += m.Aa[c](i, j);
1040Matrix<double> ag_as_double(
const Matrix<T>& m) {
1041 Matrix<double> out(m.rows(), m.cols(), 0.0);
1042 for (std::size_t i = 0; i < m.rows(); ++i)
1043 for (std::size_t j = 0; j < m.cols(); ++j)
1044 out(i, j) = num_traits<T>::to_double(m(i, j));
1054AgWirePayload wire_payload(
const RcatModel<T>& m, std::size_t k) {
1056 a.k =
static_cast<int>(k);
1057 a.n =
static_cast<int>(m.N[k]);
1058 a.mph =
static_cast<int>(m.mph[k]);
1059 a.nlev =
static_cast<int>(m.nlev[k]);
1060 a.level.assign(m.level[k].begin(), m.level[k].end());
1062 for (std::size_t c = 0; c < m.actions.size(); ++c) {
1063 if (m.psv[c] == k) {
1064 a.passive_c.push_back(
static_cast<int>(c));
1065 a.passive_m.push_back(
ag_triplets(ag_as_double(m.Pb[c])));
1066 }
else if (m.act[c] == k) {
1067 a.active_c.push_back(
static_cast<int>(c));
1068 a.active_m.push_back(
ag_triplets(ag_as_double(m.Aa[c])));
1084void compute_equilibrium(
const RcatModel<T>& m,
const std::vector<T>& x,
1085 std::vector<std::vector<T>>& pi, std::vector<Matrix<T>>& Q,
1086 const AgOptions* opt =
nullptr, AgWorkerPool* pool =
nullptr) {
1087 pi.assign(m.num_processes, std::vector<T>());
1088 Q.assign(m.num_processes, Matrix<T>());
1090 auto gen = [&](std::size_t k) {
return assemble_generator(m, x, k); };
1091 auto sol = [&](
const Matrix<T>& Qk, std::size_t k) {
1092 return solve_component(Qk, m.mph[k], m.nlev[k], m.level[k]);
1095 const std::string mode = (opt ==
nullptr) ? std::string(
exec_serial()) : opt->exec;
1099 auto payload = [&](std::size_t k) {
return wire_payload(m, k); };
1112void compute_equilibrium_qbd(
const RcatModel<T>& m,
const std::vector<T>& x,
1113 std::vector<std::vector<T>>& pi, std::vector<Matrix<T>>& Q,
1114 std::vector<double>& rho_proc, std::vector<bool>& is_geom,
1115 std::vector<QbdTail<T>>& geom_data) {
1116 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
1117 pi.assign(m.num_processes, std::vector<T>());
1118 Q.assign(m.num_processes, Matrix<T>());
1119 rho_proc.assign(m.num_processes, 0.0);
1120 is_geom.assign(m.num_processes,
false);
1121 geom_data.assign(m.num_processes, QbdTail<T>());
1123 for (std::size_t k = 0; k < m.num_processes; ++k) {
1124 const std::size_t Nk = m.N[k];
1125 Matrix<T> Off = m.L[k];
1126 for (std::size_t i = 0; i < Nk; ++i) Off(i, i) =
zero;
1127 for (std::size_t c = 0; c < m.actions.size(); ++c) {
1128 if (m.psv[c] == k) {
1129 for (std::size_t i = 0; i < Nk; ++i)
1130 for (std::size_t j = 0; j < Nk; ++j) Off(i, j) += T(x[c] * m.Pb[c](i, j));
1131 }
else if (m.act[c] == k) {
1132 for (std::size_t i = 0; i < Nk; ++i)
1133 for (std::size_t j = 0; j < Nk; ++j) Off(i, j) += m.Aa[c](i, j);
1136 for (std::size_t i = 0; i < Nk; ++i) Off(i, i) =
zero;
1139 const std::size_t mph = m.mph[k], nlev = m.nlev[k];
1140 bool solved_geom =
false;
1141 if (m.is_open_proc[k] && nlev >= 5) {
1145 const std::size_t s0 = Nk - 2;
1146 const double f = num_traits<T>::to_double(Off(s0, s0 + 1));
1147 const double b = num_traits<T>::to_double(Off(s0, s0 - 1));
1148 const double g0 = num_traits<T>::to_double(Off(s0, 0));
1152 double inter_down = 0.0;
1153 for (std::size_t j = 1; j + 3 < Nk; ++j)
1154 inter_down += num_traits<T>::to_double(Off(s0, j));
1155 if (inter_down <= 1e-11 && f > 0.0) {
1156 const double rho = qbd_scalar_rho(f, b, g0);
1157 if (std::isfinite(rho) && rho > 0.0 && rho < 1.0 - 1e-12) {
1160 pi[k].assign(Nk, zero);
1161 const T rt = num_traits<T>::from_double(rho);
1162 T acc = T(one - rt);
1163 for (std::size_t n = 0; n < Nk; ++n) {
1170 }
else if (is_block_tridiagonal(Q[k], m.level[k])) {
1174 const std::size_t s0 = nlev - 2;
1175 const Matrix<T> A0 = level_block(Q[k], s0, s0 + 1, mph);
1176 const Matrix<T> A1 = level_block(Q[k], s0, s0, mph);
1177 const Matrix<T> A2 = level_block(Q[k], s0, s0 - 1, mph);
1178 bool any_up =
false;
1179 for (std::size_t i = 0; i < mph && !any_up; ++i) {
1181 for (std::size_t j = 0; j < mph; ++j) rs += A0(i, j);
1182 if (rs > zero) any_up =
true;
1185 const QbdTail<T> g = qbd_matrix_tail(Q[k], A0, A1, A2, mph);
1189 pi[k] = qbd_tail_expand(g, nlev, mph);
1195 if (!solved_geom) pi[k] = solve_component(Q[k], mph, nlev, m.level[k]);
1201double block_norm(
const std::vector<std::vector<T>>& a,
const std::vector<std::vector<T>>& b) {
1203 for (std::size_t k = 0; k < a.size() && k < b.size(); ++k) {
1204 const std::size_t n = std::min(a[k].size(), b[k].size());
1206 for (std::size_t i = 0; i < n; ++i)
1207 s += std::fabs(num_traits<T>::to_double(T(a[k][i] - b[k][i])));
1250void closed_chain_condition(
const qn::NetworkStruct<T>& L,
const RcatModel<T>& m,
1251 std::vector<T>& x, std::vector<std::vector<T>>& pi,
1252 double tol,
int maxiter, std::vector<std::vector<double>>& ovr) {
1253 const std::size_t A = m.actions.size(), np = m.num_processes, K = L.nclasses;
1254 ovr.assign(np, std::vector<double>(3, std::numeric_limits<double>::quiet_NaN()));
1255 if (A == 0 || np == 0)
return;
1256 const T
zero = num_traits<T>::from_int(0);
1257 const std::vector<double> njobs = L.njobs();
1258 std::vector<std::size_t> pstat(np, 0), pclass(np, 0);
1259 for (std::size_t ist = 0; ist < m.process_map.size(); ++ist)
1260 for (std::size_t r = 0; r < m.process_map[ist].size(); ++r)
1261 if (m.process_map[ist][r] != 0) {
1262 pstat[m.process_map[ist][r] - 1] = ist;
1263 pclass[m.process_map[ist][r] - 1] = r;
1266 auto convolve_all = [](
const std::vector<std::vector<double>>& Mk, std::size_t skip,
1268 std::vector<double> G(Nc + 1, 0.0);
1270 for (std::size_t i = 0; i < Mk.size(); ++i) {
1271 if (i == skip)
continue;
1272 std::vector<double> H(Nc + 1, 0.0);
1273 for (std::size_t a = 0; a <= Nc; ++a) {
1274 if (G[a] == 0.0)
continue;
1275 for (std::size_t b = 0; a + b <= Nc; ++b) H[a + b] += G[a] * Mk[i][b];
1281 for (std::size_t c = 0; c < L.chains.size(); ++c) {
1282 const std::vector<bool>& ch = L.chains[c];
1284 for (std::size_t r = 0; r < K && r < ch.size(); ++r)
1285 if (ch[r]) popc += njobs[r];
1286 if (!std::isfinite(popc) || !(popc > 0.0))
continue;
1287 const std::size_t Nc =
static_cast<std::size_t
>(std::llround(popc));
1288 std::vector<long> pos(np, -1);
1289 std::vector<std::size_t> kc;
1290 for (std::size_t k = 0; k < np; ++k)
1291 if (pclass[k] < ch.size() && ch[pclass[k]]) {
1292 pos[k] =
static_cast<long>(kc.size());
1295 if (kc.empty())
continue;
1296 const std::size_t nk = kc.size();
1297 std::vector<bool> touch(A,
false);
1298 bool any =
false, eligible =
true;
1299 Matrix<double>
P(nk, nk, 0.0);
1300 for (std::size_t a = 0; a < A; ++a) {
1301 touch[a] = pos[m.act[a]] >= 0 || pos[m.psv[a]] >= 0;
1302 if (!touch[a])
continue;
1304 if (m.actions[a].is_negative || pos[m.act[a]] < 0 || pos[m.psv[a]] < 0)
1307 P(
static_cast<std::size_t
>(pos[m.act[a]]),
static_cast<std::size_t
>(pos[m.psv[a]])) +=
1311 for (std::size_t i = 0; i < nk; ++i) {
1312 const std::size_t k = kc[i];
1313 const std::size_t idx = pstat[k] * K + pclass[k];
1314 P(i, i) += num_traits<T>::to_double(L.rt(idx, idx));
1315 if (m.extsig[k]) eligible =
false;
1317 for (std::size_t i = 0; i < nk && eligible; ++i) {
1319 for (std::size_t j = 0; j < nk; ++j) rs +=
P(i, j);
1320 if (std::abs(rs - 1.0) > 1e-10) eligible =
false;
1326 for (std::size_t k : kc) {
1327 const auto& sta = L.stations[pstat[k]];
1330 "Closed chain " + std::to_string(c + 1) +
" visits an infinite-server "
1331 "station and interacts with signals or loses jobs outside its "
1332 "components, so it cannot be conditioned on its population; its "
1333 "single-server components would answer for a different model. Use "
1334 "SolverMVA, SolverNC or SolverCTMC");
1337 std::cerr <<
"[LINE] Warning: Closed chain " << (c + 1)
1338 <<
" interacts with signals or loses jobs outside its components; its "
1339 "RCAT marginals are not conditioned on the chain population, so "
1340 "population and flow balance are approximate.\n";
1345 std::vector<std::size_t> gstat;
1346 std::vector<std::string> gkind;
1347 std::vector<std::vector<std::size_t>> gmem;
1348 std::string blocked, approx;
1349 for (std::size_t ist = 0; ist < L.nstations; ++ist) {
1350 std::vector<std::size_t> mem;
1351 for (std::size_t i = 0; i < nk; ++i)
1352 if (pstat[kc[i]] == ist) mem.push_back(i);
1353 if (mem.empty())
continue;
1354 const auto& sta = L.stations[ist];
1355 for (std::size_t r = 0; r < K && approx.empty(); ++r) {
1356 const std::size_t p = ist < m.process_map.size() && r < m.process_map[ist].size()
1357 ? m.process_map[ist][r] : 0;
1358 if (p != 0 && !(r < ch.size() && ch[r]))
1359 approx =
"station " + sta.name +
" also serves class " + L.classes[r].name +
1360 " outside the chain, and RCAT does not model a server shared across "
1363 bool all_exp =
true;
1364 for (std::size_t i : mem)
1365 if (m.mph[kc[i]] != 1) all_exp =
false;
1371 }
else if (mem.size() == 1) {
1373 if (!all_exp && approx.empty())
1374 approx =
"class " + L.classes[pclass[kc[mem[0]]]].name +
" at station " +
1375 sta.name +
" is not exponential, so its population-conditioned "
1376 "marginal is not a product form";
1378 const double mu0 = num_traits<T>::to_double(L.rates(ist, pclass[kc[mem[0]]]));
1380 for (std::size_t i : mem)
1381 if (std::abs(num_traits<T>::to_double(L.rates(ist, pclass[kc[i]])) - mu0) >
1382 1e-12 * std::abs(mu0))
1384 if (all_exp && equal &&
1389 if (blocked.empty()) {
1391 for (std::size_t i : mem)
1392 names += (names.empty() ?
"" :
", ") + L.classes[pclass[kc[i]]].name;
1393 blocked =
"classes " + names +
" of the chain share the single server of "
1394 "station " + sta.name +
", which is not a product-form station "
1395 "(needs PS or LCFS-PR, or exponential service with equal rates "
1396 "under FCFS or SIRO)";
1400 gstat.push_back(ist);
1401 gkind.push_back(kind);
1402 gmem.push_back(mem);
1404 if (!blocked.empty()) {
1405 if (Nc > 1 && std::find(gkind.begin(), gkind.end(),
"inf") != gkind.end())
1407 "Closed chain " + std::to_string(c + 1) +
" visits an infinite-server station "
1408 "but cannot be conditioned on its population: " + blocked +
1409 ". Use SolverMVA, SolverNC or SolverCTMC");
1410 std::cerr <<
"[LINE] Warning: Closed chain " << (c + 1) <<
": " << blocked
1411 <<
". Its RCAT marginals are not conditioned on the chain population, so "
1412 "its results are approximate.\n";
1415 if (!approx.empty())
1416 std::cerr <<
"[LINE] Warning: Closed chain " << (c + 1) <<
": " << approx
1417 <<
". Population and flow balance are exact, the marginals are "
1419 const std::size_t nst = gstat.size();
1422 for (
double vi : v) vmax = std::max(vmax, vi);
1423 for (
double& vi : v) vi /= vmax;
1424 std::vector<double> demand(nk);
1425 for (std::size_t i = 0; i < nk; ++i)
1426 demand[i] = v[i] / num_traits<T>::to_double(L.rates(pstat[kc[i]], pclass[kc[i]]));
1427 std::vector<double> nfact(Nc + 1, 1.0);
1428 for (std::size_t n = 1; n <= Nc; ++n) nfact[n] = nfact[n - 1] *
static_cast<double>(n);
1431 double s = std::numeric_limits<double>::infinity();
1432 for (std::size_t i = 0; i < nk; ++i)
1433 s = std::min(s, num_traits<T>::to_double(L.rates(pstat[kc[i]], pclass[kc[i]])) / v[i]);
1434 std::vector<std::vector<double>> mass(nk, std::vector<double>(Nc + 1, 0.0));
1435 std::vector<std::vector<double>> Ms(nst, std::vector<double>(Nc + 1, 0.0));
1436 std::vector<std::vector<T>> pik(nk);
1437 std::vector<double> G;
1438 double gamma = std::numeric_limits<double>::quiet_NaN();
1439 bool converged =
false;
1440 for (
int it = 0; it < maxiter; ++it) {
1441 for (std::size_t a = 0; a < A; ++a)
1443 x[a] = num_traits<T>::from_double(
1444 s * v[
static_cast<std::size_t
>(pos[m.act[a]])] * m.actions[a].prob);
1445 for (std::size_t i = 0; i < nk; ++i) {
1446 const std::size_t k = kc[i];
1447 pik[i] = solve_component(assemble_generator(m, x, k), m.mph[k], m.nlev[k], m.level[k]);
1448 std::fill(mass[i].begin(), mass[i].end(), 0.0);
1449 for (std::size_t st = 0; st < m.N[k]; ++st)
1450 mass[i][m.level[k][st]] += num_traits<T>::to_double(pik[i][st]);
1452 for (std::size_t g = 0; g < nst; ++g) {
1453 if (gkind[g] ==
"single") {
1454 Ms[g] = mass[gmem[g][0]];
1457 for (std::size_t i : gmem[g]) rho += demand[i];
1459 const bool inf = gkind[g] ==
"inf";
1460 for (std::size_t n = 0; n <= Nc; ++n)
1461 Ms[g][n] = std::pow(rho,
static_cast<double>(n)) / (inf ? nfact[n] : 1.0);
1464 G = convolve_all(Ms, nst, Nc);
1465 gamma = G[Nc - 1] / G[Nc];
1466 if (std::abs(gamma - 1.0) < tol) {
1473 std::cerr <<
"[LINE] Warning: Closed chain " << (c + 1)
1474 <<
": the throughput scale did not converge in " << maxiter
1475 <<
" iterations (|G(N-1)/G(N) - 1| = " << std::abs(gamma - 1.0)
1476 <<
" > tol = " << tol
1477 <<
"); its conditioned marginals do not balance flow exactly.\n";
1478 for (std::size_t g = 0; g < nst; ++g) {
1479 const std::vector<double>
Gm = convolve_all(Ms, g, Nc);
1480 if (gkind[g] ==
"single") {
1481 const std::size_t i = gmem[g][0], k = kc[i];
1482 std::vector<T> cond(m.N[k], zero);
1483 for (std::size_t st = 0; st < m.N[k]; ++st)
1484 cond[st] = T(pik[i][st] *
1485 num_traits<T>::from_double(Gm[Nc - m.level[k][st]] / G[Nc]));
1489 std::vector<double> Pst(Nc + 1, 0.0);
1490 double qst = 0.0,
tot = 0.0;
1491 for (std::size_t n = 0; n <= Nc; ++n) {
1492 Pst[n] = Ms[g][n] *
Gm[Nc - n] / G[Nc];
1493 qst +=
static_cast<double>(n) * Pst[n];
1495 const double busy = 1.0 - Pst[0];
1496 for (std::size_t i : gmem[g])
tot += demand[i];
1497 for (std::size_t i : gmem[g]) {
1498 const std::size_t k = kc[i];
1499 const double p =
tot > 0.0 ? demand[i] /
tot : 0.0;
1501 std::vector<double> Pk(Nc + 1, 0.0);
1502 for (std::size_t j = 0; j <= Nc; ++j)
1503 for (std::size_t n = j; n <= Nc; ++n) {
1505 for (std::size_t t = 1; t <= j; ++t)
1506 b = b *
static_cast<double>(n - j + t) /
static_cast<double>(t);
1507 Pk[j] += Pst[n] * b * std::pow(p,
static_cast<double>(j)) *
1508 std::pow(1.0 - p,
static_cast<double>(n - j));
1510 std::vector<T> cond(m.N[k], zero);
1511 for (std::size_t st = 0; st < m.N[k]; ++st) {
1512 const std::size_t lv = m.level[k][st];
1513 if (mass[i][lv] > 0.0)
1514 cond[st] = T(pik[i][st] * num_traits<T>::from_double(Pk[lv] / mass[i][lv]));
1517 const double mu = num_traits<T>::to_double(L.rates(pstat[k], pclass[k]));
1518 const double qk = p * qst;
1519 const double uk = gkind[g] ==
"inf" ? qk : p * busy;
1522 ovr[k][2] = mu * uk;
1530mva::MvaSolution<T> rcat_metrics(
const qn::NetworkStruct<T>& L,
const RcatModel<T>& m,
1531 const std::vector<std::vector<T>>& pi,
1532 const std::vector<double>& rho_proc,
1533 const std::vector<bool>& is_geom,
1534 const std::vector<QbdTail<T>>& geom_data,
1535 const std::vector<std::vector<double>>& ovr = {}) {
1536 const std::size_t M = L.nstations, K = L.nclasses;
1537 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
1538 mva::MvaSolution<T> s;
1543 s.C.assign(K, zero);
1544 s.X.assign(K, zero);
1546 for (std::size_t ist = 0; ist < M; ++ist)
1547 for (std::size_t r = 0; r < K; ++r) {
1548 const std::size_t p = m.process_map.empty() ? 0 : m.process_map[ist][r];
1549 if (p == 0 || p > pi.size() || pi[p - 1].empty())
continue;
1551 if (p <= ovr.size() && !std::isnan(ovr[p - 1][0])) {
1552 s.Q(ist, r) = num_traits<T>::from_double(ovr[p - 1][0]);
1553 s.U(ist, r) = num_traits<T>::from_double(ovr[p - 1][1]);
1554 s.Tp(ist, r) = num_traits<T>::from_double(ovr[p - 1][2]);
1557 const bool disabled = L.disabled[ist][r];
1558 const std::size_t mph = m.mph[p - 1];
1559 if (!is_geom.empty() && p <= is_geom.size() && is_geom[p - 1]) {
1564 const T mu = disabled ?
zero : L.rates(ist, r);
1565 const T rho = num_traits<T>::from_double(rho_proc[p - 1]);
1566 s.Q(ist, r) = T(rho / (one - rho));
1568 if (mu > zero) s.Tp(ist, r) = T(mu * rho);
1571 const QbdTail<T>& g = geom_data[p - 1];
1573 for (std::size_t i = 0; i < mph; ++i) {
1575 tput += T(g.busy[i] * m.svcdown[p - 1][i]);
1577 s.Q(ist, r) = g.qlen;
1579 s.Tp(ist, r) = tput;
1582 const std::vector<T>& v = pi[p - 1];
1584 for (std::size_t n = 0; n < v.size(); ++n) {
1585 q += T(num_traits<T>::from_int(
static_cast<long>(m.level[p - 1][n])) * v[n]);
1589 tput += T(m.svcrate[p - 1][n] * v[n]);
1593 for (std::size_t i = 0; i < mph; ++i) level0 += v[i];
1594 s.U(ist, r) = T(one - level0);
1595 s.Tp(ist, r) = tput;
1599 for (std::size_t ist = 0; ist < M; ++ist)
1600 for (std::size_t r = 0; r < K; ++r)
1601 if (s.Tp(ist, r) > zero) s.R(ist, r) = T(s.Q(ist, r) / s.Tp(ist, r));
1613 for (std::size_t ist = 0; ist < M; ++ist) {
1614 if (L.stations[ist].nodetype != qn::NodeType::Source)
continue;
1615 for (std::size_t r = 0; r < K; ++r)
1616 s.Tp(ist, r) = L.disabled[ist][r] ?
zero : L.rates(ist, r);
1619 const std::vector<double> njobs = L.njobs();
1620 for (std::size_t r = 0; r < K; ++r) {
1621 if (!std::isfinite(njobs[r])) {
1622 for (std::size_t ist = 0; ist < M; ++ist) {
1624 if (L.stations[ist].nodetype == qn::NodeType::Source) {
1625 s.X[r] = L.disabled[ist][r] ?
zero : L.rates(ist, r);
1630 for (std::size_t ist = 0; ist < M; ++ist) c += s.R(ist, r);
1633 const std::size_t refst = L.classes[r].refstat;
1634 if (refst > 0 && refst <= M) {
1635 s.X[r] = s.Tp(refst - 1, r);
1637 s.C[r] = T(num_traits<T>::from_double(njobs[r]) / s.X[r]);
1707 using ag_detail::is_signal_class;
1709 const std::vector<double> njobs = L.
njobs();
1710 bool any_signal =
false;
1711 for (std::size_t r = 0; r < K; ++r)
1712 if (is_signal_class(L, r)) any_signal =
true;
1714 auto has_proc = [&](std::size_t ist, std::size_t r) {
1715 if (L.
stations[ist].nodetype == qn::NodeType::Source || is_signal_class(L, r))
return false;
1717 return std::isfinite(rate) && rate > 0.0;
1721 auto blocked_reason = [&](std::size_t c) -> std::string {
1722 const std::vector<bool>& ch = L.
chains[c];
1723 for (std::size_t ist = 0; ist < M; ++ist) {
1724 std::vector<std::size_t> cls;
1725 for (std::size_t r = 0; r < K; ++r)
1726 if (r < ch.size() && ch[r] && has_proc(ist, r)) cls.push_back(r);
1731 bool all_exp =
true, equal =
true;
1733 for (std::size_t r : cls) {
1734 if (L.
phases_of(ist, r) != 1) all_exp =
false;
1738 if (all_exp && equal &&
1742 for (std::size_t r : cls) names += (names.empty() ?
"" :
", ") + L.
classes[r].name;
1743 return "classes " + names +
" of the chain share the single server of station " +
1744 sta.name +
", which is not a product-form station (needs PS or LCFS-PR, or "
1745 "exponential service with equal rates under FCFS or SIRO)";
1747 return std::string();
1749 for (std::size_t ist = 0; ist < M; ++ist) {
1751 for (std::size_t r = 0; r < K; ++r) {
1753 if (L.
classes[r].self_looping || std::isnan(rate) || !(rate > 0.0))
continue;
1756 if (c == L.
chains.size())
continue;
1758 for (std::size_t s = 0; s < K && s < L.
chains[c].size(); ++s)
1759 if (L.
chains[c][s]) popc += njobs[s];
1760 const std::string where =
"the infinite-server station '" + L.
stations[ist].name +
"'";
1761 if (std::isinf(popc))
1762 return "SolverAG decomposes every station into a single-server component "
1763 "(build_rcat never reads sn.nservers), and only a closed chain is "
1764 "conditioned back onto the infinite server, so " + where +
1765 " cannot serve the open class '" + L.
classes[r].name +
1766 "'. Use SolverMVA, SolverNC or SolverCTMC";
1767 if (popc <= 1.0)
continue;
1768 std::ostringstream pop;
1770 const std::string head =
"SolverAG serves " + where +
" for the closed class '" +
1771 L.
classes[r].name +
"' (chain population " + pop.str() +
1772 ") only by conditioning its chain on the population, ";
1774 return head +
"and a signal class in the model can make that step skip the "
1775 "chain, leaving a single-server component in its place. Use "
1776 "SolverMVA, SolverNC or SolverCTMC";
1777 const std::string why = blocked_reason(c);
1779 return head +
"which it cannot do here: " + why +
1780 ". Use SolverMVA, SolverNC or SolverCTMC";
1783 return std::string();
1810 std::size_t max_states = 0) {
1811 if (max_states == 0) max_states =
opt.max_states;
1814 "solver_ag: the RCAT analyzers stop on a tolerance and the matrix-geometric "
1815 "variant takes a square root, so they need transcendental arithmetic; rerun this "
1816 "model with --arith double or --arith real");
1818 using namespace ag_detail;
1822 std::string method =
opt.method;
1823 if (method ==
"default") method =
"inap";
1824 if (method ==
"exact") {
1829 if (method !=
"inap" && method !=
"inaprc" && method !=
"inapinf")
1833 out.actualmethod = method;
1842 "solver_ag: the 'cluster' execution backend does not carry the 'inapinf' agent "
1843 "solve (the matrix-geometric tail of an open agent runs on the coordinator "
1844 "only); use exec 'serial' or 'parallel' with 'inapinf', or method 'inap'/"
1845 "'inaprc' with 'cluster'");
1852 const std::string hint = (
opt.exec ==
"threads")
1853 ?
"; 'threads' was renamed to 'parallel' (alias 'para')" :
"";
1854 throw InputError(
"solver_ag: unknown execution backend '" +
opt.exec +
1855 "'; use 'serial', 'parallel' (alias 'para') or 'cluster'" + hint);
1858 std::unique_ptr<AgWorkerPool> pool;
1863 const RcatModel<T> m = build_rcat(L, max_states);
1865 if (m.num_processes == 0) {
1872 out.sol.C.assign(K, zero);
1873 out.sol.X.assign(K, zero);
1874 out.sol.method =
opt.method;
1879 const std::size_t A = m.actions.size();
1880 std::vector<std::vector<T>> pi;
1881 std::vector<Matrix<T>> Q;
1891 pi.assign(m.num_processes, std::vector<T>());
1892 for (std::size_t k = 0; k < m.num_processes; ++k) {
1894 pi[k] = solve_component(Qk, m.mph[k], m.nlev[k], m.level[k]);
1896 out.sol = rcat_metrics(L, m, pi, std::vector<double>(), std::vector<bool>(),
1897 std::vector<QbdTail<T>>());
1898 out.sol.method =
opt.method;
1916 std::vector<bool> not_birth_death(m.num_processes,
false);
1917 for (std::size_t k = 0; k < m.num_processes; ++k) {
1918 if (m.mph[k] > 1) not_birth_death[k] =
true;
1919 for (std::size_t n = 0; n < m.N[k]; ++n)
1920 for (std::size_t j = 0; j < m.N[k]; ++j) {
1921 const std::size_t
ln = m.level[k][n], lj = m.level[k][j];
1922 const std::size_t d = (
ln > lj) ? (
ln - lj) : (lj -
ln);
1923 if (d > 1 && m.L[k](n, j) > zero) not_birth_death[k] =
true;
1929 std::vector<std::vector<std::size_t>> active_cols(A);
1930 for (std::size_t a = 0; a < A; ++a)
1931 for (std::size_t j = 0; j < m.Aa[a].cols(); ++j) {
1933 for (std::size_t i = 0; i < m.Aa[a].rows(); ++i) colsum += m.Aa[a](i, j);
1934 if (colsum > zero) active_cols[a].push_back(j);
1938 std::vector<T> x(A, zero);
1939 for (std::size_t a = 0; a < A; ++a)
1941 static_cast<double>(A + 1));
1943 std::vector<double> rho_proc;
1944 std::vector<bool> is_geom;
1945 std::vector<QbdTail<T>> geom_data;
1946 const bool qbd = (method ==
"inapinf");
1947 if (qbd) compute_equilibrium_qbd(m, x, pi, Q, rho_proc, is_geom, geom_data);
1948 else compute_equilibrium(m, x, pi, Q, &
opt, pool.get());
1950 std::vector<std::vector<T>> a_rowsum(A);
1951 for (std::size_t a = 0; a < A; ++a) {
1952 a_rowsum[a].assign(m.Aa[a].rows(), zero);
1953 for (std::size_t i = 0; i < m.Aa[a].rows(); ++i)
1954 for (std::size_t j = 0; j < m.Aa[a].cols(); ++j) a_rowsum[a][i] += m.Aa[a](i, j);
1961 const double tol =
opt.tol;
1962 std::size_t iter = 0;
1963 bool converged =
false;
1964 for (iter = 1; iter <= static_cast<std::size_t>(
opt.iter_max); ++iter) {
1965 const std::vector<std::vector<T>> pi_ref = pi;
1966 for (std::size_t a = 0; a < A; ++a) {
1967 const std::size_t k = m.act[a];
1970 if (m.mph[k] == 1) {
1973 const std::size_t idx = std::min<std::size_t>(1, m.N[k] - 1);
1980 for (std::size_t i = 0; i < m.mph[k]; ++i)
1981 acc += T(geom_data[k].busy[i] * a_rowsum[a][m.mph[k] + i]);
1986 for (std::size_t i = 0; i < pi[k].size() && i < a_rowsum[a].size(); ++i)
1987 acc += T(pi[k][i] * a_rowsum[a][i]);
1992 const std::vector<T> v =
vecmul(pi[k], m.Aa[a]);
1993 if (method ==
"inaprc" || not_birth_death[k]) {
1996 for (std::size_t j = 0; j < v.size(); ++j)
sum += v[j];
1997 if (
sum > zero) x[a] =
sum;
2005 std::size_t cnt = 0;
2006 for (std::size_t j : active_cols[a])
2007 if (pi[k][j] > zero && v[j] > zero) {
2008 sum += T(v[j] / pi[k][j]);
2015 if (qbd) compute_equilibrium_qbd(m, x, pi, Q, rho_proc, is_geom, geom_data);
2016 else compute_equilibrium(m, x, pi, Q, &
opt, pool.get());
2018 if (block_norm<T>(pi, pi_ref) < tol) {
2026 if (!converged) iter =
static_cast<std::size_t
>(
opt.iter_max) + 1;
2030 for (std::size_t a = 0; a < A; ++a) {
2031 const std::size_t k = m.act[a];
2033 for (std::size_t j = 0; j < m.N[k]; ++j) {
2034 T acc = T(x[a] * pi[k][j]);
2035 for (std::size_t i = 0; i < m.N[k]; ++i) acc -= T(pi[k][i] * m.Aa[a](i, j));
2039 res = std::max(res, std::sqrt(s));
2041 out.rcat_residual = res;
2044 std::vector<std::vector<double>> ovr;
2045 closed_chain_condition(L, m, x, pi, tol,
opt.iter_max, ovr);
2046 out.sol = rcat_metrics(L, m, pi, rho_proc, is_geom, geom_data, ovr);
2049 out.sol.method = (
opt.method ==
"exact") ? method :
opt.method;
2050 out.sol.iter =
static_cast<int>(iter);
Execution backends of the reversed-rate fixed point.
Options of the agent-based (RCAT) solver.
UnsupportedError(const std::string &what)
AgWorkerPool(const std::vector< std::string > &endpoints, double timeout_seconds)
A network plus its refreshed NetworkStruct.
std::size_t phases_of(std::size_t ist, std::size_t r) const
sn.phases(i,r): the order of the process representation.
std::vector< std::vector< bool > > chains
(nchains x nclasses)
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< double > njobs() const
sn.njobs: the population of each class, infinite for an open one.
Steady-state distribution of a continuous-time Markov chain.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
Equilibrium distribution of a discrete-time Markov chain, and stochastic complementation.
The exception types the port throws.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
LU factorization with partial pivoting, templated on the number type.
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.
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
void ag_sweep_cluster(std::size_t n, AgWorkerPool &workers, const std::vector< T > &x, Gen gen, Sol sol, Payload payload, std::vector< Matrix< T > > &Q, std::vector< std::vector< T > > &pi)
The same sweep with the agents partitioned over remote ag-worker processes.
std::vector< std::array< double, 3 > > ag_triplets(const Matrix< double > &m)
Non-zero entries of M as [row, col, value] triplets, 0-based.
void ag_sweep_serial(std::size_t n, Gen gen, Sol sol, std::vector< Matrix< T > > &Q, std::vector< std::vector< T > > &pi)
Evaluate every agent of one sweep.
Matrix< T > krons(const Matrix< T > &A, const Matrix< T > &B)
Kronecker sum, MATLAB's krons: kron(A, I_nb) + kron(I_na, B).
std::string inf_supports_reason(const qn::NetworkStruct< T > &L)
Why the RCAT decomposition cannot serve an infinite-server station of this model, or "" when it can (...
Matrix< T > kron(const Matrix< T > &A, const Matrix< T > &B)
Kronecker product.
const char * exec_cluster()
Partition the agents over remote ag-worker processes.
const char * exec_serial()
The caller's own loop, in agent order.
Matrix< T > qbd_R_logred(const Matrix< T > &B, const Matrix< T > &L, const Matrix< T > &F, unsigned iter_max, const T &tol)
R by logarithmic reduction (qbd_R_logred.m).
AgResult< T > solver_ag(const qn::NetworkStruct< T > &L, const AgOptions &opt, std::size_t max_states=0)
Port of solver_ag.m.
void ag_sweep_parallel(std::size_t n, unsigned nworkers, Gen gen, Sol sol, std::vector< Matrix< T > > &Q, std::vector< std::vector< T > > &pi)
The same sweep over a thread pool.
bool exec_is_parallel(const std::string &mode)
True for either spelling of the local-thread-pool backend.
bool rcat_supports_process(lang::ProcessType t)
The process types the RCAT construction can give a phase dimension to.
mam::Map< T > dist_to_map(const Distrib< T > &d)
The (D0,D1) pair that reaches sn.proc.
SignalType
G-network signal classes, with the values of MATLAB SignalType.
@ NEGATIVE
removes a batch of jobs (Gelenbe's negative customer)
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
@ LOGLOGISTIC
Three reliability laws of Trivedi and Bobbio, "Reliability andAvailability Engineering" (2017),...
NodeType
Node kinds, with the values of MATLAB NodeType.
double tot(const std::vector< double > &pops)
Matrix< T > krons(const Matrix< T > &A, const Matrix< T > &B)
Kronecker sum, MATLAB's krons: kron(A, I_nb) + kron(I_na, B).
Matrix< T > kron(const Matrix< T > &A, const Matrix< T > &B)
Kronecker product.
Matrix< T > qbd_R_logred(const Matrix< T > &B, const Matrix< T > &L, const Matrix< T > &F, unsigned iter_max, const T &tol)
R by logarithmic reduction (qbd_R_logred.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.
Matrix< T > ctmc_makeinfgen(const Matrix< T > &Q)
Set the diagonal so that every row sums to zero (ctmc_makeinfgen).
std::vector< T > dtmc_solve(const Matrix< T > &P)
Stationary distribution of a stochastic matrix P.
@ Gm
the reference's alias of 'cub'
std::vector< std::size_t > lu(Mat &A)
LU with partial pivoting in place; returns the row permutation.
Conservation laws of a layered queueing network, enumerated from its structure.
void lu_solve(const Matrix< T > &LU, const std::vector< std::size_t > &piv, std::vector< T > &b)
Solve LUx = Pb in place on b, using the factors from lu_factor.
std::vector< T > vecmul(const std::vector< T > &v, const Matrix< T > &A)
Row vector times matrix, v A.
std::vector< std::size_t > lu_factor(Matrix< T > &A)
In-place LU of A (n x n).
Matrix< T > inverse(const Matrix< T > &A)
Inverse by LU with one factorization and n back substitutions.
Matrix< T > matmul(const Matrix< T > &A, const Matrix< T > &B)
Matrix product A B.
std::vector< T > mulvec(const Matrix< T > &A, const std::vector< T > &v)
Matrix times column vector, A v.
std::vector< T > ones(std::size_t n)
Column vector of ones, the ubiquitous e in MAP algebra.
Matrix< T > eye(std::size_t n)
Identity of order n.
A queueing network and its refreshed NetworkStruct.
Number-type abstraction for the templated API port.
Quasi-birth-death processes: the rate matrix R, the fundamental matrix G, the caudal characteristic,...
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
What the RCAT analyzer returns beyond the metrics.
double rcat_residual
The RCAT product-form residual of Remark 2, max_l ||pi (x_l I - T_l)||_2, computed by 'inapinf' only.
mva::MvaSolution< T > sol
Class-level results, the [Q,U,R,T,C,X] of the MATLAB analyzers.