LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
amap2_fit_gamma.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_MAM_AMAP2_FIT_GAMMA_H
6#define LINE_API_MAM_AMAP2_FIT_GAMMA_H
7
8/**
9 * @file
10 * @ingroup api_mam
11 * AMAP(2) fit of three moments and the autocorrelation decay rate
12 * (matlab/lib/m3a/m3a/amap2/amap2_fit_gamma.m).
13 *
14 * A unit SCV short-circuits to a Poisson process, since the AMAP(2) canonical
15 * forms are degenerate there. Otherwise every exact solution is enumerated
16 * with amap2_fitall_gamma, normalized, and the first is returned.
17 *
18 * Not ported: the reference's approximate branch, which calls
19 * amap2_adjust_gamma to relax (M2, M3, GAMMA) into the feasible region. All
20 * four of its methods are driven by patternsearch / PSwarm / fmincon, so there
21 * is no closed form to port. When no exact solution exists this port takes the
22 * same final fallback as the in-tree MATLAB does when adjustment fails: a
23 * Poisson process matching the mean, flagged through Amap2FitGammaResult.
24 *
25 * Gated on transcendental arithmetic through amap2_fitall_gamma.
26 */
27
28#include <vector>
29
33#include "line/num/number.h"
34
35namespace line {
36namespace mam {
37
38/** Result of amap2_fit_gamma. */
39template <class T>
41 Map<T> amap; ///< the selected fit
42 std::vector<Map<T>> amaps; ///< every exact solution found
43 bool poisson_fallback; ///< true when no exact AMAP(2) exists (or SCV == 1)
44};
45
46/**
47 * Fit an AMAP(2) to (M1, M2, M3, GAMMA). cvtol is the tolerance on
48 * |M2 - 2 M1^2| below which the Poisson short-circuit fires (MATLAB 1e-6).
49 */
50template <class T>
51Amap2FitGammaResult<T> amap2_fit_gamma(const T& M1, const T& M2, const T& M3, const T& GAMMA,
52 const T& cvtol) {
54 "amap2_fit_gamma requires transcendental arithmetic");
55 const T two = num_traits<T>::from_int(2);
57 r.poisson_fallback = false;
58
59 if (num_abs(T(M2 - two * M1 * M1)) < cvtol) {
61 r.amaps.push_back(r.amap);
62 r.poisson_fallback = true;
63 return r;
64 }
65
66 std::vector<Map<T>> all = amap2_fitall_gamma(M1, M2, M3, GAMMA);
67 for (std::size_t j = 0; j < all.size(); ++j) all[j] = map_normalize(all[j]);
68 r.amaps = all;
69
70 if (r.amaps.empty()) {
72 r.amaps.push_back(r.amap);
73 r.poisson_fallback = true;
74 return r;
75 }
76 r.amap = r.amaps.front();
77 return r;
78}
79
80/** amap2_fit_gamma with the MATLAB default cvtol = 1e-6. */
81template <class T>
82Amap2FitGammaResult<T> amap2_fit_gamma(const T& M1, const T& M2, const T& M3, const T& GAMMA) {
83 return amap2_fit_gamma(M1, M2, M3, GAMMA, T(num_traits<T>::from_double(1e-6)));
84}
85
86} // namespace mam
87} // namespace line
88
89#endif // LINE_API_MAM_AMAP2_FIT_GAMMA_H
All AMAP(2) representations matching three moments and the autocorrelation decay rate (matlab/lib/m3a...
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
MAP constructors and structural transformations.
Amap2FitGammaResult< T > amap2_fit_gamma(const T &M1, const T &M2, const T &M3, const T &GAMMA, const T &cvtol)
Fit an AMAP(2) to (M1, M2, M3, GAMMA).
Map< T > map_exponential_mean(const T &mean)
Poisson process with the given mean inter-arrival time (map_exponential.m).
Map< T > map_normalize(const Map< T > &in)
Clamp negative off-diagonal entries of D0 and negative entries of D1 to zero, then rebuild the diagon...
std::vector< Map< T > > amap2_fitall_gamma(const T &M1, const T &M2, const T &M3, const T &GAMMA, const T &degentol, const T &r12tol)
Every AMAP(2) matching (M1, M2, M3, GAMMA).
T num_abs(const T &v)
Definition number.h:172
Number-type abstraction for the templated API port.
Result of amap2_fit_gamma.
bool poisson_fallback
true when no exact AMAP(2) exists (or SCV == 1)
Map< T > amap
the selected fit
std::vector< Map< T > > amaps
every exact solution found
A MAP as the pair of matrices (D0, D1).
Definition map_moment.h:53