LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
api_dispatch_pfqn_b.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 *
5 * --api dispatch entries for the 'pfqn_b' group (see api_dispatch_detail.h).
6 *
7 * Conventions shared by every entry below:
8 *
9 * - Argument keys are the MATLAB parameter names of matlab/src/api/pfqn/<fn>.m.
10 * Output keys are the MATLAB output names; a field the port returns beyond
11 * them keeps its C++ name.
12 *
13 * - STATION, CLASS AND NODE INDICES ARE 0-BASED, exactly as the port takes and
14 * returns them and as the rest of the --api table already carries them
15 * (mc's partitions and SCC labels). They pass through unchanged.
16 *
17 * - An open class in a population vector and an infinite server in a server
18 * count vector are written "Inf" (the MATLAB spelling) or -1 (the port's
19 * OPEN_CLASS / INF_SERVERS sentinel).
20 *
21 * - A port whose optional trailing arguments exist only as a FULL overload
22 * next to a SHORT one takes them together: absent all, the short overload
23 * runs; given some but not all, the call is refused naming what is missing.
24 * No default value is restated here.
25 *
26 * - Scalar tolerances that the port types as `double` are read as double at
27 * every arithmetic, because that is what the port consumes.
28 */
29
30#include "api_dispatch_detail.h"
31
32#include <cctype>
33#include <cmath>
34#include <cstdint>
35#include <initializer_list>
36#include <set>
37#include <string>
38#include <utility>
39#include <vector>
40
112
113namespace line {
114namespace reg {
115
116namespace {
117
118// ---------------------------------------------------------------------------
119// Shared readers
120// ---------------------------------------------------------------------------
121
122/**
123 * Integer vector in which "Inf" (any case) or -1 stands for the port's -1
124 * sentinel: OPEN_CLASS in a population, INF_SERVERS in a server count.
125 */
126std::vector<int> ints_with_inf(const Json& j, const std::string& where) {
127 std::vector<Json> flat;
128 if (!j.is_array()) {
129 flat.push_back(j);
130 } else {
131 for (const Json& e : j) {
132 if (e.is_array())
133 for (const Json& f : e) flat.push_back(f);
134 else
135 flat.push_back(e);
136 }
137 }
138 std::vector<int> out;
139 out.reserve(flat.size());
140 for (const Json& e : flat) {
141 if (e.is_string()) {
142 std::string s = e.get<std::string>();
143 for (char& c : s) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
144 if (s == "inf" || s == "+inf" || s == "infinity") {
145 out.push_back(-1);
146 continue;
147 }
148 }
149 const std::vector<int> one = int_vector_from_json(e, where);
150 if (one.size() != 1) throw InputError(where + ": expected an integer or \"Inf\"");
151 if (one[0] < -1)
152 throw InputError(where + ": a negative entry other than -1 has no meaning, got " +
153 std::to_string(one[0]));
154 out.push_back(one[0]);
155 }
156 return out;
157}
158
159std::vector<int> pop_with_open(Args& a, const char* key) {
160 return ints_with_inf(a.get(key), std::string("'") + key + "'");
161}
162
163std::vector<int> pop_with_open_or_empty(Args& a, const char* key) {
164 const Json* v = a.opt(key);
165 return v ? ints_with_inf(*v, std::string("'") + key + "'") : std::vector<int>();
166}
167
168/** Integer matrix: a list of rows (or one row, or a scalar). */
169Matrix<int> int_matrix(Args& a, const char* key) {
170 const Json& j = a.get(key);
171 const std::string where = std::string("'") + key + "'";
172 std::vector<std::vector<int> > rows;
173 if (j.is_array() && !j.empty() && j[0].is_array()) {
174 for (const Json& r : j) rows.push_back(int_vector_from_json(r, where));
175 } else if (!(j.is_array() && j.empty())) {
176 rows.push_back(int_vector_from_json(j, where));
177 }
178 const std::size_t m = rows.size(), n = m ? rows[0].size() : 0;
179 Matrix<int> M(m, n);
180 for (std::size_t i = 0; i < m; ++i) {
181 if (rows[i].size() != n) throw InputError(where + ": rows have different lengths");
182 for (std::size_t k = 0; k < n; ++k) M(i, k) = rows[i][k];
183 }
184 return M;
185}
186
187/** 0-based index list. */
188std::vector<std::size_t> indices(Args& a, const char* key) {
189 const std::vector<int> v = a.ints(key);
190 std::vector<std::size_t> out;
191 out.reserve(v.size());
192 for (int x : v) {
193 if (x < 0)
194 throw InputError(std::string("'") + key + "' holds 0-based indices and cannot be "
195 "negative, got " + std::to_string(x));
196 out.push_back(static_cast<std::size_t>(x));
197 }
198 return out;
199}
200
201std::vector<long> longs(Args& a, const char* key) {
202 const std::vector<int> v = a.ints(key);
203 return std::vector<long>(v.begin(), v.end());
204}
205
206long a_long(Args& a, const char* key) { return static_cast<long>(a.required_integer(key)); }
207
208/** Vector of doubles, "Inf" accepted (strtod reads it). */
209std::vector<double> doubles(Args& a, const char* key) { return a.vector<double>(key); }
210
211std::vector<bool> bools(Args& a, const char* key) {
212 const Json& j = a.get(key);
213 std::vector<bool> out;
214 auto push = [&](const Json& e) {
215 if (e.is_boolean()) {
216 out.push_back(e.get<bool>());
217 return;
218 }
219 const std::vector<int> v = int_vector_from_json(e, std::string("'") + key + "'");
220 for (int x : v) {
221 if (x != 0 && x != 1)
222 throw InputError(std::string("'") + key + "' entries must be true, false, 0 or 1");
223 out.push_back(x != 0);
224 }
225 };
226 if (j.is_array())
227 for (const Json& e : j) push(e);
228 else
229 push(j);
230 return out;
231}
232
233/**
234 * How many of an ordered list of trailing optional arguments are given. A key
235 * present after an absent one is refused: the port takes them positionally,
236 * and filling the gap would mean restating the port's default here.
237 */
238std::size_t given_prefix(Args& a, const char* fn, std::initializer_list<const char*> keys) {
239 std::size_t k = 0;
240 bool gap = false;
241 std::string missing;
242 for (const char* key : keys) {
243 if (a.has(key)) {
244 if (gap)
245 throw InputError(std::string(fn) + ": '" + key + "' is positional after " +
246 missing + ", which must then be given as well");
247 ++k;
248 } else {
249 if (!gap) missing = std::string("'") + key + "'";
250 gap = true;
251 }
252 }
253 return k;
254}
255
256/** All-or-nothing group of arguments taken together by the port's long form. */
257bool all_or_none(Args& a, const char* fn, std::initializer_list<const char*> keys) {
258 std::size_t n = 0;
259 std::string names, missing;
260 for (const char* key : keys) {
261 names += (names.empty() ? "'" : ", '") + std::string(key) + "'";
262 if (a.has(key))
263 ++n;
264 else
265 missing += (missing.empty() ? "'" : ", '") + std::string(key) + "'";
266 }
267 if (n == 0) return false;
268 if (n == keys.size()) return true;
269 throw InputError(std::string(fn) + ": arguments " + names +
270 " are taken together by the port's long form; missing " + missing);
271}
272
273/** An `options` object with its own unknown-key check. */
274class OptObj {
275public:
276 OptObj(Args& a, const char* key, const char* fn) : j_(a.opt(key)), fn_(fn), key_(key) {
277 if (j_ && !j_->is_object())
278 throw InputError(fn_ + ": '" + key_ + "' must be an object");
279 }
280 OptObj(const Json* j, std::string fn, std::string key) : j_(j), fn_(std::move(fn)), key_(std::move(key)) {
281 if (j_ && !j_->is_object())
282 throw InputError(fn_ + ": '" + key_ + "' must be an object");
283 }
284 bool given() const { return j_ != nullptr; }
285 bool has(const char* k) const { return j_ && j_->find(k) != j_->end(); }
286 const Json& get(const char* k) {
287 seen_.insert(k);
288 if (!has(k)) throw InputError(fn_ + ": missing required field '" + key_ + "." + k + "'");
289 return *j_->find(k);
290 }
291 const Json* sub(const char* k) {
292 seen_.insert(k);
293 return has(k) ? &*j_->find(k) : nullptr;
294 }
295 std::string where(const char* k) const { return fn_ + ": " + key_ + "." + k; }
296 template <class T>
297 T number(const char* k) {
298 return number_from_json<T>(get(k), where(k));
299 }
300 std::string text(const char* k) {
301 const Json& v = get(k);
302 if (!v.is_string()) throw InputError(where(k) + " must be a string");
303 return v.get<std::string>();
304 }
305 std::size_t count(const char* k) {
306 const std::vector<int> v = int_vector_from_json(get(k), where(k));
307 if (v.size() != 1 || v[0] < 0)
308 throw InputError(where(k) + " must be a single non-negative integer");
309 return static_cast<std::size_t>(v[0]);
310 }
311 void done() const {
312 if (!j_) return;
313 for (auto it = j_->begin(); it != j_->end(); ++it)
314 if (seen_.find(it.key()) == seen_.end())
315 throw InputError(fn_ + ": unknown field '" + key_ + "." + it.key() + "'");
316 }
317
318private:
319 const Json* j_;
320 std::string fn_;
321 std::string key_;
322 std::set<std::string> seen_;
323};
324
325/**
326 * The SolverNC option fields the pfqn_nc family reads: samples, seed and
327 * options.config.{aghq_nodes, mcmc_batches, mcmc_burnin}. Each field that is
328 * absent keeps NcOptions' own default.
329 */
330pfqn::NcOptions nc_options(OptObj& o, const char* fn) {
331 pfqn::NcOptions n;
332 if (o.has("samples")) n.samples = o.count("samples");
333 if (o.has("seed")) n.seed = static_cast<unsigned long>(o.count("seed"));
334 OptObj cfg(o.sub("config"), fn, "options.config");
335 if (cfg.has("aghq_nodes")) n.aghq_nodes = cfg.count("aghq_nodes");
336 if (cfg.has("mcmc_batches")) n.mcmc_batches = cfg.count("mcmc_batches");
337 if (cfg.has("mcmc_burnin")) n.mcmc_burnin = cfg.number<double>("mcmc_burnin");
338 cfg.done();
339 return n;
340}
341
342template <class E>
343E enum_of(const std::string& fn, const char* key, const std::string& value,
344 std::initializer_list<std::pair<const char*, E>> table) {
345 std::string low = value;
346 for (char& c : low) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
347 std::string accepted;
348 for (const auto& p : table) {
349 if (low == p.first) return p.second;
350 accepted += (accepted.empty() ? "" : ", ") + std::string(p.first);
351 }
352 throw InputError(fn + ": unknown " + key + " '" + value + "'; accepted: " + accepted);
353}
354
355std::vector<std::string> strings(Args& a, const char* key) {
356 const Json& j = a.get(key);
357 std::vector<std::string> out;
358 if (j.is_string()) {
359 out.push_back(j.get<std::string>());
360 return out;
361 }
362 if (!j.is_array()) throw InputError(std::string("'") + key + "' must be a list of strings");
363 for (const Json& e : j) {
364 if (!e.is_string()) throw InputError(std::string("'") + key + "' must be a list of strings");
365 out.push_back(e.get<std::string>());
366 }
367 return out;
368}
369
370/** Per-station discipline, "PS", "FCFS" or "INF" (SchedStrategy names). */
371std::vector<pfqn::SchedStrategy> sched_list(Args& a, const char* key, const char* fn) {
372 std::vector<pfqn::SchedStrategy> out;
373 for (const std::string& s : strings(a, key))
374 out.push_back(enum_of<pfqn::SchedStrategy>(fn, key, s,
377 {"inf", pfqn::SchedStrategy::INF}}));
378 return out;
379}
380
381std::vector<pfqn::AmvaSched> amva_sched_list(Args& a, const char* key, const char* fn) {
382 std::vector<pfqn::AmvaSched> out;
383 for (const std::string& s : strings(a, key))
384 out.push_back(enum_of<pfqn::AmvaSched>(fn, key, s,
385 {{"ps", pfqn::AmvaSched::PS},
386 {"fcfs", pfqn::AmvaSched::FCFS},
387 {"inf", pfqn::AmvaSched::INF}}));
388 return out;
389}
390
391Json enc_idx(const std::vector<std::size_t>& v) {
392 Json j = Json::array();
393 for (std::size_t x : v) j.push_back(static_cast<std::uint64_t>(x));
394 return j;
395}
396
397Json enc_bools(const std::vector<bool>& v) {
398 Json j = Json::array();
399 for (bool b : v) j.push_back(b);
400 return j;
401}
402
403/** An N-d array as {"sz": extents, "data": column-major entries}, MATLAB's layout. */
404template <class T>
405Json enc_tensor(const moment::MomentTensor<T>& t) {
406 Json j;
407 j["sz"] = enc_idx(t.sz);
408 j["data"] = encode_vector(t.data);
409 return j;
410}
411
412// ---------------------------------------------------------------------------
413// Result encoders shared by families
414// ---------------------------------------------------------------------------
415
416/** MvaResult under MATLAB's names; `lgkey` is lG or lGN as the MATLAB file says. */
417template <class T>
418Json mva_out(const pfqn::MvaResult<T>& r, const char* lgkey) {
419 Json out;
420 out["XN"] = encode_vector(r.XN);
421 out["QN"] = encode_matrix(r.QN);
422 out["UN"] = encode_matrix(r.UN);
423 out["CN"] = encode_matrix(r.CN);
424 out["G"] = encode_scalar(r.G);
425 out[lgkey] = r.lG;
426 return out;
427}
428
429template <class T>
430Json amva_out(const pfqn::AmvaResult<T>& r, const char* itkey) {
431 Json out;
432 out["XN"] = encode_vector(r.XN);
433 out["QN"] = encode_matrix(r.QN);
434 out["UN"] = encode_matrix(r.UN);
435 out["RN"] = encode_matrix(r.RN);
436 out[itkey] = encode_count(r.iterations);
437 out["converged"] = r.converged;
438 return out;
439}
440
441template <class T>
442Json linearizer_out(const pfqn::LinearizerResult<T>& r) {
443 Json out;
444 out["Q"] = encode_matrix(r.Q);
445 out["U"] = encode_matrix(r.U);
446 out["W"] = encode_matrix(r.W);
447 out["C"] = encode_vector(r.C);
448 out["X"] = encode_vector(r.X);
449 out["totiter"] = r.totiter;
450 return out;
451}
452
453template <class R>
454Json schmidt_out(const R& r) {
455 Json out;
456 out["XN"] = encode_vector(r.XN);
457 out["QN"] = encode_matrix(r.QN);
458 out["UN"] = encode_matrix(r.UN);
459 out["CN"] = encode_matrix(r.CN);
460 return out;
461}
462
463// ---------------------------------------------------------------------------
464// Normalizing constants
465// ---------------------------------------------------------------------------
466
467struct OpManjunath {
468 template <class T>
469 static Json run(Args& a) {
470 const Matrix<T> L = a.matrix<T>("L");
471 const std::vector<int> N = a.ints("N");
472 pfqn::PfqnManjunathOptions opt;
473 // MATLAB computes the per-class decomposition when a fourth output is
474 // requested; JSON has no nargout, so it is asked for by name.
475 if (a.has("stats")) opt.stats = a.boolean("stats", opt.stats);
476 const std::size_t k = given_prefix(a, "pfqn_manjunath", {"Z", "A", "b", "sense"});
477 pfqn::PfqnManjunathResult<T> r;
478 if (k == 0) {
479 a.done();
480 r = pfqn::pfqn_manjunath(L, N, opt);
481 } else if (k == 1) {
482 const Matrix<T> Z = a.matrix<T>("Z");
483 a.done();
484 r = pfqn::pfqn_manjunath(L, N, Z, opt);
485 } else {
486 if (k < 4)
487 throw InputError("pfqn_manjunath: 'A', 'b' and 'sense' describe the constraint "
488 "rows together; give all three");
489 const Matrix<T> Z = a.matrix<T>("Z");
490 const Matrix<T> A = a.matrix<T>("A");
491 const std::vector<long> b = longs(a, "b");
492 const std::string sense = a.text("sense", "");
493 a.done();
494 r = pfqn::pfqn_manjunath(L, N, Z, A, b, sense, opt);
495 }
496 Json out;
497 out["G"] = encode_scalar(r.G);
498 out["lG"] = r.lG;
499 out["peak"] = encode_count(r.peak_states);
500 if (r.has_stats) {
501 Json s;
502 s["Q"] = encode_vector(r.Q);
503 s["X"] = encode_vector(r.X);
504 s["U"] = encode_vector(r.U);
505 s["think"] = encode_vector(r.think);
506 s["blocked"] = encode_vector(r.blocked);
507 s["delay"] = encode_vector(r.delay);
508 out["stats"] = s;
509 }
510 return out;
511 }
512};
513
514/**
515 * pfqn_nc: options.method selects the algorithm. Without lambda and with no
516 * option other than the method, the port's (L, N, Z, method) form runs; any
517 * other call is its long form, whose absolute tolerance is options.tol (MATLAB
518 * reads the same field for both the filter and comomrm's tolerance).
519 */
520struct OpNc {
521 template <class T>
522 static Json run(Args& a) {
523 const std::vector<T> lambda = a.vector_or_empty<T>("lambda");
524 const Matrix<T> L = a.matrix<T>("L");
525 const std::vector<int> N = pop_with_open(a, "N");
526 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
527 OptObj o(a, "options", "pfqn_nc");
528 if (!o.given()) throw InputError("pfqn_nc: missing required argument 'options' (with field 'method')");
529 const pfqn::NcMethod method = pfqn::nc_method_of(o.text("method"));
530 const bool longform = !lambda.empty() || o.has("tol") || o.has("samples") ||
531 o.has("seed") || o.has("config");
532 pfqn::NcDispatchResult<T> r;
533 if (!longform) {
534 o.done();
535 a.done();
536 r = pfqn::pfqn_nc(L, N, Z, method);
537 } else {
538 if (!o.has("tol"))
539 throw InputError("pfqn_nc: the port's long form (open classes or sampling "
540 "options) takes the absolute tolerance options.tol explicitly");
541 const T atol = o.number<T>("tol");
542 pfqn::NcOptions nopt = nc_options(o, "pfqn_nc");
543 nopt.tol = num_traits<T>::to_double(atol);
544 o.done();
545 a.done();
546 r = pfqn::pfqn_nc(lambda, L, N, Z, method, atol, nopt);
547 }
548 Json out;
549 out["lG"] = r.lG;
550 out["G"] = encode_scalar(r.G);
551 out["X"] = encode_vector(r.X);
552 out["Q"] = encode_matrix(r.Q);
553 out["method"] = r.method;
554 out["valid"] = r.valid;
555 return out;
556 }
557};
558
559struct OpNcSanitize {
560 template <class T>
561 static Json run(Args& a) {
562 const bool lng = all_or_none(a, "pfqn_nc_sanitize", {"lambda", "atol"});
563 pfqn::NcSanitizeResult<T> r;
564 if (lng) {
565 const std::vector<T> lambda = a.vector_or_empty<T>("lambda");
566 const Matrix<T> L = a.matrix<T>("L");
567 const std::vector<int> N = pop_with_open(a, "N");
568 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
569 const T atol = a.number<T>("atol");
570 a.done();
571 r = pfqn::pfqn_nc_sanitize(lambda, L, N, Z, atol);
572 } else {
573 const Matrix<T> L = a.matrix<T>("L");
574 const std::vector<int> N = pop_with_open(a, "N");
575 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
576 a.done();
577 r = pfqn::pfqn_nc_sanitize(L, N, Z);
578 }
579 Json out;
580 out["lambda"] = encode_vector(r.lambda);
581 out["L"] = encode_matrix(r.L);
582 out["N"] = encode_ints(r.N);
583 out["Z"] = encode_matrix(r.Z);
584 out["lGremaind"] = r.lGremaind;
585 out["Gremaind"] = encode_scalar(r.Gremaind);
586 out["classIndex"] = enc_idx(r.classIndex);
587 return out;
588 }
589};
590
591struct OpNcld {
592 template <class T>
593 static Json run(Args& a) {
594 const Matrix<T> L = a.matrix<T>("L");
595 const std::vector<int> N = a.ints("N");
596 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
597 const Matrix<T> mu = a.matrix<T>("mu");
598 OptObj o(a, "options", "pfqn_ncld");
599 pfqn::NcldResult<T> r;
600 if (!o.given()) {
601 a.done();
602 r = pfqn::pfqn_ncld(L, N, Z, mu);
603 } else {
604 const std::string method = o.text("method");
605 const T atol = o.number<T>("tol");
606 pfqn::NcOptions nopt = nc_options(o, "pfqn_ncld");
607 nopt.tol = num_traits<T>::to_double(atol);
608 o.done();
609 a.done();
610 r = pfqn::pfqn_ncld(L, N, Z, mu, method, atol, nopt);
611 }
612 Json out;
613 out["lG"] = r.lG;
614 out["G"] = encode_scalar(r.G);
615 out["method"] = r.method;
616 return out;
617 }
618};
619
620struct OpNcldmx {
621 template <class T>
622 static Json run(Args& a) {
623 const std::vector<T> lambda = a.vector<T>("lambda");
624 const Matrix<T> D = a.matrix<T>("D");
625 const std::vector<int> N = pop_with_open(a, "N");
626 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
627 const Matrix<T> mu = a.matrix<T>("mu");
628 OptObj o(a, "options", "pfqn_ncldmx");
629 pfqn::NcldmxResult<T> r;
630 if (!o.given()) {
631 a.done();
632 r = pfqn::pfqn_ncldmx(lambda, D, N, Z, mu);
633 } else {
634 const std::string method = o.text("method");
635 const T atol = o.number<T>("tol");
636 pfqn::NcOptions nopt = nc_options(o, "pfqn_ncldmx");
637 nopt.tol = num_traits<T>::to_double(atol);
638 o.done();
639 a.done();
640 r = pfqn::pfqn_ncldmx(lambda, D, N, Z, mu, method, atol, nopt);
641 }
642 Json out;
643 out["lG"] = r.lG;
644 out["G"] = encode_scalar(r.G);
645 out["lGopen"] = r.lGopen;
646 out["Gopen"] = encode_scalar(r.Gopen);
647 out["XN"] = encode_vector(r.XN);
648 out["QN"] = encode_matrix(r.QN);
649 out["EC"] = encode_matrix(r.EC);
650 return out;
651 }
652};
653
654struct OpPerm {
655 template <class T>
656 static Json run(Args& a) {
657 const Matrix<T> A = a.matrix<T>("A");
658 const std::vector<int> m = a.ints("m");
659 a.done();
660 Json out;
661 out["val"] = encode_scalar(pfqn::pfqn_perm(A, m));
662 return out;
663 }
664};
665
666struct OpMmint2 {
667 template <class T>
668 static Json run(Args& a) {
669 const std::vector<T> L = a.vector<T>("L");
670 const std::vector<T> N = a.vector<T>("N");
671 const std::vector<T> Z = a.vector<T>("Z");
672 a.done();
673 const pfqn::MmintResult<T> r = pfqn::pfqn_mmint2(L, N, Z);
674 Json out;
675 out["G"] = encode_scalar(r.G);
676 out["lG"] = encode_scalar(r.lG);
677 return out;
678 }
679};
680
681/** pfqn_nre / pfqn_nrl / pfqn_nrp: lG from (L, N, Z, alpha). */
682template <class T>
683using NrFn = T (*)(const Matrix<T>&, const std::vector<T>&, const std::vector<T>&,
684 const Matrix<T>&);
685
686template <template <class> class F>
687struct OpNr {
688 template <class T>
689 static Json run(Args& a) {
690 const Matrix<T> L = a.matrix<T>("L");
691 const std::vector<T> N = a.vector<T>("N");
692 const std::vector<T> Z = a.vector<T>("Z");
693 const Matrix<T> alpha = a.matrix<T>("alpha");
694 a.done();
695 Json out;
696 out["lG"] = encode_scalar(F<T>::call(L, N, Z, alpha));
697 return out;
698 }
699};
700template <class T>
701struct NreCall {
702 static T call(const Matrix<T>& L, const std::vector<T>& N, const std::vector<T>& Z,
703 const Matrix<T>& al) {
704 return pfqn::pfqn_nre(L, N, Z, al);
705 }
706};
707template <class T>
708struct NrlCall {
709 static T call(const Matrix<T>& L, const std::vector<T>& N, const std::vector<T>& Z,
710 const Matrix<T>& al) {
711 return pfqn::pfqn_nrl(L, N, Z, al);
712 }
713};
714template <class T>
715struct NrpCall {
716 static T call(const Matrix<T>& L, const std::vector<T>& N, const std::vector<T>& Z,
717 const Matrix<T>& al) {
718 return pfqn::pfqn_nrp(L, N, Z, al);
719 }
720};
721
722struct OpOiFnc {
723 template <class T>
724 static Json run(Args& a) {
725 const std::vector<T> Phi = a.vector<T>("Phi");
726 const std::vector<int> N = a.ints("N");
727 a.unsupported("f", "the per-state function f is a callable, which has no JSON "
728 "representation; call pfqn_oi_fnc from the library for that case");
729 a.done();
730 const pfqn::OiFncResult<T> r = pfqn::pfqn_oi_fnc(Phi, N);
731 Json out;
732 out["Psi"] = encode_vector(r.Psi);
733 out["mu"] = encode_vector(r.mu);
734 out["stride"] = enc_idx(r.stride);
735 return out;
736 }
737};
738
739struct OpPanacea {
740 template <class T>
741 static Json run(Args& a) {
742 const Matrix<T> L = a.matrix<T>("L");
743 const std::vector<int> N = a.ints("N");
744 const std::vector<T> Z = a.vector<T>("Z");
745 pfqn::PanaceaResult<T> r;
746 if (a.has("terms")) {
747 const int terms = a.required_integer("terms");
748 a.done();
749 r = pfqn::pfqn_panacea(L, N, Z, terms);
750 } else {
751 a.done();
752 r = pfqn::pfqn_panacea(L, N, Z);
753 }
754 Json out;
755 out["Gn"] = encode_scalar(r.G);
756 out["lGn"] = encode_scalar(r.lG);
757 out["normalUsage"] = r.normalUsage;
758 return out;
759 }
760};
761
762struct OpPanaceald {
763 template <class T>
764 static Json run(Args& a) {
765 const Matrix<T> L = a.matrix<T>("L");
766 const std::vector<int> N = a.ints("N");
767 const std::vector<T> Z = a.vector<T>("Z");
768 const Matrix<T> mu = a.matrix<T>("mu");
769 pfqn::PanaceaLdResult<T> r;
770 if (a.has("terms")) {
771 const int terms = a.required_integer("terms");
772 a.done();
773 r = pfqn::pfqn_panaceald(L, N, Z, mu, terms);
774 } else {
775 a.done();
776 r = pfqn::pfqn_panaceald(L, N, Z, mu);
777 }
778 Json out;
779 out["Gn"] = encode_scalar(r.G);
780 out["lGn"] = encode_scalar(r.lG);
781 out["normalUsage"] = r.normalUsage;
782 out["reason"] = r.reason ? std::string(r.reason) : std::string();
783 return out;
784 }
785};
786
787struct OpPropfair {
788 template <class T>
789 static Json run(Args& a) {
790 const Matrix<T> L = a.matrix<T>("L");
791 const std::vector<T> N = a.vector<T>("N");
792 const std::vector<T> Z = a.vector<T>("Z");
793 a.done();
794 const pfqn::PropfairResult<T> r = pfqn::pfqn_propfair(L, N, Z);
795 Json out;
796 out["G"] = encode_scalar(r.G);
797 out["lG"] = encode_scalar(r.lG);
798 out["Xasy"] = encode_vector(r.Xasy);
799 return out;
800 }
801};
802
803struct OpRd {
804 template <class T>
805 static Json run(Args& a) {
806 const Matrix<T> L = a.matrix<T>("L");
807 const std::vector<int> N = a.ints("N");
808 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
809 const Matrix<T> mu = a.matrix<T>("mu");
810 OptObj o(a, "options", "pfqn_rd");
811 pfqn::RdResult<T> r;
812 if (!o.given()) {
813 a.done();
814 r = pfqn::pfqn_rd(L, N, Z, mu);
815 } else {
816 const double tol = o.number<double>("tol");
817 const pfqn::NcMethod method = pfqn::nc_method_of(o.text("method"));
818 o.done();
819 a.done();
820 r = pfqn::pfqn_rd(L, N, Z, mu, tol, method);
821 }
822 Json out;
823 out["lGN"] = r.lGN;
824 out["Cgamma"] = encode_scalar(r.Cgamma);
825 return out;
826 }
827};
828
829struct OpRgf {
830 template <class T>
831 static Json run(Args& a) {
832 const std::vector<T> L = a.vector<T>("L");
833 const int N = a.required_integer("N");
834 pfqn::RgfResult<T> r;
835 if (a.has("Z")) {
836 const T Z = a.number<T>("Z");
837 a.done();
838 r = pfqn::pfqn_rgf(L, N, Z);
839 } else {
840 a.done();
841 r = pfqn::pfqn_rgf(L, N);
842 }
843 Json out;
844 out["G"] = encode_scalar(r.G);
845 out["lG"] = encode_scalar(r.lG);
846 out["lg"] = encode_vector(r.lg);
847 return out;
848 }
849};
850
851struct OpRgfmc {
852 template <class T>
853 static Json run(Args& a) {
854 const Matrix<T> L = a.matrix<T>("L");
855 const std::vector<int> N = a.ints("N");
856 const std::vector<T> Z = a.vector<T>("Z");
857 OptObj o(a, "options", "pfqn_rgfmc");
858 pfqn::RgfmcResult<T> r;
859 if (!o.given()) {
860 a.done();
861 r = pfqn::pfqn_rgfmc(L, N, Z);
862 } else {
863 const T tol = o.number<T>("tol");
864 const std::size_t maxterms = o.count("maxterms");
865 const T maxcancel = o.number<T>("maxcancel");
866 o.done();
867 a.done();
868 r = pfqn::pfqn_rgfmc(L, N, Z, tol, maxterms, maxcancel);
869 }
870 Json out;
871 out["G"] = encode_scalar(r.G);
872 out["lG"] = encode_scalar(r.lG);
873 return out;
874 }
875};
876
877struct OpXia {
878 template <class T>
879 static Json run(Args& a) {
880 const std::vector<T> L = a.vector<T>("L");
881 const int N = a.required_integer("N");
882 const std::vector<T> s = a.vector<T>("s");
883 a.done();
884 Json out;
885 out["lGasy"] = encode_scalar(pfqn::pfqn_xia(L, N, s));
886 return out;
887 }
888};
889
890// ---------------------------------------------------------------------------
891// MVA family
892// ---------------------------------------------------------------------------
893
894struct OpMvams {
895 template <class T>
896 static Json run(Args& a) {
897 const std::vector<T> lambda = a.vector<T>("lambda");
898 const Matrix<T> L = a.matrix<T>("L");
899 const std::vector<int> N = pop_with_open(a, "N");
900 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
901 pfqn::MvaResult<T> r;
902 if (a.has("mi")) {
903 const std::vector<int> mi = a.ints("mi");
904 if (!a.has("S"))
905 throw InputError("pfqn_mvams: the port's form with 'mi' also takes 'S'");
906 const std::vector<int> S = pop_with_open(a, "S");
907 a.done();
908 r = pfqn::pfqn_mvams(lambda, L, N, Z, mi, S);
909 } else if (a.has("S")) {
910 const std::vector<int> S = pop_with_open(a, "S");
911 a.done();
912 r = pfqn::pfqn_mvams(lambda, L, N, Z, S);
913 } else {
914 a.done();
915 r = pfqn::pfqn_mvams(lambda, L, N, Z);
916 }
917 return mva_out(r, "lG");
918 }
919};
920
921struct OpMvamx {
922 template <class T>
923 static Json run(Args& a) {
924 const std::vector<T> lambda = a.vector<T>("lambda");
925 const Matrix<T> D = a.matrix<T>("D");
926 const std::vector<int> N = pop_with_open(a, "N");
927 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
928 const std::vector<int> mi = a.ints_or_empty("mi");
929 a.done();
930 return mva_out(pfqn::pfqn_mvamx(lambda, D, N, Z, mi), "lGN");
931 }
932};
933
934struct OpMvaldmx {
935 template <class T>
936 static Json run(Args& a) {
937 const std::vector<T> lambda = a.vector<T>("lambda");
938 const Matrix<T> D = a.matrix<T>("D");
939 const std::vector<int> N = pop_with_open(a, "N");
940 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
941 const Matrix<T> mu = a.matrix<T>("mu");
942 a.done();
943 return mva_out(pfqn::pfqn_mvaldmx(lambda, D, N, Z, mu), "lGN");
944 }
945};
946
947struct OpMvaldms {
948 template <class T>
949 static Json run(Args& a) {
950 const std::vector<T> lambda = a.vector<T>("lambda");
951 const Matrix<T> D = a.matrix<T>("D");
952 const std::vector<int> N = pop_with_open(a, "N");
953 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
954 const std::vector<int> S = pop_with_open(a, "S");
955 a.done();
956 return mva_out(pfqn::pfqn_mvaldms(lambda, D, N, Z, S), "lGN");
957 }
958};
959
960struct OpMvald {
961 template <class T>
962 static Json run(Args& a) {
963 const Matrix<T> L = a.matrix<T>("L");
964 const std::vector<int> N = a.ints("N");
965 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
966 const Matrix<T> mu = a.matrix<T>("mu");
967 pfqn::MvaLdResult<T> r;
968 if (a.has("stabilize")) {
969 const bool st = a.boolean("stabilize", false);
970 a.done();
971 r = pfqn::pfqn_mvald(L, N, Z, mu, st);
972 } else {
973 a.done();
974 r = pfqn::pfqn_mvald(L, N, Z, mu);
975 }
976 Json out;
977 out["XN"] = encode_vector(r.XN);
978 out["QN"] = encode_matrix(r.QN);
979 out["UN"] = encode_vector(r.UN);
980 out["CN"] = encode_vector(r.CN);
981 out["lGN"] = r.lG;
982 out["G"] = encode_scalar(r.G);
983 out["isNumStable"] = r.isNumStable;
984 out["pi"] = encode_matrix(r.PI);
985 out["WN"] = encode_matrix(r.WN);
986 return out;
987 }
988};
989
990struct OpDebemva {
991 template <class T>
992 static Json run(Args& a) {
993 const long N = a_long(a, "N");
994 const std::vector<T> S = a.vector<T>("S");
995 const Matrix<T> P = a.matrix<T>("P");
996 const std::vector<double> K = doubles(a, "K");
997 const std::vector<bool> isdelay = bools(a, "isdelay");
998 const std::size_t k = given_prefix(a, "pfqn_debemva", {"tol", "iter_max"});
999 pfqn::DebemvaResult<T> r;
1000 if (k == 0) {
1001 a.done();
1002 r = pfqn::pfqn_debemva(N, S, P, K, isdelay);
1003 } else if (k == 1) {
1004 const double tol = a.number<double>("tol");
1005 a.done();
1006 r = pfqn::pfqn_debemva(N, S, P, K, isdelay, tol);
1007 } else {
1008 const double tol = a.number<double>("tol");
1009 const long iter_max = a_long(a, "iter_max");
1010 a.done();
1011 r = pfqn::pfqn_debemva(N, S, P, K, isdelay, tol, iter_max);
1012 }
1013 Json out;
1014 out["X"] = encode_vector(r.X);
1015 out["Q"] = encode_vector(r.Q);
1016 out["U"] = encode_vector(r.U);
1017 out["R"] = encode_vector(r.R);
1018 out["BT"] = encode_vector(r.BT);
1019 out["z"] = encode_vector(r.z);
1020 out["theta"] = r.theta;
1021 out["iter"] = r.iter;
1022 out["converged"] = r.converged;
1023 return out;
1024 }
1025};
1026
1027struct OpOqnrc {
1028 static Json run(Args& a) {
1029 const std::vector<double> lam = a.vector<double>("lam");
1030 const Matrix<double> A = a.matrix<double>("A");
1031 const Matrix<double> S = a.matrix<double>("S");
1032 const Matrix<double> SCV = a.matrix<double>("SCV");
1033 const std::vector<double> nservers = doubles(a, "nservers");
1034 const std::vector<bool> isps = bools(a, "isps");
1035 const std::vector<double> poolv = doubles(a, "pool"); // 1-based, as in MATLAB
1036 const std::vector<double> Ncapv = doubles(a, "Ncap");
1037 const bool waitq = a.boolean("waitq", true);
1038 const double tol = a.has("tol") ? a.number<double>("tol") : 1e-8;
1039 const long iter_max = a.has("iter_max") ? a_long(a, "iter_max") : 1000L;
1040 a.done();
1041 std::vector<int> pool, Ncap;
1042 for (double v : poolv) pool.push_back(static_cast<int>(v) - 1);
1043 for (double v : Ncapv) Ncap.push_back(static_cast<int>(v));
1044 const pfqn::OqnrcResult r =
1045 pfqn::pfqn_oqnrc(lam, A, S, SCV, nservers, isps, pool, Ncap, waitq, tol, static_cast<int>(iter_max));
1046 Json out;
1047 out["Q"] = encode_matrix(r.Q);
1048 out["T"] = encode_matrix(r.T);
1049 out["U"] = encode_matrix(r.U);
1050 out["R"] = encode_matrix(r.R);
1051 out["X"] = encode_vector(r.X);
1052 out["Qext"] = encode_vector(r.Qext);
1053 out["Ploss"] = encode_vector(r.Ploss);
1054 out["iter"] = r.iter;
1055 return out;
1056 }
1057};
1058
1059struct OpMvablo {
1060 template <class T>
1061 static Json run(Args& a) {
1062 const long N = a_long(a, "N");
1063 const std::vector<T> S = a.vector<T>("S");
1064 const Matrix<T> P = a.matrix<T>("P");
1065 const std::vector<double> K = doubles(a, "K");
1066 const std::vector<bool> isdelay = bools(a, "isdelay");
1067 const std::size_t k = given_prefix(a, "pfqn_mvablo", {"tol", "iter_max"});
1068 pfqn::MvabloResult<T> r;
1069 if (k == 0) {
1070 a.done();
1071 r = pfqn::pfqn_mvablo(N, S, P, K, isdelay);
1072 } else if (k == 1) {
1073 const double tol = a.number<double>("tol");
1074 a.done();
1075 r = pfqn::pfqn_mvablo(N, S, P, K, isdelay, tol);
1076 } else {
1077 const double tol = a.number<double>("tol");
1078 const int iter_max = a.required_integer("iter_max");
1079 a.done();
1080 r = pfqn::pfqn_mvablo(N, S, P, K, isdelay, tol, iter_max);
1081 }
1082 Json out;
1083 out["X"] = encode_vector(r.X);
1084 out["Q"] = encode_vector(r.Q);
1085 out["U"] = encode_vector(r.U);
1086 out["R"] = encode_vector(r.R);
1087 out["BT"] = encode_matrix(r.BT);
1088 out["iter"] = r.iter;
1089 out["converged"] = r.converged;
1090 return out;
1091 }
1092};
1093
1094struct OpMvac {
1095 template <class T>
1096 static Json run(Args& a) {
1097 const Matrix<T> L = a.matrix<T>("L");
1098 const std::vector<int> N = a.ints("N");
1099 pfqn::MvacResult<T> r;
1100 if (a.has("Z")) {
1101 const Matrix<T> Z = a.matrix<T>("Z");
1102 a.done();
1103 r = pfqn::pfqn_mvac(L, N, Z);
1104 } else {
1105 a.done();
1106 r = pfqn::pfqn_mvac(L, N);
1107 }
1108 Json out;
1109 out["XN"] = encode_vector(r.X);
1110 out["QN"] = encode_matrix(r.Q);
1111 out["UN"] = encode_matrix(r.U);
1112 out["CN"] = encode_matrix(r.C);
1113 return out;
1114 }
1115};
1116
1117struct OpMvacld {
1118 template <class T>
1119 static Json run(Args& a) {
1120 const Matrix<T> L = a.matrix<T>("L");
1121 const std::vector<int> N = a.ints("N");
1122 const Matrix<T> Z = a.matrix<T>("Z");
1123 pfqn::MvacldResult<T> r;
1124 if (a.has("mu")) {
1125 const Matrix<T> mu = a.matrix<T>("mu");
1126 a.done();
1127 r = pfqn::pfqn_mvacld(L, N, Z, mu);
1128 } else {
1129 a.done();
1130 r = pfqn::pfqn_mvacld(L, N, Z);
1131 }
1132 Json out;
1133 out["XN"] = encode_vector(r.X);
1134 out["QN"] = encode_matrix(r.Q);
1135 out["UN"] = encode_vector(r.U);
1136 out["CN"] = encode_vector(r.C);
1137 out["pij"] = encode_matrix(r.pij);
1138 return out;
1139 }
1140};
1141
1142struct OpNintmva {
1143 template <class T>
1144 static Json run(Args& a) {
1145 const std::vector<T> L = a.vector<T>("L");
1146 const T N = a.number<T>("N");
1147 pfqn::NintMvaResult<T> r;
1148 if (a.has("Z")) {
1149 const T Z = a.number<T>("Z");
1150 a.done();
1151 r = pfqn::pfqn_nintmva(L, N, Z);
1152 } else {
1153 a.done();
1154 r = pfqn::pfqn_nintmva(L, N);
1155 }
1156 Json out;
1157 out["X"] = encode_scalar(r.X);
1158 out["Q"] = encode_vector(r.Q);
1159 out["U"] = encode_vector(r.U);
1160 out["R"] = encode_vector(r.R);
1161 return out;
1162 }
1163};
1164
1165struct OpMvaInterval {
1166 template <class T>
1167 static Json run(Args& a) {
1168 const Matrix<T> L = a.matrix<T>("L");
1169 const std::vector<int> N = a.ints("N");
1170 const std::vector<T> Z = a.vector<T>("Z");
1171 a.done();
1172 // MATLAB: a scalar is a thin interval [x x].
1173 if (N.size() != 1 && N.size() != 2)
1174 throw InputError("pfqn_mva_interval: 'N' must be [nlo nup] or a scalar");
1175 if (Z.size() != 1 && Z.size() != 2)
1176 throw InputError("pfqn_mva_interval: 'Z' must be [zlo zup] or a scalar");
1177 const pfqn::MvaIntervalResult<T> r =
1178 pfqn::pfqn_mva_interval(L, N.front(), N.back(), Z.front(), Z.back());
1179 Json out;
1180 out["X"] = Json::array({encode_scalar(r.Xlo), encode_scalar(r.Xup)});
1181 out["Q"] = encode_matrix(r.Q);
1182 out["U"] = encode_matrix(r.U);
1183 out["R"] = encode_matrix(r.R);
1184 out["Rtot"] = Json::array({encode_scalar(r.Rtot_lo), encode_scalar(r.Rtot_up)});
1185 out["Qtot"] = Json::array({encode_scalar(r.Qtot_lo), encode_scalar(r.Qtot_up)});
1186 return out;
1187 }
1188};
1189
1190struct OpMomlin {
1191 template <class T>
1192 static Json run(Args& a) {
1193 const Matrix<T> L = a.matrix<T>("L");
1194 const std::vector<int> N = a.ints("N");
1195 const std::vector<T> Z = a.vector<T>("Z");
1196 pfqn::MomlinResult<T> r;
1197 if (all_or_none(a, "pfqn_momlin", {"tol", "maxiter"})) {
1198 const T tol = a.number<T>("tol");
1199 const int maxiter = a.required_integer("maxiter");
1200 a.done();
1201 r = pfqn::pfqn_momlin(L, N, Z, tol, maxiter);
1202 } else {
1203 a.done();
1204 r = pfqn::pfqn_momlin(L, N, Z);
1205 }
1206 Json out;
1207 out["Q"] = encode_matrix(r.Q);
1208 out["X"] = encode_vector(r.X);
1209 out["U"] = encode_matrix(r.U);
1210 out["R"] = encode_matrix(r.R);
1211 out["QVar"] = encode_matrix(r.QVar);
1212 // (M x R x M x R) covariance, column-major as MATLAB stores it.
1213 Json qc;
1214 qc["sz"] = Json::array({r.M, r.Rc, r.M, r.Rc});
1215 qc["data"] = encode_vector(r.QCov);
1216 out["QCov"] = qc;
1217 out["dQ"] = encode_vector(r.dQ);
1218 return out;
1219 }
1220};
1221
1222// ---------------------------------------------------------------------------
1223// Approximate MVA
1224// ---------------------------------------------------------------------------
1225
1226struct OpQsa {
1227 template <class T>
1228 static Json run(Args& a) {
1229 const Matrix<T> L = a.matrix<T>("L");
1230 const std::vector<T> N = a.vector<T>("N");
1231 const std::size_t k =
1232 given_prefix(a, "pfqn_qsa", {"Z", "type", "tol", "maxiter", "levels"});
1233 pfqn::AmvaResult<T> r;
1234 if (k == 0) {
1235 a.done();
1236 r = pfqn::pfqn_qsa(L, N);
1237 } else if (k == 1) {
1238 const std::vector<T> Z = a.vector<T>("Z");
1239 a.done();
1240 r = pfqn::pfqn_qsa(L, N, Z);
1241 } else {
1242 const std::vector<T> Z = a.vector<T>("Z");
1243 const std::vector<pfqn::AmvaSched> type = amva_sched_list(a, "type", "pfqn_qsa");
1244 if (k == 2) {
1245 a.done();
1246 r = pfqn::pfqn_qsa(L, N, Z, type);
1247 } else if (k == 3) {
1248 const double tol = a.number<double>("tol");
1249 a.done();
1250 r = pfqn::pfqn_qsa(L, N, Z, type, tol);
1251 } else if (k == 4) {
1252 const double tol = a.number<double>("tol");
1253 const std::size_t maxiter = a.count("maxiter");
1254 a.done();
1255 r = pfqn::pfqn_qsa(L, N, Z, type, tol, maxiter);
1256 } else {
1257 const double tol = a.number<double>("tol");
1258 const std::size_t maxiter = a.count("maxiter");
1259 const int levels = a.required_integer("levels");
1260 a.done();
1261 r = pfqn::pfqn_qsa(L, N, Z, type, tol, maxiter, levels);
1262 }
1263 }
1264 Json out;
1265 out["Q"] = encode_matrix(r.QN);
1266 out["U"] = encode_matrix(r.UN);
1267 out["W"] = encode_matrix(r.RN);
1268 out["X"] = encode_vector(r.XN);
1269 out["totiter"] = encode_count(r.iterations);
1270 out["converged"] = r.converged;
1271 return out;
1272 }
1273};
1274
1275struct OpQdamva {
1276 template <class T>
1277 static Json run(Args& a) {
1278 const Matrix<T> L = a.matrix<T>("L");
1279 const std::vector<T> N = a.vector<T>("N");
1280 const std::vector<T> Z = a.vector<T>("Z");
1281 const Matrix<T> mu = a.matrix<T>("mu");
1282 const Matrix<T> Q0 = a.matrix<T>("Q0");
1283 const std::size_t k = given_prefix(a, "pfqn_qdamva", {"tol", "maxiter"});
1284 pfqn::QdAmvaResult<T> r;
1285 if (k == 0) {
1286 a.done();
1287 r = pfqn::pfqn_qdamva(L, N, Z, mu, Q0);
1288 } else if (k == 1) {
1289 const double tol = a.number<double>("tol");
1290 a.done();
1291 r = pfqn::pfqn_qdamva(L, N, Z, mu, Q0, tol);
1292 } else {
1293 const double tol = a.number<double>("tol");
1294 const std::size_t maxiter = a.count("maxiter");
1295 a.done();
1296 r = pfqn::pfqn_qdamva(L, N, Z, mu, Q0, tol, maxiter);
1297 }
1298 Json out;
1299 out["Q"] = encode_matrix(r.Q);
1300 out["X"] = encode_matrix(r.X);
1301 out["U"] = encode_matrix(r.U);
1302 out["iter"] = encode_count(r.iter);
1303 out["R"] = encode_matrix(r.R);
1304 return out;
1305 }
1306};
1307
1308struct OpQdlin {
1309 template <class T>
1310 static Json run(Args& a) {
1311 const Matrix<T> L = a.matrix<T>("L");
1312 const std::vector<T> N = a.vector<T>("N");
1313 const std::vector<T> Z = a.vector<T>("Z");
1314 pfqn::QdLinResult<T> r;
1315 const bool ld = all_or_none(a, "pfqn_qdlin", {"mu", "nservers"});
1316 const std::size_t k = given_prefix(a, "pfqn_qdlin", {"tol", "maxiter", "wtol"});
1317 Matrix<T> mu;
1318 std::vector<double> ns;
1319 if (ld) {
1320 mu = a.matrix<T>("mu");
1321 ns = doubles(a, "nservers");
1322 }
1323 double tol = 0, wtol = 0;
1324 std::size_t maxiter = 0;
1325 if (k >= 1) tol = a.number<double>("tol");
1326 if (k >= 2) maxiter = a.count("maxiter");
1327 if (k >= 3) wtol = a.number<double>("wtol");
1328 a.done();
1329 if (ld) {
1330 if (k == 0) r = pfqn::pfqn_qdlin(L, N, Z, mu, ns);
1331 else if (k == 1) r = pfqn::pfqn_qdlin(L, N, Z, mu, ns, tol);
1332 else if (k == 2) r = pfqn::pfqn_qdlin(L, N, Z, mu, ns, tol, maxiter);
1333 else r = pfqn::pfqn_qdlin(L, N, Z, mu, ns, tol, maxiter, wtol);
1334 } else {
1335 if (k == 0) r = pfqn::pfqn_qdlin(L, N, Z);
1336 else if (k == 1) r = pfqn::pfqn_qdlin(L, N, Z, tol);
1337 else if (k == 2) r = pfqn::pfqn_qdlin(L, N, Z, tol, maxiter);
1338 else r = pfqn::pfqn_qdlin(L, N, Z, tol, maxiter, wtol);
1339 }
1340 Json out;
1341 out["Q"] = encode_matrix(r.Q);
1342 out["U"] = encode_matrix(r.U);
1343 out["R"] = encode_matrix(r.R);
1344 out["X"] = encode_matrix(r.X);
1345 out["C"] = encode_matrix(r.C);
1346 out["iter"] = encode_count(r.iter);
1347 return out;
1348 }
1349};
1350
1351struct OpPam {
1352 template <class T>
1353 static Json run(Args& a) {
1354 const Matrix<T> L = a.matrix<T>("L");
1355 const std::vector<T> N = a.vector<T>("N");
1356 const std::size_t k = given_prefix(a, "pfqn_pam", {"Z", "variant"});
1357 pfqn::AmvaResult<T> r;
1358 if (k == 0) {
1359 a.done();
1360 r = pfqn::pfqn_pam(L, N);
1361 } else {
1362 const std::vector<T> Z = a.vector<T>("Z");
1363 if (k == 1) {
1364 a.done();
1365 r = pfqn::pfqn_pam(L, N, Z);
1366 } else {
1367 const pfqn::PamVariant v = enum_of<pfqn::PamVariant>(
1368 "pfqn_pam", "variant", a.text("variant", ""),
1369 {{"pamb", pfqn::PamVariant::Basic},
1370 {"pami", pfqn::PamVariant::Improved},
1371 {"pamt", pfqn::PamVariant::Two}});
1372 a.done();
1373 r = pfqn::pfqn_pam(L, N, Z, v);
1374 }
1375 }
1376 return amva_out(r, "iterations");
1377 }
1378};
1379
1380struct OpTay {
1381 template <class T>
1382 static Json run(Args& a) {
1383 const Matrix<T> L = a.matrix<T>("L");
1384 const std::vector<T> N = a.vector<T>("N");
1385 const std::size_t k = given_prefix(a, "pfqn_tay", {"Z", "tol", "maxiter", "QN0"});
1386 pfqn::AmvaResult<T> r;
1387 if (k == 0) {
1388 a.done();
1389 r = pfqn::pfqn_tay(L, N);
1390 } else {
1391 const std::vector<T> Z = a.vector<T>("Z");
1392 if (k == 1) {
1393 a.done();
1394 r = pfqn::pfqn_tay(L, N, Z);
1395 } else if (k == 2) {
1396 const double tol = a.number<double>("tol");
1397 a.done();
1398 r = pfqn::pfqn_tay(L, N, Z, tol);
1399 } else if (k == 3) {
1400 const double tol = a.number<double>("tol");
1401 const std::size_t maxiter = a.count("maxiter");
1402 a.done();
1403 r = pfqn::pfqn_tay(L, N, Z, tol, maxiter);
1404 } else {
1405 const double tol = a.number<double>("tol");
1406 const std::size_t maxiter = a.count("maxiter");
1407 const Matrix<T> QN0 = a.matrix<T>("QN0");
1408 a.done();
1409 r = pfqn::pfqn_tay(L, N, Z, tol, maxiter, QN0);
1410 }
1411 }
1412 return amva_out(r, "it");
1413 }
1414};
1415
1416struct OpScat {
1417 template <class T>
1418 static Json run(Args& a) {
1419 const Matrix<T> L = a.matrix<T>("L");
1420 const std::vector<int> N = a.ints("N");
1421 const bool lng = all_or_none(a, "pfqn_scat", {"type", "tol", "maxiter", "QN0"});
1422 pfqn::LinearizerResult<T> r;
1423 if (lng) {
1424 const Matrix<T> Z = a.matrix<T>("Z");
1425 const std::vector<pfqn::SchedStrategy> type = sched_list(a, "type", "pfqn_scat");
1426 const double tol = a.number<double>("tol");
1427 const int maxiter = a.required_integer("maxiter");
1428 const Matrix<T> QN0 = a.matrix_or_empty<T>("QN0");
1429 a.done();
1430 r = pfqn::pfqn_scat(L, N, Z, type, tol, maxiter, QN0);
1431 } else if (a.has("Z")) {
1432 const Matrix<T> Z = a.matrix<T>("Z");
1433 a.done();
1434 r = pfqn::pfqn_scat(L, N, Z);
1435 } else {
1436 a.done();
1437 r = pfqn::pfqn_scat(L, N);
1438 }
1439 return linearizer_out(r);
1440 }
1441};
1442
1443struct OpSchmidt {
1444 template <class T>
1445 static Json run(Args& a) {
1446 const Matrix<T> D = a.matrix<T>("D");
1447 const std::vector<int> N = a.ints("N");
1448 const Matrix<int> S = int_matrix(a, "S");
1449 const std::vector<pfqn::SchedStrategy> sched = sched_list(a, "sched", "pfqn_schmidt");
1450 pfqn::SchmidtResult<T> r;
1451 if (a.has("v")) {
1452 const Matrix<T> v = a.matrix<T>("v");
1453 a.done();
1454 r = pfqn::pfqn_schmidt(D, N, S, sched, v);
1455 } else {
1456 a.done();
1457 r = pfqn::pfqn_schmidt(D, N, S, sched);
1458 }
1459 return schmidt_out(r);
1460 }
1461};
1462
1463struct OpSchmidtExt {
1464 template <class T>
1465 static Json run(Args& a) {
1466 const Matrix<T> D = a.matrix<T>("D");
1467 const std::vector<int> N = a.ints("N");
1468 const Matrix<int> S = int_matrix(a, "S");
1469 const std::vector<pfqn::SchedStrategy> sched = sched_list(a, "sched", "pfqn_schmidt_ext");
1470 a.done();
1471 return schmidt_out(pfqn::pfqn_schmidt_ext(D, N, S, sched));
1472 }
1473};
1474
1475struct OpMarie {
1476 template <class T>
1477 static Json run(Args& a) {
1478 const Matrix<T> L = a.matrix<T>("L");
1479 const std::vector<int> N = a.ints("N");
1480 const std::vector<T> Z = a.vector<T>("Z");
1481 const Matrix<T> scv = a.matrix<T>("scv");
1482 pfqn::MarieResult<T> r;
1483 if (all_or_none(a, "pfqn_marie", {"tol", "maxiter", "nservers"})) {
1484 const double tol = a.number<double>("tol");
1485 const int maxiter = a.required_integer("maxiter");
1486 const std::vector<int> ns = a.ints("nservers");
1487 a.done();
1488 r = pfqn::pfqn_marie(L, N, Z, scv, tol, maxiter, ns);
1489 } else {
1490 a.done();
1491 r = pfqn::pfqn_marie(L, N, Z, scv);
1492 }
1493 Json out;
1494 out["X"] = encode_vector(r.X);
1495 out["Q"] = encode_matrix(r.Q);
1496 out["U"] = encode_matrix(r.U);
1497 out["C"] = encode_matrix(r.C);
1498 out["it"] = r.it;
1499 if (r.cds.empty()) {
1500 out["mu"] = encode_matrix(r.mu);
1501 } else {
1502 Json mu = Json::array();
1503 for (const auto& c : r.cds) {
1504 Json e;
1505 e["N"] = encode_ints(c.N);
1506 e["muCox"] = encode_vectors(c.muCox);
1507 e["muExp"] = encode_vectors(c.muExp);
1508 mu.push_back(e);
1509 }
1510 out["mu"] = mu;
1511 }
1512 return out;
1513 }
1514};
1515
1516/** pfqn_stdf and pfqn_stdf_heur share their signature and result. */
1517template <bool Heur>
1518struct OpStdf {
1519 template <class T>
1520 static Json run(Args& a) {
1521 const Matrix<T> L = a.matrix<T>("L");
1522 const std::vector<int> N = a.ints("N");
1523 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
1524 const std::vector<int> S = a.ints("S");
1525 const std::vector<std::size_t> fcfs = indices(a, "fcfsNodes");
1526 const Matrix<T> rates = a.matrix<T>("rates");
1527 const std::vector<T> tset = a.vector<T>("tset");
1528 a.done();
1529 const pfqn::StdfResult<T> r = Heur
1530 ? pfqn::pfqn_stdf_heur(L, N, Z, S, fcfs, rates, tset)
1531 : pfqn::pfqn_stdf(L, N, Z, S, fcfs, rates, tset);
1532 Json rd = Json::array();
1533 for (const auto& row : r.RD) rd.push_back(encode_matrices(row));
1534 Json out;
1535 out["RD"] = rd;
1536 out["tset"] = encode_vector(r.tset);
1537 out["isNumStable"] = r.isNumStable;
1538 return out;
1539 }
1540};
1541
1542struct OpSqni {
1543 template <class T>
1544 static Json run(Args& a) {
1545 const std::vector<T> N = a.vector<T>("N");
1546 const std::vector<T> L = a.vector<T>("L");
1547 const std::vector<T> Z = a.vector<T>("Z");
1548 a.done();
1549 const pfqn::SqniResult<T> r = pfqn::pfqn_sqni(N, L, Z);
1550 Json out;
1551 out["Q"] = encode_vector(r.Q);
1552 out["U"] = encode_vector(r.U);
1553 out["X"] = encode_vector(r.X);
1554 return out;
1555 }
1556};
1557
1558struct OpSdr {
1559 template <class T>
1560 static Json run(Args& a) {
1561 const Matrix<T> S = a.matrix<T>("S");
1562 const Matrix<T> xi = a.matrix<T>("xi");
1563 const std::vector<std::size_t> N = indices(a, "N");
1564 OptObj o(a, "sdr", "pfqn_sdr");
1565 if (!o.given()) throw InputError("pfqn_sdr: missing required argument 'sdr'");
1566 pfqn::SdrStruct sdr;
1567 auto idx = [&](const char* k) {
1568 const std::vector<int> v = int_vector_from_json(o.get(k), o.where(k));
1569 std::vector<std::size_t> out;
1570 for (int x : v) {
1571 if (x < 0) throw InputError(o.where(k) + " cannot be negative");
1572 out.push_back(static_cast<std::size_t>(x));
1573 }
1574 return out;
1575 };
1576 sdr.entry = o.count("entry");
1577 sdr.departure = o.count("departure");
1578 {
1579 const Json& br = o.get("branch");
1580 if (!br.is_array()) throw InputError(o.where("branch") + " must be a list of index lists");
1581 for (std::size_t b = 0; b < br.size(); ++b) {
1582 std::vector<std::size_t> blk;
1583 if (!br[b].is_null())
1584 for (int x : int_vector_from_json(br[b], o.where("branch"))) {
1585 if (x < 0) throw InputError(o.where("branch") + " cannot be negative");
1586 blk.push_back(static_cast<std::size_t>(x));
1587 }
1588 sdr.branch.push_back(blk);
1589 }
1590 }
1591 sdr.entryOf = idx("entryOf");
1592 sdr.departureOf = idx("departureOf");
1593 sdr.level = idx("level");
1594 sdr.C = vector_from_json<double>(o.get("C"), o.where("C"));
1595 sdr.d = matrix_from_json<double>(o.get("d"), o.where("d"));
1596 o.done();
1597 pfqn::SdrResult<T> r;
1598 if (a.has("alpha")) {
1599 const Matrix<T> alpha = a.matrix<T>("alpha");
1600 a.done();
1601 r = pfqn::pfqn_sdr(S, xi, N, sdr, alpha);
1602 } else {
1603 a.done();
1604 r = pfqn::pfqn_sdr(S, xi, N, sdr);
1605 }
1606 Json out;
1607 out["Q"] = encode_matrix(r.QN);
1608 out["X"] = encode_matrix(r.XN);
1609 out["U"] = encode_matrix(r.UN);
1610 out["R"] = encode_matrix(r.RN);
1611 out["G"] = encode_scalar(r.G);
1612 return out;
1613 }
1614};
1615
1616// ---------------------------------------------------------------------------
1617// Bounds
1618// ---------------------------------------------------------------------------
1619
1620/** Scalar-throughput bounds of signature (L, N, Z) -> T; Key is the MATLAB output name. */
1621template <template <class> class F>
1622struct OpXzBound {
1623 template <class T>
1624 static Json run(Args& a) {
1625 const std::vector<T> L = a.vector<T>("L");
1626 const T N = a.number<T>("N");
1627 const T Z = a.number<T>("Z");
1628 a.done();
1629 Json out;
1630 out[F<T>::key] = encode_scalar(F<T>::call(L, N, Z));
1631 return out;
1632 }
1633};
1634template <class T>
1635struct XzAbaLow {
1636 static constexpr const char* key = "XN";
1637 static T call(const std::vector<T>& L, const T& N, const T& Z) { return pfqn::pfqn_xzabalow(L, N, Z); }
1638};
1639template <class T>
1640struct XzAbaUp {
1641 static constexpr const char* key = "XN";
1642 static T call(const std::vector<T>& L, const T& N, const T& Z) { return pfqn::pfqn_xzabaup(L, N, Z); }
1643};
1644template <class T>
1645struct XzGsbLow {
1646 static constexpr const char* key = "X";
1647 static T call(const std::vector<T>& L, const T& N, const T& Z) { return pfqn::pfqn_xzgsblow(L, N, Z); }
1648};
1649template <class T>
1650struct XzGsbUp {
1651 static constexpr const char* key = "X";
1652 static T call(const std::vector<T>& L, const T& N, const T& Z) { return pfqn::pfqn_xzgsbup(L, N, Z); }
1653};
1654
1655template <bool Up>
1656struct OpQzgb {
1657 template <class T>
1658 static Json run(Args& a) {
1659 const std::vector<T> L = a.vector<T>("L");
1660 const T N = a.number<T>("N");
1661 const T Z = a.number<T>("Z");
1662 const std::size_t i = a.count("i");
1663 a.done();
1664 Json out;
1665 out["Qgb"] = encode_scalar(Up ? pfqn::pfqn_qzgbup(L, N, Z, i)
1666 : pfqn::pfqn_qzgblow(L, N, Z, i));
1667 return out;
1668 }
1669};
1670
1671struct OpSsd {
1672 template <class T>
1673 static Json run(Args& a) {
1674 const std::vector<T> L = a.vector<T>("L");
1675 const T N = a.number<T>("N");
1676 const T Z = a.number<T>("Z");
1677 pfqn::SsdBounds<T> r;
1678 if (a.has("nservers")) {
1679 const std::vector<T> ns = a.vector<T>("nservers");
1680 a.done();
1681 r = pfqn::pfqn_ssd(L, N, Z, ns);
1682 } else {
1683 a.done();
1684 r = pfqn::pfqn_ssd(L, N, Z);
1685 }
1686 Json out;
1687 out["Xlo"] = encode_scalar(r.Xlo);
1688 out["Xhi"] = encode_scalar(r.Xhi);
1689 return out;
1690 }
1691};
1692
1693struct OpMcub {
1694 template <class T>
1695 static Json run(Args& a) {
1696 const Matrix<T> L = a.matrix<T>("L");
1697 const std::vector<T> N = a.vector<T>("N");
1698 pfqn::McubBounds<T> r;
1699 if (a.has("Z")) {
1700 const std::vector<T> Z = a.vector<T>("Z");
1701 a.done();
1702 r = pfqn::pfqn_mcub(L, N, Z);
1703 } else {
1704 a.done();
1705 r = pfqn::pfqn_mcub(L, N);
1706 }
1707 Json out;
1708 out["Xub"] = encode_vector(r.Xub);
1709 out["Xlb"] = encode_vector(r.Xlb);
1710 return out;
1711 }
1712};
1713
1714struct OpMwrbb {
1715 template <class T>
1716 static Json run(Args& a) {
1717 const Matrix<T> V = a.matrix<T>("V");
1718 const Matrix<T> S = a.matrix<T>("S");
1719 const std::vector<T> N = a.vector<T>("N");
1720 const std::vector<T> Z = a.vector<T>("Z");
1721 pfqn::MwrbbBounds<T> r;
1722 if (all_or_none(a, "pfqn_mwrbb", {"sched", "prio"})) {
1723 std::vector<pfqn::MwrbbSched> sched;
1724 for (int c : a.ints("sched")) {
1725 if (c < 0 || c > 4)
1726 throw InputError("pfqn_mwrbb: 'sched' codes are 0=FIFO, 1=PS, 2=non-preemptive "
1727 "priority, 3=preemptive priority, 4=ABA; got " +
1728 std::to_string(c));
1729 sched.push_back(static_cast<pfqn::MwrbbSched>(c));
1730 }
1731 const std::vector<int> prio = a.ints("prio");
1732 a.done();
1733 r = pfqn::pfqn_mwrbb(V, S, N, Z, sched, prio);
1734 } else {
1735 a.done();
1736 r = pfqn::pfqn_mwrbb(V, S, N, Z);
1737 }
1738 Json out;
1739 out["Xlo"] = encode_vector(r.Xlo);
1740 out["Xup"] = encode_vector(r.Xup);
1741 out["Wlo"] = encode_matrix(r.Wlo);
1742 return out;
1743 }
1744};
1745
1746struct OpPbh {
1747 template <class T>
1748 static Json run(Args& a) {
1749 const std::vector<T> L = a.vector<T>("L");
1750 const int N = a.required_integer("N");
1751 const T Z = a.number<T>("Z");
1752 pfqn::PbhBounds<T> r;
1753 if (a.has("level")) {
1754 const int level = a.required_integer("level");
1755 a.done();
1756 r = pfqn::pfqn_pbh(L, N, Z, level);
1757 } else {
1758 a.done();
1759 r = pfqn::pfqn_pbh(L, N, Z);
1760 }
1761 Json out;
1762 out["Xlo"] = encode_scalar(r.Xlo);
1763 out["Xhi"] = encode_scalar(r.Xhi);
1764 out["Qlo"] = encode_vector(r.Qlo);
1765 out["Qhi"] = encode_vector(r.Qhi);
1766 return out;
1767 }
1768};
1769
1770struct OpSib {
1771 template <class T>
1772 static Json run(Args& a) {
1773 const std::vector<T> L = a.vector<T>("L");
1774 const int N = a.required_integer("N");
1775 const T Z = a.number<T>("Z");
1776 pfqn::SibBounds<T> r;
1777 if (a.has("level")) {
1778 const int level = a.required_integer("level");
1779 a.done();
1780 r = pfqn::pfqn_sib(L, N, Z, level);
1781 } else {
1782 a.done();
1783 r = pfqn::pfqn_sib(L, N, Z);
1784 }
1785 Json out;
1786 out["Xlo"] = encode_scalar(r.Xlo);
1787 out["Xhi"] = encode_scalar(r.Xhi);
1788 out["Wlo"] = encode_scalar(r.Wlo);
1789 out["Whi"] = encode_scalar(r.Whi);
1790 return out;
1791 }
1792};
1793
1794/** [XN,QN,UN,CN,lG,P0] = pfqn_mvams_closed(L, N, Z, mi, S). */
1795struct OpMvamsClosed {
1796 template <class T>
1797 static Json run(Args& a) {
1798 const Matrix<T> L = a.matrix<T>("L");
1799 const std::vector<int> N = a.ints("N");
1800 const Matrix<T> Z = a.matrix_or_empty<T>("Z");
1801 const std::vector<T> mi = a.vector_or_empty<T>("mi");
1802 const std::vector<int> S = pop_with_open_or_empty(a, "S");
1803 a.done();
1804 const pfqn::MvamsClosedResult<T> r = pfqn::pfqn_mvams_closed(L, N, Z, mi, S, true);
1805 Json out;
1806 out["XN"] = encode_vector(r.XN);
1807 out["QN"] = encode_matrix(r.QN);
1808 out["UN"] = encode_vector(r.UN);
1809 out["CN"] = encode_matrix(r.CN);
1810 out["lG"] = r.lG;
1811 out["P0"] = encode_vector(r.P0);
1812 return out;
1813 }
1814};
1815
1816/** [Xlo,Xhi,info] = pfqn_kriz_bounds(L, N, Z, level). */
1817struct OpKrizBounds {
1818 template <class T>
1819 static Json run(Args& a) {
1820 const std::vector<T> L = a.vector<T>("L");
1821 const int N = a.required_integer("N");
1822 const T Z = a.has("Z") ? a.number<T>("Z") : num_traits<T>::from_int(0);
1823 const int level = a.integer("level", 0);
1824 a.done();
1825 const pfqn::KrizBounds<T> r = pfqn::pfqn_kriz_bounds(L, N, Z, level);
1826 Json info;
1827 info["Ys_lo"] = encode_scalar(r.info.Ys_lo);
1828 info["Ys_hi"] = encode_scalar(r.info.Ys_hi);
1829 info["Ye"] = encode_scalar(r.info.Ye);
1830 info["Yes"] = encode_scalar(r.info.Yes);
1831 info["Xab"] = encode_scalar(r.info.Xab);
1832 // NaN at level 0 in MATLAB, spelled "NaN" as elsewhere in this layer
1833 info["Ylo_i"] = r.info.has_level ? encode_scalar(r.info.Ylo_i) : Json("NaN");
1834 info["Yhi_i"] = r.info.has_level ? encode_scalar(r.info.Yhi_i) : Json("NaN");
1835 info["level"] = r.info.level;
1836 info["B"] = encode_scalar(r.info.B);
1837 Json out;
1838 out["Xlo"] = encode_scalar(r.Xlo);
1839 out["Xhi"] = encode_scalar(r.Xhi);
1840 out["info"] = info;
1841 return out;
1842 }
1843};
1844
1845/** [Xlo,Xhi] = pfqn_ibjb_bounds(L, N, Z). */
1846struct OpIbjbBounds {
1847 template <class T>
1848 static Json run(Args& a) {
1849 const std::vector<T> L = a.vector<T>("L");
1850 const int N = a.required_integer("N");
1851 const T Z = a.has("Z") ? a.number<T>("Z") : num_traits<T>::from_int(0);
1852 a.done();
1853 const pfqn::IbjbBounds<T> r = pfqn::pfqn_ibjb_bounds(L, N, Z);
1854 Json out;
1855 out["Xlo"] = encode_scalar(r.Xlo);
1856 out["Xhi"] = encode_scalar(r.Xhi);
1857 return out;
1858 }
1859};
1860
1861/** [Xlo,Xhi,info] = pfqn_ldsy_bounds(L, N, Z, mu). */
1862struct OpLdsyBounds {
1863 template <class T>
1864 static Json run(Args& a) {
1865 const std::vector<T> L = a.vector<T>("L");
1866 const int N = a.required_integer("N");
1867 const T Z = a.has("Z") ? a.number<T>("Z") : num_traits<T>::from_int(0);
1868 const Matrix<T> mu = a.matrix_or_empty<T>("mu");
1869 a.done();
1870 const pfqn::LdsyBounds<T> r = pfqn::pfqn_ldsy_bounds(L, N, Z, mu);
1871 // NaN where MATLAB has NaN, spelled "NaN" as elsewhere in this layer
1872 const auto opt = [](bool has, const T& v) { return has ? encode_scalar(v) : Json("NaN"); };
1873 Json info;
1874 info["thm31_lo"] = opt(r.info.has_thm31_lo, r.info.thm31_lo);
1875 info["thm31_hi"] = opt(r.info.has_thm31_hi, r.info.thm31_hi);
1876 info["thm32_lo"] = opt(r.info.has_thm32_lo, r.info.thm32_lo);
1877 info["thm33_lo"] = opt(r.info.has_thm33_lo, r.info.thm33_lo);
1878 info["thm33_hi"] = opt(r.info.has_thm33_hi, r.info.thm33_hi);
1879 info["thm42_lo"] = opt(r.info.has_thm42_lo, r.info.thm42_lo);
1880 info["sec42_bjb"] = opt(r.info.has_sec42_bjb, r.info.sec42_bjb);
1881 info["sec42_hi"] = opt(r.info.has_sec42_hi, r.info.sec42_hi);
1882 info["Kopt"] = r.info.Kopt > 0 ? Json(r.info.Kopt) : Json("NaN");
1883 Json out;
1884 out["Xlo"] = encode_scalar(r.Xlo);
1885 out["Xhi"] = encode_scalar(r.Xhi);
1886 out["info"] = info;
1887 return out;
1888 }
1889};
1890
1891struct OpScb {
1892 template <class T>
1893 static Json run(Args& a) {
1894 const std::vector<T> L = a.vector<T>("L");
1895 const long N = a_long(a, "N");
1896 a.done();
1897 const pfqn::ScbBounds<T> r = pfqn::pfqn_scb(L, N);
1898 Json out;
1899 out["Xlo"] = encode_scalar(r.Xlo);
1900 out["Xhi"] = encode_scalar(r.Xhi);
1901 out["Ulo"] = encode_vector(r.Ulo);
1902 out["Uhi"] = encode_vector(r.Uhi);
1903 return out;
1904 }
1905};
1906
1907struct OpScbgap {
1908 template <class T>
1909 static Json run(Args& a) {
1910 const long N = a_long(a, "N");
1911 const long K = a_long(a, "K");
1912 T e;
1913 if (all_or_none(a, "pfqn_scbgap", {"r", "undominated"})) {
1914 const long r = a_long(a, "r");
1915 const bool und = a.boolean("undominated", false);
1916 a.done();
1917 e = pfqn::pfqn_scbgap<T>(N, K, r, und);
1918 } else {
1919 a.done();
1920 e = pfqn::pfqn_scbgap<T>(N, K);
1921 }
1922 Json out;
1923 out["e"] = encode_scalar(e);
1924 return out;
1925 }
1926};
1927
1928struct OpUsumbound {
1929 template <class T>
1930 static Json run(Args& a) {
1931 const long R = a_long(a, "R");
1932 const long K = a_long(a, "K");
1933 const long N = a_long(a, "N");
1934 a.done();
1935 Json out;
1936 out["Umax"] = encode_scalar(pfqn::pfqn_usumbound<T>(R, K, N));
1937 return out;
1938 }
1939};
1940
1941struct OpMinclasses {
1942 template <class T>
1943 static Json run(Args& a) {
1944 const T Usum = a.number<T>("Usum");
1945 const long K = a_long(a, "K");
1946 const long N = a_long(a, "N");
1947 a.done();
1948 Json out;
1949 out["Rmin"] = static_cast<long long>(pfqn::pfqn_minclasses(Usum, K, N));
1950 return out;
1951 }
1952};
1953
1954// ---------------------------------------------------------------------------
1955// Small structural helpers
1956// ---------------------------------------------------------------------------
1957
1958struct OpUnique {
1959 template <class T>
1960 static Json run(Args& a) {
1961 const Matrix<T> L = a.matrix<T>("L");
1962 pfqn::UniqueResult<T> r;
1963 if (all_or_none(a, "pfqn_unique", {"mu", "gamma"})) {
1964 const Matrix<T> mu = a.matrix_or_empty<T>("mu");
1965 const Matrix<T> gamma = a.matrix_or_empty<T>("gamma");
1966 a.done();
1967 r = pfqn::pfqn_unique(L, mu, gamma);
1968 } else {
1969 a.done();
1970 r = pfqn::pfqn_unique(L);
1971 }
1972 Json out;
1973 out["L_unique"] = encode_matrix(r.L);
1974 out["mu_unique"] = encode_matrix(r.mu);
1975 out["gamma_unique"] = encode_matrix(r.gamma);
1976 out["mi"] = encode_ints(r.mi);
1977 out["mapping"] = enc_idx(r.mapping);
1978 return out;
1979 }
1980};
1981
1982struct OpMushift {
1983 template <class T>
1984 static Json run(Args& a) {
1985 const Matrix<T> mu = a.matrix<T>("mu");
1986 const std::vector<std::size_t> iset = indices(a, "iset");
1987 a.done();
1988 Json out;
1989 out["mushifted"] = encode_matrix(pfqn::pfqn_mushift(mu, iset));
1990 return out;
1991 }
1992};
1993
1994struct OpMuMs {
1995 template <class T>
1996 static Json run(Args& a) {
1997 const int N = a.required_integer("N");
1998 const int m = a.required_integer("m");
1999 const int c = a.required_integer("c");
2000 a.done();
2001 Json out;
2002 out["mu"] = encode_vector(pfqn::pfqn_mu_ms<T>(N, m, c));
2003 return out;
2004 }
2005};
2006
2007// ---------------------------------------------------------------------------
2008// Sensitivities and moments
2009// ---------------------------------------------------------------------------
2010
2011struct OpSens {
2012 template <class T>
2013 static Json run(Args& a) {
2014 const Matrix<T> L = a.matrix<T>("L");
2015 const std::vector<int> N = a.ints("N");
2016 const std::vector<T> Z = a.vector<T>("Z");
2017 pfqn::SensResult<T> r;
2018 if (a.has("mi")) {
2019 const std::vector<int> mi = a.ints("mi");
2020 a.done();
2021 r = pfqn::pfqn_sens(L, N, Z, mi);
2022 } else {
2023 a.done();
2024 r = pfqn::pfqn_sens(L, N, Z);
2025 }
2026 Json s;
2027 s["XN"] = encode_vector(r.XN);
2028 s["QN"] = encode_matrix(r.QN);
2029 s["UN"] = encode_matrix(r.UN);
2030 s["CN"] = encode_matrix(r.CN);
2031 Json ps = Json::array();
2032 for (const auto& p : r.params) {
2033 Json e;
2034 e["type"] = std::string(1, p.type);
2035 e["station"] = p.station;
2036 e["class"] = static_cast<std::uint64_t>(p.cls);
2037 ps.push_back(e);
2038 }
2039 s["params"] = ps;
2040 s["dX"] = encode_matrix(r.dX);
2041 s["dQ"] = encode_matrices(r.dQ);
2042 s["dU"] = encode_matrices(r.dU);
2043 s["dR"] = encode_matrices(r.dR);
2044 s["QCov"] = encode_matrix(r.QCov);
2045 s["QVar"] = encode_matrix(r.QVar);
2046 s["QTotVar"] = encode_vector(r.QTotVar);
2047 s["QCovAsym"] = encode_scalar(r.QCovAsym);
2048 Json out;
2049 out["sens"] = s;
2050 return out;
2051 }
2052};
2053
2054struct OpSensLdmxEc {
2055 template <class T>
2056 static Json run(Args& a) {
2057 const std::vector<T> lambda = a.vector<T>("lambda");
2058 const Matrix<T> D = a.matrix<T>("D");
2059 const Matrix<T> mu = a.matrix<T>("mu");
2060 a.done();
2061 const pfqn::SensLdmxEcResult<T> r = pfqn::pfqn_sens_ldmx_ec(lambda, D, mu);
2062 Json out;
2063 out["EC"] = encode_matrix(r.EC);
2064 out["E"] = encode_matrix(r.E);
2065 out["Eprime"] = encode_matrix(r.Eprime);
2066 out["Lo"] = encode_vector(r.Lo);
2067 out["dEC"] = encode_matrix(r.dEC);
2068 out["dE"] = encode_matrix(r.dE);
2069 out["dEprime"] = encode_matrix(r.dEprime);
2070 return out;
2071 }
2072};
2073
2074struct OpSensLinearizer {
2075 template <class T>
2076 static Json run(Args& a) {
2077 const Matrix<T> L = a.matrix<T>("L");
2078 const std::vector<int> N = a.ints("N");
2079 const std::vector<T> Z = a.vector<T>("Z");
2080 pfqn::SensLinearizerResult<T> r;
2081 if (all_or_none(a, "pfqn_sens_linearizer", {"tol", "maxiter"})) {
2082 const T tol = a.number<T>("tol");
2083 const unsigned maxiter = a.uinteger("maxiter");
2084 a.done();
2085 r = pfqn::pfqn_sens_linearizer(L, N, Z, &tol, maxiter);
2086 } else {
2087 a.done();
2088 r = pfqn::pfqn_sens_linearizer(L, N, Z);
2089 }
2090 Json s;
2091 s["XN"] = encode_vector(r.XN);
2092 s["QN"] = encode_matrix(r.QN);
2093 s["UN"] = encode_matrix(r.UN);
2094 s["WN"] = encode_matrix(r.WN);
2095 s["m"] = encode_vector(r.m);
2096 s["dm"] = encode_matrix(r.dm);
2097 s["d2m"] = encode_vector(r.d2m);
2098 s["Cov"] = encode_matrix(r.Cov);
2099 s["Var"] = encode_vector(r.Var);
2100 s["M2"] = encode_vector(r.M2);
2101 s["M3"] = encode_vector(r.M3);
2102 s["Skew"] = encode_vector(r.Skew);
2103 s["CovAsym"] = encode_scalar(r.CovAsym);
2104 s["iter"] = r.iter;
2105 Json out;
2106 out["res"] = s;
2107 return out;
2108 }
2109};
2110
2111struct OpSensMom {
2112 template <class T>
2113 static Json run(Args& a) {
2114 const Matrix<T> L = a.matrix<T>("L");
2115 const std::vector<int> N = a.ints("N");
2116 const std::vector<T> Z = a.vector<T>("Z");
2117 pfqn::SensMomResult<T> r;
2118 if (all_or_none(a, "pfqn_sens_mom", {"mi", "groups"})) {
2119 const std::vector<int> mi = a.ints_or_empty("mi");
2120 const std::vector<int> groups = a.ints_or_empty("groups");
2121 a.done();
2122 r = pfqn::pfqn_sens_mom(L, N, Z, mi, groups);
2123 } else {
2124 a.done();
2125 r = pfqn::pfqn_sens_mom(L, N, Z);
2126 }
2127 Json s;
2128 s["XN"] = encode_vector(r.XN);
2129 s["QN"] = encode_matrix(r.QN);
2130 s["UN"] = encode_matrix(r.UN);
2131 s["CN"] = encode_matrix(r.CN);
2132 s["m"] = encode_matrix(r.m);
2133 s["d2m"] = encode_matrix(r.d2m);
2134 s["Cov"] = encode_matrix(r.Cov);
2135 s["dm"] = encode_matrix(r.dm);
2136 s["Var"] = encode_matrix(r.Var);
2137 s["M2"] = encode_matrix(r.M2);
2138 s["M3"] = encode_matrix(r.M3);
2139 s["Skew"] = encode_matrix(r.Skew);
2140 s["CovAsym"] = encode_scalar(r.CovAsym);
2141 Json out;
2142 out["mom"] = s;
2143 return out;
2144 }
2145};
2146
2147struct OpSensMva {
2148 template <class T>
2149 static Json run(Args& a) {
2150 const Matrix<T> L = a.matrix<T>("L");
2151 const std::vector<int> N = a.ints("N");
2152 const std::vector<T> Z = a.vector<T>("Z");
2153 pfqn::SensMvaResult<T> r;
2154 if (a.has("mi")) {
2155 const std::vector<int> mi = a.ints("mi");
2156 a.done();
2157 r = pfqn::pfqn_sens_mva(L, N, Z, mi);
2158 } else {
2159 a.done();
2160 r = pfqn::pfqn_sens_mva(L, N, Z);
2161 }
2162 Json s;
2163 s["XN"] = encode_vector(r.XN);
2164 s["QN"] = encode_matrix(r.QN);
2165 s["UN"] = encode_matrix(r.UN);
2166 s["CN"] = encode_matrix(r.CN);
2167 s["QCov"] = encode_matrices(r.QCov);
2168 s["QVar"] = encode_matrix(r.QVar);
2169 s["QTotVar"] = encode_vector(r.QTotVar);
2170 s["QCovAsym"] = encode_scalar(r.QCovAsym);
2171 Json out;
2172 out["mom"] = s;
2173 return out;
2174 }
2175};
2176
2177struct OpSensMvaldmx {
2178 template <class T>
2179 static Json run(Args& a) {
2180 const std::vector<T> lambda = a.vector<T>("lambda");
2181 const Matrix<T> D = a.matrix<T>("D");
2182 const std::vector<int> N = pop_with_open(a, "N");
2183 const std::vector<T> Z = a.vector<T>("Z");
2184 const Matrix<T> mu = a.matrix<T>("mu");
2185 a.done();
2186 const pfqn::SensMvaldmxResult<T> r = pfqn::pfqn_sens_mvaldmx(lambda, D, N, Z, mu);
2187 Json s;
2188 s["XN"] = encode_vector(r.XN);
2189 s["QN"] = encode_matrix(r.QN);
2190 s["UN"] = encode_matrix(r.UN);
2191 s["CN"] = encode_matrix(r.CN);
2192 s["QCov"] = encode_matrices(r.QCov);
2193 s["QCovFull"] = encode_matrix(r.QCovFull);
2194 s["QVar"] = encode_matrix(r.QVar);
2195 s["QTotVar"] = encode_vector(r.QTotVar);
2196 s["QCovAsym"] = encode_scalar(r.QCovAsym);
2197 Json out;
2198 out["mom"] = s;
2199 return out;
2200 }
2201};
2202
2203struct OpSensMomldmx {
2204 template <class T>
2205 static Json run(Args& a) {
2206 const std::vector<T> lambda = a.vector<T>("lambda");
2207 const Matrix<T> D = a.matrix<T>("D");
2208 const std::vector<int> N = pop_with_open(a, "N");
2209 const std::vector<T> Z = a.vector<T>("Z");
2210 const Matrix<T> mu = a.matrix<T>("mu");
2211 const std::vector<int> groups = a.ints_or_empty("groups");
2212 a.done();
2213 const pfqn::SensMomResult<T> r = pfqn::pfqn_sens_momldmx(lambda, D, N, Z, mu, groups);
2214 Json s;
2215 s["XN"] = encode_vector(r.XN);
2216 s["QN"] = encode_matrix(r.QN);
2217 s["UN"] = encode_matrix(r.UN);
2218 s["CN"] = encode_matrix(r.CN);
2219 s["m"] = encode_matrix(r.m);
2220 s["d2m"] = encode_matrix(r.d2m);
2221 s["Cov"] = encode_matrix(r.Cov);
2222 s["dm"] = encode_matrix(r.dm);
2223 s["Var"] = encode_matrix(r.Var);
2224 s["M2"] = encode_matrix(r.M2);
2225 s["M3"] = encode_matrix(r.M3);
2226 s["Skew"] = encode_matrix(r.Skew);
2227 s["CovAsym"] = encode_scalar(r.CovAsym);
2228 Json out;
2229 out["mom"] = s;
2230 return out;
2231 }
2232};
2233
2234struct OpSensRespt {
2235 template <class T>
2236 static Json run(Args& a) {
2237 const std::vector<T> S = a.vector<T>("S");
2238 const Matrix<T> V = a.matrix<T>("V");
2239 const std::vector<int> N = a.ints("N");
2240 const std::vector<T> Z = a.vector<T>("Z");
2241 pfqn::SensResptResult<T> r;
2242 if (all_or_none(a, "pfqn_sens_respt", {"b", "tmax"})) {
2243 const std::vector<int> b = a.ints("b");
2244 const int tmax = a.required_integer("tmax");
2245 a.done();
2246 r = pfqn::pfqn_sens_respt(S, V, N, Z, b, tmax);
2247 } else {
2248 a.done();
2249 r = pfqn::pfqn_sens_respt(S, V, N, Z);
2250 }
2251 Json s;
2252 s["XN"] = encode_vector(r.XN);
2253 s["QN"] = encode_matrix(r.QN);
2254 s["UN"] = encode_matrix(r.UN);
2255 s["W"] = encode_matrix(r.W);
2256 s["WM"] = encode_matrices(r.WM);
2257 s["WVar"] = encode_matrix(r.WVar);
2258 s["WSkew"] = encode_matrix(r.WSkew);
2259 s["m"] = encode_vector(r.m);
2260 s["Var"] = encode_vector(r.Var);
2261 s["p"] = encode_matrix(r.p);
2262 s["Wresid"] = encode_matrix(r.Wresid);
2263 Json out;
2264 out["res"] = s;
2265 return out;
2266 }
2267};
2268
2269struct OpQlenJointMoments {
2270 template <class T>
2271 static Json run(Args& a) {
2272 const Matrix<T> L = a.matrix<T>("L");
2273 const std::vector<int> N = a.ints("N");
2274 const std::vector<T> Z = a.vector<T>("Z");
2275 a.unsupported("lGsrc", "the injected log G source is a callable, which has no JSON "
2276 "representation; omit it to have pfqn_nc supply log G");
2277 std::vector<std::pair<std::size_t, std::size_t> > pairs;
2278 if (a.has("pairs")) {
2279 const Matrix<int> P = int_matrix(a, "pairs");
2280 if (!P.empty() && P.cols() != 2)
2281 throw InputError("pfqn_qlen_joint_moments: 'pairs' must be a P x 2 matrix of "
2282 "0-based (station, class) indices");
2283 for (std::size_t p = 0; p < P.rows(); ++p) {
2284 if (P(p, 0) < 0 || P(p, 1) < 0)
2285 throw InputError("pfqn_qlen_joint_moments: 'pairs' holds 0-based indices");
2286 pairs.emplace_back(static_cast<std::size_t>(P(p, 0)),
2287 static_cast<std::size_t>(P(p, 1)));
2288 }
2289 }
2290 const bool lng = all_or_none(a, "pfqn_qlen_joint_moments", {"route", "options"});
2291 pfqn::QlenJointMomentsResult<T> r;
2292 if (lng) {
2293 const pfqn::QlenJointRoute route = enum_of<pfqn::QlenJointRoute>(
2294 "pfqn_qlen_joint_moments", "route", a.text("route", ""),
2295 {{"auto", pfqn::QlenJointRoute::Auto},
2296 {"tail", pfqn::QlenJointRoute::Tail},
2297 {"pmf", pfqn::QlenJointRoute::Pmf}});
2298 OptObj o(a, "options", "pfqn_qlen_joint_moments");
2299 const pfqn::NcMethod method = pfqn::nc_method_of(o.text("method"));
2300 pfqn::NcOptions nopt = nc_options(o, "pfqn_qlen_joint_moments");
2301 if (o.has("tol")) nopt.tol = o.number<double>("tol");
2302 o.done();
2303 a.done();
2304 r = pfqn::pfqn_qlen_joint_moments(L, N, Z, pairs, route,
2305 pfqn::QlenJointLgSource<T>(), method, nopt);
2306 } else {
2307 a.done();
2308 r = a.has("pairs") ? pfqn::pfqn_qlen_joint_moments(L, N, Z, pairs)
2309 : pfqn::pfqn_qlen_joint_moments(L, N, Z);
2310 }
2311 Json out;
2312 out["tail"] = enc_tensor(r.tail);
2313 out["binomial"] = enc_tensor(r.binomial);
2314 out["factorial"] = enc_tensor(r.factorial);
2315 out["raw"] = enc_tensor(r.raw);
2316 out["central"] = enc_tensor(r.central);
2317 out["cumulant"] = enc_tensor(r.cumulant);
2318 out["mean"] = encode_vector(r.mean);
2319 out["cov"] = encode_matrix(r.cov);
2320 Json info;
2321 info["route"] = r.route;
2322 info["points"] = encode_count(r.points);
2323 info["served"] = encode_count(r.served);
2324 info["evals"] = encode_count(r.evals);
2325 info["exact"] = r.exact;
2326 Json pj = Json::array();
2327 for (const auto& p : r.pairs)
2328 pj.push_back(Json::array({static_cast<std::uint64_t>(p.first),
2329 static_cast<std::uint64_t>(p.second)}));
2330 info["pairs"] = pj;
2331 info["dims"] = enc_idx(r.dims);
2332 out["info"] = info;
2333 return out;
2334 }
2335};
2336
2337struct OpResptPsMoments {
2338 template <class T>
2339 static Json run(Args& a) {
2340 const std::vector<T> S = a.vector<T>("S");
2341 const std::vector<long> N = longs(a, "N");
2342 const std::vector<T> Z = a.vector<T>("Z");
2343 pfqn::ResptPsMomentsResult<T> r;
2344 if (a.has("method")) {
2345 const pfqn::ResptPsRoute m = enum_of<pfqn::ResptPsRoute>(
2346 "pfqn_respt_ps_moments", "method", a.text("method", ""),
2347 {{"auto", pfqn::ResptPsRoute::Auto},
2348 {"exact", pfqn::ResptPsRoute::Exact},
2349 {"asymptotic", pfqn::ResptPsRoute::Asymptotic}});
2350 a.done();
2351 r = pfqn::pfqn_respt_ps_moments(S, N, Z, m);
2352 } else {
2353 a.done();
2354 r = pfqn::pfqn_respt_ps_moments(S, N, Z);
2355 }
2356 Json o;
2357 Json ms = Json::array();
2358 for (pfqn::ResptPsMethod m : r.method) {
2359 switch (m) {
2360 case pfqn::ResptPsMethod::None: ms.push_back("none"); break;
2361 case pfqn::ResptPsMethod::Exact: ms.push_back("exact"); break;
2362 case pfqn::ResptPsMethod::Asymptotic: ms.push_back("asymptotic"); break;
2363 case pfqn::ResptPsMethod::Unavailable: ms.push_back("unavailable"); break;
2364 }
2365 }
2366 o["method"] = ms;
2367 o["c0"] = encode_vector(r.c0);
2368 o["c1"] = encode_vector(r.c1);
2369 o["alpha"] = encode_vector(r.alpha);
2370 o["nstates"] = encode_vector(r.nstates);
2371 o["expansionParam"] = encode_scalar(r.expansionParam);
2372 Json out;
2373 out["W"] = encode_vector(r.W);
2374 out["W2"] = encode_vector(r.W2);
2375 out["out"] = o;
2376 return out;
2377 }
2378};
2379
2380
2381// ---------------------------------------------------------------------------
2382// Seeded Monte Carlo estimators: the port draws from a caller-owned
2383// pfqn::McRng, which the JSON form names by a required seed (required_seed).
2384// ---------------------------------------------------------------------------
2385
2386/**
2387 * pfqn_mcmc(L, N, Z, s, options): MATLAB's [X, Q, ci]. options carries
2388 * samples (1e5), seed (required) and config.mcmc_batches (30) and
2389 * config.mcmc_burnin (0.1); s lists servers per station, Inf for a delay.
2390 */
2391struct OpMcmc {
2392 template <class T>
2393 static Json run(Args& a) {
2394 const Matrix<T> L = a.matrix<T>("L");
2395 const std::vector<int> N = a.ints("N");
2396 const std::vector<T> Z = a.vector_or_empty<T>("Z");
2397 const std::vector<double> sv = a.vector_or_empty<double>("s");
2398 const Json* oj = a.opt("options");
2399 a.done();
2400 if (!oj)
2401 throw InputError("pfqn_mcmc: options.seed is required; the port draws from a seeded "
2402 "generator and has no session RNG to fall back on");
2403 Args op(*oj, "pfqn_mcmc options");
2404 const std::size_t samples = op.count("samples", 100000);
2405 std::size_t nbatches = pfqn::MCMC_DEFAULT_BATCHES;
2406 double burnin = pfqn::MCMC_DEFAULT_BURNIN;
2407 if (const Json* cj = op.opt("config")) {
2408 Args cfg(*cj, "pfqn_mcmc options.config");
2409 nbatches = cfg.count("mcmc_batches", nbatches);
2410 burnin = cfg.scalar<double>("mcmc_burnin", burnin);
2411 cfg.done();
2412 }
2413 const std::uint64_t seed = required_seed(op, "pfqn_mcmc", "options.seed");
2414 op.done();
2415 pfqn::McRng rng(seed);
2416 const pfqn::McmcResult<T> r = pfqn::pfqn_mcmc(L, N, Z, sv, samples, nbatches, burnin, rng);
2417 Json ci;
2418 ci["Xse"] = encode_vector(r.Xse);
2419 ci["Xlo"] = encode_vector(r.Xlo);
2420 ci["Xhi"] = encode_vector(r.Xhi);
2421 ci["Qse"] = encode_matrix(r.Qse);
2422 ci["Qlo"] = encode_matrix(r.Qlo);
2423 ci["Qhi"] = encode_matrix(r.Qhi);
2424 ci["batches"] = encode_count(r.batches);
2425 ci["samples"] = encode_count(r.samples);
2426 ci["burnin"] = encode_count(r.burnin);
2427 Json out;
2428 out["X"] = encode_vector(r.X);
2429 out["Q"] = encode_matrix(r.Q);
2430 out["ci"] = ci;
2431 return out;
2432 }
2433};
2434
2435/**
2436 * pfqn_mci(D, N, Z, I, variant, seed): Monte Carlo integration estimate of
2437 * [G, lG]. The port returns the estimate, not MATLAB's third output lZ (the
2438 * per-sample log values), so lZ is not in the result.
2439 */
2440struct OpMci {
2441 template <class T>
2442 static Json run(Args& a) {
2443 const Matrix<T> D = a.matrix<T>("D");
2444 const std::vector<int> N = a.ints("N");
2445 const std::vector<T> Z = a.vector_or_empty<T>("Z");
2446 const std::size_t I = a.count("I", 100000);
2447 std::string v = a.text("variant", "imci");
2448 for (char& c : v) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
2449 pfqn::MciVariant variant;
2450 if (v == "imci") variant = pfqn::MciVariant::Imci;
2451 else if (v == "mci") variant = pfqn::MciVariant::Mci;
2452 else if (v == "rm") variant = pfqn::MciVariant::Rm;
2453 else if (v == "amci") variant = pfqn::MciVariant::Amci;
2454 else if (v == "lhsmci") variant = pfqn::MciVariant::Lhsmci;
2455 else throw InputError("pfqn_mci: variant must be one of imci, mci, rm, amci, lhsmci; got '" +
2456 v + "'");
2457 pfqn::McRng rng(required_seed(a, "pfqn_mci"));
2458 a.done();
2459 const pfqn::NcResult<T> r = pfqn::pfqn_mci(D, N, Z, I, variant, rng);
2460 Json out;
2461 out["G"] = encode_scalar(r.G);
2462 out["lG"] = encode_scalar(r.lG);
2463 return out;
2464 }
2465};
2466
2467/** pfqn_mmsample2(L, N, Z, samples, seed): McKenna-Mitra sampling estimate. */
2468struct OpMmsample2 {
2469 template <class T>
2470 static Json run(Args& a) {
2471 const Matrix<T> L = a.matrix<T>("L");
2472 const std::vector<int> N = a.ints("N");
2473 const std::vector<T> Z = a.vector_or_empty<T>("Z");
2474 const std::size_t samples = a.count("samples", 100000);
2475 pfqn::McRng rng(required_seed(a, "pfqn_mmsample2"));
2476 a.done();
2477 const pfqn::NcResult<T> r = pfqn::pfqn_mmsample2(L, N, Z, samples, rng);
2478 Json out;
2479 out["G"] = encode_scalar(r.G);
2480 out["lG"] = encode_scalar(r.lG);
2481 return out;
2482 }
2483};
2484
2485} // namespace
2486
2488 t["pfqn_manjunath"] = make<OpManjunath>();
2489 t["pfqn_marie"] = make<NeedsTranscendental<OpMarie> >();
2490 t["pfqn_stdf"] = make<NeedsTranscendental<OpStdf<false> > >();
2491 t["pfqn_stdf_heur"] = make<NeedsTranscendental<OpStdf<true> > >();
2492 t["pfqn_mvams"] = make<OpMvams>();
2493 t["pfqn_mvablo"] = make<OpMvablo>();
2494 t["pfqn_oqnrc"] = make<DoubleOnly<OpOqnrc> >();
2495 t["pfqn_debemva"] = make<OpDebemva>();
2496 t["pfqn_mvald"] = make<OpMvald>();
2497 t["pfqn_mvamx"] = make<OpMvamx>();
2498 t["pfqn_mvaldmx"] = make<OpMvaldmx>();
2499 t["pfqn_mvaldms"] = make<OpMvaldms>();
2500 t["pfqn_xzabalow"] = make<OpXzBound<XzAbaLow> >();
2501 t["pfqn_xzabaup"] = make<OpXzBound<XzAbaUp> >();
2502 t["pfqn_xzgsblow"] = make<NeedsTranscendental<OpXzBound<XzGsbLow> > >();
2503 t["pfqn_xzgsbup"] = make<NeedsTranscendental<OpXzBound<XzGsbUp> > >();
2504 t["pfqn_qzgblow"] = make<OpQzgb<false> >();
2505 t["pfqn_qzgbup"] = make<OpQzgb<true> >();
2506 t["pfqn_ssd"] = make<OpSsd>();
2507 t["pfqn_unique"] = make<OpUnique>();
2508 t["pfqn_mushift"] = make<OpMushift>();
2509 t["pfqn_qsa"] = make<NeedsTranscendental<OpQsa> >();
2510 t["pfqn_qdamva"] = make<NeedsTranscendental<OpQdamva> >();
2511 t["pfqn_qdlin"] = make<NeedsTranscendental<OpQdlin> >();
2512 t["pfqn_schmidt"] = make<NeedsTranscendental<OpSchmidt> >();
2513 t["pfqn_schmidt_ext"] = make<NeedsTranscendental<OpSchmidtExt> >();
2514 t["pfqn_mu_ms"] = make<OpMuMs>();
2515 t["pfqn_nc"] = make<OpNc>();
2516 t["pfqn_nc_sanitize"] = make<OpNcSanitize>();
2517 t["pfqn_ncld"] = make<OpNcld>();
2518 t["pfqn_ncldmx"] = make<OpNcldmx>();
2519 t["pfqn_perm"] = make<OpPerm>();
2520 t["pfqn_mcub"] = make<OpMcub>();
2521 t["pfqn_mmint2"] = make<NeedsTranscendental<OpMmint2> >();
2522 t["pfqn_mwrbb"] = make<OpMwrbb>();
2523 t["pfqn_nre"] = make<NeedsTranscendental<OpNr<NreCall> > >();
2524 t["pfqn_nrl"] = make<NeedsTranscendental<OpNr<NrlCall> > >();
2525 t["pfqn_nrp"] = make<NeedsTranscendental<OpNr<NrpCall> > >();
2526 t["pfqn_oi_fnc"] = make<OpOiFnc>();
2527 t["pfqn_panacea"] = make<NeedsTranscendental<OpPanacea> >();
2528 t["pfqn_panaceald"] = make<NeedsTranscendental<OpPanaceald> >();
2529 t["pfqn_pbh"] = make<OpPbh>();
2530 t["pfqn_propfair"] = make<NeedsTranscendental<OpPropfair> >();
2531 t["pfqn_sens"] = make<OpSens>();
2532 t["pfqn_sens_ldmx_ec"] = make<OpSensLdmxEc>();
2533 t["pfqn_sens_linearizer"] = make<NeedsTranscendental<OpSensLinearizer> >();
2534 t["pfqn_sens_mom"] = make<OpSensMom>();
2535 t["pfqn_sens_momldmx"] = make<OpSensMomldmx>();
2536 t["pfqn_sens_mva"] = make<NeedsTranscendental<OpSensMva> >();
2537 t["pfqn_sens_mvaldmx"] = make<OpSensMvaldmx>();
2538 t["pfqn_sens_respt"] = make<OpSensRespt>();
2539 t["pfqn_scb"] = make<OpScb>();
2540 t["pfqn_scbgap"] = make<OpScbgap>();
2541 t["pfqn_usumbound"] = make<OpUsumbound>();
2542 t["pfqn_minclasses"] = make<OpMinclasses>();
2543 t["pfqn_sib"] = make<NeedsTranscendental<OpSib> >();
2544 t["pfqn_mvams_closed"] = make<OpMvamsClosed>();
2545 t["pfqn_kriz_bounds"] = make<NeedsTranscendental<OpKrizBounds> >();
2546 t["pfqn_ibjb_bounds"] = make<OpIbjbBounds>();
2547 t["pfqn_ldsy_bounds"] = make<OpLdsyBounds>();
2548 t["pfqn_sqni"] = make<NeedsTranscendental<OpSqni> >();
2549 t["pfqn_rd"] = make<NeedsTranscendental<OpRd> >();
2550 t["pfqn_rgf"] = make<NeedsTranscendental<OpRgf> >();
2551 t["pfqn_rgfmc"] = make<NeedsTranscendental<OpRgfmc> >();
2552 t["pfqn_nintmva"] = make<OpNintmva>();
2553 t["pfqn_qlen_joint_moments"] = make<NeedsTranscendental<OpQlenJointMoments> >();
2554 t["pfqn_respt_ps_moments"] = make<NeedsTranscendental<OpResptPsMoments> >();
2555 t["pfqn_momlin"] = make<NeedsTranscendental<OpMomlin> >();
2556 t["pfqn_mva_interval"] = make<OpMvaInterval>();
2557 t["pfqn_mvac"] = make<OpMvac>();
2558 t["pfqn_mvacld"] = make<OpMvacld>();
2559 t["pfqn_pam"] = make<OpPam>();
2560 t["pfqn_scat"] = make<NeedsTranscendental<OpScat> >();
2561 t["pfqn_sdr"] = make<OpSdr>();
2562 t["pfqn_tay"] = make<NeedsTranscendental<OpTay> >();
2563 t["pfqn_xia"] = make<NeedsTranscendental<OpXia> >();
2564 t["pfqn_mcmc"] = make<NeedsTranscendental<OpMcmc> >();
2565 t["pfqn_mci"] = make<NeedsTranscendental<OpMci> >();
2566 t["pfqn_mmsample2"] = make<NeedsTranscendental<OpMmsample2> >();
2567}
2568
2570 const std::string oirate =
2571 "the per-station service rates are callables of the station state "
2572 "(std::function), which have no JSON representation.";
2573 t["pfqn_oi_insvc"] = "the in-service rate oirate is a callable of the station state "
2574 "(std::function), which has no JSON representation.";
2575 t["pfqn_mvajd"] = oirate;
2576 t["pfqn_mvaoi"] = oirate;
2577 t["pfqn_mvaoi_marg"] = oirate;
2578 t["pfqn_ncoi"] = oirate;
2579 t["pfqn_oi_mixed"] = "the order-independent rate mu is a callable of the per-class count "
2580 "vector (std::function), which has no JSON representation.";
2581 t["pfqn_oi_open"] = t["pfqn_oi_mixed"];
2582 t["pfqn_oi_respt"] = t["pfqn_oi_mixed"];
2583 t["pfqn_ncjd"] = oirate;
2584 t["pfqn_pas_nc"] = oirate;
2585 t["pfqn_oi_is"] = "the per-station rates are callables (std::function), which have no JSON "
2586 "representation.";
2587 t["pfqn_pas_is"] = t["pfqn_oi_is"];
2588}
2589
2590} // namespace reg
2591} // namespace line
Internal to the –api dispatcher: what a per-domain translation unit needs to add entries to the table...
InputError(const std::string &what)
Definition error.h:105
Named-argument reader over the parsed JSON object.
Definition api_json.h:391
const std::string * get(const Element *el, const std::string &key)
el.get(key), with Python's None as a null pointer.
Definition srvn_writer.h:62
SqniResult< T > pfqn_sqni(const std::vector< T > &N, const std::vector< T > &L, const std::vector< T > &Z)
Square-root non-iterative (SQNI) approximation for a single queueing station with per-class delay.
Definition pfqn_sqni.h:57
RgfmcResult< T > pfqn_rgfmc(const Matrix< T > &L, const std::vector< int > &N, const std::vector< T > &Z, const T &tol, std::size_t maxterms, const T &maxcancel)
Multiclass Recursion by Generating Functions (RGF), with think times.
Definition pfqn_rgfmc.h:465
RdResult< T > pfqn_rd(const Matrix< T > &L0, const std::vector< int > &N, const Matrix< T > &Z, const Matrix< T > &mu0, double tol, NcMethod method)
Reduction heuristic (RD) for the normalizing constant of a closed LOAD-DEPENDENT product-form network...
Definition pfqn_rd.h:101
MvaIntervalResult< T > pfqn_mva_interval(const Matrix< T > &L, int nlo, int nup, const T &zlo, const T &zup)
Exact interval-valued MVA for single-class closed product-form networks.
QlenJointMomentsResult< T > pfqn_qlen_joint_moments(const Matrix< T > &L, const std::vector< int > &N, const std::vector< T > &Z, const std::vector< std::pair< std::size_t, std::size_t > > &pairs, QlenJointRoute route, const QlenJointLgSource< T > &lGsrc, NcMethod method, const NcOptions &nopt)
Joint moments of the queue-length vector of a closed product-form network, obtained from normalizing ...
T pfqn_xia(const std::vector< T > &L, int N, const std::vector< T > &s)
Xia's asymptotic approximation of the normalizing constant of a load-dependent (multiserver) closed n...
Definition pfqn_xia.h:90
PanaceaLdResult< T > pfqn_panaceald(const Matrix< T > &L, const std::vector< int > &N, const std::vector< T > &Z, const Matrix< T > &mu, int terms)
PANACEA normal-usage asymptotic expansion for LOAD-DEPENDENT closed networks (Mitra and McKenna,...
SchmidtExtResult< T > pfqn_schmidt_ext(const Matrix< T > &D, const std::vector< int > &N, const Matrix< int > &S, const std::vector< SchedStrategy > &sched)
Extended Schmidt MVA with queue-aware alpha corrections.
SensResptResult< T > pfqn_sens_respt(const std::vector< T > &S, const Matrix< T > &V, const std::vector< int > &N, const std::vector< T > &Z, const std::vector< int > &b, int tmax)
Exact raw moments E[W^t], t = 1..3, of the sojourn time of a job at an FCFS b-server center of a clos...
LinearizerResult< T > pfqn_scat(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const std::vector< SchedStrategy > &type, double tol, int maxiter, const Matrix< T > &QN0)
Neuse-Chandy SCAT (Self-Correcting Approximation Technique) approximate MVA.
Definition pfqn_scat.h:71
OqnrcResult pfqn_oqnrc(const std::vector< double > &lam, const Matrix< double > &A, const Matrix< double > &S, const Matrix< double > &SCV, const std::vector< double > &nservers, const std::vector< bool > &isps, const std::vector< int > &pool, const std::vector< int > &Ncap, bool waitq, double tol, int iter_max)
Runs OQNRC.
Definition pfqn_oqnrc.h:179
StdfResult< T > pfqn_stdf(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const std::vector< int > &S, const std::vector< std::size_t > &fcfsNodes, const Matrix< T > &rates, const std::vector< T > &tset)
Sojourn-time distribution at the listed FCFS stations.
Definition pfqn_stdf.h:322
KrizBounds< T > pfqn_kriz_bounds(const std::vector< T > &Lin, int N, const T &Z, int level=0)
Kriz (1984) throughput bounds.
AmvaResult< T > pfqn_pam(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, PamVariant variant=PamVariant::Basic)
Hsieh-Lam Proportional Approximation Methods (PAMB / PAMI / PAMT).
Definition pfqn_pam.h:55
NcldmxResult< T > pfqn_ncldmx(const std::vector< T > &lambda, const Matrix< T > &D, const std::vector< int > &N, const Matrix< T > &Z, const Matrix< T > &mu, const std::string &method, const T &atol, const NcOptions &nopt)
Normalizing constant of a MIXED open/closed network with limited load dependence.
ScbBounds< T > pfqn_scb(const std::vector< T > &L, long N)
Bracket on the throughput and the per-device utilizations of the UNKNOWN multiclass system whose sing...
Definition pfqn_scb.h:67
NcSanitizeResult< T > pfqn_nc_sanitize(const std::vector< T > &lambda, const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const T &atol)
Preprocessing shared by the normalizing-constant solvers: drop the classes that cannot contribute,...
MarieResult< T > pfqn_marie(const Matrix< T > &L, const std::vector< int > &N, const std::vector< T > &Z, const Matrix< T > &scv, double tol, int maxiter, const std::vector< int > &nservers)
Marie's method for a closed network with FCFS Coxian service.
Definition pfqn_marie.h:647
SensMvaResult< T > pfqn_sens_mva(const Matrix< T > &L, const std::vector< int > &N, const std::vector< T > &Z, const std::vector< int > &mi)
Exact per-station queue-length variances and covariances of a closed product-form network,...
SensLinearizerResult< T > pfqn_sens_linearizer(const Matrix< T > &L, const std::vector< int > &N, const std::vector< T > &Z, const T *tol, unsigned maxiter)
Approximate moments E[Q_i], Var[Q_i], Cov[Q_i,Q_j], E[Q_i^2] and E[Q_i^3] of the per-station total qu...
long pfqn_minclasses(const T &Usum, long K, long N)
Smallest number of customer classes R consistent with an observed sum of device utilizations,...
Definition pfqn_scb.h:192
MvaResult< T > pfqn_mvams(const std::vector< T > &lambda, const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const std::vector< int > &mi, const std::vector< int > &S)
General-purpose exact MVA for mixed networks with multiserver stations.
Definition pfqn_mvams.h:893
UniqueResult< T > pfqn_unique(const Matrix< T > &L, const Matrix< T > &mu, const Matrix< T > &gamma)
Merge stations whose (L, mu, gamma) rows are exactly equal.
Definition pfqn_unique.h:47
McubBounds< T > pfqn_mcub(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z)
Kerola's multiclass composite bound (Perf.
Definition pfqn_mcub.h:55
PropfairResult< T > pfqn_propfair(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z)
Proportionally fair allocation estimate of the normalizing constant (Schweitzer 1979; Walton,...
NcldResult< T > pfqn_ncld(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const Matrix< T > &mu, const std::string &method_name, const T &atol, const NcOptions &nopt)
Normalizing constant of a LOAD-DEPENDENT closed network: the dispatcher.
Definition pfqn_ncld.h:167
std::function< std::vector< double >(const Matrix< T > &, const std::vector< std::vector< int > > &)> QlenJointLgSource
Injected source of log G.
LdsyBounds< T > pfqn_ldsy_bounds(const std::vector< T > &Lin, int N, const T &Zin, const Matrix< T > &muIn)
Shanthikumar-Yao (1988) throughput bounds for queue-dependent rates.
MvaResult< T > pfqn_mvamx(const std::vector< T > &lambda, const Matrix< T > &D, const std::vector< int > &N, const Matrix< T > &Z, const std::vector< int > &mi)
Exact MVA for a mixed open/closed network of single-server stations.
Definition pfqn_mvams.h:314
T pfqn_xzabaup(const std::vector< T > &L, const T &N, const T &Z)
X <= min(1/Lmax, N/(sum(L)+Z)): capacity bound and population bound.
NcMethod nc_method_of(const std::string &s)
Map a method name to its enum; throws UnsupportedError on an unknown one.
Definition pfqn_nc.h:177
QlenJointRoute
Which survival-array identity is used.
MvabloResult< T > pfqn_mvablo(long N, const std::vector< T > &S, const Matrix< T > &P, const std::vector< double > &K, std::vector< bool > isdelay, double tol=1e-4, int iter_max=1000)
Runs MVABLO.
Definition pfqn_mvablo.h:75
T pfqn_xzabalow(const std::vector< T > &L, const T &N, const T &Z)
X >= N / (Z + N sum(L)), the ABA population bound.
MvaLdResult< T > pfqn_mvald(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const Matrix< T > &mu, bool stabilize=true)
Exact MVA for a closed network of load-dependent stations.
Definition pfqn_mvams.h:130
StdfResult< T > pfqn_stdf_heur(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const std::vector< int > &S, const std::vector< std::size_t > &fcfsNodes, const Matrix< T > &rates, const std::vector< T > &tset)
Heuristic sojourn-time distribution at the listed FCFS stations.
T pfqn_nre(const Matrix< T > &L0, const std::vector< T > &N, const std::vector< T > &Z, const Matrix< T > &alpha0)
Saddle-tilted Edgeworth approximation of log G for a limited load-dependent model.
Definition pfqn_nre.h:466
MwrbbSched
Station discipline codes, matching the MATLAB sched argument.
Definition pfqn_mwrbb.h:48
AmvaResult< T > pfqn_tay(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, double tol=1e-6, std::size_t maxiter=1000, const Matrix< T > &QN0=Matrix< T >())
Tay's arrival-instant approximate MVA.
Definition pfqn_tay.h:83
T pfqn_xzgsbup(const std::vector< T > &L, const T &N, const T &Z)
X = 2N / (R + sqrt(R^2 - 4 Z Lmax N)), R from the geometric queue bound.
MvaResult< T > pfqn_mvaldms(const std::vector< T > &lambda, const Matrix< T > &D, const std::vector< int > &N, const Matrix< T > &Z, const std::vector< int > &S)
Exact MVA for mixed open/closed networks with multiserver stations.
Definition pfqn_mvams.h:820
DebemvaResult< T > pfqn_debemva(long N, const std::vector< T > &S, const Matrix< T > &P, const std::vector< double > &K, std::vector< bool > isdelay, double tol=0.0, long iter_max=100000)
Runs debeMVA.
QdAmvaResult< T > pfqn_qdamva(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, const Matrix< T > &mu, const Matrix< T > &Q0, double tol=1e-6, std::size_t maxiter=10000)
QD-AMVA: queue-dependent approximate mean value analysis.
Definition pfqn_qdamva.h:88
AmvaResult< T > pfqn_qsa(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, const std::vector< AmvaSched > &type, double tol=1e-10, std::size_t maxiter=100, int levels=3)
Queue-Shift Approximation (QSA) for closed product-form networks.
Definition pfqn_qsa.h:221
T pfqn_usumbound(long R, long K, long N)
Largest value the sum of device utilizations can take in any closed product-form network with R class...
Definition pfqn_scb.h:168
SibBounds< T > pfqn_sib(const std::vector< T > &L, int N, const T &Z, int level)
Successively Improving Bounds (Srinivasan 1985/1987) on the cycle time and throughput of a single-cla...
Definition pfqn_sib.h:86
SchmidtResult< T > pfqn_schmidt(const Matrix< T > &D, const std::vector< int > &N, const Matrix< int > &S, const std::vector< SchedStrategy > &sched, const Matrix< T > &v)
Schmidt's MVA for closed networks with general scheduling disciplines and class-dependent multiserver...
SdrResult< T > pfqn_sdr(const Matrix< T > &S, const Matrix< T > &xi, const std::vector< std::size_t > &N, const SdrStruct &sdr, const Matrix< T > &alpha=Matrix< T >())
Exact product form of eq.
Definition pfqn_sdr.h:304
T pfqn_qzgbup(const std::vector< T > &L, const T &N, const T &Z, std::size_t i)
As the lower bound, with Y from the ABA upper bound and the sigma term.
Definition pfqn_qzgbup.h:34
MvaResult< T > pfqn_mvaldmx(const std::vector< T > &lambda, const Matrix< T > &D, const std::vector< int > &N, const Matrix< T > &Z, const Matrix< T > &mu)
Exact MVA for mixed open/closed networks with limited load dependence.
Definition pfqn_mvams.h:577
MmintResult< T > pfqn_mmint2(const std::vector< T > &L, const std::vector< T > &N, const std::vector< T > &Z)
Adaptive form (MATLAB pfqn_mmint2): Gauss-Kronrod on [0, u_hi], u_hi past the mode where log f has fa...
Definition pfqn_mmint2.h:83
OiFncResult< T > pfqn_oi_fnc(const std::vector< T > &Phi, const std::vector< int > &N, const std::function< T(const std::vector< int > &)> &f)
Order-independent (OI) functional server: the balance function Psi and the rate mu_f of an auxiliary ...
Definition pfqn_oi_fnc.h:76
PanaceaResult< T > pfqn_panacea(const Matrix< T > &L, const std::vector< int > &N, const std::vector< T > &Z, int terms)
PANACEA normal-usage asymptotic expansion of the normalizing constant (Ramakrishnan and Mitra,...
T pfqn_scbgap(long N, long K, long r, bool undominated)
Demand-free bound on the relative throughput error incurred when r of the N single-customer classes a...
Definition pfqn_scb.h:135
T pfqn_nrp(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, const Matrix< T > &alpha)
Norlund-Rice probit approximation of log G.
Definition pfqn_nrl.h:140
RgfResult< T > pfqn_rgf(const std::vector< T > &L, int N, const T &Z)
Recursion by Generating Functions (RGF) for the normalizing constant of a SINGLE-CLASS closed product...
Definition pfqn_rgf.h:91
MwrbbBounds< T > pfqn_mwrbb(const Matrix< T > &V, const Matrix< T > &S, const std::vector< T > &N, const std::vector< T > &Z, const std::vector< MwrbbSched > &sched, const std::vector< int > &prio)
Majumdar-Woodside robust box bounds on the per-class throughput of a closed multiclass network with m...
Definition pfqn_mwrbb.h:161
NintMvaResult< T > pfqn_nintmva(const std::vector< T > &L, const T &N, const T &Z)
Mean value analysis at a nonintegral population (fractional-base aMVA).
SensMomResult< T > pfqn_sens_mom(const Matrix< T > &L, const std::vector< int > &N, const std::vector< T > &Z, const std::vector< int > &mi, const std::vector< int > &groups)
Exact moments E[Q], Var[Q], E[Q^2] and E[Q^3] of the grouped queue lengths of a closed product-form n...
MvamsClosedResult< T > pfqn_mvams_closed(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const std::vector< T > &mi, const std::vector< int > &S, bool wantP0=true)
Memory-bounded exact MVA for closed networks with multiserver stations (Reiser 1981).
PbhBounds< T > pfqn_pbh(const std::vector< T > &L, int N, const T &Z, int level)
Performance Bound Hierarchy (Eager and Sevcik 1983, ACM TOCS 1(2):99-115) for single-class closed pro...
Definition pfqn_pbh.h:114
Matrix< T > pfqn_mushift(const Matrix< T > &mu, const std::vector< std::size_t > &iset)
Shift the load-dependent service-rate lattice of selected stations.
NcMethod
The methods this port dispatches, one per compute_norm_const case.
Definition pfqn_nc.h:101
SensLdmxEcResult< T > pfqn_sens_ldmx_ec(const std::vector< T > &lambda, const Matrix< T > &D, const Matrix< T > &mu)
Effective capacity terms of the mixed load-dependent MVA of Bruell-Balbo-Afshari, together with their...
QdLinResult< T > pfqn_qdlin(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, const Matrix< T > &mu, const std::vector< double > &nservers, double tol=1e-6, std::size_t maxiter=1000, double wtol=1e-4)
QD-LIN: the Linearizer arm of AMVA-LD, on a plain demand matrix.
Definition pfqn_qdlin.h:205
SensMvaldmxResult< T > pfqn_sens_mvaldmx(const std::vector< T > &lambda, const Matrix< T > &D, const std::vector< int > &N, const std::vector< T > &Z, const Matrix< T > &mu)
Exact queue-length variances and covariances of a mixed open/closed product-form network with limited...
std::vector< T > pfqn_mu_ms(int N, int m, int c)
Aggregate load-dependent rate of m identical c-server FCFS stations.
Definition pfqn_mu_ms.h:56
PamVariant
Which of the three proportional approximations to run.
Definition pfqn_pam.h:46
T pfqn_nrl(const Matrix< T > &L, const std::vector< T > &N, const std::vector< T > &Z, const Matrix< T > &alpha)
Norlund-Rice logit approximation of log G.
Definition pfqn_nrl.h:130
T pfqn_qzgblow(const std::vector< T > &L, const T &N, const T &Z, std::size_t i)
Qgb = y/(1-y) - y^(N+1)/(1-y) with y = N L_i / (Z + sum(L) + Lmax N).
T pfqn_perm(const Matrix< T > &A, const std::vector< int > &m)
Permanent of a matrix with repeated columns, by Ryser's formula.
Definition pfqn_perm.h:52
SensMomResult< T > pfqn_sens_momldmx(const std::vector< T > &lambda, const Matrix< T > &D, const std::vector< int > &N, const std::vector< T > &Z, const Matrix< T > &mu, const std::vector< int > &groups)
Exact moments E[Q], Var[Q], E[Q^2], E[Q^3] and covariances of the grouped queue lengths of a mixed lo...
MomlinResult< T > pfqn_momlin(const Matrix< T > &L, const std::vector< int > &N, const std::vector< T > &Z, const T &tol, int maxiter)
Moment linearizer: approximate first and second queue-length moments of a large closed product-form n...
Definition pfqn_momlin.h:87
SensResult< T > pfqn_sens(const Matrix< T > &L, const std::vector< int > &N, const std::vector< T > &Z, const std::vector< int > &mi)
Exact analytic derivatives of the mean performance measures {X,Q,U,R} of a closed product-form (BCMP)...
Definition pfqn_sens.h:348
IbjbBounds< T > pfqn_ibjb_bounds(const std::vector< T > &Lin, int N, const T &Z)
Dallery (1987) improved balanced job bounds.
T pfqn_xzgsblow(const std::vector< T > &L, const T &N, const T &Z)
X = 2N / (R + sqrt(R^2 - 4 Z Lmax (N-1))), R from the geometric queue bound.
MvacldResult< T > pfqn_mvacld(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const Matrix< T > &mu)
MVAC for networks with queue-length dependent (QLD) service centers, the Section V extension of Conwa...
NcDispatchResult< T > pfqn_nc(const std::vector< T > &lambda, const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, NcMethod method, const T &atol, const NcOptions &nopt)
Normalizing constant of a product-form queueing network: the dispatcher.
Definition pfqn_nc.h:275
MvacResult< T > pfqn_mvac(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z)
MVAC: exact mean value analysis BY CHAIN of a closed multichain product-form network (Conway,...
Definition pfqn_mvac.h:246
PfqnManjunathResult< T > pfqn_manjunath(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const Matrix< T > &A, const std::vector< long > &b, const std::string &sense, const PfqnManjunathOptions &options={})
Exact normalizing constant of a closed multiclass product-form network whose state space carries arbi...
SsdBounds< T > pfqn_ssd(const std::vector< T > &L, const T &N, const T &Z, const std::vector< T > &nservers)
Server-Station Disaggregation bounds for a multiserver closed network (Dallery and Suri,...
Definition pfqn_ssd.h:55
void push(EventOutcome< T > &out, const std::vector< T > &row, const T &rate, const T &prob, const std::vector< std::size_t > &start, const std::vector< std::size_t > &preempt)
Append one successor with its tags.
nlohmann::json Json
Definition api_json.h:63
std::uint64_t required_seed(Args &a, const std::string &fn, const char *spelled="seed")
The seed of a function whose port draws from a caller-owned generator.
Json encode_vectors(const std::vector< std::vector< T > > &v)
A list of vectors, the shape a per-class or per-segment result returns.
Definition api_json.h:366
Matrix< T > matrix_from_json(const Json &j, const std::string &where)
A matrix from a JSON value: 2-D array as rows, 1-D array as a row vector, scalar as 1x1,...
Definition api_json.h:223
std::vector< int > int_vector_from_json(const Json &j, const std::string &where)
An integer vector; a non-integral entry is an error, never a truncation.
Definition api_json.h:267
Json encode_matrices(const std::vector< Matrix< T > > &v)
A list of matrices, the shape the MMAP/BMAP families return as {D0,D1,...}.
Definition api_json.h:358
T number_from_json(const Json &j, const std::string &where)
Definition api_json.h:210
std::vector< T > vector_from_json(const Json &j, const std::string &where)
A flat numeric vector: 1-D array, or a 1-row / 1-column 2-D array.
Definition api_json.h:255
Json encode_vector(const std::vector< T > &v)
Definition api_json.h:333
Json encode_matrix(const Matrix< T > &m)
Definition api_json.h:340
void register_library_only_pfqn_b(LibraryOnlyTable &t)
Json encode_scalar(const T &v)
Definition api_json.h:328
Json encode_count(std::size_t n)
A count.
Definition api_json.h:378
void register_api_pfqn_b(DispatchTable &t)
std::map< std::string, std::string > LibraryOnlyTable
name -> why the function has no named-JSON-argument form.
Json encode_ints(const std::vector< int > &v)
Definition api_json.h:350
std::map< std::string, Invoker > DispatchTable
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
debeMVA: MVA-based approximation of closed networks with finite (BAS) buffers.
Dallery (1987) improved balanced job bounds for single-class closed networks of single-server station...
Kriz (1984) closed-form throughput bounds for single-class closed networks of single-server stations ...
Shanthikumar-Yao (1988) throughput bounds for single-class closed networks with arbitrary queue-depen...
Exact normalizing constant of a closed multiclass product-form network whose state space carries arbi...
Marie's iterative aggregation-decomposition for closed networks with FCFS general (Coxian) service.
Monte Carlo Integration estimate of the normalizing constant of a closed product-form network (Ross,...
Chen-O'Cinneide REGULARIZATION: a Markov chain Monte Carlo estimator of the class throughputs X(r) = ...
Kerola's multiclass composite bound (Perf.
McKenna-Mitra integral form of the normalizing constant of a repairman model (one queueing station,...
Sampled McKenna-Mitra integral form of the normalizing constant of a repairman (single-queue plus del...
Moment linearizer: approximate first and second queue-length moments of a large closed product-form n...
Aggregate load-dependent rate of m identical c-server FCFS stations.
Shift the load-dependent service-rate lattice of selected stations.
Exact interval-valued MVA for single-class closed product-form networks.
MVABLO: approximate MVA for closed networks with Blocking-After-Service (BAS).
MVAC: exact mean value analysis BY CHAIN of a closed multichain product-form network (Conway,...
MVAC for networks with queue-length dependent (QLD) service centers, the Section V extension of Conwa...
Exact Mean Value Analysis for mixed open/closed networks with multiserver stations.
Memory-bounded exact MVA for closed networks with multiserver stations.
Majumdar-Woodside robust box bounds on the per-class throughput of a closed multiclass network with m...
Normalizing constant of a product-form queueing network: the dispatcher.
Preprocessing shared by the normalizing-constant solvers: drop the classes that cannot contribute,...
Normalizing constant of a LOAD-DEPENDENT closed network: the dispatcher.
Normalizing constant of a MIXED open/closed network with limited load dependence.
Mean value analysis at a nonintegral population (fractional-base aMVA).
Norlund-Rice inversion of the normalizing constant on a SADDLE-TILTED contour, with a second-order Ed...
Norlund-Rice inversion of the normalizing constant, in its logistic (NRL) and probit (NRP) substituti...
Order-independent (OI) functional server: the balance function Psi and the rate mu_f of an auxiliary ...
OQNRC: approximate MVA for open queueing networks with restricted capacity.
Hsieh-Lam Proportional Approximation Methods (PAMB / PAMI / PAMT).
PANACEA normal-usage asymptotic expansion of the normalizing constant (Ramakrishnan and Mitra,...
PANACEA normal-usage asymptotic expansion for LOAD-DEPENDENT closed networks (Mitra and McKenna,...
Performance Bound Hierarchy (Eager and Sevcik 1983, ACM TOCS 1(2):99-115) for single-class closed pro...
Permanent of a matrix with repeated columns, by Ryser's formula.
Proportionally fair allocation estimate of the normalizing constant (Schweitzer 1979; Walton,...
QD-AMVA: queue-dependent approximate mean value analysis.
QD-LIN: the Linearizer arm of AMVA-LD, on a plain demand matrix.
Joint moments of the queue-length vector of a closed product-form network, obtained from normalizing ...
Queue-Shift Approximation (QSA) for closed product-form networks.
Geometric-bound lower bound on the queue length at station i.
Geometric-bound upper bound on the queue length at station i.
Reduction heuristic (RD) for the normalizing constant of a closed LOAD-DEPENDENT product-form network...
Sojourn-time moments at the processor-sharing station of a closed terminal-driven system (Mitra and M...
Recursion by Generating Functions (RGF) for the normalizing constant of a SINGLE-CLASS closed product...
Multiclass Recursion by Generating Functions (RGF), with think times.
Neuse-Chandy SCAT (Self-Correcting Approximation Technique) approximate MVA.
Dowdy-Carlson-Krantz-Tripathi (1992) single-class bounds of multi-class queueing networks,...
Schmidt's MVA for closed networks with general scheduling disciplines and class-dependent multiserver...
Extended Schmidt MVA with queue-aware alpha corrections.
Product-form state-dependent routing.
Exact analytic derivatives of the mean performance measures {X,Q,U,R} of a closed product-form (BCMP)...
Effective capacity terms of the mixed load-dependent MVA of Bruell-Balbo-Afshari, together with their...
Approximate moments E[Q_i], Var[Q_i], Cov[Q_i,Q_j], E[Q_i^2] and E[Q_i^3] of the per-station total qu...
Exact moments E[Q], Var[Q], E[Q^2] and E[Q^3] of the grouped queue lengths of a closed product-form n...
Exact higher queue-length moments (up to order three) of mixed open/closed product-form networks with...
Exact per-station queue-length variances and covariances of a closed product-form network,...
Exact queue-length variances and covariances of a mixed open/closed product-form network with limited...
Exact raw moments E[W^t], t = 1..3, of the sojourn time of a job at an FCFS b-server center of a clos...
Successively Improving Bounds (Srinivasan 1985/1987) on the cycle time and throughput of a single-cla...
Square-root non-iterative (SQNI) approximation for a single queueing station with per-class delay.
Server-Station Disaggregation bounds for a multiserver closed network (Dallery and Suri,...
Sojourn-time distribution at multiserver FCFS stations of a closed product-form network (J.
Heuristic sojourn-time distribution at multiserver FCFS stations, a variant of J.
Tay's arrival-instant approximate MVA.
Station consolidation and its inverse: merge identical demand rows into one station with a multiplici...
Xia's asymptotic approximation of the normalizing constant of a load-dependent (multiserver) closed n...
Asymptotic-bound-analysis lower bound on throughput.
Asymptotic-bound-analysis upper bound on throughput.
Geometric-square-root bound, lower bound on throughput.
Geometric-square-root bound, upper bound on throughput.