31 const std::string& traffic_method =
"auto",
const std::string& compress_method =
"exponential",
32 double fit_time = 1.0,
double fit_tolerance = .05,
37 if (!rec.first)
throw detail::mam_refusal(
"Model", rec.second);
39 double source_bytes = 0.0;
40 std::size_t mark_total = 0;
41 for (std::size_t a : m.
sources) {
43 const double n =
static_cast<double>(s.
states.size());
44 source_bytes += n * n * (m.
marks.at(s.
name).size() + 2.0) * 8;
47 if (m.
queues.size() > 100 || mark_total > 64 || source_bytes > 32.0 * 1024 * 1024)
48 throw detail::mam_refusal(
"StateSpace",
"Marked network exceeds its queue or mark limit.");
49 const std::size_t count = m.
queues.size();
50 std::map<std::string, std::size_t> qidx;
51 for (std::size_t i = 0; i < count; ++i) qidx[snapshot.
agents[m.
queues[i]].name] = i;
52 const Matrix<double> routing = detail::map_routing(snapshot, m, qidx);
53 if (detail::spectral_radius_nonneg(routing) >= 1 - 1e-12)
54 throw detail::mam_refusal(
"Stability",
"Marked network routing must be open.");
58 for (std::size_t i = 0; i < count; ++i)
59 for (std::size_t j = 0; j < count; ++j) lu(i, j) = (i == j ? 1.0 : 0.0) - routing(i, j);
60 const std::vector<std::size_t> piv =
lu_factor(lu);
61 for (std::size_t col = 0; col < count; ++col) {
62 std::vector<double> b(count, 0.0);
65 for (std::size_t i = 0; i < count; ++i) visits(i, col) = b[i];
68 throw detail::mam_refusal(
"Numerical",
"Marked network visit equations are singular.");
73 std::vector<std::pair<std::string, Matrix<double>>> marked;
75 std::vector<Stream> streams;
76 std::map<std::string, std::vector<double>> distributions;
77 std::map<std::string, double> action_rates;
78 for (std::size_t a : m.
sources) {
80 const std::vector<std::string>& marks = m.
marks.at(source.
name);
81 const double n =
static_cast<double>(source.
states.size());
82 if (n * n * (marks.size() + 2.0) * 8 > 32.0 * 1024 * 1024)
83 throw detail::mam_refusal(
"StateSpace",
"Marked source exceeds the storage limit.");
84 std::vector<double> phase_reward;
85 const MarkedMatrices stream = detail::source_stream(source, marks, phase_reward);
86 const Matrix<double> generator = detail::add(stream[0], stream[1]);
87 if (detail::strong_count_positive(generator) != 1)
88 throw detail::mam_refusal(
"Model",
"Marked source phases must be irreducible.");
89 const std::vector<double> pi = detail::stationary(generator);
90 distributions[source.
name] = pi;
92 for (std::size_t i = 0; i < pi.size(); ++i) pr += pi[i] * phase_reward[i];
93 action_rates[source.
name +
"._phase"] = pr;
94 Stream s{a, stream[0], {}};
95 for (std::size_t k = 0; k < marks.size(); ++k) {
96 const std::vector<double> rs = detail::row_sums(stream[k + 2]);
98 for (std::size_t i = 0; i < pi.size(); ++i) rate += pi[i] * rs[i];
99 action_rates[source.
name +
"." + marks[k]] = rate;
100 s.marked.push_back({marks[k], stream[k + 2]});
102 streams.push_back(s);
104 std::vector<double> external(count, 0.0);
105 for (
const Stream& s : streams) {
106 const std::string& sname = snapshot.
agents[s.agent].name;
107 for (
const auto& mk : s.marked) external[qidx.at(m.
sync.at(
Endpoint(sname, mk.first)))] += action_rates[sname +
"." + mk.first];
109 std::vector<double> rates(count, 0.0);
110 for (std::size_t j = 0; j < count; ++j)
111 for (std::size_t i = 0; i < count; ++i) rates[j] += visits(i, j) * external[i];
112 for (
double r : rates)
113 if (!std::isfinite(r) || r < -1e-10)
114 throw detail::mam_refusal(
"Numerical",
"Marked network traffic equations produced invalid rates.");
115 for (
double& r : rates) r = std::max(r, 0.0);
116 for (std::size_t i = 0; i < count; ++i)
118 throw detail::mam_refusal(
"Initial",
"A PH station with no arrivals can retain an initial-dependent idle phase.");
119 std::vector<double> means(count);
120 for (std::size_t i = 0; i < count; ++i) {
123 means[i] = it != m.
clocks.end() ? it->second.mean : 1.0 / *q.
queue_info->service_rate;
125 for (std::size_t i = 0; i < count; ++i)
126 if (rates[i] * means[i] >=
static_cast<double>(snapshot.
agents[m.
queues[i]].queue_info->servers) * (1 - 1e-12))
127 throw detail::mam_refusal(
"Stability",
"A marked network queue is unstable or critically loaded.");
129 nlohmann::json native_scales = nlohmann::json::object();
130 nlohmann::json station_fits = nlohmann::json::array();
131 std::vector<std::vector<std::pair<std::string, double>>> feedback_rates;
132 for (std::size_t j = 0; j < count; ++j) {
134 std::vector<MarkedMatrices> inputs;
135 std::vector<std::string> labels;
136 std::vector<std::pair<std::string, double>> station_feedback;
137 for (
const Stream& s : streams) {
138 const std::string& sname = snapshot.
agents[s.agent].name;
139 const std::size_t n = s.d0.rows();
140 std::vector<double> probabilities;
141 for (
const auto& mk : s.marked) probabilities.push_back(visits(qidx.at(m.
sync.at(
Endpoint(sname, mk.first))), j));
142 for (
double p : probabilities)
143 if (p < -1e-10)
throw detail::mam_refusal(
"Numerical",
"Marked network produced a negative visit count.");
144 std::vector<double> excess;
145 double excess_total = 0.0;
146 for (std::size_t k = 0; k < s.marked.size(); ++k) {
147 const double e = std::max(0.0, probabilities[k] - 1) * action_rates[sname +
"." + s.marked[k].first];
151 const std::string label = sname +
"." + s.marked[k].first;
153 for (
auto& kv : station_feedback)
154 if (kv.first == label) {
158 if (!found) station_feedback.push_back({label, e});
161 std::vector<Matrix<double>> kept;
162 bool any_positive =
false;
163 for (std::size_t k = 0; k < s.marked.size(); ++k) {
164 const double p = std::max(0.0, std::min(1.0, probabilities[k]));
166 for (std::size_t i = 0; i < n; ++i) x(i, i) += excess[k];
167 for (std::size_t e = 0; e < x.
size(); ++e)
168 if (x[e] > 0) any_positive =
true;
171 if (!any_positive)
continue;
173 for (std::size_t k = 0; k < s.marked.size(); ++k) {
174 const double p = std::max(0.0, std::min(1.0, probabilities[k]));
175 hidden = detail::add(hidden, detail::scaled(s.marked[k].second, 1 - p));
176 visible = detail::add(visible, kept[k]);
179 for (std::size_t i = 0; i < n; ++i) d0(i, i) -= excess_total;
181 in.insert(in.end(), kept.begin(), kept.end());
182 inputs.push_back(in);
183 for (
const auto& mk : s.marked) labels.push_back(sname +
"." + mk.first);
185 feedback_rates.push_back(station_feedback);
187 const long servers = queue.
queue_info->servers;
188 if (inputs.empty() || rates[j] <= 0) {
189 station_fits.push_back(
nullptr);
190 native_scales[queue.
name] =
nullptr;
193 for (
const MarkedMatrices& in : inputs) order *=
static_cast<double>(in[0].rows());
194 std::string
selected = traffic_method;
196 selected = order <= std::min(64.0, max_phases) ?
"exact" : (inputs.size() > 1 ?
"interpos" :
"compress");
197 const bool materialize = order * order * (labels.size() + 2.0) * 8 <= 32.0 * 1024 * 1024;
198 std::optional<MarkedMatrices> aggregate;
201 for (std::size_t k = 1; k < inputs.size(); ++k) combined =
mam::mmap_super(combined, detail::to_mmap(inputs[k]));
202 aggregate = detail::from_mmap(combined);
204 nlohmann::json fit =
nullptr;
205 double fitted_rate = rates[j];
209 description.
marks = labels;
211 fit = nlohmann::json::object();
213 fit[
"marks"] = labels;
216 fit[
"original_count_covariance"] = detail::matrix_json(f.
before_cov);
217 fit[
"fitted_count_covariance"] = detail::matrix_json(f.
after_cov);
220 fit[
"fit_time"] = fit_time;
221 fit[
"fit_tolerance"] = fit_tolerance;
225 if (!aggregate ||
static_cast<double>((*aggregate)[0].rows()) > max_phases)
226 throw detail::mam_refusal(
"StateSpace",
"A marked station exceeds the phase limit.");
228 if (ck != m.
clocks.end() &&
229 static_cast<double>((*aggregate)[0].rows() * snapshot.
agents[ck->second.agent].states.size()) > max_phases)
230 throw detail::mam_refusal(
"StateSpace",
"A marked MAP/PH station exceeds the phase limit.");
231 if (fitted_rate * means[j] >=
static_cast<double>(servers) * (1 - 1e-12))
232 throw detail::mam_refusal(
"Stability",
"A fitted marked station is unstable.");
233 const double service_scale = ck != m.
clocks.end() ? detail::max_abs(ck->second.T) : *queue.
queue_info->service_rate;
234 const double native_scale = std::max({detail::max_abs((*aggregate)[0]), detail::max_abs((*aggregate)[1]), service_scale});
235 if (!std::isfinite(native_scale) || native_scale <= 0)
236 throw detail::mam_refusal(
"Numerical",
"Marked station has an invalid time scale.");
237 native_scales[queue.
name] = native_scale;
238 std::vector<std::pair<std::optional<std::pair<std::vector<double>,
Matrix<double>>>,
double>> services;
240 services.push_back({std::make_pair(ck->second.alpha, detail::scaled(ck->second.T, 1.0 / native_scale)), 0.0});
242 services.push_back({std::nullopt, *queue.
queue_info->service_rate / native_scale});
243 const auto native = detail::run_native_dec_source(
244 native_backend, detail::scaled((*aggregate)[0], 1.0 / native_scale), detail::scaled((*aggregate)[1], 1.0 / native_scale), services,
245 std::vector<long>{servers},
Matrix<double>(1, 1, 0.0), 0,
"LAL_Marked_Station",
"Queue", tol, max_iter, max_phases);
246 qlen = native.first[0];
247 if (!std::isfinite(qlen) || qlen < 0)
248 throw detail::mam_refusal(
"Numerical",
"Marked station decomposition returned invalid queue length.");
249 const double native_throughput = native.second[0] * native_scale;
250 const double throughput_scale = std::max(std::fabs(fitted_rate), detail::tiny());
251 if (!std::isfinite(native_throughput) || std::fabs(native_throughput - fitted_rate) > 1e-6 * throughput_scale)
252 throw detail::mam_refusal(
"Numerical",
"Native MAM station did not preserve the fitted traffic rate.");
253 station_fits.push_back(fit);
255 const double util = rates[j] * means[j] /
static_cast<double>(servers);
256 const double busy = servers == 1 ?
util : std::numeric_limits<double>::quiet_NaN();
257 const double response = rates[j] > 0 ? qlen / rates[j] : std::numeric_limits<double>::quiet_NaN();
261 detail::clock_rows_and_rates(snapshot, m, rates, qidx, distributions, action_rates, res.
state_table);
262 for (
const std::string& label : detail::active_labels(snapshot))
266 at->method =
"marked.thinning";
268 at->truncated = truncated;
270 double residual = 0.0;
271 for (std::size_t i = 0; i < count; ++i) {
272 double r = rates[i] - external[i];
273 for (std::size_t k = 0; k < count; ++k) r -= routing(k, i) * rates[k];
274 residual = std::max(residual, std::fabs(r));
276 res.
method =
"marked.thinning";
282 "Visit thinning ignores queue-induced changes to source correlations and network delays; "
283 "visits beyond the first are approximated as independent Poisson events.");
284 detail::clock_fidelity(snapshot, m, res);
285 for (std::size_t i = 0; i < count; ++i)
286 if (snapshot.
agents[m.
queues[i]].queue_info->servers > 1)
290 nlohmann::json excess = nlohmann::json::object();
291 std::vector<std::string> excess_order;
293 nlohmann::json feedback_json = nlohmann::json::array();
294 for (std::size_t j = 0; j < count; ++j) {
295 nlohmann::json station = nlohmann::json::object();
296 for (
const auto& kv : feedback_rates[j]) station[kv.first] = kv.second;
297 feedback_json.push_back(station);
298 if (feedback_rates[j].empty())
continue;
299 const std::string& qname = snapshot.
agents[m.
queues[j]].name;
300 excess[qname] = station;
301 excess_order.push_back(qname);
302 for (
const auto& kv : feedback_rates[j]) total += kv.second;
305 if (!excess_order.empty()) {
307 std::snprintf(buf,
sizeof(buf),
"%.6g", total);
309 for (std::size_t k = 0; k < excess_order.size(); ++k) joined += (k ?
", " :
"") + excess_order[k];
310 *res.
approximation += std::string(
" Poissonised visit excess (rate ") + buf +
" in total) at: " + joined +
".";
315 res.
extra[
"routing"] = detail::matrix_json(routing);
316 res.
extra[
"queue_rates"] = rates;
319 t.
method =
"marked.thinning";
321 t.
extra[
"visits"] = detail::matrix_json(visits);
322 std::vector<std::string> stations;
323 for (std::size_t q : m.
queues) stations.push_back(snapshot.
agents[q].name);
324 t.
extra[
"stations"] = stations;
325 t.
extra[
"station_fits"] = station_fits;
326 t.
extra[
"feedback_poisson_rates"] = feedback_json;
327 t.
extra[
"poissonised_visit_excess"] = excess;