110 const std::optional<std::string>& level_name = std::nullopt) {
112 const auto st =
qbd_structure(snapshot, max_phases, max_states, level_name);
113 if (!st.first)
throw detail::mam_refusal(
"Model", st.second);
114 const std::size_t level_pos = snapshot.
index_of(*st.first);
116 std::vector<std::size_t> phase_agents, dims;
117 for (std::size_t a = 0; a < snapshot.
agents.size(); ++a)
118 if (a != level_pos) {
119 phase_agents.push_back(a);
120 dims.push_back(snapshot.
agents[a].states.size());
122 const std::vector<std::vector<std::size_t>> phases = detail::phase_product(snapshot, phase_agents);
123 const std::size_t m = phases.size(), levels = level_agent.
states.size();
125 const std::vector<std::string>& labels = kernel.
labels;
126 const double estimated = 8.0 * (5.0 * levels * m * m +
static_cast<double>(labels.size()) * levels * m);
127 if (estimated > 256.0 * 1024 * 1024)
128 throw detail::mam_refusal(
"StateSpace",
"Finite QBD block storage exceeds 256 MiB.");
129 std::vector<Matrix<double>> diag(levels,
Matrix<double>(m, m, 0.0)), up = diag, down = diag;
130 std::vector<Matrix<double>> rewards(labels.size(),
Matrix<double>(levels, m, 0.0));
131 std::size_t event_count = 0;
132 for (std::size_t n = 0; n < levels; ++n) {
133 for (std::size_t i = 0; i < m; ++i) {
134 std::vector<std::size_t> state(snapshot.
agents.size());
135 for (std::size_t k = 0; k < phase_agents.size(); ++k) state[phase_agents[k]] = phases[i][k];
136 state[level_pos] = n;
138 if (++event_count > 5000000)
139 throw detail::mam_refusal(
"StateSpace",
"Finite QBD exceeds the event-branch limit.");
140 if (mv.rate <= 0)
continue;
141 const long delta =
static_cast<long>(mv.target[level_pos]) -
static_cast<long>(n);
142 if (std::labs(delta) > 1)
143 throw detail::mam_refusal(
"Model",
144 "Finite QBD requires every event to change the level by at most one.");
145 std::vector<std::size_t> tp;
146 for (std::size_t a : phase_agents) tp.push_back(mv.target[a]);
147 const std::size_t j = detail::flat_index(tp, dims);
148 rewards[mv.label](n, i) += mv.rate;
150 if (i != j) diag[n](i, j) += mv.rate;
151 }
else if (delta == 1) {
152 up[n](i, j) += mv.rate;
154 down[n](i, j) += mv.rate;
158 const std::vector<double> rd = detail::row_sums(diag[n]), ru = detail::row_sums(up[n]),
159 rn = detail::row_sums(down[n]);
160 for (std::size_t i = 0; i < m; ++i) diag[n](i, i) = -(rd[i] + ru[i] + rn[i]);
162 std::vector<std::vector<std::size_t>> adj(levels * m);
163 for (std::size_t n = 0; n < levels; ++n) {
164 const std::pair<const Matrix<double>*,
long> blocks[3] = {
165 {&diag[n],
static_cast<long>(n)}, {&up[n],
static_cast<long>(n) + 1}, {&down[n],
static_cast<long>(n) - 1}};
166 for (
const auto& b : blocks) {
167 if (b.second < 0 || b.second >=
static_cast<long>(levels))
continue;
168 for (std::size_t i = 0; i < m; ++i)
169 for (std::size_t j = 0; j < m; ++j)
170 if ((*b.first)(i, j) > 0) adj[n * m + i].push_back(
static_cast<std::size_t
>(b.second) * m + j);
173 if (!detail::unique_closed_class(adj))
174 throw detail::mam_refusal(
"Model",
175 "Finite QBD requires a unique closed class; use SolverCTMC for reducible models "
176 "with multiple closed classes.");
177 std::vector<Matrix<double>> schur = diag;
178 std::vector<Matrix<double>> ratios(levels > 0 ? levels - 1 : 0);
179 std::vector<std::vector<double>> prob(levels, std::vector<double>(m, 0.0));
181 for (
long n =
static_cast<long>(levels) - 2; n >= 0; --n) {
182 ratios[n] = detail::solve_right(schur[n + 1], detail::scaled(up[n], -1.0));
183 schur[n] = detail::add(schur[n],
matmul(ratios[n], down[n + 1]));
185 prob[0] = detail::stationary(schur[0]);
186 for (std::size_t n = 0; n + 1 < levels; ++n) prob[n + 1] =
vecmul(prob[n], ratios[n]);
188 throw detail::mam_refusal(
"Numerical",
"Finite QBD block elimination failed due to a singular block.");
190 double total = 0.0, minimum = std::numeric_limits<double>::infinity();
192 for (
const auto& row : prob)
193 for (
double p : row) {
194 finite = finite && std::isfinite(p);
196 minimum = std::min(minimum, p);
198 if (!finite || minimum < -1e-10 || total <= 0)
199 throw detail::mam_refusal(
"Numerical",
"Finite QBD produced invalid stationary probabilities.");
201 for (
auto& row : prob)
202 for (
double& p : row) {
203 p = std::max(p, 0.0);
206 for (
auto& row : prob)
207 for (
double& p : row) p /= total;
208 double residual = 0.0, scale = detail::tiny();
209 for (std::size_t n = 0; n < levels; ++n) {
210 std::vector<double> r =
vecmul(prob[n], diag[n]);
212 const std::vector<double> u =
vecmul(prob[n - 1], up[n - 1]);
213 for (std::size_t i = 0; i < m; ++i) r[i] += u[i];
215 if (n + 1 < levels) {
216 const std::vector<double> d =
vecmul(prob[n + 1], down[n + 1]);
217 for (std::size_t i = 0; i < m; ++i) r[i] += d[i];
219 for (
double x : r) residual += std::fabs(x);
220 for (std::size_t i = 0; i < m; ++i) scale = std::max(scale, -diag[n](i, i));
222 if (residual > 1e-8 * scale)
223 throw detail::mam_refusal(
"Numerical",
"Finite QBD residual " + detail::fmt_g(residual) +
" is too large.");
225 for (std::size_t a = 0; a < snapshot.
agents.size(); ++a) {
227 for (std::size_t s = 0; s < agent.
states.size(); ++s) {
229 if (a == level_pos) {
230 p = detail::sum(prob[s]);
232 const std::size_t pos =
static_cast<std::size_t
>(
233 std::find(phase_agents.begin(), phase_agents.end(), a) - phase_agents.begin());
234 for (std::size_t n = 0; n < levels; ++n)
235 for (std::size_t i = 0; i < m; ++i)
236 if (phases[i][pos] == s) p += prob[n][i];
241 for (std::size_t l = 0; l < labels.size(); ++l) {
243 for (std::size_t n = 0; n < levels; ++n)
244 for (std::size_t i = 0; i < m; ++i) x += prob[n][i] * rewards[l](n, i);
249 at->method =
"finiteqbd";
250 at->exact = !truncated;
251 at->truncated = truncated;
253 if (8.0 * m * m * (labels.size() + 2.0) <= 32.0 * 1024 * 1024)
256 res.
exact = !truncated;
259 for (
const auto& ph : phases) res.
phase_states.push_back(detail::phase_names_of(snapshot, phase_agents, ph));