5#ifndef LINE_LANG_LAL_MAM_REDUCED_H
6#define LINE_LANG_LAL_MAM_REDUCED_H
40 const std::string& compress_method,
double fit_time,
double fit_tolerance,
41 double max_phases,
double max_states,
long max_iter = 100,
double tol = 1e-4) {
42 auto refuse = [](
const std::string& message) {
return detail::mam_refusal(
"Model", message); };
43 if (!std::isfinite(fit_time) || fit_time <= 0)
throw InputError(
"lal_fit_time must be finite and positive");
44 if (!std::isfinite(fit_tolerance) || fit_tolerance < 0)
45 throw InputError(
"lal_fit_tolerance must be finite and nonnegative");
47 double phase_order = 1.0, local_bytes = 0.0;
48 std::size_t label_count = 0;
50 if (a.
name == level_name)
continue;
51 std::set<std::string> labels;
54 const double n =
static_cast<double>(a.
states.size());
56 label_count += labels.size();
57 local_bytes += n * n * (2.0 + labels.size()) * 8;
59 const bool materialize = phase_order * phase_order * (label_count + 2.0) * 8 <= 32.0 * 1024 * 1024;
60 if (method ==
"compress" && !materialize)
61 throw refuse(
"The original offered MMAP exceeds the 32 MiB compression diagnostics limit; use interposition.");
62 if (local_bytes > 32.0 * 1024 * 1024 || label_count > 64)
63 throw refuse(
"The individual offered streams or their mark statistics exceed the fit-diagnostics limit.");
64 const std::optional<Traffic> traffic =
offered_traffic(snapshot, level_name, materialize);
66 throw refuse(
"Traffic fitting requires independent marked sources synchronized only with the chosen level agent.");
67 const std::optional<MarkedMatrices> original = materialize ? traffic->matrices : std::nullopt;
68 const TrafficFit fit =
fit_traffic(*traffic, original, method, compress_method, fit_time, fit_tolerance);
69 const Agent* level_builder =
nullptr;
71 if (a.
name() == level_name) level_builder = &a;
72 if (!level_builder)
throw InternalError(
"solve_reduced: level agent " + level_name +
" is not in the composition");
73 const std::string synthetic_name =
"_MAMTraffic";
74 if (snapshot.
has_agent(synthetic_name))
throw refuse(
"Agent name _MAMTraffic is reserved for reduced traffic analysis.");
76 for (std::size_t i = 0; i < traffic->marks.size(); ++i) marks.push_back({
"mark_" + std::to_string(i), fit.fitted[i + 2]});
77 const Agent synthetic = detail::Records::mmap(synthetic_name, fit.
fitted[0], marks);
78 Composition reduced(std::vector<Agent>{synthetic, *level_builder});
79 for (std::size_t i = 0; i < traffic->targets.size(); ++i)
82 ?
infinite_qbd(reduced, max_phases, max_states, level_name, max_iter, tol)
83 :
finite_qbd(reduced, max_phases, max_states, level_name);
84 std::map<std::string, std::string> label_map;
85 for (std::size_t i = 0; i < traffic->marks.size(); ++i)
86 label_map[synthetic_name +
".mark_" + std::to_string(i)] = traffic->marks[i];
87 std::map<std::string, double> values;
89 if (row.action == synthetic_name +
"._phase")
continue;
90 auto it = label_map.find(row.action);
91 values[it != label_map.end() ? it->second : row.action] = row.throughput;
94 for (
const std::string& label : detail::kernel_labels(snapshot)) {
95 auto it = values.find(label);
96 actions.
rows.push_back(
ActionRow{label, it != values.end() ? it->second : std::numeric_limits<double>::quiet_NaN()});
100 if (a.
name == level_name) {
102 if (r.
agent == level_name) states.
rows.push_back(r);
104 for (
const std::string& s : a.
states) states.
rows.push_back(
StateRow{a.
name, s, std::numeric_limits<double>::quiet_NaN()});
109 result.
exact =
false;
114 if (method ==
"compress") t.
compression = compress_method;
117 t.
marks = traffic->marks;
129 t.
unavailable = std::string(
"Original source phase probabilities and internal action throughputs.");
132 at->method = result.
method;
135 at->extra[
"traffic_method"] = method;
136 at->extra[
"fit_time"] = fit_time;
137 at->extra[
"fit_tolerance"] = fit_tolerance;
InternalError(const std::string &what)
A finite-state agent with active, weighted passive and immediate transitions (handle semantics).
bool unbounded_queue() const
ActionRef action(const std::string &name) const
Reference an existing action (a transition or a command) for synchronisation.
A collection of agents and embedded LINE models with atomic synchronisations (Composition).
Composition & synchronize(const ActionRef &active, const std::vector< ActionRef > &passives)
Synchronise one active action with one or more hand passive actions.
Model model(const std::vector< Sync > *syncs=nullptr, const std::vector< ExtSync > *ext_syncs=nullptr) const
The current model snapshot (_model).
const std::vector< Agent > & agents() const
LINE Agent Language (LAL): matrix-geometric analysis of one structured unbounded queue (solver_mam/la...
LINE Agent Language (LAL): exact finite, level-dependent QBD analysis of a composition (solver_mam/la...
LINE Agent Language (LAL): independent marked arrival proposals of a finite composition and their exp...
MamResult finite_qbd(const Composition &model, double max_phases=64, double max_states=50000, const std::optional< std::string > &level_name=std::nullopt)
Finite QBD solution of a composition (lal_qbd.solve_composition).
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).
MamResult infinite_qbd(const Composition &model, double max_phases=64, double max_states=50000, const std::optional< std::string > &level_name=std::nullopt, long max_iter=100, double tol=1e-4)
Infinite QBD solution of a composition (lal_infinite_qbd.solve_composition).
MamResult solve_reduced(const Composition &model, const std::string &level_name, const std::string &method, const std::string &compress_method, double fit_time, double fit_tolerance, double max_phases, double max_states, long max_iter=100, double tol=1e-4)
Fit the offered streams, then solve the reduced QBD (solve_reduced).
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).
std::vector< std::pair< std::string, Matrix< double > > > NamedMatrices
Conservation laws of a layered queueing network, enumerated from its structure.
One row of an action table: Action, Throughput.
std::vector< ActionRow > rows
The immutable agent record (core.Agent).
std::vector< Transition > transitions
std::vector< std::string > states
The union of Python's LAL MAM result dataclasses.
std::optional< Traffic > traffic
A composition snapshot (core.Model): agents in declaration order plus synchronisations.
bool has_agent(const std::string &name) const
std::vector< AgentRecord > agents
One row of a state table: Agent, State, Probability.
std::vector< StateRow > rows
The attrs of a LAL result table.
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).
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< double > rate_error
std::optional< MarkedMatrices > original_matrices
std::optional< std::string > unavailable
std::optional< double > fit_time
std::optional< double > count_covariance_error
std::vector< double > original_rates
std::optional< bool > feasible
One labelled local transition source --(action, value)--> target.