LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
network_writer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_IO_NETWORK_WRITER_H
6#define LINE_IO_NETWORK_WRITER_H
7
8/**
9 * @file
10 * @ingroup line_io
11 * `qn::NetworkStruct` -> model.json, the inverse of network_reader.h.
12 *
13 * The wire format is the one `linemodel_save.m`, the Python `save_model` and
14 * the JAR `LineModelIO` write, so a model saved here loads in any of the four
15 * codebases and a model saved by any of them and read back here is unchanged.
16 *
17 * WHAT IS AND IS NOT ROUND-TRIPPABLE. Two things on a model are FUNCTIONS, and
18 * a function cannot cross JSON:
19 *
20 * the rate-scaling handles beta_r(n), eta_i(n) and the OI mu(c)
21 * a reward built from a lambda
22 *
23 * The reference solves the first by MATERIALIZING the handle over the per-class
24 * box lattice, and this writer does the same, with the same cutoffs (a closed
25 * class's population, or 10 for an open one) and the same comma-joined key, so
26 * the table is byte-comparable with MATLAB's. The second it solves by writing
27 * only the DECLARATIVE rewards and warning about the rest; here a reward with
28 * no `kind` is omitted for the same reason -- emitting a guess would produce a
29 * model.json that reloads into a different reward.
30 *
31 * EVERY OTHER KEY IS EXACT. The writer emits exactly the keys the reader
32 * consumes, which is what makes the round trip a test rather than a hope: a
33 * field added to one side without the other shows up as a read-back mismatch
34 * in `test_network_roundtrip.cpp`.
35 */
36
37#include <cctype>
38#include <cmath>
39#include <fstream>
40#include <limits>
41#include <map>
42#include <string>
43#include <vector>
44
49#include "line/num/number.h"
50#include "line/util/error.h"
51
52namespace line {
53namespace io {
54
55namespace detail {
56
57/** The model.json `type` of a node kind. */
58inline const char* node_type_to_json(lang::NodeType t) {
59 switch (t) {
60 case lang::NodeType::Queue: return "Queue";
61 case lang::NodeType::Source: return "Source";
62 case lang::NodeType::Delay: return "Delay";
63 case lang::NodeType::ClassSwitch: return "ClassSwitch";
64 case lang::NodeType::Logger: return "Logger";
65 case lang::NodeType::Cache: return "Cache";
66 case lang::NodeType::Router: return "Router";
67 case lang::NodeType::Fork: return "Fork";
68 case lang::NodeType::Place: return "Place";
69 case lang::NodeType::Transition: return "Transition";
70 case lang::NodeType::Region: return "Region";
71 case lang::NodeType::Join: return "Join";
72 case lang::NodeType::Sink: return "Sink";
73 }
74 throw UnsupportedError("network_writer: unnamed node type");
75}
76
77inline const char* drop_to_json(lang::DropStrategy d) {
78 switch (d) {
79 case lang::DropStrategy::WAITQ: return "waitingQueue";
80 case lang::DropStrategy::DROP: return "drop";
81 case lang::DropStrategy::BAS: return "blockingAfterService";
82 case lang::DropStrategy::BBS: return "blockingBeforeService";
83 case lang::DropStrategy::RSRD: return "resamplingRepetitiveService";
84 case lang::DropStrategy::RETRIAL: return "retrial";
85 case lang::DropStrategy::RETRIAL_WITH_LIMIT: return "retrialWithLimit";
86 }
87 throw UnsupportedError("network_writer: unnamed drop strategy");
88}
89
90inline const char* replacement_to_json(lang::ReplacementStrategy r) {
91 switch (r) {
92 case lang::ReplacementStrategy::RR: return "RR";
93 case lang::ReplacementStrategy::FIFO: return "FIFO";
94 case lang::ReplacementStrategy::SFIFO: return "SFIFO";
95 case lang::ReplacementStrategy::LRU: return "LRU";
96 case lang::ReplacementStrategy::HLRU: return "HLRU";
97 case lang::ReplacementStrategy::CLIMB: return "CLIMB";
98 case lang::ReplacementStrategy::QLRU: return "QLRU";
99 }
100 throw UnsupportedError("network_writer: unnamed replacement strategy");
101}
102
103inline const char* polling_to_json(lang::PollingType p) {
104 switch (p) {
105 case lang::PollingType::EXHAUSTIVE: return "EXHAUSTIVE";
106 case lang::PollingType::GATED: return "GATED";
107 case lang::PollingType::KLIMITED: return "KLIMITED";
108 case lang::PollingType::DECREMENTING: return "DECREMENTING";
109 }
110 throw UnsupportedError("network_writer: unnamed polling type");
111}
112
113inline const char* impatience_to_json(lang::ImpatienceType i) {
114 switch (i) {
115 case lang::ImpatienceType::RENEGING: return "RENEGING";
116 case lang::ImpatienceType::BALKING: return "BALKING";
117 case lang::ImpatienceType::RETRIAL: return "RETRIAL";
118 case lang::ImpatienceType::NONE: break;
119 }
120 throw UnsupportedError("network_writer: a patience block with no impatience type");
121}
122
123inline const char* balking_to_json(lang::BalkingStrategy b) {
124 switch (b) {
125 case lang::BalkingStrategy::QUEUE_LENGTH: return "QUEUE_LENGTH";
126 case lang::BalkingStrategy::EXPECTED_WAIT: return "EXPECTED_WAIT";
127 case lang::BalkingStrategy::COMBINED: return "COMBINED";
128 case lang::BalkingStrategy::NONE: break;
129 }
130 throw UnsupportedError("network_writer: a balking block with no strategy");
131}
132
133/**
134 * The `routingStrategies` name of a dispatcher.
135 *
136 * NOT `lang::routing_to_text`, which is the lower-case name `sn.routing` prints
137 * in a struct dump. The wire carries the enum CONSTANT, upper case, matching
138 * the JAR's `RoutingStrategy` names one for one -- and the readers compare it
139 * exactly, so `rrobin` reloads as an unknown strategy rather than as RROBIN.
140 */
141inline const char* routing_to_json(lang::RoutingStrategy r) {
142 switch (r) {
143 case lang::RoutingStrategy::RAND: return "RAND";
144 case lang::RoutingStrategy::PROB: return "PROB";
145 case lang::RoutingStrategy::RROBIN: return "RROBIN";
146 case lang::RoutingStrategy::WRROBIN: return "WRROBIN";
147 case lang::RoutingStrategy::JSQ: return "JSQ";
148 case lang::RoutingStrategy::FIRING: return "FIRING";
149 case lang::RoutingStrategy::SQ: return "SQ";
150 case lang::RoutingStrategy::SDR: return "SDR";
151 case lang::RoutingStrategy::DISABLED: return "DISABLED";
152 }
153 throw UnsupportedError("network_writer: unnamed routing strategy");
154}
155
156inline const char* hetero_to_json(lang::HeteroSchedPolicy h) {
157 switch (h) {
158 case lang::HeteroSchedPolicy::ORDER: return "ORDER";
159 case lang::HeteroSchedPolicy::ALIS: return "ALIS";
160 case lang::HeteroSchedPolicy::ALFS: return "ALFS";
161 case lang::HeteroSchedPolicy::FAIRNESS: return "FAIRNESS";
162 case lang::HeteroSchedPolicy::FSF: return "FSF";
163 case lang::HeteroSchedPolicy::RAIS: return "RAIS";
164 }
165 throw UnsupportedError("network_writer: unnamed heterogeneous scheduling policy");
166}
167
168/**
169 * The `scheduling` name of a discipline.
170 *
171 * UPPER CASE, which is not what `lang::sched_to_text` returns: that is the
172 * lower-case spelling `sn.sched` prints in a struct dump, while the wire
173 * carries the enum constant (`upper(SchedStrategy.toText(...))` in
174 * `linemodel_save.m`, matching `jline.lang.constant.SchedStrategy`). A
175 * lower-case `fcfs` reloads as an unknown discipline.
176 */
177inline std::string sched_to_json(lang::SchedStrategy s) {
178 std::string out = lang::sched_to_text(s);
179 for (std::size_t i = 0; i < out.size(); ++i)
180 out[i] = static_cast<char>(std::toupper(static_cast<unsigned char>(out[i])));
181 return out;
182}
183
184/** A dense matrix as the array of row arrays the readers expect. */
185template <class T>
186json mat_to_json(const Matrix<T>& M) {
187 json rows = json::array();
188 for (std::size_t i = 0; i < M.rows(); ++i) {
189 json row = json::array();
190 for (std::size_t j = 0; j < M.cols(); ++j)
191 row.push_back(num_traits<T>::to_double(M(i, j)));
192 rows.push_back(row);
193 }
194 return rows;
195}
196
197template <class T>
198json vec_to_json(const std::vector<T>& v) {
199 json a = json::array();
200 for (const T& x : v) a.push_back(num_traits<T>::to_double(x));
201 return a;
202}
203
204/**
205 * Rewrite every non-finite number into the wire's own INFINITY SPELLING.
206 *
207 * JSON has no infinity literal, so `linemodel_save` -- the reference -- writes
208 * an infinite scalar as the STRING "Infinity" / "-Infinity" and a NaN as
209 * `null`, a rule it applies to EVERY numeric field rather than a named few;
210 * `num_from_json` on the reading side decodes exactly that pair. nlohmann
211 * silently dumps an infinity as `null` instead, which the reader then takes for
212 * a NaN, so a state row that carries the sentinel -- an open class holds an
213 * infinite population, so a Source's `initialState` does the moment the model
214 * is initialized -- crossed as a hole rather than as a value. Applied once over
215 * the finished document, as the JAR and Python writers apply theirs, so no
216 * individual field has to remember the rule.
217 */
218inline void wire_nonfinite(json& j) {
219 if (j.is_object() || j.is_array()) {
220 for (json::iterator it = j.begin(); it != j.end(); ++it) wire_nonfinite(*it);
221 return;
222 }
223 if (!j.is_number_float()) return;
224 const double v = j.get<double>();
225 if (std::isnan(v))
226 j = json();
227 else if (std::isinf(v))
228 j = json(v > 0.0 ? "Infinity" : "-Infinity");
229}
230
231/**
232 * `x` of a `DiscreteSampler` whose support is the item index, i.e. 1..n.
233 *
234 * MATLAB `DiscreteSampler(p)` defaults `x = 1:n` and every writer records it;
235 * the JAR and Python readers require the key and fail on a file that omits it,
236 * so a pmf-only document is unreadable outside this port even though the value
237 * is implied.
238 */
239inline json discrete_support(std::size_t n) {
240 json a = json::array();
241 for (std::size_t i = 1; i <= n; ++i) a.push_back(static_cast<double>(i));
242 return a;
243}
244
245/**
246 * A distribution as its model.json record, the inverse of `dist_from_json`.
247 *
248 * The parameter-carrying families are written from `Distrib::params`, which
249 * holds the constructor arguments in MATLAB's getParam order -- that is what
250 * makes the record reload into the SAME object rather than into a phase-type
251 * that shares two moments with it.
252 */
253template <class T>
254json dist_to_json(const lang::Distrib<T>& d) {
255 using lang::ProcessType;
256 json j;
257 j["type"] = lang::process_to_text(d.type);
258 const std::vector<T>& p = d.params;
259 auto need = [&](std::size_t n, const char* who) {
260 if (p.size() < n)
261 throw InputError(std::string("network_writer: a ") + who + " carries " +
262 std::to_string(p.size()) + " parameters, and " + std::to_string(n) +
263 " are needed to write it back");
264 };
265 switch (d.type) {
266 case ProcessType::DISABLED:
267 case ProcessType::IMMEDIATE:
268 return j; // both are bare tags
269 case ProcessType::EXP:
270 need(1, "Exp");
271 j["params"]["lambda"] = num_traits<T>::to_double(p[0]);
272 return j;
273 case ProcessType::DET:
274 j["params"]["value"] = num_traits<T>::to_double(d.mean);
275 return j;
276 case ProcessType::ERLANG:
277 need(2, "Erlang");
278 j["params"]["lambda"] = num_traits<T>::to_double(p[0]);
279 j["params"]["k"] = static_cast<long>(std::lround(num_traits<T>::to_double(p[1])));
280 return j;
281 case ProcessType::HYPEREXP: {
282 // The two-branch form stores (p, lambda1, lambda2) and the general
283 // one (p_1..p_n, lambda_1..lambda_n); both write the same two
284 // vectors, which is the only form on the wire.
285 json pv = json::array(), lv = json::array();
286 if (p.size() == 3) {
287 pv.push_back(num_traits<T>::to_double(p[0]));
288 pv.push_back(1.0 - num_traits<T>::to_double(p[0]));
289 lv.push_back(num_traits<T>::to_double(p[1]));
290 lv.push_back(num_traits<T>::to_double(p[2]));
291 } else {
292 const std::size_t n = p.size() / 2;
293 for (std::size_t i = 0; i < n; ++i) pv.push_back(num_traits<T>::to_double(p[i]));
294 for (std::size_t i = 0; i < n; ++i) lv.push_back(num_traits<T>::to_double(p[n + i]));
295 }
296 j["params"]["p"] = pv;
297 j["params"]["lambda"] = lv;
298 return j;
299 }
300 case ProcessType::COXIAN:
301 case ProcessType::COX2: {
302 const std::size_t n = p.size() / 2;
303 json mu = json::array(), phi = json::array();
304 for (std::size_t i = 0; i < n; ++i) mu.push_back(num_traits<T>::to_double(p[i]));
305 for (std::size_t i = 0; i < n; ++i) phi.push_back(num_traits<T>::to_double(p[n + i]));
306 // Coxian is the only name the readers reconstruct from (mu, phi);
307 // Cox2 is the two-phase constructor and shares the representation.
308 j["type"] = "Coxian";
309 j["params"]["mu"] = mu;
310 j["params"]["phi"] = phi;
311 return j;
312 }
313 case ProcessType::PH:
314 case ProcessType::APH:
315 case ProcessType::ME: {
316 json alpha = json::array();
317 for (const T& v : p) alpha.push_back(num_traits<T>::to_double(v));
318 if (d.type == ProcessType::ME) {
319 j["params"]["alpha"] = alpha;
320 j["params"]["A"] = mat_to_json(d.D0);
321 } else {
322 j["ph"]["alpha"] = alpha;
323 j["ph"]["T"] = mat_to_json(d.D0);
324 }
325 return j;
326 }
327 case ProcessType::MAP:
328 j["map"]["D0"] = mat_to_json(d.D0);
329 j["map"]["D1"] = mat_to_json(d.D1);
330 return j;
331 case ProcessType::RAP:
332 j["params"]["H0"] = mat_to_json(d.D0);
333 j["params"]["H1"] = mat_to_json(d.D1);
334 return j;
335 case ProcessType::DMAP:
336 j["params"]["D0"] = mat_to_json(d.D0);
337 j["params"]["D1"] = mat_to_json(d.D1);
338 return j;
339 case ProcessType::MMPP2: {
340 // Recovered from the pair rather than from `params`, which
341 // `map_dist` does not fill: lambda_i is the arrival rate in phase i
342 // and sigma_i the modulating rate out of it.
343 if (d.D0.rows() != 2)
344 throw InputError("network_writer: an MMPP2 must be of order two");
345 j["params"]["lambda0"] = num_traits<T>::to_double(d.D1(0, 0));
346 j["params"]["lambda1"] = num_traits<T>::to_double(d.D1(1, 1));
347 j["params"]["sigma0"] = num_traits<T>::to_double(d.D0(0, 1));
348 j["params"]["sigma1"] = num_traits<T>::to_double(d.D0(1, 0));
349 return j;
350 }
351 case ProcessType::MMAP: {
352 j["mmap"]["D0"] = mat_to_json(d.D0);
353 json blocks = json::array();
354 for (const Matrix<T>& Dk : d.Dmark) blocks.push_back(mat_to_json(Dk));
355 j["mmap"]["D1k"] = blocks;
356 return j;
357 }
358 case ProcessType::BMAP: {
359 json blocks = json::array();
360 blocks.push_back(mat_to_json(d.D0));
361 for (const Matrix<T>& Dk : d.Dmark) blocks.push_back(mat_to_json(Dk));
362 j["params"]["D"] = blocks;
363 return j;
364 }
365 case ProcessType::UNIFORM:
366 need(2, "Uniform");
367 j["params"]["a"] = num_traits<T>::to_double(p[0]);
368 j["params"]["b"] = num_traits<T>::to_double(p[1]);
369 return j;
370 case ProcessType::PARETO:
371 need(2, "Pareto");
372 j["params"]["alpha"] = num_traits<T>::to_double(p[0]);
373 j["params"]["scale"] = num_traits<T>::to_double(p[1]);
374 return j;
375 case ProcessType::GAMMA:
376 need(2, "Gamma");
377 j["params"]["alpha"] = num_traits<T>::to_double(p[0]);
378 j["params"]["beta"] = num_traits<T>::to_double(p[1]);
379 return j;
380 case ProcessType::WEIBULL:
381 // `alpha` is the SCALE here and the SHAPE in Pareto above; that is
382 // the reference's own key naming and the one trap in this table.
383 need(2, "Weibull");
384 j["params"]["alpha"] = num_traits<T>::to_double(p[0]);
385 j["params"]["beta"] = num_traits<T>::to_double(p[1]);
386 return j;
387 case ProcessType::LOGNORMAL:
388 need(2, "Lognormal");
389 j["params"]["mu"] = num_traits<T>::to_double(p[0]);
390 j["params"]["sigma"] = num_traits<T>::to_double(p[1]);
391 return j;
392 case ProcessType::DUNIFORM:
393 need(2, "DiscreteUniform");
394 j["params"]["min"] = num_traits<T>::to_double(p[0]);
395 j["params"]["max"] = num_traits<T>::to_double(p[1]);
396 return j;
397 case ProcessType::BERNOULLI:
398 need(1, "Bernoulli");
399 j["params"]["p"] = num_traits<T>::to_double(p[0]);
400 return j;
401 case ProcessType::BINOMIAL:
402 need(2, "Binomial");
403 j["params"]["n"] = static_cast<long>(std::lround(num_traits<T>::to_double(p[0])));
404 j["params"]["p"] = num_traits<T>::to_double(p[1]);
405 return j;
406 case ProcessType::POISSON:
407 need(1, "Poisson");
408 j["params"]["lambda"] = num_traits<T>::to_double(p[0]);
409 return j;
410 case ProcessType::GEOMETRIC:
411 need(1, "Geometric");
412 j["params"]["p"] = num_traits<T>::to_double(p[0]);
413 return j;
414 case ProcessType::ZIPF:
415 need(2, "Zipf");
416 j["params"]["s"] = num_traits<T>::to_double(p[0]);
417 j["params"]["n"] = static_cast<long>(std::lround(num_traits<T>::to_double(p[1])));
418 return j;
419 case ProcessType::DISCRETESAMPLER:
420 j["params"]["p"] = vec_to_json(p);
421 if (!d.trace.empty()) j["params"]["x"] = vec_to_json(d.trace);
422 return j;
423 case ProcessType::EMPIRICALCDF:
424 j["params"]["x"] = vec_to_json(d.trace);
425 j["params"]["F"] = vec_to_json(p);
426 return j;
427 case ProcessType::REPLAYER:
428 // The trace itself has no wire form: every writer emits the file
429 // PATH, plus the mean beside it as the documented fallback for a
430 // reader on another machine where the path does not resolve. A
431 // Replayer built from in-memory samples has no path, and then only
432 // the moments can be written -- which reloads as a distribution
433 // matching them, not as the trace, exactly as the reference warns.
434 if (!d.trace_file.empty()) j["params"]["fileName"] = d.trace_file;
435 j["params"]["mean"] = num_traits<T>::to_double(d.mean);
436 if (d.trace_file.empty())
437 j["params"]["scv"] = num_traits<T>::to_double(d.scv);
438 return j;
439 case ProcessType::NHPP:
440 case ProcessType::MAPT:
441 case ProcessType::PHT: {
442 j["params"]["breakpoints"] = vec_to_json(d.sched_bp);
443 j["params"]["cyclic"] = d.sched_cyclic;
444 if (d.type == ProcessType::NHPP) {
445 // A one-phase schedule: the rate of segment k is D1[k](0,0).
446 json rates = json::array();
447 for (const Matrix<T>& D1 : d.sched_D1)
448 rates.push_back(num_traits<T>::to_double(D1(0, 0)));
449 j["params"]["rates"] = rates;
450 return j;
451 }
452 json A = json::array(), B = json::array();
453 for (const Matrix<T>& M : d.sched_D0) A.push_back(mat_to_json(M));
454 for (const Matrix<T>& M : d.sched_D1) B.push_back(mat_to_json(M));
455 // A PHt is STORED converted to (D0, D1) -- see the Distrib header --
456 // so it is written in the MAPt spelling it reloads identically from.
457 j["type"] = "MAPt";
458 j["params"]["D0"] = A;
459 j["params"]["D1"] = B;
460 return j;
461 }
462 case ProcessType::PRIOR:
463 throw UnsupportedError(
464 "network_writer: a Prior is a set of alternative MODELS rather than one law; save "
465 "the design point SolverUQ built, not the design");
466 default:
467 break;
468 }
469 throw UnsupportedError(std::string("network_writer: distribution family '") +
470 lang::process_to_text(d.type) + "' has no model.json record");
471}
472
473/**
474 * The per-class cutoffs a materialized rate table is swept over: a closed
475 * class's population, and 10 for an open one.
476 *
477 * The same rule as `linemodel_save.m:157-163`, and it has to be, or the tables
478 * this writer emits would be keyed over a different lattice than the reference's
479 * for the same model and the two files would not compare.
480 */
481template <class T>
482std::vector<int> lattice_cutoffs(const qn::NetworkStruct<T>& sn) {
483 std::vector<int> cut(sn.classes.size(), 10);
484 for (std::size_t r = 0; r < sn.classes.size(); ++r)
485 if (sn.classes[r].type == lang::JobClassType::CLOSED &&
486 std::isfinite(sn.classes[r].population))
487 cut[r] = static_cast<int>(std::lround(sn.classes[r].population));
488 return cut;
489}
490
491/** Enumerate the box lattice 0 <= n(r) <= cut(r), calling `visit(counts, key)`. */
492template <class F>
493void for_each_lattice_point(const std::vector<int>& cut, const F& visit) {
494 std::size_t total = 1;
495 for (int c : cut) total *= static_cast<std::size_t>(c + 1);
496 const std::size_t K = cut.size();
497 for (std::size_t i = 0; i < total; ++i) {
498 std::vector<int> cnt(K, 0);
499 std::size_t li = i;
500 for (std::size_t d = 0; d < K; ++d) {
501 cnt[d] = static_cast<int>(li % static_cast<std::size_t>(cut[d] + 1));
502 li /= static_cast<std::size_t>(cut[d] + 1);
503 }
504 std::string key;
505 int tot = 0;
506 for (std::size_t d = 0; d < K; ++d) {
507 if (d) key += ',';
508 key += std::to_string(cnt[d]);
509 tot += cnt[d];
510 }
511 if (tot == 0) continue; // the empty state is omitted, as the reference omits it
512 visit(cnt, key);
513 }
514}
515
516} // namespace detail
517
518/**
519 * `qn::NetworkStruct` -> the model.json `model` object.
520 *
521 * Takes the struct rather than the `qn::Network` builder because that is what a
522 * solver holds, and because `get_struct()` is what the builder hands out.
523 */
524template <class T>
526 using detail::json;
527 typedef qn::NetworkStruct<T> SN;
528 const std::size_t K = sn.classes.size();
529 const std::vector<int> cut = detail::lattice_cutoffs(sn);
530
531 json model;
532 model["type"] = "Network";
533 model["name"] = sn.name;
534 // The directory every Logger writes into; model-level because that is where
535 // the reference keeps it and because a Logger's constructor refuses without it.
536 if (!sn.log_path.empty()) model["logPath"] = sn.log_path;
537
538 // -- classes --------------------------------------------------------------
539 json classes = json::array();
540 for (std::size_t r = 0; r < K; ++r) {
541 const qn::JobClass& c = sn.classes[r];
542 json cj;
543 cj["name"] = c.name;
544 // A G-network SIGNAL is written as its own wire type with the kind it
545 // removes under: written back as an ordinary class it becomes inert,
546 // and the reloaded model is a queueing network with no removals at all.
547 const bool sig = r < sn.issignal.size() && sn.issignal[r];
548 if (sig) {
549 cj["type"] = "Signal";
550 cj["openOrClosed"] = c.type == lang::JobClassType::CLOSED ? "Closed" : "Open";
551 const lang::SignalType st = sn.signaltype[r];
552 cj["signalType"] = st == lang::SignalType::REPLY
553 ? "reply"
554 : (st == lang::SignalType::CATASTROPHE ? "catastrophe"
555 : "negative");
556 if (sn.signaltarget[r] >= 1 && sn.signaltarget[r] <= K)
557 cj["targetClass"] = sn.classes[sn.signaltarget[r] - 1].name;
558 const lang::RemovalPolicy rp = sn.signalrempolicy[r];
560 cj["removalPolicy"] = rp == lang::RemovalPolicy::FCFS ? "FCFS" : "LCFS";
561 if (!sn.signalremdist[r].empty()) {
562 json pv = json::array();
563 for (const T& v : sn.signalremdist[r]) pv.push_back(num_traits<T>::to_double(v));
564 json xv = json::array();
565 // THE SUPPORT STARTS AT ZERO, unlike every other pmf written
566 // here: `signalremdist` is indexed BY BATCH SIZE, and a batch of
567 // zero is a real outcome. `discrete_support` numbers items from
568 // 1, so using it shifted every batch by one job on the way back
569 // through any reader that honours `x` -- MATLAB's included.
570 for (std::size_t b = 0; b < pv.size(); ++b) xv.push_back(static_cast<double>(b));
571 json rd;
572 rd["type"] = "DiscreteSampler";
573 rd["params"]["p"] = pv;
574 rd["params"]["x"] = xv;
575 cj["removalDistribution"] = rd;
576 }
577 } else {
578 cj["type"] = c.type == lang::JobClassType::CLOSED
579 ? (c.self_looping ? "SelfLooping" : "Closed")
580 : "Open";
581 }
583 cj["population"] = c.population;
584 if (c.refstat == 0 || c.refstat > sn.station_to_node.size())
585 throw InputError("network_writer: class '" + c.name +
586 "' names no reference station, which a closed class must have");
587 cj["refNode"] = sn.nodes[sn.station_to_node[c.refstat - 1] - 1].name;
588 }
589 if (c.prio != 0) cj["priority"] = c.prio;
590 if (c.immfeed) cj["immediateFeedback"] = true;
591 if (c.is_ref_class) cj["isReferenceClass"] = true;
592 if (std::isfinite(c.deadline)) cj["deadline"] = c.deadline;
593 if (c.spawn >= 1 && c.spawn <= K) cj["spawnClass"] = sn.classes[c.spawn - 1].name;
594 if (r < sn.syncreply.size() && sn.syncreply[r] >= 1 && sn.syncreply[r] <= K)
595 cj["replySignalClass"] = sn.classes[sn.syncreply[r] - 1].name;
596 classes.push_back(cj);
597 }
598 model["classes"] = classes;
599
600 // -- nodes ----------------------------------------------------------------
601 json nodes = json::array();
602 for (std::size_t i = 0; i < sn.nodes.size(); ++i) {
603 const qn::NodeDef& nd = sn.nodes[i];
604 const std::size_t ind = i + 1, ist = nd.station;
605 json nj;
606 nj["name"] = nd.name;
607 nj["type"] = detail::node_type_to_json(nd.nodetype);
608
609 if (nd.nodetype == lang::NodeType::Fork && nd.tasks_per_link > 1.0)
610 nj["tasksPerLink"] = nd.tasks_per_link;
611 // VARIABLE FORKING LEVELS, in the record shape `linemodel_save.m:509-553`
612 // writes: one {dest, class, value} per link rather than a matrix, since
613 // that is what the reader replays through the setters.
614 //
615 // Every link a class takes is listed, not only the ones the model
616 // overrode. The reader seeds an untouched link from `tasksPerLink`,
617 // which is the MEAN once an override exists, so a partial list would
618 // reload a fork that emits a count it was never given.
619 if (nd.nodetype == lang::NodeType::Fork) {
620 const qn::ForkParam<T>* fp = sn.fork_param_of(ind);
621 if (fp != 0) {
622 json by_dest = json::array(), by_prob = json::array(), by_dist = json::array();
623 for (std::size_t k = 0; k < fp->fan_out_link.rows(); ++k)
624 for (std::size_t r = 0; r < fp->fan_out_link.cols() && r < K; ++r) {
625 const double p = num_traits<T>::to_double(fp->fan_out_prob(k, r));
626 if (p == 0.0) continue; // link this class does not take
627 json rec;
628 rec["dest"] = sn.nodes[k].name;
629 rec["class"] = r + 1;
630 rec["value"] = num_traits<T>::to_double(fp->fan_out_link(k, r));
631 by_dest.push_back(rec);
632 if (p != 1.0) {
633 json pr;
634 pr["dest"] = sn.nodes[k].name;
635 pr["class"] = r + 1;
636 pr["value"] = p;
637 by_prob.push_back(pr);
638 }
639 const lang::Distrib<T>& d = fp->fan_out_dist[k][r];
640 if (!d.disabled) {
641 json dr;
642 dr["dest"] = sn.nodes[k].name;
643 dr["class"] = r + 1;
644 json pv = json::array(), xv = json::array();
645 for (std::size_t e = 0; e < d.params.size(); ++e) {
646 pv.push_back(num_traits<T>::to_double(d.params[e]));
647 xv.push_back(d.trace.empty()
648 ? static_cast<double>(e + 1)
650 }
651 dr["p"] = pv;
652 dr["x"] = xv;
653 by_dist.push_back(dr);
654 }
655 }
656 if (!by_dest.empty()) nj["fanOutByDest"] = by_dest;
657 if (!by_prob.empty()) nj["fanOutProb"] = by_prob;
658 if (!by_dist.empty()) nj["fanOutDist"] = by_dist;
659 }
660 }
661 // A Logger with no file name exports as a tunnel that logs nowhere, so
662 // the name is what makes the node mean anything on reload.
663 if (nd.nodetype == lang::NodeType::Logger && !nd.logger.file_name.empty())
664 nj["fileName"] = nd.logger.file_name;
665 if (nd.nodetype == lang::NodeType::Join) {
666 typename std::map<std::size_t, std::pair<std::size_t, std::size_t> >::const_iterator fj;
667 for (std::size_t f = 0; f < sn.fj.size(); ++f)
668 if (sn.fj[f].second == ind)
669 nj["forkNode"] = sn.nodes[sn.fj[f].first - 1].name;
670 typename std::map<std::size_t, typename SN::JoinDecl>::const_iterator jd =
671 sn.joindecl.find(ind);
672 if (jd != sn.joindecl.end()) {
673 if (jd->second.strategy == lang::JoinStrategy::PARTIAL)
674 nj["joinStrategy"] = "PARTIAL";
675 if (jd->second.quorum > 0) nj["joinQuorum"] = jd->second.quorum;
676 }
677 }
678 {
679 typename std::map<std::size_t, Matrix<T> >::const_iterator cs = sn.csmatrix.find(ind);
680 if (cs != sn.csmatrix.end()) {
681 json csm;
682 for (std::size_t r = 0; r < K; ++r) {
683 json row;
684 for (std::size_t s = 0; s < K; ++s) {
685 const double v = num_traits<T>::to_double(cs->second(r, s));
686 if (v != 0.0) row[sn.classes[s].name] = v;
687 }
688 if (!row.empty()) csm[sn.classes[r].name] = row;
689 }
690 nj["classSwitchMatrix"] = csm;
691 }
692 }
693 {
694 typename std::map<std::size_t, qn::CacheParam<T> >::const_iterator cp =
695 sn.nodeparam.find(ind);
696 if (cp != sn.nodeparam.end()) {
697 const qn::CacheParam<T>& c = cp->second;
698 nj["numItems"] = c.nitems;
699 nj["itemLevelCap"] = c.itemcap;
700 nj["replacementStrategy"] = detail::replacement_to_json(c.replacestrat);
701 if (num_traits<T>::to_double(c.qlru) != 1.0)
702 nj["admissionProb"] = num_traits<T>::to_double(c.qlru);
703 if (!c.itemsize.empty()) nj["itemSizes"] = c.itemsize;
704 if (!c.costcap.empty()) {
705 if (c.costcapglobal) nj["costCaps"] = c.costcap.empty() ? 0 : c.costcap[0];
706 else nj["costCaps"] = c.costcap;
707 }
708 json pop, hit, miss, itemcls;
709 for (std::size_t r = 0; r < K && r < c.pread.size(); ++r) {
710 if (c.pread[r].empty()) continue;
711 json pj;
712 pj["type"] = "DiscreteSampler";
713 pj["params"]["p"] = detail::vec_to_json(c.pread[r]);
714 pj["params"]["x"] = detail::discrete_support(c.pread[r].size());
715 pop[sn.classes[r].name] = pj;
716 }
717 for (std::size_t r = 0; r < K && r < c.hitclass.size(); ++r)
718 if (c.hitclass[r] != 0)
719 hit[sn.classes[r].name] = sn.classes[c.hitclass[r] - 1].name;
720 for (std::size_t r = 0; r < K && r < c.missclass.size(); ++r)
721 if (c.missclass[r] != 0)
722 miss[sn.classes[r].name] = sn.classes[c.missclass[r] - 1].name;
723 for (std::size_t r = 0; r < K && r < c.classitem.size(); ++r)
724 if (c.classitem[r] != 0)
725 itemcls[sn.classes[r].name] = static_cast<double>(c.classitem[r]);
726 if (!pop.empty()) nj["popularity"] = pop;
727 if (!hit.empty()) nj["hitClass"] = hit;
728 if (!miss.empty()) nj["missClass"] = miss;
729 if (!itemcls.empty()) nj["itemClass"] = itemcls;
730 if (!c.accost.empty()) {
731 json ap = json::array();
732 for (const std::vector<Matrix<T> >& per_class : c.accost) {
733 json row = json::array();
734 for (const Matrix<T>& g : per_class)
735 row.push_back(g.rows() == 0 ? json::array() : detail::mat_to_json(g));
736 ap.push_back(row);
737 }
738 nj["accessProb"] = ap;
739 }
740 if (!c.initstate.empty()) nj["initialState"] = detail::vec_to_json(c.initstate);
741 if (c.retrieval_capacity > 0 || !c.retrieval_queues.empty()) {
742 json rs;
743 rs["capacity"] = c.retrieval_capacity;
744 json by;
745 for (const auto& kv : c.retrieval_queues) {
746 json entry;
747 json qs = json::array();
748 for (std::size_t q : kv.second) qs.push_back(sn.nodes[q - 1].name);
749 entry["queues"] = qs;
750 json items;
751 for (std::size_t it = 0; it < c.retrieval_classes.size(); ++it)
752 if (kv.first < c.retrieval_classes[it].size() &&
753 c.retrieval_classes[it][kv.first] != 0)
754 items[std::to_string(it)] =
755 sn.classes[c.retrieval_classes[it][kv.first] - 1].name;
756 entry["items"] = items;
757 by[sn.classes[kv.first].name] = entry;
758 }
759 rs["byClass"] = by;
760 nj["retrievalSystem"] = rs;
761 }
762 }
763 }
764 {
765 typename std::map<std::size_t, std::vector<T> >::const_iterator im =
766 sn.initmarking.find(ind);
767 if (im != sn.initmarking.end()) nj["initialState"] = detail::vec_to_json(im->second);
768 typename std::map<std::size_t, std::vector<T> >::const_iterator sp =
769 sn.stateprior.find(ind);
770 typename std::map<std::size_t, Matrix<T> >::const_iterator ss = sn.statespace.find(ind);
771 if (sp != sn.stateprior.end() && ss != sn.statespace.end()) {
772 nj["stateSpace"] = detail::mat_to_json(ss->second);
773 nj["statePrior"] = detail::vec_to_json(sp->second);
774 }
775 }
776
777 if (ist != 0) {
778 const qn::Station<T>& st = sn.stations[ist - 1];
781 nj["scheduling"] = detail::sched_to_json(st.sched);
782 if (nd.nodetype == lang::NodeType::Queue && std::isfinite(st.nservers) &&
783 st.nservers > 1.0)
784 nj["servers"] = st.nservers;
785 if (std::isfinite(st.cap) && st.cap > 0) nj["buffer"] = st.cap;
786
787 json svc, cc, dr, sp, imf;
788 for (std::size_t r = 0; r < K; ++r) {
789 if (ist - 1 < sn.service.size() && r < sn.service[ist - 1].size() &&
790 !sn.service[ist - 1][r].disabled)
791 svc[sn.classes[r].name] = detail::dist_to_json(sn.service[ist - 1][r]);
792 if (r < st.classcap.size() && std::isfinite(st.classcap[r]))
793 cc[sn.classes[r].name] = st.classcap[r];
794 // 0 is the "the model declared none" sentinel of the station's
795 // own vector, not a DropStrategy; `refresh_capacity` reads it
796 // as `user_rule` for exactly that reason. Naming it would have
797 // to invent a rule, so the entry is simply not written.
798 if (r < st.droprule.size() && st.droprule[r] != 0)
799 dr[sn.classes[r].name] =
800 detail::drop_to_json(static_cast<lang::DropStrategy>(st.droprule[r]));
801 if (r < st.immfeed.size() && st.immfeed[r]) imf[sn.classes[r].name] = true;
802 }
805 for (std::size_t r = 0; r < K && r < st.schedparam.size(); ++r)
806 sp[sn.classes[r].name] = num_traits<T>::to_double(st.schedparam[r]);
807 if (!svc.empty()) nj["service"] = svc;
808 if (!cc.empty()) nj["classCap"] = cc;
809 if (!dr.empty()) nj["dropRule"] = dr;
810 if (!sp.empty()) nj["schedParams"] = sp;
811 if (!imf.empty()) nj["immediateFeedback"] = imf;
812
813 if (!st.lldscaling.empty()) {
814 json ld;
815 ld["type"] = "loadDependent";
816 ld["scaling"] = detail::vec_to_json(st.lldscaling);
817 nj["loadDependence"] = ld;
818 }
819 // The two rate-scaling handles, materialized over the box lattice:
820 // the only way a function reaches the wire.
821 for (int kind = 0; kind < 2; ++kind) {
822 const lang::CdScaling<T>& fun = kind == 0 ? st.cdscaling : st.jdscaling;
823 if (!static_cast<bool>(fun)) continue;
824 json blk, tbl;
825 blk["type"] = kind == 0 ? "classDependent" : "jointDependent";
826 blk["cutoffs"] = cut;
827 detail::for_each_lattice_point(cut, [&](const std::vector<int>& cnt,
828 const std::string& key) {
829 std::vector<T> n(K, num_traits<T>::from_int(0));
830 for (std::size_t r = 0; r < K; ++r)
831 n[r] = num_traits<T>::from_int(static_cast<long>(cnt[r]));
832 tbl[key] = detail::vec_to_json(fun(n));
833 });
834 blk["scaling"] = tbl;
835 blk["peak"] = detail::vec_to_json(kind == 0 ? st.cdscalingpeak : st.jdscalingpeak);
836 nj[kind == 0 ? "classDependence" : "jointDependence"] = blk;
837 }
838 {
839 typename std::map<std::size_t, typename SN::PasParam>::const_iterator pas =
840 sn.pasparam.find(ist);
841 if (pas != sn.pasparam.end() && static_cast<bool>(pas->second.svc_rate_fun)) {
842 json tbl;
843 detail::for_each_lattice_point(
844 cut, [&](const std::vector<int>& cnt, const std::string& key) {
845 std::vector<std::size_t> micro;
846 for (std::size_t r = 0; r < K; ++r)
847 for (int c = 0; c < cnt[r]; ++c) micro.push_back(r + 1);
848 tbl[key] = num_traits<T>::to_double(pas->second.svc_rate_fun(micro));
849 });
850 nj["oiServiceRate"] = tbl;
851 nj["oiCutoffs"] = cut;
852 bool any_swap = false;
853 for (const std::vector<bool>& row : pas->second.swap_graph)
854 for (bool v : row) any_swap = any_swap || v;
855 if (any_swap) {
856 json sg = json::array();
857 for (const std::vector<bool>& row : pas->second.swap_graph) {
858 json rj = json::array();
859 for (bool v : row) rj.push_back(v ? 1 : 0);
860 sg.push_back(rj);
861 }
862 nj["swapGraph"] = sg;
863 }
864 }
865 }
866 if (!st.polling_type.empty()) {
867 nj["pollingType"] = detail::polling_to_json(st.polling_type[0]);
868 if (st.polling_par != 0) nj["pollingPar"] = st.polling_par;
869 }
870 {
871 json so = json::array();
872 for (std::size_t r = 0; r < K && r < st.switchover.size(); ++r) {
873 if (st.switchover[r].disabled) continue;
874 json e;
875 e["from"] = sn.classes[r].name;
876 e["distribution"] = detail::dist_to_json(st.switchover[r]);
877 so.push_back(e);
878 }
879 if (!so.empty()) nj["switchoverTimes"] = so;
880 }
881 {
882 typename std::map<std::size_t, qn::SetupDelayOffParam<T> >::const_iterator sd =
883 sn.setupparam.find(ist);
884 if (sd != sn.setupparam.end()) {
885 json su, doff;
886 for (std::size_t r = 0; r < K && r < sd->second.setup.size(); ++r) {
887 if (sd->second.setup[r].disabled) continue;
888 su[sn.classes[r].name] = detail::dist_to_json(sd->second.setup[r]);
889 doff[sn.classes[r].name] = detail::dist_to_json(sd->second.delayoff[r]);
890 }
891 if (!su.empty()) {
892 nj["setupTime"] = su;
893 nj["delayOffTime"] = doff;
894 }
895 }
896 }
897 {
898 // Server breakdown, in `linemodel_save`'s shape: the two clocks
899 // always, and `downService` keyed by class name only where a
900 // degraded rate was declared. A zero rate is an ABSENT entry and
901 // not a zero-mean distribution, which has no reading.
902 typename std::map<std::size_t, qn::BreakdownParam<T> >::const_iterator bd =
903 sn.breakdownparam.find(ist);
904 if (bd != sn.breakdownparam.end()) {
905 json bj;
906 bj["failure"] = detail::dist_to_json(bd->second.failure);
907 bj["repair"] = detail::dist_to_json(bd->second.repair);
908 json ds;
909 for (std::size_t r = 0; r < K && r < bd->second.down_service_rates.size(); ++r) {
910 const double rate =
911 num_traits<T>::to_double(bd->second.down_service_rates[r]);
912 if (!(rate > 0.0)) continue;
913 ds[sn.classes[r].name] = detail::dist_to_json(
915 }
916 if (!ds.empty()) bj["downService"] = ds;
917 nj["breakdown"] = bj;
918 }
919 }
920 {
921 typename std::map<std::size_t, qn::RetrialParam<T> >::const_iterator rt =
922 sn.retrialparam.find(ist);
923 if (rt != sn.retrialparam.end()) {
924 json rj;
925 for (std::size_t r = 0; r < K && r < rt->second.retrial_proc.size(); ++r) {
926 if (rt->second.retrial_proc[r].disabled) continue;
927 json e;
928 e["delay"] = detail::dist_to_json(rt->second.retrial_proc[r]);
929 e["maxAttempts"] = rt->second.max_attempts[r];
930 rj[sn.classes[r].name] = e;
931 }
932 if (!rj.empty()) nj["retrial"] = rj;
933 }
934 }
935 {
936 json pat, orb, brp, blk;
937 for (std::size_t r = 0; r < K; ++r) {
938 if (r < st.patience.size() && !st.patience[r].disabled) {
939 json e;
940 e["distribution"] = detail::dist_to_json(st.patience[r]);
941 if (r < st.impatience.size() &&
943 e["impatienceType"] = detail::impatience_to_json(st.impatience[r]);
944 pat[sn.classes[r].name] = e;
945 }
946 if (r < st.orbit_impatience.size() && !st.orbit_impatience[r].disabled)
947 orb[sn.classes[r].name] = detail::dist_to_json(st.orbit_impatience[r]);
948 if (r < st.batch_reject.size() &&
950 brp[sn.classes[r].name] = num_traits<T>::to_double(st.batch_reject[r]);
951 if (r < st.balking.size() &&
952 st.balking[r].strategy != lang::BalkingStrategy::NONE) {
953 json e;
954 e["strategy"] = detail::balking_to_json(st.balking[r].strategy);
955 json ths = json::array();
956 for (const typename qn::Station<T>::BalkingThreshold& th :
957 st.balking[r].thresholds) {
958 json tj;
959 tj["minJobs"] = th.min_jobs;
960 tj["maxJobs"] = th.max_jobs;
961 tj["probability"] = num_traits<T>::to_double(th.probability);
962 ths.push_back(tj);
963 }
964 e["thresholds"] = ths;
965 blk[sn.classes[r].name] = e;
966 }
967 }
968 if (!pat.empty()) nj["patience"] = pat;
969 if (!orb.empty()) nj["orbitImpatience"] = orb;
970 if (!brp.empty()) nj["batchRejectProb"] = brp;
971 if (!blk.empty()) nj["balking"] = blk;
972 }
973 if (!st.server_types.empty()) {
974 json sts = json::array();
975 for (const typename qn::Station<T>::ServerType& t : st.server_types) {
976 json tj;
977 tj["name"] = t.name;
978 tj["count"] = t.count;
979 if (!t.compatible.empty()) {
980 json cn = json::array();
981 for (std::size_t r = 0; r < K && r < t.compatible.size(); ++r)
982 if (t.compatible[r]) cn.push_back(sn.classes[r].name);
983 tj["compatibleClasses"] = cn;
984 }
985 json sv;
986 for (std::size_t r = 0; r < K && r < t.service.size(); ++r)
987 if (!t.service[r].disabled)
988 sv[sn.classes[r].name] = detail::dist_to_json(t.service[r]);
989 if (!sv.empty()) tj["service"] = sv;
990 sts.push_back(tj);
991 }
992 nj["serverTypes"] = sts;
994 nj["heteroSchedPolicy"] = detail::hetero_to_json(st.hetero_policy);
995 }
996 {
997 json par;
998 for (std::size_t r = 0; r < K && r < st.server_parallelism.size(); ++r)
999 if (st.server_parallelism[r] > 1)
1000 par[sn.classes[r].name] = st.server_parallelism[r];
1001 if (!par.empty()) nj["serverParallelism"] = par;
1002 }
1003 {
1004 json ab;
1005 for (std::size_t r = 0; r < K && r < st.arrival_batch.size(); ++r)
1006 if (!st.arrival_batch[r].disabled)
1007 ab[sn.classes[r].name] = detail::dist_to_json(st.arrival_batch[r]);
1008 if (!ab.empty()) nj["arrivalBatch"] = ab;
1009 if (!st.marked_classes.empty()) {
1010 json mc = json::array();
1011 for (std::size_t c : st.marked_classes) mc.push_back(sn.classes[c - 1].name);
1012 nj["markedClasses"] = mc;
1013 }
1014 json dd;
1015 for (std::size_t r = 0; r < K && r < st.departure_discipline.size(); ++r)
1017 dd[sn.classes[r].name] = "FIFO";
1018 if (!dd.empty()) nj["departureDiscipline"] = dd;
1019 }
1020 }
1021
1022 {
1023 typename std::map<std::size_t, qn::TransitionParam<T> >::const_iterator tp =
1024 sn.transparam.find(ind);
1025 if (tp != sn.transparam.end()) {
1026 json modes = json::array();
1027 for (std::size_t m = 0; m < tp->second.nmodes; ++m) {
1028 json mj;
1029 mj["name"] = tp->second.modenames[m];
1030 mj["timingStrategy"] =
1031 tp->second.timing[m] == lang::TimingStrategy::IMMEDIATE ? "IMMEDIATE"
1032 : "TIMED";
1033 if (!tp->second.firingproc[m].disabled)
1034 mj["distribution"] = detail::dist_to_json(tp->second.firingproc[m]);
1035 mj["numServers"] = tp->second.nmodeservers[m];
1036 mj["firingPriority"] = tp->second.firingprio[m];
1037 mj["firingWeight"] = num_traits<T>::to_double(tp->second.fireweight[m]);
1038 const char* kArcKey[3] = {"enablingConditions", "inhibitingConditions",
1039 "firingOutcomes"};
1040 for (int which = 0; which < 3; ++which) {
1041 const Matrix<T>& row = which == 0 ? tp->second.enabling[m]
1042 : which == 1 ? tp->second.inhibiting[m]
1043 : tp->second.firing[m];
1044 json arcs = json::array();
1045 for (std::size_t q = 0; q < row.rows(); ++q)
1046 for (std::size_t r = 0; r < row.cols(); ++r) {
1047 const double v = num_traits<T>::to_double(row(q, r));
1048 // An inhibiting threshold of Inf is "never
1049 // blocks" and an enabling or firing count of 0
1050 // is no arc.
1051 if (which == 1 ? !std::isfinite(v) : v == 0.0) continue;
1052 json a;
1053 a["node"] = sn.nodes[q].name;
1054 // REQUIRED by the reference readers: both index
1055 // the arc by (node, CLASS) -- `linemodel_load.m:
1056 // 852` reads `ec.class` unguarded and
1057 // `linemodel_io.py` does `ic["class"]` -- so an
1058 // arc written without it makes the whole
1059 // document unloadable there.
1060 a["class"] = sn.classes[r].name;
1061 a["count"] = v;
1062 arcs.push_back(a);
1063 }
1064 if (!arcs.empty()) mj[kArcKey[which]] = arcs;
1065 }
1066 // A marking-dependent firing rate is a CLOSURE, so it is
1067 // written the only way it can cross JSON: materialized over
1068 // the box lattice of the enabling slots, exactly as
1069 // `firingdep_scaling_table` in linemodel_save.m does. Dropped
1070 // instead, the net reloads at its nominal rate -- a different
1071 // marking process, reported without a diagnostic.
1072 if (m < tp->second.firingdep.size() && tp->second.firingdep[m]) {
1073 const Matrix<T>& enab = tp->second.enabling[m];
1074 json slots = json::array();
1075 std::vector<std::size_t> slot_node;
1076 std::vector<long> caps;
1077 // ONE SLOT PER PLACE, not per (place, class): the
1078 // closure's own domain is the node-indexed marking that
1079 // `state_events.h` hands it, and the reader resolves a
1080 // slot by node. The class is the first the mode reads
1081 // there, which is the only one a single-class arc set
1082 // can name and is what labels the slot for the
1083 // reference readers.
1084 for (std::size_t q = 0; q < enab.rows(); ++q) {
1085 std::size_t rq = enab.cols();
1086 for (std::size_t r = 0; r < enab.cols(); ++r)
1087 if (num_traits<T>::to_double(enab(q, r)) > 0.0) { rq = r; break; }
1088 if (rq == enab.cols()) continue;
1089 json sm;
1090 sm["node"] = sn.nodes[q].name;
1091 sm["class"] = sn.classes[rq].name;
1092 slots.push_back(sm);
1093 slot_node.push_back(q);
1094 // The open-place saturation cutoff of the three
1095 // reference writers: an unbounded place would make
1096 // the lattice infinite, so it is tabulated to 10.
1097 const std::size_t sq = sn.nodes[q].station;
1098 const double pc = sq == 0 ? std::numeric_limits<double>::infinity()
1099 : sn.stations[sq - 1].cap;
1100 caps.push_back(std::isfinite(pc) ? std::lround(pc) : 10L);
1101 }
1102 if (!slots.empty()) {
1103 json frm;
1104 frm["slots"] = slots;
1105 frm["cutoffs"] = caps;
1106 json scaling;
1107 std::size_t total = 1;
1108 for (std::size_t s = 0; s < caps.size(); ++s)
1109 total *= static_cast<std::size_t>(caps[s]) + 1;
1110 std::vector<T> mk(sn.nodes.size(), num_traits<T>::from_int(0));
1111 for (std::size_t li = 0; li < total; ++li) {
1112 std::size_t rem = li;
1113 std::string key;
1114 for (std::size_t s = 0; s < caps.size(); ++s) {
1115 const std::size_t shp = static_cast<std::size_t>(caps[s]) + 1;
1116 const std::size_t c = rem % shp;
1117 rem /= shp;
1118 mk[slot_node[s]] = num_traits<T>::from_int(
1119 static_cast<long>(c));
1120 if (s) key += ',';
1121 key += std::to_string(c);
1122 }
1123 const double v =
1124 num_traits<T>::to_double(tp->second.firingdep[m](mk));
1125 scaling[key] = std::isfinite(v) ? v : 0.0;
1126 }
1127 for (std::size_t s = 0; s < slot_node.size(); ++s)
1128 mk[slot_node[s]] = num_traits<T>::from_int(0);
1129 frm["scaling"] = scaling;
1130 mj["firingRateDependence"] = frm;
1131 }
1132 }
1133 modes.push_back(mj);
1134 }
1135 nj["modes"] = modes;
1136 }
1137 }
1138 nodes.push_back(nj);
1139 }
1140 model["nodes"] = nodes;
1141
1142 // -- routing --------------------------------------------------------------
1143 json matrix;
1144 for (const auto& kv : sn.P) {
1145 const std::size_t r = kv.first.first, s = kv.first.second;
1146 json from_to;
1147 for (std::size_t a = 0; a < kv.second.rows(); ++a) {
1148 json row;
1149 for (std::size_t b = 0; b < kv.second.cols(); ++b) {
1150 const double p = num_traits<T>::to_double(kv.second(a, b));
1151 if (p != 0.0) row[sn.nodes[b].name] = p;
1152 }
1153 if (!row.empty()) from_to[sn.nodes[a].name] = row;
1154 }
1155 if (!from_to.empty())
1156 matrix[sn.classes[r - 1].name + "," + sn.classes[s - 1].name] = from_to;
1157 }
1158 json routing;
1159 routing["type"] = "matrix";
1160 routing["matrix"] = matrix;
1161 model["routing"] = routing;
1162
1163 json strategies, weights, params;
1164 for (std::size_t i = 0; i < sn.nodes.size(); ++i) {
1165 const qn::NodeDef& nd = sn.nodes[i];
1166 json per_class, per_class_w, per_class_p;
1167 for (std::size_t r = 0; r < K && r < nd.routing.size(); ++r) {
1168 // PROB is the matrix itself; RAND is DECLARED not derived, and dropping it wrote JMT's Empirical strategy where the model asks for Random.
1170 per_class[sn.classes[r].name] = detail::routing_to_json(nd.routing[r]);
1171 if (r < nd.routing_weights.size() && !nd.routing_weights[r].empty()) {
1172 json dw;
1173 for (const auto& kv : nd.routing_weights[r]) dw[sn.nodes[kv.first - 1].name] = kv.second;
1174 per_class_w[sn.classes[r].name] = dw;
1175 }
1176 if (r < nd.routing_param.size() && nd.routing_param[r] != 0) {
1177 json pj;
1178 pj["d"] = nd.routing_param[r];
1179 per_class_p[sn.classes[r].name] = pj;
1180 }
1181 }
1182 if (!per_class.empty()) strategies[nd.name] = per_class;
1183 if (!per_class_w.empty()) weights[nd.name] = per_class_w;
1184 if (!per_class_p.empty()) params[nd.name] = per_class_p;
1185 }
1186 if (!strategies.empty()) model["routingStrategies"] = strategies;
1187 if (!weights.empty()) model["routingWeights"] = weights;
1188 if (!params.empty()) model["routingParams"] = params;
1189
1190 // Krzesinski state-dependent routing. Every centre travels by NODE NAME, so
1191 // the block is language independent and a node reordering on either side
1192 // cannot shift a centre. Branch index 1 denotes the complement M-V and is
1193 // written as an empty list, keeping the paper's own numbering.
1194 // See _kb/16-state-dependent-routing.md
1195 if (!sn.sdr_nodes.empty()) {
1196 const pfqn::SdrStruct& sd = sn.sdr_nodes; // 0-based NODE indices
1197 std::size_t cls = 0;
1198 for (std::size_t r = 0; r < K && cls == 0; ++r)
1199 if (sn.nodes[sd.entry].routing.size() > r &&
1200 sn.nodes[sd.entry].routing[r] == lang::RoutingStrategy::SDR)
1201 cls = r + 1;
1202 if (cls == 0)
1203 throw InputError("network_writer: the model declares state-dependent routing at node '" +
1204 sn.nodes[sd.entry].name + "' but no class routes SDR there");
1205 json sdr;
1206 sdr["entry"] = sn.nodes[sd.entry].name;
1207 sdr["departure"] = sn.nodes[sd.departure].name;
1208 sdr["class"] = sn.classes[cls - 1].name;
1209 json branches = json::array();
1210 for (std::size_t b = 0; b < sd.branch.size(); ++b) {
1211 json bn = json::array();
1212 for (std::size_t q = 0; q < sd.branch[b].size(); ++q)
1213 bn.push_back(sn.nodes[sd.branch[b][q]].name);
1214 branches.push_back(bn);
1215 }
1216 sdr["branches"] = branches;
1217 json level = json::array(), Cs = json::array(), dm = json::array();
1218 for (std::size_t b = 0; b < sd.level.size(); ++b)
1219 level.push_back(static_cast<double>(sd.level[b]));
1220 for (std::size_t t = 0; t < sd.C.size(); ++t) Cs.push_back(sd.C[t]);
1221 for (std::size_t t = 0; t < sd.d.rows(); ++t) {
1222 json row = json::array();
1223 for (std::size_t b = 0; b < sd.d.cols(); ++b) row.push_back(sd.d(t, b));
1224 dm.push_back(row);
1225 }
1226 sdr["level"] = level;
1227 sdr["C"] = Cs;
1228 sdr["d"] = dm;
1229 model["stateDepRouting"] = sdr;
1230 }
1231
1232 // -- finite capacity regions ---------------------------------------------
1233 if (!sn.regions.empty()) {
1234 json fcr = json::array();
1235 for (std::size_t g = 0; g < sn.regions.size(); ++g) {
1236 const typename SN::Region& rg = sn.regions[g];
1237 json rj;
1238 rj["name"] = rg.name.empty() ? "Region" + std::to_string(g + 1) : rg.name;
1239 json stations = json::array();
1240 double global = -1.0, globalmem = -1.0;
1241 json class_cap, class_size, class_weight;
1242 for (std::size_t m = 0; m < rg.members.size(); ++m) {
1243 if (!rg.members[m]) continue;
1244 json sj;
1245 sj["node"] = sn.nodes[sn.station_to_node[m] - 1].name;
1246 stations.push_back(sj);
1247 global = rg.cap[m][K];
1248 // The region's global MEMORY budget, which is enforced beside the
1249 // job cap by the CTMC, the loss-network NC arm and the JMT export
1250 // rather than folded into it. Omitted, the region reloads with
1251 // unbounded memory and every one of those three drops a
1252 // constraint the model declared.
1253 if (m < rg.maxmem.size()) globalmem = rg.maxmem[m];
1254 for (std::size_t r = 0; r < K; ++r)
1255 if (rg.cap[m][r] != -1.0) class_cap[sn.classes[r].name] = rg.cap[m][r];
1256 }
1257 rj["stations"] = stations;
1258 if (global != -1.0) rj["globalMaxJobs"] = global;
1259 if (globalmem != -1.0) rj["globalMaxMemory"] = globalmem;
1260 if (!class_cap.empty()) rj["classMaxJobs"] = class_cap;
1261 json rule;
1262 for (std::size_t r = 0; r < K && r < rg.rule.size(); ++r)
1263 rule[sn.classes[r].name] = detail::drop_to_json(rg.rule[r]);
1264 rj["dropRule"] = rule;
1265 for (std::size_t r = 0; r < K; ++r) {
1266 if (r < rg.size.size() && num_traits<T>::to_double(rg.size[r]) != 1.0)
1267 class_size[sn.classes[r].name] = num_traits<T>::to_double(rg.size[r]);
1268 if (r < rg.weight.size() && num_traits<T>::to_double(rg.weight[r]) != 1.0)
1269 class_weight[sn.classes[r].name] = num_traits<T>::to_double(rg.weight[r]);
1270 }
1271 // classSize and classWeight are per-STATION on the wire, and the
1272 // struct holds one vector per region, so they are written onto every
1273 // member station -- which is the same model the reader rebuilds.
1274 if (!class_size.empty() || !class_weight.empty())
1275 for (json& sj : rj["stations"]) {
1276 if (!class_size.empty()) sj["classSize"] = class_size;
1277 if (!class_weight.empty()) sj["classWeight"] = class_weight;
1278 }
1279 if (rg.lincon_A.rows() > 0) {
1280 rj["constraintA"] = detail::mat_to_json(rg.lincon_A);
1281 rj["constraintB"] = detail::vec_to_json(rg.lincon_b);
1282 }
1283 fcr.push_back(rj);
1284 }
1285 model["finiteCapacityRegions"] = fcr;
1286 }
1287
1288 // -- global (Whittle) dependence -----------------------------------------
1289 //
1290 // phi(n) reads the FULL population matrix, so unlike the per-station
1291 // classDependence/jointDependence blocks it is materialized over the lattice
1292 // of the WHOLE network state. Only the (station,class) SLOTS a class can
1293 // actually occupy carry a coordinate: a Source holds no jobs and a class with
1294 // zero per-class capacity at a station never appears there. That restriction
1295 // is lossless, since no DEP or PHASE event ever fires at such a slot.
1296 if (static_cast<bool>(sn.gdscaling)) {
1297 const std::size_t M = sn.nstations, K = sn.nclasses;
1298 const std::vector<double> njobs = sn.njobs();
1299 const int wcut = sn.gdscalingcutoff;
1300 std::vector<std::size_t> slot_st, slot_cl;
1301 std::vector<int> cuts;
1302 for (std::size_t i = 0; i < M; ++i) {
1303 if (sn.nodes[sn.station_to_node[i] - 1].nodetype == lang::NodeType::Source) continue;
1304 for (std::size_t r = 0; r < K; ++r) {
1305 const double cap = sn.classcap[i][r];
1306 if (!(cap > 0)) continue;
1307 int c = std::isfinite(njobs[r]) ? static_cast<int>(std::lround(njobs[r])) : wcut;
1308 if (std::isfinite(cap)) c = std::min(c, static_cast<int>(std::lround(cap)));
1309 slot_st.push_back(i);
1310 slot_cl.push_back(r);
1311 cuts.push_back(c > 0 ? c : 0);
1312 }
1313 }
1314 const std::size_t P = cuts.size();
1315 std::size_t total = 1;
1316 for (std::size_t d = 0; d < P; ++d) {
1317 total *= static_cast<std::size_t>(cuts[d] + 1);
1318 if (total > 200000u)
1319 throw InputError(
1320 "the global dependence lattice exceeds the wire limit of 200000 points; lower "
1321 "the wireCutoff argument of set_global_dependence, or solve the model "
1322 "natively");
1323 }
1324
1325 json blk, slots, tbl;
1326 blk["type"] = "globalDependent";
1327 std::vector<std::string> station_names(M), class_names(K);
1328 for (std::size_t i = 0; i < M; ++i)
1329 station_names[i] = sn.nodes[sn.station_to_node[i] - 1].name;
1330 for (std::size_t r = 0; r < K; ++r) class_names[r] = sn.classes[r].name;
1331 blk["stations"] = station_names;
1332 blk["classes"] = class_names;
1333 slots = json::array();
1334 for (std::size_t d = 0; d < P; ++d) {
1335 json sm;
1336 sm["station"] = station_names[slot_st[d]];
1337 sm["class"] = class_names[slot_cl[d]];
1338 slots.push_back(sm);
1339 }
1340 blk["slots"] = slots;
1341 blk["cutoffs"] = cuts;
1342 blk["cutoff"] = wcut;
1343
1344 for (std::size_t li = 0; li < total; ++li) {
1345 std::size_t rem = li;
1346 std::vector<int> cnt(P, 0);
1347 for (std::size_t d = 0; d < P; ++d) {
1348 cnt[d] = static_cast<int>(rem % static_cast<std::size_t>(cuts[d] + 1));
1349 rem /= static_cast<std::size_t>(cuts[d] + 1);
1350 }
1351 std::vector<T> n(M * K, num_traits<T>::from_int(0));
1352 for (std::size_t d = 0; d < P; ++d)
1353 n[slot_st[d] * K + slot_cl[d]] = num_traits<T>::from_int(cnt[d]);
1354 const std::vector<T> v = sn.gdscaling(n);
1355 std::vector<double> flat(M * K, 1.0);
1356 for (std::size_t i = 0; i < M; ++i)
1357 for (std::size_t r = 0; r < K; ++r) {
1358 double x;
1359 if (v.size() == 1) x = num_traits<T>::to_double(v[0]);
1360 else if (v.size() == M) x = num_traits<T>::to_double(v[i]);
1361 else x = num_traits<T>::to_double(v[i * K + r]);
1362 flat[i * K + r] = std::isfinite(x) ? x : 0.0;
1363 }
1364 std::string key;
1365 if (P == 0) key = "0";
1366 else
1367 for (std::size_t d = 0; d < P; ++d) {
1368 if (d) key += ',';
1369 key += std::to_string(cnt[d]);
1370 }
1371 tbl[key] = flat;
1372 }
1373 blk["scaling"] = tbl;
1374 std::vector<double> pk(M * K, 1.0);
1375 for (std::size_t j = 0; j < pk.size() && j < sn.gdscalingpeak.size(); ++j)
1376 pk[j] = num_traits<T>::to_double(sn.gdscalingpeak[j]);
1377 blk["peak"] = pk;
1378 model["globalDependence"] = blk;
1379 }
1380
1381 // -- rewards --------------------------------------------------------------
1382 //
1383 // The DECLARATIVE ones only. A reward built from a lambda carries no `kind`
1384 // and is dropped here, exactly as `linemodel_save` drops it: any record
1385 // written for it would reload as a different function.
1386 {
1387 // IN NAME ORDER, which is the cross-codebase convention and not a
1388 // cosmetic choice: `rewards2json` sorts for it explicitly, because the
1389 // JAR holds the rewards in a HashMap and has no insertion order to
1390 // preserve. Emitting them in struct order made the same model produce a
1391 // different document here than in the other three, which the JSON arm of
1392 // the parity harness reports as a difference in the model.
1393 std::map<std::string, json> by_name;
1394 for (const typename SN::Reward& rw : sn.reward) {
1395 if (rw.kind.empty() || rw.node == 0) continue;
1396 json rj;
1397 rj["name"] = rw.name;
1398 rj["type"] = rw.kind;
1399 rj["node"] = sn.nodes[rw.node - 1].name;
1400 if (rw.cls != 0) rj["class"] = sn.classes[rw.cls - 1].name;
1401 by_name[rw.name] = rj;
1402 }
1403 json rewards = json::array();
1404 for (const std::pair<const std::string, json>& kv : by_name) rewards.push_back(kv.second);
1405 if (!rewards.empty()) model["rewards"] = rewards;
1406 }
1407 return model;
1408}
1409
1410/** The complete model.json envelope: `{format, version, model}`. */
1411template <class T>
1413 detail::json root;
1414 root["format"] = "line-model";
1415 root["version"] = "1.0";
1416 root["model"] = network_to_json(sn);
1417 detail::wire_nonfinite(root);
1418 return root;
1419}
1420
1421/** Write a model.json file, indented as the reference writers indent it. */
1422template <class T>
1423void write_network_json(const qn::NetworkStruct<T>& sn, const std::string& path) {
1424 std::ofstream out(path.c_str());
1425 if (!out) throw InputError("network_writer: cannot open " + path + " for writing");
1426 out << network_json_envelope(sn).dump(2) << "\n";
1427}
1428
1429} // namespace io
1430} // namespace line
1431
1432#endif // LINE_IO_NETWORK_WRITER_H
InputError(const std::string &what)
Definition error.h:39
std::size_t cols() const
Definition matrix.h:90
std::size_t rows() const
Definition matrix.h:89
bool empty() const
Definition matrix.h:92
UnsupportedError(const std::string &what)
Definition error.h:51
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.
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.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
Definition lang_types.h:181
DropStrategy
Blocking and loss rules, with the values of MATLAB DropStrategy.
Definition lang_types.h:424
@ IMMEDIATE
fires with zero delay, resolved by weight and priority
Definition lang_types.h:363
BalkingStrategy
Balking rules, with the values of MATLAB BalkingStrategy.
Definition lang_types.h:445
SignalType
G-network signal classes, with the values of MATLAB SignalType.
Definition lang_types.h:167
@ REPLY
completes a synchronous call, releasing a held server
Definition lang_types.h:168
@ CATASTROPHE
removes EVERY job at the station
Definition lang_types.h:170
RoutingStrategy
Routing strategies, with the values of MATLAB RoutingStrategy.
Definition lang_types.h:389
@ SDR
Krzesinski (1987) product-form state-dependent routing.
Definition lang_types.h:398
RemovalPolicy
Which job a negative signal removes, with the values of MATLAB RemovalPolicy.
Definition lang_types.h:174
@ FCFS
the oldest waiting job; servers only once nobody waits
Definition lang_types.h:176
@ RANDOM
uniform over waiting AND in-service jobs
Definition lang_types.h:175
HeteroSchedPolicy
How a heterogeneous station picks among its server types, MATLAB HeteroSchedPolicy.
Definition lang_types.h:451
PollingType
Polling service disciplines, with the values of MATLAB PollingType.
Definition lang_types.h:370
@ KLIMITED
serve at most K per visit (K in pollingPar)
Definition lang_types.h:373
@ EXHAUSTIVE
serve until the queue empties
Definition lang_types.h:372
@ GATED
serve exactly the jobs present at the polling instant
Definition lang_types.h:371
@ DECREMENTING
serve until the queue is one shorter than at arrival
Definition lang_types.h:374
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
Definition lang_types.h:483
const char * sched_to_text(SchedStrategy s)
Definition lang_types.h:230
const char * process_to_text(ProcessType p)
The MATLAB ProcessType name, as sn.procid prints it.
Definition lang_types.h:560
std::function< std::vector< T >(const std::vector< T > &)> CdScaling
A class-dependent scaling map, sn.cdscaling.
Definition lang_types.h:639
NodeType
Node kinds, with the values of MATLAB NodeType.
Definition lang_types.h:324
ReplacementStrategy
Cache replacement policies, with the values of MATLAB ReplacementStrategy.
Definition lang_types.h:378
@ HLRU
h-LRU / LRU(m): h lists, promote i -> i+1 on a hit
Definition lang_types.h:383
@ CLIMB
move up one position on a hit (transposition rule)
Definition lang_types.h:384
@ QLRU
q-LRU: LRU with probabilistic admission on a miss
Definition lang_types.h:385
@ LRU
least recently used
Definition lang_types.h:382
@ FIFO
first in, first out
Definition lang_types.h:380
ImpatienceType
Impatience kinds, with the values of MATLAB ImpatienceType.
Definition lang_types.h:442
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.
static Distrib exp_rate(const T &r)
Definition lang_types.h:814
std::vector< T > params
Constructor arguments, in MATLAB getParam order.
Definition lang_types.h:734
std::vector< T > trace
Replayer / Trace samples; empty for every other type.
Definition lang_types.h:736
Topology and coefficients of a state-dependent routing subnetwork.
Definition pfqn_sdr.h:59
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.
Definition pfqn_sdr.h:71
Matrix< double > d
Coefficients d_tb of eq.
Definition pfqn_sdr.h:75
std::vector< double > C
Coefficients C_t of eq.
Definition pfqn_sdr.h:73
std::size_t departure
Departure centre d of Q(V,V); may equal entry.
Definition pfqn_sdr.h:63
std::vector< std::vector< std::size_t > > branch
branch[b] holds the centres of branch b, b >= 1; branch[0] is unused.
Definition pfqn_sdr.h:65
std::size_t entry
Entry centre e of Q(V,V).
Definition pfqn_sdr.h:61
T qlru
Delayed-hit retrieval system (Cache.setRetrievalSystem).
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
std::string file_name
base name, no directory
A node of the network.
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...
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...
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< int > droprule
Per-class blocking rule as an INT, with 0 meaning "not set".
SchedStrategy sched
double nservers
may be infinite (a Delay, or an inf-scheduled task)
std::vector< lang::PollingType > polling_type
Polling parameters for a POLLING station, MATLAB's pollingType, switchoverTime and pollingPar on the ...
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::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
std::vector< bool > immfeed
Node-level immediate feedback, per class; empty when the station sets none.
std::vector< ServerType > server_types