LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
mam_reduced.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_REDUCED_H
6#define LINE_LANG_LAL_MAM_REDUCED_H
7
8/**
9 * @file
10 * @ingroup line_lang
11 * LINE Agent Language (LAL): the fitted-traffic QBD (`lal_traffic.solve_reduced`).
12 *
13 * The independent offered streams are fitted (compression or interposition),
14 * replaced by one synthetic MMAP agent `_MAMTraffic`, and the reduced
15 * two-agent composition is solved by the finite or infinite QBD. Original
16 * source phase probabilities and internal transition rates have no generally
17 * valid inverse mapping and are reported as NaN: every row of every source
18 * agent in the state table, every `<source>._phase` row and any label the
19 * reduced composition does not fire in the action table. Callers must treat
20 * these NaNs as "not computed", never as zero; `traffic.unavailable` names them.
21 */
22
23#include <cmath>
24#include <limits>
25#include <map>
26#include <optional>
27#include <set>
28#include <string>
29#include <vector>
30
34
35namespace line {
36namespace lal {
37
38/** Fit the offered streams, then solve the reduced QBD (`solve_reduced`). */
39inline MamResult solve_reduced(const Composition& model, const std::string& level_name, const std::string& method,
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");
46 const Model snapshot = model.model();
47 double phase_order = 1.0, local_bytes = 0.0;
48 std::size_t label_count = 0;
49 for (const AgentRecord& a : snapshot.agents) {
50 if (a.name == level_name) continue;
51 std::set<std::string> labels;
52 for (const Transition& t : a.transitions)
53 if (t.role == Role::Active) labels.insert(t.action);
54 const double n = static_cast<double>(a.states.size());
55 phase_order *= n;
56 label_count += labels.size();
57 local_bytes += n * n * (2.0 + labels.size()) * 8;
58 }
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);
65 if (!traffic)
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;
70 for (const Agent& a : model.agents())
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.");
75 NamedMatrices marks;
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)
80 reduced.synchronize(synthetic.action("mark_" + std::to_string(i)), level_builder->action(traffic->targets[i]));
81 MamResult result = level_builder->unbounded_queue()
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;
88 for (const ActionRow& row : result.action_table.rows) {
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;
92 }
93 ActionTable actions;
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()});
97 }
98 StateTable states;
99 for (const AgentRecord& a : snapshot.agents) {
100 if (a.name == level_name) {
101 for (const StateRow& r : result.state_table.rows)
102 if (r.agent == level_name) states.rows.push_back(r);
103 } else {
104 for (const std::string& s : a.states) states.rows.push_back(StateRow{a.name, s, std::numeric_limits<double>::quiet_NaN()});
105 }
106 }
107 result.action_table = actions;
108 result.state_table = states;
109 result.exact = false;
110 result.truncated = model.source_truncated;
111 Traffic t;
112 t.kind = "offered";
113 t.method = method;
114 if (method == "compress") t.compression = compress_method;
115 t.feasible = true;
116 t.exact = false;
117 t.marks = traffic->marks;
118 t.original_matrices = original;
119 t.original_streams = traffic->streams;
120 t.fitted_matrices = fit.fitted;
121 t.fit_time = fit_time;
122 t.fit_tolerance = fit_tolerance;
124 t.fitted_rates = fit.after_rate;
127 t.rate_error = fit.rate_error;
129 t.unavailable = std::string("Original source phase probabilities and internal action throughputs.");
130 result.traffic = t;
131 for (TableAttrs* at : {&result.state_table.attrs, &result.action_table.attrs}) {
132 at->method = result.method;
133 at->exact = false;
134 at->truncated = result.truncated;
135 at->extra["traffic_method"] = method;
136 at->extra["fit_time"] = fit_time;
137 at->extra["fit_tolerance"] = fit_tolerance;
138 }
139 return result;
140}
141
142} // namespace lal
143} // namespace line
144
145#endif
InputError(const std::string &what)
Definition error.h:105
InternalError(const std::string &what)
Definition error.h:133
A finite-state agent with active, weighted passive and immediate transitions (handle semantics).
Definition builder.h:286
bool unbounded_queue() const
Definition builder.h:337
ActionRef action(const std::string &name) const
Reference an existing action (a transition or a command) for synchronisation.
Definition builder.h:614
A collection of agents and embedded LINE models with atomic synchronisations (Composition).
Definition builder.h:655
Composition & synchronize(const ActionRef &active, const std::vector< ActionRef > &passives)
Synchronise one active action with one or more hand passive actions.
Definition builder.h:798
Model model(const std::vector< Sync > *syncs=nullptr, const std::vector< ExtSync > *ext_syncs=nullptr) const
The current model snapshot (_model).
Definition builder.h:906
const std::vector< Agent > & agents() const
Definition builder.h:944
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...
Definition ag.h:49
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).
Definition mam_qbd.h:109
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).
Definition mam_reduced.h:39
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
Definition builder.h:76
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
One row of an action table: Action, Throughput.
Definition ctmc.h:62
std::vector< ActionRow > rows
Definition ctmc.h:80
TableAttrs attrs
Definition ctmc.h:81
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
The union of Python's LAL MAM result dataclasses.
Definition mam_common.h:118
std::optional< Traffic > traffic
Definition mam_common.h:126
A composition snapshot (core.Model): agents in declaration order plus synchronisations.
Definition core.h:310
bool has_agent(const std::string &name) const
Definition core.h:323
std::vector< AgentRecord > agents
Definition core.h:311
std::string method
Definition ctmc.h:97
bool truncated
Definition ctmc.h:99
ActionTable action_table
Definition ctmc.h:104
StateTable state_table
Definition ctmc.h:103
One row of a state table: Agent, State, Probability.
Definition ctmc.h:55
std::string agent
Definition ctmc.h:56
TableAttrs attrs
Definition ctmc.h:69
std::vector< StateRow > rows
Definition ctmc.h:68
The attrs of a LAL result table.
Definition ctmc.h:46
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::optional< MarkedMatrices > fitted_matrices
Definition mam_common.h:81
std::optional< std::string > compression
Definition mam_common.h:70
std::string kind
Definition mam_common.h:68
std::optional< double > fit_tolerance
Definition mam_common.h:83
std::optional< Matrix< double > > original_count_covariance
Definition mam_common.h:86
std::optional< Matrix< double > > fitted_count_covariance
Definition mam_common.h:87
std::vector< std::string > marks
Definition mam_common.h:74
std::vector< MarkedMatrices > original_streams
Definition mam_common.h:80
std::vector< double > fitted_rates
Definition mam_common.h:85
std::optional< double > rate_error
Definition mam_common.h:88
std::optional< MarkedMatrices > original_matrices
Definition mam_common.h:79
std::optional< std::string > unavailable
Definition mam_common.h:90
std::optional< double > fit_time
Definition mam_common.h:82
std::optional< double > count_covariance_error
Definition mam_common.h:89
std::vector< double > original_rates
Definition mam_common.h:84
std::optional< bool > feasible
Definition mam_common.h:72
One labelled local transition source --(action, value)--> target.
Definition core.h:111
std::string action
Definition core.h:113