LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
mam_traffic.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_LANG_LAL_MAM_TRAFFIC_H
6#define LINE_LANG_LAL_MAM_TRAFFIC_H
7
8/**
9 * @file
10 * @ingroup line_lang
11 * LINE Agent Language (LAL): independent marked arrival proposals of a finite
12 * composition and their explicit fitting (solver_mam/lal_traffic.py,
13 * `offered_traffic` and `fit_traffic`).
14 *
15 * These MMAPs describe offered traffic before any queue blocking. They are not
16 * substitutes for the joint queue/phase dynamics of a blocked composition.
17 */
18
19#include <cmath>
20#include <functional>
21#include <map>
22#include <optional>
23#include <string>
24#include <vector>
25
32#include "line/util/expm.h"
33
34namespace line {
35namespace lal {
36
37namespace detail {
38
39inline mam::Mmap<double> to_mmap(const MarkedMatrices& m) {
40 mam::Mmap<double> out;
41 out.D0 = m.at(0);
42 out.D1 = m.at(1);
43 for (std::size_t k = 2; k < m.size(); ++k) out.Dc.push_back(m[k]);
44 return out;
45}
46
47inline MarkedMatrices from_mmap(const mam::Mmap<double>& m) {
48 MarkedMatrices out{m.D0, m.D1};
49 for (const Matrix<double>& d : m.Dc) out.push_back(d);
50 return out;
51}
52
53/**
54 * Python's `mmap_compress(D0, D_list, method=...)`: the M3A methods of
55 * mmap_compress.h plus Python's own 'exponential' (a marked Poisson process
56 * keeping the aggregate rate and the class probabilities), then
57 * mmap_normalize as for every method.
58 */
59inline mam::Mmap<double> compress_mmap(const mam::Mmap<double>& in, const std::string& method) {
61 if (method == "exponential") {
62 const double lambda = mam::map_lambda(in.map());
63 const std::vector<double> p = mam::mmap_pc(in);
64 mam::Mmap<double> out;
65 out.D0 = Matrix<double>(1, 1, -lambda);
66 out.D1 = Matrix<double>(1, 1, lambda);
67 for (std::size_t c = 0; c < in.classes(); ++c) out.Dc.push_back(Matrix<double>(1, 1, p[c] * lambda));
68 return mam::mmap_normalize(out);
69 }
70 static const std::map<std::string, MmapCompressMethod> names = {
71 {"default", MmapCompressMethod::MixtureOrder1},
72 {"mixture", MmapCompressMethod::MixtureOrder1},
73 {"mixture.order1", MmapCompressMethod::MixtureOrder1},
74 {"mixture.order2", MmapCompressMethod::MixtureOrder2},
75 {"mamap2", MmapCompressMethod::Mamap2},
76 {"mamap2.fb", MmapCompressMethod::Mamap2Fb},
77 {"m3pp.approx_cov", MmapCompressMethod::M3ppApproxCov},
78 {"m3pp.approx_ag", MmapCompressMethod::M3ppApproxAg},
79 {"m3pp.exact_delta", MmapCompressMethod::M3ppExactDelta},
80 {"m3pp.approx_delta", MmapCompressMethod::M3ppApproxDelta}};
81 auto it = names.find(method);
82 if (it == names.end()) throw InputError("Unknown compression method: " + method);
83 return mam::mmap_compress(in, it->second);
84}
85
86inline bool feasible(const MarkedMatrices& m, double tol) {
87 return mam::mmap_isfeasible_tol(to_mmap(m), tol);
88}
89
90inline Matrix<double> block_diag(const std::vector<Matrix<double>>& blocks) {
91 std::size_t n = 0;
92 for (const Matrix<double>& b : blocks) n += b.rows();
93 Matrix<double> out(n, n, 0.0);
94 std::size_t off = 0;
95 for (const Matrix<double>& b : blocks) {
96 for (std::size_t i = 0; i < b.rows(); ++i)
97 for (std::size_t j = 0; j < b.cols(); ++j) out(off + i, off + j) = b(i, j);
98 off += b.rows();
99 }
100 return out;
101}
102
103} // namespace detail
104
105/**
106 * Exact superposed offered MMAP and mark provenance, or nullopt
107 * (`offered_traffic`).
108 *
109 * Each non-level agent must be a standalone active phase process whose marks
110 * synchronize solely with an input of the level agent. A min-sync, a passive
111 * source transition or another dependency invalidates this decomposition.
112 */
113inline std::optional<Traffic> offered_traffic(const Model& snapshot, const std::string& level_name,
114 bool materialize = true) {
115 if (!snapshot.min_syncs.empty()) return std::nullopt;
116 std::vector<std::string> names;
117 for (const AgentRecord& a : snapshot.agents)
118 if (a.name != level_name) names.push_back(a.name);
119 if (names.empty()) return std::nullopt;
120 std::map<Endpoint, std::vector<Endpoint>> sync;
121 std::vector<Endpoint> sync_order;
122 for (const Sync& s : snapshot.syncs) {
123 if (!sync.count(s.active)) sync_order.push_back(s.active);
124 sync[s.active] = s.passive;
125 }
126 Traffic t;
127 for (const std::string& name : names) {
128 const AgentRecord& agent = snapshot.agent(name);
129 for (std::size_t i = 0; i < agent.states.size(); ++i)
130 if (agent.states[i] != "phase_" + std::to_string(i)) return std::nullopt;
131 std::vector<std::string> actions;
132 for (const Transition& tr : agent.transitions) {
133 if (tr.role != Role::Active) return std::nullopt;
134 if (tr.action != "_phase" && std::find(actions.begin(), actions.end(), tr.action) == actions.end())
135 actions.push_back(tr.action);
136 }
137 if (actions.empty() || sync.count(Endpoint(name, "_phase"))) return std::nullopt;
138 for (const std::string& action : actions) {
139 auto it = sync.find(Endpoint(name, action));
140 if (it == sync.end() || it->second.size() != 1 || it->second[0].first != level_name) return std::nullopt;
141 }
142 const std::size_t n = agent.states.size();
143 Matrix<double> d0(n, n, 0.0);
144 std::vector<Matrix<double>> marks(actions.size(), Matrix<double>(n, n, 0.0));
145 for (const Transition& tr : agent.transitions) {
146 const std::size_t i = agent.state_index(tr.source), j = agent.state_index(tr.target);
147 if (tr.action == "_phase") {
148 if (i != j) d0(i, j) += tr.value;
149 } else {
150 const std::size_t k = static_cast<std::size_t>(
151 std::find(actions.begin(), actions.end(), tr.action) - actions.begin());
152 marks[k](i, j) += tr.value;
153 }
154 }
155 Matrix<double> d1(n, n, 0.0);
156 for (const Matrix<double>& m : marks) d1 = detail::add(d1, m);
157 for (std::size_t i = 0; i < n; ++i) {
158 double r = 0.0;
159 for (std::size_t j = 0; j < n; ++j) r += d0(i, j);
160 for (const Matrix<double>& m : marks)
161 for (std::size_t j = 0; j < n; ++j) r += m(i, j);
162 d0(i, i) = -r;
163 }
164 MarkedMatrices stream{d0, d1};
165 stream.insert(stream.end(), marks.begin(), marks.end());
166 t.streams.push_back(stream);
167 for (const std::string& action : actions) {
168 t.marks.push_back(name + "." + action);
169 t.targets.push_back(sync[Endpoint(name, action)][0].second);
170 }
171 }
172 for (const Endpoint& active : sync_order) {
173 const std::vector<Endpoint>& partners = sync[active];
174 if (active.first == level_name || std::find(names.begin(), names.end(), active.first) == names.end() ||
175 active.second == "_phase")
176 return std::nullopt;
177 if (partners.size() != 1 || partners[0].first != level_name) return std::nullopt;
178 }
179 for (const MarkedMatrices& s : t.streams)
180 if (!detail::feasible(s, 1e-6)) return std::nullopt;
181 if (materialize) {
182 mam::Mmap<double> combined = detail::to_mmap(t.streams[0]);
183 for (std::size_t k = 1; k < t.streams.size(); ++k)
184 combined = mam::mmap_super(combined, detail::to_mmap(t.streams[k]));
185 const MarkedMatrices c = detail::from_mmap(combined);
186 if (!detail::feasible(c, 1e-6)) return std::nullopt;
187 t.matrices = c;
188 }
189 t.kind = "offered";
190 t.method = "mmap_super";
191 t.exact = true;
192 t.sources = names;
193 t.blocking = std::string("Excluded: accepted traffic depends on queue state.");
194 return t;
195}
196
197/**
198 * Replaceable numerical kernels of `fit_traffic`, so a test can intercept them
199 * as the Python tests monkeypatch `mmap_ops.mmap_compress` and
200 * `m3a.m3pp2m_fitc_theoretical`. An empty member keeps the library kernel.
201 * `compress(original, compress_method)` and
202 * `m3pp_fit(source, "exact_delta", t, tinf)` take and return [D0, D1, marks...].
203 */
205 std::function<MarkedMatrices(const MarkedMatrices&, const std::string&)> compress;
206 std::function<MarkedMatrices(const MarkedMatrices&, const std::string&, double, double)> m3pp_fit;
207};
208
209/** Output of `fit_traffic`. */
219
220namespace detail {
221
222/** Largest absolute entry over every matrix of a marked process (0 when empty). */
223inline double marked_max_abs(const MarkedMatrices& m) {
224 double s = 0.0;
225 for (const Matrix<double>& x : m) s = std::max(s, max_abs(x));
226 return s;
227}
228
229/**
230 * Marked count rates and count covariance over a window (`_count_statistics`),
231 * through one block exponential of the generator scaled by the window, so the
232 * statistics are free of the process time scale.
233 */
234inline std::pair<std::vector<double>, Matrix<double>> count_statistics(const MarkedMatrices& stream, double duration) {
235 const Matrix<double> q = add(stream[0], stream[1]);
236 const std::size_t n = q.rows();
237 const std::size_t marks = stream.size() - 2;
238 const std::vector<double> pi = stationary(q);
239 auto row_times = [&](const std::vector<double>& v, const Matrix<double>& a) {
240 std::vector<double> out(a.cols(), 0.0);
241 for (std::size_t i = 0; i < a.rows(); ++i)
242 for (std::size_t j = 0; j < a.cols(); ++j) out[j] += v[i] * a(i, j);
243 return out;
244 };
245 auto total = [](const std::vector<double>& v) {
246 double s = 0.0;
247 for (double x : v) s += x;
248 return s;
249 };
250 std::vector<double> rates(marks, 0.0);
251 for (std::size_t k = 0; k < marks; ++k) rates[k] = total(row_times(pi, stream[k + 2]));
252 Matrix<double> generator(3 * n, 3 * n, 0.0);
253 bool finite = true;
254 for (std::size_t i = 0; i < n; ++i) {
255 for (std::size_t j = 0; j < n; ++j) {
256 generator(i, j) = q(i, j) * duration;
257 finite = finite && std::isfinite(generator(i, j));
258 }
259 generator(i, n + i) = 1.0;
260 generator(n + i, 2 * n + i) = 1.0;
261 }
262 if (!finite) throw mam_refusal("Numerical", "Traffic count window exceeds numerical limits.");
263 const Matrix<double> e = line::expm(generator);
264 Matrix<double> integral(n, n, 0.0);
265 for (std::size_t i = 0; i < n; ++i)
266 for (std::size_t j = 0; j < n; ++j) integral(i, j) = e(i, 2 * n + j);
267 std::vector<Matrix<double>> scaled_marks;
268 for (std::size_t k = 0; k < marks; ++k) scaled_marks.push_back(scaled(stream[k + 2], duration));
269 std::vector<double> means(marks, 0.0);
270 for (std::size_t k = 0; k < marks; ++k) means[k] = rates[k] * duration;
271 Matrix<double> covariance(marks, marks, 0.0);
272 for (std::size_t i = 0; i < marks; ++i)
273 for (std::size_t j = 0; j < marks; ++j) {
274 const double lr = total(row_times(row_times(row_times(pi, scaled_marks[i]), integral), scaled_marks[j]));
275 const double rl = total(row_times(row_times(row_times(pi, scaled_marks[j]), integral), scaled_marks[i]));
276 covariance(i, j) = lr + rl - means[i] * means[j] + (i == j ? means[i] : 0.0);
277 }
278 return {rates, covariance};
279}
280
281} // namespace detail
282
283/**
284 * Fit offered marked streams and enforce a finite-time counting budget
285 * (`fit_traffic`). Every check is RELATIVE to the magnitude of its reference
286 * quantity, per mark and per covariance entry, with floors at the smallest
287 * normal double (`std::numeric_limits<double>::min()`) only to keep an all-zero
288 * reference from dividing by zero: a floor of 1 would make the tolerance
289 * absolute for low-rate streams. Streams whose largest rate lies outside
290 * [1e-8, 1e8] are fitted on a normalised time scale and scaled back.
291 */
292inline TrafficFit fit_traffic(const Traffic& traffic, const std::optional<MarkedMatrices>& original,
293 const std::string& method, const std::string& compress_method, double fit_time,
294 double fit_tolerance, const TrafficFitBackend& backend = TrafficFitBackend()) {
295 auto refuse = [](const std::string& message) { return detail::mam_refusal("Model", message); };
296 if (!std::isfinite(fit_time) || fit_time <= 0) throw InputError("lal_fit_time must be finite and positive");
297 if (!std::isfinite(fit_tolerance) || fit_tolerance < 0)
298 throw InputError("lal_fit_tolerance must be finite and nonnegative");
299 if (method == "compress" && !original)
300 throw refuse("Compression requires the original superposed MMAP within its storage limit.");
301 const double tiny = detail::tiny();
302 for (const MarkedMatrices& stream : traffic.streams) {
303 if (detail::strong_count_positive(detail::add(stream[0], stream[1])) != 1)
304 throw refuse("Traffic fitting requires irreducible offered phase processes.");
305 bool any = false;
306 for (std::size_t k = 0; k < stream[1].size(); ++k)
307 if (stream[1][k] > 0) any = true;
308 if (!any) throw refuse("Traffic fitting does not support an entirely zero-rate source.");
309 }
310 double largest_rate = 0.0;
311 for (const MarkedMatrices& stream : traffic.streams) largest_rate = std::max(largest_rate, detail::marked_max_abs(stream));
312 const double factor = (largest_rate < 1e-8 || largest_rate > 1e8) ? largest_rate : 1.0;
313 if (!std::isfinite(factor) || factor <= 0 || !std::isfinite(fit_time * factor) || fit_time * factor == 0)
314 throw refuse("Traffic fitting time scale exceeds numerical limits.");
315 auto divided = [](const MarkedMatrices& m, double f) {
316 MarkedMatrices out;
317 for (const Matrix<double>& x : m) out.push_back(detail::scaled(x, 1.0 / f));
318 return out;
319 };
320 auto multiplied = [](const MarkedMatrices& m, double f) {
321 MarkedMatrices out;
322 for (const Matrix<double>& x : m) out.push_back(detail::scaled(x, f));
323 return out;
324 };
325 std::vector<MarkedMatrices> fitting_streams;
326 for (const MarkedMatrices& stream : traffic.streams) fitting_streams.push_back(divided(stream, factor));
327 const double fitting_time = fit_time * factor;
328 MarkedMatrices fitted;
329 if (method == "compress") {
330 const MarkedMatrices scaled_original = divided(*original, factor);
331 if (backend.compress)
332 fitted = backend.compress(scaled_original, compress_method);
333 else
334 fitted = detail::from_mmap(detail::compress_mmap(detail::to_mmap(scaled_original), compress_method));
335 } else if (method == "interpos") {
336 if (traffic.streams.size() < 2) throw refuse("Interposition requires at least two independent marked sources.");
337 // A one- or two-phase M3PP is already in the required family; avoid an
338 // unnecessary, potentially ill-conditioned moment fit.
339 auto fit_source = [&](const MarkedMatrices& source) -> MarkedMatrices {
340 bool diagonal = true;
341 for (std::size_t k = 2; k < source.size(); ++k) {
342 const double scale = std::max(tiny, detail::max_abs(source[k]));
343 for (std::size_t i = 0; i < source[k].rows(); ++i)
344 for (std::size_t j = 0; j < source[k].cols(); ++j)
345 if (i != j && std::fabs(source[k](i, j)) > 1e-12 * scale) diagonal = false;
346 }
347 if (source[0].rows() <= 2 && diagonal) return source;
348 const double source_scale = detail::marked_max_abs(source);
349 const double local_factor = (source_scale < 1e-8 || source_scale > 1e8) ? source_scale : 1.0;
350 const double local_time = fitting_time * local_factor;
351 if (!std::isfinite(local_time) || local_time <= 0)
352 throw refuse("An interposition source has an unsupported time scale.");
353 const MarkedMatrices normalized = divided(source, local_factor);
354 const MarkedMatrices fitted_source =
355 backend.m3pp_fit ? backend.m3pp_fit(normalized, std::string("exact_delta"), local_time, 1e6)
356 : detail::from_mmap(mam::m3pp2m_fitc_theoretical(detail::to_mmap(normalized),
357 std::string("exact_delta"), local_time, 1e6));
358 return multiplied(fitted_source, local_factor);
359 };
360 std::vector<MarkedMatrices> fitted_sources;
361 for (const MarkedMatrices& s : fitting_streams) fitted_sources.push_back(fit_source(s));
362 for (const MarkedMatrices& s : fitted_sources)
363 if (s[0].rows() != 1 && s[0].rows() != 2)
364 throw refuse("Interposition requires one- or two-phase fitted source processes.");
365 // Interleaving differences successive upward rates. Order the inputs to
366 // make those differences nonnegative where possible, then restore the
367 // declared mark order in the result.
368 std::vector<std::size_t> order, poisson;
369 for (std::size_t i = 0; i < fitted_sources.size(); ++i)
370 (fitted_sources[i][0].rows() == 2 ? order : poisson).push_back(i);
371 std::stable_sort(order.begin(), order.end(), [&](std::size_t a, std::size_t b) {
372 return fitted_sources[a][0](0, 1) > fitted_sources[b][0](0, 1);
373 });
374 // A Poisson flow needs no additional interleaving level: it fires at
375 // every phase, so it adds no unreachable levels.
376 MarkedMatrices combined;
377 if (!order.empty()) {
378 std::vector<mam::Mmap<double>> parts;
379 for (std::size_t i : order) parts.push_back(detail::to_mmap(fitted_sources[i]));
380 combined = detail::from_mmap(mam::m3pp2m_interleave(parts));
381 } else {
382 combined = {Matrix<double>(1, 1, 0.0), Matrix<double>(1, 1, 0.0)};
383 }
384 std::map<std::size_t, std::vector<Matrix<double>>> slices;
385 std::size_t offset = 2;
386 for (std::size_t i : order) {
387 const std::size_t count = fitted_sources[i].size() - 2;
388 slices[i] = std::vector<Matrix<double>>(combined.begin() + static_cast<long>(offset),
389 combined.begin() + static_cast<long>(offset + count));
390 offset += count;
391 }
392 const std::size_t n = combined[0].rows();
393 double added_rate = 0.0;
394 for (std::size_t i : poisson) {
395 for (std::size_t k = 2; k < fitted_sources[i].size(); ++k) {
396 const double rate = fitted_sources[i][k](0, 0);
397 slices[i].push_back(detail::scaled(eye<double>(n), rate));
398 added_rate += rate;
399 }
400 }
401 fitted = {detail::add(combined[0], detail::scaled(eye<double>(n), -added_rate)),
402 detail::add(combined[1], detail::scaled(eye<double>(n), added_rate))};
403 for (std::size_t i = 0; i < fitted_sources.size(); ++i)
404 for (const Matrix<double>& b : slices[i]) fitted.push_back(b);
405 } else {
406 throw InputError("Unknown LAL traffic fitting method");
407 }
408 fitted = multiplied(fitted, factor);
409 const double fitted_scale = std::max(tiny, detail::marked_max_abs(fitted));
410 bool finite = true;
411 for (const Matrix<double>& x : fitted) finite = finite && detail::all_finite(x);
412 if (fitted.size() != traffic.marks.size() + 2 || !finite || !detail::feasible(fitted, 1e-10 * fitted_scale))
413 throw refuse("The fitted marked process is infeasible or lost mark matrices.");
414 const std::size_t n = fitted[0].rows();
415 bool negative = false;
416 for (std::size_t i = 0; i < n; ++i)
417 for (std::size_t j = 0; j < n; ++j) {
418 if (i != j && fitted[0](i, j) < 0) negative = true;
419 if (i == j && fitted[0](i, j) > 0) negative = true;
420 }
421 for (std::size_t k = 1; k < fitted.size(); ++k)
422 for (std::size_t e = 0; e < fitted[k].size(); ++e)
423 if (fitted[k][e] < 0) negative = true;
424 if (negative) throw refuse("The fitted marked process contains negative transition rates.");
425 for (std::size_t i = 0; i < n; ++i) {
426 double row = 0.0, row_scale = 0.0;
427 for (std::size_t j = 0; j < n; ++j) {
428 row += fitted[0](i, j) + fitted[1](i, j);
429 row_scale += std::fabs(fitted[0](i, j)) + std::fabs(fitted[1](i, j));
430 }
431 if (std::fabs(row) > 1e-10 * std::max(row_scale, tiny))
432 throw refuse("The fitted marked process has invalid generator row sums.");
433 }
434 for (std::size_t e = 0; e < fitted[1].size(); ++e) {
435 double mark_sum = 0.0;
436 for (std::size_t k = 2; k < fitted.size(); ++k) mark_sum += fitted[k][e];
437 const double scale = std::max(std::max(std::fabs(fitted[1][e]), std::fabs(mark_sum)), tiny);
438 if (std::fabs(fitted[1][e] - mark_sum) > 1e-10 * scale)
439 throw refuse("The fitted aggregate event matrix does not match its marks.");
440 }
441 if (detail::strong_count_positive(detail::add(fitted[0], fitted[1])) != 1)
442 throw refuse("The fitted offered phase process is reducible.");
443 TrafficFit out;
444 std::vector<Matrix<double>> covs;
445 for (const MarkedMatrices& stream : traffic.streams) {
446 const auto stats = detail::count_statistics(stream, fit_time);
447 out.before_rate.insert(out.before_rate.end(), stats.first.begin(), stats.first.end());
448 covs.push_back(stats.second);
449 }
450 out.before_cov = detail::block_diag(covs);
451 const auto after = detail::count_statistics(fitted, fit_time);
452 out.after_rate = after.first;
453 out.after_cov = after.second;
454 bool ok = detail::all_finite(out.before_cov) && detail::all_finite(out.after_cov);
455 for (double r : out.before_rate) ok = ok && std::isfinite(r);
456 for (double r : out.after_rate) ok = ok && std::isfinite(r);
457 if (!ok) throw refuse("Traffic fit counting statistics are not finite.");
458 for (const Matrix<double>* cov : {&out.before_cov, &out.after_cov}) {
459 const double scale = std::max(tiny, detail::max_abs(*cov));
460 const std::size_t m = cov->rows();
461 bool bad = false;
462 Matrix<double> sym(m, m, 0.0);
463 for (std::size_t i = 0; i < m; ++i)
464 for (std::size_t j = 0; j < m; ++j) {
465 if (std::fabs((*cov)(i, j) - (*cov)(j, i)) > 1e-8 * scale) bad = true;
466 sym(i, j) = ((*cov)(i, j) + (*cov)(j, i)) / 2.0;
467 }
468 if (!bad && m > 0) {
469 const std::vector<double> ev = detail::eigvalsh(sym);
470 if (ev.front() < -1e-8 * scale) bad = true;
471 }
472 if (bad) throw refuse("Traffic fit count covariance is numerically invalid.");
473 }
474 if (out.after_rate.size() != out.before_rate.size() || out.after_cov.rows() != out.before_cov.rows())
475 throw refuse("The fitted marked process is infeasible or lost mark matrices.");
476 // Check every mark, rather than allowing a large class to mask the loss of
477 // a low-rate one. Cross-covariances are scaled by the geometric mean of the
478 // two reference variances, which is meaningful when the original
479 // cross-covariance is zero.
480 double max_rate = 0.0;
481 for (double r : out.before_rate) max_rate = std::max(max_rate, std::fabs(r));
482 const double rate_floor = std::max(tiny, max_rate * 1e-12);
483 out.rate_error = 0.0;
484 for (std::size_t k = 0; k < out.before_rate.size(); ++k) {
485 const double scale = std::fabs(out.before_rate[k]) > 0 ? std::fabs(out.before_rate[k]) : rate_floor;
486 out.rate_error = std::max(out.rate_error, std::fabs(out.after_rate[k] - out.before_rate[k]) / scale);
487 }
488 const std::size_t m = out.before_cov.rows();
489 const double covariance_floor = std::max(tiny, detail::max_abs(out.before_cov) * 1e-12);
490 std::vector<double> sd(m, 0.0);
491 for (std::size_t i = 0; i < m; ++i) sd[i] = std::sqrt(std::max(out.before_cov(i, i), 0.0));
492 out.covariance_error = 0.0;
493 for (std::size_t i = 0; i < m; ++i)
494 for (std::size_t j = 0; j < m; ++j) {
495 const double s = sd[i] * sd[j];
496 const double scale = s > 0 ? s : covariance_floor;
497 out.covariance_error =
498 std::max(out.covariance_error, std::fabs(out.after_cov(i, j) - out.before_cov(i, j)) / scale);
499 }
500 if (std::max(out.rate_error, out.covariance_error) > fit_tolerance)
501 throw refuse("Traffic fit error exceeds lal_fit_tolerance: rate=" + detail::fmt_g(out.rate_error) +
502 ", count covariance=" + detail::fmt_g(out.covariance_error) + ".");
503 out.fitted = fitted;
504 return out;
505}
506
507} // namespace lal
508} // namespace line
509
510#endif
InputError(const std::string &what)
Definition error.h:105
std::size_t rows() const
Definition matrix.h:89
Matrix exponential by scaling and squaring with a diagonal Pade approximant.
LUMPED interleaving of several M3PP(2, m), and the two fitters built on it (matlab/lib/m3a/m3a/m3pp/m...
LINE Agent Language (LAL): shared types and numerical helpers of the SolverMAM LAL paths (python/line...
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Compression of a marked MAP into a smaller representation, and the two M3A primitives it is built fro...
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
Marked MAP statistics: embedded chains, class-transition probabilities, forward and cross moments,...
Definition ag.h:49
std::vector< Matrix< double > > MarkedMatrices
A marked process as Python's list [D0, D1, D1_1, ..., D1_K].
Definition mam_common.h:54
std::pair< std::string, std::string > Endpoint
(agent, action).
Definition core.h:139
TrafficFit fit_traffic(const Traffic &traffic, const std::optional< MarkedMatrices > &original, const std::string &method, const std::string &compress_method, double fit_time, double fit_tolerance, const TrafficFitBackend &backend=TrafficFitBackend())
Fit offered marked streams and enforce a finite-time counting budget (fit_traffic).
std::optional< Traffic > offered_traffic(const Model &snapshot, const std::string &level_name, bool materialize=true)
Exact superposed offered MMAP and mark provenance, or nullopt (offered_traffic).
bool in(const std::string &s, std::initializer_list< const char * > set)
Mmap< T > mmap_normalize(const Mmap< T > &in)
Clamp negative off-diagonal and per-class entries to zero and rebuild D1 and the diagonal of D0 from ...
MmapCompressMethod
The compression methods of mmap_compress.m.
Mmap< T > m3pp2m_interleave(const std::vector< Mmap< T > > &parts)
Interleave L M3PP(2, m_i) into one M3PP of order L + 1 whose class list is the concatenation of their...
bool mmap_isfeasible_tol(const Mmap< T > &m, const T &tol)
Feasibility of a marked MAP WITHIN A TOLERANCE, the semantics of matlab/lib/m3a/m3a/mmap/mmap_isfeasi...
Mmap< T > mmap_compress(const Mmap< T > &in, MmapCompressMethod method)
Compress an MMAP (mmap_compress.m).
Mmap< T > mmap_super(const Mmap< T > &a, const Mmap< T > &b)
Superposition of two MMAPs: the phase process is the product chain, and the class list of the result ...
Definition mmap_lambda.h:88
std::vector< T > mmap_pc(const Mmap< T > &m)
Class probabilities seen by an arriving job, pc = pie (-D0)^-1 D1^(c) e.
T map_lambda(const Map< T > &m)
Stationary arrival rate, lambda = pi D1 e.
Definition map_moment.h:79
Mmap< T > m3pp2m_fitc_theoretical(const Mmap< T > &mm, const std::string &method, const T &t, const T &tinf)
Fit the counting characteristics of a GIVEN MMAP with an M3PP(2, m).
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Matrix< T > eye(std::size_t n)
Identity of order n.
Definition linalg.h:28
Matrix< T > expm(const Matrix< T > &A)
Matrix exponential exp(A).
Definition expm.h:141
The immutable agent record (core.Agent).
Definition core.h:284
std::string name
Definition core.h:285
std::vector< Transition > transitions
Definition core.h:288
std::vector< std::string > states
Definition core.h:286
std::size_t state_index(const std::string &s) const
Zero-based index of a local state, or npos.
Definition core.h:302
A composition snapshot (core.Model): agents in declaration order plus synchronisations.
Definition core.h:310
std::vector< MinSync > min_syncs
Definition core.h:313
std::vector< Sync > syncs
Definition core.h:312
std::vector< AgentRecord > agents
Definition core.h:311
const AgentRecord & agent(const std::string &name) const
Definition core.h:324
An ordinary synchronisation: one active endpoint, hand passives and injected node arrivals.
Definition core.h:250
Endpoint active
Definition core.h:251
std::vector< Endpoint > passive
Definition core.h:252
Replaceable numerical kernels of fit_traffic, so a test can intercept them as the Python tests monkey...
std::function< MarkedMatrices(const MarkedMatrices &, const std::string &, double, double)> m3pp_fit
std::function< MarkedMatrices(const MarkedMatrices &, const std::string &)> compress
Output of fit_traffic.
MarkedMatrices fitted
Matrix< double > before_cov
Matrix< double > after_cov
std::vector< double > before_rate
std::vector< double > after_rate
The traffic dictionary of a MAM result (result.traffic).
Definition mam_common.h:67
std::string method
Definition mam_common.h:69
std::string kind
Definition mam_common.h:68
std::vector< std::string > marks
Definition mam_common.h:74
std::optional< MarkedMatrices > matrices
Definition mam_common.h:77
std::vector< std::string > targets
Definition mam_common.h:75
std::vector< std::string > sources
Definition mam_common.h:73
std::optional< std::string > blocking
Definition mam_common.h:78
std::vector< MarkedMatrices > streams
Definition mam_common.h:76
One labelled local transition source --(action, value)--> target.
Definition core.h:111
An MMAP: the underlying MAP plus the per-class arrival matrices.
Definition mmap_lambda.h:45