5#ifndef LINE_IO_NETWORK_WRITER_H
6#define LINE_IO_NETWORK_WRITER_H
76 throw UnsupportedError(ErrorKind{
"Export"},
"network_writer: unnamed node type");
89 throw UnsupportedError(ErrorKind{
"Export"},
"network_writer: unnamed drop strategy");
102 throw UnsupportedError(ErrorKind{
"Export"},
"network_writer: unnamed replacement strategy");
113 throw UnsupportedError(ErrorKind{
"Export"},
"network_writer: unnamed polling type");
123 throw UnsupportedError(ErrorKind{
"Export"},
"network_writer: unnamed polling order");
133 throw UnsupportedError(ErrorKind{
"Export"},
"network_writer: a patience block with no impatience type");
143 throw UnsupportedError(ErrorKind{
"Export"},
"network_writer: a balking block with no strategy");
166 throw UnsupportedError(ErrorKind{
"Export"},
"network_writer: unnamed routing strategy");
178 throw UnsupportedError(ErrorKind{
"Export"},
"network_writer: unnamed heterogeneous scheduling policy");
192 for (std::size_t i = 0; i < out.size(); ++i)
193 out[i] =
static_cast<char>(std::toupper(
static_cast<unsigned char>(out[i])));
199json mat_to_json(
const Matrix<T>& M) {
200 json rows = json::array();
201 for (std::size_t i = 0; i < M.rows(); ++i) {
202 json row = json::array();
203 for (std::size_t j = 0; j < M.cols(); ++j)
204 row.push_back(num_traits<T>::to_double(M(i, j)));
211json vec_to_json(
const std::vector<T>& v) {
212 json a = json::array();
213 for (
const T& x : v) a.push_back(num_traits<T>::to_double(x));
231inline void wire_nonfinite(json& j) {
232 if (j.is_object() || j.is_array()) {
233 for (json::iterator it = j.begin(); it != j.end(); ++it) wire_nonfinite(*it);
236 if (!j.is_number_float())
return;
237 const double v = j.get<
double>();
240 else if (std::isinf(v))
241 j = json(v > 0.0 ?
"Infinity" :
"-Infinity");
252inline json discrete_support(std::size_t n) {
253 json a = json::array();
254 for (std::size_t i = 1; i <= n; ++i) a.push_back(
static_cast<double>(i));
267json dist_to_json(
const lang::Distrib<T>& d) {
271 const std::vector<T>& p = d.params;
272 auto need = [&](std::size_t n,
const char* who) {
274 throw InputError(std::string(
"network_writer: a ") + who +
" carries " +
275 std::to_string(p.size()) +
" parameters, and " + std::to_string(n) +
276 " are needed to write it back");
279 case ProcessType::DISABLED:
280 case ProcessType::IMMEDIATE:
282 case ProcessType::EXP:
284 j[
"params"][
"lambda"] = num_traits<T>::to_double(p[0]);
286 case ProcessType::DET:
287 j[
"params"][
"value"] = num_traits<T>::to_double(d.mean);
289 case ProcessType::ERLANG:
291 j[
"params"][
"lambda"] = num_traits<T>::to_double(p[0]);
292 j[
"params"][
"k"] =
static_cast<long>(std::lround(num_traits<T>::to_double(p[1])));
294 case ProcessType::HYPEREXP: {
298 json pv = json::array(), lv = json::array();
300 pv.push_back(num_traits<T>::to_double(p[0]));
301 pv.push_back(1.0 - num_traits<T>::to_double(p[0]));
302 lv.push_back(num_traits<T>::to_double(p[1]));
303 lv.push_back(num_traits<T>::to_double(p[2]));
305 const std::size_t n = p.size() / 2;
306 for (std::size_t i = 0; i < n; ++i) pv.push_back(num_traits<T>::to_double(p[i]));
307 for (std::size_t i = 0; i < n; ++i) lv.push_back(num_traits<T>::to_double(p[n + i]));
309 j[
"params"][
"p"] = pv;
310 j[
"params"][
"lambda"] = lv;
313 case ProcessType::COXIAN:
314 case ProcessType::COX2: {
315 const std::size_t n = p.size() / 2;
316 json mu = json::array(), phi = json::array();
317 for (std::size_t i = 0; i < n; ++i) mu.push_back(num_traits<T>::to_double(p[i]));
318 for (std::size_t i = 0; i < n; ++i) phi.push_back(num_traits<T>::to_double(p[n + i]));
321 j[
"type"] =
"Coxian";
322 j[
"params"][
"mu"] = mu;
323 j[
"params"][
"phi"] = phi;
326 case ProcessType::PH:
327 case ProcessType::APH:
328 case ProcessType::ME: {
329 json alpha = json::array();
330 for (
const T& v : p) alpha.push_back(num_traits<T>::to_double(v));
331 if (d.type == ProcessType::ME) {
332 j[
"params"][
"alpha"] = alpha;
333 j[
"params"][
"A"] = mat_to_json(d.D0);
335 j[
"ph"][
"alpha"] = alpha;
336 j[
"ph"][
"T"] = mat_to_json(d.D0);
340 case ProcessType::MAP:
341 j[
"map"][
"D0"] = mat_to_json(d.D0);
342 j[
"map"][
"D1"] = mat_to_json(d.D1);
344 case ProcessType::RAP:
345 j[
"params"][
"H0"] = mat_to_json(d.D0);
346 j[
"params"][
"H1"] = mat_to_json(d.D1);
348 case ProcessType::DMAP:
349 j[
"params"][
"D0"] = mat_to_json(d.D0);
350 j[
"params"][
"D1"] = mat_to_json(d.D1);
352 case ProcessType::MMPP2: {
356 if (d.D0.rows() != 2)
357 throw InputError(
"network_writer: an MMPP2 must be of order two");
358 j[
"params"][
"lambda0"] = num_traits<T>::to_double(d.D1(0, 0));
359 j[
"params"][
"lambda1"] = num_traits<T>::to_double(d.D1(1, 1));
360 j[
"params"][
"sigma0"] = num_traits<T>::to_double(d.D0(0, 1));
361 j[
"params"][
"sigma1"] = num_traits<T>::to_double(d.D0(1, 0));
364 case ProcessType::MPH: {
370 const std::size_t H = d.D0.rows();
371 std::vector<double> alpha(H, 0.0);
372 for (std::size_t i = 0; i < H; ++i) {
374 for (std::size_t q = 0; q < H; ++q) row += num_traits<T>::to_double(d.D1(i, q));
376 for (std::size_t q = 0; q < H; ++q)
377 alpha[q] = num_traits<T>::to_double(d.D1(i, q)) / row;
381 j[
"mph"][
"alpha"] = alpha;
382 j[
"mph"][
"S"] = mat_to_json(d.D0);
383 json exits = json::array();
384 for (
const Matrix<T>& Dk : d.Dmark) {
385 std::vector<double> sk(H, 0.0);
386 for (std::size_t i = 0; i < H; ++i)
387 for (std::size_t q = 0; q < H; ++q)
388 sk[i] += num_traits<T>::to_double(Dk(i, q));
391 j[
"mph"][
"exit"] = exits;
394 case ProcessType::MMAP: {
395 j[
"mmap"][
"D0"] = mat_to_json(d.D0);
396 json blocks = json::array();
397 for (
const Matrix<T>& Dk : d.Dmark) blocks.push_back(mat_to_json(Dk));
398 j[
"mmap"][
"D1k"] = blocks;
401 case ProcessType::BMAP: {
402 json blocks = json::array();
403 blocks.push_back(mat_to_json(d.D0));
404 for (
const Matrix<T>& Dk : d.Dmark) blocks.push_back(mat_to_json(Dk));
405 j[
"params"][
"D"] = blocks;
408 case ProcessType::UNIFORM:
410 j[
"params"][
"a"] = num_traits<T>::to_double(p[0]);
411 j[
"params"][
"b"] = num_traits<T>::to_double(p[1]);
413 case ProcessType::PARETO:
415 j[
"params"][
"alpha"] = num_traits<T>::to_double(p[0]);
416 j[
"params"][
"scale"] = num_traits<T>::to_double(p[1]);
418 case ProcessType::GAMMA:
420 j[
"params"][
"alpha"] = num_traits<T>::to_double(p[0]);
421 j[
"params"][
"beta"] = num_traits<T>::to_double(p[1]);
423 case ProcessType::WEIBULL:
427 j[
"params"][
"alpha"] = num_traits<T>::to_double(p[0]);
428 j[
"params"][
"beta"] = num_traits<T>::to_double(p[1]);
430 case ProcessType::LOGLOGISTIC:
432 need(2,
"LogLogistic");
433 j[
"params"][
"alpha"] = num_traits<T>::to_double(p[0]);
434 j[
"params"][
"beta"] = num_traits<T>::to_double(p[1]);
436 case ProcessType::MASSATORIGIN:
437 case ProcessType::DEFECTIVE:
440 throw InputError(ErrorKind{
"Format"},
"network_writer: a wrapper law without its inner law");
441 j[
"params"][d.type == ProcessType::MASSATORIGIN ?
"p0" :
"pInf"] =
442 num_traits<T>::to_double(p[0]);
443 j[
"dist"] = dist_to_json(*d.inner);
445 case ProcessType::LOGNORMAL:
446 need(2,
"Lognormal");
447 j[
"params"][
"mu"] = num_traits<T>::to_double(p[0]);
448 j[
"params"][
"sigma"] = num_traits<T>::to_double(p[1]);
450 case ProcessType::DUNIFORM:
451 need(2,
"DiscreteUniform");
452 j[
"params"][
"min"] = num_traits<T>::to_double(p[0]);
453 j[
"params"][
"max"] = num_traits<T>::to_double(p[1]);
455 case ProcessType::BERNOULLI:
456 need(1,
"Bernoulli");
457 j[
"params"][
"p"] = num_traits<T>::to_double(p[0]);
459 case ProcessType::BINOMIAL:
461 j[
"params"][
"n"] =
static_cast<long>(std::lround(num_traits<T>::to_double(p[0])));
462 j[
"params"][
"p"] = num_traits<T>::to_double(p[1]);
464 case ProcessType::POISSON:
466 j[
"params"][
"lambda"] = num_traits<T>::to_double(p[0]);
468 case ProcessType::GEOMETRIC:
469 need(1,
"Geometric");
470 j[
"params"][
"p"] = num_traits<T>::to_double(p[0]);
472 case ProcessType::ZIPF:
474 j[
"params"][
"s"] = num_traits<T>::to_double(p[0]);
475 j[
"params"][
"n"] =
static_cast<long>(std::lround(num_traits<T>::to_double(p[1])));
477 case ProcessType::DISCRETESAMPLER:
478 j[
"params"][
"p"] = vec_to_json(p);
479 if (!d.trace.empty()) j[
"params"][
"x"] = vec_to_json(d.trace);
481 case ProcessType::EMPIRICALCDF:
482 j[
"params"][
"x"] = vec_to_json(d.trace);
483 j[
"params"][
"F"] = vec_to_json(p);
485 case ProcessType::REPLAYER:
492 if (!d.trace_file.empty()) j[
"params"][
"fileName"] = d.trace_file;
493 j[
"params"][
"mean"] = num_traits<T>::to_double(d.mean);
494 if (d.trace_file.empty())
495 j[
"params"][
"scv"] = num_traits<T>::to_double(d.scv);
497 case ProcessType::MMAPT:
498 case ProcessType::MPHT: {
499 j[
"params"][
"breakpoints"] = vec_to_json(d.sched_bp);
500 j[
"params"][
"cyclic"] = d.sched_cyclic;
501 json A = json::array();
502 for (
const Matrix<T>& M : d.sched_D0) A.push_back(mat_to_json(M));
503 json blocks = json::array();
504 for (
const std::vector<Matrix<T> >& per_mark : d.sched_Dmark) {
505 json segs = json::array();
506 for (
const Matrix<T>& M : per_mark) segs.push_back(mat_to_json(M));
507 blocks.push_back(segs);
514 j[
"params"][
"D0"] = A;
515 j[
"params"][
"D1k"] = blocks;
518 case ProcessType::BMMAPT: {
523 j[
"params"][
"breakpoints"] = vec_to_json(d.sched_bp);
524 j[
"params"][
"cyclic"] = d.sched_cyclic;
525 json A = json::array();
526 for (
const Matrix<T>& M : d.sched_D0) A.push_back(mat_to_json(M));
527 json blocks = json::array();
528 for (
const std::vector<std::vector<Matrix<T> > >& per_mark : d.sched_Dbatch) {
529 json per_batch = json::array();
530 for (
const std::vector<Matrix<T> >& batch : per_mark) {
531 json segs = json::array();
532 for (
const Matrix<T>& M : batch) segs.push_back(mat_to_json(M));
533 per_batch.push_back(segs);
535 blocks.push_back(per_batch);
537 j[
"params"][
"D0"] = A;
538 j[
"params"][
"D1kb"] = blocks;
546 case ProcessType::NHPP:
547 case ProcessType::MAPT:
548 case ProcessType::PHT: {
549 j[
"params"][
"breakpoints"] = vec_to_json(d.sched_bp);
550 j[
"params"][
"cyclic"] = d.sched_cyclic;
551 if (d.type == ProcessType::NHPP) {
553 json rates = json::array();
554 for (
const Matrix<T>& D1 : d.sched_D1)
555 rates.push_back(num_traits<T>::to_double(D1(0, 0)));
556 j[
"params"][
"rates"] = rates;
559 json A = json::array(), B = json::array();
560 for (
const Matrix<T>& M : d.sched_D0) A.push_back(mat_to_json(M));
561 for (
const Matrix<T>& M : d.sched_D1) B.push_back(mat_to_json(M));
565 j[
"params"][
"D0"] = A;
566 j[
"params"][
"D1"] = B;
569 case ProcessType::NNPP: {
573 throw InputError(
"network_writer: an NNPP carries no rate function to write back");
574 const lang::Nnpp& n = *d.nnpp;
575 auto arr = [](
const std::vector<double>& v) {
576 json a = json::array();
577 for (
double x : v) a.push_back(x);
581 rate[
"kind"] = n.kind_name();
583 rate[
"alpha"] = arr(n.alpha());
584 rate[
"gamma"] = arr(n.gamma());
585 rate[
"omega"] = arr(n.omega());
586 rate[
"phi"] = arr(n.phi());
588 rate[
"times"] = arr(n.times());
589 rate[
"rates"] = arr(n.rates());
591 j[
"params"][
"rate"] = rate;
592 j[
"params"][
"horizon"] = n.horizon();
593 j[
"params"][
"dispersion"] = n.dispersion();
594 j[
"params"][
"cyclic"] = n.is_cyclic();
595 j[
"params"][
"Q"] = n.q();
598 case ProcessType::PRIOR:
600 "network_writer: a Prior is a set of alternative MODELS rather than one law; save "
601 "the design point SolverUQ built, not the design");
605 throw UnsupportedError(ErrorKind{
"Export"}, std::string(
"network_writer: distribution family '") +
618std::vector<int> lattice_cutoffs(
const qn::NetworkStruct<T>& sn) {
619 std::vector<int> cut(sn.classes.size(), 10);
620 for (std::size_t r = 0; r < sn.classes.size(); ++r)
622 std::isfinite(sn.classes[r].population))
623 cut[r] =
static_cast<int>(std::lround(sn.classes[r].population));
633void for_each_lattice_point(
const std::vector<int>& cut,
const F& visit,
bool with_empty =
false) {
634 std::size_t total = 1;
635 for (
int c : cut) total *=
static_cast<std::size_t
>(c + 1);
636 const std::size_t K = cut.size();
637 for (std::size_t i = 0; i < total; ++i) {
638 std::vector<int> cnt(K, 0);
640 for (std::size_t d = 0; d < K; ++d) {
641 cnt[d] =
static_cast<int>(li %
static_cast<std::size_t
>(cut[d] + 1));
642 li /=
static_cast<std::size_t
>(cut[d] + 1);
646 for (std::size_t d = 0; d < K; ++d) {
648 key += std::to_string(cnt[d]);
651 if (tot == 0 && !with_empty)
continue;
662void for_each_simplex_point(std::size_t K,
int cmax,
const F& visit) {
663 for (
int tot = 1;
tot <= cmax; ++
tot) {
664 std::vector<int> cnt(K, 0);
668 for (std::size_t d = 0; d < K; ++d) {
670 key += std::to_string(cnt[d]);
676 for (std::size_t d = 0; d + 1 < K; ++d)
682 const int v = cnt[j];
715 const std::function<std::vector<double>(
const std::vector<int>&)>& f_at, std::size_t K,
716 double cmax = std::numeric_limits<double>::infinity(),
int max_cutoff = 256,
719 out.
tau.assign(K, 1);
723 for (std::size_t r = 0; r < K; ++r) {
725 std::size_t total = 1;
726 for (std::size_t j = 0; j < K; ++j)
727 if (j != r) total *=
static_cast<std::size_t
>(out.
tau[j] + 1);
729 for (std::size_t li = 0; li < total && sat; ++li) {
730 std::vector<int> m(K, 0);
731 std::size_t rem = li;
733 for (std::size_t j = 0; j < K; ++j) {
737 m[j] =
static_cast<int>(rem %
static_cast<std::size_t
>(out.
tau[j] + 1));
738 rem /=
static_cast<std::size_t
>(out.
tau[j] + 1);
742 if (
static_cast<double>(
sum) + 1.0 > cmax)
continue;
743 std::vector<int> mp(m);
745 const std::vector<double> a = f_at(mp), b = f_at(m);
747 for (std::size_t k = 0; k < a.size() && k < b.size(); ++k)
748 d = std::max(d, std::fabs(a[k] - b[k]));
749 if (d > tol) sat =
false;
754 if (out.
tau[r] > max_cutoff) {
761 for (std::size_t r = 0; r < K; ++r)
762 if (!(
static_cast<double>(out.
tau[r]) < cmax)) out.
bounded =
false;
775 const std::function<T(
const std::vector<std::size_t>&)>& mu, std::size_t K,
776 double cmax = std::numeric_limits<double>::infinity()) {
778 [&mu, K](
const std::vector<int>& n) {
779 std::vector<std::size_t> order;
780 for (std::size_t r = 0; r < K; ++r)
781 for (
int c = 0; c < n[r]; ++c) order.push_back(r + 1);
783 return std::vector<double>(1, std::isfinite(v) ? v : 0.0);
792std::vector<double> cd_scaling_at(
const lang::CdScaling<T>& fun,
const std::vector<int>& cnt,
795 for (std::size_t r = 0; r < K; ++r) n[r] = num_traits<T>::from_int(
static_cast<long>(cnt[r]));
796 const std::vector<T> v = fun(n);
797 std::vector<double> out(K, 0.0);
798 for (std::size_t r = 0; r < K; ++r) {
802 out[r] = std::isfinite(x) ? x : 0.0;
821void cd_scaling_extent(
const lang::CdScaling<T>& fun, std::size_t K,
const std::vector<double>& bb,
822 double C, json& blk) {
823 const auto put = [&](json& tbl,
const std::vector<int>& cnt,
const std::string& key) {
824 tbl[key] = cd_scaling_at<T>(fun, cnt, K);
827 for (std::size_t r = 0; r < K; ++r) finite = finite && r < bb.size() && std::isfinite(bb[r]);
828 json tbl = json::object();
830 std::vector<int> maxc(K, 0);
832 for (std::size_t r = 0; r < K; ++r) {
833 maxc[r] =
static_cast<int>(std::floor(bb[r]));
836 const double cs = std::isfinite(C) ? std::floor(C) : C;
837 if (
static_cast<double>(tot) <= cs) {
838 for_each_lattice_point(maxc, [&](
const std::vector<int>& cnt,
const std::string& key) {
841 blk[
"cutoffs"] = maxc;
843 for_each_lattice_point(maxc, [&](
const std::vector<int>& cnt,
const std::string& key) {
845 for (
int c : cnt) s += c;
846 if (
static_cast<double>(s) <= cs)
put(tbl, cnt, key);
849 blk[
"scaling"] = tbl;
853 [&fun, K](
const std::vector<int>& n) {
return cd_scaling_at<T>(fun, n, K); }, K);
854 std::vector<int> maxc;
859 for (std::size_t r = 0; r < K; ++r)
860 if (r < bb.size() && std::isfinite(bb[r])) maxc[r] =
static_cast<int>(std::floor(bb[r]));
862 for_each_lattice_point(maxc, [&](
const std::vector<int>& cnt,
const std::string& key) {
865 blk[
"cutoffs"] = maxc;
866 blk[
"scaling"] = tbl;
881 const std::size_t K =
sn.classes.size();
882 const std::vector<int> cut = detail::lattice_cutoffs(
sn);
885 model[
"type"] =
"Network";
886 model[
"name"] =
sn.name;
889 if (!
sn.log_path.empty()) model[
"logPath"] =
sn.log_path;
892 json classes = json::array();
893 for (std::size_t r = 0; r < K; ++r) {
900 const bool sig = r <
sn.issignal.size() &&
sn.issignal[r];
902 cj[
"type"] =
"Signal";
909 if (
sn.signaltarget[r] >= 1 &&
sn.signaltarget[r] <= K)
910 cj[
"targetClass"] =
sn.classes[
sn.signaltarget[r] - 1].name;
914 if (!
sn.signalremdist[r].empty()) {
915 json pv = json::array();
917 json xv = json::array();
923 for (std::size_t b = 0; b < pv.size(); ++b) xv.push_back(
static_cast<double>(b));
925 rd[
"type"] =
"DiscreteSampler";
926 rd[
"params"][
"p"] = pv;
927 rd[
"params"][
"x"] = xv;
928 cj[
"removalDistribution"] = rd;
938 if (std::isfinite(pop) && pop == std::floor(pop) && std::fabs(pop) < 9.0e15)
939 cj[
"population"] =
static_cast<long long>(pop);
941 cj[
"population"] = pop;
944 "' names no reference station, which a closed class must have");
945 cj[
"refNode"] =
sn.nodes[
sn.station_to_node[c.
refstat - 1] - 1].name;
947 if (c.
prio != 0) cj[
"priority"] = c.
prio;
948 if (c.
immfeed) cj[
"immediateFeedback"] =
true;
951 if (c.
spawn >= 1 && c.
spawn <= K) cj[
"spawnClass"] =
sn.classes[c.
spawn - 1].name;
952 if (r <
sn.syncreply.size() &&
sn.syncreply[r] >= 1 &&
sn.syncreply[r] <= K)
953 cj[
"replySignalClass"] =
sn.classes[
sn.syncreply[r] - 1].name;
954 classes.push_back(cj);
956 model[
"classes"] = classes;
959 json nodes = json::array();
960 for (std::size_t i = 0; i <
sn.nodes.size(); ++i) {
962 const std::size_t ind = i + 1, ist = nd.
station;
964 nj[
"name"] = nd.
name;
981 json by_dest = json::array(), by_prob = json::array(), by_dist = json::array();
982 for (std::size_t k = 0; k < fp->
fan_out_link.rows(); ++k)
983 for (std::size_t r = 0; r < fp->
fan_out_link.cols() && r < K; ++r) {
985 if (p == 0.0)
continue;
987 rec[
"dest"] =
sn.nodes[k].name;
988 rec[
"class"] = r + 1;
990 by_dest.push_back(rec);
993 pr[
"dest"] =
sn.nodes[k].name;
996 by_prob.push_back(pr);
1001 dr[
"dest"] =
sn.nodes[k].name;
1002 dr[
"class"] = r + 1;
1003 json pv = json::array(), xv = json::array();
1004 for (std::size_t e = 0; e < d.
params.size(); ++e) {
1006 xv.push_back(d.
trace.empty()
1007 ?
static_cast<double>(e + 1)
1012 by_dist.push_back(dr);
1015 if (!by_dest.empty()) nj[
"fanOutByDest"] = by_dest;
1016 if (!by_prob.empty()) nj[
"fanOutProb"] = by_prob;
1017 if (!by_dist.empty()) nj[
"fanOutDist"] = by_dist;
1039 typename std::map<std::size_t, std::pair<std::size_t, std::size_t> >::const_iterator
fj;
1040 for (std::size_t f = 0; f <
sn.fj.size(); ++f)
1041 if (
sn.fj[f].second == ind)
1042 nj[
"forkNode"] =
sn.nodes[
sn.fj[f].first - 1].name;
1043 typename std::map<std::size_t, typename SN::JoinDecl>::const_iterator jd =
1044 sn.joindecl.find(ind);
1045 if (jd !=
sn.joindecl.end()) {
1047 nj[
"joinStrategy"] =
"PARTIAL";
1048 if (jd->second.quorum > 0) nj[
"joinQuorum"] = jd->second.quorum;
1052 typename std::map<std::size_t, Matrix<T> >::const_iterator cs =
sn.csmatrix.find(ind);
1053 if (cs !=
sn.csmatrix.end()) {
1055 for (std::size_t r = 0; r < K; ++r) {
1057 for (std::size_t s = 0; s < K; ++s) {
1059 if (v != 0.0) row[
sn.classes[s].name] = v;
1061 if (!row.empty()) csm[
sn.classes[r].name] = row;
1063 nj[
"classSwitchMatrix"] = csm;
1067 typename std::map<std::size_t, qn::CacheParam<T> >::const_iterator cp =
1068 sn.nodeparam.find(ind);
1069 if (cp !=
sn.nodeparam.end()) {
1071 nj[
"numItems"] = c.
nitems;
1072 nj[
"itemLevelCap"] = c.
itemcap;
1073 nj[
"replacementStrategy"] = detail::replacement_to_json(c.
replacestrat);
1079 else nj[
"costCaps"] = c.
costcap;
1081 json pop, hit, miss, itemcls;
1082 for (std::size_t r = 0; r < K && r < c.
pread.size(); ++r) {
1083 if (c.
pread[r].empty())
continue;
1085 pj[
"type"] =
"DiscreteSampler";
1086 pj[
"params"][
"p"] = detail::vec_to_json(c.
pread[r]);
1087 pj[
"params"][
"x"] = detail::discrete_support(c.
pread[r].size());
1088 pop[
sn.classes[r].name] = pj;
1090 for (std::size_t r = 0; r < K && r < c.
hitclass.size(); ++r)
1092 hit[
sn.classes[r].name] =
sn.classes[c.
hitclass[r] - 1].name;
1093 for (std::size_t r = 0; r < K && r < c.
missclass.size(); ++r)
1095 miss[
sn.classes[r].name] =
sn.classes[c.
missclass[r] - 1].name;
1096 for (std::size_t r = 0; r < K && r < c.
classitem.size(); ++r)
1098 itemcls[
sn.classes[r].name] =
static_cast<double>(c.
classitem[r]);
1099 if (!pop.empty()) nj[
"popularity"] = pop;
1100 if (!hit.empty()) nj[
"hitClass"] = hit;
1101 if (!miss.empty()) nj[
"missClass"] = miss;
1102 if (!itemcls.empty()) nj[
"itemClass"] = itemcls;
1104 json ap = json::array();
1106 json row = json::array();
1108 row.push_back(g.rows() == 0 ? json::array() : detail::mat_to_json(g));
1111 nj[
"accessProb"] = ap;
1120 json qs = json::array();
1121 for (std::size_t q : kv.second) qs.push_back(
sn.nodes[q - 1].name);
1122 entry[
"queues"] = qs;
1127 items[std::to_string(it)] =
1129 entry[
"items"] = items;
1130 by[
sn.classes[kv.first].name] = entry;
1133 nj[
"retrievalSystem"] = rs;
1138 typename std::map<std::size_t, std::vector<T> >::const_iterator im =
1139 sn.initmarking.find(ind);
1140 if (im !=
sn.initmarking.end()) nj[
"initialState"] = detail::vec_to_json(im->second);
1141 typename std::map<std::size_t, std::vector<T> >::const_iterator sp =
1142 sn.stateprior.find(ind);
1143 typename std::map<std::size_t, Matrix<T> >::const_iterator ss =
sn.statespace.find(ind);
1144 if (sp !=
sn.stateprior.end() && ss !=
sn.statespace.end()) {
1145 nj[
"stateSpace"] = detail::mat_to_json(ss->second);
1146 nj[
"statePrior"] = detail::vec_to_json(sp->second);
1154 nj[
"scheduling"] = detail::sched_to_json(st.
sched);
1162 if (std::isfinite(st.
cap) && st.
cap > 0) nj[
"buffer"] = st.
cap;
1171 json svc, cc, dr, sp, imf;
1172 for (std::size_t r = 0; r < K; ++r) {
1173 if (ist - 1 <
sn.service.size() && r <
sn.service[ist - 1].size() &&
1174 !
sn.service[ist - 1][r].disabled)
1175 svc[
sn.classes[r].name] = detail::dist_to_json(
sn.service[ist - 1][r]);
1183 dr[
sn.classes[r].name] =
1185 if (r < st.
immfeed.size() && st.
immfeed[r]) imf[
sn.classes[r].name] =
true;
1189 for (std::size_t r = 0; r < K && r < st.
schedparam.size(); ++r)
1191 if (!svc.empty()) nj[
"service"] = svc;
1192 if (!cc.empty()) nj[
"classCap"] = cc;
1193 if (!dr.empty()) nj[
"dropRule"] = dr;
1194 if (!sp.empty()) nj[
"schedParams"] = sp;
1195 if (!imf.empty()) nj[
"immediateFeedback"] = imf;
1199 ld[
"type"] =
"loadDependent";
1200 ld[
"scaling"] = detail::vec_to_json(st.
lldscaling);
1201 nj[
"loadDependence"] = ld;
1206 for (
int kind = 0; kind < 2; ++kind) {
1208 if (!
static_cast<bool>(fun))
continue;
1210 blk[
"type"] = kind == 0 ?
"classDependent" :
"jointDependent";
1211 std::vector<double> bb(K, std::numeric_limits<double>::infinity());
1212 if (ist - 1 <
sn.classcap.size())
1213 for (std::size_t r = 0; r < K && r <
sn.classcap[ist - 1].size(); ++r)
1214 bb[r] =
sn.classcap[ist - 1][r];
1215 const double C = ist - 1 <
sn.cap.size() ?
sn.cap[ist - 1]
1216 : std::numeric_limits<double>::infinity();
1217 detail::cd_scaling_extent<T>(fun, K, bb, C, blk);
1219 nj[kind == 0 ?
"classDependence" :
"jointDependence"] = blk;
1222 typename std::map<std::size_t, typename SN::PasParam>::const_iterator pas =
1223 sn.pasparam.find(ist);
1224 if (pas !=
sn.pasparam.end() &&
static_cast<bool>(pas->second.svc_rate_fun)) {
1232 const auto put = [&](
const std::vector<int>& cnt,
const std::string& key) {
1233 std::vector<std::size_t> micro;
1234 for (std::size_t r = 0; r < K; ++r)
1235 for (
int c = 0; c < cnt[r]; ++c) micro.push_back(r + 1);
1237 tbl[key] = std::isfinite(v) ? v : 0.0;
1242 const double cmax = std::isfinite(st.
cap)
1243 ? std::max(std::floor(st.
cap), 1.0)
1244 : std::numeric_limits<double>::infinity();
1248 detail::for_each_lattice_point(sat.
tau, put);
1249 nj[
"oiServiceRate"] = tbl;
1250 nj[
"oiCutoffs"] = sat.
tau;
1251 }
else if (std::isfinite(st.
cap)) {
1252 detail::for_each_simplex_point(
1253 K, std::max(
static_cast<int>(std::floor(st.
cap)), 1), put);
1254 nj[
"oiServiceRate"] = tbl;
1256 for (std::size_t r = 0; r < K; ++r)
1258 !std::isfinite(
sn.classes[r].population))
1260 "' has an unbounded order-independent service rate (mu does not "
1261 "saturate) and no finite buffer; call setCap(N) on the station.");
1262 detail::for_each_lattice_point(cut, put);
1263 nj[
"oiServiceRate"] = tbl;
1264 nj[
"oiCutoffs"] = cut;
1266 bool any_swap =
false;
1267 for (
const std::vector<bool>& row : pas->second.swap_graph)
1268 for (
bool v : row) any_swap = any_swap || v;
1270 json sg = json::array();
1271 for (
const std::vector<bool>& row : pas->second.swap_graph) {
1272 json rj = json::array();
1273 for (
bool v : row) rj.push_back(v ? 1 : 0);
1276 nj[
"swapGraph"] = sg;
1289 sn.pollingparam.find(ist) !=
sn.pollingparam.end();
1290 const std::vector<lang::Distrib<T> >& so_buf =
1291 via_param ?
sn.pollingparam.find(ist)->second.switchover : st.
switchover;
1294 sn.pollingparam.find(ist)->second;
1295 nj[
"pollingType"] = detail::polling_to_json(pp.
ptype);
1297 nj[
"pollingPar"] = pp.
pk;
1299 nj[
"pollingType"] = detail::polling_to_json(st.
polling_type[0]);
1306 json so = json::array();
1307 for (std::size_t r = 0; r < K && r < so_buf.size(); ++r) {
1308 if (so_buf[r].disabled)
continue;
1310 e[
"from"] =
sn.classes[r].name;
1311 e[
"distribution"] = detail::dist_to_json(so_buf[r]);
1315 for (std::size_t c = 0; c < K && c < st.
switchover_pair[r].size(); ++c) {
1318 e[
"from"] =
sn.classes[r].name;
1319 e[
"to"] =
sn.classes[c].name;
1323 if (!so.empty()) nj[
"switchoverTimes"] = so;
1328 json qm = json::object();
1329 for (std::size_t r = 0; r < K && r < st.
quantum.size(); ++r) {
1330 if (st.
quantum[r].disabled)
continue;
1331 qm[
sn.classes[r].name] = detail::dist_to_json(st.
quantum[r]);
1333 if (!qm.empty()) nj[
"quantum"] = qm;
1339 json tab = json::array();
1347 nj[
"pollingTable"] = tab;
1350 nj[
"pollingOrder"] = detail::polling_order_to_json(st.
polling_order);
1360 json td = json::array();
1361 for (std::size_t r = 0; r < K && r < st.
polling_timer.size(); ++r) {
1364 e[
"class"] =
sn.classes[r].name;
1365 e[
"distribution"] = detail::dist_to_json(st.
polling_timer[r]);
1368 tm[
"distributions"] = td;
1369 nj[
"pollingTimer"] = tm;
1373 typename std::map<std::size_t, qn::SetupDelayOffParam<T> >::const_iterator sd =
1374 sn.setupparam.find(ist);
1375 if (sd !=
sn.setupparam.end()) {
1377 for (std::size_t r = 0; r < K && r < sd->second.setup.size(); ++r) {
1378 if (sd->second.setup[r].disabled)
continue;
1379 su[
sn.classes[r].name] = detail::dist_to_json(sd->second.setup[r]);
1380 doff[
sn.classes[r].name] = detail::dist_to_json(sd->second.delayoff[r]);
1383 nj[
"setupTime"] = su;
1384 nj[
"delayOffTime"] = doff;
1393 typename std::map<std::size_t, qn::BreakdownParam<T> >::const_iterator bd =
1394 sn.breakdownparam.find(ist);
1395 if (bd !=
sn.breakdownparam.end()) {
1397 bj[
"failure"] = detail::dist_to_json(bd->second.failure);
1398 bj[
"repair"] = detail::dist_to_json(bd->second.repair);
1400 for (std::size_t r = 0; r < K && r < bd->second.down_service_rates.size(); ++r) {
1403 if (!(rate > 0.0))
continue;
1404 ds[
sn.classes[r].name] = detail::dist_to_json(
1407 if (!ds.empty()) bj[
"downService"] = ds;
1408 nj[
"breakdown"] = bj;
1412 typename std::map<std::size_t, qn::RetrialParam<T> >::const_iterator rt =
1413 sn.retrialparam.find(ist);
1414 if (rt !=
sn.retrialparam.end()) {
1416 for (std::size_t r = 0; r < K && r < rt->second.retrial_proc.size(); ++r) {
1417 if (rt->second.retrial_proc[r].disabled)
continue;
1419 e[
"delay"] = detail::dist_to_json(rt->second.retrial_proc[r]);
1420 e[
"maxAttempts"] = rt->second.max_attempts[r];
1421 if (r < rt->second.policy.size() && rt->second.policy[r] == 2)
1422 e[
"policy"] =
"constant";
1423 if (r < rt->second.max_orbit.size() && rt->second.max_orbit[r] >= 0)
1424 e[
"maxOrbit"] = rt->second.max_orbit[r];
1431 if (jp < 1.0) e[
"joinProb"] = jp;
1432 if (pp < 1.0) e[
"persistProb"] = pp;
1433 if (sp > 0.0) e[
"searchProb"] = sp;
1435 rj[
sn.classes[r].name] = e;
1437 if (!rj.empty()) nj[
"retrial"] = rj;
1441 json pat, orb, brp, blk;
1442 for (std::size_t r = 0; r < K; ++r) {
1445 e[
"distribution"] = detail::dist_to_json(st.
patience[r]);
1448 e[
"impatienceType"] = detail::impatience_to_json(st.
impatience[r]);
1449 pat[
sn.classes[r].name] = e;
1459 e[
"strategy"] = detail::balking_to_json(st.
balking[r].strategy);
1460 json ths = json::array();
1464 tj[
"minJobs"] = th.min_jobs;
1465 tj[
"maxJobs"] = th.max_jobs;
1469 e[
"thresholds"] = ths;
1470 blk[
sn.classes[r].name] = e;
1473 if (!pat.empty()) nj[
"patience"] = pat;
1474 if (!orb.empty()) nj[
"orbitImpatience"] = orb;
1475 if (!brp.empty()) nj[
"batchRejectProb"] = brp;
1476 if (!blk.
empty()) nj[
"balking"] = blk;
1479 json sts = json::array();
1482 tj[
"name"] = t.
name;
1483 tj[
"count"] = t.
count;
1485 json cn = json::array();
1486 for (std::size_t r = 0; r < K && r < t.
compatible.size(); ++r)
1488 tj[
"compatibleClasses"] = cn;
1491 for (std::size_t r = 0; r < K && r < t.
service.size(); ++r)
1493 sv[
sn.classes[r].name] = detail::dist_to_json(t.
service[r]);
1494 if (!sv.empty()) tj[
"service"] = sv;
1497 nj[
"serverTypes"] = sts;
1499 nj[
"heteroSchedPolicy"] = detail::hetero_to_json(st.
hetero_policy);
1506 if (!par.empty()) nj[
"serverParallelism"] = par;
1510 for (std::size_t r = 0; r < K && r < st.
arrival_batch.size(); ++r)
1513 if (!ab.empty()) nj[
"arrivalBatch"] = ab;
1515 json
mc = json::array();
1517 nj[
"markedClasses"] =
mc;
1522 dd[
sn.classes[r].name] =
"FIFO";
1523 if (!dd.empty()) nj[
"departureDiscipline"] = dd;
1528 typename std::map<std::size_t, qn::TransitionParam<T> >::const_iterator tp =
1529 sn.transparam.find(ind);
1530 if (tp !=
sn.transparam.end()) {
1531 json modes = json::array();
1532 for (std::size_t m = 0; m < tp->second.nmodes; ++m) {
1534 mj[
"name"] = tp->second.modenames[m];
1535 mj[
"timingStrategy"] =
1538 if (!tp->second.firingproc[m].disabled)
1539 mj[
"distribution"] = detail::dist_to_json(tp->second.firingproc[m]);
1540 mj[
"numServers"] = tp->second.nmodeservers[m];
1541 mj[
"firingPriority"] = tp->second.firingprio[m];
1543 const char* kArcKey[3] = {
"enablingConditions",
"inhibitingConditions",
1545 for (
int which = 0; which < 3; ++which) {
1546 const Matrix<T>& row = which == 0 ? tp->second.enabling[m]
1547 : which == 1 ? tp->second.inhibiting[m]
1548 : tp->second.firing[m];
1549 json arcs = json::array();
1550 for (std::size_t q = 0; q < row.rows(); ++q)
1551 for (std::size_t r = 0; r < row.cols(); ++r) {
1556 if (which == 1 ? !std::isfinite(v) : v == 0.0)
continue;
1558 a[
"node"] =
sn.nodes[q].name;
1565 a[
"class"] =
sn.classes[r].name;
1569 if (!arcs.empty()) mj[kArcKey[which]] = arcs;
1580 if (m < tp->second.firingdep.size() && tp->second.firingdep[m]) {
1581 const Matrix<T>& enab = tp->second.enabling[m];
1582 json slots = json::array();
1583 std::vector<std::size_t> slot_pos;
1584 std::vector<long> caps;
1589 const std::size_t KW =
sn.classes.size();
1590 const std::vector<double> popw =
sn.njobs();
1591 for (std::size_t q = 0; q < enab.
rows(); ++q) {
1592 bool is_input =
false;
1593 for (std::size_t rq = 0; rq < enab.
cols(); ++rq)
1595 if (!is_input)
continue;
1596 const std::size_t sq =
sn.nodes[q].station;
1597 const double pc = sq == 0 ? std::numeric_limits<double>::infinity()
1598 :
sn.stations[sq - 1].cap;
1599 for (std::size_t rq = 0; rq < KW; ++rq) {
1601 sm[
"node"] =
sn.nodes[q].name;
1602 sm[
"class"] =
sn.classes[rq].name;
1603 slots.push_back(sm);
1604 slot_pos.push_back(q * KW + rq);
1605 const double cc = rq < popw.size() && std::isfinite(popw[rq]) ? popw[rq] : 10.0;
1606 caps.push_back(std::lround(std::min(pc, cc)));
1609 if (!slots.empty()) {
1611 frm[
"slots"] = slots;
1612 frm[
"cutoffs"] = caps;
1614 std::size_t total = 1;
1615 for (std::size_t s = 0; s < caps.size(); ++s)
1616 total *=
static_cast<std::size_t
>(caps[s]) + 1;
1618 for (std::size_t li = 0; li < total; ++li) {
1619 std::size_t rem = li;
1621 for (std::size_t s = 0; s < caps.size(); ++s) {
1622 const std::size_t shp =
static_cast<std::size_t
>(caps[s]) + 1;
1623 const std::size_t c = rem % shp;
1626 static_cast<long>(c));
1628 key += std::to_string(c);
1632 scaling[key] = std::isfinite(v) ? v : 0.0;
1634 for (std::size_t s = 0; s < slot_pos.size(); ++s)
1636 frm[
"scaling"] = scaling;
1637 mj[
"firingRateDependence"] = frm;
1643 json lst = json::array();
1646 e[
"node"] =
sn.nodes[a.node - 1].name;
1647 e[
"rate"] = a.value;
1650 mj[
"fluidRates"] = lst;
1653 json lst = json::array();
1656 e[
"node"] =
sn.nodes[g.node - 1].name;
1657 e[
"op"] = g.sgn > 0 ?
">=" :
"<=";
1658 e[
"threshold"] = g.thr;
1661 mj[
"fluidGuards"] = lst;
1664 json lst = json::array();
1667 e[
"node"] =
sn.nodes[a.node - 1].name;
1668 e[
"delta"] = a.value;
1671 mj[
"fluidImpulses"] = lst;
1676 json lst = json::array();
1678 const std::size_t sq =
sn.nodes[d.node - 1].station;
1681 if (!std::isfinite(L)) {
1683 for (
const auto& kv :
sn.transparam)
1684 for (std::size_t mm = 0; mm < kv.second.nmodes; ++mm)
1685 for (
const auto& g : kv.second.fluid_guards(mm))
1686 if (g.node == d.node) L = std::max(L, std::fabs(g.thr));
1687 L = std::max(10.0, 10.0 * L);
1689 const std::size_t npts = 201;
1690 json levels = json::array(), values = json::array();
1691 for (std::size_t i = 0; i < npts; ++i) {
1692 const double lv = L *
static_cast<double>(i) /
static_cast<double>(npts - 1);
1693 levels.push_back(lv);
1694 values.push_back(d.fun(lv));
1697 e[
"node"] =
sn.nodes[d.node - 1].name;
1698 e[
"levels"] = levels;
1699 e[
"values"] = values;
1702 mj[
"fluidRateDependence"] = lst;
1709 const std::vector<double> pop =
sn.njobs();
1710 const std::size_t KF =
sn.classes.size();
1711 json lst = json::array();
1713 json slots = json::array(), readn = json::array();
1714 std::vector<std::size_t> sflat;
1715 std::vector<long> caps;
1716 for (std::size_t q : fd.read) {
1717 readn.push_back(
sn.nodes[q - 1].name);
1718 const std::size_t sq =
sn.nodes[q - 1].station;
1719 const double pc = sq == 0 ? std::numeric_limits<double>::infinity()
1720 :
sn.stations[sq - 1].cap;
1721 for (std::size_t r = 0; r < KF; ++r) {
1723 sm[
"node"] =
sn.nodes[q - 1].name;
1724 sm[
"class"] =
sn.classes[r].name;
1725 slots.push_back(sm);
1726 sflat.push_back((q - 1) * KF + r);
1727 const double cc = r < pop.size() && std::isfinite(pop[r]) ? pop[r] : 10.0;
1728 caps.push_back(std::lround(std::min(pc, cc)));
1731 std::size_t total = 1;
1732 for (
long c : caps) total *=
static_cast<std::size_t
>(c) + 1;
1733 std::vector<double> mk(
sn.nodes.size() * KF, 0.0);
1734 json scaling = json::object();
1735 for (std::size_t li = 0; li < total; ++li) {
1736 std::size_t rem = li;
1738 for (std::size_t s = 0; s < caps.size(); ++s) {
1739 const std::size_t shp =
static_cast<std::size_t
>(caps[s]) + 1;
1740 const std::size_t c = rem % shp;
1742 mk[sflat[s]] =
static_cast<double>(c);
1744 key += std::to_string(c);
1746 const double v = fd.fun(mk);
1747 scaling[key] = std::isfinite(v) ? v : 0.0;
1750 e[
"node"] =
sn.nodes[fd.node - 1].name;
1753 e[
"cutoffs"] = caps;
1754 e[
"scaling"] = scaling;
1757 mj[
"fluidFlowDependence"] = lst;
1759 modes.push_back(mj);
1761 nj[
"modes"] = modes;
1764 nodes.push_back(nj);
1766 model[
"nodes"] = nodes;
1770 for (
const auto& kv :
sn.P) {
1771 const std::size_t r = kv.first.first, s = kv.first.second;
1773 for (std::size_t a = 0; a < kv.second.rows(); ++a) {
1775 for (std::size_t b = 0; b < kv.second.cols(); ++b) {
1777 if (p != 0.0) row[
sn.nodes[b].name] = p;
1779 if (!row.empty()) from_to[
sn.nodes[a].name] = row;
1781 if (!from_to.empty())
1782 matrix[
sn.classes[r - 1].name +
"," +
sn.classes[s - 1].name] = from_to;
1785 routing[
"type"] =
"matrix";
1786 routing[
"matrix"] = matrix;
1787 model[
"routing"] = routing;
1789 json strategies, weights, params;
1790 for (std::size_t i = 0; i <
sn.nodes.size(); ++i) {
1792 json per_class, per_class_w, per_class_p;
1793 for (std::size_t r = 0; r < K && r < nd.
routing.size(); ++r) {
1796 per_class[
sn.classes[r].name] = detail::routing_to_json(nd.
routing[r]);
1799 for (
const auto& kv : nd.
routing_weights[r]) dw[
sn.nodes[kv.first - 1].name] = kv.second;
1800 per_class_w[
sn.classes[r].name] = dw;
1805 per_class_p[
sn.classes[r].name] = pj;
1808 if (!per_class.empty()) strategies[nd.
name] = per_class;
1809 if (!per_class_w.empty()) weights[nd.
name] = per_class_w;
1810 if (!per_class_p.empty()) params[nd.
name] = per_class_p;
1812 if (!strategies.empty()) model[
"routingStrategies"] = strategies;
1813 if (!weights.empty()) model[
"routingWeights"] = weights;
1814 if (!params.empty()) model[
"routingParams"] = params;
1821 if (!
sn.sdr_nodes.empty()) {
1823 std::size_t cls = 0;
1824 for (std::size_t r = 0; r < K && cls == 0; ++r)
1825 if (
sn.nodes[sd.
entry].routing.size() > r &&
1829 throw InputError(
"network_writer: the model declares state-dependent routing at node '" +
1830 sn.nodes[sd.
entry].name +
"' but no class routes SDR there");
1832 sdr[
"entry"] =
sn.nodes[sd.
entry].name;
1834 sdr[
"class"] =
sn.classes[cls - 1].name;
1835 json branches = json::array();
1836 for (std::size_t b = 0; b < sd.
branch.size(); ++b) {
1837 json bn = json::array();
1838 for (std::size_t q = 0; q < sd.
branch[b].size(); ++q)
1839 bn.push_back(
sn.nodes[sd.
branch[b][q]].name);
1840 branches.push_back(bn);
1842 sdr[
"branches"] = branches;
1843 json level = json::array(), Cs = json::array(), dm = json::array();
1844 for (std::size_t b = 0; b < sd.
level.size(); ++b)
1845 level.push_back(
static_cast<double>(sd.
level[b]));
1846 for (std::size_t t = 0; t < sd.
C.size(); ++t) Cs.push_back(sd.
C[t]);
1847 for (std::size_t t = 0; t < sd.
d.
rows(); ++t) {
1848 json row = json::array();
1849 for (std::size_t b = 0; b < sd.
d.
cols(); ++b) row.push_back(sd.
d(t, b));
1852 sdr[
"level"] = level;
1855 model[
"stateDepRouting"] = sdr;
1859 if (!
sn.regions.empty()) {
1860 json fcr = json::array();
1861 for (std::size_t g = 0; g <
sn.regions.size(); ++g) {
1862 const typename SN::Region& rg =
sn.regions[g];
1864 rj[
"name"] = rg.name.empty() ?
"Region" + std::to_string(g + 1) : rg.name;
1865 json stations = json::array();
1866 double global = -1.0, globalmem = -1.0;
1867 json class_cap, class_size, class_weight;
1868 for (std::size_t m = 0; m < rg.members.size(); ++m) {
1869 if (!rg.members[m])
continue;
1871 sj[
"node"] =
sn.nodes[
sn.station_to_node[m] - 1].name;
1872 stations.push_back(sj);
1873 global = rg.cap[m][K];
1879 if (m < rg.maxmem.size()) globalmem = rg.maxmem[m];
1880 for (std::size_t r = 0; r < K; ++r)
1881 if (rg.cap[m][r] != -1.0) class_cap[
sn.classes[r].name] = rg.cap[m][r];
1883 rj[
"stations"] = stations;
1884 if (global != -1.0) rj[
"globalMaxJobs"] = global;
1885 if (globalmem != -1.0) rj[
"globalMaxMemory"] = globalmem;
1886 if (!class_cap.empty()) rj[
"classMaxJobs"] = class_cap;
1888 for (std::size_t r = 0; r < K && r < rg.rule.size(); ++r)
1889 rule[
sn.classes[r].name] = detail::drop_to_json(rg.rule[r]);
1890 rj[
"dropRule"] = rule;
1891 for (std::size_t r = 0; r < K; ++r) {
1900 if (!class_size.empty() || !class_weight.empty())
1901 for (json& sj : rj[
"stations"]) {
1902 if (!class_size.empty()) sj[
"classSize"] = class_size;
1903 if (!class_weight.empty()) sj[
"classWeight"] = class_weight;
1905 if (rg.lincon_A.rows() > 0) {
1906 rj[
"constraintA"] = detail::mat_to_json(rg.lincon_A);
1907 rj[
"constraintB"] = detail::vec_to_json(rg.lincon_b);
1911 model[
"finiteCapacityRegions"] = fcr;
1922 if (
static_cast<bool>(
sn.gdscaling)) {
1923 const std::size_t M =
sn.nstations, K =
sn.nclasses;
1924 const std::vector<double> njobs =
sn.njobs();
1925 const int wcut =
sn.gdscalingcutoff;
1926 std::vector<std::size_t> slot_st, slot_cl;
1927 std::vector<int> cuts;
1928 for (std::size_t i = 0; i < M; ++i) {
1930 for (std::size_t r = 0; r < K; ++r) {
1931 const double cap =
sn.classcap[i][r];
1932 if (!(cap > 0))
continue;
1933 int c = std::isfinite(njobs[r]) ?
static_cast<int>(std::lround(njobs[r])) : wcut;
1934 if (std::isfinite(cap)) c = std::min(c,
static_cast<int>(std::lround(cap)));
1935 slot_st.push_back(i);
1936 slot_cl.push_back(r);
1937 cuts.push_back(c > 0 ? c : 0);
1940 const std::size_t P = cuts.size();
1941 std::size_t total = 1;
1942 for (std::size_t d = 0; d < P; ++d) {
1943 total *=
static_cast<std::size_t
>(cuts[d] + 1);
1944 if (total > 200000u)
1946 "the global dependence lattice exceeds the wire limit of 200000 points; lower "
1947 "the wireCutoff argument of set_global_dependence, or solve the model "
1951 json blk, slots, tbl;
1952 blk[
"type"] =
"globalDependent";
1953 std::vector<std::string> station_names(M), class_names(K);
1954 for (std::size_t i = 0; i < M; ++i)
1955 station_names[i] =
sn.nodes[
sn.station_to_node[i] - 1].name;
1956 for (std::size_t r = 0; r < K; ++r) class_names[r] =
sn.classes[r].name;
1957 blk[
"stations"] = station_names;
1958 blk[
"classes"] = class_names;
1959 slots = json::array();
1960 for (std::size_t d = 0; d < P; ++d) {
1962 sm[
"station"] = station_names[slot_st[d]];
1963 sm[
"class"] = class_names[slot_cl[d]];
1964 slots.push_back(sm);
1966 blk[
"slots"] = slots;
1967 blk[
"cutoffs"] = cuts;
1968 blk[
"cutoff"] = wcut;
1970 for (std::size_t li = 0; li < total; ++li) {
1971 std::size_t rem = li;
1972 std::vector<int> cnt(P, 0);
1973 for (std::size_t d = 0; d < P; ++d) {
1974 cnt[d] =
static_cast<int>(rem %
static_cast<std::size_t
>(cuts[d] + 1));
1975 rem /=
static_cast<std::size_t
>(cuts[d] + 1);
1978 for (std::size_t d = 0; d < P; ++d)
1980 const std::vector<T> v =
sn.gdscaling(n);
1981 std::vector<double> flat(M * K, 1.0);
1982 for (std::size_t i = 0; i < M; ++i)
1983 for (std::size_t r = 0; r < K; ++r) {
1988 flat[i * K + r] = std::isfinite(x) ? x : 0.0;
1991 if (P == 0) key =
"0";
1993 for (std::size_t d = 0; d < P; ++d) {
1995 key += std::to_string(cnt[d]);
1999 blk[
"scaling"] = tbl;
2000 std::vector<double> pk(M * K, 1.0);
2001 for (std::size_t j = 0; j < pk.size() && j <
sn.gdscalingpeak.size(); ++j)
2004 model[
"globalDependence"] = blk;
2019 std::map<std::string, json> by_name;
2020 for (
const typename SN::Reward& rw :
sn.reward) {
2021 if (rw.kind.empty() || rw.node == 0)
continue;
2023 rj[
"name"] = rw.name;
2024 rj[
"type"] = rw.kind;
2025 rj[
"node"] =
sn.nodes[rw.node - 1].name;
2026 if (rw.cls != 0) rj[
"class"] =
sn.classes[rw.cls - 1].name;
2027 by_name[rw.name] = rj;
2029 json rewards = json::array();
2030 for (
const std::pair<const std::string, json>& kv : by_name) rewards.push_back(kv.second);
2031 if (!rewards.empty()) model[
"rewards"] = rewards;
2040 root[
"format"] =
"line-model";
2041 root[
"version"] =
"1.0";
2043 detail::wire_nonfinite(root);
2060 const std::size_t K =
sn.classes.size();
2062 it =
sn.pasparam.begin();
2063 it !=
sn.pasparam.end(); ++it) {
2064 if (!
static_cast<bool>(it->second.svc_rate_fun))
continue;
2065 const std::size_t ist = it->first;
2066 if (ist < 1 || ist >
sn.stations.size())
continue;
2067 const double cap =
sn.stations[ist - 1].cap;
2068 if (std::isfinite(cap))
continue;
2069 const std::string& name =
sn.stations[ist - 1].name;
2072 std::string tau =
"[";
2073 for (std::size_t r = 0; r < s.
tau.size(); ++r)
2074 tau += (r ?
", " :
"") + std::to_string(s.
tau[r]);
2077 "' has no finite buffer: LDES state-space generation requires one. "
2078 "Its order-independent service rate saturates at class counts " +
2080 ", so call setCap(N) with N comfortably above the mean occupancy.");
2083 "' has an unbounded order-independent service rate (mu does not "
2084 "saturate) and no finite buffer; call setCap(N) on the station.");
2091 std::ofstream out(path.c_str());
2092 if (!out)
throw InputError(
"network_writer: cannot open " + path +
" for writing");
UnsupportedError(const std::string &what)
A network plus its refreshed NetworkStruct.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
detail::json network_to_json(const qn::NetworkStruct< T > &sn)
qn::NetworkStruct -> the model.json model object.
void require_finite_pas_buffers(const qn::NetworkStruct< T > &sn)
Refuse an OI/PAS station with no finite buffer before it reaches LDES.
PasSaturation saturation_cutoffs(const std::function< std::vector< double >(const std::vector< int > &)> &f_at, std::size_t K, double cmax=std::numeric_limits< double >::infinity(), int max_cutoff=256, double tol=1e-9)
Per-class saturation cutoffs of a count-indexed function f(n), the search behind the OI/PAS and class...
detail::json network_json_envelope(const qn::NetworkStruct< T > &sn)
The complete model.json envelope: {format, version, model}.
void write_network_json(const qn::NetworkStruct< T > &sn, const std::string &path)
Write a model.json file, indented as the reference writers indent it.
PasSaturation pas_saturation_cutoffs(const std::function< T(const std::vector< std::size_t > &)> &mu, std::size_t K, double cmax=std::numeric_limits< double >::infinity())
The saturation cutoffs of an order-independent rate mu(c), searched over the states a buffer of cmax ...
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
DropStrategy
Blocking and loss rules, with the values of MATLAB DropStrategy.
@ IMMEDIATE
fires with zero delay, resolved by weight and priority
BalkingStrategy
Balking rules, with the values of MATLAB BalkingStrategy.
SignalType
G-network signal classes, with the values of MATLAB SignalType.
@ REPLY
completes a synchronous call, releasing a held server
@ CATASTROPHE
removes EVERY job at the station
PollingOrder
Visit-order rules of a polling server, with the values of MATLAB PollingOrder.
@ THRESHOLD
a priority buffer above its threshold, else SKIPEMPTY
@ SKIPEMPTY
next non-empty buffer in cyclic order
@ CYCLIC
fixed cyclic order (or the polling table)
@ LQF
longest buffer first, ties to the cyclic successor
RoutingStrategy
Routing strategies, with the values of MATLAB RoutingStrategy.
@ SDR
Krzesinski (1987) product-form state-dependent routing.
@ GLOBAL
a timed token: per-class target rotation timers
RemovalPolicy
Which job a negative signal removes, with the values of MATLAB RemovalPolicy.
@ FCFS
the oldest waiting job; servers only once nobody waits
@ RANDOM
uniform over waiting AND in-service jobs
HeteroSchedPolicy
How a heterogeneous station picks among its server types, MATLAB HeteroSchedPolicy.
PollingType
Polling service disciplines, with the values of MATLAB PollingType.
@ KLIMITED
serve at most K per visit (K in pollingPar)
@ EXHAUSTIVE
serve until the queue empties
@ GATED
serve exactly the jobs present at the polling instant
@ GLIMITED
serve at most K of the jobs present at the polling instant
@ DECREMENTING
serve until the queue is one shorter than at arrival
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
const char * sched_to_text(SchedStrategy s)
const char * process_to_text(ProcessType p)
The MATLAB ProcessType name, as sn.procid prints it.
std::function< std::vector< T >(const std::vector< T > &)> CdScaling
A class-dependent scaling map, sn.cdscaling.
NodeType
Node kinds, with the values of MATLAB NodeType.
ReplacementStrategy
Cache replacement policies, with the values of MATLAB ReplacementStrategy.
@ HLRU
h-LRU / LRU(m): h lists, promote i -> i+1 on a hit
@ CLIMB
move up one position on a hit (transposition rule)
@ QLRU
q-LRU: LRU with probabilistic admission on a miss
@ FIFO
first in, first out
ImpatienceType
Impatience kinds, with the values of MATLAB ImpatienceType.
double tot(const std::vector< double > &pops)
void put(Matrix< double > &A, std::size_t r0, std::size_t c0, const Matrix< double > &S)
A(r0:, c0:) = S.
Conservation laws of a layered queueing network, enumerated from its structure.
Reader for the LINE model.json interchange (a Network model) into a qn::Network<T> built through the ...
A queueing network and its refreshed NetworkStruct.
Number-type abstraction for the templated API port.
The per-class saturation cutoffs of a count-indexed rate table.
bool bounded
false when some tau(r) passed the search limit or reached cmax
std::vector< int > tau
tau(r): beyond it the function no longer changes with class-r jobs
static Distrib exp_rate(const T &r)
std::vector< T > params
Constructor arguments, in MATLAB getParam order.
std::vector< T > trace
Replayer / Trace samples; empty for every other type.
Topology and coefficients of a state-dependent routing subnetwork.
std::vector< std::size_t > level
level[b] is the unique t with B_b in V_t - V_{t+1}; level[0] is unused.
Matrix< double > d
Coefficients d_tb of eq.
std::vector< double > C
Coefficients C_t of eq.
std::size_t departure
Departure centre d of Q(V,V); may equal entry.
std::vector< std::vector< std::size_t > > branch
branch[b] holds the centres of branch b, b >= 1; branch[0] is unused.
std::size_t entry
Entry centre e of Q(V,V).
The parameters of a Cache node, MATLAB's sn.nodeparam{ind} for a Cache.
int retrieval_capacity
Delayed-hit retrieval system (Cache.setRetrievalSystem).
T qlru
q-LRU admission probability; 1 admits every miss (plain LRU).
std::vector< T > initstate
The DECLARED initial contents of the cache, as the reference dumps the node's state row: the per-clas...
std::vector< int > itemsize
Per-item storage cost (size) and per-list cap on the total cost of the resident items (ton21cache Sec...
std::vector< int > costcap
std::map< std::size_t, std::vector< std::size_t > > retrieval_queues
read class(0-based)->nodes
std::vector< std::vector< Matrix< T > > > accost
(u) x (n) of (h+1)x(h+1), or empty
std::vector< std::vector< std::size_t > > retrieval_classes
(nitems x nclasses), 1-based
std::vector< int > itemcap
std::vector< std::size_t > missclass
std::vector< std::size_t > hitclass
lang::ReplacementStrategy replacestrat
std::vector< std::size_t > classitem
Item read by each per-item class of a cache network (MATLAB Cache.setItemReadClasses,...
std::vector< std::vector< T > > pread
(u) x (n), empty row = NaN
Variable forking levels, the twin of MATLAB sn.nodeparam{f}.fanOutLink / .fanOutProb / ....
std::vector< std::vector< lang::Distrib< T > > > fan_out_dist
One job class of the network.
std::size_t refstat
1-based reference station
double deadline
sn.classdeadline(r): the soft deadline EDD and EDF order by, and the tardiness JMT reports.
double population
infinite for an open class
bool immfeed
Class-level immediate feedback, ORed with the station's own setting into sn.immfeed.
std::size_t spawn
sn.classspawn(r): the 1-based class injected at the SAME station on every completion of this class,...
bool self_looping
A SelfLoopingClass: a closed class that perpetually cycles at its reference station.
bool is_ref_class
marks the chain's reference class
The pass-and-swap / order-independent parameters of a PAS station, keyed by station index (Dorsman an...
The polling controller of a POLLING station, keyed by station index.
std::size_t pk
the K of K-LIMITED
A Logger node's trace configuration, MATLAB's Logger properties and sn.nodeparam{ind}...
std::string file_name
base name, no directory
std::string file_path
directory, MATLAB's model.getLogPath
std::vector< int > routing_param
The scalar parameter of a parameterized dispatcher, per class: the d of a power-of-d (SQ) choice.
std::vector< std::map< std::size_t, double > > routing_weights
The per-destination weights of a WRROBIN dispatcher, per class: a map from 1-based destination NODE i...
bool queue_object
Declared as a Queue although its INF discipline makes nodetype Delay (Network::add_queue).
std::vector< RoutingStrategy > routing
sn.routing, per class.
double tasks_per_link
Fork.output.tasksPerLink == MATLAB sn.nodeparam{f}.fanOut: how many tasks a fork emits per outgoing l...
One balking threshold: with min_jobs <= n <= max_jobs at the station, an arriving job of the class re...
A heterogeneous server pool: count servers that serve only compatible classes, each with its own serv...
std::vector< bool > compatible
per class; empty = every class
std::vector< Distrib< T > > service
per class
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...
bool polling_timer_preempt
double cap
Station capacity in Kendall's K, as setCapacity sets it.
std::vector< T > jdscalingpeak
sn.jdscalingpeak for this station: the declared peak joint-dependent scaling per class.
std::vector< BalkingParam > balking
std::vector< T > batch_reject
Queue.setBatchRejectProbability: per-class rejection of a whole batch.
std::vector< Distrib< T > > patience
Queue.setPatience(class, dist): the abandonment timer of a WAITING job, with impatience[r] naming whi...
std::vector< std::size_t > server_parallelism
Queue.setServerParallelism(class, n): the servers a job seizes for the whole of its service,...
std::vector< Distrib< T > > quantum
Queue.setQuantum(class, distrib): the per-class time slice of a round-robin (SchedStrategy::RR) stati...
std::vector< int > droprule
Per-class blocking rule as an INT, with 0 meaning "not set".
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
double nservers
may be infinite (a Delay, or an inf-scheduled task)
std::size_t polling_order_class
std::vector< lang::PollingType > polling_type
Polling parameters for a POLLING station, MATLAB's pollingType, switchoverTime and pollingPar on the ...
T orbit_join_of(std::size_t r) const
orbit_join[r] with the default 1 for a missing slot (0-based r).
CdScaling< T > jdscaling
sn.jdscaling for this station: MATLAB's Station.ljdScaling, the JOINT dependence map eta_i(n),...
std::vector< T > cdscalingpeak
sn.cdscalingpeak for this station: the DECLARED peak rate scaling per class, empty when the station i...
std::vector< std::size_t > marked_classes
Source.markedClasses: the 1-based class of each mark of an MMAP arrival.
std::vector< T > lldscaling
sn.lldscaling for this station: the multiplier at population 1, 2, ... Empty when the station is not ...
std::vector< lang::ImpatienceType > impatience
std::vector< T > schedparam
sn.schedparam, per class: the DPS / GPS weight, or the SEPT / LEPT rank.
lang::PollingOrder polling_order
std::vector< std::size_t > polling_table
The polling extensions (Queue.setPollingTable, setPollingOrder, setPollingTimer).
lang::HeteroSchedPolicy hetero_policy
CdScaling< T > cdscaling
sn.cdscaling for this station: the class-dependence map, empty when unset.
std::vector< double > classcap
Per-class buffer from setChainCapacity; infinite where unset.
std::vector< lang::DepartureDiscipline > departure_discipline
Place.departureDiscipline, per class.
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,...
lang::PollingTimer polling_timer_scope
T orbit_search_of(std::size_t r) const
orbit_search[r] with the default 0 for a missing slot (0-based r).
std::vector< bool > immfeed
Node-level immediate feedback, per class; empty when the station sets none.
std::vector< ServerType > server_types
long polling_order_threshold
T orbit_persist_of(std::size_t r) const
orbit_persist[r] with the default 1 for a missing slot (0-based r).
The parameters of a Transition node: MATLAB sn.nodeparam{ind} for an SPN transition,...
const std::vector< FluidArc > & fluid_rates(std::size_t m) const
const std::vector< FluidArc > & fluid_impulses(std::size_t m) const
const std::vector< FluidDep > & fluid_deps(std::size_t m) const
const std::vector< FluidFlowDep > & fluid_flow_deps(std::size_t m) const
const std::vector< FluidGuard > & fluid_guards(std::size_t m) const