133 if (!rec.first)
throw detail::mam_refusal(
"Model", rec.second);
134 if (!(max_states > 0) || max_states != std::floor(max_states))
135 throw InputError(
"lal_max_states must be a positive integer");
137 const std::size_t n = match.
queues.size();
138 std::map<std::string, std::size_t> index;
139 for (std::size_t i = 0; i < n; ++i) index[snapshot.
agents[match.
queues[i]].name] = i;
140 if (n > 512)
throw detail::mam_refusal(
"StateSpace",
"Jackson traffic system exceeds 512 queues.");
141 std::vector<double> external(n, 0.0);
142 std::vector<std::pair<Endpoint, double>> source_rates;
143 auto add_rate = [&](
const Endpoint& e,
double r) {
144 for (
auto& kv : source_rates)
149 source_rates.push_back({e, r});
151 for (std::size_t a : match.
sources)
153 for (
const auto& kv : source_rates) {
154 auto it = match.
sync.find(kv.first);
155 if (it != match.
sync.end()) external[index[it->second]] += kv.second;
158 for (std::size_t i = 0; i < n; ++i) {
160 for (
const auto& route : q.
queue_info->routes) {
162 if (it != match.
sync.end()) routing(i, index[it->second]) += route.second;
165 if (detail::spectral_radius_nonneg(routing) >= 1 - 1e-12)
166 throw detail::mam_refusal(
"Stability",
"Jackson routing must be open, with every job eventually departing.");
168 for (std::size_t i = 0; i < n; ++i)
169 for (std::size_t j = 0; j < n; ++j) system(i, j) = (i == j ? 1.0 : 0.0) - routing(j, i);
170 std::vector<double> rates;
172 rates = detail::lu_solve_vec(system, external);
174 throw detail::mam_refusal(
"Numerical",
"Jackson traffic equations are singular.");
176 double residual = 0.0, rmax = -std::numeric_limits<double>::infinity(), rmin = std::numeric_limits<double>::infinity();
178 for (std::size_t i = 0; i < n; ++i) {
179 double r = rates[i] - external[i];
180 for (std::size_t j = 0; j < n; ++j) r -= routing(j, i) * rates[j];
181 residual = std::max(residual, std::fabs(r));
182 finite = finite && std::isfinite(rates[i]);
183 rmax = std::max(rmax, rates[i]);
184 rmin = std::min(rmin, rates[i]);
186 if (!finite || rmin < -1e-10 || residual > 1e-8 * std::max(1.0, rmax))
187 throw detail::mam_refusal(
"Numerical",
"Jackson traffic equations produced invalid rates.");
188 for (
double& r : rates) r = std::max(r, 0.0);
190 std::map<std::string, std::pair<std::vector<double>,
double>> distributions;
191 for (std::size_t i = 0; i < n; ++i) {
193 const double rate = rates[i], mu = *q.
queue_info->service_rate;
195 const double rho = rate / (servers * mu);
196 if (rho >= 1 - 1e-12)
197 throw detail::mam_refusal(
"Stability",
"Jackson queue " + q.
name +
" is unstable or critically loaded (utilization " +
198 detail::fmt_g(rho) +
").");
199 if (max_states < servers + 1)
200 throw detail::mam_refusal(
"StateSpace",
"Jackson display limit must exceed the number of servers.");
201 std::vector<double> probabilities;
202 double tail = 0.0, mean = 0.0;
204 probabilities = {1.0};
206 const double a = rate / mu;
207 std::vector<double> log_weights(
static_cast<std::size_t
>(servers) + 1);
208 for (
long k = 0; k <= servers; ++k) log_weights[k] = k * std::log(a) - std::lgamma(k + 1.0);
209 std::vector<double> terms(log_weights.begin(), log_weights.end() - 1);
210 terms.push_back(log_weights.back() - std::log1p(-rho));
211 double mx = -std::numeric_limits<double>::infinity();
212 for (
double t : terms) mx = std::max(mx, t);
214 for (
double t : terms) acc += std::exp(t - mx);
215 const double log_norm = mx + std::log(acc);
216 const double p_c = std::exp(log_weights.back() - log_norm);
217 const double erlang_c = p_c / (1 - rho);
218 mean = a + erlang_c * rho / (1 - rho);
219 const long display_limit =
static_cast<long>(std::min(max_states, std::max(1000.0, servers + 1.0)));
220 for (
long k = 0; k < display_limit; ++k) {
221 const double probability = k <= servers ? std::exp(log_weights[k] - log_norm)
222 : p_c * std::pow(rho,
static_cast<double>(k - servers));
223 probabilities.push_back(probability);
224 if (k >= servers && probabilities.back() * rho / (1 - rho) < 1e-10)
break;
226 tail = probabilities.back() * rho / (1 - rho);
228 distributions[q.
name] = {probabilities, tail};
232 if (index.count(agent.
name)) {
233 const auto& d = distributions[agent.
name];
234 for (std::size_t k = 0; k < d.first.size(); ++k)
241 std::map<std::string, double> action_rates;
242 for (
const auto& kv : source_rates) action_rates[
endpoint_label(kv.first)] = kv.second;
243 for (std::size_t i = 0; i < n; ++i) {
245 for (
const auto& route : q.
queue_info->routes) action_rates[q.
name +
"." + route.first] = rates[i] * route.second;
247 for (
const std::string& label : detail::active_labels(snapshot))
251 at->method =
"jackson";
252 at->exact = !truncated;
253 at->truncated = truncated;
256 res.
exact = !truncated;
260 res.
extra[
"routing"] = detail::matrix_json(routing);
261 res.
extra[
"external_rates"] = external;
262 res.
extra[
"queue_rates"] = rates;