LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
sn_nonmarkov_toph.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_API_SN_SN_NONMARKOV_TOPH_H
6#define LINE_API_SN_SN_NONMARKOV_TOPH_H
7
8/**
9 * @file
10 * @ingroup api_sn
11 * Replace every non-Markovian service and firing law by a Markovian surrogate.
12 *
13 * Templated port of matlab/src/api/sn/sn_nonmarkov_toph.m, mirrored by
14 * jline.api.sn.SnNonmarkovToPh and the native Python sn_nonmarkov_toph. The
15 * MAM, CTMC, Fluid and SSA analyzers all run it on their own copy of the struct
16 * before any state space is built, so a Gamma, Weibull, Lognormal, Pareto,
17 * Uniform or Det service law reaches an algorithm that only understands
18 * generators.
19 *
20 * IT IS NOT `convertToMAP`. The struct refresh already lowers those families to
21 * an Erlang of ceil(1/scv) phases (`dist_to_map` in lang/distribution.h); that
22 * fit matches the mean and, above SCV 1, nothing else. This is the SOLVER-side
23 * conversion, with an explicit phase budget (20 by default) and a choice of fit:
24 *
25 * - phfit = Cme, the default: a concentrated matrix exponential convolved with
26 * an exponential (`dist_fit_me`), which matches the first TWO moments exactly
27 * whenever the SCV is in (0,1). It is what makes an M/G/1 mean come out
28 * right: on M/Gamma/1 at rho 0.5 the two-moment fit lands on the exact mean
29 * queue length where the density fit lands 2.7e-2 away.
30 * - phfit = Ph: the Bernstein density fit (`map_bernstein`), kept because it
31 * carries SHAPE information a two-moment fit cannot, and because SSA, Fluid
32 * and JMT cannot consume a matrix exponential at all.
33 *
34 * DET IS ERLANG, WHATEVER phfit ASKS FOR. A concentrated ME matches a Det's
35 * moments far better (SCV 5.7e-3 against Erlang-20's 0.05) but is not a
36 * generator: its off-diagonal entries are not rates, so a CTMC built from it
37 * does not describe the model. Measured on the reference's test_OQN_DM1 (Det(1)
38 * arrivals, Exp(2) service, rho = 0.5), the ME surrogate reported U = 0.993 and
39 * a departure rate of 2.0 against a mean interarrival of 1.0, where JMT gives
40 * 0.497, LDES 0.502 and the golden 0.500.
41 *
42 * WHAT IS NOT TOUCHED. The Markovian families, DISABLED, IMMEDIATE, and the
43 * three time-inhomogeneous families NHPP / MAPt / PHt, whose whole content is
44 * the schedule -- collapsing one to a single homogeneous surrogate would erase
45 * the time dependence that is the reason it was declared.
46 *
47 * NO STATE SURGERY IS NEEDED HERE. The MATLAB reference rewrites sn.phases,
48 * phasessz, phaseshift, mu, phi, pie and then splices extra columns into any
49 * pre-initialized sn.state and sn.space, because those are stored arrays that
50 * would otherwise disagree with the new phase count. In this port every one of
51 * them is DERIVED from `sn.service[i][r]` on demand (`phases_of`, `dist_pie`,
52 * `dist_to_map`), so replacing the distribution updates all of them at once and
53 * there is nothing left to splice. Callers run this before generating states,
54 * exactly as the reference's analyzers do.
55 *
56 * ARITHMETIC: transcendental. The fits evaluate densities and take square
57 * roots, so this does not instantiate under Rational.
58 */
59
60#include <cctype>
61#include <cmath>
62#include <cstddef>
63#include <functional>
64#include <map>
65#include <string>
66#include <vector>
67
69#include "line/api/mam/cme.h"
78#include "line/num/number.h"
79#include "line/util/error.h"
80
81namespace line {
82namespace api {
83
84/** Which Markovian surrogate to fit; `options.config.phfit`. */
85enum class PhFit {
86 Cme, ///< concentrated ME plus exponential tail: two moments, exactly
87 Ph, ///< Bernstein density fit: a genuine phase-type, shape-carrying
88 /**
89 * Mixture of exponentials fitted to the ccdf ITSELF across decades of time
90 * scale (`hyperexp_fit_longtail`, Feldmann and Whitt 1998).
91 *
92 * The only one of the three that says anything about a LONG TAIL: a Pareto
93 * with tail index below 2 has no finite variance, so a two-moment fit does
94 * not exist at all, and even where the moments are finite they say nothing
95 * about the several orders of magnitude over which such a law acts. It
96 * applies only to the families with a closed-form tail below; a light-tailed
97 * law falls through to the two-moment surrogate.
98 */
100};
101
102/** `options.config.nonmkv` and friends. */
104 bool enabled = true; ///< false is the reference's nonmkv = 'none'
105 std::size_t order = 20; ///< `nonmkvorder`, the phase budget
106 PhFit phfit = PhFit::Cme; ///< which surrogate family
107 /**
108 * Leave Det alone for the exact MAP/D/c branch, `options.config.preserveDet`.
109 * The MAM analyzer sets it; every other caller converts.
110 */
111 bool preserve_det = false;
112};
113
114/**
115 * Resolve `options.config.nonmkv` to `NonmarkovOptions::enabled`.
116 *
117 * Case-insensitive, as in the reference: `'none'` leaves the struct unchanged
118 * (the refresh-time `dist_to_map` fit is what the solver then reads) and
119 * `'bernstein'`, the default, runs the conversion. Any other name is refused as
120 * `LINE:input:ConfigInvalid`, the id every codebase's options.config validator
121 * raises for a value outside the schema's list (lineConfigValidate.m).
122 */
123inline bool nonmkv_enabled(const std::string& method) {
124 std::string m = method;
125 for (char& c : m) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
126 if (m == "none") return false;
127 if (m == "bernstein") return true;
128 throw InputError(ErrorKind{"ConfigInvalid"},
129 "options.config.nonmkv expects one of 'bernstein', 'none', got '" + method + "'.");
130}
131
132namespace detail {
133
134/** True for the families that already carry a Markovian representation. */
135inline bool nonmkv_is_markovian(lang::ProcessType p) {
136 using lang::ProcessType;
137 switch (p) {
138 case ProcessType::EXP:
139 case ProcessType::ERLANG:
140 case ProcessType::HYPEREXP:
141 case ProcessType::PH:
142 case ProcessType::APH:
143 case ProcessType::MAP:
144 case ProcessType::DMAP:
145 case ProcessType::MMAP:
146 // The marked families already carry an exact matrix representation, so
147 // the Bernstein conversion has nothing to add and would drop the marks.
148 case ProcessType::MPH:
149 case ProcessType::MMAPT:
150 case ProcessType::MPHT:
151 case ProcessType::BMMAPT:
152 case ProcessType::BMAP:
153 case ProcessType::ME:
154 case ProcessType::RAP:
155 case ProcessType::COXIAN:
156 case ProcessType::COX2:
157 case ProcessType::MMPP2:
158 case ProcessType::IMMEDIATE:
159 case ProcessType::DISABLED:
160 return true;
161 default:
162 return false;
163 }
164}
165
166/**
167 * True for the families whose content IS a schedule.
168 *
169 * A single homogeneous surrogate has no way to carry a piecewise-constant
170 * D0(t)/D1(t), so converting one would silently answer for a stationary model.
171 */
172inline bool nonmkv_is_scheduled(lang::ProcessType p) {
173 using lang::ProcessType;
174 // An NNPP carries a rate function rather than a schedule, but no homogeneous
175 // surrogate can carry it either.
176 return p == ProcessType::NHPP || p == ProcessType::MAPT || p == ProcessType::PHT ||
177 p == ProcessType::NNPP;
178}
179
180/**
181 * The density of the five families the reference fits by shape, or an empty
182 * function for anything else (which then takes the moment-only surrogate).
183 */
184template <class T>
185std::function<double(double)> nonmkv_density(const lang::Distrib<T>& d) {
186 using lang::ProcessType;
187 std::vector<double> p;
188 for (std::size_t i = 0; i < d.params.size(); ++i)
189 p.push_back(num_traits<T>::to_double(d.params[i]));
190 switch (d.type) {
191 case ProcessType::GAMMA: { // params (shape, scale)
192 if (p.size() < 2) break;
193 const double k = p[0], th = p[1];
194 return [k, th](double x) {
195 if (!(x > 0.0)) return 0.0;
196 return std::exp((k - 1.0) * std::log(x) - x / th - k * std::log(th) -
197 std::lgamma(k));
198 };
199 }
200 case ProcessType::WEIBULL: { // params (scale, shape), the C++ builder order
201 if (p.size() < 2) break;
202 const double a = p[0], r = p[1];
203 return [a, r](double x) {
204 if (!(x > 0.0)) return 0.0;
205 return (r / a) * std::pow(x / a, r - 1.0) * std::exp(-std::pow(x / a, r));
206 };
207 }
208 case ProcessType::LOGNORMAL: { // params (mu, sigma) of the log
209 if (p.size() < 2) break;
210 const double mu = p[0], sg = p[1];
211 return [mu, sg](double x) {
212 if (!(x > 0.0)) return 0.0;
213 const double z = (std::log(x) - mu) / sg;
214 return std::exp(-0.5 * z * z) / (x * sg * std::sqrt(2.0 * 3.14159265358979323846));
215 };
216 }
217 case ProcessType::LOGLOGISTIC: { // params (alpha scale, beta shape), getParam order
218 if (p.size() < 2) break;
219 const double a = p[0], b = p[1];
220 return [a, b](double x) {
221 if (!(x > 0.0)) return 0.0;
222 const double z = x / a;
223 const double zb = std::pow(z, b);
224 return (b / a) * std::pow(z, b - 1.0) / ((1.0 + zb) * (1.0 + zb));
225 };
226 }
227 case ProcessType::PARETO: { // params (shape alpha, scale k)
228 if (p.size() < 2) break;
229 const double al = p[0], k = p[1];
230 return [al, k](double x) {
231 if (!(x >= k) || !(x > 0.0)) return 0.0;
232 return al * std::pow(k, al) / std::pow(x, al + 1.0);
233 };
234 }
235 case ProcessType::UNIFORM: { // params (a, b)
236 if (p.size() < 2) break;
237 const double a = p[0], b = p[1];
238 return [a, b](double x) { return (x >= a && x <= b) ? 1.0 / (b - a) : 0.0; };
239 }
240 default:
241 break;
242 }
243 return std::function<double(double)>();
244}
245
246/**
247 * The declared law's complementary cdf, for the families whose tail the
248 * long-tail fit is stated for. Empty for every other law, which is what makes
249 * `phfit = Hyperexp` fall through to the two-moment surrogate there.
250 *
251 * Built on `dist_cdf`, which already carries the closed form of each of these
252 * four families -- the Gamma from the regularized incomplete gamma, the Pareto
253 * from 1 - (k/x)^alpha -- so there is no second copy of any of them here.
254 */
255template <class T>
256std::function<double(double)> nonmkv_ccdf(const lang::Distrib<T>& d) {
257 using lang::ProcessType;
258 if (d.type != ProcessType::GAMMA && d.type != ProcessType::WEIBULL &&
259 d.type != ProcessType::LOGNORMAL && d.type != ProcessType::PARETO &&
260 d.type != ProcessType::LOGLOGISTIC)
261 return std::function<double(double)>();
262 const lang::Distrib<T> law = d;
263 return [law](double x) {
264 if (!(x > 0.0)) return 1.0;
265 return 1.0 - num_traits<T>::to_double(lang::dist_cdf(law, num_traits<T>::from_double(x)));
266 };
267}
268
269/**
270 * A hyperexponential fitted to the ccdf across decades, as its MAP pair and
271 * rescaled to the mean the struct carries.
272 *
273 * `ok` comes back false when the recursion declines the law: the components have
274 * to dominate one another at their own time scales, which a light-tailed law
275 * does not provide, and answering with a fit that does not hold is worse than
276 * falling through to the two-moment surrogate.
277 */
278template <class T>
279mam::Map<T> nonmkv_longtail(const std::function<double(double)>& ccdf, double mean, bool& ok) {
280 ok = false;
281 mam::Map<T> out;
282 // The fit itself runs in double: it bisects for quantiles and takes
283 // logarithms of the ccdf, which is a `double(double)` here whatever T is.
284 mam::HyperexpLongtailResult<double> fit;
285 try {
287 } catch (const std::exception&) {
288 return out;
289 }
290 const std::size_t n = fit.p.size();
291 if (n == 0) return out;
292 Matrix<T> D0(n, n, num_traits<T>::from_int(0));
293 Matrix<T> D1(n, n, num_traits<T>::from_int(0));
294 for (std::size_t i = 0; i < n; ++i) {
295 if (!std::isfinite(fit.lambda[i]) || fit.lambda[i] <= 0.0) return out;
296 D0(i, i) = num_traits<T>::from_double(-fit.lambda[i]);
297 for (std::size_t j = 0; j < n; ++j)
298 D1(i, j) = num_traits<T>::from_double(fit.lambda[i] * fit.p[j]);
299 }
300 mam::Map<T> m;
301 m.D0 = D0;
302 m.D1 = D1;
303 out = mam::map_scale(m, num_traits<T>::from_double(mean));
304 ok = true;
305 return out;
306}
307
308/**
309 * The moment-only surrogate: a concentrated ME under phfit = Cme, an Erlang of
310 * the full budget otherwise. Port of the reference's fitConcentratedSurrogate.
311 */
312template <class T>
313mam::Map<T> nonmkv_concentrated(double mean, double scv, std::size_t order, PhFit phfit) {
314 if (phfit == PhFit::Cme) {
315 // A Det has SCV 0, which no ME attains; the budget-limited branch of the
316 // fitter then returns the most concentrated member that fits.
317 const double s = scv > 1e-12 ? scv : 1e-12;
318 if (s < 1.0) return mam::dist_fit_me<T>(mean, s < 1.0 - 1e-12 ? s : 1.0 - 1e-12, order);
319 }
320 return mam::map_erlang(num_traits<T>::from_double(mean), static_cast<unsigned>(order));
321}
322
323/** Install a fitted (D0,D1) as the law of a station-class pair or a firing mode. */
324template <class T>
325void nonmkv_install(lang::Distrib<T>& d, const mam::Map<T>& m) {
326 std::vector<Matrix<T>> blocks;
327 blocks.push_back(m.D0);
328 blocks.push_back(m.D1);
329 const bool isph = sn_is_phasetype(blocks, std::vector<T>());
330 // The declared law is kept alongside the fit. Everything downstream reads
331 // the surrogate, as it did; MMAP[K]/G[K]/1 evaluates the ORIGINAL transform
332 // and would otherwise find only the fit it exists to avoid.
333 std::shared_ptr<lang::Distrib<T>> declared(new lang::Distrib<T>(d));
334 declared->declared.reset();
335 // A concentrated-ME surrogate is NOT a phase-type, so it is tagged ME: the
336 // CTMC then assembles a rational generator and the PH-only consumers refuse
337 // it. sn_is_phasetype is the single test, exactly as in the reference.
338 d = lang::Distrib<T>::map_dist(m.D0, m.D1,
341 d.declared = declared;
342}
343
344/**
345 * True when mode `m`'s firing law is ALREADY a usable Markovian one and needs
346 * no fit.
347 *
348 * A POSITIVE PHASE COUNT IS NOT ENOUGH, and reading it as such is what left
349 * `spn_pareto_service` absorbing. `network_reader` records
350 * `firingphases[m] = dist_to_map(proc).order()` at read time, so a Pareto mode
351 * arrives with a phase count of the fit that WOULD be made while `firingproc[m]`
352 * is still the Pareto, whose (D0,D1) are empty. The old guard read that count,
353 * skipped the conversion, and `after_global_event` then found no firing matrix:
354 * the chain reached the enabled state and stopped there, filling the place to
355 * the cutoff and reporting Tput 0 with QLen equal to the cutoff at every cutoff.
356 * The matrices themselves are the test.
357 */
358template <class T>
359bool nonmkv_firing_ready(const qn::TransitionParam<T>& np, std::size_t m) {
360 if (m >= np.firingphases.size() || np.firingphases[m] <= 0) return false;
361 if (m >= np.firingproc.size()) return false;
362 return np.firingproc[m].D0.rows() == static_cast<std::size_t>(np.firingphases[m]);
363}
364
365} // namespace detail
366
367/**
368 * Whether any law in the struct would be replaced, so a caller can skip copying
369 * the struct when there is nothing to convert.
370 *
371 * @param sn the struct to inspect
372 * @param preserve_det leave Det alone, as the MAM analyzer does
373 */
374template <class T>
375bool sn_has_nonmarkov(const qn::NetworkStruct<T>& sn, bool preserve_det = false) {
376 using lang::ProcessType;
377 for (std::size_t ist = 0; ist < sn.nstations; ++ist)
378 for (std::size_t r = 0; r < sn.nclasses; ++r) {
379 if (ist < sn.disabled.size() && r < sn.disabled[ist].size() && sn.disabled[ist][r])
380 continue;
381 const lang::Distrib<T>& d = sn.service[ist][r];
382 if (d.disabled || d.is_prior()) continue;
383 if (detail::nonmkv_is_markovian(d.type) || detail::nonmkv_is_scheduled(d.type))
384 continue;
385 if (d.type == ProcessType::DET && preserve_det) continue;
386 return true;
387 }
388 for (typename std::map<std::size_t, qn::TransitionParam<T>>::const_iterator it =
389 sn.transparam.begin();
390 it != sn.transparam.end(); ++it) {
391 const qn::TransitionParam<T>& np = it->second;
392 for (std::size_t m = 0; m < np.firingproc.size(); ++m) {
393 if (detail::nonmkv_firing_ready(np, m)) continue;
394 const lang::Distrib<T>& d = np.firingproc[m];
395 if (d.disabled || d.is_prior()) continue;
396 if (detail::nonmkv_is_markovian(d.type) || detail::nonmkv_is_scheduled(d.type))
397 continue;
398 if (d.type == ProcessType::DET && preserve_det) continue;
399 return true;
400 }
401 }
402 return false;
403}
404
405/**
406 * @brief Replace every non-Markovian service and firing law by a Markovian
407 * surrogate.
408 *
409 * @param sn the struct to convert IN PLACE; callers pass their own copy
410 * @param opts the conversion method, order and fit family
411 */
412template <class T>
415 "sn_nonmarkov_toph evaluates densities and fits moments");
416 using lang::ProcessType;
417 if (!opts.enabled) return;
418 if (opts.order == 0) throw InputError("sn_nonmarkov_toph: the phase budget must be positive");
419
420 for (std::size_t ist = 0; ist < sn.nstations; ++ist) {
421 for (std::size_t r = 0; r < sn.nclasses; ++r) {
422 if (ist < sn.disabled.size() && r < sn.disabled[ist].size() && sn.disabled[ist][r])
423 continue;
424 lang::Distrib<T>& d = sn.service[ist][r];
425 if (d.disabled || d.is_prior()) continue;
426 const ProcessType p = d.type;
427 if (detail::nonmkv_is_markovian(p) || detail::nonmkv_is_scheduled(p)) continue;
428 if (p == ProcessType::DET && opts.preserve_det) continue;
429
430 const double mean = num_traits<T>::to_double(d.mean);
431 const double scv = num_traits<T>::to_double(d.scv);
432 if (!std::isfinite(mean) || mean <= 0.0) continue;
433
434 if (p == ProcessType::DET) {
435 detail::nonmkv_install(
437 static_cast<unsigned>(opts.order)));
438 continue;
439 }
440
441 if (p == ProcessType::MASSATORIGIN) {
442 // An atom at zero has no density for the Bernstein fit to read,
443 // and its SCV is above one whenever the inner law is not too
444 // concentrated, where an Erlang cannot follow. A two-moment APH
445 // matches the mean and the SCV exactly, so it reproduces the
446 // Pollaczek-Khinchine mean, as MATLAB's APH.fitMeanAndSCV.
447 detail::nonmkv_install(d, mam::aph_fit_mean_scv(num_traits<T>::from_double(mean),
449 continue;
450 }
451
452 // The long-tail fit, when it was asked for and the law has a tail
453 // to fit. It matches the ccdf at points spread over decades rather
454 // than matching two moments, so it is the route for a Pareto, a
455 // Weibull with shape below one or a Lognormal with a large sigma.
456 if (opts.phfit == PhFit::Hyperexp) {
457 const std::function<double(double)> ccdf = detail::nonmkv_ccdf(d);
458 if (ccdf) {
459 bool ok = false;
460 const mam::Map<T> lt = detail::nonmkv_longtail<T>(ccdf, mean, ok);
461 if (ok) {
462 detail::nonmkv_install(d, lt);
463 continue;
464 }
465 }
466 }
467
468 // A Bernstein fit cannot carry the LogLogistic power-law tail: at
469 // beta 2.5 (SCV 1.45) it lost a quarter of the M/G/1 queue. With a
470 // finite SCV >= 1 a two-moment APH (a genuine phase type) matches
471 // mean and SCV exactly, hence the Pollaczek-Khinchine mean; with an
472 // infinite SCV (beta <= 2) the long-tail hyperexponential fit of the
473 // closed-form ccdf is used. Mirrors MATLAB sn_nonmarkov_toph.
474 if (p == ProcessType::LOGLOGISTIC && opts.phfit != PhFit::Hyperexp) {
475 if (std::isfinite(scv) && scv >= 1.0) {
476 detail::nonmkv_install(d, mam::aph_fit_mean_scv(num_traits<T>::from_double(mean),
478 continue;
479 }
480 if (!std::isfinite(scv)) {
481 bool ok = false;
482 const mam::Map<T> lt =
483 detail::nonmkv_longtail<T>(detail::nonmkv_ccdf(d), mean, ok);
484 if (ok) {
485 detail::nonmkv_install(d, lt);
486 continue;
487 }
488 }
489 }
490
491 const std::function<double(double)> pdf = detail::nonmkv_density(d);
492 if (!pdf) { // no density to fit by shape: moments only
493 detail::nonmkv_install(d,
494 detail::nonmkv_concentrated<T>(mean, scv, opts.order,
495 opts.phfit));
496 continue;
497 }
498 if (opts.phfit == PhFit::Cme && scv >= 0.0 && scv < 1.0) {
499 detail::nonmkv_install(d,
500 detail::nonmkv_concentrated<T>(mean, scv, opts.order,
501 opts.phfit));
502 continue;
503 }
504 const mam::Map<T> fit =
505 mam::map_scale(mam::map_bernstein<T>(pdf, static_cast<unsigned>(opts.order)),
507 detail::nonmkv_install(d, fit);
508 }
509 }
510
511 // ---- SPN transition firing laws -------------------------------------
512 for (std::size_t ind = 0; ind < sn.nodes.size(); ++ind) {
513 if (sn.nodes[ind].nodetype != qn::NodeType::Transition) continue;
514 typename std::map<std::size_t, qn::TransitionParam<T>>::iterator it =
515 sn.transparam.find(ind + 1);
516 if (it == sn.transparam.end()) continue;
517 qn::TransitionParam<T>* np = &it->second;
518 for (std::size_t m = 0; m < np->firingproc.size(); ++m) {
519 // refreshPetriNetNodes already gives a Markovian mode a valid
520 // (D0,D1) and a positive phase count; those are left alone. The
521 // MATRICES decide, not the count -- see `nonmkv_firing_ready`.
522 if (detail::nonmkv_firing_ready(*np, m)) continue;
523 lang::Distrib<T>& d = np->firingproc[m];
524 if (d.disabled || d.is_prior()) continue;
525 const ProcessType p = d.type;
526 if (detail::nonmkv_is_markovian(p) || detail::nonmkv_is_scheduled(p)) continue;
527 if (p == ProcessType::DET && opts.preserve_det) continue;
528
529 const double mean = num_traits<T>::to_double(d.mean);
530 mam::Map<T> fit;
531 if (p == ProcessType::DET) {
532 // the reference: map_erlang(targetMean_t, nPhases), no surrogate
533 const double mu = (std::isfinite(mean) && mean > 0.0) ? mean : 1.0;
535 static_cast<unsigned>(opts.order));
536 } else {
537 // The reference's switch knows five families (Gamma, Weibull,
538 // Lognormal, Pareto, Uniform) and `continue`s on any other, and
539 // for those five ALWAYS fits by shape with map_bernstein: the
540 // cme / concentrated and the long-tail branches of the station
541 // loop do not exist for a firing law.
542 const std::function<double(double)> pdf = detail::nonmkv_density(d);
543 if (!pdf) continue;
544 if (!std::isfinite(mean) || mean <= 0.0) {
545 // A firing law with no usable mean (a Pareto with alpha <= 1)
546 // falls back to the unit-mean Erlang, as the reference does
547 // rather than refusing the model.
549 static_cast<unsigned>(opts.order));
550 } else {
551 try {
552 fit = mam::map_scale(
553 mam::map_bernstein<T>(pdf, static_cast<unsigned>(opts.order)),
555 } catch (const std::exception&) {
557 static_cast<unsigned>(opts.order));
558 }
559 }
560 }
561 detail::nonmkv_install(d, fit);
562 if (m < np->firingphases.size()) np->firingphases[m] = fit.order();
563 }
564 }
565}
566
567} // namespace api
568} // namespace line
569
570#endif // LINE_API_SN_SN_NONMARKOV_TOPH_H
Acyclic phase-type fitters from the first two moments.
InputError(const std::string &what)
Definition error.h:105
A network plus its refreshed NetworkStruct.
Concentrated matrix exponentials, and the two-moment fit built on them.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
Fitting a hyperexponential to a long-tail distribution.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
Acyclic phase-type approximation of an arbitrary density by Bernstein exponentials.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
MAP constructors and structural transformations.
bool sn_has_nonmarkov(const qn::NetworkStruct< T > &sn, bool preserve_det=false)
Whether any law in the struct would be replaced, so a caller can skip copying the struct when there i...
bool nonmkv_enabled(const std::string &method)
Resolve options.config.nonmkv to NonmarkovOptions::enabled.
PhFit
Which Markovian surrogate to fit; options.config.phfit.
@ Ph
Bernstein density fit: a genuine phase-type, shape-carrying.
@ Hyperexp
Mixture of exponentials fitted to the ccdf ITSELF across decades of time scale (hyperexp_fit_longtail...
@ Cme
concentrated ME plus exponential tail: two moments, exactly
bool sn_is_phasetype(const std::vector< Matrix< T > > &maps, const std::vector< T > &pie)
Whether a (D0, D1, ...) list is a valid phase-type / MAP representation.
void sn_nonmarkov_toph(qn::NetworkStruct< T > &sn, const NonmarkovOptions &opts=NonmarkovOptions())
Replace every non-Markovian service and firing law by a Markovian surrogate.
T dist_cdf(const Distrib< T > &d, const T &x)
F(x) = P{X <= x}, MATLAB's Distribution.evalCDF.
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
Definition lang_types.h:524
void dist_refresh_moments(Distrib< T > &d)
Fill in the first two moments of a distribution given by its matrices.
double z(double v)
Definition lqn_focus.h:261
Map< T > aph_fit_mean_scv(const T &mean, const T &scv)
Port of APH.fitMeanAndSCV, the entry point the analyzers fit arrivals with.
HyperexpLongtailResult< T > hyperexp_fit_longtail(Ccdf &&ccdf, const T &b=num_traits< T >::from_rational(3, 2), const T &decade=num_traits< T >::from_int(4), const T &c1=num_traits< T >::from_int(0))
The fit with the component count chosen automatically: one per decade between the 0....
Map< T > map_bernstein(const std::function< double(double)> &f, unsigned order=20)
Acyclic phase-type approximation of an arbitrary density by Bernstein exponentials.
Map< T > dist_fit_me(double mean, double scv, std::size_t maxPhases=0)
Two-moment matrix-exponential fit for 0 < scv < 1, a port of dist_fit_me.m.
Definition cme.h:168
Map< T > map_erlang(const T &mean, unsigned k)
Erlang-k renewal MAP with the given mean (map_erlang.m).
Map< T > map_scale(const Map< T > &in, const T &new_mean)
Rescale time so that the mean inter-arrival time becomes new_mean.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
Number-type abstraction for the templated API port.
Whether a (D0, D1, ...) list is a valid phase-type / MAP representation.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37
options.config.nonmkv and friends.
std::size_t order
nonmkvorder, the phase budget
PhFit phfit
which surrogate family
bool preserve_det
Leave Det alone for the exact MAP/D/c branch, options.config.preserveDet.
bool enabled
false is the reference's nonmkv = 'none'
static Distrib map_dist(const Matrix< T > &D0, const Matrix< T > &D1, ProcessType tag)
A MAP given by its two matrices; the moments are those of its stationary phase.
bool is_prior() const
Definition lang_types.h:961
A MAP as the pair of matrices (D0, D1).
Definition map_moment.h:53
std::size_t order() const
Definition map_moment.h:57
The parameters of a Transition node: MATLAB sn.nodeparam{ind} for an SPN transition,...
std::vector< lang::Distrib< T > > firingproc
firing distribution per mode
std::vector< std::size_t > firingphases
phase count per mode, 0 when non-Markovian