LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
mam_parallel_qbd.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_PARALLEL_QBD_H
6#define LINE_LANG_LAL_MAM_PARALLEL_QBD_H
7
8/**
9 * @file
10 * @ingroup line_lang
11 * LINE Agent Language (LAL): exact marginal QBDs for independent stations
12 * with shared marked sources (python/line_solver/solvers/solver_mam/
13 * lal_parallel_qbd.py, method `parallelqbd`).
14 *
15 * Each queue is solved as its own infinite QBD over the background agents,
16 * the queue and its PH service clock; the shared background marginals and
17 * rates must agree across those QBDs. The joint queue-state distribution is
18 * not available.
19 */
20
21#include <cmath>
22#include <map>
23#include <set>
24#include <string>
25#include <vector>
26
28
29namespace line {
30namespace lal {
31
32inline MamResult parallel_qbd(const Composition& model, double max_phases, double max_states, long max_iter,
33 double tol) {
34 const Model snapshot = model.model();
35 validate_model(snapshot, true);
36 const auto match = recognize_parallel(snapshot);
37 if (!match.first) throw detail::mam_refusal("Model", match.second);
38 const MapMatch& m = *match.first;
39 if (m.queues.size() > 100) throw detail::mam_refusal("StateSpace", "Parallel QBD exceeds 100 queues.");
40 std::map<std::string, Agent> builders;
41 for (const Agent& a : model.agents()) builders.emplace(a.name(), a);
42 std::map<std::string, std::vector<StateRow>> state_frames;
43 std::map<std::string, double> action_values;
44 QueueTable queue_table;
45 double residual = 0.0;
46 for (std::size_t qi : m.queues) {
47 const AgentRecord& queue = snapshot.agents[qi];
48 std::set<std::string> names;
49 for (std::size_t s : m.sources) names.insert(snapshot.agents[s].name);
50 names.insert(queue.name);
51 auto clock = m.clocks.find(queue.name);
52 if (clock != m.clocks.end()) names.insert(snapshot.agents[clock->second.agent].name);
53 std::vector<Agent> members;
54 for (const AgentRecord& a : snapshot.agents)
55 if (names.count(a.name)) members.push_back(builders.at(a.name));
56 Composition local(members);
57 for (const Sync& s : snapshot.syncs) {
58 std::vector<ActionRef> partners;
59 for (const Endpoint& e : s.passive)
60 if (names.count(e.first)) partners.push_back(builders.at(e.first).action(e.second));
61 if (names.count(s.active.first) && !partners.empty())
62 local.synchronize(builders.at(s.active.first).action(s.active.second), partners);
63 }
64 for (const MinSync& s : snapshot.min_syncs) {
65 bool inside = true;
66 for (const Endpoint& e : s.active) inside = inside && names.count(e.first) > 0;
67 if (!inside) continue;
68 std::vector<ActionRef> actives, partners;
69 for (const Endpoint& e : s.active) actives.push_back(builders.at(e.first).action(e.second));
70 for (const Endpoint& e : s.passive)
71 if (names.count(e.first)) partners.push_back(builders.at(e.first).action(e.second));
72 local.synchronize_min(actives, partners);
73 }
74 MAMOptions o;
75 o.method = "infiniteqbd";
76 o.lal_traffic_method = "exact";
77 o.lal_max_phases = max_phases;
78 o.lal_max_states = max_states;
79 o.max_iter = max_iter;
80 o.tol = tol;
81 SolverMAM solver(local, o);
82 const MamResult& result = solver.getMAMResult();
83 residual = std::max(residual, result.residual);
84 // groupby('Agent', sort=False): groups in order of first appearance
85 std::vector<std::string> order;
86 std::map<std::string, std::vector<StateRow>> groups;
87 for (const StateRow& row : result.state_table.rows) {
88 if (!groups.count(row.agent)) order.push_back(row.agent);
89 groups[row.agent].push_back(row);
90 }
91 for (const std::string& name : order) {
92 const std::vector<StateRow>& group = groups.at(name);
93 auto it = state_frames.find(name);
94 if (it == state_frames.end()) {
95 state_frames.emplace(name, group);
96 continue;
97 }
98 bool close = it->second.size() == group.size();
99 for (std::size_t k = 0; close && k < group.size(); ++k)
100 close = std::fabs(it->second[k].probability - group[k].probability) <=
101 1e-10 + 1e-8 * std::fabs(group[k].probability);
102 if (!close) throw detail::mam_refusal("Numerical", "Shared source marginals disagree across QBDs.");
103 }
104 for (const ActionRow& row : result.action_table.rows) {
105 auto it = action_values.find(row.action);
106 if (it != action_values.end() &&
107 !(std::fabs(it->second - row.throughput) <= 1e-10 + 1e-8 * std::fabs(row.throughput)))
108 throw detail::mam_refusal("Numerical", "Shared source rates disagree across QBDs.");
109 action_values[row.action] = row.throughput;
110 }
111 const QueueTable q = solver.getQueueTable();
112 queue_table.rows.insert(queue_table.rows.end(), q.rows.begin(), q.rows.end());
113 }
114 MamResult out;
115 for (const AgentRecord& a : snapshot.agents) {
116 const std::vector<StateRow>& rows = state_frames.at(a.name);
117 out.state_table.rows.insert(out.state_table.rows.end(), rows.begin(), rows.end());
118 }
119 for (const std::string& label : detail::kernel_labels(snapshot)) {
120 auto it = action_values.find(label);
121 if (it == action_values.end()) throw value_error("No parallel QBD throughput for action " + label);
122 out.action_table.rows.push_back(ActionRow{label, it->second});
123 }
124 const bool truncated = model.source_truncated;
125 for (TableAttrs* attrs : {&out.state_table.attrs, &out.action_table.attrs, &queue_table.attrs}) {
126 attrs->method = "parallelqbd";
127 attrs->exact = !truncated;
128 attrs->truncated = truncated;
129 attrs->extra["joint_queue_distribution"] = "unavailable";
130 }
131 out.queue_measures = queue_table;
132 out.residual = residual;
133 out.truncated = truncated;
134 out.exact = !truncated;
135 out.method = "parallelqbd";
136 out.extra["unavailable"] = {"joint queue-state distribution"};
137 out.extra["number_of_qbds"] = m.queues.size();
138 return out;
139}
140
141} // namespace lal
142} // namespace line
143
144#endif // LINE_LANG_LAL_MAM_PARALLEL_QBD_H
A finite-state agent with active, weighted passive and immediate transitions (handle semantics).
Definition builder.h:286
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
Composition & synchronize_min(const std::vector< ActionRef > &actives, const std::vector< ActionRef > &passives=std::vector< ActionRef >())
Synchronise active actions at the minimum enabled total rate.
Definition builder.h:871
const std::vector< Agent > & agents() const
Definition builder.h:944
SolverMAM on a LAL composition.
Definition solver_mam.h:231
QueueTable getQueueTable()
Queue measures of unchanged structured queue declarations (getQueueTable).
Definition solver_mam.h:514
const MamResult & getMAMResult()
The result container, solving first if needed (getMAMResult).
Definition solver_mam.h:761
Definition ag.h:49
std::pair< std::optional< MapMatch >, std::string > recognize_parallel(const Model &snapshot)
Recognise independent stations with shared marked sources (lal_parallel_qbd.recognize).
std::pair< std::string, std::string > Endpoint
(agent, action).
Definition core.h:139
InputError value_error(const std::string &what)
LINE:input:Unspecified, the C++ spelling of the Python ValueError raised throughout LAL.
Definition core.h:85
void validate_model(const Model &model, bool allow_unbounded=false)
Validate the common agent model used by both frontends (validate_model).
Definition core.h:550
MamResult parallel_qbd(const Composition &model, double max_phases, double max_states, long max_iter, double tol)
Exact marginal QBDs of independent stations sharing marked sources (mam_parallel_qbd....
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
LINE Agent Language (LAL): the SolverMAM binding of a Composition (the _lal_composition branches of p...
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
The options of SolverMAM on a LAL composition (SolverMAMOptions).
Definition solver_mam.h:74
std::string lal_traffic_method
Definition solver_mam.h:89
The union of Python's LAL MAM result dataclasses.
Definition mam_common.h:118
nlohmann::json extra
Definition mam_common.h:148
std::optional< QueueTable > queue_measures
Definition mam_common.h:127
The recognised MAP network (lal_map_network.recognize tuple).
std::vector< std::size_t > queues
std::vector< std::size_t > sources
std::map< std::string, ServiceClock > clocks
keyed by queue name
A minimum-rate synchronisation: at least two active endpoints and optional passive ones.
Definition core.h:258
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
std::vector< QueueRow > rows
Definition mam_common.h:107
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
TableAttrs attrs
Definition ctmc.h:69
std::vector< StateRow > rows
Definition ctmc.h:68
An ordinary synchronisation: one active endpoint, hand passives and injected node arrivals.
Definition core.h:250
The attrs of a LAL result table.
Definition ctmc.h:46