71 <<
"LDES: discrete-event simulation engine for LINE models.\n\n"
72 <<
"Usage: ldes solve <model.json> [-o <result.json>] [options]\n\n"
74 <<
" -s, --samples N service completions to simulate (default 200000)\n"
75 <<
" -e, --maxevents N event budget; overrides --samples\n"
76 <<
" --maxtime SECONDS wall-clock budget\n"
77 <<
" --seed N run seed (default 23000; -1 draws one)\n"
78 <<
" --method NAME engine method (default 'default')\n"
79 <<
" --cnvgon stop on the convergence check\n"
80 <<
" --cnvgtol X relative tolerance of that check\n"
81 <<
" --cnvgbatch N batches before the first check\n"
82 <<
" --cnvgchk N events between checks\n"
83 <<
" --tranfilter NAME mser5 | fixed | none\n"
84 <<
" --mserbatch N MSER batch size\n"
85 <<
" --warmupfrac X warmup fraction, for --tranfilter fixed\n"
86 <<
" --tranobs N transient observation points over --timespan\n"
87 <<
" --cimethod NAME obm | bm | spectral | none\n"
88 <<
" --obmoverlap X overlap of the batch means\n"
89 <<
" --ciminbatch N batches below which no CI is reported\n"
90 <<
" --ciminobs N observations below which no CI is reported\n"
91 <<
" --spectrallowfreqfrac X low-frequency fraction of the spectral CI\n"
92 <<
" --slotted run on the slot lattice\n"
93 <<
" --slotlength X slot length (implies --slotted)\n"
94 <<
" --replications N independent replications\n"
95 <<
" --numthreads N threads across replications\n"
96 <<
" --timespan T0,T1 transient run over [T0,T1]\n"
97 <<
" --busyperiod K busy-period orders 1..K\n"
98 <<
" --busyperiod-subnet i,j,... an extra busy-period target\n"
99 <<
" --initsol V initial state, station-major, comma separated\n"
100 <<
" --initjobs J job-level initial state, one row per job, rows ';'\n"
101 <<
" separated: station,class,remaining,elapsed,qage,sage,order[,id]\n"
102 <<
" --trackjobs export the final job-level state and tagged departures\n"
103 <<
" --export-histogram fill the state histogram block\n"
104 <<
" --trajectory fill the state trajectory block\n"
105 <<
" --respt-samples record every per-visit response time\n"
106 <<
" --sample-path <N> Export the exact sample path: N rows of events and LINE State\n"
107 <<
" (one replication, no warmup removal)\n"
108 <<
" -h, --help this text\n";
111[[noreturn]]
void fail(
const std::string& msg) {
112 std::cerr <<
"Error: " << msg <<
"\n";
117[[noreturn]]
void refuse(
const std::string& flag,
const std::string& why) {
118 std::cerr <<
"Error: " << flag <<
" is accepted by the Java engine but not by this one: "
119 << why <<
".\nRun the model through common/ldes.jar for it, rather than "
120 "taking a silently different simulation.\n";
124std::string need_value(
int argc,
char** argv,
int& i,
const std::string& flag) {
125 if (i + 1 >= argc)
fail(flag +
" requires a value.");
126 return std::string(argv[++i]);
129std::vector<double> parse_doubles(
const std::string& csv) {
130 std::vector<double> out;
131 std::stringstream ss(csv);
133 while (std::getline(ss, tok,
',')) {
134 if (!tok.empty()) out.push_back(std::atof(tok.c_str()));
139std::vector<std::size_t> parse_indices(
const std::string& csv) {
140 std::vector<std::size_t> out;
141 std::stringstream ss(csv);
143 while (std::getline(ss, tok,
',')) {
144 if (!tok.empty()) out.push_back(
static_cast<std::size_t
>(std::atol(tok.c_str())));
150std::string num(
double v) {
151 if (std::isnan(v))
return "null";
152 if (std::isinf(v))
return v > 0 ?
"1e999" :
"-1e999";
154 std::snprintf(buf,
sizeof(buf),
"%.17g", v);
155 return std::string(buf);
161 for (std::size_t i = 0; i < rows; ++i) {
164 for (std::size_t j = 0; j < cols; ++j) {
166 const bool inside = (i < m.
rows() && j < m.
cols());
167 s += num(inside ? m(i, j) : 0.0);
176std::string respt_block_json(
const std::vector<std::vector<std::vector<double>>>& b) {
178 for (std::size_t i = 0; i < b.size(); ++i) {
181 for (std::size_t k = 0; k < b[i].size(); ++k) {
184 for (std::size_t j = 0; j < b[i][k].size(); ++j) {
186 s += num(b[i][k][j]);
197std::string respt_samples_json(
const LdesResult& r) {
198 if (r.respTimeSamples.empty())
return std::string();
199 return respt_block_json(r.respTimeSamples);
207std::string respt_samples_rep_json(
const LdesResult& r) {
208 if (r.respTimeSamplesRep.size() < 2)
return std::string();
210 for (std::size_t q = 0; q < r.respTimeSamplesRep.size(); ++q) {
212 s += respt_block_json(r.respTimeSamplesRep[q]);
218std::string str_json(
const std::string& s) {
219 std::string out =
"\"";
221 if (c ==
'"' || c ==
'\\') {
224 }
else if (c ==
'\n') {
245 for (std::size_t i = 0; i < m.
rows(); ++i) {
248 for (std::size_t j = 0; j < m.
cols(); ++j) {
259 std::string s =
"{\"t\": [";
260 for (std::size_t k = 0; k < sp.
t.size(); ++k) s += (k ?
", " :
"") + num(sp.
t[k]);
261 s +=
"], \"events\": [";
262 for (std::size_t k = 0; k < sp.
events.size(); ++k) {
263 s += k ?
", [" :
"[";
264 for (std::size_t c = 0; c < sp.
events[k].size(); ++c) s += (c ?
", " :
"") + num(sp.
events[k][c]);
267 s +=
"], \"statefulNodes\": [";
270 s +=
"], \"state\": [";
271 for (std::size_t k = 0; k < sp.
state.size(); ++k)
272 s += (k ?
", " :
"") + (sp.
refusals[k].empty() ? sp_mat(sp.
state[k]) : std::string(
"null"));
273 s +=
"], \"aggr\": [";
274 for (std::size_t k = 0; k < sp.
aggr.size(); ++k) s += (k ?
", " :
"") + sp_mat(sp.
aggr[k]);
275 s +=
"], \"refusals\": [";
276 for (std::size_t k = 0; k < sp.
refusals.size(); ++k)
277 s += (k ?
", " :
"") + (sp.
refusals[k].empty() ? std::string(
"null") : str_json(sp.
refusals[k]));
278 s +=
"], \"phases\": [";
279 for (std::size_t i = 0; i < sp.
phases.size(); ++i) {
280 s += i ?
", [" :
"[";
281 for (std::size_t k = 0; k < sp.
phases[i].size(); ++k)
282 s += (k ?
", " :
"") + num(
static_cast<double>(sp.
phases[i][k]));
286 s +=
", \"simTime\": " + num(sp.
sim_time) +
"}";
290std::string result_json(
const LdesResult& r,
const LdesOptions& o,
double runtime,
291 std::size_t events) {
292 const std::size_t S = r.nstations, C = r.nclasses;
295 s +=
" \"format\": \"ldes-result\",\n";
296 s +=
" \"version\": \"1.0\",\n";
297 s +=
" \"solver\": \"SolverLDES\",\n";
298 s +=
" \"method\": " + str_json(o.method) +
",\n";
299 s +=
" \"runtime\": " + num(runtime) +
",\n";
302 s += std::string(
" \"converged\": ") + (r.converged ?
"true" :
"false") +
",\n";
303 s += std::string(
" \"stoppingReason\": \"") +
304 (r.stopping_reason ==
"sample_path" || r.stopping_reason ==
"max_time" ||
305 r.stopping_reason ==
"convergence"
307 : std::string(
"max_events")) +
309 s +=
" \"convergenceBatches\": " + std::to_string(r.convergence_batches) +
",\n";
310 s +=
" \"totalSimulatedEvents\": " + std::to_string(events) +
",\n";
312 s +=
" \"dimensions\": {\"nstations\": " + std::to_string(S) +
313 ", \"nclasses\": " + std::to_string(C) +
", \"nchains\": " + std::to_string(r.nchains) +
314 ", \"stationNames\": [";
315 for (std::size_t i = 0; i < r.station_names.size(); ++i) {
317 s += str_json(r.station_names[i]);
319 s +=
"], \"classNames\": [";
320 for (std::size_t i = 0; i < r.class_names.size(); ++i) {
322 s += str_json(r.class_names[i]);
326 s +=
" \"metrics\": {";
327 s +=
"\"QN\": " + mat_json(r.QN, S, C);
328 s +=
", \"UN\": " + mat_json(r.UN, S, C);
329 s +=
", \"RN\": " + mat_json(r.RN, S, C);
330 s +=
", \"TN\": " + mat_json(r.TN, S, C);
331 s +=
", \"AN\": " + mat_json(r.AN, S, C);
332 s +=
", \"WN\": " + mat_json(r.WN, S, C);
333 s +=
", \"CN\": " + mat_json(r.CN, 1, C);
334 s +=
", \"XN\": " + mat_json(r.XN, 1, C);
339 if (!r.DropRateJoin.empty()) s +=
", \"DropRateJoin\": " + mat_json(r.DropRateJoin, S, C);
350 if (o.export_histogram && r.histogram_space.rows() > 0 && r.histogram_time.rows() > 0) {
351 s +=
" \"stateHistogram\": {";
352 s +=
"\"space\": " + mat_json(r.histogram_space, r.histogram_space.rows(),
353 r.histogram_space.cols());
354 s +=
", \"time\": " + mat_json(r.histogram_time, r.histogram_time.rows(), 1);
355 if (r.traj_space.rows() > 0 && r.traj_time.rows() > 0) {
356 s +=
", \"trajSpace\": " +
357 mat_json(r.traj_space, r.traj_space.rows(), r.traj_space.cols());
358 s +=
", \"trajTime\": " + mat_json(r.traj_time, r.traj_time.rows(), 1);
363 if (!r.busy_periods.empty()) {
364 s +=
" \"busyPeriods\": {\"orders\": " +
365 std::to_string(r.busy_periods[0].mean.size()) +
", \"targets\": [";
366 for (std::size_t ti = 0; ti < r.busy_periods.size(); ++ti) {
367 const LdesResult::BusyPeriodTarget& t = r.busy_periods[ti];
369 s +=
"{\"name\": " + str_json(t.name) +
", \"stations\": [";
370 for (std::size_t k = 0; k < t.stations.size(); ++k) {
372 s += std::to_string(t.stations[k]);
374 s +=
"], \"class\": " + std::to_string(t.job_class) +
", \"mean\": [";
375 for (std::size_t k = 0; k < t.mean.size(); ++k) {
379 s +=
"], \"count\": [";
380 for (std::size_t k = 0; k < t.count.size(); ++k) {
382 s += num(t.count[k]);
390 if (o.track_jobs || o.init_jobs.rows() > 0) {
391 s +=
" \"jobState\": {\"finalJobs\": " +
392 mat_json(r.finalJobs, r.finalJobs.rows(), r.finalJobs.cols());
393 s +=
", \"jobDepartures\": " +
394 mat_json(r.jobDepartures, r.jobDepartures.rows(), r.jobDepartures.cols()) +
"},\n";
398 if (r.has_sample_path) s +=
" \"samplePath\": " + sample_path_json(r.sample_path) +
",\n";
400 if (!r.cache_metrics.empty()) {
401 s +=
" \"cacheMetrics\": {";
403 for (std::map<std::string, line::ldes::LdesCacheMetrics>::const_iterator it =
404 r.cache_metrics.begin();
405 it != r.cache_metrics.end(); ++it) {
406 if (!first) s +=
", ";
409 s += str_json(it->first) +
": {";
422 if (r.QNfcr.rows() > 0) {
423 const std::size_t G = r.QNfcr.rows();
424 s +=
" \"fcr\": {\"nregions\": " + std::to_string(G);
425 s +=
", \"QNfcr\": " + mat_json(r.QNfcr, G, C);
426 s +=
", \"UNfcr\": " + mat_json(r.UNfcr, G, C);
427 s +=
", \"RNfcr\": " + mat_json(r.RNfcr, G, C);
428 s +=
", \"TNfcr\": " + mat_json(r.TNfcr, G, C);
429 s +=
", \"ANfcr\": " + mat_json(r.ANfcr, G, C);
430 s +=
", \"WNfcr\": " + mat_json(r.WNfcr, G, C);
431 s +=
", \"WeightNfcr\": " + mat_json(r.WeightNfcr, G, C);
432 s +=
", \"MemOccNfcr\": " + mat_json(r.MemOccNfcr, G, C);
433 s +=
", \"DropRateNfcr\": " + mat_json(r.DropRateNfcr, G, C);
439 if (r.t.empty() && !r.fj_forks.empty() && r.FJTput.rows() == r.fj_forks.size()) {
440 const std::size_t
P = r.fj_forks.size();
441 s +=
" \"forkJoin\": {\"forks\": [";
442 for (std::size_t p = 0; p <
P; ++p) s += (p ?
", " :
"") + str_json(r.fj_forks[p]);
443 s +=
"], \"joins\": [";
444 for (std::size_t p = 0; p <
P; ++p) s += (p ?
", " :
"") + str_json(r.fj_joins[p]);
446 s +=
", \"FJQLen\": " + mat_json(r.FJQLen, P, C);
447 s +=
", \"FJRespT\": " + mat_json(r.FJRespT, P, C);
448 s +=
", \"FJTput\": " + mat_json(r.FJTput, P, C);
459 const std::string respt_json = respt_samples_json(r);
460 if (o.export_trajectory && !r.t.empty()) {
461 s +=
" \"transient\": {\"t\": [";
462 for (std::size_t k = 0; k < r.t.size(); ++k) {
467 const char* keys[3] = {
"QNt",
"UNt",
"TNt"};
468 const std::vector<std::vector<line::Matrix<double>>>* src[3] = {&r.QNt, &r.UNt, &r.TNt};
469 for (
int q = 0; q < 3; ++q) {
470 s += std::string(
", \"") + keys[q] +
"\": [";
471 for (std::size_t i = 0; i < src[q]->size(); ++i) {
474 for (std::size_t k = 0; k < (*src[q])[i].size(); ++k) {
477 s += mat_json(mm, mm.
rows(), mm.
cols());
483 if (!respt_json.empty()) s +=
", \"respTimeSamples\": " + respt_json;
487 if (o.export_respt && !respt_json.empty())
488 s +=
" \"respTimeSamples\": " + respt_json +
",\n";
490 if (o.export_respt && o.replications >= 2) {
491 const std::string rep_json = respt_samples_rep_json(r);
492 if (!rep_json.empty()) s +=
" \"respTimeSamplesRep\": " + rep_json +
",\n";
494 s +=
" \"sampleCounts\": {";
495 s +=
"\"QNSamples\": " + mat_json(r.QNSamples, S, C);
496 s +=
", \"UNSamples\": " + mat_json(r.UNSamples, S, C);
497 s +=
", \"RNSamples\": " + mat_json(r.RNSamples, S, C);
498 s +=
", \"TNSamples\": " + mat_json(r.TNSamples, S, C);
501 s +=
" \"confidenceIntervals\": {";
502 s +=
"\"QNCI\": " + mat_json(r.QNCI, S, C);
503 s +=
", \"UNCI\": " + mat_json(r.UNCI, S, C);
504 s +=
", \"RNCI\": " + mat_json(r.RNCI, S, C);
505 s +=
", \"TNCI\": " + mat_json(r.TNCI, S, C);
506 s +=
", \"ANCI\": " + mat_json(r.ANCI, S, C);
507 s +=
", \"WNCI\": " + mat_json(r.WNCI, S, C);
510 s +=
" \"relativePrecision\": {";
511 s +=
"\"QNRelPrec\": " + mat_json(r.QNRelPrec, S, C);
512 s +=
", \"UNRelPrec\": " + mat_json(r.UNRelPrec, S, C);
513 s +=
", \"RNRelPrec\": " + mat_json(r.RNRelPrec, S, C);
514 s +=
", \"TNRelPrec\": " + mat_json(r.TNRelPrec, S, C);
517 s +=
" \"impatience\": {";
518 s +=
"\"renegedCustomers\": " + mat_json(r.renegedCustomers, S, C);
519 s +=
", \"avgRenegingWaitTime\": " + mat_json(r.avgRenegingWaitTime, S, C);
520 s +=
", \"renegingRate\": " + mat_json(r.renegingRate, S, C);
521 s +=
", \"balkedCustomers\": " + mat_json(r.balkedCustomers, S, C);
522 s +=
", \"balkingProbability\": " + mat_json(r.balkingProbability, S, C);
523 s +=
", \"retriedCustomers\": " + mat_json(r.retriedCustomers, S, C);
524 s +=
", \"retrialDropped\": " + mat_json(r.retrialDropped, S, C);
525 s +=
", \"avgOrbitSize\": " + mat_json(r.avgOrbitSize, S, C);
545 if (r.UN.rows() != M || r.TN.rows() != M || r.UN.cols() != K || r.TN.cols() != K)
return;
546 bool any_open =
false;
547 for (std::size_t k = 0; k < K; ++k)
548 if (std::isinf(L.
classes[k].population)) any_open =
true;
549 if (!any_open)
return;
550 const double dinf = std::numeric_limits<double>::infinity();
551 for (std::size_t i = 0; i < M; ++i) {
552 const double c = L.
stations[i].nservers;
553 if (std::isinf(c) || c <= 0.0)
continue;
554 if (L.
stations[i].nodetype == line::qn::NodeType::Source)
continue;
555 bool exempt = (i < L.
cap.size() && std::isfinite(L.
cap[i]) && L.
cap[i] >= 0.0);
556 if (!exempt && i < L.
classcap.size())
558 if (std::isfinite(cc) && cc >= 0.0) { exempt =
true;
break; }
560 for (
const auto& rg : L.
regions)
561 if (i < rg.members.size() && rg.members[i]) { exempt =
true;
break; }
566 for (std::size_t k = 0; k < K; ++k)
568 if (exempt)
continue;
569 double lldpeak = 1.0;
570 for (
double v : L.
stations[i].lldscaling)
571 if (!std::isnan(v) && !std::isinf(v) && v > lldpeak) lldpeak = v;
572 std::vector<double> rho(K, 0.0);
573 double rho_open = 0.0, rho_tot = 0.0;
574 bool has_open =
false;
575 for (std::size_t k = 0; k < K; ++k) {
576 const double rate = L.
rates(i, k), t = r.TN(i, k);
577 if (rate > 0.0 && t > 0.0) {
578 rho[k] = t / (c * lldpeak * rate);
580 if (std::isinf(L.
classes[k].population)) {
586 if (!(has_open && rho_open >= 1.0 && rho_tot > 0.0))
continue;
587 for (std::size_t k = 0; k < K; ++k) {
588 r.UN(i, k) = rho[k] / rho_tot;
589 if (std::isinf(L.
classes[k].population)) {
590 if (r.RN.rows() == M && r.RN.cols() == K) r.RN(i, k) = dinf;
591 if (r.QN.rows() == M && r.QN.cols() == K) r.QN(i, k) = dinf;
608 for (std::size_t c = 0; c < L.
nchains; ++c)
609 for (std::size_t i = 0; i < M; ++i) {
611 for (std::size_t k = 0; k < K; ++k) V(i, k) += L.
visits[c](sf - 1, k);
615 for (std::size_t i = 0; i < M; ++i)
616 for (std::size_t k = 0; k < K; ++k) {
617 const bool is_place = L.
stations[i].nodetype == line::qn::NodeType::Place;
618 if ((L.
disabled[i][k] && !is_place) || !(RN(i, k) > 0.0))
continue;
619 if (RN(i, k) < fine) {
624 for (std::size_t cc = 0; cc < L.
nchains; ++cc)
625 if (L.
chains[cc][k]) c = cc;
627 const std::size_t rs = L.
classes[k].refstat;
632 for (std::size_t r : L.
inchain[c]) den += V(rs - 1, r - 1);
634 if (den > 0.0) WN(i, k) = RN(i, k) * V(i, k) / den;
636 for (std::size_t i = 0; i < M; ++i)
637 for (std::size_t k = 0; k < K; ++k)
638 if (WN(i, k) < 10.0 * fine) WN(i, k) = 0.0;
643 std::string s =
"[[";
644 for (std::size_t k = 1; k <= nidx; ++k) {
645 if (k > 1) s +=
", ";
646 s += num(k < m.
rows() ? m(k, 0) : 0.0);
652std::string ln_vec_json(
const std::vector<double>& v) {
653 std::string s =
"[[";
654 for (std::size_t k = 0; k < v.size(); ++k) {
671 double runtime,
bool respt) {
672 const std::size_t n = lsn.
nidx;
679 for (std::size_t k = 1; k <= n; ++k) {
684 std::string s =
"{\"format\": \"ldes-result\", \"version\": \"1.0\", "
685 "\"modelType\": \"LayeredNetwork\", \"solver\": \"LDES\", ";
686 s +=
"\"method\": " + str_json(o.method) +
", \"runtime\": " + num(runtime) +
", ";
688 s += std::string(
"\"stoppingReason\": \"") + (r.
timed_out ?
"max_time" :
"max_events") +
"\", ";
689 s +=
"\"dimensions\": {\"nidx\": " + std::to_string(n) +
690 ", \"nhosts\": " + std::to_string(lsn.
nhosts) +
691 ", \"ntasks\": " + std::to_string(lsn.
ntasks) +
692 ", \"nentries\": " + std::to_string(lsn.
nentries) +
693 ", \"nacts\": " + std::to_string(lsn.
nacts) +
694 ", \"ncalls\": " + std::to_string(lsn.
ncalls) +
695 ", \"tshift\": " + std::to_string(lsn.
tshift) +
696 ", \"eshift\": " + std::to_string(lsn.
eshift) +
697 ", \"ashift\": " + std::to_string(lsn.
ashift) +
", \"names\": [";
698 for (std::size_t k = 1; k <= n; ++k) {
699 if (k > 1) s +=
", ";
700 s += str_json(k < lsn.
names.size() ? lsn.
names[k] :
"Element" + std::to_string(k - 1));
702 s +=
"]}, \"metrics\": {";
703 s +=
"\"QLN\": " + ln_row_json(QLN, n) +
", \"ULN\": " + ln_row_json(ULN, n);
704 s +=
", \"RLN\": " + ln_row_json(RLN, n) +
", \"WLN\": " + ln_row_json(r.
WLN, n);
705 s +=
", \"TLN\": " + ln_row_json(r.
TLN, n) +
", \"ALN\": " + ln_row_json(r.
ALN, n);
706 s +=
", \"ZLN\": " + ln_row_json(r.
ZLN, n);
707 s +=
", \"UCallLN\": " + ln_vec_json(r.
UCallLN) +
", \"TCallLN\": " + ln_vec_json(r.
TCallLN);
708 s +=
", \"UEntryClassLN\": " + ln_vec_json(r.
UEntryClassLN) +
"}, ";
709 s +=
"\"confidenceIntervals\": {\"QLNCI\": null, \"ULNCI\": null, \"RLNCI\": null, "
712 s +=
", \"cacheMetrics\": [";
713 for (std::size_t i = 0; i < r.
cache_task.size(); ++i) {
715 s +=
"{\"cacheTaskIdx\": " + std::to_string(r.
cache_task[i] - 1) +
716 ", \"itemEntryIdx\": " + std::to_string(r.
cache_entry[i] - 1) +
717 ", \"hitProb\": " + num(r.
cache_hit[i]) +
", \"missProb\": " +
724 s +=
", \"entryRespTimeSamples\": [";
741int main(
int argc,
char** argv) {
746 const std::string cmd = argv[1];
747 if (cmd ==
"-h" || cmd ==
"--help") {
751 if (cmd !=
"solve") {
752 std::cerr <<
"Error: unknown command '" << cmd <<
"'. The only command is 'solve'.\n";
755 if (argc < 3) fail(
"solve requires a model file.");
757 const std::string model_path = argv[2];
758 std::string output_path;
760 bool have_samples =
false, have_events =
false;
761 std::vector<std::string> given;
763 for (
int i = 3; i < argc; ++i) {
764 const std::string a = argv[i];
767 output_path = need_value(argc, argv, i, a);
768 }
else if (a ==
"-s" || a ==
"--samples") {
769 o.samples =
static_cast<std::size_t
>(std::atol(need_value(argc, argv, i, a).c_str()));
771 }
else if (a ==
"-e" || a ==
"--maxevents") {
772 o.events =
static_cast<std::size_t
>(std::atol(need_value(argc, argv, i, a).c_str()));
774 }
else if (a ==
"--maxtime") {
775 o.timeout = std::atof(need_value(argc, argv, i, a).c_str());
776 }
else if (a ==
"--seed") {
777 o.seed = std::atol(need_value(argc, argv, i, a).c_str());
778 }
else if (a ==
"--method") {
779 o.method = need_value(argc, argv, i, a);
780 }
else if (a ==
"--cnvgon") {
782 }
else if (a ==
"--cnvgtol") {
783 o.cnvgtol = std::atof(need_value(argc, argv, i, a).c_str());
784 }
else if (a ==
"--cnvgbatch") {
785 o.cnvgbatch = std::atoi(need_value(argc, argv, i, a).c_str());
786 }
else if (a ==
"--cnvgchk") {
787 o.cnvgchk = std::atoi(need_value(argc, argv, i, a).c_str());
788 }
else if (a ==
"--tranfilter") {
789 o.tranfilter = need_value(argc, argv, i, a);
790 }
else if (a ==
"--mserbatch") {
791 o.mserbatch = std::atoi(need_value(argc, argv, i, a).c_str());
792 }
else if (a ==
"--tranobs") {
793 o.tranobs = std::atoi(need_value(argc, argv, i, a).c_str());
794 if (o.tranobs <= 0) fail(
"--tranobs must be positive.");
795 }
else if (a ==
"--warmupfrac") {
796 o.warmupfrac = std::atof(need_value(argc, argv, i, a).c_str());
797 }
else if (a ==
"--cimethod") {
798 o.cimethod = need_value(argc, argv, i, a);
799 }
else if (a ==
"--obmoverlap") {
800 o.obmoverlap = std::atof(need_value(argc, argv, i, a).c_str());
801 }
else if (a ==
"--ciminbatch") {
802 o.ciminbatch = std::atoi(need_value(argc, argv, i, a).c_str());
803 }
else if (a ==
"--ciminobs") {
804 o.ciminobs = std::atoi(need_value(argc, argv, i, a).c_str());
805 }
else if (a ==
"--spectrallowfreqfrac") {
806 o.spectral_low_freq_frac = std::atof(need_value(argc, argv, i, a).c_str());
807 }
else if (a ==
"--slotted") {
809 }
else if (a ==
"--slotlength") {
810 o.slot_length = std::atof(need_value(argc, argv, i, a).c_str());
812 }
else if (a ==
"--replications") {
813 o.replications = std::atoi(need_value(argc, argv, i, a).c_str());
814 }
else if (a ==
"--numthreads") {
815 o.numthreads = std::atoi(need_value(argc, argv, i, a).c_str());
816 }
else if (a ==
"--timespan") {
817 const std::vector<double> t = parse_doubles(need_value(argc, argv, i, a));
818 if (t.size() != 2) fail(
"--timespan takes T0,T1.");
819 o.has_timespan =
true;
822 }
else if (a ==
"--busyperiod") {
823 o.busy_period_orders = std::atoi(need_value(argc, argv, i, a).c_str());
824 }
else if (a ==
"--busyperiod-subnet") {
825 o.busy_period_subnets.push_back(parse_indices(need_value(argc, argv, i, a)));
826 }
else if (a ==
"--initsol") {
827 o.init_sol = parse_doubles(need_value(argc, argv, i, a));
828 }
else if (a ==
"--initjobs") {
829 const std::string v = need_value(argc, argv, i, a);
830 std::vector<std::vector<double>> rows;
832 while (at <= v.size()) {
833 const std::size_t semi = std::min(v.find(
';', at), v.size());
834 if (semi > at) rows.push_back(parse_doubles(v.substr(at, semi - at)));
837 const std::size_t ncols = rows.empty() ? 0 : rows[0].size();
838 for (std::size_t r = 0; r < rows.size(); ++r)
839 if (rows[r].size() != ncols) fail(
"--initjobs rows must have the same length.");
841 for (std::size_t r = 0; r < rows.size(); ++r)
842 for (std::size_t c = 0; c < ncols; ++c) o.init_jobs(r, c) = rows[r][c];
843 }
else if (a ==
"--trackjobs") {
845 }
else if (a ==
"--export-histogram") {
846 o.export_histogram =
true;
847 }
else if (a ==
"--trajectory") {
848 o.export_trajectory =
true;
849 }
else if (a ==
"--respt-samples") {
850 o.export_respt =
true;
851 }
else if (a ==
"--sample-path") {
853 if (i + 1 >= argc) fail(
"--sample-path requires the number of rows.");
854 const std::string v(argv[++i]);
857 const long n = std::strtol(v.c_str(), &end, 10);
858 if (v.empty() || *end !=
'\0' || errno == ERANGE || n > 2147483647L || n < -2147483648L ||
859 std::isspace(
static_cast<unsigned char>(v[0])))
860 fail(
"Invalid sample-path value.");
861 if (n <= 0) fail(
"--sample-path requires a positive number of rows.");
862 o.sample_path_events =
static_cast<int>(n);
863 }
else if (a ==
"--rest") {
864 refuse(a,
"server mode belongs to line-cli, not to this binary");
865 }
else if (a ==
"-h" || a ==
"--help") {
869 fail(
"unknown option '" + a +
"'.");
872 if (have_events && !have_samples) o.samples = o.events;
883 if (o.sample_path_events > 0)
884 fail(
"--sample-path is a Network-path option; the layered engine has no LINE State "
885 "layout to record.");
886 static const char*
const network_only[] = {
887 "--timespan",
"--tranobs",
"--trajectory",
"--export-histogram",
"--busyperiod",
888 "--busyperiod-subnet",
"--initsol",
"--initjobs",
"--trackjobs",
"--slotted",
889 "--slotlength",
"--replications"};
890 for (
const std::string& g : given)
891 for (
const char* f : network_only)
893 fail(g +
" is a Network-path option; the layered engine reports the steady "
894 "state of one replication only.");
895 if (o.method !=
"default" && o.method !=
"ssj")
896 fail(
"method '" + o.method +
"' is not a layered LDES method; use 'default' or 'ssj'.");
899 const std::chrono::steady_clock::time_point t0 = std::chrono::steady_clock::now();
901 const double runtime =
902 std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
903 const std::string doc = ln_result_json(lsn, r, o, runtime, o.export_respt);
904 if (output_path.empty()) {
907 std::ofstream out(output_path.c_str());
908 if (!out) fail(
"cannot write '" + output_path +
"'.");
911 std::cerr <<
"LDES analysis [method: default; type: approximate, randomized; lang: cpp; "
912 "native LN engine] completed in " << runtime <<
"s. Iterations: "
917 if (o.sample_path_events > 0) o.replications = 1;
923 const std::chrono::steady_clock::time_point t_start = std::chrono::steady_clock::now();
925 const double runtime =
926 std::chrono::duration<double>(std::chrono::steady_clock::now() - t_start).count();
933 r.WN = ldes_residt_from_respt(sn, r.RN);
940 if (r.RNCI.rows() == r.RN.rows() && r.RNCI.cols() == r.RN.cols() &&
941 r.WN.rows() == r.RN.rows() && r.WN.cols() == r.RN.cols()) {
943 for (std::size_t i = 0; i < r.RN.rows(); ++i)
944 for (std::size_t k = 0; k < r.RN.cols(); ++k) {
945 const double rn = r.RN(i, k), wn = r.WN(i, k);
946 if (rn > 0.0 && std::isfinite(rn) && std::isfinite(wn)) r.WNCI(i, k) = r.RNCI(i, k) * (wn / rn);
949 ldes_cap_unstable_open_stations(sn, r);
954 const std::string doc = result_json(r, o, runtime,
955 static_cast<std::size_t
>(std::max<long long>(0, r.total_simulated_events)));
956 if (output_path.empty()) {
959 std::ofstream out(output_path.c_str());
960 if (!out) fail(
"cannot write '" + output_path +
"'.");
963 std::cerr <<
"LDES analysis [method: " << o.method
964 <<
"; type: approximate, randomized; lang: cpp] completed in " << runtime
965 <<
"s. Iterations: " << o.samples <<
".\n";
968 std::cerr <<
"Error: [" << e.
id() <<
"] " << e.what() <<
"\n";
972 std::cerr <<
"Error: [" << e.
id() <<
"] " << e.what() <<
"\n";
974 }
catch (
const std::exception& e) {
975 std::cerr <<
"Error: " << e.what() <<
"\n";
Base error for the multiprecision C++ port.
const std::string & id() const noexcept
The full identifier, e.g.
A refusal: the feature, method or analysis is not covered: LINE:unsupported:<kind>.
A network plus its refreshed NetworkStruct.
std::size_t stateful_of_station(std::size_t st) const
std::vector< std::vector< bool > > chains
(nchains x nclasses)
std::vector< std::size_t > refclass
(nchains) 1-based class, 0 = none
std::vector< std::vector< bool > > disabled
Matrix< double > procid() const
sn.procid (M x K): the ProcessType id of each (station, class) pair, as refreshProcessTypes....
std::vector< double > cap
sn.cap and sn.classcap: the total and per-class buffers.
std::vector< JobClass > classes
std::vector< Station< T > > stations
stations[k-1] is the k-th station
Matrix< T > rates
(nstations x nclasses) service rates and SCVs, with a PARALLEL disabled flag instead of MATLAB's NaN ...
std::vector< std::vector< std::size_t > > inchain
1-based class indices per chain
std::vector< std::vector< double > > classcap
std::vector< Matrix< T > > visits
(nchains) each (nstateful x nclasses)
std::vector< Region > regions
A queueing network under construction.
const NetworkStruct< T > & get_struct()
The refreshed struct, MATLAB's model.getStruct().
The exception types the port throws.
int main(int argc, char **argv)
The NATIVE LDES discrete-event engine.
The NATIVE LDES engine for LAYERED (LQN) models, the C++ twin of jline/solvers/ldes/handlers/Solver_s...
The option and result records of SolverLDES, the discrete-event simulator.
model.json with type: "LayeredNetwork" -> LqnStruct, via LqnBuilder.
bool is_layered_json(const std::string &path)
True when a file is a LayeredNetwork model.json rather than an .lqnx.
qn::Network< T > read_network_json(const std::string &path)
Parse a model.json file into a qn::Network<T>.
lqn::LqnStruct< T > read_layered_model(const std::string &path)
Read a layered model from any of its interchanges: the LINE model.json, the LQNS ....
bool process_is_batch(ProcessType p)
True when an EVENT of this process releases (or, as a service process, completes) a BATCH of jobs who...
ImpatienceType
Impatience kinds, with the values of MATLAB ImpatienceType.
LdesResult ldes_engine_solve(const qn::NetworkStruct< T > &sn, const LdesOptions &o)
Simulate sn, over independent REPLICATIONS when options.replications > 1.
engine::LnResult ldes_ln_engine_solve(const lqn::LqnStruct< T > &lsn, const LdesOptions &o)
Simulate a layered model in process.
void fail(const std::string &msg)
void sn_arvr_state_dependent_balance(const qn::NetworkStruct< T > &L, Matrix< T > &AN, const Matrix< T > &TN, Matrix< T > *WN)
void refuse(const std::string &m)
Reader for the LINE model.json interchange (a Network model) into a qn::Network<T> built through the ...
Arrival rates at stations fed by a JSQ or SQ(d) dispatcher, from flow balance.
static constexpr double FineTol
Per-cache hit/miss/latency, as the cacheMetrics block carries them.
Matrix< double > listCost
(1 x nlists) mean storage cost held per list
Matrix< double > latency
(1 x nclasses) expected retrieval latency
Matrix< double > hitList
(nclasses x nlists) hit probability by list
Matrix< double > itemProb
(nitems x nlists+1) item position law
Matrix< double > delayed
(1 x nclasses) delayed-hit probability
Matrix< double > miss
(1 x nclasses) miss probability
Matrix< double > hit
(1 x nclasses) hit probability
The knobs of one LDES run.
One ldes-result document, parsed.
The samplePath block (--sample-path N): one simulated path in SolverSSA's row convention.
std::vector< Matrix< double > > aggr
[stateful] per-class counts per row (Inf at a source).
std::vector< std::vector< double > > events
Rows {step, time, type, node, class, jobId, phase}.
std::vector< int > stateful_nodes
0-based node index of each stateful node, in stateful order.
std::vector< std::vector< int > > phases
[stateful][class] phase count used for the state layout.
std::vector< Matrix< double > > state
[stateful] detailed rows (empty when refused), left-padded to equal width.
std::vector< std::string > refusals
[stateful] why the detailed state is not recorded, empty when it is.
long long unbound_scripts
Drawn intervals no phase script could be bound to (recorded as one phase).
The layered result: per element, the mean measures.
std::vector< double > cache_read_rate
Matrix< double > ZLN
Think time per element, Solver_ssj_ln's ZLN: Little's law on a task's thread pool read backwards,...
bool timed_out
The run stopped on its wall-clock budget (LdesOptions::timeout) before max_events.
std::vector< double > cache_miss
Matrix< double > ALN
Arrival rate per element, Solver_ssj_ln's ALN: an entry's request rate (its throughput,...
std::vector< double > UEntryClassLN
Matrix< double > WLN
Residence time per element, the ResidT column: the time an activity holds ITS HOST PROCESSOR per visi...
std::vector< std::size_t > cache_entry
std::vector< double > cache_hit
std::vector< std::vector< double > > entry_resp_samples
Every per-request ENTRY response time observed, one vector per entry in LOCAL index space (0....
std::vector< std::size_t > cache_task
Cache measures, one row per ItemEntry that was read, pooled over the replicas of its cache task as So...
long long events
Activity completions over the whole run, warmup included: what the budget counts.
std::vector< double > TCallLN
Dispatches of each call per unit time, counted where the CALLER issues it (TCallLN[c-1]),...
std::vector< double > cache_delayed
std::vector< double > UCallLN
Per-CLASS occupancy of the task thread pools, Solver_ssj_ln's UCallLN and UEntryClassLN: the mean num...
std::vector< std::string > names
(nidx+1) declared name
double host_reach_mult(std::size_t idx) const
Servers of the host of element idx that its work can reach: the divisor of every LQN Util column (Sol...