5#ifndef LINE_LANG_LAL_MAM_COMMON_H
6#define LINE_LANG_LAL_MAM_COMMON_H
92 nlohmann::json
extra = nlohmann::json::object();
112 if (r.agent == agent)
return r;
148 nlohmann::json
extra = nlohmann::json::object();
160inline UnsupportedError mam_refusal(
const char* kind,
const std::string& msg) {
165inline std::string fmt_g(
double x) {
167 std::snprintf(buf,
sizeof(buf),
"%g", x);
171inline double tiny() {
return std::numeric_limits<double>::min(); }
174inline bool unique_closed_class(
const std::vector<std::vector<std::size_t>>& adj) {
176 const std::vector<int> comp = strong_components(adj, count);
177 if (count == 1)
return true;
178 std::vector<bool> opened(
static_cast<std::size_t
>(count),
false);
179 for (std::size_t i = 0; i < adj.size(); ++i)
180 for (std::size_t j : adj[i])
181 if (comp[i] != comp[j]) opened[
static_cast<std::size_t
>(comp[i])] =
true;
182 std::size_t closed = 0;
183 for (
bool o : opened)
189inline int strong_count_positive(
const Matrix<double>& q) {
190 std::vector<std::vector<std::size_t>> adj(q.rows());
191 for (std::size_t i = 0; i < q.rows(); ++i)
192 for (std::size_t j = 0; j < q.cols(); ++j)
193 if (i != j && q(i, j) > 0) adj[i].push_back(j);
195 strong_components(adj, count);
199inline double norm_inf(
const Matrix<double>& a);
209inline double spectral_radius_nonneg(
const Matrix<double>& A) {
210 const std::size_t n = A.rows();
211 if (n == 0)
return 0.0;
213 Matrix<double> B = A;
217 for (
int s = 0; s < 64; ++s) {
218 const double norm = norm_inf(B);
219 if (norm == 0.0)
return 0.0;
220 const double next = std::exp((std::log(norm) + L) / power);
221 if (s > 8 && std::fabs(next - estimate) <= 1e-15 * std::max(1.0, next))
return next;
223 for (std::size_t k = 0; k < B.size(); ++k) B[k] /= norm;
224 L = 2.0 * (L + std::log(norm));
232inline std::vector<double> eigvalsh(Matrix<double> a) {
233 const std::size_t n = a.rows();
234 for (
int sweep = 0; sweep < 100; ++sweep) {
235 double off = 0.0, total = 0.0;
236 for (std::size_t i = 0; i < n; ++i)
237 for (std::size_t j = 0; j < n; ++j) {
238 total += a(i, j) * a(i, j);
239 if (i != j) off += a(i, j) * a(i, j);
241 if (off <= 1e-30 * std::max(total, std::numeric_limits<double>::min()))
break;
242 for (std::size_t p = 0; p + 1 < n; ++p)
243 for (std::size_t q = p + 1; q < n; ++q) {
244 if (a(p, q) == 0.0)
continue;
245 const double theta = (a(q, q) - a(p, p)) / (2.0 * a(p, q));
246 const double t = (theta >= 0 ? 1.0 : -1.0) / (std::fabs(theta) + std::sqrt(theta * theta + 1.0));
247 const double c = 1.0 / std::sqrt(t * t + 1.0), s = t * c;
248 for (std::size_t k = 0; k < n; ++k) {
249 const double akp = a(k, p), akq = a(k, q);
250 a(k, p) = c * akp - s * akq;
251 a(k, q) = s * akp + c * akq;
253 for (std::size_t k = 0; k < n; ++k) {
254 const double apk = a(p, k), aqk = a(q, k);
255 a(p, k) = c * apk - s * aqk;
256 a(q, k) = s * apk + c * aqk;
260 std::vector<double> ev(n);
261 for (std::size_t i = 0; i < n; ++i) ev[i] = a(i, i);
262 std::sort(ev.begin(), ev.end());
267inline Matrix<double> solve_right(
const Matrix<double>& A,
const Matrix<double>& B) {
268 Matrix<double>
lu = A.transpose();
269 const std::vector<std::size_t> piv =
lu_factor(lu);
270 Matrix<double> X(B.rows(), B.cols(), 0.0);
271 std::vector<double> b(A.rows());
272 for (std::size_t i = 0; i < B.rows(); ++i) {
273 for (std::size_t j = 0; j < B.cols(); ++j) b[j] = B(i, j);
275 for (std::size_t j = 0; j < B.cols(); ++j) X(i, j) = b[j];
280inline Matrix<double> add(
const Matrix<double>& a,
const Matrix<double>& b) {
281 Matrix<double> c = a;
282 for (std::size_t k = 0; k < c.size(); ++k) c[k] += b[k];
286inline Matrix<double> scaled(
const Matrix<double>& a,
double s) {
287 Matrix<double> c = a;
288 for (std::size_t k = 0; k < c.size(); ++k) c[k] *= s;
292inline double sum(
const std::vector<double>& v) {
294 for (
double x : v) s += x;
300inline double norm_inf(
const Matrix<double>& a) {
302 for (std::size_t i = 0; i < a.rows(); ++i) {
304 for (std::size_t j = 0; j < a.cols(); ++j) r += std::fabs(a(i, j));
310inline std::vector<double> row_sums(
const Matrix<double>& a) {
311 std::vector<double> r(a.rows(), 0.0);
312 for (std::size_t i = 0; i < a.rows(); ++i)
313 for (std::size_t j = 0; j < a.cols(); ++j) r[i] += a(i, j);
325inline std::vector<double> stationary(
const Matrix<double>& q) {
326 const std::size_t n = q.rows();
327 if (n == 1)
return std::vector<double>(1, 1.0);
330 for (std::size_t k = 0; k < q.size(); ++k) {
331 finite = finite && std::isfinite(q[k]);
332 scale = std::max(scale, std::fabs(q[k]));
334 if (!finite || scale == 0)
335 throw mam_refusal(
"Numerical",
"QBD phase generator has no unique stationary solution.");
336 const Matrix<double> m = scaled(q, 1.0 / scale);
337 Matrix<double> eq(n, n, 0.0);
338 for (std::size_t i = 0; i < n; ++i)
339 for (std::size_t j = 0; j < n; ++j) eq(i, j) = m(j, i);
340 for (std::size_t i = 0; i < n; ++i) {
342 for (std::size_t j = 0; j < n; ++j) r = std::max(r, std::fabs(eq(i, j)));
344 for (std::size_t j = 0; j < n; ++j) eq(i, j) /= r;
346 for (std::size_t j = 0; j < n; ++j) eq(n - 1, j) = 1.0;
347 std::vector<double> p(n, 0.0);
350 const std::vector<std::size_t> piv =
lu_factor(eq);
352 }
catch (
const NumericError&) {
353 throw mam_refusal(
"Numerical",
"QBD phase balance equations are singular.");
358 ok = ok && std::isfinite(x);
359 low = std::min(low, x);
361 double residual = 0.0;
363 for (std::size_t j = 0; j < n; ++j) {
365 for (std::size_t i = 0; i < n; ++i) s += p[i] * m(i, j);
366 residual += std::fabs(s);
368 if (!ok || low < -1e-10 || residual > 1e-8)
369 throw mam_refusal(
"Numerical",
"QBD phase balance equations produced invalid probabilities.");
371 for (
double& x : p) {
372 x = std::max(x, 0.0);
375 for (
double& x : p) x /= total;
380inline std::pair<long, bool> queue_level(
const std::string& state) {
381 const bool tail = state.compare(0, 5,
"n_ge_") == 0;
382 return {std::stol(tail ? state.substr(5) : state.substr(2)), tail};
386inline Model bounded_template(
const Model& snapshot) {
388 for (AgentRecord& a : t.agents) a.unbounded_queue =
false;
393inline std::vector<std::string> kernel_labels(
const Model& snapshot) {
394 const Model t = bounded_template(snapshot);
LINE Agent Language (LAL): programmatic construction of finite agents and their composition,...
A refusal: the feature, method or analysis is not covered: LINE:unsupported:<kind>.
UnsupportedError(const std::string &what)
LINE Agent Language (LAL): the finite-agent model record, its validation, the joint-state event kerne...
LINE Agent Language (LAL): the result tables every LAL solver returns, the exact finite CTMC analysis...
Steady-state distribution of a continuous-time Markov chain.
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.
Dense matrix and non-owning view.
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
std::vector< Matrix< double > > MarkedMatrices
A marked process as Python's list [D0, D1, D1_1, ..., D1_K].
InputError value_error(const std::string &what)
LINE:input:Unspecified, the C++ spelling of the Python ValueError raised throughout LAL.
HittingDistResult estimate(const Step &step, const IsRates &isr, bool hasIS, const Target &target, const State &x0, const std::vector< double > &t, const Checked &c, const HittingDistOptions &opt)
The cycles and the five estimators, common to both forms.
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< std::size_t > lu_factor(Matrix< T > &A)
In-place LU of A (n x n).
Matrix< T > matmul(const Matrix< T > &A, const Matrix< T > &B)
Matrix product A B.
The union of Python's LAL MAM result dataclasses.
std::optional< std::string > approximation
std::map< std::string, double > queue_means
empty: Python has no queue_means
std::optional< QueueTable > queue_measures
std::optional< double > tail_probability
std::vector< std::string > phase_dependent_routing
std::vector< std::vector< double > > level_probabilities
One row per displayed level (finite and infinite QBD) or per level (reachable QBD).
std::vector< MarkedMatrices > source_matrices
std::optional< Traffic > traffic
std::optional< std::string > residual_kind
std::vector< std::vector< std::string > > phase_states
Phase tuples as agent-state names, in the column order of level_probabilities.
nlohmann::json native_internal_fitting
null: absent
bool has_converged
whether the lowering reports converged at all
std::optional< double > native_time_scale
dec.source: factor dividing the lowered network's rates
std::vector< std::string > correlated_service_reset
std::optional< double > drift
std::vector< std::string > unavailable_clock_phases
std::vector< std::string > unavailable_busy_probabilities
multiserver queues: Busy is NaN
std::vector< std::string > source_marks
nlohmann::json poissonised_visit_excess
std::optional< double > mean_level
std::optional< Matrix< double > > rate_matrix
std::vector< double > closed_class_weights
std::optional< double > rate_matrix_residual
std::optional< double > spectral_radius
nlohmann::json native_time_scales
marked.thinning: station -> scale (null without input); null: absent
std::optional< ActionTable > raw_action_table
The action table under the lowered agents' own route-specific labels, kept when an embedded network w...
std::optional< std::size_t > reachable_states
One row of getQueueTable(): Agent, QLen, Busy, Util, ArvR, Tput, RespT.
const QueueRow & row(const std::string &agent) const
std::vector< QueueRow > rows
The result container shared by the LAL solver paths (LALCTMCResult, the AG decomposition results,...
The attrs of a LAL result table.
The traffic dictionary of a MAM result (result.traffic).
std::optional< MarkedMatrices > fitted_matrices
std::optional< std::string > compression
std::optional< double > fit_tolerance
std::optional< Matrix< double > > original_count_covariance
std::optional< Matrix< double > > fitted_count_covariance
std::vector< std::string > marks
std::vector< MarkedMatrices > original_streams
std::vector< double > fitted_rates
std::optional< std::string > fallback_reason
std::optional< MarkedMatrices > matrices
std::optional< double > rate_error
std::vector< std::string > targets
std::vector< std::string > sources
std::optional< MarkedMatrices > original_matrices
std::optional< std::string > unavailable
std::optional< double > fit_time
std::optional< double > count_covariance_error
std::optional< std::string > blocking
std::vector< double > original_rates
std::vector< MarkedMatrices > streams
std::optional< bool > feasible