5#ifndef LINE_LANG_LAL_SOLVER_AG_H
6#define LINE_LANG_LAL_SOLVER_AG_H
52 std::optional<std::vector<std::vector<std::string>>>
groups;
56 std::string
key()
const {
57 std::ostringstream os;
86 return {
"default",
"inap",
"inaprc",
"ctmc",
"rcat",
"meanfield",
"grouped"};
128 if (std::find(methods.begin(), methods.end(), method) == methods.end()) {
130 for (std::size_t i = 0; i < methods.size(); ++i) list += (i ?
", " :
"") + methods[i];
131 return {
false,
"Finite LAL compositions support " + list +
"; other RCAT methods are not implemented."};
137 snapshot = model_->model();
139 }
catch (
const Error& exc) {
140 return {
false, std::string(exc.what())};
143 if (!features.empty() && method !=
"default" && method !=
"ctmc")
146 return {
false,
"SolverAG method " + method +
" supports timed hand-declared agents only; the composition "
147 "uses " + detail::join_sorted(features) +
" actions (use ctmc)."};
148 if (!features.empty() && method ==
"default")
return {
true, std::nullopt};
149 if (method ==
"inap" || method ==
"inaprc" || method ==
"rcat") {
154 if (method ==
"grouped") {
158 return {
false, std::string(exc.what())};
161 return {
true, std::nullopt};
169 const Model snapshot = model_->model();
171 std::optional<std::string> fallback_reason;
173 if (method ==
"default" || method ==
"ctmc") {
177 if (method !=
"default" || exc.
id() !=
"LINE:unsupported:StateSpace")
throw;
180 fallback_reason = exc.what();
181 method = supported ?
"inaprc" :
"meanfield";
184 if (method ==
"inap" || method ==
"inaprc" || method ==
"rcat") {
188 if (!fallback_reason)
throw;
190 *fallback_reason +
" AG decomposition also refused the model: " + exc.what());
192 if (!result.
converged) warn_(
"SolverAG LAL " + method +
" did not converge within the iteration limit.");
193 if (method ==
"rcat" || fallback_reason) {
195 if (method ==
"rcat" && !cert.first)
197 "RCAT product form could not be certified: " + cert.second);
198 if (fallback_reason && !cert.first) result.
diagnostics[
"certificate_reason"] = cert.second;
201 if (method ==
"meanfield") {
205 if (!fallback_reason)
throw;
207 " Mean-field decomposition also refused the model: " +
210 if (!result.
converged) warn_(
"SolverAG LAL meanfield did not converge within the iteration limit.");
212 if (method ==
"grouped") {
214 model_->source_truncated);
215 if (!result.
converged) warn_(
"SolverAG LAL grouped did not converge within the iteration limit.");
219 result.
truncated = model_->source_truncated;
221 result.
exact =
false;
225 if (method ==
"ctmc" || (method ==
"default" && !fallback_reason)) {
233 a->fallback_reason = fallback_reason;
235 result_ = std::move(result);
265 const std::optional<std::string>& jobclass = std::nullopt,
266 const std::optional<std::string>& mode = std::nullopt) {
267 return getAvgAct(ActionSpec::key(node, event, jobclass, mode));
270 return getAvgAct(ActionSpec::key(node, event, jobclass));
273 std::vector<double>
getActAvg(
const std::vector<ActionSpec>& actions) {
274 std::vector<double> out;
275 for (
const ActionSpec& a : actions) out.push_back(
getAvgAct(a));
282 "Queueing-network averages are not defined for a general LAL composition; "
283 "use getAvgTable() for state probabilities or getActTable() for event rates.");
294 nlohmann::json
getAGDiagnostics(
bool compare_ctmc =
false, std::optional<std::size_t> max_states = std::nullopt) {
297 if (diagnostics.empty())
298 diagnostics = {{
"product_form_certified",
false},
299 {
"certificate_reason",
"The selected method is finite CTMC analysis."}};
301 const std::vector<std::string>
ag{
"inap",
"inaprc",
"rcat",
"meanfield",
"grouped"};
302 if (std::find(
ag.begin(),
ag.end(), r.
method) ==
ag.end())
304 diagnostics[
"ctmc_comparison"] =
compare_with_ctmc(model_->model(), r, max_states);
311 backend_ = std::move(backend);
316 const std::vector<std::string>&
warnings()
const {
return warnings_; }
320 CTMCBackend backend_ = [](
const Model& m, std::optional<std::size_t> max_states) {
323 std::optional<AGResult> result_;
324 std::optional<std::string> signature_;
325 std::vector<std::string> warnings_;
327 std::string key_()
const {
331 const AGResult& ensure_() {
332 if (!result_ || !signature_ || *signature_ != key_())
runAnalyzer();
336 void warn_(
const std::string& message) {
337 warnings_.push_back(message);
338 std::cerr <<
"RuntimeWarning: " << message << std::endl;
LINE Agent Language (LAL): the agent-based (AG) decompositions of a finite composition,...
LINE Agent Language (LAL): programmatic construction of finite agents and their composition,...
Base error for the multiprecision C++ port.
const std::string & id() const noexcept
The full identifier, e.g.
A refusal: the feature, method or analysis is not covered: LINE:unsupported:<kind>.
UnsupportedError(const std::string &what)
A collection of agents and embedded LINE models with atomic synchronisations (Composition).
Model model(const std::vector< Sync > *syncs=nullptr, const std::vector< ExtSync > *ext_syncs=nullptr) const
The current model snapshot (_model).
SolverAG & runAnalyzer()
Run the selected method and store the result (runAnalyzer).
static AGOptions defaultOptions()
static qn::FeatureSet getFeatureSet()
getFeatureSet, static as in SolverAG.m: the RCAT feature envelope of the network arm,...
const AGResult & getAGResult()
The result container, solving first if needed (getAGResult).
StateTable getAvgTable()
State probabilities (getAvgTable).
nlohmann::json getAGDiagnostics(bool compare_ctmc=false, std::optional< std::size_t > max_states=std::nullopt)
Diagnostics, optionally with a CTMC comparison (getAGDiagnostics).
double getAvgAct(std::size_t node, lang::EventType event, std::size_t jobclass=0)
static bool supports(const Composition &)
supports(model): a LAL composition has no queueing-network feature set, so the reference answers true...
void setCTMCBackend(CTMCBackend backend)
Replace the explicit-CTMC backend (Python tests monkeypatch it).
std::vector< Matrix< double > > get_avg_tard()
std::vector< Matrix< double > > getAvgTard()
getAvgTard(), inherited by the reference from NetworkSolver: mean class tardiness against the JobClas...
static std::vector< std::string > listLALMethods()
Methods available for a finite LAL Composition (listLALMethods).
std::function< Result(const Model &, std::optional< std::size_t >)> CTMCBackend
The explicit-CTMC backend of default/ctmc (Python's module-level lal_ctmc.solve_composition); replace...
static std::vector< std::string > listValidMethods()
listValidMethods: on a Composition the reference answers listLALMethods (SolverAG....
double getAvgAct(const ActionSpec &action)
Throughput of one LAL action (named as in SolverCTMC::getAvgAct).
ActionTable getActionTable()
const std::vector< std::string > & warnings() const
Warnings raised by the last solves, in order (also written to std::cerr).
ActionTable getActTable()
Steady-state labelled event rates (getActTable).
double getAvgAct(const std::string &node, lang::EventType event, const std::optional< std::string > &jobclass=std::nullopt, const std::optional< std::string > &mode=std::nullopt)
std::string resolveMethod() const
The identity: default is gated as itself (resolveMethod).
static std::vector< std::string > list_valid_methods()
Compiled getStruct() const
getStruct: on a Composition the reference returns model.getStruct(), the finite CTMC states,...
Support supportsModelMethod(const std::string &method) const
Structural gate for the requested method (supportsModelMethod).
std::vector< double > getActAvg(const std::vector< ActionSpec > &actions)
Throughputs of several actions, each named as in getAvgAct.
void getAvg() const
Queueing-network averages are refused on a general composition (getAvg).
static qn::FeatureSet get_feature_set()
SolverAG(const Composition &model, AGOptions options=AGOptions())
SolverAG(const Composition &&, AGOptions=AGOptions())=delete
Compiled get_struct() const
A subset of the registry: MATLAB's SolverFeatureSet, whose list is a flag per field.
LINE Agent Language (LAL): the result tables every LAL solver returns, the exact finite CTMC analysis...
AGResult rcat_solve(const Model &original, double tol=1e-4, long max_iter=100, const std::string &method="inaprc")
Finite-agent RCAT fixed point (lal_rcat.solve_composition), method inap or inaprc.
std::string signature(const Model &m)
A deterministic text signature of a model snapshot (Python's repr(model)), used by the solvers to cac...
AGResult meanfield_solve(const Model &snapshot, double tol=1e-4, long max_iter=100, double damping=.5)
Finite LAL mean-field approximation (lal_meanfield.solve_composition).
Support certificate_support(const Model &original)
The certificate's conservative structural prerequisites (certificate_support).
AGResult grouped_solve(const Model &snapshot, const std::optional< std::vector< std::vector< std::string > > > &groups, long max_states=256, double tol=1e-4, long max_iter=100, double damping=.5, bool source_truncated=false)
Grouped decomposition (lal_grouped.solve_composition): exact within groups, mean-field across them.
void validate_model(const Model &model, bool allow_unbounded=false)
Validate the common agent model used by both frontends (validate_model).
std::vector< std::vector< std::string > > group_partition(const Model &snapshot, const std::optional< std::vector< std::vector< std::string > > > &groups, long max_states)
Validate and complete a user partition without enumeration (group_partition).
Support structural_support(const Model &original, const std::string &method="inaprc")
Decomposition constraints checked without a global state space (structural_support).
std::pair< bool, std::string > certify_result(const Model &original, AGResult &result, double tol)
Replace an approximate solution with an exactly verified RCAT solution (certify_result).
Result solve_composition(const Model &snapshot, std::size_t max_states=0, std::size_t max_dense_bytes=kDefaultMaxDenseBytes)
Solve a finite composition, respecting its initial state if reducible (solve_composition).
nlohmann::json compare_with_ctmc(const Model &snapshot, const AGResult &result, std::optional< std::size_t > max_states=std::nullopt)
Numerically compare the product of agent marginals with the exact finite CTMC (compare_with_ctmc).
std::set< std::string > model_features(const Model &model)
Features of a model that only the extended kernel interprets (model_features).
EventType
The events a state can undergo, with the values of MATLAB EventType.
FeatureSet ag_feature_set(const std::string &)
SolverAG.getFeatureSet: what the RCAT decomposition can represent.
Conservation laws of a layered queueing network, enumerated from its structure.
The DECLARED side of the gate: one feature set per solver.
Options of the LAL arm of SolverAG (SolverAGOptions).
std::optional< std::vector< std::vector< std::string > > > groups
std::optional< std::size_t > lal_ctmc_max_states
bound on the explicit CTMC of default/ctmc; nullopt is Python's None (dense-storage bound only)
std::string key() const
The cache key (Python repr(self.options)).
The result of an AG decomposition (LALRCATResult, LALMeanFieldResult, LALGroupedResult).
The compiled finite CTMC (core.Compiled).
A composition snapshot (core.Model): agents in declaration order plus synchronisations.
The result container shared by the LAL solver paths (LALCTMCResult, the AG decomposition results,...
nlohmann::json diagnostics
std::optional< std::string > fallback_reason
A support answer: (ok, reason).
std::optional< std::string > reason
The attrs of a LAL result table.
std::optional< bool > exact