LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
core.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_CORE_H
6#define LINE_LANG_LAL_CORE_H
7
8/**
9 * @file
10 * @ingroup line_lang
11 * LINE Agent Language (LAL): the finite-agent model record, its validation,
12 * the joint-state event kernels and the labelled finite CTMC compiler.
13 *
14 * Port of python/line_solver/lal/core.py (model record, validate_model,
15 * passive_endpoints, events, selected, event_kernel, compile_model,
16 * _complement, StateView, model_features). The semantics are the Python
17 * ones exactly:
18 *
19 * - an ACTIVE transition carries a rate, a PASSIVE one a positive weight that
20 * is normalised within each partner's current state, and an IMMEDIATE one a
21 * positive weight: it fires in zero time;
22 * - rates and weights may be FUNCTIONAL, a `std::function` of the joint-state
23 * view (`StateView`);
24 * - a synchronised active transition is blocked when any passive partner has
25 * no matching transition; unsynchronised active actions are local;
26 * - immediate actions have MAXIMAL PROGRESS: in a state where any immediate
27 * event is enabled only the immediate events of the highest priority fire,
28 * in proportion to their weights, and timed actions wait. Such VANISHING
29 * states are eliminated by stochastic complementation in `compile_model`;
30 * - timed actions of one agent are ordered by `priority`: an action is
31 * disabled while a timed action of the same agent with a higher priority is
32 * enabled;
33 * - `synchronize_min` fires at the minimum of the enabled active totals,
34 * samples each participant proportionally and is labelled by its first
35 * active endpoint;
36 * - an agent may be an EMBEDDED LINE model (`EmbeddedPart`, see embed.h),
37 * whose events carry `NodeEvent` keys `(node, EventType, class | mode)`;
38 * - self-events stay in the event matrices (they are observable) but are
39 * excluded from the generator, whose diagonal is the negated off-diagonal
40 * row sum.
41 *
42 * Joint states are vectors of LOCAL STATE INDICES, one per agent in
43 * declaration order. For an embedded agent the local index is the global state
44 * index of the embedded model. `Compiled::state_names` spells joint states as
45 * the Python tuples of state names.
46 *
47 * Two kernels are exposed. `Events` is the extended kernel every exact path
48 * reads. `EventKernel` is the legacy timed kernel of the decomposition and
49 * matrix-analytic methods; it refuses compositions with immediate, embedded or
50 * multi-label actions, so a path built on it cannot silently misread them.
51 */
52
53#include <algorithm>
54#include <cmath>
55#include <cstddef>
56#include <cstdint>
57#include <functional>
58#include <limits>
59#include <map>
60#include <memory>
61#include <optional>
62#include <set>
63#include <sstream>
64#include <string>
65#include <tuple>
66#include <unordered_map>
67#include <utility>
68#include <vector>
69
71#include "line/util/error.h"
72#include "line/util/lu.h"
73#include "line/util/matrix.h"
74
75namespace line {
76namespace lal {
77
78/** Raised when finite-state compilation exceeds a configured resource limit. */
80 public:
81 explicit StateSpaceLimitError(const std::string& what) : InputError(ErrorKind("StateSpace"), what) {}
82};
83
84/** `LINE:input:Unspecified`, the C++ spelling of the Python ValueError raised throughout LAL. */
85inline InputError value_error(const std::string& what) { return InputError(what); }
86
87/** Transition role: active (rate), passive (weight) or immediate (weight, zero time). */
88enum class Role { Active, Passive, Immediate };
89
90inline const char* role_name(Role r) {
91 return r == Role::Active ? "active" : (r == Role::Passive ? "passive" : "immediate");
92}
93
94class StateView;
95
96/** A functional rate or weight: a function of the joint-state view. */
97typedef std::function<double(const StateView&)> ValueFn;
98
99/** A state reward: a function of the joint-state view (`Composition::setReward`). */
100typedef std::function<double(const StateView&)> RewardFn;
101
102/**
103 * One labelled local transition `source --(action, value)--> target`.
104 *
105 * `label` is empty for the default label `agent.action`; one entry is a user
106 * alias; several entries credit one firing to each (a multi-label event, which
107 * only the embedding of a LINE Workflow creates). `fn`, when set, makes the
108 * value functional and `value` is then ignored. `priority` orders immediate
109 * actions (and the timed actions of one agent).
110 */
112 std::string source;
113 std::string action;
115 double value = 0.0;
116 std::string target;
117 int priority = 0;
118 std::vector<std::string> label;
120 /**
121 * The label is a TUPLE (python `Transition.label` given as a tuple), which
122 * marks a multi-label event even when it holds a single label: the
123 * Workflow embedding labels its moves so.
124 */
125 bool tuple_label = false;
126
127 bool functional() const { return static_cast<bool>(fn); }
128
129 /** Value equality; two functional values compare equal by kind only (a std::function has no identity). */
130 bool operator==(const Transition& o) const {
131 return source == o.source && action == o.action && role == o.role && value == o.value &&
132 target == o.target && priority == o.priority && label == o.label && functional() == o.functional() &&
134 }
135 bool operator!=(const Transition& o) const { return !(*this == o); }
136};
137
138/** `(agent, action)`. */
139typedef std::pair<std::string, std::string> Endpoint;
140
141inline std::string endpoint_label(const Endpoint& e) { return e.first + "." + e.second; }
142
143/** Python's repr of an endpoint tuple, `('agent', 'action')`. */
144inline std::string endpoint_repr(const Endpoint& e) { return "('" + e.first + "', '" + e.second + "')"; }
145
146/**
147 * Key of an embedded node action: `(node, EventType, class | mode)`.
148 *
149 * `model` is the embedding prefix (empty when none was given); `index` is a
150 * class name, a Petri-net mode name, a stage arc `e->h`, or empty for an event
151 * that names no class. Rendered for tables as `[prefix.]Node.EVENT[index]`.
152 */
153struct NodeEvent {
154 std::string model;
155 std::string node;
157 std::optional<std::string> index;
158
159 std::string label() const {
160 std::string out = model.empty() ? std::string() : model + ".";
161 out += node + "." + lang::event_to_text(event);
162 if (index) out += "[" + *index + "]";
163 return out;
164 }
165 bool operator==(const NodeEvent& o) const {
166 return model == o.model && node == o.node && event == o.event && index == o.index;
167 }
168 bool operator!=(const NodeEvent& o) const { return !(*this == o); }
169 bool operator<(const NodeEvent& o) const {
170 return std::make_tuple(model, node, static_cast<int>(event), index) <
171 std::make_tuple(o.model, o.node, static_cast<int>(o.event), o.index);
172 }
173};
174
175/** True when `key` is `pattern`, or `pattern` leaves the index open. */
176inline bool node_event_matches(const NodeEvent& key, const NodeEvent& pattern) {
177 return key.model == pattern.model && key.node == pattern.node && key.event == pattern.event &&
178 (!pattern.index || key.index == pattern.index);
179}
180
181/** One arc of an embedded model: `(target, keys, rate, immediate)` (an immediate arc carries a weight). */
183 std::size_t target = 0;
184 std::vector<NodeEvent> keys;
185 double rate = 0.0;
186 bool immediate = false;
187};
188
189/**
190 * An embedded LINE model seen by the LAL core (Python's `EmbeddedNetwork` /
191 * `EmbeddedEnvironment` duck type).
192 *
193 * The local states of the agent are the global state indices of the embedded
194 * model. The enumeration is LAZY: an implementation builds it on first use and
195 * drops it on `refresh()`, which is why the queries are not const.
196 */
198 public:
199 virtual ~EmbeddedPart() {}
200 /** The part name (the prefix, or the model name). */
201 virtual const std::string& name() const = 0;
202 /** The embedding prefix, empty when none was given. */
203 virtual const std::string& prefix() const = 0;
204 /** The global index of the initial state. */
205 virtual std::size_t initial() = 0;
206 /** Every node key an arc carries, in first-seen order. */
207 virtual const std::vector<NodeEvent>& labels() = 0;
208 /** Arcs leaving global state `s`. */
209 virtual std::vector<EmbeddedArc> outgoing(std::size_t s) = 0;
210 /** True when `s` is a vanishing state of the embedded model. */
211 virtual bool is_vanishing(std::size_t s) = 0;
212 /** True when the embedded model has any vanishing state. */
213 virtual bool has_vanishing() = 0;
214 /** Passive response to an injected arrival: `[(target, probability)]`; empty blocks it. */
215 virtual std::vector<std::pair<std::size_t, double>> inject(std::size_t s, const NodeEvent& key) = 0;
216 /** Refuse a key that a hand agent cannot drive. */
217 virtual void check_injection(const NodeEvent& key) = 0;
218 /** The key of a node action, validated against the model's names. */
219 virtual NodeEvent resolve(const std::string& node, lang::EventType event, const std::optional<std::string>& jobclass,
220 const std::optional<std::string>& mode) = 0;
221 /** Name of the 1-based node index of the model. */
222 virtual std::string node_name(std::size_t node) = 0;
223 /** Name of the 1-based class index of the model. */
224 virtual std::string class_name(std::size_t cls) = 0;
225 /** True when `key` is `pattern`, or `pattern` leaves the index open. */
226 virtual bool matches(const NodeEvent& key, const NodeEvent& pattern) const {
227 return node_event_matches(key, pattern);
228 }
229 /** `[(node name, isf)]` of the stateful node agents (isf is 1-based). */
230 virtual std::vector<std::pair<std::string, std::size_t>> node_states() = 0;
231 /** The state row of stateful node `isf` in global state `s`, as `[v1 v2 ...]`. */
232 virtual std::string node_state_label(std::size_t s, std::size_t isf) = 0;
233 /** The state row of a stateful node in global state `s`. */
234 virtual std::vector<double> node_row(std::size_t s, const std::string& node) = 0;
235 /** Jobs at a station in global state `s`: the total, or those of class `jobclass` when nonempty. */
236 virtual double node_jobs(std::size_t s, const std::string& node, const std::string& jobclass) = 0;
237 /** True for an embedded Environment, whose stage is an agent of its own. */
238 virtual bool has_stages() { return false; }
239 /** The stage of global state `s` (Environment only). */
240 virtual std::size_t stage_of(std::size_t) { return 0; }
241 /** The stage names (Environment only). */
242 virtual std::vector<std::string> stage_names() { return std::vector<std::string>(); }
243 /** Record the classes hand agents inject (names); a change invalidates the enumeration. */
244 virtual void set_injected(const std::set<std::string>&) {}
245 /** Re-read the embedded model on next use. */
246 virtual void refresh() = 0;
247};
248
249/** An ordinary synchronisation: one active endpoint, hand passives and injected node arrivals. */
250struct Sync {
252 std::vector<Endpoint> passive;
253 /** `(embedded part, ARV key)`: the hand action injects an arrival at that node. */
254 std::vector<std::pair<std::string, NodeEvent>> node_passive;
255};
256
257/** A minimum-rate synchronisation: at least two active endpoints and optional passive ones. */
258struct MinSync {
259 std::vector<Endpoint> active;
260 std::vector<Endpoint> passive;
261};
262
263/** Hand passives that join every event of an embedded part carrying a key matching `pattern`. */
264struct ExtSync {
265 std::string agent;
267 std::vector<Endpoint> passive;
268};
269
270/** Structured queue declaration retained while a queue agent is unmodified (`queue_info`). */
271struct QueueInfo {
272 std::optional<long> capacity; ///< nullopt: unbounded
273 long servers = 1;
274 std::vector<std::string> arrivals;
275 std::vector<std::string> departures;
276 std::optional<std::string> service_input;
277 std::vector<std::string> service_inputs;
278 std::optional<std::string> service_phase;
279 std::optional<double> service_rate; ///< nullopt: passive service
280 std::vector<std::pair<std::string, double>> routes;
281};
282
283/** The immutable agent record (`core.Agent`). */
285 std::string name;
286 std::vector<std::string> states;
287 std::string initial;
288 std::vector<Transition> transitions;
289 std::optional<std::vector<double>> initial_distribution;
290 bool unbounded_queue = false;
291 std::optional<long> unbounded_initial;
292 std::optional<QueueInfo> queue_info;
293 /** An embedded LINE model, whose states are global state indices; null for a hand agent. */
294 std::shared_ptr<EmbeddedPart> embedded;
295 /** A discrete-time agent (an embedded MarkovChain). */
296 bool discrete = false;
297 /** Typed-variable agents: the variable names, and the valuation of each local state. */
298 std::vector<std::string> var_names;
299 std::vector<std::vector<long>> var_values;
300
301 /** Zero-based index of a local state, or npos. */
302 std::size_t state_index(const std::string& s) const {
303 for (std::size_t i = 0; i < states.size(); ++i)
304 if (states[i] == s) return i;
305 return static_cast<std::size_t>(-1);
306 }
307};
308
309/** A composition snapshot (`core.Model`): agents in declaration order plus synchronisations. */
310struct Model {
311 std::vector<AgentRecord> agents;
312 std::vector<Sync> syncs;
313 std::vector<MinSync> min_syncs;
314 std::vector<ExtSync> ext_syncs;
315 std::set<std::string> hidden; ///< labels removed from the action tables
316 std::vector<std::pair<std::string, RewardFn>> rewards; ///< declared state rewards, in order
317
318 std::size_t index_of(const std::string& name) const {
319 for (std::size_t i = 0; i < agents.size(); ++i)
320 if (agents[i].name == name) return i;
321 return static_cast<std::size_t>(-1);
322 }
323 bool has_agent(const std::string& name) const { return index_of(name) != static_cast<std::size_t>(-1); }
324 const AgentRecord& agent(const std::string& name) const {
325 const std::size_t i = index_of(name);
326 if (i == static_cast<std::size_t>(-1)) throw value_error("Unknown agent " + name);
327 return agents[i];
328 }
329 AgentRecord& agent(const std::string& name) {
330 const std::size_t i = index_of(name);
331 if (i == static_cast<std::size_t>(-1)) throw value_error("Unknown agent " + name);
332 return agents[i];
333 }
334};
335
336/**
337 * Read-only view of a joint state, for functional rates and rewards (`StateView`).
338 *
339 * `view[agent]` is the local state name of a hand agent (the decimal global
340 * state index of an embedded one); `index(agent)` the local index; `var(agent,
341 * v)` the value of a typed variable; `node(agent, node)` the state row of a
342 * stateful node of an embedded LINE model, as in LINE's state space; and
343 * `jobs(agent, node[, class])` the jobs at one of its stations.
344 */
346 public:
347 StateView(const Model& model, const std::vector<std::size_t>& state) : model_(&model), state_(&state) {}
348
349 bool contains(const std::string& agent) const { return model_->has_agent(agent); }
350
351 std::string operator[](const std::string& agent) const {
352 const std::size_t a = position(agent);
353 const AgentRecord& rec = model_->agents[a];
354 if (rec.embedded) return std::to_string((*state_)[a]);
355 return rec.states[(*state_)[a]];
356 }
357
358 std::size_t index(const std::string& agent) const { return (*state_)[position(agent)]; }
359
360 long var(const std::string& agent, const std::string& variable) const {
361 const std::size_t a = position(agent);
362 const AgentRecord& rec = model_->agents[a];
363 for (std::size_t k = 0; k < rec.var_names.size(); ++k)
364 if (rec.var_names[k] == variable) return rec.var_values[(*state_)[a]][k];
365 throw value_error("agent " + agent + " has no variable " + variable);
366 }
367
368 std::vector<double> node(const std::string& agent, const std::string& node) const {
369 const std::size_t a = position(agent);
370 const AgentRecord& rec = model_->agents[a];
371 if (!rec.embedded) throw value_error(agent + " is not an embedded LINE model");
372 return rec.embedded->node_row((*state_)[a], node);
373 }
374
375 double jobs(const std::string& agent, const std::string& node, const std::string& jobclass = std::string()) const {
376 const std::size_t a = position(agent);
377 const AgentRecord& rec = model_->agents[a];
378 if (!rec.embedded) throw value_error(agent + " is not an embedded LINE model");
379 return rec.embedded->node_jobs((*state_)[a], node, jobclass);
380 }
381
382 const Model& model() const { return *model_; }
383 const std::vector<std::size_t>& state() const { return *state_; }
384
385 private:
386 std::size_t position(const std::string& agent) const {
387 const std::size_t a = model_->index_of(agent);
388 if (a == static_cast<std::size_t>(-1)) throw value_error("Unknown agent " + agent);
389 return a;
390 }
391 const Model* model_;
392 const std::vector<std::size_t>* state_;
393};
394
395/** The compiled finite CTMC (`core.Compiled`). */
396struct Compiled {
397 std::vector<std::vector<std::size_t>> states; ///< joint states as local indices
398 std::size_t initial = 0;
400 std::vector<std::string> labels; ///< event labels, in order
401 std::vector<Matrix<double>> events; ///< one rate matrix per label
402 std::vector<double> initial_distribution;
403 std::vector<std::string> agent_names;
404 std::vector<std::vector<std::string>> agent_states; ///< empty for an embedded agent
405 std::size_t vanishing_eliminated = 0;
406
407 /** The Python tuple of state names for joint state `i` (an embedded agent shows its global index). */
408 std::vector<std::string> state_names(std::size_t i) const {
409 std::vector<std::string> out;
410 for (std::size_t a = 0; a < states[i].size(); ++a)
411 out.push_back(agent_states[a].empty() ? std::to_string(states[i][a]) : agent_states[a][states[i][a]]);
412 return out;
413 }
414 /** The rate matrix of a label; throws for an unknown label. */
415 const Matrix<double>& event(const std::string& label) const {
416 for (std::size_t k = 0; k < labels.size(); ++k)
417 if (labels[k] == label) return events[k];
418 throw value_error("Unknown event label " + label);
419 }
420};
421
422namespace detail {
423
424inline bool is_identifier(const std::string& v) {
425 if (v.empty()) return false;
426 auto alpha = [](char c) { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_'; };
427 if (!alpha(v[0])) return false;
428 for (char c : v)
429 if (!alpha(c) && !(c >= '0' && c <= '9')) return false;
430 return true;
431}
432
433inline std::string repr_double(double x) {
434 std::ostringstream os;
435 os.precision(17);
436 os << x;
437 return os.str();
438}
439
440inline bool contains(const std::vector<Endpoint>& v, const Endpoint& e) {
441 return std::find(v.begin(), v.end(), e) != v.end();
442}
443
444inline std::string join_sorted(const std::set<std::string>& s) {
445 std::string out;
446 for (const std::string& x : s) out += (out.empty() ? "" : ", ") + x;
447 return out;
448}
449
450} // namespace detail
451
452/** Primary label of a transition (its first label when it carries several). */
453inline std::string transition_label(const std::string& agent, const Transition& t) {
454 return t.label.empty() ? agent + "." + t.action : t.label.front();
455}
456
457/** All labels of a transition: a multi-label transition credits one firing to each. */
458inline std::vector<std::string> transition_labels(const std::string& agent, const Transition& t) {
459 return t.label.empty() ? std::vector<std::string>{agent + "." + t.action} : t.label;
460}
461
462/** The value of a transition in a joint state (`_value`). */
463inline double transition_value(const Transition& t, const StateView& view) { return t.fn ? t.fn(view) : t.value; }
464
465/**
466 * A deterministic text signature of a model snapshot (Python's `repr(model)`),
467 * used by the solvers to cache a result against the composition it came from.
468 * A functional value is represented by its callable type; an embedded part by
469 * its identity (Python's default repr of the object), so a refresh alone does
470 * not change the signature.
471 */
472inline std::string signature(const Model& m) {
473 std::ostringstream os;
474 os.precision(17);
475 for (const AgentRecord& a : m.agents) {
476 os << "A[" << a.name << "|";
477 if (a.embedded) {
478 os << "E:" << a.embedded->name() << ":" << static_cast<const void*>(a.embedded.get()) << "]";
479 continue;
480 }
481 for (const std::string& s : a.states) os << s << ",";
482 os << "|" << a.initial << "|";
483 for (const Transition& t : a.transitions) {
484 os << t.source << ":" << t.action << ":" << role_name(t.role) << ":";
485 if (t.fn)
486 os << "fn<" << t.fn.target_type().name() << ">";
487 else
488 os << t.value;
489 os << ":" << t.target << ":" << t.priority;
490 for (const std::string& l : t.label) os << ":" << l;
491 os << ";";
492 }
493 if (a.initial_distribution) {
494 os << "|p";
495 for (double p : *a.initial_distribution) os << p << ",";
496 }
497 os << "|u" << a.unbounded_queue;
498 if (a.unbounded_initial) os << ":" << *a.unbounded_initial;
499 os << "|q" << (a.queue_info ? 1 : 0) << "|d" << a.discrete << "]";
500 }
501 for (const Sync& s : m.syncs) {
502 os << "S[" << endpoint_label(s.active);
503 for (const Endpoint& p : s.passive) os << "," << endpoint_label(p);
504 for (const auto& p : s.node_passive) os << "," << p.first << ":" << p.second.label();
505 os << "]";
506 }
507 for (const MinSync& s : m.min_syncs) {
508 os << "M[";
509 for (const Endpoint& p : s.active) os << endpoint_label(p) << ",";
510 os << "->";
511 for (const Endpoint& p : s.passive) os << endpoint_label(p) << ",";
512 os << "]";
513 }
514 for (const ExtSync& s : m.ext_syncs) {
515 os << "X[" << s.agent << ":" << s.pattern.label();
516 for (const Endpoint& p : s.passive) os << "," << endpoint_label(p);
517 os << "]";
518 }
519 for (const std::string& h : m.hidden) os << "H[" << h << "]";
520 for (const auto& r : m.rewards) os << "R[" << r.first << ":" << r.second.target_type().name() << "]";
521 return os.str();
522}
523
524/** Features of a model that only the extended kernel interprets (`model_features`). */
525inline std::set<std::string> model_features(const Model& model) {
526 std::set<std::string> features;
527 for (const AgentRecord& agent : model.agents) {
528 if (agent.embedded) {
529 features.insert("embedded");
530 continue;
531 }
532 for (const Transition& t : agent.transitions) {
533 if (t.role == Role::Immediate) features.insert("immediate");
534 if (t.fn) features.insert("functional");
535 if (t.label.size() > 1 || t.tuple_label) features.insert("multilabel");
536 }
537 }
538 if (!model.ext_syncs.empty()) features.insert("embedded");
539 return features;
540}
541
542/** True when any agent of the model is discrete-time. */
543inline bool has_discrete(const Model& model) {
544 for (const AgentRecord& a : model.agents)
545 if (a.discrete) return true;
546 return false;
547}
548
549/** Validate the common agent model used by both frontends (`validate_model`). */
550inline void validate_model(const Model& model, bool allow_unbounded = false) {
551 const std::vector<AgentRecord>& agents = model.agents;
552 if (agents.empty()) throw value_error("At least one agent is required");
553 {
554 std::set<std::string> names;
555 for (const AgentRecord& a : agents)
556 if (!names.insert(a.name).second) throw value_error("Invalid states or initial state in " + a.name);
557 }
558 for (const AgentRecord& agent : agents) {
559 const std::string& name = agent.name;
560 if (agent.embedded) {
561 if (name != agent.embedded->name()) throw value_error("Invalid embedded agent name " + name);
562 continue;
563 }
564 if (agent.unbounded_queue && !allow_unbounded)
565 throw UnsupportedError(ErrorKind("Model"),
566 "This method requires finite agents; the composition contains an unbounded queue.");
567 std::set<std::string> uniq(agent.states.begin(), agent.states.end());
568 if (agent.states.empty() || uniq.size() != agent.states.size() || !uniq.count(agent.initial))
569 throw value_error("Invalid states or initial state in " + name);
570 if (agent.initial_distribution) {
571 const std::vector<double>& p = *agent.initial_distribution;
572 bool ok = p.size() == agent.states.size();
573 double sum = 0.0;
574 for (double x : p) {
575 if (!std::isfinite(x) || x < 0) ok = false;
576 sum += x;
577 }
578 if (!ok || !(std::fabs(sum - 1.0) <= 1e-12)) throw value_error("Invalid initial distribution in " + name);
579 }
580 for (const Transition& t : agent.transitions) {
581 if (!uniq.count(t.source) || !uniq.count(t.target)) throw value_error("Unknown state in " + name);
582 if (t.fn) continue;
583 if (!std::isfinite(t.value) || t.value < 0 ||
584 ((t.role == Role::Passive || t.role == Role::Immediate) && t.value == 0))
585 throw value_error(
586 "Rates must be finite and nonnegative; passive and immediate weights must be positive");
587 }
588 }
589 // roles(ep): nullopt for an embedded agent, else the roles of the action's transitions
590 auto roles = [&](const Endpoint& ep) -> std::optional<std::set<Role>> {
591 const AgentRecord& a = model.agent(ep.first);
592 if (a.embedded) return std::nullopt;
593 std::set<Role> r;
594 for (const Transition& t : a.transitions)
595 if (t.action == ep.second) r.insert(t.role);
596 return r;
597 };
598 if (has_discrete(model)) {
599 if (agents.size() != 1 || !model.syncs.empty() || !model.min_syncs.empty() || !model.ext_syncs.empty())
600 throw UnsupportedError(ErrorKind("Model"),
601 "a discrete-time agent (MarkovChain) must be the only part of its composition: "
602 "LINE has no composition of DTMCs.");
603 }
604 std::set<Endpoint> used_active, used_passive;
605 for (const Sync& s : model.syncs) {
606 std::vector<std::string> agent_names{s.active.first};
607 for (const Endpoint& p : s.passive) agent_names.push_back(p.first);
608 for (const auto& p : s.node_passive) agent_names.push_back(p.first);
609 const std::set<std::string> distinct(agent_names.begin(), agent_names.end());
610 bool unknown = false;
611 for (const std::string& n : agent_names)
612 if (!model.has_agent(n)) unknown = true;
613 if ((s.passive.empty() && s.node_passive.empty()) || unknown || distinct.size() != agent_names.size())
614 throw value_error("Synchronization requires distinct known agents");
615 bool reused = used_active.count(s.active) || used_passive.count(s.active);
616 for (const Endpoint& p : s.passive)
617 if (used_active.count(p)) reused = true;
618 if (reused) throw value_error("An endpoint can participate in only one synchronization");
619 used_active.insert(s.active);
620 used_passive.insert(s.passive.begin(), s.passive.end());
621 const std::optional<std::set<Role>> r = roles(s.active);
622 if (!r)
624 ": an embedded node action cannot drive a hand-agent synchronization; synchronize the "
625 "node key with hand passives instead");
626 bool only_active = !r->empty() && r->size() == 1;
627 for (Role x : *r)
628 if (x == Role::Passive) only_active = false;
629 if (!only_active) throw value_error(endpoint_repr(s.active) + " must have only active transitions");
630 for (const Endpoint& b : s.passive) {
631 const std::optional<std::set<Role>> rb = roles(b);
632 if (!rb) throw value_error(endpoint_repr(b) + " is not an action of the embedded agent " + b.first);
633 if (rb->empty() || *rb != std::set<Role>{Role::Passive})
634 throw value_error(endpoint_repr(b) + " must have only passive transitions");
635 }
636 for (const auto& b : s.node_passive) {
637 const AgentRecord& part = model.agent(b.first);
638 if (!part.embedded)
639 throw value_error("('" + b.first + "', " + b.second.label() + ") must have only passive transitions");
640 part.embedded->check_injection(b.second);
641 }
642 }
643 for (const MinSync& s : model.min_syncs) {
644 std::vector<Endpoint> endpoints = s.active;
645 endpoints.insert(endpoints.end(), s.passive.begin(), s.passive.end());
646 if (s.active.size() < 2) throw value_error("minimum synchronization requires at least two active endpoints");
647 const std::set<Endpoint> eps(endpoints.begin(), endpoints.end());
648 bool reused = eps.size() != endpoints.size();
649 for (const Endpoint& e : endpoints)
650 if (used_active.count(e) || used_passive.count(e)) reused = true;
651 if (reused) throw value_error("An endpoint can participate in only one synchronization");
652 std::set<std::string> agents_in;
653 bool unknown = false;
654 for (const Endpoint& e : endpoints) {
655 if (!model.has_agent(e.first)) unknown = true;
656 agents_in.insert(e.first);
657 }
658 if (unknown || agents_in.size() != endpoints.size())
659 throw value_error("Synchronization requires distinct known agents");
660 for (const Endpoint& e : endpoints)
661 if (model.agent(e.first).embedded)
662 throw UnsupportedError(ErrorKind("Synchronization"),
663 "synchronize_min cannot take an embedded node action: it would retime LINE's "
664 "own event.");
665 used_active.insert(s.active.begin(), s.active.end());
666 used_passive.insert(s.passive.begin(), s.passive.end());
667 auto check = [&](const Endpoint& ep, Role role) {
668 bool any = false, bad = false;
669 for (const Transition& t : model.agent(ep.first).transitions)
670 if (t.action == ep.second) {
671 any = true;
672 if (t.role != role) bad = true;
673 }
674 if (!any || bad)
675 throw value_error(endpoint_repr(ep) + " must have only " + role_name(role) + " transitions");
676 };
677 for (const Endpoint& p : s.active) check(p, Role::Active);
678 for (const Endpoint& p : s.passive) check(p, Role::Passive);
679 }
680 for (const ExtSync& x : model.ext_syncs) {
681 if (!model.has_agent(x.agent) || !model.agent(x.agent).embedded)
682 throw value_error(x.agent + " is not an embedded agent");
683 std::set<std::string> names{x.agent};
684 bool unknown = false;
685 for (const Endpoint& p : x.passive) {
686 if (!model.has_agent(p.first)) unknown = true;
687 names.insert(p.first);
688 }
689 if (x.passive.empty() || unknown || names.size() != x.passive.size() + 1)
690 throw value_error("Synchronization requires distinct known agents");
691 for (const Endpoint& b : x.passive) {
692 const std::optional<std::set<Role>> rb = roles(b);
693 if (!rb || *rb != std::set<Role>{Role::Passive})
694 throw value_error(endpoint_repr(b) + " must have only passive transitions");
695 }
696 used_passive.insert(x.passive.begin(), x.passive.end());
697 }
698 for (const AgentRecord& agent : agents) {
699 if (agent.embedded) continue;
700 for (const Transition& t : agent.transitions)
701 if (t.role == Role::Passive && !used_passive.count(Endpoint(agent.name, t.action)))
702 throw value_error("Unconnected passive action " + agent.name + "." + t.action);
703 }
704}
705
706/** Ordered event labels: hand active/immediate labels, then embedded node keys (`event_labels`). */
707inline std::vector<std::string> event_labels(const Model& model) {
708 std::set<Endpoint> min_members, min_heads;
709 for (const MinSync& s : model.min_syncs) {
710 for (const Endpoint& e : s.active) min_members.insert(e);
711 min_heads.insert(s.active.front());
712 }
713 std::vector<std::string> labels;
714 std::set<std::string> seen;
715 auto add = [&](const std::string& l) {
716 if (seen.insert(l).second) labels.push_back(l);
717 };
718 for (const AgentRecord& agent : model.agents) {
719 if (agent.embedded) {
720 for (const NodeEvent& k : agent.embedded->labels()) add(k.label());
721 for (const Sync& s : model.syncs)
722 for (const auto& p : s.node_passive)
723 if (p.second.model == agent.embedded->prefix()) add(p.second.label());
724 continue;
725 }
726 for (const Transition& t : agent.transitions) {
727 if (t.role == Role::Passive) continue;
728 const Endpoint ep(agent.name, t.action);
729 if (min_members.count(ep) && !min_heads.count(ep)) continue;
730 for (const std::string& l : transition_labels(agent.name, t)) add(l);
731 }
732 }
733 return labels;
734}
735
736/** One enabled joint event: successor, label indices, rate (weight if immediate), priority (`Event`). */
737struct Event {
738 std::vector<std::size_t> target;
739 std::vector<std::size_t> labels; ///< indices into Events::labels
740 double rate = 0.0;
741 bool immediate = false;
742 int priority = 0;
743};
744
745/** Apply maximal progress and immediate priorities to the enabled events (`selected`). */
746inline std::vector<Event> selected(const std::vector<Event>& events) {
747 bool any = false;
748 int top = std::numeric_limits<int>::min();
749 for (const Event& e : events)
750 if (e.immediate) {
751 any = true;
752 top = std::max(top, e.priority);
753 }
754 if (!any) return events;
755 std::vector<Event> out;
756 for (const Event& e : events)
757 if (e.immediate && e.priority == top) out.push_back(e);
758 return out;
759}
760
761/**
762 * The extended event kernel (`events`): the ordered labels and the enabled
763 * events of a joint state, in the Python iteration order: agents in order
764 * (an embedded agent's arcs, a hand agent's immediate events as met and its
765 * timed events of the top priority), passive choices in Cartesian-product
766 * order, then each minimum synchronisation.
767 *
768 * The kernel keeps a reference to the model; the model must outlive it.
769 */
770class Events {
771 public:
772 explicit Events(const Model& model) : model_(model) {
775 for (std::size_t k = 0; k < labels.size(); ++k) label_index_[labels[k]] = k;
776 const std::size_t n = model.agents.size();
777 local_.resize(n);
778 tlabels_.resize(n);
779 ext_.resize(n);
780 for (std::size_t a = 0; a < n; ++a) {
781 const AgentRecord& ag = model.agents[a];
782 if (ag.embedded) continue;
783 local_[a].assign(ag.states.size(), {});
784 tlabels_[a].resize(ag.transitions.size());
785 for (std::size_t k = 0; k < ag.transitions.size(); ++k) {
786 const Transition& t = ag.transitions[k];
787 local_[a][ag.state_index(t.source)].push_back(k);
788 if (t.role != Role::Passive) {
789 const Endpoint ep(ag.name, t.action);
790 bool member = false, head = false;
791 for (const MinSync& s : model.min_syncs) {
792 if (detail::contains(s.active, ep)) member = true;
793 if (s.active.front() == ep) head = true;
794 }
795 if (member && !head) continue;
796 for (const std::string& l : transition_labels(ag.name, t)) tlabels_[a][k].push_back(index_of(l));
797 }
798 }
799 }
800 for (const MinSync& s : model.min_syncs) {
801 min_members_.insert(s.active.begin(), s.active.end());
802 const AgentRecord& head = model.agent(s.active.front().first);
803 const Transition* ht = nullptr;
804 for (const Transition& t : head.transitions)
805 if (t.action == s.active.front().second) {
806 ht = &t;
807 break;
808 }
809 if (!ht) throw value_error(endpoint_repr(s.active.front()) + " must have only active transitions");
810 min_label_.push_back(index_of(transition_label(head.name, *ht)));
811 }
812 for (const Sync& s : model.syncs) {
813 std::vector<PassiveRef> refs;
814 std::vector<std::size_t> extra;
815 for (const Endpoint& p : s.passive) refs.push_back(PassiveRef{model.index_of(p.first), p.second, false, NodeEvent()});
816 for (const auto& p : s.node_passive) {
817 refs.push_back(PassiveRef{model.index_of(p.first), std::string(), true, p.second});
818 extra.push_back(index_of(p.second.label()));
819 }
820 sync_[s.active] = refs;
821 sync_labels_[s.active] = extra;
822 }
823 for (const ExtSync& x : model.ext_syncs) {
824 std::vector<PassiveRef> refs;
825 for (const Endpoint& p : x.passive) refs.push_back(PassiveRef{model.index_of(p.first), p.second, false, NodeEvent()});
826 ext_[model.index_of(x.agent)].push_back(std::make_pair(x.pattern, refs));
827 }
828 }
829
830 std::vector<std::string> labels;
831
832 std::size_t label_index(const std::string& label) const {
833 auto it = label_index_.find(label);
834 if (it == label_index_.end()) throw value_error("Unknown event label " + label);
835 return it->second;
836 }
837
838 const Model& model() const { return model_; }
839
840 /** The enabled events of `state` (zero-rate events are kept out). */
841 std::vector<Event> outgoing(const std::vector<std::size_t>& state) const {
842 std::vector<Event> out;
843 const StateView view(model_, state);
844 const std::size_t n = model_.agents.size();
845 for (std::size_t ai = 0; ai < n; ++ai) {
846 const AgentRecord& agent = model_.agents[ai];
847 if (agent.embedded) {
848 EmbeddedPart& emb = *agent.embedded;
849 for (const EmbeddedArc& arc : emb.outgoing(state[ai])) {
850 std::vector<PassiveRef> partners;
851 for (const auto& join : ext_[ai]) {
852 bool hit = false;
853 for (const NodeEvent& k : arc.keys)
854 if (emb.matches(k, join.first)) hit = true;
855 if (hit) partners.insert(partners.end(), join.second.begin(), join.second.end());
856 }
857 std::vector<std::size_t> keys;
858 for (const NodeEvent& k : arc.keys) keys.push_back(index_of(k.label()));
859 const std::vector<std::vector<Choice>> sets = passive_choices(state, view, partners);
860 product(sets, [&](const std::vector<const Choice*>& choice) {
861 Event e;
862 e.target = state;
863 e.target[ai] = arc.target;
864 double prob = 1.0;
865 for (const Choice* c : choice) {
866 e.target[c->agent] = c->target;
867 prob *= c->prob;
868 }
869 e.labels = keys;
870 e.rate = arc.rate * prob;
871 e.immediate = arc.immediate;
872 e.priority = 0;
873 out.push_back(std::move(e));
874 });
875 }
876 continue;
877 }
878 std::vector<std::pair<int, std::vector<Event>>> timed;
879 for (std::size_t k : local_[ai][state[ai]]) {
880 const Transition& t = agent.transitions[k];
881 if (t.role == Role::Passive) continue;
882 const Endpoint endpoint(agent.name, t.action);
883 if (min_members_.count(endpoint)) continue;
884 const double rate = transition_value(t, view);
885 if (!(rate > 0)) continue;
886 const std::size_t tgt = agent.state_index(t.target);
887 std::vector<std::size_t> tl = tlabels_[ai][k];
888 std::vector<std::vector<Choice>> sets;
889 auto sit = sync_.find(endpoint);
890 if (sit != sync_.end()) {
891 sets = passive_choices(state, view, sit->second);
892 const std::vector<std::size_t>& extra = sync_labels_.at(endpoint);
893 tl.insert(tl.end(), extra.begin(), extra.end());
894 }
895 std::vector<Event> evs;
896 product(sets, [&](const std::vector<const Choice*>& choice) {
897 Event e;
898 e.target = state;
899 e.target[ai] = tgt;
900 double prob = 1.0;
901 for (const Choice* c : choice) {
902 e.target[c->agent] = c->target;
903 prob *= c->prob;
904 }
905 e.labels = tl;
906 e.rate = rate * prob;
908 e.priority = t.priority;
909 evs.push_back(std::move(e));
910 });
911 if (t.role == Role::Immediate) {
912 for (Event& e : evs) out.push_back(std::move(e));
913 } else if (!evs.empty()) {
914 timed.push_back(std::make_pair(t.priority, std::move(evs)));
915 }
916 }
917 // Priorities on timed actions: a timed action of an agent is disabled
918 // while a timed action of the same agent with a higher priority is
919 // enabled (e.g. preemptive priority service of one server).
920 if (!timed.empty()) {
921 int top = timed.front().first;
922 for (const auto& p : timed) top = std::max(top, p.first);
923 for (auto& p : timed)
924 if (p.first == top)
925 for (Event& e : p.second) out.push_back(std::move(e));
926 }
927 }
928 for (std::size_t m = 0; m < model_.min_syncs.size(); ++m) {
929 const MinSync& s = model_.min_syncs[m];
930 std::vector<std::vector<Choice>> choices;
931 std::vector<double> totals;
932 std::vector<Endpoint> all = s.active;
933 all.insert(all.end(), s.passive.begin(), s.passive.end());
934 for (const Endpoint& e : all) {
935 const std::size_t pi = model_.index_of(e.first);
936 const Role role = detail::contains(s.active, e) ? Role::Active : Role::Passive;
937 double total = 0.0;
938 choices.push_back(candidates(pi, e.second, role, state[pi], view, &total));
939 if (role == Role::Active) totals.push_back(total);
940 }
941 const double rate = *std::min_element(totals.begin(), totals.end());
942 if (rate == 0) continue;
943 product(choices, [&](const std::vector<const Choice*>& choice) {
944 Event e;
945 e.target = state;
946 double prob = 1.0;
947 for (const Choice* c : choice) {
948 e.target[c->agent] = c->target;
949 prob *= c->prob;
950 }
951 e.labels = std::vector<std::size_t>{min_label_[m]};
952 e.rate = rate * prob;
953 out.push_back(std::move(e));
954 });
955 }
956 return out;
957 }
958
959 private:
960 struct Choice {
961 std::size_t agent;
962 std::size_t target;
963 double prob;
964 };
965 struct PassiveRef {
966 std::size_t agent;
967 std::string action;
968 bool node;
969 NodeEvent key;
970 };
971
972 std::size_t index_of(const std::string& label) const {
973 auto it = label_index_.find(label);
974 if (it == label_index_.end())
975 throw InternalError(ErrorKind("LAL"), "LAL event label " + label + " is missing from the label list");
976 return it->second;
977 }
978
979 std::vector<Choice> candidates(std::size_t pi, const std::string& action, Role role, std::size_t local,
980 const StateView& view, double* total_out = nullptr) const {
981 const AgentRecord& other = model_.agents[pi];
982 std::vector<std::pair<const Transition*, double>> cand;
983 double total = 0.0;
984 for (std::size_t k : local_[pi][local]) {
985 const Transition& p = other.transitions[k];
986 if (p.role != role || p.action != action) continue;
987 const double w = transition_value(p, view);
988 if (w > 0) {
989 cand.push_back(std::make_pair(&p, w));
990 total += w;
991 }
992 }
993 if (total_out) *total_out = total;
994 std::vector<Choice> out;
995 if (total == 0) return out;
996 for (const auto& c : cand) out.push_back(Choice{pi, other.state_index(c.first->target), c.second / total});
997 return out;
998 }
999
1000 std::vector<std::vector<Choice>> passive_choices(const std::vector<std::size_t>& state, const StateView& view,
1001 const std::vector<PassiveRef>& partners) const {
1002 std::vector<std::vector<Choice>> out;
1003 for (const PassiveRef& p : partners) {
1004 const AgentRecord& other = model_.agents[p.agent];
1005 if (p.node) {
1006 std::vector<Choice> c;
1007 for (const auto& r : other.embedded->inject(state[p.agent], p.key))
1008 c.push_back(Choice{p.agent, r.first, r.second});
1009 out.push_back(c);
1010 continue;
1011 }
1012 out.push_back(candidates(p.agent, p.action, Role::Passive, state[p.agent], view));
1013 }
1014 return out;
1015 }
1016
1017 /** Cartesian product of the choice sets; one empty choice when there are no sets. */
1018 template <class F>
1019 static void product(const std::vector<std::vector<Choice>>& sets, F&& f) {
1020 for (const auto& s : sets)
1021 if (s.empty()) return;
1022 std::vector<std::size_t> idx(sets.size(), 0);
1023 std::vector<const Choice*> pick(sets.size());
1024 while (true) {
1025 for (std::size_t i = 0; i < sets.size(); ++i) pick[i] = &sets[i][idx[i]];
1026 f(pick);
1027 if (sets.empty()) return;
1028 std::size_t d = sets.size();
1029 while (d > 0) {
1030 --d;
1031 if (++idx[d] < sets[d].size()) break;
1032 idx[d] = 0;
1033 if (d == 0) return;
1034 }
1035 }
1036 }
1037
1038 const Model& model_;
1039 std::map<std::string, std::size_t> label_index_;
1040 std::vector<std::vector<std::vector<std::size_t>>> local_; ///< hand agent -> local state -> transitions
1041 std::vector<std::vector<std::vector<std::size_t>>> tlabels_; ///< hand agent -> transition -> label indices
1042 std::map<Endpoint, std::vector<PassiveRef>> sync_;
1043 std::map<Endpoint, std::vector<std::size_t>> sync_labels_; ///< injected node keys an active event also carries
1044 std::set<Endpoint> min_members_;
1045 std::vector<std::size_t> min_label_;
1046 std::vector<std::vector<std::pair<NodeEvent, std::vector<PassiveRef>>>> ext_; ///< embedded agent -> joins
1047};
1048
1049/** One outgoing joint-state move of the legacy event kernel. */
1050struct Move {
1051 std::vector<std::size_t> target;
1052 std::size_t label = 0; ///< index into EventKernel::labels
1053 double rate = 0.0;
1054};
1055
1056/**
1057 * The legacy timed event kernel (`event_kernel`): the ordered event labels and
1058 * a lazy joint-state transition generator yielding `(target, label, rate)`.
1059 *
1060 * Models with immediate actions, embedded LINE objects or multi-label events
1061 * need the extended `Events` interface and are refused here
1062 * (`LINE:unsupported:Model`), so a solver path built on this kernel cannot
1063 * silently misread them.
1064 */
1066 public:
1067 explicit EventKernel(const Model& model) : model_(model), events_(check(model)) {
1068 labels = events_.labels;
1069 }
1070
1071 std::vector<std::string> labels;
1072
1073 std::size_t label_index(const std::string& label) const { return events_.label_index(label); }
1074
1075 /**
1076 * Outgoing moves of `state`. When `include` is non-null only labels whose
1077 * index is flagged there are generated (Python's `include` set).
1078 */
1079 std::vector<Move> outgoing(const std::vector<std::size_t>& state,
1080 const std::vector<bool>* include = nullptr) const {
1081 std::vector<Move> out;
1082 for (Event& e : events_.outgoing(state)) {
1083 const std::size_t lab = e.labels.front();
1084 if (include && !(*include)[lab]) continue;
1085 Move m;
1086 m.target = std::move(e.target);
1087 m.label = lab;
1088 m.rate = e.rate;
1089 out.push_back(std::move(m));
1090 }
1091 return out;
1092 }
1093
1094 const Model& model() const { return model_; }
1095
1096 private:
1097 static const Model& check(const Model& model) {
1098 std::set<std::string> features = model_features(model);
1099 std::set<std::string> refused;
1100 for (const std::string& f : features)
1101 if (f == "immediate" || f == "embedded" || f == "multilabel") refused.insert(f);
1102 if (!refused.empty())
1103 throw UnsupportedError(ErrorKind("Model"),
1104 "this LAL method supports only timed hand-declared agents; the composition uses " +
1105 detail::join_sorted(refused) + " actions.");
1106 return model;
1107 }
1108
1109 const Model& model_;
1110 Events events_;
1111};
1112
1113/** Options of `compile_model`. A zero limit means "no limit" (Python's None). */
1115 bool reachable_only = false;
1116 std::size_t max_states = 0;
1117 std::size_t max_dense_bytes = 0;
1118};
1119
1120namespace detail {
1121
1122inline std::size_t isqrt(std::size_t v) {
1123 std::size_t r = static_cast<std::size_t>(std::sqrt(static_cast<double>(v)));
1124 while (r * r > v) --r;
1125 while ((r + 1) * (r + 1) <= v) ++r;
1126 return r;
1127}
1128
1129struct StateHash {
1130 std::size_t operator()(const std::vector<std::size_t>& v) const {
1131 std::size_t h = 1469598103934665603ULL;
1132 for (std::size_t x : v) {
1133 h ^= x + 0x9e3779b97f4a7c15ULL + (h << 6) + (h >> 2);
1134 }
1135 return h;
1136 }
1137};
1138
1139/**
1140 * Eliminate the vanishing states by stochastic complementation (`_complement`).
1141 *
1142 * The generator over the tangible states T is `Q_TT + Q_TV N Q_VT` with
1143 * `N = (-Q_VV)^-1`, and each label matrix becomes
1144 * `R_TT + R_TV N Q_VT + Q_TV N R_VT + Q_TV N R_VV N Q_VT`: the firings made
1145 * inside a vanishing excursion are credited to the tangible state it started
1146 * from and the one it ends in. An immediate cycle with no exit is refused.
1147 */
1148inline void complement(Compiled& c, const Matrix<double>& allrates, const std::vector<bool>& vanish) {
1149 const std::size_t n = c.states.size();
1150 std::vector<std::size_t> T, V;
1151 for (std::size_t i = 0; i < n; ++i) (vanish[i] ? V : T).push_back(i);
1152 if (T.empty())
1153 throw UnsupportedError(ErrorKind("Model"),
1154 "every reachable state is vanishing: the immediate actions never let time advance.");
1155 const std::size_t nt = T.size(), nv = V.size();
1156 Matrix<double> off = allrates;
1157 for (std::size_t i = 0; i < n; ++i) off(i, i) = 0.0;
1158 // A = -Q_VV
1159 Matrix<double> A(nv, nv, 0.0), At(nv, nv, 0.0);
1160 for (std::size_t a = 0; a < nv; ++a) {
1161 double out = 0.0;
1162 for (std::size_t j = 0; j < n; ++j) out += off(V[a], j);
1163 for (std::size_t b = 0; b < nv; ++b) A(a, b) = -off(V[a], V[b]);
1164 A(a, a) += out;
1165 }
1166 for (std::size_t a = 0; a < nv; ++a)
1167 for (std::size_t b = 0; b < nv; ++b) At(b, a) = A(a, b);
1168 Matrix<double> absorb(nv, nt, 0.0), entry(nt, nv, 0.0); // N Q_VT and Q_TV N
1169 bool ok = true;
1170 try {
1171 std::vector<std::size_t> piv = lu_factor(A);
1172 std::vector<std::size_t> pivt = lu_factor(At);
1173 for (std::size_t t = 0; t < nt; ++t) {
1174 std::vector<double> b(nv);
1175 for (std::size_t a = 0; a < nv; ++a) b[a] = off(V[a], T[t]);
1176 lu_solve(A, piv, b);
1177 for (std::size_t a = 0; a < nv; ++a) absorb(a, t) = b[a];
1178 }
1179 for (std::size_t t = 0; t < nt; ++t) {
1180 std::vector<double> b(nv);
1181 for (std::size_t a = 0; a < nv; ++a) b[a] = off(T[t], V[a]);
1182 lu_solve(At, pivt, b);
1183 for (std::size_t a = 0; a < nv; ++a) entry(t, a) = b[a];
1184 }
1185 } catch (const NumericError&) {
1186 ok = false;
1187 }
1188 for (std::size_t a = 0; ok && a < nv; ++a) {
1189 double s = 0.0;
1190 for (std::size_t t = 0; t < nt; ++t) s += absorb(a, t);
1191 if (!std::isfinite(s) || std::fabs(s - 1.0) > 1e-8 + 1e-5 * 1.0) ok = false;
1192 }
1193 if (!ok)
1194 throw UnsupportedError(ErrorKind("Model"),
1195 "the immediate actions form a cycle that never returns to a tangible state.");
1196 auto restrict = [&](const Matrix<double>& r) {
1197 Matrix<double> out(nt, nt, 0.0);
1198 // R_TV N Q_VT + Q_TV N R_VT + Q_TV N R_VV N Q_VT
1199 Matrix<double> rva(nv, nt, 0.0); // R_VV absorb
1200 for (std::size_t a = 0; a < nv; ++a)
1201 for (std::size_t b = 0; b < nv; ++b) {
1202 const double x = r(V[a], V[b]);
1203 if (x == 0) continue;
1204 for (std::size_t t = 0; t < nt; ++t) rva(a, t) += x * absorb(b, t);
1205 }
1206 for (std::size_t i = 0; i < nt; ++i)
1207 for (std::size_t j = 0; j < nt; ++j) {
1208 double v = r(T[i], T[j]);
1209 for (std::size_t a = 0; a < nv; ++a) {
1210 v += r(T[i], V[a]) * absorb(a, j);
1211 v += entry(i, a) * (r(V[a], T[j]) + rva(a, j));
1212 }
1213 out(i, j) = v;
1214 }
1215 return out;
1216 };
1217 Matrix<double> q(nt, nt, 0.0);
1218 for (std::size_t i = 0; i < nt; ++i)
1219 for (std::size_t j = 0; j < nt; ++j) {
1220 double v = off(T[i], T[j]);
1221 for (std::size_t a = 0; a < nv; ++a) v += off(T[i], V[a]) * absorb(a, j);
1222 q(i, j) = v;
1223 }
1224 for (std::size_t i = 0; i < nt; ++i) {
1225 q(i, i) = 0.0;
1226 double s = 0.0;
1227 for (std::size_t j = 0; j < nt; ++j) s += q(i, j);
1228 q(i, i) = -s;
1229 }
1230 for (Matrix<double>& E : c.events) E = restrict(E);
1231 std::vector<double> p0(nt, 0.0);
1232 for (std::size_t t = 0; t < nt; ++t) {
1233 p0[t] = c.initial_distribution[T[t]];
1234 for (std::size_t a = 0; a < nv; ++a) p0[t] += c.initial_distribution[V[a]] * absorb(a, t);
1235 }
1236 std::vector<std::vector<std::size_t>> tangible;
1237 for (std::size_t t : T) tangible.push_back(c.states[t]);
1238 std::size_t init = 0;
1239 for (std::size_t t = 0; t < nt; ++t)
1240 if (p0[t] > 0) {
1241 init = t;
1242 break;
1243 }
1244 c.states = std::move(tangible);
1245 c.generator = std::move(q);
1246 c.initial_distribution = std::move(p0);
1247 c.initial = init;
1248 c.vanishing_eliminated = nv;
1249}
1250
1251} // namespace detail
1252
1253/**
1254 * Compile the Cartesian (or, with `reachable_only`, the reachable) finite
1255 * state space and its event matrices (`compile_model`). Matrices hold
1256 * nonnegative event rates including observable self-events; the generator
1257 * excludes those self-events. A composition with an embedded LINE model is
1258 * always enumerated from its initial state. Vanishing states (an immediate
1259 * event enabled, or a vanishing state of an embedded model) are eliminated by
1260 * stochastic complementation.
1261 */
1263 validate_model(model);
1264 const Events kernel(model);
1265 const std::vector<std::string>& labels = kernel.labels;
1266 const std::set<std::string> features = model_features(model);
1267 const bool reachable_only = opt.reachable_only || features.count("embedded");
1268 std::size_t max_states = opt.max_states;
1269 std::string limit_note;
1270 if (reachable_only && opt.max_dense_bytes > 0) {
1271 const std::size_t cap = std::max<std::size_t>(
1272 1, detail::isqrt(opt.max_dense_bytes / (8 * (labels.size() + 4))));
1273 if (max_states == 0 || cap < max_states) {
1274 max_states = cap;
1275 limit_note = " (set by the dense matrix storage limit of " + std::to_string(opt.max_dense_bytes) +
1276 " bytes)";
1277 }
1278 }
1279 const std::size_t n = model.agents.size();
1280 std::vector<std::vector<std::pair<std::size_t, double>>> choices(n);
1281 double support = 1.0;
1282 for (std::size_t a = 0; a < n; ++a) {
1283 const AgentRecord& agent = model.agents[a];
1284 if (agent.embedded) {
1285 choices[a].push_back({agent.embedded->initial(), 1.0});
1286 } else if (!agent.initial_distribution) {
1287 choices[a].push_back({agent.state_index(agent.initial), 1.0});
1288 } else {
1289 for (std::size_t s = 0; s < agent.states.size(); ++s)
1290 if ((*agent.initial_distribution)[s] > 0) choices[a].push_back({s, (*agent.initial_distribution)[s]});
1291 }
1292 support *= static_cast<double>(choices[a].size());
1293 }
1294 if (reachable_only && max_states > 0 && support > static_cast<double>(max_states))
1295 throw StateSpaceLimitError("initial support exceeds reachable state space limit " +
1296 std::to_string(max_states) + limit_note);
1297 std::vector<std::pair<std::vector<std::size_t>, double>> initial_states;
1298 {
1299 std::vector<std::size_t> idx(n, 0);
1300 bool done = false;
1301 for (const auto& c : choices)
1302 if (c.empty()) done = true;
1303 while (!done) {
1304 std::vector<std::size_t> st(n);
1305 double p = 1.0;
1306 for (std::size_t a = 0; a < n; ++a) {
1307 st[a] = choices[a][idx[a]].first;
1308 p *= choices[a][idx[a]].second;
1309 }
1310 initial_states.push_back({st, p});
1311 std::size_t d = n;
1312 while (true) {
1313 if (d == 0) {
1314 done = true;
1315 break;
1316 }
1317 --d;
1318 if (++idx[d] < choices[d].size()) break;
1319 idx[d] = 0;
1320 }
1321 }
1322 }
1323 std::vector<std::pair<std::size_t, EmbeddedPart*>> vanishing_parts;
1324 for (std::size_t a = 0; a < n; ++a)
1325 if (model.agents[a].embedded && model.agents[a].embedded->has_vanishing())
1326 vanishing_parts.push_back({a, model.agents[a].embedded.get()});
1327 auto enabled = [&](const std::vector<std::size_t>& state, bool& is_vanishing) {
1328 std::vector<Event> evs;
1329 for (Event& e : kernel.outgoing(state))
1330 if (e.rate > 0) evs.push_back(std::move(e));
1331 is_vanishing = false;
1332 for (const Event& e : evs)
1333 if (e.immediate) is_vanishing = true;
1334 for (const auto& vp : vanishing_parts)
1335 if (vp.second->is_vanishing(state[vp.first])) is_vanishing = true;
1336 return is_vanishing ? selected(evs) : evs;
1337 };
1338
1339 Compiled c;
1340 std::unordered_map<std::vector<std::size_t>, std::size_t, detail::StateHash> lookup;
1341 std::vector<std::vector<Event>> out;
1342 std::vector<bool> vanish;
1343 if (reachable_only) {
1344 if (max_states > 0 && initial_states.size() > max_states)
1345 throw StateSpaceLimitError("reachable state space exceeds limit " + std::to_string(max_states) +
1346 limit_note);
1347 for (const auto& is : initial_states) {
1348 if (!lookup.count(is.first)) lookup[is.first] = c.states.size();
1349 c.states.push_back(is.first);
1350 }
1351 for (std::size_t i = 0; i < c.states.size(); ++i) {
1352 const std::vector<std::size_t> state = c.states[i];
1353 bool v = false;
1354 std::vector<Event> evs = enabled(state, v);
1355 for (const Event& e : evs) {
1356 if (!lookup.count(e.target)) {
1357 if (max_states > 0 && c.states.size() >= max_states)
1358 throw StateSpaceLimitError("reachable state space exceeds limit " +
1359 std::to_string(max_states) + limit_note);
1360 lookup[e.target] = c.states.size();
1361 c.states.push_back(e.target);
1362 }
1363 }
1364 out.push_back(std::move(evs));
1365 vanish.push_back(v);
1366 }
1367 } else {
1368 std::vector<std::size_t> idx(n, 0);
1369 while (true) {
1370 lookup[idx] = c.states.size();
1371 c.states.push_back(idx);
1372 std::size_t d = n;
1373 bool done = false;
1374 while (true) {
1375 if (d == 0) {
1376 done = true;
1377 break;
1378 }
1379 --d;
1380 if (++idx[d] < model.agents[d].states.size()) break;
1381 idx[d] = 0;
1382 }
1383 if (done) break;
1384 }
1385 for (std::size_t i = 0; i < c.states.size(); ++i) {
1386 bool v = false;
1387 out.push_back(enabled(c.states[i], v));
1388 vanish.push_back(v);
1389 }
1390 }
1391 const std::size_t N = c.states.size();
1392 const double estimate = static_cast<double>(N) * static_cast<double>(N) * 8.0 * (labels.size() + 4);
1393 if (opt.max_dense_bytes > 0 && estimate > static_cast<double>(opt.max_dense_bytes)) {
1394 std::ostringstream os;
1395 os.precision(17);
1396 os << "estimated dense matrix storage is " << static_cast<unsigned long long>(estimate)
1397 << " bytes; limit is " << opt.max_dense_bytes;
1398 throw StateSpaceLimitError(os.str());
1399 }
1400 c.initial_distribution.assign(N, 0.0);
1401 for (const auto& is : initial_states) c.initial_distribution[lookup.at(is.first)] += is.second;
1402 c.initial = lookup.at(initial_states.front().first);
1403 c.labels = labels;
1404 // Retain declared labels even when an action is disabled in every state.
1405 c.events.assign(labels.size(), Matrix<double>(N, N, 0.0));
1406 Matrix<double> allrates(N, N, 0.0);
1407 for (std::size_t i = 0; i < N; ++i)
1408 for (const Event& e : out[i]) {
1409 const std::size_t j = lookup.at(e.target);
1410 allrates(i, j) += e.rate;
1411 for (std::size_t l : e.labels) c.events[l](i, j) += e.rate;
1412 }
1413 for (const AgentRecord& a : model.agents) {
1414 c.agent_names.push_back(a.name);
1415 c.agent_states.push_back(a.embedded ? std::vector<std::string>() : a.states);
1416 }
1417 bool any_vanishing = false;
1418 for (bool v : vanish) any_vanishing = any_vanishing || v;
1419 if (!any_vanishing) {
1420 c.generator = allrates;
1421 for (std::size_t i = 0; i < N; ++i) {
1422 c.generator(i, i) = 0.0;
1423 double s = 0.0;
1424 for (std::size_t j = 0; j < N; ++j) s += c.generator(i, j);
1425 c.generator(i, i) = -s;
1426 }
1427 return c;
1428 }
1429 detail::complement(c, allrates, vanish);
1430 return c;
1431}
1432
1433} // namespace lal
1434} // namespace line
1435
1436#endif // LINE_LANG_LAL_CORE_H
Malformed or inconsistent input (dimensions, negative populations, ...): LINE:input:<kind>.
Definition error.h:103
InputError(const std::string &what)
Definition error.h:105
InternalError(const std::string &what)
Definition error.h:133
One CTMC state as a state-set predicate sees it, the reference's RewardState: the AGGREGATE row of st...
Definition solver.h:77
UnsupportedError(const std::string &what)
Definition error.h:119
An embedded LINE model seen by the LAL core (Python's EmbeddedNetwork / EmbeddedEnvironment duck type...
Definition core.h:197
virtual NodeEvent resolve(const std::string &node, lang::EventType event, const std::optional< std::string > &jobclass, const std::optional< std::string > &mode)=0
The key of a node action, validated against the model's names.
virtual double node_jobs(std::size_t s, const std::string &node, const std::string &jobclass)=0
Jobs at a station in global state s: the total, or those of class jobclass when nonempty.
virtual bool has_vanishing()=0
True when the embedded model has any vanishing state.
virtual std::size_t initial()=0
The global index of the initial state.
virtual const std::string & prefix() const =0
The embedding prefix, empty when none was given.
virtual std::vector< std::pair< std::string, std::size_t > > node_states()=0
[(node name, isf)] of the stateful node agents (isf is 1-based).
virtual void check_injection(const NodeEvent &key)=0
Refuse a key that a hand agent cannot drive.
virtual void refresh()=0
Re-read the embedded model on next use.
virtual void set_injected(const std::set< std::string > &)
Record the classes hand agents inject (names); a change invalidates the enumeration.
Definition core.h:244
virtual bool is_vanishing(std::size_t s)=0
True when s is a vanishing state of the embedded model.
virtual std::string class_name(std::size_t cls)=0
Name of the 1-based class index of the model.
virtual std::vector< double > node_row(std::size_t s, const std::string &node)=0
The state row of a stateful node in global state s.
virtual std::vector< std::pair< std::size_t, double > > inject(std::size_t s, const NodeEvent &key)=0
Passive response to an injected arrival: [(target, probability)]; empty blocks it.
virtual std::size_t stage_of(std::size_t)
The stage of global state s (Environment only).
Definition core.h:240
virtual bool matches(const NodeEvent &key, const NodeEvent &pattern) const
True when key is pattern, or pattern leaves the index open.
Definition core.h:226
virtual std::string node_name(std::size_t node)=0
Name of the 1-based node index of the model.
virtual bool has_stages()
True for an embedded Environment, whose stage is an agent of its own.
Definition core.h:238
virtual const std::vector< NodeEvent > & labels()=0
Every node key an arc carries, in first-seen order.
virtual const std::string & name() const =0
The part name (the prefix, or the model name).
virtual std::vector< EmbeddedArc > outgoing(std::size_t s)=0
Arcs leaving global state s.
virtual std::string node_state_label(std::size_t s, std::size_t isf)=0
The state row of stateful node isf in global state s, as [v1 v2 ...].
virtual ~EmbeddedPart()
Definition core.h:199
virtual std::vector< std::string > stage_names()
The stage names (Environment only).
Definition core.h:242
std::size_t label_index(const std::string &label) const
Definition core.h:1073
std::vector< std::string > labels
Definition core.h:1071
std::vector< Move > outgoing(const std::vector< std::size_t > &state, const std::vector< bool > *include=nullptr) const
Outgoing moves of state.
Definition core.h:1079
EventKernel(const Model &model)
Definition core.h:1067
const Model & model() const
Definition core.h:1094
The extended event kernel (events): the ordered labels and the enabled events of a joint state,...
Definition core.h:770
Events(const Model &model)
Definition core.h:772
std::size_t label_index(const std::string &label) const
Definition core.h:832
std::vector< std::string > labels
Definition core.h:830
std::vector< Event > outgoing(const std::vector< std::size_t > &state) const
The enabled events of state (zero-rate events are kept out).
Definition core.h:841
const Model & model() const
Definition core.h:838
StateSpaceLimitError(const std::string &what)
Definition core.h:81
Read-only view of a joint state, for functional rates and rewards (StateView).
Definition core.h:345
std::size_t index(const std::string &agent) const
Definition core.h:358
StateView(const Model &model, const std::vector< std::size_t > &state)
Definition core.h:347
std::vector< double > node(const std::string &agent, const std::string &node) const
Definition core.h:368
const Model & model() const
Definition core.h:382
bool contains(const std::string &agent) const
Definition core.h:349
const std::vector< std::size_t > & state() const
Definition core.h:383
std::string operator[](const std::string &agent) const
Definition core.h:351
long var(const std::string &agent, const std::string &variable) const
Definition core.h:360
double jobs(const std::string &agent, const std::string &node, const std::string &jobclass=std::string()) const
Definition core.h:375
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
Definition ag.h:49
const char * role_name(Role r)
Definition core.h:90
std::string signature(const Model &m)
A deterministic text signature of a model snapshot (Python's repr(model)), used by the solvers to cac...
Definition core.h:472
double transition_value(const Transition &t, const StateView &view)
The value of a transition in a joint state (_value).
Definition core.h:463
Role
Transition role: active (rate), passive (weight) or immediate (weight, zero time).
Definition core.h:88
std::function< double(const StateView &)> RewardFn
A state reward: a function of the joint-state view (Composition::setReward).
Definition core.h:100
std::pair< std::string, std::string > Endpoint
(agent, action).
Definition core.h:139
std::function< double(const StateView &)> ValueFn
A functional rate or weight: a function of the joint-state view.
Definition core.h:97
std::string endpoint_repr(const Endpoint &e)
Python's repr of an endpoint tuple, ('agent', 'action').
Definition core.h:144
std::vector< std::string > transition_labels(const std::string &agent, const Transition &t)
All labels of a transition: a multi-label transition credits one firing to each.
Definition core.h:458
bool has_discrete(const Model &model)
True when any agent of the model is discrete-time.
Definition core.h:543
std::vector< std::string > event_labels(const Model &model)
Ordered event labels: hand active/immediate labels, then embedded node keys (event_labels).
Definition core.h:707
std::vector< Event > selected(const std::vector< Event > &events)
Apply maximal progress and immediate priorities to the enabled events (selected).
Definition core.h:746
Compiled compile_model(const Model &model, const CompileOptions &opt=CompileOptions())
Compile the Cartesian (or, with reachable_only, the reachable) finite state space and its event matri...
Definition core.h:1262
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
std::string endpoint_label(const Endpoint &e)
Definition core.h:141
bool node_event_matches(const NodeEvent &key, const NodeEvent &pattern)
True when key is pattern, or pattern leaves the index open.
Definition core.h:176
std::set< std::string > model_features(const Model &model)
Features of a model that only the extended kernel interprets (model_features).
Definition core.h:525
std::string transition_label(const std::string &agent, const Transition &t)
Primary label of a transition (its first label when it carries several).
Definition core.h:453
EventType
The events a state can undergo, with the values of MATLAB EventType.
Definition lang_types.h:116
@ LOCAL
dummy event, no state change outside the node
Definition lang_types.h:118
const char * event_to_text(EventType e)
Definition lang_types.h:148
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
void lu_solve(const Matrix< T > &LU, const std::vector< std::size_t > &piv, std::vector< T > &b)
Solve LUx = Pb in place on b, using the factors from lu_factor.
Definition lu.h:94
std::vector< std::size_t > lu_factor(Matrix< T > &A)
In-place LU of A (n x n).
Definition lu.h:48
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
ErrorKind(std::string n)
Definition error.h:39
The immutable agent record (core.Agent).
Definition core.h:284
std::string name
Definition core.h:285
std::optional< std::vector< double > > initial_distribution
Definition core.h:289
std::string initial
Definition core.h:287
std::vector< Transition > transitions
Definition core.h:288
std::vector< std::vector< long > > var_values
Definition core.h:299
std::vector< std::string > var_names
Typed-variable agents: the variable names, and the valuation of each local state.
Definition core.h:298
std::vector< std::string > states
Definition core.h:286
std::optional< QueueInfo > queue_info
Definition core.h:292
std::size_t state_index(const std::string &s) const
Zero-based index of a local state, or npos.
Definition core.h:302
std::shared_ptr< EmbeddedPart > embedded
An embedded LINE model, whose states are global state indices; null for a hand agent.
Definition core.h:294
std::optional< long > unbounded_initial
Definition core.h:291
bool discrete
A discrete-time agent (an embedded MarkovChain).
Definition core.h:296
Options of compile_model.
Definition core.h:1114
std::size_t max_dense_bytes
Definition core.h:1117
std::size_t max_states
Definition core.h:1116
The compiled finite CTMC (core.Compiled).
Definition core.h:396
std::vector< std::string > labels
event labels, in order
Definition core.h:400
std::vector< std::vector< std::string > > agent_states
empty for an embedded agent
Definition core.h:404
std::vector< double > initial_distribution
Definition core.h:402
const Matrix< double > & event(const std::string &label) const
The rate matrix of a label; throws for an unknown label.
Definition core.h:415
std::size_t vanishing_eliminated
Definition core.h:405
std::vector< std::string > state_names(std::size_t i) const
The Python tuple of state names for joint state i (an embedded agent shows its global index).
Definition core.h:408
std::vector< Matrix< double > > events
one rate matrix per label
Definition core.h:401
std::size_t initial
Definition core.h:398
std::vector< std::string > agent_names
Definition core.h:403
std::vector< std::vector< std::size_t > > states
joint states as local indices
Definition core.h:397
Matrix< double > generator
Definition core.h:399
One arc of an embedded model: (target, keys, rate, immediate) (an immediate arc carries a weight).
Definition core.h:182
std::vector< NodeEvent > keys
Definition core.h:184
std::size_t target
Definition core.h:183
One enabled joint event: successor, label indices, rate (weight if immediate), priority (Event).
Definition core.h:737
std::vector< std::size_t > labels
indices into Events::labels
Definition core.h:739
double rate
Definition core.h:740
std::vector< std::size_t > target
Definition core.h:738
bool immediate
Definition core.h:741
Hand passives that join every event of an embedded part carrying a key matching pattern.
Definition core.h:264
NodeEvent pattern
Definition core.h:266
std::vector< Endpoint > passive
Definition core.h:267
std::string agent
Definition core.h:265
A minimum-rate synchronisation: at least two active endpoints and optional passive ones.
Definition core.h:258
std::vector< Endpoint > passive
Definition core.h:260
std::vector< Endpoint > active
Definition core.h:259
A composition snapshot (core.Model): agents in declaration order plus synchronisations.
Definition core.h:310
std::vector< MinSync > min_syncs
Definition core.h:313
bool has_agent(const std::string &name) const
Definition core.h:323
std::vector< std::pair< std::string, RewardFn > > rewards
declared state rewards, in order
Definition core.h:316
std::vector< Sync > syncs
Definition core.h:312
std::vector< ExtSync > ext_syncs
Definition core.h:314
std::size_t index_of(const std::string &name) const
Definition core.h:318
std::vector< AgentRecord > agents
Definition core.h:311
AgentRecord & agent(const std::string &name)
Definition core.h:329
const AgentRecord & agent(const std::string &name) const
Definition core.h:324
std::set< std::string > hidden
labels removed from the action tables
Definition core.h:315
One outgoing joint-state move of the legacy event kernel.
Definition core.h:1050
double rate
Definition core.h:1053
std::vector< std::size_t > target
Definition core.h:1051
std::size_t label
index into EventKernel::labels
Definition core.h:1052
Key of an embedded node action: (node, EventType, class | mode).
Definition core.h:153
std::optional< std::string > index
Definition core.h:157
bool operator!=(const NodeEvent &o) const
Definition core.h:168
std::string model
Definition core.h:154
bool operator==(const NodeEvent &o) const
Definition core.h:165
bool operator<(const NodeEvent &o) const
Definition core.h:169
std::string node
Definition core.h:155
std::string label() const
Definition core.h:159
lang::EventType event
Definition core.h:156
Structured queue declaration retained while a queue agent is unmodified (queue_info).
Definition core.h:271
std::optional< long > capacity
nullopt: unbounded
Definition core.h:272
std::optional< std::string > service_phase
Definition core.h:278
std::vector< std::string > service_inputs
Definition core.h:277
std::optional< double > service_rate
nullopt: passive service
Definition core.h:279
std::vector< std::string > departures
Definition core.h:275
std::optional< std::string > service_input
Definition core.h:276
std::vector< std::pair< std::string, double > > routes
Definition core.h:280
std::vector< std::string > arrivals
Definition core.h:274
An ordinary synchronisation: one active endpoint, hand passives and injected node arrivals.
Definition core.h:250
Endpoint active
Definition core.h:251
std::vector< std::pair< std::string, NodeEvent > > node_passive
(embedded part, ARV key): the hand action injects an arrival at that node.
Definition core.h:254
std::vector< Endpoint > passive
Definition core.h:252
One labelled local transition source --(action, value)--> target.
Definition core.h:111
std::vector< std::string > label
Definition core.h:118
std::string target
Definition core.h:116
bool tuple_label
The label is a TUPLE (python Transition.label given as a tuple), which marks a multi-label event even...
Definition core.h:125
bool operator==(const Transition &o) const
Value equality; two functional values compare equal by kind only (a std::function has no identity).
Definition core.h:130
std::string source
Definition core.h:112
bool functional() const
Definition core.h:127
std::string action
Definition core.h:113
bool operator!=(const Transition &o) const
Definition core.h:135