9#ifndef LINE_LANG_LAL_EMBED_H
10#define LINE_LANG_LAL_EMBED_H
79inline std::string
identifier_from(
const std::string& name,
const std::string& fallback) {
80 return detail::is_identifier(name) ? name : fallback;
84inline std::string
fmt_g(
double v) {
86 std::snprintf(buf,
sizeof(buf),
"%g", v);
92 const std::vector<double>& row) {
93 const std::size_t K =
sn.nclasses;
94 std::vector<std::size_t> ph(K, 1), shift(K, 0);
96 for (std::size_t k = 0; k < K; ++k) {
97 ph[k] =
sn.phasessz_of(ist, k + 1);
128 std::optional<double> cutoff)
129 : network_(std::move(network)), prefix_(
prefix), cutoff_(cutoff) {
130 check_network(network_->raw_struct(), network_->raw_struct().name);
131 name_ = prefix_.empty() ? network_->raw_struct().name : prefix_;
134 : stage_(std::make_shared<
qn::NetworkStruct<double>>(
sn)), prefix_(
prefix), cutoff_(cutoff) {
136 name_ = prefix_.empty() ? stage_->name : prefix_;
139 const std::string&
name()
const override {
return name_; }
140 const std::string&
prefix()
const override {
return prefix_; }
146 const std::vector<NodeEvent>&
labels()
override {
150 std::vector<EmbeddedArc>
outgoing(std::size_t s)
override {
154 const std::vector<EmbeddedArc>&
arcs(std::size_t s) {
160 return vanishing_[s];
164 return any_vanishing_;
168 return space_.size();
174 const std::vector<qn::NetState<double>>&
space() {
184 const auto it = index_.find(key_(state,
nullptr));
185 return it == index_.end() ?
static_cast<std::size_t
>(-1) : it->second;
194 if (classes != injected_) {
209 std::vector<std::pair<std::size_t, double>>
inject(std::size_t s,
const NodeEvent& key)
override {
211 const std::size_t node = node_position(key.
node);
212 const std::size_t cls = class_position(key.
index ? *key.
index : std::string());
213 const std::size_t isf = sn_->stateful_index(node);
216 std::vector<std::pair<std::size_t, double>> result;
217 for (std::size_t i = 0; i < out.
space.size(); ++i) {
220 const auto it = index_.find(key_(target, buf_.empty() ?
nullptr : &buf_[s]));
221 const double p = i < out.
prob.size() ? out.
prob[i] : 1.0;
222 if (it != index_.end() && p > 0) result.push_back(std::make_pair(it->second, p));
229 throw value_error(
"only ARV node actions can be driven by a hand agent, not " + key.
label());
232 std::size_t node = 0, cls = 0;
233 for (std::size_t i = 0; i <
sn.
nodes.size(); ++i)
235 for (std::size_t r = 0; r <
sn.
classes.size(); ++r)
237 if (node == 0)
throw value_error(
"network " + name_ +
" has no node '" + key.
node +
"'");
238 if (cls == 0)
throw value_error(
"network " + name_ +
" has no class '" + *key.
index +
"'");
239 if (!
sn.
nodes[node - 1].stateful)
240 throw value_error(key.
node +
" is not a stateful node and cannot receive an injected arrival");
241 const std::vector<double> nj =
sn.
njobs();
242 if (cls - 1 < nj.size() && std::isfinite(nj[cls - 1]))
245 ": it would break the population of the chain LINE enumerates.");
249 const std::optional<std::string>& mode)
override {
253 if (nd.
name == node) found =
true;
254 if (!found)
throw value_error(
"network " + name_ +
" has no node '" + node +
"'");
255 if (mode && jobclass)
throw value_error(
"give jobclass or mode, not both");
256 std::optional<std::string> index;
259 }
else if (jobclass) {
262 if (c.
name == *jobclass) known =
true;
263 if (!known)
throw value_error(
"network " + name_ +
" has no class '" + *jobclass +
"'");
266 return NodeEvent{prefix_, node, event, index};
271 if (node == 0 || node >
sn.
nodes.size())
throw value_error(
"network " + name_ +
" has no node " + std::to_string(node));
272 return sn.
nodes[node - 1].name;
277 throw value_error(
"network " + name_ +
" has no class " + std::to_string(cls));
281 std::vector<std::pair<std::string, std::size_t>>
node_states()
override {
283 std::vector<std::pair<std::string, std::size_t>> out;
284 for (std::size_t isf = 1; isf <= sn_->stateful_nodes.size(); ++isf)
285 out.push_back(std::make_pair(sn_->nodes[sn_->stateful_nodes[isf - 1] - 1].name, isf));
291 const std::vector<double>& row = space_[s].local[isf - 1];
292 std::string out =
"[";
293 for (std::size_t i = 0; i < row.size(); ++i) out += (i ?
" " :
"") +
embed_detail::fmt_g(row[i]);
297 std::vector<double>
node_row(std::size_t s,
const std::string& node)
override {
299 const std::size_t ind = node_position(node);
300 const std::size_t isf = sn_->stateful_index(ind);
301 if (isf == 0)
throw value_error(node +
" is not a stateful node");
302 return space_[s].local[isf - 1];
305 double node_jobs(std::size_t s,
const std::string& node,
const std::string& jobclass)
override {
307 const std::size_t ind = node_position(node);
308 const std::size_t isf = sn_->stateful_index(ind);
309 if (isf == 0)
throw value_error(node +
" is not a stateful node");
310 const std::size_t ist = sn_->nodes[ind - 1].station;
311 if (ist == 0)
throw value_error(node +
" is not a station and holds no jobs");
313 if (jobclass.empty()) {
315 for (
double x : m.
nir) total += x;
318 return m.
nir[class_position(jobclass) - 1];
322 const std::pair<Matrix<double>, std::vector<std::vector<std::vector<double>>>>&
queue_lengths(std::size_t x) {
324 auto it = qmemo_.find(x);
325 if (it != qmemo_.end())
return it->second;
326 const std::size_t M = sn_->nstations, K = sn_->nclasses;
328 std::vector<std::vector<std::vector<double>>> kir(M);
329 for (std::size_t ist = 1; ist <= M; ++ist) {
330 const std::size_t isf = sn_->stateful_of_station(ist);
339 if (isf == 0)
continue;
342 for (std::size_t k = 0; k < K; ++k) q(ist - 1, k) = m.
nir[k];
343 kir[ist - 1] = m.
kir;
345 return qmemo_.emplace(x, std::make_pair(q, kir)).first->second;
349 std::shared_ptr<qn::Network<double>> network_;
350 std::shared_ptr<qn::NetworkStruct<double>> stage_;
351 std::string prefix_, name_;
352 std::optional<double> cutoff_;
353 std::set<std::string> injected_;
356 std::shared_ptr<const qn::NetworkStruct<double>> sn_;
366 std::vector<std::string> class_names_;
367 std::vector<qn::NetState<double>> space_;
373 std::vector<std::vector<std::vector<std::size_t>>> buf_;
374 std::map<std::vector<double>, std::size_t> index_;
375 std::vector<bool> vanishing_;
376 bool any_vanishing_ =
false;
377 std::size_t initial_ = 0;
378 std::vector<std::vector<EmbeddedArc>> arcs_;
379 std::vector<NodeEvent> labels_;
380 std::map<std::size_t, std::pair<Matrix<double>, std::vector<std::vector<std::vector<double>>>>> qmemo_;
384 class_names_.clear();
389 any_vanishing_ =
false;
401 const std::vector<std::vector<std::size_t>>* buf)
const {
402 if (buf_.empty())
return ctmc::ctmc_detail::state_key(state);
405 ws.buf = buf ? *buf : std::vector<std::vector<std::size_t>>(buf_.front().size());
406 return ctmc::waitq_detail::waitq_key(
ws);
410 const qn::NetworkStruct<double>& names_struct() {
return network_ ? network_->raw_struct() : *stage_; }
413 const qn::NetworkStruct<double>& fresh_struct() {
return network_ ? network_->get_struct() : *stage_; }
421 std::size_t node_position(
const std::string& node)
const {
422 for (std::size_t i = 0; i < sn_->nodes.size(); ++i)
423 if (sn_->nodes[i].name == node)
return i + 1;
424 throw value_error(
"network " + name_ +
" has no node '" + node +
"'");
426 std::size_t class_position(
const std::string& cls)
const {
427 for (std::size_t r = 0; r < sn_->classes.size(); ++r)
428 if (sn_->classes[r].name == cls)
return r + 1;
429 throw value_error(
"network " + name_ +
" has no class '" + cls +
"'");
444 qn::NetworkStruct<double> enumeration_struct(std::set<std::pair<std::size_t, std::size_t>>& placeholders) {
445 const qn::NetworkStruct<double>& names = names_struct();
446 std::vector<std::pair<std::size_t, std::size_t>> todo;
447 for (
const std::string& cname : injected_) {
449 for (std::size_t r = 0; r < names.classes.size(); ++r)
450 if (names.classes[r].name == cname) cls = r + 1;
451 if (cls == 0)
continue;
452 for (std::size_t ind = 1; ind <= names.nodes.size(); ++ind) {
453 const qn::NodeDef& nd = names.nodes[ind - 1];
455 const bool disabled = nd.station - 1 >= names.service.size() ||
456 cls - 1 >= names.service[nd.station - 1].size() ||
458 if (disabled) todo.push_back(std::make_pair(ind, cls));
461 placeholders.clear();
462 for (
const auto& t : todo) placeholders.insert(t);
464 if (todo.empty())
return network_->get_struct();
465 qn::Network<double> copy = *network_;
467 return copy.get_struct();
469 qn::NetworkStruct<double> copy = *stage_;
471 for (
const auto& t : todo) {
474 copy.set_service(copy.nodes[t.first - 1].station, t.second, d);
476 copy.refresh_struct();
483 enum class Kind { Sync, Mode, Fork };
484 Kind kind = Kind::Sync;
486 std::size_t node = 0;
488 std::size_t mode = 0;
492 bool released =
false;
495 std::string mode_name(std::size_t node, std::size_t mode0)
const {
496 const auto it = sn_->transparam.find(node);
497 if (it != sn_->transparam.end() && mode0 < it->second.modenames.size() && !it->second.modenames[mode0].empty())
498 return it->second.modenames[mode0];
499 return "mode" + std::to_string(mode0);
503 std::optional<NodeEvent> key_of(
const Rec& r)
const {
504 if (r.node == 0 || r.node > sn_->nodes.size())
return std::nullopt;
505 const std::string& nname = sn_->nodes[r.node - 1].name;
506 if (r.kind == Rec::Kind::Fork)
510 return NodeEvent{prefix_, nname, r.event, mode_name(r.node, r.mode - 1)};
512 return NodeEvent{prefix_, nname, r.event, mode_name(r.node, r.cls - 1)};
513 std::optional<std::string> index;
514 if (r.cls >= 1 && r.cls <= class_names_.size()) index = class_names_[r.cls - 1];
515 return NodeEvent{prefix_, nname, r.event, index};
519 std::vector<embed_detail::Hop> close_(std::vector<embed_detail::Hop> hops, std::size_t cls0)
const {
530 std::vector<NodeEvent> hops;
539 std::vector<Path> paths_(std::size_t node_a0, std::size_t class_a0, std::size_t node_p0, std::size_t class_p0)
const {
540 const std::size_t K = sn_->nclasses;
541 const Matrix<double>& rt = sn_->rtnodes;
542 std::vector<std::pair<double, std::pair<std::vector<embed_detail::Hop>,
bool>>> out;
543 std::function<void(std::size_t, std::size_t,
double,
const std::vector<embed_detail::Hop>&,
544 const std::set<std::pair<std::size_t, std::size_t>>&)>
545 walk = [&](std::size_t node, std::size_t cls,
double prob,
const std::vector<embed_detail::Hop>& hops,
546 const std::set<std::pair<std::size_t, std::size_t>>& seen) {
547 const std::size_t row = node * K + cls;
548 if (row >= rt.rows())
return;
549 for (std::size_t col = 0; col < rt.
cols(); ++col) {
550 const double v = rt(row, col);
551 if (!(v > 0))
continue;
552 const std::size_t nxt = col / K, ncls = col % K;
553 const double q = prob * v;
554 if (sn_->nodes[nxt].stateful) {
555 if (!hops.empty() && hops.back().sink) {
556 if (nxt == node_p0 && ncls == class_p0) out.push_back({q, {hops,
true}});
559 if (nxt == node_p0 && ncls == class_p0) out.push_back({q, {close_(hops, ncls),
false}});
562 if (seen.count(std::make_pair(nxt, ncls)))
564 "LAL cannot embed network " + name_ +
565 ": the routing cycles among non-stateful nodes through " +
566 sn_->nodes[nxt].name +
".");
567 std::vector<embed_detail::Hop> hop = close_(hops, ncls);
568 embed_detail::Hop arv;
572 embed_detail::Hop mark;
576 std::set<std::pair<std::size_t, std::size_t>> seen2 = seen;
577 seen2.insert(std::make_pair(nxt, ncls));
578 walk(nxt, ncls, q, hop, seen2);
581 walk(node_a0, class_a0, 1.0, std::vector<embed_detail::Hop>(), std::set<std::pair<std::size_t, std::size_t>>());
582 std::vector<Path> paths;
583 for (
const auto& o : out) {
586 for (
const embed_detail::Hop& h : o.second.first)
587 if (!h.sink) p.hops.push_back(h.key);
588 p.via_sink = o.second.second;
609 std::vector<std::pair<double, std::vector<NodeEvent>>> split_(
const std::vector<Rec>& actives,
610 const std::vector<Rec>& all_passives)
const {
611 std::vector<Rec> passives;
612 std::vector<NodeEvent> extra;
613 for (
const Rec& r : all_passives) {
615 passives.push_back(r);
618 if (
const auto k = key_of(r)) extra.push_back(*k);
620 if (actives.size() > 1 && actives.size() == passives.size()) {
621 std::vector<std::pair<double, std::vector<NodeEvent>>> combos{
622 std::make_pair(1.0, std::vector<NodeEvent>())};
623 for (std::size_t i = 0; i < actives.size(); ++i) {
624 const std::vector<std::pair<double, std::vector<NodeEvent>>> one =
625 split_(std::vector<Rec>{actives[i]}, std::vector<Rec>{passives[i]});
626 std::vector<std::pair<double, std::vector<NodeEvent>>> next;
627 for (
const auto& c : combos)
628 for (
const auto& o : one) {
629 std::vector<NodeEvent> keys = c.second;
630 keys.insert(keys.end(), o.second.begin(), o.second.end());
631 next.push_back(std::make_pair(c.first * o.first, keys));
635 for (
auto& c : combos) c.second.insert(c.second.end(), extra.begin(), extra.end());
638 const bool parked = passives.size() == 1 && passives[0].parked;
639 std::vector<NodeEvent> base;
640 for (
const Rec& r : actives)
641 if (
const auto k = key_of(r)) base.push_back(*k);
643 for (
const Rec& r : passives)
644 if (
const auto k = key_of(r)) base.push_back(*k);
645 std::vector<NodeEvent> flat = base;
646 flat.insert(flat.end(), extra.begin(), extra.end());
647 const std::size_t N = sn_->nodes.size(), K = sn_->nclasses;
648 const bool has_rt = sn_->rtnodes.rows() >= N * K && sn_->rtnodes.cols() >= N * K && N * K > 0;
649 if (actives.size() != 1 || passives.size() != 1 || actives[0].kind != Rec::Kind::Sync ||
651 return {std::make_pair(1.0, flat)};
652 const Rec& a = actives[0];
653 const Rec& p = passives[0];
654 const std::vector<Path> paths = paths_(a.node - 1, a.cls - 1, p.node - 1, p.cls - 1);
656 for (
const Path& x : paths) total += x.prob;
657 if (paths.empty() || !(total > 0))
return {std::make_pair(1.0, flat)};
658 const std::optional<NodeEvent> akey = key_of(a), pkey = key_of(p);
659 std::vector<std::pair<double, std::vector<NodeEvent>>> out;
660 for (
const Path& x : paths) {
661 std::vector<NodeEvent> keys;
662 if (akey) keys.push_back(*akey);
663 keys.insert(keys.end(), x.hops.begin(), x.hops.end());
664 if (!x.via_sink && !parked && pkey) keys.push_back(*pkey);
665 keys.insert(keys.end(), extra.begin(), extra.end());
666 out.push_back(std::make_pair(x.prob / total, keys));
673 std::set<std::pair<std::size_t, std::size_t>> placeholders;
674 const qn::NetworkStruct<double> net = enumeration_struct(placeholders);
675 ctmc::CtmcOptions opt;
676 if (cutoff_) opt.cutoff = *cutoff_;
677 opt.lal_collect = std::make_shared<ctmc::LalCollect>();
678 opt.lal_collect->stop =
true;
685 const ctmc::LalCollect& c = *opt.lal_collect;
688 "LAL could not embed network " + name_ +
689 ": SolverCTMC did not build its generator through the sync-action builder "
691 if (c.initial_index ==
static_cast<std::size_t
>(-1))
693 "LAL could not locate the initial state of network " + name_ +
694 " in its enumerated state space; check the initial state and the cutoff.");
696 class_names_.clear();
697 for (
const qn::JobClass& jc : sn_->classes) class_names_.push_back(jc.name);
699 for (std::size_t a = 0; a < sn_->fjclassmap.size() && a < class_names_.size(); ++a) {
700 const std::size_t orig = sn_->fjclassmap[a];
701 if (orig >= 1 && orig <= class_names_.size()) class_names_[a] = class_names_[orig - 1];
706 for (std::size_t s = 0; s < space_.size(); ++s) index_[key_(space_[s], buf_.empty() ?
nullptr : &buf_[s])] = s;
707 vanishing_.assign(space_.size(),
false);
708 any_vanishing_ =
false;
709 for (std::size_t v : c.vanishing)
710 if (v < vanishing_.size()) {
711 vanishing_[v] =
true;
712 any_vanishing_ =
true;
714 initial_ = c.initial_index;
715 arcs_.assign(space_.size(), std::vector<EmbeddedArc>());
717 std::set<NodeEvent> seen;
718 const std::size_t local = sn_->nodes.size() + 1;
719 for (
const ctmc::LalCollectedEvent& ev : c.events) {
720 std::vector<Rec> actives, passives;
722 const qn::Sync<double>& sy = c.sync[ev.index];
724 placeholders.count(std::make_pair(sy.active.node, sy.active.cls)))
727 a.kind = Rec::Kind::Sync;
728 a.event = sy.active.event;
729 a.node = sy.active.node;
730 a.cls = sy.active.cls;
731 actives.push_back(a);
732 if (sy.passive.node != local) {
734 p.kind = Rec::Kind::Sync;
735 p.event = sy.passive.event;
736 p.node = sy.passive.node;
737 p.cls = sy.passive.cls;
738 p.parked = ev.parked;
739 passives.push_back(p);
741 for (
const auto& rl : ev.released) {
743 p.kind = Rec::Kind::Sync;
748 passives.push_back(p);
752 const qn::Sync<double>& sy = c.sync[ev.index];
754 a.kind = Rec::Kind::Sync;
755 a.event = sy.active.event;
756 a.node = sy.active.node;
757 a.cls = sy.active.cls;
758 for (
const auto& job : ev.jobs) {
759 const qn::Sync<double>& sj = c.sync[job.first];
761 p.kind = Rec::Kind::Sync;
762 p.event = sj.passive.event;
763 p.node = sj.passive.node;
764 p.cls = sj.passive.cls;
765 p.parked = job.second;
766 actives.push_back(a);
767 passives.push_back(p);
770 const qn::GlobalSync<double>& g = c.gsync[ev.index];
772 a.kind = Rec::Kind::Mode;
773 a.event = g.active.event;
774 a.node = g.active.node;
775 a.cls = g.active.cls;
776 a.mode = g.active.mode;
777 actives.push_back(a);
778 for (
const qn::ModeEvent<double>& pe : g.passive) {
780 p.kind = Rec::Kind::Mode;
785 passives.push_back(p);
788 const qn::FjSync<double>& f = c.fjsync[ev.index];
790 a.kind = Rec::Kind::Fork;
794 actives.push_back(a);
797 for (
const auto& fk : split_(actives, passives)) {
798 for (
const NodeEvent& k : fk.second)
799 if (seen.insert(k).second) labels_.push_back(k);
800 for (
const auto& arc : ev.arcs) {
801 const double rate = std::get<2>(arc);
802 if (!(rate > 0))
continue;
804 e.target = std::get<1>(arc);
806 e.rate = rate * fk.first;
807 e.immediate = ev.immediate;
808 arcs_[std::get<0>(arc)].push_back(e);
837 : env_(std::make_shared<
env::Environment<double>>(e)),
840 for (std::size_t s = 0; s < env_->nstages(); ++s)
843 "LAL embeds an Environment of Network stages; stage " + env_->stage(s).name +
844 " holds a LayeredNetwork.");
847 const std::string&
name()
const override {
return prefix_; }
848 const std::string&
prefix()
const override {
return prefix_; }
854 const std::vector<NodeEvent>&
labels()
override {
860 const std::pair<std::size_t, std::size_t>& cx = state_list_[s];
861 return nets()[clocks_[cx.first].stage]->is_vanishing(cx.second);
864 for (
const auto& n : nets())
865 if (n->has_vanishing())
return true;
874 state_index_.clear();
878 const std::optional<std::string>& mode)
override {
880 return nets()[0]->resolve(node, event, jobclass, mode);
882 std::string
node_name(std::size_t node)
override {
return nets()[0]->node_name(node); }
883 std::string
class_name(std::size_t cls)
override {
return nets()[0]->class_name(cls); }
887 "injection into an embedded Environment is not supported: its stage models are "
888 "re-entered with reset policies that an external arrival stream does not carry.");
890 std::vector<std::pair<std::size_t, double>>
inject(std::size_t,
const NodeEvent& key)
override {
892 return std::vector<std::pair<std::size_t, double>>();
895 std::vector<std::pair<std::string, std::size_t>>
node_states()
override {
return nets()[0]->node_states(); }
898 const std::pair<std::size_t, std::size_t>& cx = state_list_[s];
899 return nets()[clocks_[cx.first].stage]->node_state_label(cx.second, isf);
901 std::vector<double>
node_row(std::size_t s,
const std::string& node)
override {
903 const std::pair<std::size_t, std::size_t>& cx = state_list_[s];
904 return nets()[clocks_[cx.first].stage]->node_row(cx.second, node);
906 double node_jobs(std::size_t s,
const std::string& node,
const std::string& jobclass)
override {
908 const std::pair<std::size_t, std::size_t>& cx = state_list_[s];
909 return nets()[clocks_[cx.first].stage]->node_jobs(cx.second, node, jobclass);
915 return clocks_[state_list_[s].first].stage;
918 std::vector<std::string> out;
919 for (std::size_t e = 0; e < env_->nstages(); ++e) {
920 const std::string& n = env_->stage(e).model.name;
921 out.push_back(n.empty() ?
"stage" + std::to_string(e) : n);
926 std::vector<EmbeddedArc>
outgoing(std::size_t s)
override {
928 const std::size_t c = state_list_[s].first, x = state_list_[s].second;
929 const std::size_t e = clocks_[c].stage;
930 const std::vector<std::size_t> phases = clocks_[c].phases;
932 std::vector<EmbeddedArc> out;
935 b.
target = state(c, a.target);
938 const StageArcs& sa = stages_[e];
939 if (sa.arcs.empty())
return out;
940 auto fire = [&](std::size_t h,
double rate) {
942 for (
const auto& w : entry_[h]) total += w.second;
943 for (
const auto& yp : carry(x, e, h)) {
944 for (
const auto& w : entry_[h]) {
946 b.
target = state(w.first, yp.first);
947 b.
keys = std::vector<NodeEvent>{stage_key(e, h)};
948 b.
rate = rate * yp.second * w.second / total;
955 const std::size_t k = phases[0], ph = phases[1];
956 const StageArc& arc = sa.arcs[k];
957 for (std::size_t j = 0; j < arc.D0.
cols(); ++j)
958 if (j != ph && arc.D0(ph, j) > 0) {
960 b.
target = state(clock_index_.at(Clock{e, {k, j}}), x);
961 b.rate = arc.D0(ph, j);
965 for (std::size_t j = 0; j < arc.D1.cols(); ++j) done += arc.D1(ph, j);
966 if (done > 0) fire(arc.dest, done);
969 for (std::size_t k = 0; k < sa.arcs.size(); ++k) {
970 const StageArc& arc = sa.arcs[k];
971 const std::size_t ph = phases[k];
972 for (std::size_t j = 0; j < arc.D0.cols(); ++j)
973 if (j != ph && arc.D0(ph, j) > 0) {
974 std::vector<std::size_t> nxt = phases;
977 b.
target = state(clock_index_.at(Clock{e, nxt}), x);
978 b.
rate = arc.D0(ph, j);
982 for (std::size_t j = 0; j < arc.D1.cols(); ++j) done += arc.D1(ph, j);
983 if (done > 0) fire(arc.dest, done);
990 std::size_t dest = 0;
991 Matrix<double> D0, D1;
992 std::vector<double> alpha;
997 std::vector<StageArc> arcs;
1001 std::vector<std::size_t> phases;
1002 bool operator<(
const Clock& o)
const {
return std::tie(stage, phases) < std::tie(o.stage, o.phases); }
1007 env::ResetMarginal fun;
1008 env::ResetStateVec<double> state_fun;
1013 std::vector<std::size_t> states;
1014 std::vector<std::size_t> position;
1017 std::shared_ptr<env::Environment<double>> env_;
1018 std::map<std::size_t, Tangible> tangible_;
1019 std::string prefix_;
1020 std::optional<double> cutoff_;
1021 bool built_ =
false;
1022 std::vector<std::shared_ptr<EmbeddedNetwork>> nets_;
1023 std::vector<StageArcs> stages_;
1024 std::vector<Clock> clocks_;
1025 std::map<Clock, std::size_t> clock_index_;
1026 std::vector<std::vector<std::pair<std::size_t, double>>> entry_;
1027 std::map<std::pair<std::size_t, std::size_t>, Reset> resets_;
1028 std::vector<NodeEvent> labels_;
1029 std::size_t initial_ = 0;
1030 std::vector<std::pair<std::size_t, std::size_t>> state_list_;
1031 std::map<std::pair<std::size_t, std::size_t>, std::size_t> state_index_;
1033 const std::vector<std::shared_ptr<EmbeddedNetwork>>& nets() {
1035 for (std::size_t e = 0; e < env_->nstages(); ++e)
1036 nets_.push_back(std::make_shared<EmbeddedNetwork>(env_->stage(e).model, prefix_, cutoff_));
1040 NodeEvent stage_key(std::size_t e, std::size_t h)
const {
1044 std::size_t state(std::size_t clock, std::size_t x) {
1045 const std::pair<std::size_t, std::size_t> key(clock, x);
1046 const auto it = state_index_.find(key);
1047 if (it != state_index_.end())
return it->second;
1048 const std::size_t i = state_list_.size();
1049 state_index_[key] = i;
1050 state_list_.push_back(key);
1055 const Tangible& tangible(std::size_t e) {
1056 auto it = tangible_.find(e);
1057 if (it != tangible_.end())
return it->second;
1060 t.position.assign(net.nstates(),
static_cast<std::size_t
>(-1));
1061 for (std::size_t i = 0; i < net.nstates(); ++i)
1062 if (!net.is_vanishing(i)) {
1063 t.position[i] = t.states.size();
1064 t.states.push_back(i);
1066 return tangible_.emplace(e, std::move(t)).first->second;
1076 Reset reset_kind(std::size_t e, std::size_t h) {
1078 const env::EnvArc<double>& arc = env_->arc(e, h);
1079 if (arc.reset_state) {
1081 r.state_fun = arc.reset_state;
1084 if (!arc.reset)
return r;
1086 const std::size_t M = net.
sn().
nstations, K = net.sn().nclasses;
1091 const qn::NetworkStruct<double>& sn = net.sn();
1092 Matrix<double> probe(M, K, 0.0);
1093 for (std::size_t i = 0; i < M; ++i) {
1095 for (std::size_t k = 0; k < K; ++k) probe(i, k) = static_cast<double>(i * K + k + 1);
1097 const Matrix<double> out = arc.reset(probe);
1098 bool same = out.rows() == M && out.cols() == K;
1099 for (std::size_t i = 0; same && i < M; ++i)
1100 for (std::size_t k = 0; same && k < K; ++k)
1101 if (out(i, k) != probe(i, k)) same =
false;
1109 std::vector<std::pair<std::size_t, double>> carry(std::size_t x, std::size_t e, std::size_t h) {
1112 const Reset& kind = resets_.at(std::make_pair(e, h));
1114 const std::size_t y = dst.index_of(src.space()[x]);
1115 if (y !=
static_cast<std::size_t
>(-1))
return {std::make_pair(y, 1.0)};
1117 "Environment stage " + std::to_string(e) +
"->" + std::to_string(h) +
1118 ": the network state of stage " + std::to_string(e) +
1119 " has no counterpart in the state space of stage " + std::to_string(h) +
1120 ", so the keep policy cannot carry it.");
1122 if (kind.state_fun) {
1125 const Tangible& src_t = tangible(e);
1126 const Tangible& dst_t = tangible(h);
1127 const std::size_t pos = src_t.position[x];
1128 if (pos ==
static_cast<std::size_t
>(-1))
1131 ": a resetStateFun cannot be applied in a vanishing "
1133 std::vector<double> pex(src_t.states.size(), 0.0);
1135 const std::vector<double> pin = kind.state_fun(pex);
1136 bool ok = pin.size() == dst_t.states.size();
1138 for (std::size_t i = 0; ok && i < pin.size(); ++i) {
1139 if (!(pin[i] >= -1e-12)) ok =
false;
1142 if (!ok || !(total > 0) || !std::isfinite(total))
1145 ": resetStateFun must return a nonnegative vector over the " +
1146 std::to_string(dst_t.states.size()) +
" states of stage " +
1147 std::to_string(h) +
".");
1148 std::vector<std::pair<std::size_t, double>> law;
1149 for (std::size_t i = 0; i < pin.size(); ++i)
1150 if (pin[i] > 0) law.push_back(std::make_pair(dst_t.states[i], pin[i] / total));
1155 const auto& qk = src.queue_lengths(x);
1156 Matrix<double> target = kind.fun(qk.first);
1157 bool ok = target.rows() == qk.first.rows() && target.cols() == qk.first.cols();
1158 for (std::size_t k = 0; ok && k < target.size(); ++k)
1159 if (std::fabs(target[k] - std::round(target[k])) > 1e-9 || target[k] < 0) ok =
false;
1161 throw InputError(
ErrorKind(
"Model"),
"Environment arc " + std::to_string(e) +
"->" + std::to_string(h) +
1162 ": the reset function must return nonnegative integer queue "
1164 for (std::size_t k = 0; k < target.size(); ++k) target[k] = std::round(target[k]);
1165 std::optional<std::size_t> fallback;
1166 for (std::size_t y = 0; y < dst.nstates(); ++y) {
1167 const auto& qy = dst.queue_lengths(y);
1168 bool equal = qy.first.rows() == target.rows() && qy.first.cols() == target.cols();
1169 for (std::size_t k = 0; equal && k < target.size(); ++k)
1170 if (qy.first[k] != target[k]) equal =
false;
1171 if (!equal)
continue;
1172 if (qy.second == qk.second)
return {std::make_pair(y, 1.0)};
1173 if (!fallback) fallback = y;
1175 if (fallback)
return {std::make_pair(*fallback, 1.0)};
1177 "Environment arc " + std::to_string(e) +
"->" + std::to_string(h) +
": stage " +
1179 " has no state with the queue lengths the reset produces (closed populations are "
1180 "conserved, and open classes are bounded by the cutoff).");
1185 const std::vector<std::shared_ptr<EmbeddedNetwork>>& ns = nets();
1186 const std::size_t E = ns.size();
1187 stages_.assign(E, StageArcs());
1188 for (std::size_t e = 0; e < E; ++e) {
1189 stages_[e].kernel = env_->is_kernel_stage(e);
1190 for (std::size_t h = 0; h < E; ++h) {
1191 const env::EnvArc<double>& a = env_->arc(e, h);
1199 arc.prob = stages_[e].kernel ? env_->kernel_prob(e, h) : 1.0;
1200 stages_[e].arcs.push_back(arc);
1204 clock_index_.clear();
1205 entry_.assign(E, std::vector<std::pair<std::size_t, double>>());
1206 for (std::size_t e = 0; e < E; ++e) {
1207 const StageArcs& sa = stages_[e];
1208 if (sa.arcs.empty()) {
1209 clock_index_[Clock{e, {}}] = clocks_.size();
1210 entry_[e].push_back(std::make_pair(clocks_.size(), 1.0));
1211 clocks_.push_back(Clock{e, {}});
1215 for (std::size_t k = 0; k < sa.arcs.size(); ++k)
1216 for (std::size_t ph = 0; ph < sa.arcs[k].D0.rows(); ++ph) {
1217 const Clock c{e, {k, ph}};
1218 clock_index_[c] = clocks_.size();
1219 if (sa.arcs[k].alpha[ph] > 0)
1220 entry_[e].push_back(std::make_pair(clocks_.size(), sa.arcs[k].prob * sa.arcs[k].alpha[ph]));
1221 clocks_.push_back(c);
1225 std::vector<std::size_t> idx(sa.arcs.size(), 0);
1227 const Clock c{e, idx};
1229 for (std::size_t k = 0; k < idx.size(); ++k) w *= sa.arcs[k].alpha[idx[k]];
1230 clock_index_[c] = clocks_.size();
1231 if (w > 0) entry_[e].push_back(std::make_pair(clocks_.size(), w));
1232 clocks_.push_back(c);
1233 std::size_t d = idx.size();
1241 if (++idx[d] < sa.arcs[d].D0.rows())
break;
1253 for (std::size_t e = 0; e < E; ++e) {
1255 for (
const auto& w : entry_[e]) total += w.second;
1256 if (entry_[e].empty() || !(total > 0.0) || !std::isfinite(total))
1258 "Environment stage " + std::to_string(e) +
1259 ": its stage clocks have no phase with a positive entry probability, so the "
1260 "stage cannot be entered.");
1263 std::set<NodeEvent> seen;
1264 for (
const auto& n : ns)
1266 if (seen.insert(k).second) labels_.push_back(k);
1269 for (std::size_t e = 0; e < E; ++e)
1270 for (
const StageArc& a : stages_[e].arcs) {
1271 const NodeEvent k = stage_key(e, a.dest);
1272 if (seen.insert(k).second) labels_.push_back(k);
1273 resets_[std::make_pair(e, a.dest)] = reset_kind(e, a.dest);
1275 state_list_.clear();
1276 state_index_.clear();
1277 std::size_t start = entry_[0].front().first;
1278 double best = entry_[0].front().second;
1279 for (
const auto& w : entry_[0])
1280 if (w.second > best) {
1285 initial_ = state(start, ns[0]->initial());
1289namespace embed_detail {
1326 const std::size_t
n = T.
rows();
1328 for (std::size_t i = 0; i <
n; ++i)
1329 for (std::size_t j = 0; j <
n; ++j)
1330 if (i != j && T(i, j) > 0) l.
internal.push_back(
LphMove{i, j, T(i, j), {},
false});
1331 for (std::size_t i = 0; i <
n; ++i) {
1333 for (std::size_t j = 0; j <
n; ++j) s += T(i, j);
1334 if (-s > 0) l.
exits.push_back(
LphExit{i, -s, {label},
false});
1337 for (
double a :
alpha) asum += a;
1338 for (
double a :
alpha) l.
alpha.push_back(a / asum);
1354 out.
n =
n + other.
n;
1357 for (std::size_t j = 0; j < other.
n; ++j)
1358 if (other.
alpha[j] > 0)
1364 out.
alpha.resize(
n + other.
n, 0.0);
1371 const std::size_t n1 =
n, n2 = other.
n;
1372 auto both = [&](std::size_t i, std::size_t j) {
return i * n2 + j; };
1373 auto only1 = [&](std::size_t i) {
return n1 * n2 + i; };
1374 auto only2 = [&](std::size_t j) {
return n1 * n2 + n1 + j; };
1377 for (std::size_t j = 0; j < n2; ++j)
1378 out.
internal.push_back(
LphMove{both(m.i, j), both(m.j, j), m.rate, m.labels, m.immediate});
1379 out.
internal.push_back(
LphMove{only1(m.i), only1(m.j), m.rate, m.labels, m.immediate});
1382 for (std::size_t i = 0; i < n1; ++i)
1383 out.
internal.push_back(
LphMove{both(i, m.i), both(i, m.j), m.rate, m.labels, m.immediate});
1387 for (std::size_t j = 0; j < n2; ++j)
1388 out.
internal.push_back(
LphMove{both(x.i, j), only2(j), x.rate, x.labels, x.immediate});
1389 out.
exits.push_back(
LphExit{only1(x.i), x.rate, x.labels, x.immediate});
1392 for (std::size_t i = 0; i < n1; ++i)
1393 out.
internal.push_back(
LphMove{both(i, x.i), only1(i), x.rate, x.labels, x.immediate});
1396 out.
n = n1 * n2 + n1 + n2;
1397 out.
alpha.assign(out.
n, 0.0);
1398 for (std::size_t i = 0; i < n1; ++i)
1399 for (std::size_t j = 0; j < n2; ++j) out.
alpha[both(i, j)] =
alpha[i] * other.
alpha[j];
1400 for (std::size_t i = 0; i < n1; ++i)
1407 static Lph mixture(
const std::vector<Lph>& kids,
const std::vector<double>& probs) {
1410 for (std::size_t c = 0; c < kids.size(); ++c) {
1411 const Lph& kid = kids[c];
1416 for (
double a : kid.
alpha) out.
alpha.push_back(probs[c] * a);
1425 if (count <= 0)
return skip();
1426 if (count < 1)
return mixture({*
this,
skip()}, {count, 1.0 - count});
1427 const double p = 1.0 - 1.0 / count;
1432 for (std::size_t j = 0; j <
n; ++j)
1435 out.
exits.push_back(
LphExit{x.i, x.rate * (1.0 - p), x.labels, x.immediate});
1445 const std::size_t n = d.
D0.rows();
1447 return std::vector<double>(d.
params.begin(), d.
params.begin() +
static_cast<long>(n));
1448 for (std::size_t i = 0; i < n; ++i) {
1450 for (std::size_t j = 0; j < n; ++j) out += d.
D1(i, j);
1451 if (!(out > 0))
continue;
1452 std::vector<double> alpha(n);
1453 for (std::size_t j = 0; j < n; ++j) alpha[j] = d.
D1(i, j) / out;
1456 std::vector<double> alpha(n, 0.0);
1457 if (n > 0) alpha[0] = 1.0;
1463 std::vector<std::string> states;
1464 for (std::size_t i = 0; i < lph.
n; ++i) states.push_back(
"p" + std::to_string(i));
1465 std::size_t start = 0;
1466 for (std::size_t i = 0; i < lph.
n; ++i)
1467 if (lph.
alpha[i] > lph.
alpha[start]) start = i;
1468 Agent agent(name, states, states[start]);
1470 for (
double a : lph.
alpha) asum += a;
1471 std::vector<double> p0;
1472 for (
double a : lph.
alpha) p0.push_back(a / asum);
1473 detail::Records::set_initial_distribution(agent, p0);
1474 const std::string done = name +
".complete";
1475 std::vector<Transition> trans;
1476 for (std::size_t k = 0; k < lph.
internal.size(); ++k) {
1478 if (!(m.
rate > 0))
continue;
1481 t.
action =
"m" + std::to_string(k);
1492 for (std::size_t j = 0; j < lph.
n; ++j) {
1493 if (!(lph.
alpha[j] > 0))
continue;
1494 const double rate = x.
rate * lph.
alpha[j];
1495 if (!(rate > 0))
continue;
1502 t.
label.push_back(done);
1507 detail::Records::set_transitions(agent, trans);
1512 std::vector<std::string> tags;
1513 if (space.
rows() != n || space.
cols() == 0) {
1514 for (std::size_t i = 0; i < n; ++i) tags.push_back(std::to_string(i));
1517 for (std::size_t i = 0; i < n; ++i) {
1518 std::string s =
"(";
1519 for (std::size_t j = 0; j < space.
cols(); ++j) s += (j ?
"," :
"") +
fmt_g(space(i, j));
1520 tags.push_back(s +
")");
1536 for (std::size_t k = 0; finite && k < q.
size(); ++k)
1537 if (!std::isfinite(q[k])) finite =
false;
1538 if (!finite)
throw InputError(
ErrorKind(
"Model"),
"MarkovProcess generator must be a finite square matrix.");
1539 const std::string name = prefix.empty() ? std::string(
"MarkovProcess") : prefix;
1540 const std::size_t n = q.
rows();
1541 std::vector<std::string> states;
1542 for (std::size_t i = 0; i < n; ++i) states.push_back(
"s" + std::to_string(i));
1543 Agent agent(name, states, states[0]);
1544 detail::Records::set_initial_distribution(agent, std::vector<double>(n, 1.0 /
static_cast<double>(n)));
1546 for (std::size_t i = 0; i < n; ++i)
1547 for (std::size_t j = 0; j < n; ++j)
1548 if (i != j && q(i, j) > 0)
1549 agent.
active(states[i],
"T_" + std::to_string(i) +
"_" + std::to_string(j), q(i, j), states[j],
1550 name +
".T[" + tags[i] +
"->" + tags[j] +
"]");
1565 for (std::size_t k = 0; ok && k < p.
size(); ++k)
1566 if (!(p[k] >= 0)) ok =
false;
1567 for (std::size_t i = 0; ok && i < p.
rows(); ++i) {
1569 for (std::size_t j = 0; j < p.
cols(); ++j) s += p(i, j);
1570 if (!(std::fabs(s - 1.0) <= 1e-8 + 1e-5)) ok =
false;
1572 if (!ok)
throw InputError(
ErrorKind(
"Model"),
"MarkovChain transition matrix must be square and stochastic.");
1573 const std::string name = prefix.empty() ? std::string(
"MarkovChain") : prefix;
1574 const std::size_t n = p.
rows();
1575 std::vector<std::string> states;
1576 for (std::size_t i = 0; i < n; ++i) states.push_back(
"s" + std::to_string(i));
1577 Agent agent(name, states, states[0]);
1578 detail::Records::set_initial_distribution(agent, std::vector<double>(n, 1.0 /
static_cast<double>(n)));
1579 detail::Records::set_discrete(agent,
true);
1581 for (std::size_t i = 0; i < n; ++i)
1582 for (std::size_t j = 0; j < n; ++j)
1584 agent.
active(states[i],
"T_" + std::to_string(i) +
"_" + std::to_string(j), p(i, j), states[j],
1585 name +
".T[" + tags[i] +
"->" + tags[j] +
"]");
1603 const std::string wname =
wf.name().empty() ? std::string(
"Workflow") :
wf.name();
1614 switch (node.
type) {
1624 for (std::size_t c = 1; c < node.
kids.size(); ++c) out = out.
serial(build(node.
kids[c]));
1629 for (std::size_t c = 1; c < node.
kids.size(); ++c) out = out.
parallel(build(node.
kids[c]));
1633 std::vector<embed_detail::Lph> kids;
1634 for (std::size_t c : node.
kids) kids.push_back(build(c));
1638 return build(node.
kids[0]).loop(node.
count);
1648 if (!network)
throw value_error(
"expected a LINE Network");
1665 auto network = std::make_shared<qn::Network<double>>(
io::lqn2qn(layered,
"auto", layered.
name));
1672 for (
const Agent& a : agents_)
1673 if (a.name() == part.
name())
1674 throw value_error(
"a part named " + part.
name() +
" is already in the composition; give a prefix");
1675 for (
const auto& p : embedded_)
1676 if (p->name() == part.
name())
1677 throw value_error(
"a part named " + part.
name() +
" is already in the composition; give a prefix");
1678 agents_.push_back(part);
1685 for (
const Agent& a : agents_)
1686 if (a.name() == part.
name())
1687 throw value_error(
"a part named " + part.
name() +
" is already in the composition; give a prefix");
1688 for (
const auto& p : embedded_)
1689 if (p->name() == part.
name())
1690 throw value_error(
"a part named " + part.
name() +
" is already in the composition; give a prefix");
1691 agents_.push_back(part);
LINE Agent Language (LAL): programmatic construction of finite agents and their composition,...
A refusal: the feature, method or analysis is not covered: LINE:unsupported:<kind>.
UnsupportedError(const std::string &what)
A finite-state agent with active, weighted passive and immediate transitions (handle semantics).
Agent & active(const std::string &source, const std::string &action, double rate, const std::string &target, const std::optional< std::string > &label=std::nullopt, int priority=0)
Add a timed action at rate.
const std::string & name() const
Composition & add(const Agent &agent)
Add a hand-declared agent.
Composition & add_part(std::shared_ptr< EmbeddedPart > part)
Add an embedded part built elsewhere (embed_part).
const std::string & name() const
double node_jobs(std::size_t s, const std::string &node, const std::string &jobclass) override
Jobs at a station in global state s: the total, or those of class jobclass when nonempty.
EmbeddedEnvironment(const env::Environment< double > &e, const std::string &prefix, std::optional< double > cutoff)
NodeEvent resolve(const std::string &node, lang::EventType event, const std::optional< std::string > &jobclass, const std::optional< std::string > &mode) override
The key of a node action, validated against the model's names.
std::size_t initial() override
The global index of the initial state.
std::vector< std::pair< std::size_t, double > > inject(std::size_t, const NodeEvent &key) override
Passive response to an injected arrival: [(target, probability)]; empty blocks it.
std::vector< EmbeddedArc > outgoing(std::size_t s) override
Arcs leaving global state s.
const std::string & prefix() const override
The embedding prefix, empty when none was given.
std::string class_name(std::size_t cls) override
Name of the 1-based class index of the model.
std::size_t stage_of(std::size_t s) override
The stage of global state s (Environment only).
bool has_stages() override
True for an embedded Environment, whose stage is an agent of its own.
void refresh() override
Re-read the embedded model on next use.
std::vector< std::pair< std::string, std::size_t > > node_states() override
[(node name, isf)] of the stateful node agents (isf is 1-based).
std::vector< std::string > stage_names() override
The stage names (Environment only).
std::string node_name(std::size_t node) override
Name of the 1-based node index of the model.
std::string node_state_label(std::size_t s, std::size_t isf) override
The state row of stateful node isf in global state s, as [v1 v2 ...].
bool has_vanishing() override
True when the embedded model has any vanishing state.
std::vector< double > node_row(std::size_t s, const std::string &node) override
The state row of a stateful node in global state s.
void check_injection(const NodeEvent &) override
Refuse a key that a hand agent cannot drive.
const std::vector< NodeEvent > & labels() override
Every node key an arc carries, in first-seen order.
const std::string & name() const override
The part name (the prefix, or the model name).
bool is_vanishing(std::size_t s) override
True when s is a vanishing state of the embedded model.
A LINE Network embedded as one agent per node (EmbeddedNetwork).
std::size_t initial() override
The global index of the initial state.
std::size_t index_of(const qn::NetState< double > &state)
The global index of a network state, or npos.
std::vector< std::pair< std::size_t, double > > inject(std::size_t s, const NodeEvent &key) override
Passive response of a node to an injected arrival in global state s (inject): [(target,...
NodeEvent resolve(const std::string &node, lang::EventType event, const std::optional< std::string > &jobclass, const std::optional< std::string > &mode) override
The key of a node action, validated against the model's names.
const qn::NetworkStruct< double > & sn()
void set_injected(const std::set< std::string > &classes) override
Record the classes hand agents inject (names); a change invalidates the enumeration.
void check_injection(const NodeEvent &key) override
Refuse a key that a hand agent cannot drive.
std::vector< std::pair< std::string, std::size_t > > node_states() override
[(node name, isf)] of the stateful node agents (isf is 1-based).
std::string node_state_label(std::size_t s, std::size_t isf) override
The state row of stateful node isf in global state s, as [v1 v2 ...].
std::string node_name(std::size_t node) override
Name of the 1-based node index of the model.
const std::vector< NodeEvent > & labels() override
Every node key an arc carries, in first-seen order.
const std::string & prefix() const override
The embedding prefix, empty when none was given.
EmbeddedNetwork(const qn::NetworkStruct< double > &sn, const std::string &prefix, std::optional< double > cutoff)
bool is_vanishing(std::size_t s) override
True when s is a vanishing state of the embedded model.
const std::vector< EmbeddedArc > & arcs(std::size_t s)
const std::pair< Matrix< double >, std::vector< std::vector< std::vector< double > > > > & queue_lengths(std::size_t x)
Station-by-class queue lengths and per-station service phases of global state x (memoized).
std::vector< EmbeddedArc > outgoing(std::size_t s) override
Arcs leaving global state s.
void refresh() override
Re-read the embedded model on next use.
const std::vector< qn::NetState< double > > & space()
bool has_vanishing() override
True when the embedded model has any vanishing state.
const qn::NetworkStruct< double > & model_struct()
The embedded model's own refreshed struct (network.get_struct()), without any enumeration.
std::string class_name(std::size_t cls) override
Name of the 1-based class index of the model.
EmbeddedNetwork(std::shared_ptr< qn::Network< double > > network, const std::string &prefix, std::optional< double > cutoff)
std::vector< double > node_row(std::size_t s, const std::string &node) override
The state row of a stateful node in global state s.
const std::string & name() const override
The part name (the prefix, or the model name).
double node_jobs(std::size_t s, const std::string &node, const std::string &jobclass) override
Jobs at a station in global state s: the total, or those of class jobclass when nonempty.
An embedded LINE model seen by the LAL core (Python's EmbeddedNetwork / EmbeddedEnvironment duck type...
A network plus its refreshed NetworkStruct.
std::vector< JobClass > classes
std::vector< NodeDef > nodes
every node, in creation order
std::vector< double > njobs() const
sn.njobs: the population of each class, infinite for an open one.
A queueing network under construction.
A computational activity.
const std::string & name() const
PhLaw< T > ph_representation() const
The (alpha, T) pair of this activity.
T host_demand_mean() const
const Distrib< T > & host_demand() const
LINE Agent Language (LAL): the finite-agent model record, its validation, the joint-state event kerne...
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
A random environment: a port of matlab/src/lang/Environment.m, restricted to what SolverENV reads out...
Convert a layered queueing network into a single queueing network in which synchronous call blocking ...
.lqnx -> LqnStruct, a port of matlab/src/lang/layered/@LayeredNetwork/parseXML.m followed by ....
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
LINE Agent Language (LAL): the Markovian admission check.
The MarkovProcess / MarkovChain object surface.
CtmcSolution< T > solver_ctmc_analyzer(const NetworkStruct< T > &sn_in, const CtmcOptions &opt, const CtmcStationaryHook< T > &stationary=CtmcStationaryHook< T >())
Port of solver_ctmc_analyzer.m plus the fork-join wrapper of @@SolverCTMC/runAnalyzer....
CtmcAnySolution< T > solver_ctmc_analyzer_any(const NetworkStruct< T > &sn, const CtmcOptions &opt)
The entry point a caller who does not know which path a model needs should use: pick the WAITQ walk w...
bool ctmc_has_waitq_region(const NetworkStruct< T > &sn)
True when the model declares a region that applies anything other than DROP.
qn::Network< T > lqn2qn(const lqn::LqnModel< T > &lqn, const std::string &replication="auto", const std::string &name="model", std::vector< std::string > *warnings=nullptr)
Port of MATLAB LQN2QN(lqn, replication): flatten a layered network into a queueing network whose sync...
Agent lph_agent(const std::string &name, const Lph &lph)
Renewal agent of a labelled phase-type automaton: absorption restarts it (_lph_agent).
std::vector< double > init_prob(const lang::Distrib< double > &d)
The initial vector of a Markovian law with a (D0, D1) pair (getInitProb).
std::string fmt_g(double v)
g of a double.
std::vector< std::string > state_tags(const Matrix< double > &space, std::size_t n)
std::string identifier_from(const std::string &name, const std::string &fallback)
qn::Marginal< double > station_marginal(const qn::NetworkStruct< double > &sn, std::size_t ist, const std::vector< double > &row)
The queue-length / phase marginal of a station row (toMarginal).
void check_network(const qn::NetworkStruct< double > &sn, const std::string &where=std::string())
Refuse a network holding any non-Markovian specification (check_network).
Agent markov_chain_agent(const lang::processes::MarkovChainModel< double > &mc, const std::string &prefix)
One discrete-time agent for a MarkovChain (_markov_chain_agent).
void check_workflow(const workflow::Workflow< double > &wf)
Refuse a Workflow with a non-Markovian activity law.
void check_environment(const env::Environment< double > &e)
Refuse an Environment with a non-Markovian holding law or stage model (check_environment).
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).
InputError value_error(const std::string &what)
LINE:input:Unspecified, the C++ spelling of the Python ValueError raised throughout LAL.
Agent markov_process_agent(const lang::processes::MarkovChainModel< double > &mp, const std::string &prefix)
One agent for a MarkovProcess: a timed action per generator entry (_markov_process_agent).
Agent workflow_agent(workflow::Workflow< double > &wf, const std::string &prefix)
One renewal agent for a Workflow, labelled by activity (_workflow_agent).
mam::Map< T > dist_to_map(const Distrib< T > &d)
The (D0,D1) pair that reaches sn.proc.
EventType
The events a state can undergo, with the values of MATLAB EventType.
@ PHASE
service advances a phase WITHOUT departing
@ STAGE
a random environment changes stage
@ LOCAL
dummy event, no state change outside the node
@ ENABLE
an SPN mode becomes enabled
void dist_refresh_moments(Distrib< T > &d)
Fill in the first two moments of a distribution given by its matrices.
LqnFocusResult build(const LqnModel< double > &model, const Inv &inv, const Om &om, const Sat &sat, std::vector< bool > PT, std::vector< bool > PH, const LqnFocusOptions &opt, bool validate)
LqnStruct< T > lqn_finalize(const LqnModel< T > &m)
Port of @LayeredNetwork/getStruct.m: flatten the model into its struct.
std::vector< T > map_pie(const Map< T > &m)
Phase distribution seen by an arriving job, pie = pi D1 / (pi D1 e).
void walk(const PollingInfo< T > &pi, PollingAdvance< T > &B, std::vector< long > c, const std::vector< long > &nbuf, std::size_t p, bool arrived, std::size_t steps_left, T prob, std::size_t startpos)
Marginal< T > to_marginal(const NetworkStruct< T > &sn, std::size_t ist, const std::vector< T > &state_i, const std::vector< std::size_t > &phasesz, const std::vector< std::size_t > &phaseshift, std::size_t nvar=0)
Port of State.toMarginal for a STATION, one state row at a time.
EventOutcome< T > after_event(const NetworkStruct< T > &sn, std::size_t ind, const std::vector< T > &inspace, EventType event, std::size_t cls, bool no_promote=false, const T &aux_rate=num_traits< T >::from_int(0))
Port of State.afterEvent: the successors of one event at one NODE.
Conservation laws of a layered queueing network, enumerated from its structure.
The Network constructor API: Queue, Delay, Source, Sink, Router, ClassSwitch, Cache,...
Port of solver_ctmc.m: the infinitesimal generator of a queueing network, assembled from the enumerat...
Port of solver_ctmc_analyzer.m and the parts of @@SolverCTMC/runAnalyzer.m that surround one solve: t...
Port of solver_ctmc_fcr_waitq.m: the reachability-built generator of a model whose finite capacity re...
Port of the MATLAB +State package: the encoding that turns a station's state row into marginal job co...
Port of the event half of MATLAB's +State package: the successor states an event produces at one node...
One augmented state: the network state, plus the token FIFO of every region.
Keyword arguments of Composition::add for an embedded LINE model.
std::string prefix
Names the embedded part; required when two parts would share node names.
std::optional< double > cutoff
Bounds open classes as SolverCTMC's cutoff does.
One arc of an embedded model: (target, keys, rate, immediate) (an immediate arc carries a weight).
std::vector< NodeEvent > keys
Key of an embedded node action: (node, EventType, class | mode).
std::optional< std::string > index
std::string label() const
One labelled local transition source --(action, value)--> target.
std::vector< std::string > label
bool tuple_label
The label is a TUPLE (python Transition.label given as a tuple), which marks a multi-label event even...
One step of a routing path: a node key, or the marker of a Sink crossing.
std::vector< std::string > labels
(i, j, rate, labels, immediate) and (i, rate, labels, immediate) of a labelled phase-type automaton.
std::vector< std::string > labels
A labelled absorbing phase-type automaton, for the Workflow embedding (_LPH): internal moves,...
std::vector< bool > immediate
static Lph leaf(const std::string &label, const std::vector< double > &alpha, const Matrix< double > &T, bool zero_time)
std::vector< double > alpha
std::vector< LphMove > internal
Lph parallel(const Lph &other) const
static Lph mixture(const std::vector< Lph > &kids, const std::vector< double > &probs)
std::vector< LphExit > exits
Lph serial(const Lph &other) const
Lph loop(double count) const
Geometric repetition of mean count, as Workflow's geometric loop composition.
Matrix< T > D0
The (D0,D1) pair when the type carries one directly.
static Distrib exp_rate(const T &r)
std::vector< T > params
Constructor arguments, in MATLAB getParam order.
static constexpr double FineTol
A user-supplied chain: a MarkovProcess when discrete is false, else a MarkovChain.
Matrix< T > mat
generator Q (CTMC) or transition matrix P (DTMC)
Matrix< T > state_space
optional; empty means the chain carries none
bool discrete
true for a MarkovChain
The intermediate model, and the second stage that flattens it.
std::string name
LayeredNetwork.getName(); empty when unnamed.
What one event produces at one node: the successor rows, their rates and their probabilities,...
std::vector< T > prob
per-row probability of the choice
std::vector< std::vector< T > > space
successor local state rows
One job class of the network.
What State.toMarginal returns for one station and one state row.
std::vector< std::vector< T > > kir
jobs in service per class and phase
std::vector< T > nir
jobs per class
One network state: the per-stateful-node local rows it is composed of.
std::vector< std::vector< T > > local
local[isf] is that node's state row
A phase-type law as the composition rules pass it around.
T count
Loop count for a LOOP node.
std::vector< T > probs
Branch probabilities for an OR node.
std::vector< std::size_t > kids
std::size_t act
Activity index for a LEAF, npos otherwise.
The flat series-parallel tree.
std::vector< SPNode< T > > nodes
An activity workflow reduced to one phase-type law.