447inline std::pair<std::optional<Certificate>, std::string>
certify(
const Model& snapshot,
448 const std::vector<double>& estimated_rates,
450 typedef std::pair<std::optional<Certificate>, std::string> Out;
451 const std::vector<AgentRecord>& agents = snapshot.
agents;
452 bool large = !snapshot.
min_syncs.empty() || snapshot.
syncs.size() > 12;
454 if (a.states.size() > 12) large =
true;
455 if (large)
return Out(std::nullopt,
"Certificate supports at most 12 actions and 12 states per finite agent.");
456 std::set<Endpoint> active_synced;
458 std::vector<detail::RMatrix> local;
459 bool autonomous =
true;
461 const std::size_t n = agent.states.size();
462 detail::RMatrix l(n, std::vector<Rational>(n,
Rational(0)));
465 l[agent.state_index(t.
source)][agent.state_index(t.
target)] += detail::exact_rational(t.
value);
467 for (std::size_t i = 0; i < n; ++i)
468 for (std::size_t j = 0; j < n; ++j) g(i, j) = (i != j && l[i][j] > 0) ? 1.0 : 0.0;
469 if (!detail::irreducible(g)) autonomous =
false;
474 detail::RMatrix active;
475 std::vector<std::pair<std::size_t, detail::RMatrix>> partners;
477 std::vector<RAction> actions;
481 auto build = [&](std::size_t k,
const std::string& label,
Role role) -> detail::RMatrix {
483 const std::size_t n = a.
states.size();
484 detail::RMatrix b(n, std::vector<Rational>(n,
Rational(0)));
492 const std::size_t k = snapshot.
index_of(p.first);
494 for (
auto& row : b) {
496 for (
const Rational& v : row) total += v;
497 if (total <= 0)
return Out(std::nullopt,
"A passive action is disabled.");
500 act.partners.push_back({k, b});
502 actions.push_back(std::move(act));
504 std::vector<std::vector<Rational>> candidates;
505 for (
double value : estimated_rates) {
506 if (!std::isfinite(value) || value <= 0)
return Out(std::nullopt,
"A positive finite reversed rate is required.");
507 std::vector<Rational> values;
508 const Rational exact = detail::exact_rational(value);
509 for (
long den : {1L, 10L, 100L, 1000L, 10000L, 100000L, 1000000L}) {
511 if (x > 0 && std::abs(detail::to_double(x) - value) <= std::max(10 * tol, 1e-12) &&
512 std::find(values.begin(), values.end(), x) == values.end())
515 if (values.empty())
return Out(std::nullopt,
"No bounded rational rate candidate matches the fixed point.");
516 candidates.push_back(values);
519 std::vector<std::size_t> idx(candidates.size(), 0);
520 bool exhausted =
false;
521 for (std::size_t attempt = 0; !exhausted; ++attempt) {
522 if (attempt >= 128)
return Out(std::nullopt,
"Rational-rate candidate search exceeds its limit.");
523 std::vector<Rational> rates;
524 for (std::size_t c = 0; c < candidates.size(); ++c) rates.push_back(candidates[c][idx[c]]);
527 std::size_t d = idx.size();
531 if (++idx[d] < candidates[d].size()) {
538 std::vector<std::vector<Rational>> marginals;
539 for (std::size_t k = 0; k < agents.size(); ++k) {
540 const std::size_t n = agents[k].states.size();
541 detail::RMatrix off = local[k];
542 for (std::size_t r = 0; r < actions.size(); ++r) {
543 if (actions[r].ai == k)
544 for (std::size_t i = 0; i < n; ++i)
545 for (std::size_t j = 0; j < n; ++j) off[i][j] += actions[r].active[i][j];
546 for (
const auto& pp : actions[r].partners)
548 for (std::size_t i = 0; i < n; ++i)
549 for (std::size_t j = 0; j < n; ++j) off[i][j] += rates[r] * pp.second[i][j];
551 for (std::size_t i = 0; i < n; ++i) {
554 for (std::size_t j = 0; j < n; ++j) s += off[i][j];
557 const std::optional<std::vector<Rational>> pi = detail::exact_stationary(off);
559 marginals.push_back(*pi);
561 if (marginals.size() != agents.size())
continue;
562 bool balanced =
true;
563 for (std::size_t r = 0; r < actions.size() && balanced; ++r) {
564 const detail::RMatrix& act = actions[r].active;
565 const std::vector<Rational>& m = marginals[actions[r].ai];
566 for (std::size_t j = 0; j < act.size() && balanced; ++j) {
568 for (std::size_t i = 0; i < act.size(); ++i) s += m[i] * act[i][j];
569 if (s != rates[r] * m[j]) balanced =
false;
572 if (!balanced)
continue;
577 for (
const RAction& act : actions) {
578 int noninvariant = 0;
579 for (
const auto& pp : act.partners) {
580 const std::vector<Rational>& m = marginals[pp.first];
581 const detail::RMatrix& P = pp.second;
583 for (std::size_t j = 0; j < P.size() && !moved; ++j) {
585 for (std::size_t i = 0; i < P.size(); ++i) s += m[i] * P[i][j];
586 if (s != m[j]) moved =
true;
588 if (moved) ++noninvariant;
590 if (noninvariant > 1) {
595 if (!valid)
continue;
598 if (!connected.
ok)
return Out(std::nullopt, *connected.
reason);
604 c.marginals = marginals;
605 c.irreducibility = autonomous ?
"autonomous" :
"full_reachability";
606 return Out(c, std::string());
608 return Out(std::nullopt,
"No candidate satisfies the exact reversed-rate and multi-party balance conditions.");
616 const std::string& method =
"inaprc") {
618 if (method !=
"inap" && method !=
"inaprc")
throw value_error(
"finite LAL RCAT method must be inap or inaprc");
623 std::map<std::string, std::string> renames;
624 for (
const auto& t : low.
transformations) renames[detail::join_label(t[
"active"])] = detail::join_label(t[
"event_label"]);
625 auto rename = [&](
const std::string& l) {
626 auto it = renames.find(l);
627 return it == renames.end() ? l : it->second;
629 detail::check_iteration(tol, max_iter,
"RCAT");
630 const std::vector<AgentRecord>& agents = snapshot.
agents;
631 const std::size_t K = agents.size();
632 std::set<Endpoint> active_synced;
634 std::vector<Matrix<double>> local;
636 const std::size_t n = agent.states.size();
645 std::vector<std::pair<std::size_t, Matrix<double>>> partners;
649 std::vector<Act> actions;
655 const std::size_t pi = snapshot.
index_of(p.first);
657 const std::vector<double> totals = detail::normalise_rows(m);
658 for (
double t : totals)
661 a.partners.push_back({pi, m});
664 actions.push_back(std::move(a));
666 auto stationary = [&](
const std::vector<double>& rates) {
667 std::vector<std::vector<double>> results;
668 for (std::size_t k = 0; k < K; ++k) {
670 for (std::size_t r = 0; r < actions.size(); ++r) {
671 const Act& a = actions[r];
673 for (std::size_t i = 0; i < off.
size(); ++i) off[i] += a.active[i];
674 for (
const auto& pp : a.partners)
676 for (std::size_t i = 0; i < off.
size(); ++i) off[i] += rates[r] * pp.second[i];
678 results.push_back(detail::local_stationary(
679 off,
"SolverAG LAL " + method +
" requires an irreducible isolated agent; " + agents[k].name +
684 std::vector<double> rates(actions.size(), 1.0);
685 bool converged = actions.empty();
688 std::vector<std::vector<double>> marginals = stationary(rates);
689 if (!actions.empty()) {
690 for (iterations = 1; iterations <= max_iter; ++iterations) {
691 std::vector<double> new_rates;
692 for (
const Act& a : actions) {
693 const std::vector<double>& m = marginals[a.ai];
694 const std::vector<double> incoming = detail::rowvec_mul(m, a.active);
695 double new_rate = 0.0;
696 if (method ==
"inaprc") {
697 for (
double x : incoming) new_rate += x;
701 for (std::size_t j = 0; j < incoming.size(); ++j) {
703 for (std::size_t i = 0; i < a.active.rows(); ++i) col += a.active(i, j);
704 if (col > 0 && m[j] > 0 && incoming[j] > 0) {
705 sum += incoming[j] / m[j];
709 new_rate = cnt ?
sum /
static_cast<double>(cnt) : 0.0;
711 new_rates.push_back(new_rate);
713 const std::vector<std::vector<double>> next = stationary(new_rates);
714 double dr = 0.0, dm = 0.0;
715 for (std::size_t r = 0; r < rates.size(); ++r) dr = std::max(dr, std::abs(new_rates[r] - rates[r]));
716 for (std::size_t k = 0; k < K; ++k) dm = std::max(dm, detail::l1_diff(next[k], marginals[k]));
717 diff = std::max(dr, dm);
725 if (iterations > max_iter) iterations = max_iter;
727 double residual = 0.0;
728 for (std::size_t r = 0; r < actions.size(); ++r) {
729 const std::vector<double>& m = marginals[actions[r].ai];
730 const std::vector<double> in = detail::rowvec_mul(m, actions[r].active);
732 for (std::size_t j = 0; j < in.size(); ++j) s += std::abs(in[j] - rates[r] * m[j]);
733 residual = std::max(residual, s);
735 nlohmann::json action_diag = nlohmann::json::array();
736 bool pairwise =
true;
737 for (std::size_t r = 0; r < actions.size(); ++r) {
738 const Act& a = actions[r];
739 if (a.partners.size() != 1) pairwise =
false;
740 const std::vector<double>& m = marginals[a.ai];
741 const std::vector<double> in = detail::rowvec_mul(m, a.active);
742 std::vector<double> rev(in.size());
744 for (std::size_t j = 0; j < in.size(); ++j) {
745 rev[j] = m[j] > 0 ? in[j] / m[j] : std::numeric_limits<double>::quiet_NaN();
746 if (!std::isfinite(rev[j])) finite =
false;
748 double rres = detail::kInf;
751 for (
double x : rev) rres = std::max(rres, std::abs(x - rates[r]));
753 nlohmann::json passive_agents = nlohmann::json::array();
754 for (
const auto& pp : a.partners) passive_agents.push_back(agents[pp.first].name);
755 action_diag.push_back({{
"action", a.label},
756 {
"active_agent", agents[a.ai].name},
757 {
"passive_agent", a.partners.size() == 1 ? nlohmann::json(agents[a.partners[0].first].name)
758 : nlohmann::json(
nullptr)},
759 {
"passive_agents", passive_agents},
760 {
"effective_rate", rates[r]},
761 {
"reversed_rates_by_state", detail::vec_json(rev)},
762 {
"reversed_rate_residual", rres}});
764 nlohmann::json autonomous = nlohmann::json::object();
765 for (std::size_t k = 0; k < K; ++k) autonomous[agents[k].name] = detail::irreducible(local[k]);
770 res.
iterations =
static_cast<int>(iterations);
776 {{
"pairwise", pairwise}, {
"passive_always_enabled",
true}, {
"isolated_agents_irreducible",
true}}},
778 {
"autonomous_irreducible", autonomous},
779 {
"actions", action_diag},
780 {
"fixed_point_change", diff},
781 {
"product_form_certified",
false},
782 {
"certificate_reason",
783 "Numerical residuals alone do not establish all product-form conditions or global uniqueness."}};
784 for (std::size_t k = 0; k < K; ++k)
785 for (std::size_t j = 0; j < agents[k].states.size(); ++j)
786 res.
state_table.
rows.push_back({agents[k].name, agents[k].states[j], marginals[k][j]});
787 std::map<std::string, double> by_label;
788 for (std::size_t k = 0; k < K; ++k)
789 for (
const std::string& label : detail::active_actions(agents[k])) {
791 for (
const Transition& t : agents[k].transitions)
793 rate += marginals[k][agents[k].state_index(t.source)] * t.value;
794 by_label[rename(agents[k].name +
"." + label)] = rate;
797 for (
const std::string& label : original_kernel.
labels) res.
action_table.
rows.push_back({label, by_label.at(label)});
800 res.
state_table.
attrs.
extra = {{
"converged", converged}, {
"rcat_residual", residual}, {
"product_form_certified",
false}};
903 detail::check_iteration(tol, max_iter,
"Mean-field");
904 const std::vector<AgentRecord>& agents = snapshot.
agents;
905 const std::size_t K = agents.size();
907 const std::vector<std::string>& labels = kernel.
labels;
908 std::map<Endpoint, std::vector<Endpoint>> ordinary;
910 std::set<Endpoint> min_members;
915 std::vector<double> totals;
921 b.probability = detail::block_of(agents[b.i], ep.second, role);
922 b.totals = detail::normalise_rows(b.probability);
925 std::map<std::string, std::pair<std::vector<Blk>, std::vector<Blk>>> events;
927 for (
const std::string& action : detail::active_actions(agent)) {
928 const Endpoint ep(agent.name, action);
929 if (min_members.count(ep))
continue;
930 std::vector<Blk> passive;
931 auto it = ordinary.find(ep);
932 if (it != ordinary.end())
937 std::vector<Blk> a, p;
942 auto local_solve = [&](
const std::vector<std::vector<double>>& marginals, std::map<std::string, double>& throughputs) {
943 std::vector<Matrix<double>> blocks;
945 for (
const std::string& label : labels) {
946 const auto& ev = events.at(label);
947 std::vector<detail::RateLaw> distributions;
948 for (
const Blk& b : ev.first) distributions.push_back({b.totals, marginals[b.i]});
949 const double active_rate = detail::expected_min(distributions);
950 std::vector<double> enabled;
951 for (
const Blk& b : ev.second) {
953 for (std::size_t j = 0; j < b.totals.size(); ++j)
954 if (b.totals[j] > 0) s += marginals[b.i][j];
955 enabled.push_back(s);
957 double prod_enabled = 1.0;
958 for (
double e : enabled) prod_enabled *= e;
959 throughputs[label] = active_rate * prod_enabled;
960 for (std::size_t ai = 0; ai < ev.first.size(); ++ai) {
961 const Blk& b = ev.first[ai];
962 std::vector<detail::RateLaw> others;
963 for (std::size_t o = 0; o < distributions.size(); ++o)
964 if (o != ai) others.push_back(distributions[o]);
965 for (std::size_t r = 0; r < b.totals.size(); ++r) {
966 const double f = detail::expected_min(others, b.totals[r]) * prod_enabled;
967 for (std::size_t c = 0; c < b.totals.size(); ++c) blocks[b.i](r, c) += f * b.probability(r, c);
970 for (std::size_t pidx = 0; pidx < ev.second.size(); ++pidx) {
971 const Blk& b = ev.second[pidx];
972 double other_enabled = 1.0;
973 for (std::size_t o = 0; o < enabled.size(); ++o)
974 if (o != pidx) other_enabled *= enabled[o];
975 for (std::size_t r = 0; r < b.totals.size(); ++r) {
976 const double f = (b.totals[r] > 0 ? 1.0 : 0.0) * active_rate * other_enabled;
977 for (std::size_t c = 0; c < b.totals.size(); ++c) blocks[b.i](r, c) += f * b.probability(r, c);
981 std::vector<std::vector<double>> updated;
982 for (std::size_t i = 0; i < K; ++i)
983 updated.push_back(detail::local_stationary(
984 blocks[i],
"Mean-field local chain for " + agents[i].name +
985 " is reducible; the stationary approximation is not uniquely determined."));
988 std::vector<std::vector<double>> marginals;
990 marginals.push_back(std::vector<double>(a.states.size(), 1.0 /
static_cast<double>(a.states.size())));
991 bool converged =
false;
992 double residual = detail::kInf;
994 std::map<std::string, double> throughputs;
995 for (iteration = 1; iteration <= max_iter; ++iteration) {
996 const std::vector<std::vector<double>> updated = local_solve(marginals, throughputs);
998 for (std::size_t i = 0; i < K; ++i) residual = std::max(residual, detail::l1_diff(updated[i], marginals[i]));
999 if (residual <= tol) {
1003 for (std::size_t i = 0; i < K; ++i)
1004 for (std::size_t j = 0; j < marginals[i].size(); ++j)
1005 marginals[i][j] = (1 - damping) * marginals[i][j] + damping * updated[i][j];
1007 if (iteration > max_iter) iteration = max_iter;
1008 throughputs.clear();
1009 local_solve(marginals, throughputs);
1011 res.
method =
"meanfield";
1014 res.
iterations =
static_cast<int>(iteration);
1016 for (std::size_t i = 0; i < K; ++i)
1017 for (std::size_t j = 0; j < agents[i].states.size(); ++j)
1018 res.
state_table.
rows.push_back({agents[i].name, agents[i].states[j], marginals[i][j]});
1019 for (
const std::string& label : labels) res.
action_table.
rows.push_back({label, throughputs.at(label)});
1023 {
"converged", converged}, {
"fixed_point_residual", residual}, {
"product_form_certified",
false}};
1025 res.
diagnostics = {{
"product_form_certified",
false},
1026 {
"fixed_point_residual", residual},
1027 {
"approximation",
"independent local marginals"},
1028 {
"damping", damping},
1029 {
"certificate_reason",
"Mean-field independence is an approximation, not an RCAT proof."}};
1063 long max_states = 256,
double tol = 1e-4,
long max_iter = 100,
double damping = .5,
1064 bool source_truncated =
false) {
1066 detail::check_iteration(tol, max_iter,
"Grouped");
1067 const std::vector<std::vector<std::string>> partition =
group_partition(snapshot, groups, max_states);
1068 std::vector<std::string> names;
1070 nlohmann::json partition_json = partition;
1071 if (partition.size() == 1) {
1075 const bool exact = !source_truncated;
1084 {
"group_states", nlohmann::json::array({res.
states.size()})},
1086 {
"approximation",
nullptr},
1087 {
"product_form_certified",
false},
1088 {
"certificate_reason",
"One group was solved by explicit finite CTMC."}};
1090 a->method =
"grouped";
1092 a->extra[
"converged"] =
true;
1093 a->extra[
"groups"] = partition_json;
1094 a->extra[
"product_form_certified"] =
false;
1098 const std::vector<AgentRecord>& agents = snapshot.
agents;
1100 const std::vector<std::string>& labels = kernel.
labels;
1101 std::map<Endpoint, std::vector<Endpoint>> ordinary;
1103 std::set<Endpoint> min_members;
1108 std::vector<Endpoint> active, passive;
1110 std::vector<Decl> declarations;
1112 for (
const std::string& action : detail::active_actions(agent)) {
1113 const Endpoint ep(agent.name, action);
1114 if (min_members.count(ep))
continue;
1116 auto it = ordinary.find(ep);
1117 if (it != ordinary.end()) d.passive = it->second;
1118 declarations.push_back(d);
1123 struct Participant {
1129 typedef std::vector<std::size_t> GState;
1131 auto options = [&](std::size_t a,
const std::string& action,
Role role, std::size_t state) {
1134 std::vector<std::pair<std::size_t, double>> raw;
1141 for (
auto& r : raw) r.second /= total;
1144 return std::make_pair(total, raw);
1146 std::vector<std::map<std::string, std::pair<std::vector<double>,
Matrix<double>>>> group_events;
1147 std::vector<std::vector<GState>> group_states;
1148 double dense_bytes = 0.0;
1149 for (
const auto& group : partition) {
1150 std::map<std::string, std::size_t> positions;
1151 for (std::size_t i = 0; i < group.size(); ++i) positions[group[i]] = i;
1152 std::vector<std::pair<std::string, std::vector<Participant>>> local_events;
1153 for (
const Decl& d : declarations) {
1154 std::vector<Participant> ps;
1156 if (positions.count(e.first)) ps.push_back({snapshot.
index_of(e.first), positions[e.first], e.second,
Role::Active});
1157 for (
const Endpoint& e : d.passive)
1158 if (positions.count(e.first))
1160 if (!ps.empty()) local_events.push_back({d.label, ps});
1162 std::vector<std::vector<std::size_t>> initial_choices;
1163 double support = 1.0;
1164 for (
const std::string& name : group) {
1166 std::vector<std::size_t> c;
1170 for (std::size_t s = 0; s < agent.
states.size(); ++s)
1172 support *=
static_cast<double>(c.size());
1173 initial_choices.push_back(c);
1175 if (support >
static_cast<double>(max_states))
1177 std::vector<GState> states;
1179 std::vector<std::size_t> idx(initial_choices.size(), 0);
1182 for (std::size_t i = 0; i < idx.size(); ++i) s.push_back(initial_choices[i][idx[i]]);
1183 states.push_back(s);
1184 std::size_t d = idx.size();
1188 if (++idx[d] < initial_choices[d].size()) {
1197 std::map<GState, std::size_t> lookup;
1198 for (std::size_t i = 0; i < states.size(); ++i) lookup[states[i]] = i;
1199 auto transitions = [&](
const GState& state,
const std::vector<Participant>& participants) {
1200 std::vector<double> caps;
1201 std::vector<std::pair<std::size_t, std::vector<std::pair<std::size_t, double>>>> branches;
1202 std::vector<std::pair<GState, double>> choices;
1203 for (
const Participant& p : participants) {
1204 const auto o = options(p.agent, p.action, p.role, state[p.pos]);
1205 if (o.first <= 0)
return std::make_pair(0.0, std::vector<std::pair<GState, double>>());
1207 branches.push_back({p.pos, o.second});
1209 const double cap = caps.empty() ? detail::kInf : *std::min_element(caps.begin(), caps.end());
1210 double combos = 1.0;
1211 for (
const auto& b : branches) combos *=
static_cast<double>(b.second.size());
1212 if (combos > 1000000)
1214 "Grouped event has more than 1000000 local branch combinations.");
1215 std::vector<std::size_t> idx(branches.size(), 0);
1217 GState target = state;
1218 double probability = 1.0;
1219 for (std::size_t b = 0; b < branches.size(); ++b) {
1220 target[branches[b].first] = branches[b].second[idx[b]].first;
1221 probability *= branches[b].second[idx[b]].second;
1223 choices.push_back({target, probability});
1224 std::size_t d = idx.size();
1228 if (++idx[d] < branches[d].second.size()) {
1236 return std::make_pair(cap, choices);
1241 for (std::size_t s = 0; s < states.size(); ++s)
1242 for (
const auto& le : local_events) {
1243 const GState current = states[s];
1244 const auto tr = transitions(current, le.second);
1245 for (
const auto& ch :
tr.second)
1246 if (ch.second > 0 && !lookup.count(ch.first)) {
1247 if (states.size() >=
static_cast<std::size_t
>(max_states))
1249 lookup[ch.first] = states.size();
1250 states.push_back(ch.first);
1253 const double n =
static_cast<double>(states.size());
1254 dense_bytes +=
static_cast<double>(local_events.size() + 1) * n * n * 8.0;
1255 if (dense_bytes > 256.0 * 1024 * 1024)
1257 std::map<std::string, std::pair<std::vector<double>,
Matrix<double>>> compiled;
1258 for (
const auto& le : local_events) {
1259 std::vector<double> caps(states.size(), 0.0);
1261 for (std::size_t i = 0; i < states.size(); ++i) {
1262 const auto tr = transitions(states[i], le.second);
1264 for (
const auto& ch :
tr.second) k(i, lookup.at(ch.first)) += ch.second;
1266 compiled[le.first] = {caps, k};
1268 group_events.push_back(compiled);
1269 group_states.push_back(states);
1272 auto expectation = [](
const std::vector<detail::RateLaw>& distributions, std::optional<double> cutoff) ->
double {
1275 if (cutoff && *cutoff <= 0)
return 0.0;
1276 std::set<double> points;
1277 for (
const auto& d : distributions)
1278 for (
double x : d.first)
1279 if (x > 0 && x < detail::kInf) points.insert(x);
1280 if (cutoff && std::isfinite(*cutoff)) {
1281 std::set<double> kept;
1282 for (
double x : points)
1283 if (x < *cutoff) kept.insert(x);
1284 kept.insert(*cutoff);
1287 if (points.empty()) {
1291 for (
const auto& d : distributions)
1292 for (
double x : d.first)
1293 if (std::isfinite(x))
return 0.0;
1296 double result = 0.0, last = 0.0;
1297 for (
double point : points) {
1299 for (
const auto& d : distributions) {
1301 for (std::size_t i = 0; i < d.first.size(); ++i)
1302 if (d.first[i] >= point) s += d.second[i];
1305 result += (point - last) * prob;
1310 const std::size_t G = partition.size();
1311 auto local_solve = [&](
const std::vector<std::vector<double>>& marginals, std::map<std::string, double>& throughputs) {
1312 std::vector<Matrix<double>> blocks;
1313 for (
const auto& s : group_states) blocks.push_back(
Matrix<double>(s.size(), s.size(), 0.0));
1314 for (
const std::string& label : labels) {
1315 std::vector<std::size_t> part;
1316 std::vector<detail::RateLaw> distributions;
1317 for (std::size_t i = 0; i < G; ++i) {
1318 auto it = group_events[i].find(label);
1319 if (it == group_events[i].end())
continue;
1321 distributions.push_back({it->second.first, marginals[i]});
1323 throughputs[label] = expectation(distributions, std::nullopt);
1324 for (std::size_t off = 0; off < part.size(); ++off) {
1325 const std::size_t i = part[off];
1326 const auto& ev = group_events[i].at(label);
1327 std::vector<detail::RateLaw> other;
1328 for (std::size_t o = 0; o < distributions.size(); ++o)
1329 if (o != off) other.push_back(distributions[o]);
1330 for (std::size_t r = 0; r < ev.first.size(); ++r) {
1331 const double cap = ev.first[r];
1332 const double f = std::isfinite(cap) ? expectation(other, cap) : expectation(other, std::nullopt);
1333 for (std::size_t c = 0; c < ev.first.size(); ++c) blocks[i](r, c) += f * ev.second(r, c);
1337 std::vector<std::vector<double>> updated;
1338 for (std::size_t i = 0; i < G; ++i) {
1339 std::string tuple =
"(";
1340 for (std::size_t k = 0; k < partition[i].size(); ++k)
1341 tuple += (k ?
", '" :
"'") + partition[i][k] +
"'";
1342 tuple += partition[i].size() == 1 ?
",)" :
")";
1343 updated.push_back(detail::local_stationary(
1344 blocks[i],
"Grouped local chain " + tuple +
" is reducible; its stationary approximation is not unique."));
1348 std::vector<std::vector<double>> marginals;
1349 for (
const auto& s : group_states) marginals.push_back(std::vector<double>(s.size(), 1.0 /
static_cast<double>(s.size())));
1350 bool converged =
false;
1351 double residual = detail::kInf;
1353 std::map<std::string, double> throughputs;
1354 for (iteration = 1; iteration <= max_iter; ++iteration) {
1355 const std::vector<std::vector<double>> updated = local_solve(marginals, throughputs);
1357 for (std::size_t i = 0; i < G; ++i) residual = std::max(residual, detail::l1_diff(updated[i], marginals[i]));
1358 if (residual <= tol) {
1362 for (std::size_t i = 0; i < G; ++i)
1363 for (std::size_t j = 0; j < marginals[i].size(); ++j)
1364 marginals[i][j] = (1 - damping) * marginals[i][j] + damping * updated[i][j];
1366 if (iteration > max_iter) iteration = max_iter;
1367 throughputs.clear();
1368 local_solve(marginals, throughputs);
1369 std::map<std::pair<std::size_t, std::size_t>,
double> local_prob;
1370 for (std::size_t g = 0; g < G; ++g)
1371 for (std::size_t pos = 0; pos < partition[g].size(); ++pos) {
1372 const std::size_t a = snapshot.
index_of(partition[g][pos]);
1373 for (std::size_t v = 0; v < agents[a].states.size(); ++v) {
1375 for (std::size_t k = 0; k < group_states[g].size(); ++k)
1376 if (group_states[g][k][pos] == v) s += marginals[g][k];
1377 local_prob[{a, v}] = s;
1384 res.
iterations =
static_cast<int>(iteration);
1386 for (std::size_t a = 0; a < agents.size(); ++a)
1387 for (std::size_t v = 0; v < agents[a].states.size(); ++v)
1388 res.
state_table.
rows.push_back({agents[a].name, agents[a].states[v], local_prob.at({a, v})});
1389 for (
const std::string& label : labels) res.
action_table.rows.push_back({label, throughputs.at(label)});
1390 std::vector<GroupDistribution> dists;
1391 nlohmann::json sizes = nlohmann::json::array();
1392 for (std::size_t g = 0; g < G; ++g) {
1393 dists.push_back(GroupDistribution{partition[g], group_states[g], marginals[g]});
1394 sizes.push_back(group_states[g].size());
1396 res.group_distributions = dists;
1397 res.diagnostics = {{
"groups", partition_json},
1398 {
"group_states", sizes},
1399 {
"approximation",
"independent group marginals"},
1400 {
"damping", damping},
1401 {
"fixed_point_residual", residual},
1402 {
"product_form_certified",
false},
1403 {
"group_distributions", detail::group_json(dists)}};
1404 for (
TableAttrs* a : {&res.state_table.attrs, &res.action_table.attrs}) {
1405 a->method =
"grouped";
1407 a->extra[
"converged"] = converged;
1408 a->extra[
"groups"] = partition_json;
1409 a->extra[
"fixed_point_residual"] = residual;
1410 a->extra[
"product_form_certified"] =
false;