34 const std::optional<std::string>& level_name = std::nullopt,
long max_iter = 100,
37 std::vector<std::size_t> unbounded;
38 for (std::size_t a = 0; a < snapshot.
agents.size(); ++a)
39 if (snapshot.
agents[a].unbounded_queue) unbounded.push_back(a);
40 if (unbounded.size() != 1 || (level_name && *level_name != snapshot.
agents[unbounded[0]].name))
41 throw detail::mam_refusal(
"Model",
"Infinite QBD requires exactly one selected unbounded queue.");
42 const std::size_t pos = unbounded[0];
44 std::vector<std::size_t> phase_agents, dims;
46 for (std::size_t a = 0; a < snapshot.
agents.size(); ++a)
48 phase_agents.push_back(a);
49 dims.push_back(snapshot.
agents[a].states.size());
50 md *=
static_cast<double>(snapshot.
agents[a].states.size());
57 if (queue.
states.size() < 2)
58 throw detail::mam_refusal(
"Model",
59 "Infinite QBD requires an unbounded queue template with at least two levels.");
60 const long threshold =
static_cast<long>(queue.
states.size()) - 2;
61 auto positive_int = [](
double x) {
return x > 0 && (std::isinf(x) || x == std::floor(x)); };
62 if (!positive_int(max_phases) || !positive_int(max_states))
63 throw InputError(
"lal_max_phases and lal_max_states must be positive integers");
64 if (max_iter <= 0 || !std::isfinite(tol) || tol <= 0)
throw InputError(
"max_iter and tol must be positive");
65 if (md > max_phases || (threshold + 2) * md > max_states)
66 throw detail::mam_refusal(
"StateSpace",
"Infinite QBD exceeds the phase or boundary-state limit.");
67 if (8.0 * (threshold + 2) * md * md * 5 > 256.0 * 1024 * 1024)
68 throw detail::mam_refusal(
"StateSpace",
"Infinite QBD block storage exceeds 256 MiB.");
69 const std::vector<std::vector<std::size_t>> phases = detail::phase_product(snapshot, phase_agents);
70 const std::size_t m = phases.size();
71 const Model tmpl = detail::bounded_template(snapshot);
73 const std::vector<std::string>& labels = kernel.
labels;
74 const std::size_t count =
static_cast<std::size_t
>(threshold + 1);
75 if (8.0 * (5.0 * (count + 1) * m * m +
static_cast<double>(labels.size()) * count * m) > 256.0 * 1024 * 1024)
76 throw detail::mam_refusal(
"StateSpace",
"Infinite QBD block and reward storage exceeds 256 MiB.");
77 std::vector<Matrix<double>> diag(count,
Matrix<double>(m, m, 0.0)), up = diag, down = diag;
78 std::vector<Matrix<double>> rewards(labels.size(),
Matrix<double>(count, m, 0.0));
79 std::vector<std::vector<std::size_t>> adj((count + 1) * m);
80 std::size_t event_count = 0;
81 for (std::size_t n = 0; n <= count; ++n) {
82 for (std::size_t i = 0; i < m; ++i) {
83 std::vector<std::size_t> state(snapshot.
agents.size());
84 for (std::size_t k = 0; k < phase_agents.size(); ++k) state[phase_agents[k]] = phases[i][k];
87 if (++event_count > 5000000)
88 throw detail::mam_refusal(
"StateSpace",
"Infinite QBD exceeds the event-branch limit.");
89 if (mv.rate <= 0)
continue;
90 const std::size_t dest = mv.target[pos];
91 const long delta =
static_cast<long>(dest) -
static_cast<long>(n);
92 if (std::labs(delta) > 1)
93 throw detail::mam_refusal(
"Model",
"Infinite QBD requires level jumps of at most one.");
94 std::vector<std::size_t> tp;
95 for (std::size_t a : phase_agents) tp.push_back(mv.target[a]);
96 const std::size_t j = detail::flat_index(tp, dims);
97 if (dest != n || j != i) adj[n * m + i].push_back(dest * m + j);
98 if (n == count)
continue;
99 rewards[mv.label](n, i) += mv.rate;
101 if (i != j) diag[n](i, j) += mv.rate;
102 }
else if (delta == 1) {
103 up[n](i, j) += mv.rate;
105 down[n](i, j) += mv.rate;
110 const std::vector<double> rd = detail::row_sums(diag[n]), ru = detail::row_sums(up[n]),
111 rn = detail::row_sums(down[n]);
112 for (std::size_t i = 0; i < m; ++i) diag[n](i, i) = -(rd[i] + ru[i] + rn[i]);
115 if (!detail::unique_closed_class(adj))
116 throw detail::mam_refusal(
"Model",
"Infinite QBD requires a unique closed boundary/phase class.");
117 const Matrix<double> B = down.back(), L = diag.back(), F = up.back();
118 const Matrix<double> phase_generator = detail::add(detail::add(B, L), F);
120 std::vector<std::vector<std::size_t>> padj(m);
121 for (std::size_t i = 0; i < m; ++i)
122 for (std::size_t j = 0; j < m; ++j)
123 if (i != j && phase_generator(i, j) > 0) padj[i].push_back(j);
124 if (!detail::unique_closed_class(padj))
125 throw detail::mam_refusal(
"Model",
"Infinite QBD requires a unique closed homogeneous phase class.");
127 const std::vector<double> theta = detail::stationary(phase_generator);
128 double drift = 0.0, ldiag = detail::tiny();
130 const std::vector<double> fb = detail::row_sums(detail::add(F, detail::scaled(B, -1.0)));
131 for (std::size_t i = 0; i < m; ++i) {
132 drift += theta[i] * fb[i];
133 ldiag = std::max(ldiag, -L(i, i));
136 const double rate_scale = std::max({ldiag, detail::norm_inf(F), detail::norm_inf(B)});
137 if (drift >= -1e-12 * rate_scale)
138 throw detail::mam_refusal(
"Stability",
139 "Infinite QBD is unstable or critically loaded (drift " + detail::fmt_g(drift) +
").");
141 double R_residual = 0.0, rho = 0.0;
142 const double scale = std::max({detail::tiny(), detail::norm_inf(F), detail::norm_inf(L), detail::norm_inf(B)});
143 std::vector<std::vector<double>> boundary(count, std::vector<double>(m, 0.0));
144 auto zsum = [&](
const std::vector<double>& v) {
return detail::sum(
vecmul(v, Z)); };
146 R =
mam::qbd_R_logred(B, L, F,
static_cast<unsigned>(max_iter), std::min(tol, 1e-12));
147 R_residual = detail::norm_inf(detail::add(detail::add(F,
matmul(R, L)),
matmul(
matmul(R, R), B)));
151 for (std::size_t k = 0; k < R.
size(); ++k) {
152 finite = finite && std::isfinite(R[k]);
153 rmin = std::min(rmin, R[k]);
154 Rp[k] = std::max(R[k], 0.0);
156 rho = finite ? detail::spectral_radius_nonneg(Rp) : std::numeric_limits<double>::quiet_NaN();
157 if (!finite || rmin < -1e-10 || !(rho < 1) || R_residual > 1e-8 * scale)
158 throw detail::mam_refusal(
"Numerical",
"Infinite QBD rate matrix failed stability or residual checks.");
161 std::vector<Matrix<double>> schur = diag;
162 schur.back() = detail::add(schur.back(),
matmul(R, B));
163 std::vector<Matrix<double>> ratios(
static_cast<std::size_t
>(threshold));
164 for (
long n = threshold - 1; n >= 0; --n) {
165 ratios[n] = detail::solve_right(schur[n + 1], detail::scaled(up[n], -1.0));
166 schur[n] = detail::add(schur[n],
matmul(ratios[n], down[n + 1]));
168 boundary[0] = detail::stationary(schur[0]);
169 for (
long n = 0; n < threshold; ++n) boundary[n + 1] =
vecmul(boundary[n], ratios[n]);
170 double mass = zsum(boundary.back());
171 for (std::size_t n = 0; n + 1 < count; ++n) mass += detail::sum(boundary[n]);
172 for (
auto& row : boundary)
173 for (
double& p : row) p /= mass;
175 throw detail::mam_refusal(
"Numerical",
"Infinite QBD matrix solution is singular.");
178 for (
const auto& row : boundary)
180 if (!std::isfinite(p) || p < -1e-10) valid =
false;
181 if (!valid)
throw detail::mam_refusal(
"Numerical",
"Infinite QBD produced invalid probabilities.");
182 for (
auto& row : boundary)
183 for (
double& p : row) p = std::max(p, 0.0);
185 double mass = zsum(boundary.back());
186 for (std::size_t n = 0; n + 1 < count; ++n) mass += detail::sum(boundary[n]);
187 for (
auto& row : boundary)
188 for (
double& p : row) p /= mass;
190 double residual = 0.0;
191 for (std::size_t n = 0; n < count; ++n) {
192 std::vector<double> row =
vecmul(boundary[n], diag[n]);
194 const std::vector<double> u =
vecmul(boundary[n - 1], up[n - 1]);
195 for (std::size_t i = 0; i < m; ++i) row[i] += u[i];
197 const std::vector<double> d = n + 1 < count ?
vecmul(boundary[n + 1], down[n + 1])
199 for (std::size_t i = 0; i < m; ++i) row[i] += d[i];
200 for (
double x : row) residual += std::fabs(x);
202 if (residual > 1e-8 * scale)
203 throw detail::mam_refusal(
"Numerical",
"Infinite QBD boundary residual is too large.");
204 const std::vector<double> lastZ =
vecmul(boundary.back(), Z);
205 std::vector<double> phase_marginal = lastZ;
206 for (std::size_t n = 0; n + 1 < count; ++n)
207 for (std::size_t i = 0; i < m; ++i) phase_marginal[i] += boundary[n][i];
208 std::vector<double> event_rates(labels.size(), 0.0);
209 for (std::size_t l = 0; l < labels.size(); ++l) {
211 for (std::size_t n = 0; n + 1 < count; ++n)
212 for (std::size_t i = 0; i < m; ++i) x += boundary[n][i] * rewards[l](n, i);
213 for (std::size_t i = 0; i < m; ++i) x += lastZ[i] * rewards[l](count - 1, i);
216 std::vector<std::vector<double>> vectors = boundary;
217 const double by_states = std::isinf(max_states) ? kMamNoLimit
218 : std::floor(max_states /
static_cast<double>(m)) - 1.0;
219 const double limit = std::min(1000.0, std::max(
static_cast<double>(threshold + 1), by_states));
220 while (
static_cast<double>(vectors.size()) < limit) {
221 const std::vector<double> next_row =
vecmul(vectors.back(), R);
222 if (zsum(next_row) < 1e-10)
break;
223 vectors.push_back(next_row);
225 const double tail = zsum(
vecmul(vectors.back(), R));
227 for (std::size_t a = 0; a < snapshot.
agents.size(); ++a) {
230 for (std::size_t n = 0; n < vectors.size(); ++n)
234 const std::size_t p =
static_cast<std::size_t
>(
235 std::find(phase_agents.begin(), phase_agents.end(), a) - phase_agents.begin());
236 for (std::size_t s = 0; s < agent.
states.size(); ++s) {
238 for (std::size_t i = 0; i < m; ++i)
239 if (phases[i][p] == s) x += phase_marginal[i];
247 at->method =
"infiniteqbd";
248 at->exact = !truncated;
249 at->truncated = truncated;
250 at->extra[
"level_tail"] = tail;
252 res.
method =
"infiniteqbd";
253 res.
exact = !truncated;
256 for (
const auto& ph : phases) res.
phase_states.push_back(detail::phase_names_of(snapshot, phase_agents, ph));
265 for (std::size_t n = 0; n + 1 < count; ++n) mean +=
static_cast<double>(n) * detail::sum(boundary[n]);
266 mean +=
static_cast<double>(threshold) * zsum(boundary.back());