LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
markov.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_MARKOV_H
6#define LINE_LANG_LAL_MARKOV_H
7
8/**
9 * @file
10 * @ingroup line_lang
11 * LINE Agent Language (LAL): the Markovian admission check. Port of
12 * python/line_solver/lal/markov.py.
13 *
14 * LAL captures the MARKOVIAN models LINE can declare, and nothing else. Every
15 * law that reaches a composition, declared by hand or carried by an embedded
16 * LINE object, is classified here, and anything outside the accepted set is
17 * refused with `LINE:unsupported:NonMarkovian` rather than fitted.
18 *
19 * The accepted set is EXPLICIT and deliberately narrower than
20 * `lang::process_is_markovian`, which also admits ME and RAP
21 * (matrix-exponential laws with no CTMC representation in general), DMAP
22 * (discrete time) and the time-inhomogeneous marked schedules.
23 *
24 * Classification reads the DECLARED law: `Distrib::declared` when a solver
25 * fitted a surrogate over it, else the law's own type, never a surrogate's
26 * matrices. C++ has no reflection, so `check_network` walks the struct fields
27 * of a network that carry a distribution explicitly: service and arrival,
28 * switchover, polling tables and timers, quantum, patience, orbit impatience,
29 * heterogeneous-server service, retrial, setup and delay-off, breakdown and
30 * repair, Petri-net mode firing laws and fork task-count laws.
31 */
32
33#include <string>
34
41#include "line/util/error.h"
42
43namespace line {
44namespace lal {
45
46/** The error kind of a refusal: `LINE:unsupported:NonMarkovian`. */
47inline const char* nonmarkovian_kind() { return "NonMarkovian"; }
48
49/** How a Markovian law is interpreted: a timed holding time, a zero-time step or no transition. */
51
52inline const char* law_class_name(LawClass c) {
53 return c == LawClass::Timed ? "timed" : (c == LawClass::Immediate ? "immediate" : "disabled");
54}
55
56/** The declared law of a distribution (the original when a solver fitted a surrogate). */
58 return d.declared ? *d.declared : d;
59}
60
61/** A short readable name of a law for refusal messages (`law_name`). */
62inline std::string law_name(const lang::Distrib<double>& d) {
63 return lang::process_to_text(declared_law(d).type);
64}
65
66namespace markov_detail {
67
68inline UnsupportedError refusal(const lang::Distrib<double>& d, const std::string& where) {
69 const std::string place = where.empty() ? std::string() : " at " + where;
71 "LAL admits only Markovian specifications; " + law_name(d) + place +
72 " is not Markovian (accepted: Exp, Erlang, HyperExp, PH, APH, Coxian, Cox2, MAP, "
73 "MMPP, MMPP2, BMAP, MMAP, MarkedMMPP, MPH, Immediate, Disabled).");
74}
75
76/** The discrete laws, accepted only as a cache popularity or a fork task count. */
92
93} // namespace markov_detail
94
95/**
96 * Classify one law as timed, immediate or disabled (`classify`).
97 *
98 * Accepted: Exp, Erlang, HyperExp, PH, APH, Coxian, Cox2, MAP (with MMPP and
99 * MMPP2), BMAP, MMAP (with MarkedMMPP) and MPH, Immediate and Disabled.
100 * Everything else, ME and RAP included, is refused with
101 * `LINE:unsupported:NonMarkovian`.
102 */
127
128/** Classify `dist` and name `where` in the refusal when it is refused (`check_markovian`). */
129inline LawClass check_markovian(const lang::Distrib<double>& dist, const std::string& where) {
130 try {
131 return classify(dist);
132 } catch (const UnsupportedError&) {
133 throw markov_detail::refusal(dist, where);
134 }
135}
136
137/**
138 * Refuse a network holding any non-Markovian specification (`check_network`).
139 *
140 * Walks every node: service, arrival, setup, delay-off, patience, retrial,
141 * switchover, breakdown, heterogeneous-server and Petri-net mode laws. Batch
142 * arrivals (a discrete batch-size law) and fluid Petri-net places are refused
143 * as well; a fork draws its tasks per link from a count law that is not a
144 * holding time, so a discrete law is accepted there.
145 */
146inline void check_network(const qn::NetworkStruct<double>& sn, const std::string& where = std::string()) {
147 const std::string label = where.empty() ? (sn.name.empty() ? std::string("Network") : sn.name) : where;
148 auto cname = [&](std::size_t r) {
149 return r < sn.classes.size() ? sn.classes[r].name : std::to_string(r);
150 };
151 auto check = [&](const lang::Distrib<double>& d, const std::string& path) { check_markovian(d, path); };
152 for (std::size_t ind = 1; ind <= sn.nodes.size(); ++ind) {
153 const qn::NodeDef& nd = sn.nodes[ind - 1];
154 const std::string name = nd.name;
155 const std::string base = label + "." + name;
156 const bool count_laws = nd.nodetype == lang::NodeType::Fork || nd.nodetype == lang::NodeType::Cache;
157 const std::size_t ist = nd.station;
158 if (ist != 0 && ist <= sn.stations.size()) {
159 const qn::Station<double>& st = sn.stations[ist - 1];
160 if (st.fluid)
162 "LAL admits only Markovian specifications; place " + name + " of " + label +
163 " is a fluid place.");
164 for (const lang::Distrib<double>& b : st.arrival_batch)
167 "LAL admits only Markovian specifications; source " + name + " of " +
168 label +
169 " declares batch arrivals with a discrete batch-size law (use a BMAP "
170 "for Markovian batches).");
171 const std::string what = st.nodetype == lang::NodeType::Source ? "arrival" : "service";
172 if (ist - 1 < sn.service.size())
173 for (std::size_t r = 0; r < sn.service[ist - 1].size(); ++r)
174 check(sn.service[ist - 1][r], base + "." + what + "[" + cname(r) + "]");
175 for (std::size_t r = 0; r < st.switchover.size(); ++r)
176 check(st.switchover[r], base + ".switchover[" + cname(r) + "]");
177 for (std::size_t k = 0; k < st.polling_table_sw.size(); ++k)
178 check(st.polling_table_sw[k], base + ".polling_table_switchover[" + std::to_string(k) + "]");
179 for (std::size_t r = 0; r < st.polling_timer.size(); ++r)
180 check(st.polling_timer[r], base + ".polling_timer[" + cname(r) + "]");
181 for (std::size_t r = 0; r < st.switchover_pair.size(); ++r)
182 for (std::size_t s = 0; s < st.switchover_pair[r].size(); ++s)
183 check(st.switchover_pair[r][s], base + ".switchover[" + cname(r) + "][" + cname(s) + "]");
184 for (std::size_t r = 0; r < st.quantum.size(); ++r)
185 check(st.quantum[r], base + ".quantum[" + cname(r) + "]");
186 for (std::size_t r = 0; r < st.patience.size(); ++r)
187 check(st.patience[r], base + ".patience[" + cname(r) + "]");
188 for (std::size_t r = 0; r < st.orbit_impatience.size(); ++r)
189 check(st.orbit_impatience[r], base + ".orbit_impatience[" + cname(r) + "]");
190 for (const auto& stype : st.server_types)
191 for (std::size_t r = 0; r < stype.service.size(); ++r)
192 check(stype.service[r], base + ".server_type[" + stype.name + "][" + cname(r) + "]");
193 const auto rp = sn.retrialparam.find(ist);
194 if (rp != sn.retrialparam.end())
195 for (std::size_t r = 0; r < rp->second.retrial_proc.size(); ++r)
196 check(rp->second.retrial_proc[r], base + ".retrial[" + cname(r) + "]");
197 const auto sp = sn.setupparam.find(ist);
198 if (sp != sn.setupparam.end()) {
199 for (std::size_t r = 0; r < sp->second.setup.size(); ++r)
200 check(sp->second.setup[r], base + ".setup[" + cname(r) + "]");
201 for (std::size_t r = 0; r < sp->second.delayoff.size(); ++r)
202 check(sp->second.delayoff[r], base + ".delayoff[" + cname(r) + "]");
203 }
204 const auto bp = sn.breakdownparam.find(ist);
205 if (bp != sn.breakdownparam.end()) {
206 check(bp->second.failure, base + ".breakdown_failure");
207 check(bp->second.repair, base + ".breakdown_repair");
208 }
209 // `set_polling` stores its controller in `sn.pollingparam`, NOT in
210 // the Station fields walked above (see
211 // `NetworkStruct::effective_polling`), so its switchover, table
212 // switchover and timer laws are walked here under the same names.
213 const auto pp = sn.pollingparam.find(ist);
214 if (pp != sn.pollingparam.end()) {
215 for (std::size_t r = 0; r < pp->second.switchover.size(); ++r)
216 check(pp->second.switchover[r], base + ".switchover[" + cname(r) + "]");
217 for (std::size_t k = 0; k < pp->second.table_sw.size(); ++k)
218 check(pp->second.table_sw[k], base + ".polling_table_switchover[" + std::to_string(k) + "]");
219 for (std::size_t r = 0; r < pp->second.timer.size(); ++r)
220 check(pp->second.timer[r], base + ".polling_timer[" + cname(r) + "]");
221 }
222 }
223 const auto tp = sn.transparam.find(ind);
224 if (tp != sn.transparam.end())
225 for (std::size_t m = 0; m < tp->second.firingproc.size(); ++m) {
226 const std::string mode =
227 m < tp->second.modenames.size() ? tp->second.modenames[m] : "mode" + std::to_string(m);
228 check(tp->second.firingproc[m], base + ".distribution[" + mode + "]");
229 }
230 const auto fp = sn.forkparam.find(ind);
231 if (fp != sn.forkparam.end())
232 for (std::size_t k = 0; k < fp->second.fan_out_dist.size(); ++k)
233 for (std::size_t r = 0; r < fp->second.fan_out_dist[k].size(); ++r) {
234 const lang::Distrib<double>& d = fp->second.fan_out_dist[k][r];
235 if (count_laws && markov_detail::is_discrete_law(declared_law(d).type)) continue;
236 check(d, base + ".fan_out[" + std::to_string(k + 1) + "][" + cname(r) + "]");
237 }
238 }
239}
240
241/** Refuse a LayeredNetwork with a non-Markovian demand, think time, setup or arrival (`check_layered`). */
242inline void check_layered(const lqn::LqnStruct<double>& lsn, const std::string& name) {
243 const std::string label = name.empty() ? std::string("LayeredNetwork") : name;
244 auto ename = [&](std::size_t i) { return i < lsn.names.size() ? lsn.names[i] : std::to_string(i); };
245 auto walk = [&](const std::vector<lang::Distrib<double>>& v, const char* field) {
246 for (std::size_t i = 0; i < v.size(); ++i) check_markovian(v[i], label + "." + ename(i) + "." + field);
247 };
248 walk(lsn.hostdem, "host_demand");
249 walk(lsn.think, "think_time");
250 walk(lsn.actthink, "think_time");
251 walk(lsn.arrival, "arrival");
252 walk(lsn.setuptime, "setup_time");
253 walk(lsn.delayofftime, "delay_off_time");
254}
255
256/** Refuse an Environment with a non-Markovian holding law or stage model (`check_environment`). */
258 const std::string label = e.name().empty() ? std::string("Environment") : e.name();
259 const std::size_t E = e.nstages();
260 for (std::size_t s = 0; s < E; ++s)
261 for (std::size_t h = 0; h < E; ++h) {
262 const env::EnvArc<double>& a = e.arc(s, h);
263 if (!a.enabled) continue;
264 check_markovian(a.dist, label + ".env[" + std::to_string(s) + "][" + std::to_string(h) + "]");
265 }
266 for (std::size_t s = 0; s < E; ++s)
267 if (e.stage(s).has_model) check_network(e.stage(s).model);
268}
269
270/** Refuse a Workflow with a non-Markovian activity law. */
272 const std::string wname = wf.name().empty() ? std::string("Workflow") : wf.name();
273 for (const auto& act : wf.activities()) check_markovian(act.host_demand(), wname + "." + act.name() + ".host_demand");
274}
275
276} // namespace lal
277} // namespace line
278
279#endif // LINE_LANG_LAL_MARKOV_H
A refusal: the feature, method or analysis is not covered: LINE:unsupported:<kind>.
Definition error.h:117
UnsupportedError(const std::string &what)
Definition error.h:119
const std::string & name() const
const EnvStage< T > & stage(std::size_t e) const
std::size_t nstages() const
const EnvArc< T > & arc(std::size_t e, std::size_t h) const
A network plus its refreshed NetworkStruct.
A random environment: a port of matlab/src/lang/Environment.m, restricted to what SolverENV reads out...
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
.lqnx -> LqnStruct, a port of matlab/src/lang/layered/@LayeredNetwork/parseXML.m followed by ....
LayeredNetworkStruct, the flattened description of a layered queueing network.
UnsupportedError refusal(const lang::Distrib< double > &d, const std::string &where)
Definition markov.h:68
bool is_discrete_law(lang::ProcessType t)
The discrete laws, accepted only as a cache popularity or a fork task count.
Definition markov.h:77
Definition ag.h:49
void check_network(const qn::NetworkStruct< double > &sn, const std::string &where=std::string())
Refuse a network holding any non-Markovian specification (check_network).
Definition markov.h:146
void check_workflow(const workflow::Workflow< double > &wf)
Refuse a Workflow with a non-Markovian activity law.
Definition markov.h:271
void check_environment(const env::Environment< double > &e)
Refuse an Environment with a non-Markovian holding law or stage model (check_environment).
Definition markov.h:257
LawClass
How a Markovian law is interpreted: a timed holding time, a zero-time step or no transition.
Definition markov.h:50
const char * nonmarkovian_kind()
The error kind of a refusal: LINE:unsupported:NonMarkovian.
Definition markov.h:47
std::string law_name(const lang::Distrib< double > &d)
A short readable name of a law for refusal messages (law_name).
Definition markov.h:62
void check_layered(const lqn::LqnStruct< double > &lsn, const std::string &name)
Refuse a LayeredNetwork with a non-Markovian demand, think time, setup or arrival (check_layered).
Definition markov.h:242
LawClass check_markovian(const lang::Distrib< double > &dist, const std::string &where)
Classify dist and name where in the refusal when it is refused (check_markovian).
Definition markov.h:129
LawClass classify(const lang::Distrib< double > &dist)
Classify one law as timed, immediate or disabled (classify).
Definition markov.h:103
const lang::Distrib< double > & declared_law(const lang::Distrib< double > &d)
The declared law of a distribution (the original when a solver fitted a surrogate).
Definition markov.h:57
const char * law_class_name(LawClass c)
Definition markov.h:52
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
Definition lang_types.h:524
@ MPH
The MARKED families, MATLAB's ProcessType.m:36-38.
Definition lang_types.h:598
const char * process_to_text(ProcessType p)
The MATLAB ProcessType name, as sn.procid prints it.
Definition lang_types.h:655
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
ErrorKind(std::string n)
Definition error.h:39
One arc of the environment process.
lang::Distrib< T > dist
the e -> h transition time
std::shared_ptr< Distrib< T > > declared
The law as DECLARED, when this one is a surrogate fitted over it.
Definition lang_types.h:889
A node of the network.
One station of the network.
std::vector< Distrib< T > > orbit_impatience
Queue.setOrbitImpatience(class, dist): abandonment from the RETRIAL ORBIT, which is a different popul...
std::vector< Distrib< T > > patience
Queue.setPatience(class, dist): the abandonment timer of a WAITING job, with impatience[r] naming whi...
std::vector< Distrib< T > > quantum
Queue.setQuantum(class, distrib): the per-class time slice of a round-robin (SchedStrategy::RR) stati...
std::vector< Distrib< T > > polling_timer
std::vector< std::vector< Distrib< T > > > switchover_pair
Queue.setSwitchover(fromClass, toClass, distrib): the walk the server takes when it turns from servin...
std::vector< Distrib< T > > polling_table_sw
std::vector< Distrib< T > > arrival_batch
Source.setArrivalBatch(class, dist): the batch-size law released at each arrival epoch.
std::vector< Distrib< T > > switchover
bool fluid
Place.setFluid (MATLAB sn.fluid): the place holds a continuous LEVEL rather than tokens,...
std::vector< ServerType > server_types
An activity workflow reduced to one phase-type law.