83std::size_t index_from_json(
const Json& j,
const std::string& where) {
86 const long long v = std::strtoll(text.c_str(), &end, 10);
87 if (end == text.c_str() || *end !=
'\0')
88 throw InputError(where +
": an index must be an integer, got " + text);
89 if (v < 0)
throw InputError(where +
": an index cannot be negative, got " + text);
90 return static_cast<std::size_t
>(v);
94std::vector<std::size_t> indices_from_json(
const Json& j,
const std::string& where) {
95 std::vector<std::size_t> out;
97 out.push_back(index_from_json(j, where));
100 for (std::size_t k = 0; k < j.size(); ++k)
101 out.push_back(index_from_json(j[k], where +
"[" + std::to_string(k) +
"]"));
105std::vector<std::size_t> read_indices(
Args& a,
const char* key) {
106 return indices_from_json(a.get(key), a.get(key).is_null() ? key : std::string(key));
110std::vector<std::vector<std::size_t> > read_index_lists(
Args& a,
const char* key) {
111 const Json& j = a.get(key);
112 const std::string where = std::string(key);
113 if (!j.is_array())
throw InputError(where +
": expected a list of index lists");
114 std::vector<std::vector<std::size_t> > out;
115 for (std::size_t b = 0; b < j.size(); ++b)
116 out.push_back(indices_from_json(j[b], where +
"[" + std::to_string(b) +
"]"));
120Json encode_indices(
const std::vector<std::size_t>& v) {
121 Json a = Json::array();
122 for (std::size_t i = 0; i < v.size(); ++i) a.push_back(
static_cast<std::uint64_t
>(v[i]));
127std::vector<bool> read_flags(
Args& a,
const char* key) {
128 const Json& j = a.get(key);
129 const std::string where = std::string(key);
130 std::vector<bool> out;
131 const Json flat = j.is_array() ? j : Json::array({j});
132 for (std::size_t k = 0; k < flat.size(); ++k) {
133 const Json& e = flat[k];
134 if (e.is_boolean()) {
135 out.push_back(e.get<
bool>());
138 if (text !=
"0" && text !=
"1")
139 throw InputError(where +
": a flag must be true, false, 0 or 1, got " + text);
140 out.push_back(text ==
"1");
148std::vector<std::vector<T> > read_vector_list(
Args& a,
const char* key) {
149 const Json& j = a.get(key);
150 const std::string where = std::string(key);
151 if (!j.is_array())
throw InputError(where +
": expected a list of vectors");
152 std::vector<std::vector<T> > out;
153 for (std::size_t i = 0; i < j.size(); ++i)
160std::vector<std::vector<T> > rows_of(
const Matrix<T>& m) {
161 std::vector<std::vector<T> > out(m.rows(), std::vector<T>(m.cols()));
162 for (std::size_t i = 0; i < m.rows(); ++i)
163 for (std::size_t k = 0; k < m.cols(); ++k) out[i][k] = m(i, k);
169mam::Mmap<T> mmap_from_json(
const Json& j,
const std::string& where) {
170 if (!j.is_array())
throw InputError(where +
": expected an MMAP, a list of matrices");
172 if (j.empty())
return m;
174 throw InputError(where +
": an MMAP is {D0, D1, D1^(1), ...}, got one matrix");
177 for (std::size_t k = 2; k < j.size(); ++k)
183std::vector<mam::Mmap<T> > read_mmap_list(
Args& a,
const char* key) {
184 const Json& j = a.get(key);
185 const std::string where = std::string(key);
186 if (!j.is_array())
throw InputError(where +
": expected a list of MMAPs");
187 std::vector<mam::Mmap<T> > out;
188 for (std::size_t i = 0; i < j.size(); ++i)
189 out.push_back(mmap_from_json<T>(j[i], where +
"[" + std::to_string(i) +
"]"));
194Json encode_mmap(
const mam::Mmap<T>& m) {
195 std::vector<Matrix<T> > cell;
196 cell.push_back(m.D0);
197 cell.push_back(m.D1);
198 for (
const Matrix<T>& d : m.Dc) cell.push_back(d);
203std::vector<std::complex<double> > read_complex(
Args& a,
const char* key) {
204 const Json& j = a.get(key);
205 const std::string where = std::string(key);
206 const Json flat = j.is_array() ? j : Json::array({j});
207 std::vector<std::complex<double> > out;
208 for (std::size_t k = 0; k < flat.size(); ++k) {
209 const Json& e = flat[k];
210 const std::string w = where +
"[" + std::to_string(k) +
"]";
212 if (e.size() != 2)
throw InputError(w +
": a complex number is a pair [re, im]");
221Json encode_complex(
const std::vector<std::complex<double> >& v) {
222 Json a = Json::array();
223 for (
const std::complex<double>& z : v) a.push_back(Json::array({
z.real(),
z.imag()}));
228const Json* sub_object(
Args& a,
const char* key) {
229 const Json* j = a.opt(key);
230 if (j && !j->is_object())
231 throw InputError(std::string(key) +
": expected an object with named fields");
236struct PassageOptions {
237 bool hasMethod =
false;
248PassageOptions read_passage_options(
Args& a,
const std::string& fn) {
250 const Json* j = sub_object(a,
"options");
252 Args op(*j, fn +
": options");
253 o.hasMethod = op.has(
"method");
254 if (o.hasMethod) o.method = op.text(
"method",
"");
255 o.hasLti = op.has(
"lti_method");
256 if (o.hasLti) o.lti = op.text(
"lti_method",
"");
258 if (o.hasLti && !(o.hasMethod && o.method ==
"lt"))
259 throw InputError(fn +
": options.lti_method selects the inversion of method 'lt' and "
260 "has no effect otherwise; set options.method to 'lt' or drop it");
269struct OpCtmcStochcomp {
271 static Json run(Args& a) {
272 const Matrix<T> Q = a.matrix<T>(
"Q");
273 const bool hasI = a.has(
"I");
274 const std::vector<std::size_t> I = hasI ? read_indices(a,
"I") : std::vector<std::size_t>();
290struct OpCtmcPseudostochcomp {
292 static Json run(Args& a) {
293 const Matrix<T> Q = a.matrix<T>(
"Q");
294 const bool hasI = a.has(
"I");
295 const std::vector<std::size_t> I = hasI ? read_indices(a,
"I") : std::vector<std::size_t>();
297 const mc::PseudoStochCompResult<T> r =
311struct OpCtmcTestpfKolmogorov {
313 static Json run(Args& a) {
314 const Matrix<T> Q = a.matrix<T>(
"Q");
327template <
class R,
class F>
328R call_tol_maxiter(
Args& a,
const std::string& fn, F f) {
329 const bool hasTol = a.has(
"tol"), hasMax = a.has(
"maxiter");
330 if (hasMax && !hasTol)
331 throw InputError(fn +
": 'maxiter' follows 'tol' in the MATLAB signature; name both");
332 const double tol = hasTol ? a.number<
double>(
"tol") : 0.0;
333 const long maxiter = hasMax ?
static_cast<long>(a.required_integer(
"maxiter")) : 0;
335 if (hasMax)
return f(3, tol, maxiter);
336 if (hasTol)
return f(2, tol, maxiter);
337 return f(1, tol, maxiter);
341struct OpCtmcUniformization {
343 static Json run(Args& a) {
344 const std::vector<T> pi0 = a.vector<T>(
"pi0");
345 const Matrix<T> Q = a.matrix<T>(
"Q");
346 const T t = a.number<T>(
"t");
347 const mc::UniformizationResult<T> r = call_tol_maxiter<mc::UniformizationResult<T> >(
348 a,
"ctmc_uniformization", [&](
int n,
double tol,
long maxiter) {
361struct OpCtmcTimeaverage {
363 static Json run(Args& a) {
364 const std::vector<T> pi0 = a.vector<T>(
"pi0");
365 const Matrix<T> Q = a.matrix<T>(
"Q");
366 const T t = a.number<T>(
"t");
367 const mc::TimeAverageResult<T> r = call_tol_maxiter<mc::TimeAverageResult<T> >(
368 a,
"ctmc_timeaverage", [&](
int n,
double tol,
long maxiter) {
387template <mc::detail::SweepKind K>
388struct OpCtmcClassical {
390 static Json run(Args& a) {
391 const Matrix<T> Q = a.matrix<T>(
"Q");
394 const double omega = (K == mc::detail::SweepKind::Sor) ? a.scalar<
double>(
"omega", 0.0)
395 : (K == mc::detail::SweepKind::GaussSeidel) ? 1.0 : 0.0;
396 const std::vector<T> p0 = a.vector_or_empty<T>(
"p0");
398 const mc::ClassicalResult<T> r = mc::detail::stationary_sweep_iterate(K, Q, tol, maxiter, omega, p0);
401 out[
"flag"] = r.flag;
403 out[
"iter"] =
static_cast<std::int64_t
>(r.iter);
404 if (K == mc::detail::SweepKind::Sor) out[
"omega"] =
encode_scalar(r.omega);
417 static Json run(Args& a) {
418 const Matrix<T> Q = a.matrix<T>(Dtmc ?
"P" :
"Q");
425 Json nan = Json::array();
426 for (std::size_t i = 0; i < Q.rows(); ++i) nan.push_back(
"NaN");
429 out[
"flag"] = r.flag;
438 static Json run(Args& a) {
439 const Matrix<T> Q = a.matrix<T>(Dtmc ?
"P" :
"Q");
449 static Json run(Args& a) {
450 const Matrix<double> C = a.matrix<
double>(
"C");
451 const Matrix<double> R = a.matrix<
double>(
"R");
460struct OpCtmcAccumulated {
462 static Json run(Args& a) {
463 const std::vector<T> pi0 = a.vector<T>(
"pi0");
464 const Matrix<T> Q = a.matrix<T>(
"Q");
465 const std::vector<T> t = a.vector<T>(
"t");
466 const mc::AccumulatedResult<T> r = call_tol_maxiter<mc::AccumulatedResult<T> >(
467 a,
"ctmc_accumulated", [&](
int n,
double tol,
long maxiter) {
485struct OpCtmcMemoryGate {
487 static Json run(Args& a) {
488 const double logN = a.number<
double>(
"logNstates");
489 a.unsupported(
"verbose",
490 "ctmc_memory_gate: 'verbose' prints to stdout, which carries the JSON "
491 "reply; the decision and its message are returned as ok and msg");
492 const bool hasForce = a.has(
"force"), hasSafety = a.has(
"safetyFraction");
493 const bool force = hasForce ? a.boolean(
"force",
false) :
false;
494 const double safety = hasSafety ? a.number<
double>(
"safetyFraction") : 0.0;
496 mc::CtmcGateResult r;
505 out[
"msg"] = r.message;
511struct OpCtmcIsfeasible {
513 static Json run(Args& a) {
514 const Matrix<T> Q = a.matrix<T>(
"Q");
515 const bool hasTol = a.has(
"tol");
516 const T tol = hasTol ? a.number<T>(
"tol") : T();
525struct OpCtmcHittingTime {
527 static Json run(Args& a) {
528 const Matrix<T> Q = a.matrix<T>(
"Q");
529 const std::vector<std::size_t> target = read_indices(a,
"targetStates");
537struct OpDtmcHittingTime {
539 static Json run(Args& a) {
540 const Matrix<T>
P = a.matrix<T>(
"P");
541 const std::vector<std::size_t> target = read_indices(a,
"targetStates");
554struct OpDtmcTransient {
556 static Json run(Args& a) {
557 const Matrix<T>
P = a.matrix<T>(
"P");
558 const bool hasPi0 = a.has(
"pi0"), hasSteps = a.has(
"steps");
559 const std::vector<T> pi0 = a.vector_or_empty<T>(
"pi0");
560 const std::size_t steps = hasSteps ? a.count(
"steps") : 1;
570struct OpCtmcPassagePh {
572 static Json run(Args& a) {
573 const Matrix<T> Q = a.matrix<T>(
"Q");
574 const std::vector<T> pi0 = a.vector_or_empty<T>(
"pi0");
575 const std::vector<std::size_t> target = read_indices(a,
"target");
582 out[
"keep"] = encode_indices(r.keep);
589struct OpCtmcPassageLst {
591 static Json run(Args& a) {
592 const Matrix<T> Q = a.matrix<T>(
"Q");
593 const std::vector<T> pi0 = a.vector_or_empty<T>(
"pi0");
594 const std::vector<std::size_t> target = read_indices(a,
"target");
595 const std::vector<std::complex<double> > s = read_complex(a,
"s");
604struct OpCtmcPassageMoments {
606 static Json run(Args& a) {
607 const Matrix<T> Q = a.matrix<T>(
"Q");
608 const std::vector<T> pi0 = a.vector_or_empty<T>(
"pi0");
609 const std::vector<std::size_t> target = read_indices(a,
"target");
610 const bool hasN = a.has(
"nmax");
611 const std::size_t nmax = hasN ? a.count(
"nmax") : 0;
627struct OpCtmcPassageTime {
629 static Json run(Args& a) {
630 const Matrix<T> Q = a.matrix<T>(
"Q");
631 const std::vector<T> pi0 = a.vector_or_empty<T>(
"pi0");
632 const std::vector<std::size_t> target = read_indices(a,
"target");
633 const std::vector<double> tset = a.vector<
double>(
"tset");
634 const PassageOptions o = read_passage_options(a,
"ctmc_passage_time");
636 const mc::PassageCurve<T> c =
649 o2[
"keep"] = encode_indices(ph.keep);
656struct OpCtmcPassageTaboo {
658 static Json run(Args& a) {
659 const Matrix<T> Q = a.matrix<T>(
"Q");
660 const std::vector<T> pi0 = a.vector_or_empty<T>(
"pi0");
661 const std::vector<std::size_t> target = read_indices(a,
"target");
662 const std::vector<std::size_t> taboo = read_indices(a,
"taboo");
663 const bool hasT = a.has(
"tset");
664 const std::vector<double> tset = a.vector_or_empty<
double>(
"tset");
665 const PassageOptions o = read_passage_options(a,
"ctmc_passage_taboo");
667 const mc::PassageTaboo<T> r =
685 o2[
"keep"] = encode_indices(r.keep);
686 o2[
"target"] = encode_indices(r.target);
687 o2[
"taboo"] = encode_indices(r.taboo);
688 o2[
"method"] = r.method;
695struct OpCtmcPassageTabooMoments {
697 static Json run(Args& a) {
698 const Matrix<T> Q = a.matrix<T>(
"Q");
699 const std::vector<T> pi0 = a.vector_or_empty<T>(
"pi0");
700 const std::vector<std::size_t> target = read_indices(a,
"target");
701 const std::vector<std::size_t> taboo = read_indices(a,
"taboo");
702 const bool hasN = a.has(
"nmax");
703 const std::size_t nmax = hasN ? a.count(
"nmax") : 0;
705 const mc::PassageTabooMoments<T> r =
723 static Json run(Args& a) {
724 const std::vector<T> p = a.vector<T>(
"p");
725 const std::size_t N = a.count(
"N");
739 static Json run(Args& a) {
740 const Matrix<T>
P = a.matrix<T>(
"P");
741 const std::size_t N = a.count(
"N");
763struct OpNpfqnTrafficIdc {
765 static Json run(Args& a) {
766 const std::vector<T> lambda0 = a.vector<T>(
"lambda0");
767 const Matrix<T>
P = a.matrix<T>(
"P");
768 const std::vector<T> c2a0 = a.vector<T>(
"c2a0");
769 const std::vector<T> mu = a.vector<T>(
"mu");
770 const std::vector<T> cs2 = a.vector<T>(
"cs2");
771 a.unsupported(
"a0IdcFun",
"npfqn_traffic_idc: a0IdcFun is a function handle with no "
772 "JSON form; it is used only by npfqn_traffic_idc_at");
773 a.unsupported(
"sIdcFun",
"npfqn_traffic_idc: sIdcFun is a function handle with no "
774 "JSON form; it is used only by npfqn_traffic_idc_at");
775 const Json* cj = sub_object(a,
"corrections");
776 npfqn::TrafficIdcCorrections corr;
778 Args c(*cj,
"npfqn_traffic_idc: corrections");
779 if (c.has(
"alpha")) corr.alpha = c.boolean(
"alpha", corr.alpha);
780 if (c.has(
"beta")) corr.beta = c.boolean(
"beta", corr.beta);
813struct OpNpfqnTrafficSplitCs {
815 static Json run(Args& a) {
816 const std::vector<Matrix<T> > M = a.matrices<T>(
"MMAP");
817 const Matrix<T>
P = a.matrix<T>(
"P");
818 a.unsupported(
"config",
"npfqn_traffic_split_cs: the reference accepts config but never "
819 "reads it; there is nothing for it to select");
822 Json out = Json::array();
830npfqn::Merge merge_from_text(
const std::string& s,
const std::string& fn) {
835 throw InputError(fn +
": config.merge must be 'default', 'super', 'mixture' or 'interpos', got '" +
839npfqn::Compress compress_from_text(
const std::string& s,
const std::string& fn) {
842 throw InputError(fn +
": config.compress must be 'default' or 'none', got '" + s +
"'");
846struct OpNpfqnTrafficMergeCs {
848 static Json run(Args& a) {
849 const std::vector<mam::Mmap<T> > flows = read_mmap_list<T>(a,
"MMAP");
850 const Matrix<T> prob = a.matrix<T>(
"prob");
851 const Json* cj = sub_object(a,
"config");
852 bool hasMerge =
false;
855 Args c(*cj,
"npfqn_traffic_merge_cs: config");
856 hasMerge = c.has(
"merge");
857 if (hasMerge) merge = merge_from_text(c.text(
"merge",
""),
"npfqn_traffic_merge_cs");
864 out[
"SMMAP"] = encode_mmap(s);
870struct OpNpfqnTrafficMerge {
872 static Json run(Args& a) {
873 const std::vector<mam::Mmap<T> > flows = read_mmap_list<T>(a,
"MMAP");
874 const Json* cj = sub_object(a,
"config");
875 npfqn::MergeConfig cfg;
877 Args c(*cj,
"npfqn_traffic_merge: config");
878 if (c.has(
"merge")) cfg.merge = merge_from_text(c.text(
"merge",
""),
"npfqn_traffic_merge");
879 if (c.has(
"compress"))
880 cfg.compress = compress_from_text(c.text(
"compress",
""),
"npfqn_traffic_merge");
886 out[
"SMMAP"] = encode_mmap(s);
892struct OpNpfqnRqnaWeight {
894 static Json run(Args& a) {
895 const bool vec = a.get(
"t").is_array();
898 const std::vector<T> t = a.vector<T>(
"t");
902 const T t = a.number<T>(
"t");
911struct OpNpfqnTrafficRqt {
913 static Json run(Args& a) {
914 const std::vector<T> lambda0 = a.vector<T>(
"lambda0");
915 const std::vector<T> Gamma0 = a.vector<T>(
"Gamma0");
916 const std::vector<T> alpha0 = a.vector<T>(
"alpha0");
917 const Matrix<T> F = a.matrix<T>(
"F");
932struct OpNpfqnBndBpt {
934 static Json run(Args& a) {
935 const std::vector<T> lambda0 = a.vector<T>(
"lambda0");
936 const std::vector<T> mu = a.vector<T>(
"mu");
937 const Matrix<T>
P = a.matrix<T>(
"P");
938 const std::vector<std::size_t> stationOf = read_indices(a,
"stationOf");
939 const std::vector<T> c = a.vector_or_empty<T>(
"c");
963struct OpNpfqnBndBgt {
965 static Json run(Args& a) {
966 const std::vector<T> lambda = a.vector<T>(
"lambda");
967 const std::vector<std::vector<T> > mu = read_vector_list<T>(a,
"mu");
968 const std::vector<std::vector<std::size_t> > sigma = read_index_lists(a,
"sigma");
973 for (
const std::vector<std::size_t>& s : sigma)
974 for (std::size_t j : s) J = std::max(J, j + 1);
981 Json fin = Json::array();
982 for (
bool b : r.finite) fin.push_back(b);
983 info[
"finite"] = fin;
995 info[
"classType"] = encode_indices(r.classType);
996 info[
"classStage"] = encode_indices(r.classStage);
997 info[
"classStation"] = encode_indices(r.classStation);
1009struct OpNpfqnNonexpApprox {
1011 static Json run(Args& a) {
1012 a.unsupported(
"sn",
"npfqn_nonexp_approx: pass the two sn fields the routine reads, "
1013 "isFCFS (sn.sched == FCFS per station) and rates (sn.rates)");
1014 const std::string method = a.text(
"method",
"");
1015 if (method.empty())
throw InputError(
"npfqn_nonexp_approx: missing required argument 'method'");
1016 const std::vector<bool> isFCFS = read_flags(a,
"isFCFS");
1017 const Matrix<T> rates = a.matrix<T>(
"rates");
1018 const Matrix<T> ST = a.matrix<T>(
"ST");
1019 const Matrix<T> V = a.matrix<T>(
"V");
1020 const Matrix<T> SCV = a.matrix<T>(
"SCV");
1021 const Matrix<T>
Tput = a.matrix<T>(
"T");
1022 const Matrix<T> U = a.matrix<T>(
"U");
1023 const std::vector<T> gamma = a.vector<T>(
"gamma");
1024 const std::vector<T> nservers = a.vector<T>(
"nservers");
1026 const npfqn::NonexpApproxResult<T> r =
1048 static Json run(Args& a) {
1049 a.unsupported(
"sn",
"npfqn_sqd: pass the arrays the routine unpacks from sn: ST, V, cap, "
1050 "isDelay, rt, stationToStateful and nclasses (see npfqn_sqd.h)");
1051 const std::vector<T> ST = a.vector<T>(
"ST");
1052 const std::vector<T> V = a.vector<T>(
"V");
1053 const std::vector<T> cap = a.vector<T>(
"cap");
1054 const std::vector<bool> isDelay = read_flags(a,
"isDelay");
1055 const Matrix<T> rt = a.matrix<T>(
"rt");
1056 const std::vector<std::size_t> s2s = read_indices(a,
"stationToStateful");
1057 const std::size_t nclasses = a.count(
"nclasses");
1058 const int N = a.required_integer(
"N");
1059 npfqn::SqdOptions<T> opt;
1061 if (a.has(
"calibrationMode")) {
1062 opt.calibrationMode = a.required_integer(
"calibrationMode");
1065 if (a.has(
"serverBlockingTime")) {
1066 opt.serverBlockingTime = a.boolean(
"serverBlockingTime", opt.serverBlockingTime);
1069 if (a.has(
"neighborMode")) {
1070 const std::string m = a.text(
"neighborMode",
"");
1071 if (m !=
"downstream" && m !=
"ownserver")
1072 throw InputError(
"npfqn_sqd: neighborMode must be 'downstream' or 'ownserver', got '" + m +
"'");
1073 opt.ownServerNeighbor = (m ==
"ownserver");
1076 if (a.has(
"v1Policy")) {
1077 const std::string m = a.text(
"v1Policy",
"");
1078 if (m !=
"compound" && m !=
"fresh")
1079 throw InputError(
"npfqn_sqd: v1Policy must be 'compound' or 'fresh', got '" + m +
"'");
1080 opt.freshV1 = (m ==
"fresh");
1083 if (a.has(
"initialV1")) {
1084 opt.initialV1 = a.vector_or_empty<T>(
"initialV1");
1088 const npfqn::SqdResult<T> r =
1090 : npfqn::
npfqn_sqd(ST, V, cap, isDelay, rt, s2s, nclasses, N);
1101struct OpNpfqnDpsMorrison {
1103 static Json run(Args& a) {
1104 const std::vector<T> N = a.vector<T>(
"N");
1105 const std::vector<T> Z = a.vector<T>(
"Z");
1106 const std::vector<T> S = a.vector<T>(
"S");
1107 const std::vector<T> w = a.vector<T>(
"w");
1117#define LINE_MC_AUX(f) aux[#f] = encode_scalar(r.f);
1136struct OpNpfqnFeedbackElim {
1138 static Json run(Args& a) {
1139 const Matrix<T>
P = a.matrix<T>(
"P");
1140 const std::vector<T> rho = a.vector<T>(
"rho");
1141 const std::vector<T> cs2 = a.vector_or_empty<T>(
"cs2");
1142 const std::vector<T> lambda = a.vector_or_empty<T>(
"lambda");
1143 const bool hasImm = a.has(
"immediateOnly");
1144 const bool imm = hasImm ? a.boolean(
"immediateOnly",
false) :
false;
1151 if (!r.modifiedScv.empty()) res[
"modifiedScv"] =
encode_vector(r.modifiedScv);
1152 if (!r.modifiedRates.empty()) res[
"modifiedRates"] =
encode_vector(r.modifiedRates);
1154 res[
"reductionExact"] = r.reductionExact;
1156 out[
"result"] = res;
1165Json env_json(
const snc::Env& e) {
1167 out[
"sigma"] = e.sigma;
1173snc::Env read_env(
Args& a,
const char* s,
const char* r) {
1175 e.sigma = a.number<
double>(s);
1176 e.rho = a.number<
double>(r);
1180struct OpSncEnvPoisson {
1181 static Json run(Args& a) {
1182 const double lambda = a.number<
double>(
"lambda"), theta = a.number<
double>(
"theta");
1188struct OpSncEnvCpoisson {
1189 static Json run(Args& a) {
1190 const double lambda = a.number<
double>(
"lambda"), mu = a.number<
double>(
"mu");
1191 const double theta = a.number<
double>(
"theta");
1198struct OpSncEnvTokenbucket {
1199 static Json run(Args& a) {
1200 const double b = a.number<
double>(
"b"), r = a.number<
double>(
"r");
1201 const bool hasTheta = a.has(
"theta");
1202 const double theta = hasTheta ? a.number<
double>(
"theta") : 0.0;
1209 static Json run(Args& a) {
1210 const Matrix<double> D0 = a.matrix<
double>(
"D0"), D1 = a.matrix<
double>(
"D1");
1211 const double theta = a.number<
double>(
"theta");
1218struct OpSncSrvRate {
1219 static Json run(Args& a) {
1220 const double C = a.number<
double>(
"C");
1221 const bool hasTheta = a.has(
"theta");
1222 const double theta = hasTheta ? a.number<
double>(
"theta") : 0.0;
1229 static Json run(Args& a) {
1230 const double mu = a.number<
double>(
"mu"), theta = a.number<
double>(
"theta");
1236struct OpSncLeftover {
1237 static Json run(Args& a) {
1238 const snc::Env srv = read_env(a,
"sigmaS",
"rhoS");
1239 const snc::Env cross = read_env(a,
"sigmaX",
"rhoX");
1247 static Json run(Args& a) {
1248 const snc::Env s1 = read_env(a,
"sigma1",
"rho1");
1249 const snc::Env s2 = read_env(a,
"sigma2",
"rho2");
1250 const double theta = a.number<
double>(
"theta");
1251 const bool hasDelta = a.has(
"delta");
1252 const double delta = hasDelta ? a.number<
double>(
"delta") : 0.0;
1259 static Json run(Args& a) {
1260 const snc::Env arv = read_env(a,
"sigmaA",
"rhoA");
1261 const snc::Env srv = read_env(a,
"sigmaS",
"rhoS");
1262 const double theta = a.number<
double>(
"theta");
1277 static Json run(Args& a) {
1278 const std::size_t n = a.count(
"n");
1291struct OpCtmcSimulate {
1293 static Json run(Args& a) {
1294 const Matrix<T> Q = a.matrix<T>(
"Q");
1295 const std::vector<T> pi0 = a.vector_or_empty<T>(
"pi0");
1296 const std::size_t n = a.count(
"n");
1302 out[
"sts"] = encode_indices(p.states);
1309struct OpStmonotone {
1311 static Json run(Args& a) {
1312 const Matrix<T> M = a.matrix<T>(Dtmc ?
"P" :
"Q");
1387 const std::string
sn =
1388 "it takes a NetworkStruct; build the model and run line-cli -s ctmc -a <analysis> "
1391 t[
"ctmc_ssg_reachability"] =
sn;
1392 t[
"ctmc_state_space_logsize"] =
sn;
1393 t[
"npfqn_traffic_split_rr"] =
1394 "it takes a NetworkStruct; build the model and run line-cli -s <solver> -a <analysis> "
1396 t[
"npfqn_traffic_idc_at"] =
1397 "it evaluates two caller-supplied IDC functions (a0IdcFun and sIdcFun), which are "
1398 "callables with no JSON form.";
1399 const std::string
env =
1400 "its arrival and service envelopes are functions of theta (e.g. snc_env_poisson as a "
1401 "handle), callables with no JSON form; the envelope values at one theta are exposed "
1402 "through the snc_env_* and snc_srv_* entries.";
1403 t[
"snc_bound_backlog"] =
env;
1404 t[
"snc_bound_delay"] =
env;
1405 t[
"snc_perc_backlog"] =
env;
1406 t[
"snc_perc_delay"] =
env;
1407 t[
"snc_mean_backlog"] =
env;
1408 t[
"snc_mean_delay"] =
env;
1410 "its objective is a function of theta passed as a callable, which has no JSON form.";
1411 const std::string cosimla =
1412 "its transition function (successors and probabilities or rates of a state) and its "
1413 "reward function are callables with no JSON form.";
1414 t[
"ctmc_cosimla"] = cosimla;
1415 t[
"dtmc_cosimla"] = cosimla;
1416 t[
"ctmc_hitting_dist"] =
1417 "its transition function, target predicate and change of measure are callables with no "
1418 "JSON form in its function form, and its seeded estimator option set and result record "
1419 "have no JSON spelling yet.";
Internal to the –api dispatcher: what a per-domain translation unit needs to add entries to the table...
Named-argument reader over the parsed JSON object.
Accumulated state probabilities L(t) = int_0^t pi(s) ds of a CTMC by uniformisation (Reibman and Triv...
Classical stationary iterative solvers for pi Q = 0, sum(pi) = 1: the power method on the uniformised...
Grassmann-Taksar-Heyman (GTH) elimination for the stationary vector, and the limit matrix J = lim exp...
Feasibility predicates for generators and stochastic matrices.
Host-aware memory pre-gate for SolverCTMC.
First passage times into a target STATE SET, for Markov and semi-Markov chains.
Pseudo stochastic complement of a CTMC partition.
Random infinitesimal generator of a CTMC.
Sample path of a continuous-time Markov chain given its generator.
Stochastically stable distribution of a perturbed Markov chain (Wicks 2009).
Stochastically monotone upper bound of a Markov chain.
Kolmogorov reversibility criterion, used as a product-form test.
Normalizing constants of a discrete-time closed cycle of Bernoulli servers.
Equilibrium distribution of a discrete-time Markov chain, and stochastic complementation.
Discrete-time transient distributions, hitting times and uniformization.
DtNcLdResult< T > dpfqn_ncld(const std::vector< std::vector< T > > &P, std::size_t N)
Theorem 3.2 for a cycle whose service probabilities depend on the local queue length.
DtNcResult< T > dpfqn_nc(const std::vector< T > &p, std::size_t N)
Propositions 3.18 and 3.19 for a cycle with state independent service probabilities.
Matrix< T > dtmc_stmonotone(const Matrix< T > &P)
std::vector< std::complex< double > > ctmc_passage_lst(const Matrix< T > &Q, const std::vector< T > &pi0, const std::vector< std::size_t > &target, const std::vector< std::complex< double > > &s)
L(s) = alpha (sI-S)^{-1} s0 + atom at the (complex) points s.
PassageMoments< T > ctmc_passage_moments(const Matrix< T > &Q, const std::vector< T > &pi0, const std::vector< std::size_t > &target, std::size_t nmax=1)
Moments of order 1..nmax of the first passage time into target.
Matrix< T > ctmc_cesaro(const Matrix< T > &Q)
Limit matrix J = lim expm(Q t) of any finite CTMC (Agaev-Chebotarev 2000).
std::vector< double > ctmc_ssd(const Matrix< double > &Cin, const Matrix< double > &Rin)
Stochastically stable distribution, rates C(i,j) eps^R(i,j) (Wicks 2009).
StochCompResult< T > ctmc_stochcomp(const Matrix< T > &Q, const std::vector< std::size_t > &I)
CtmcGateResult ctmc_memory_gate(double log_nstates, bool force=false, double safety_fraction=CTMC_DEFAULT_SAFETY_FRACTION, bool verbose=false)
Decide whether a state space of log-size log_nstates can be solved here.
UniformizationResult< T > ctmc_uniformization(const std::vector< T > &pi0, const Matrix< T > &Q, const T &t, double tol=1e-12, long maxiter=-1)
Transient distribution of a CTMC by uniformization (Jensen's method), and the time-averaged distribut...
PassageCurve< T > ctmc_passage_time(const Matrix< T > &Q, const std::vector< T > &pi0, const std::vector< std::size_t > &target, const std::vector< double > &tset, const std::string &method="expm", const std::string <i_method="euler")
CDF and density of the first passage time on the grid tset: F(t) = 1 - alpha exp(St) 1 and f(t) = alp...
Matrix< T > ctmc_rand(std::size_t n, Gen &gen)
Random infinitesimal generator of a CTMC.
std::vector< T > dtmc_hitting_time(const Matrix< T > &P, const std::vector< std::size_t > &target)
Expected number of steps to reach the target set, zero on the target set itself.
TimeAverageResult< T > ctmc_timeaverage(const std::vector< T > &pi0, const Matrix< T > &Q, const T &t, double tol=1e-12, long maxiter=-1)
Time-averaged distribution (1/t) int_0^t pi(u) du, plus pi(t) itself.
GthResult< T > ctmc_gth_flag(const Matrix< T > &Q)
Stationary vector of a CTMC by GTH elimination, with a flag.
PassagePh< T > ctmc_passage_ph(const Matrix< T > &Q, const std::vector< T > &pi0, const std::vector< std::size_t > &target)
Phase-type representation of the first passage time from pi0 into target.
bool ctmc_isfeasible(const Matrix< T > &Q, const T &tol)
True when Q is square, has nonnegative off-diagonals, nonpositive diagonal and zero row sums.
constexpr long CLASSICAL_DEFAULT_MAXITER
PseudoStochCompResult< T > ctmc_pseudostochcomp(const Matrix< T > &Q, const std::vector< std::size_t > &keep)
Pseudo stochastic complement of a CTMC partition.
bool ctmc_testpf_kolmogorov(const Matrix< T > &Qin)
Kolmogorov reversibility criterion, used as a product-form test.
Matrix< T > ctmc_stmonotone(const Matrix< T > &Q)
Stochastically monotone upper bound of a Markov chain.
PassageTabooMoments< T > ctmc_passage_taboo_moments(const Matrix< T > &Q, const std::vector< T > &pi0, const std::vector< std::size_t > &target, const std::vector< std::size_t > &taboo, std::size_t nmax=1)
Moments of order 1..nmax of the first passage time into target, CONDITIONAL on entering it before tab...
CtmcPath< T > ctmc_simulate(const Matrix< T > &Q, const std::vector< T > &pi0, std::size_t n, pfqn::McRng &rng)
Simulate n steps of the CTMC with generator Q.
PassageTaboo< T > ctmc_passage_taboo(const Matrix< T > &Q, const std::vector< T > &pi0, const std::vector< std::size_t > &target, const std::vector< std::size_t > &taboo, const std::vector< double > &tset=std::vector< double >(), const std::string &method="expm", const std::string <i_method="euler")
First passage into target under the taboo set taboo, the reference's ctmc_passage_taboo.
Matrix< T > dtmc_transient(const Matrix< T > &P, const std::vector< T > &pi0, std::size_t steps)
Trajectory of the law over steps transitions, row k holding pi0 P^k.
std::vector< T > ctmc_hitting_time(const Matrix< T > &Q, const std::vector< std::size_t > &target)
Mean time to reach any state in target from each state of a CTMC.
AccumulatedResult< T > ctmc_accumulated(const std::vector< T > &pi0, const Matrix< T > &Q, const std::vector< T > &t, double tol=1e-12, long maxiter=-1)
Accumulated state probabilities int_0^t pi(s) ds at each time point.
constexpr double CLASSICAL_DEFAULT_TOL
Merge
Merge rule, MATLAB's config.merge.
@ Interpos
'interpos', lumped interleaving of per-flow M3PP(2, m) fits
@ Default
'default', identical to 'super'
@ Mixture
'mixture', superpose then refit with mmap_mixture_fit_mmap
TrafficIdcContext< T > npfqn_traffic_idc(const std::vector< T > &lambda0, const Matrix< T > &P, const std::vector< T > &c2a0, const std::vector< T > &mu, const std::vector< T > &cs2, const TrafficIdcCorrections &corrections)
Traffic variability equations of the Robust Queueing Network Analyzer (W.
T npfqn_rqna_weight(const T &t)
Canonical reflected-Brownian-motion correlation weight w*(t) used by the Robust Queueing Network Anal...
SqdResult< T > npfqn_sqd(const std::vector< T > &ST, const std::vector< T > &V, const std::vector< T > &cap, const std::vector< bool > &isDelay, const Matrix< T > &rt, const std::vector< std::size_t > &stationToStateful, std::size_t nclasses, int N, const SqdOptions< T > &opt)
Smith Queue Decomposition (SQD): approximate MVA for closed networks under Blocking-After-Service (ma...
NonexpApproxResult< T > npfqn_nonexp_approx(const std::string &method, const std::vector< bool > &isFCFS, const Matrix< T > &rates, const Matrix< T > &ST, const Matrix< T > &V, const Matrix< T > &SCV, const Matrix< T > &Tput, const Matrix< T > &U, const std::vector< T > &gamma, const std::vector< T > &nservers)
Handler for non-exponential service and arrival processes in AMVA and NC.
mam::Mmap< T > npfqn_traffic_merge_cs(const std::vector< mam::Mmap< T > > &flows, const Matrix< T > &prob, Merge merge=Merge::Default)
Merge flows that switch class on departure.
std::vector< Mmap< T > > npfqn_traffic_split_cs(const Mmap< T > &MMAP, const Matrix< T > &P)
Splitting of a marked MAP departure flow with class switching.
FeedbackElimResult< T > npfqn_feedback_elim(const Matrix< T > &P, const std::vector< T > &rho, const std::vector< T > &cs2=std::vector< T >(), const std::vector< T > &lambda=std::vector< T >(), bool immediateOnly=false)
Near-immediate feedback elimination for the robust queueing network analyzer.
BndBpt< T > npfqn_bnd_bpt(const std::vector< T > &lambda0, const std::vector< T > &mu, const Matrix< T > &P, const std::vector< std::size_t > &stationOf, const std::vector< T > &c)
First-order linear-programming relaxation of the achievable region of a multiclass open Markovian que...
TrafficRqt< T > npfqn_traffic_rqt(const std::vector< T > &lambda0, const std::vector< T > &Gamma0, const std::vector< T > &alpha0, const Matrix< T > &F)
Effective arrival processes of a network under the Robust Queueing calculus.
Compress
Post-merge compression, MATLAB's config.compress.
@ Default
'default', mmap_compress with its own default method
std::vector< Matrix< T > > Mmap
An MMAP as the MATLAB cell {D0, D1, D1^(1), ..., D1^(R)}.
BndBgt< T > npfqn_bnd_bgt(const std::vector< T > &lambda, const std::vector< std::vector< T > > &mu, const std::vector< std::vector< std::size_t > > &sigma, std::size_t J)
Piecewise-linear Lyapunov UPPER bound on the steady-state queue lengths of a multitype (deterministic...
DpsMorrisonResult< T > npfqn_dps_morrison(const std::vector< T > &N, const std::vector< T > &Z, const std::vector< T > &S, const std::vector< T > &w)
Evaluates Morrison's two-term approximation.
mam::Mmap< T > npfqn_traffic_merge(const std::vector< mam::Mmap< T > > &flows, const MergeConfig &config=MergeConfig())
Merge a list of MMAPs carrying the same classes.
std::mt19937_64 McRng
The generator type every Monte Carlo entry point in this tree accepts.
std::uint64_t required_seed(Args &a, const std::string &fn, const char *spelled="seed")
The seed of a function whose port draws from a caller-owned generator.
Json encode_vectors(const std::vector< std::vector< T > > &v)
A list of vectors, the shape a per-class or per-segment result returns.
Matrix< T > matrix_from_json(const Json &j, const std::string &where)
A matrix from a JSON value: 2-D array as rows, 1-D array as a row vector, scalar as 1x1,...
Json encode_matrices(const std::vector< Matrix< T > > &v)
A list of matrices, the shape the MMAP/BMAP families return as {D0,D1,...}.
void register_library_only_mc(LibraryOnlyTable &t)
T number_from_json(const Json &j, const std::string &where)
std::vector< T > vector_from_json(const Json &j, const std::string &where)
A flat numeric vector: 1-D array, or a 1-row / 1-column 2-D array.
Json encode_vector(const std::vector< T > &v)
Json encode_matrix(const Matrix< T > &m)
std::string decimal_text(const Json &j, const std::string &where)
The decimal literal behind a JSON scalar, per the policy in the file header.
void register_api_mc(DispatchTable &t)
Json encode_scalar(const T &v)
Json encode_count(std::size_t n)
A count.
std::map< std::string, std::string > LibraryOnlyTable
name -> why the function has no named-JSON-argument form.
std::map< std::string, Invoker > DispatchTable
Env snc_leftover(const Env &srv, const Env &cross)
Leftover service envelope under blind (arbitrary) multiplexing.
Env snc_conv(const Env &s1, const Env &s2, double theta, double delta=-1.0)
Min-plus convolution of two service envelopes (tandem concatenation).
Env snc_output(const Env &arv, const Env &srv, double theta)
Output (departure) arrival envelope of a flow leaving a server.
Env snc_srv_exp(double mu, double theta)
MGF service envelope of an exponential server, in JOB units.
Env snc_env_map(const Matrix< double > &D0, const Matrix< double > &D1, double theta)
MGF arrival envelope of a MAP/MMPP flow with unit-size jobs.
Env snc_srv_rate(double C)
MGF service envelope of a constant-rate work-conserving server.
Env snc_env_tokenbucket(double b, double r)
Deterministic token-bucket arrival envelope.
Env snc_env_poisson(double lambda, double theta)
MGF arrival envelope of a Poisson flow with unit-size jobs.
Env snc_env_cpoisson(double lambda, double mu, double theta)
MGF arrival envelope of a compound Poisson flow with Exp job sizes.
Conservation laws of a layered queueing network, enumerated from its structure.
Piecewise-linear Lyapunov UPPER bound on the steady-state queue lengths of a multitype (deterministic...
First-order linear-programming relaxation of the achievable region of a multiclass open Markovian que...
Two-term heavy-usage asymptotic approximation for a closed queueing network with one infinite-server ...
Near-immediate feedback elimination for the robust queueing network analyzer.
Handler for non-exponential service and arrival processes in AMVA and NC.
Canonical reflected-Brownian-motion correlation weight w*(t) used by the Robust Queueing Network Anal...
Smith Queue Decomposition (SQD): approximate MVA for closed networks under Blocking-After-Service (ma...
Traffic variability equations of the Robust Queueing Network Analyzer (W.
Superposition of several marked arrival flows into one.
Merge of marked arrival flows with class switching.
Effective arrival processes of a network under the Robust Queueing calculus.
Splitting of a marked MAP departure flow with class switching.
Min-plus convolution of two service envelopes (tandem concatenation).
MGF arrival envelope of a compound Poisson flow with Exp job sizes.
MGF arrival envelope of a MAP/MMPP flow with unit-size jobs.
MGF arrival envelope of a Poisson flow with unit-size jobs.
Deterministic token-bucket arrival envelope.
Leftover service envelope under blind (arbitrary) multiplexing.
Output (departure) arrival envelope of a flow leaving a server.
MGF service envelope of an exponential server, in JOB units.
MGF service envelope of a constant-rate work-conserving server.