5#ifndef LINE_API_QSYS_QSYS_MAPG1_H
6#define LINE_API_QSYS_QSYS_MAPG1_H
146 throw InputError(
"qsys_mapg1: the mean service time must be positive");
155mam::Map<T> mapg1_erlang(
const T& mean, std::size_t k) {
156 if (k == 0)
throw InputError(
"qsys_mapg1: an Erlang fit needs at least one phase");
162 for (std::size_t i = 0; i < k; ++i) {
164 if (i + 1 < k) m.
D0(i, i + 1) = mu;
178mam::Map<T> mapg1_erlang_mixture(
const T& mean,
const T& cv2) {
180 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
181 double kd = std::ceil(1.0 / num_traits<T>::to_double(cv2));
182 std::size_t k = kd < 2.0 ? 2u :
static_cast<std::size_t
>(kd);
183 auto kT = [](std::size_t n) {
return num_traits<T>::from_int(
static_cast<long>(n)); };
184 while (T(kT(k) * cv2) < one) ++k;
185 while (k > 2 && !(T(kT(k - 1) * cv2) < one)) --k;
187 T p = T(T(K * cv2 - sqrt(T(K * T(one + cv2) - T(K * K) * cv2))) / T(one + cv2));
188 if (p < zero) p =
zero;
189 if (p > one) p = one;
190 const T a = T(T(K - p) / mean);
194 for (std::size_t i = 0; i < k; ++i) {
196 if (i + 1 < k) m.D0(i, i + 1) = a;
198 m.D1(k - 1, 0) = T(a * T(one - p));
199 m.D1(k - 1, 1) = T(a * p);
208mam::Map<T> mapg1_hyperexp2(
const T& mean,
const T& cv2) {
210 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
211 const T two = num_traits<T>::from_int(2);
212 const T p = T(T(one + sqrt(T(T(cv2 - one) / T(cv2 + one)))) / two);
213 const T l1 = T(two * p / mean), l2 = T(two * T(one - p) / mean);
220 m.D1(0, 1) = l1 * T(one - p);
222 m.D1(1, 1) = l2 * T(one - p);
227inline double mapg1_aph_m2_lower(
double m1,
double n) {
return m1 * m1 * (n + 1.0) / n; }
230inline double mapg1_aph_m3_lower(
double m1,
double m2,
double n) {
231 const double n2 = m2 / m1 / m1;
232 if (n2 < (n + 1.0) / n)
return std::numeric_limits<double>::infinity();
233 if (n2 < (n + 4.0) / (n + 1.0)) {
234 const double p = ((n + 1.0) * (n2 - 2.0)) / (3.0 * n2 * (n - 1.0)) *
235 ((-2.0 * std::sqrt(n + 1.0)) / std::sqrt(-3.0 * n * n2 + 4.0 * n + 4.0) - 1.0);
236 const double a = (n2 - 2.0) / (p * (1.0 - n2) + std::sqrt(p * p + p * n * (n2 - 2.0) / (n - 1.0)));
237 const double l = ((3.0 + a) * (n - 1.0) + 2.0 * a) / ((n - 1.0) * (1.0 + a * p)) -
238 (2.0 * a * (n + 1.0)) / (2.0 * (n - 1.0) + a * p * (n * a + 2.0 * n - 2.0));
241 return (n + 1.0) / n * n2 * m1 * m2;
245inline double mapg1_aph_m3_upper(
double m1,
double m2,
double n) {
246 const double n2 = m2 / m1 / m1;
247 if (n2 < (n + 1.0) / n)
return -std::numeric_limits<double>::infinity();
248 if (n2 <= n / (n - 1.0))
250 (2.0 * (n - 2.0) * (n * n2 - n - 1.0) * std::sqrt(1.0 + (n * (n2 - 2.0)) / (n - 1.0)) +
251 (n + 2.0) * (3.0 * n * n2 - 2.0 * n - 2.0)) /
253 return std::numeric_limits<double>::infinity();
262mam::Map<T> mapg1_ph2_from_3moments(
const T& m1T,
const T& m2T,
const T& m3T) {
264 const double prec = 1e-14;
265 const double m1 = num_traits<T>::to_double(m1T), m2 = num_traits<T>::to_double(m2T),
266 m3 = num_traits<T>::to_double(m3T);
267 if (m2 < mapg1_aph_m2_lower(m1, 2.0))
268 throw NumericError(ErrorKind{
"Infeasible"},
"PH2From3Moments: The given second moment is not feasible!");
269 if (m3 < mapg1_aph_m3_lower(m1, m2, 2.0))
270 throw NumericError(ErrorKind{
"Infeasible"},
"PH2From3Moments: The given third moment is not feasible (too small)!");
271 if (m3 > mapg1_aph_m3_upper(m1, m2, 2.0))
272 throw NumericError(ErrorKind{
"Infeasible"},
"PH2From3Moments: The given third moment is not feasible (too large)!");
273 if (std::fabs(m2 / m1 / m1 - 2.0) < prec)
return mapg1_exponential(m1T);
275 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
276 const T two = num_traits<T>::from_int(2), three = num_traits<T>::from_int(3), six = num_traits<T>::from_int(6);
277 const T b = T(three * m1T * m2T - m3T);
278 const T c = T(three * m2T * m2T - two * m1T * m3T);
279 const T e = T(m2T - two * m1T * m1T);
280 T a = T(b * b + six * c * e);
281 if (a < zero) a =
zero;
283 T lambda1, lambda2, p;
285 lambda1 = T(T(b - a) / c);
286 lambda2 = T(T(b + a) / c);
287 p = T(T(-b - six * m1T * e + a) / T(b + a));
288 }
else if (c < zero) {
289 lambda1 = T(T(b + a) / c);
290 lambda2 = T(T(b - a) / c);
291 p = T(T(b + six * m1T * e + a) / T(-b + a));
294 lambda2 = T(one / m1T);
301 m.D0(0, 0) = -lambda1;
302 m.D0(0, 1) = lambda1;
303 m.D0(1, 1) = -lambda2;
304 m.D1(1, 0) = lambda2 * p;
305 m.D1(1, 1) = lambda2 * T(one - p);
323 "qsys_mapg1_service_fit requires transcendental arithmetic");
324 if (moments.empty())
throw InputError(
"qsys_mapg1: no service moments given");
326 const T m1 = moments[0];
328 if (moments.size() >= 3) {
333 return mam::aph_fit(moments[0], moments[1], moments[2]).aph;
334 }
catch (
const Error&) {
338 return detail::mapg1_ph2_from_3moments(moments[0], moments[1], moments[2]);
339 }
catch (
const Error&) {
342 return detail::mapg1_exponential(m1);
344 if (moments.size() == 2) {
345 const T cv2 = T(T(moments[1] / T(m1 * m1)) - one);
350 const T den = cv2 > floored ? cv2 : floored;
352 const std::size_t k = kd < 1.0 ? 1u :
static_cast<std::size_t
>(kd);
354 return detail::mapg1_erlang(m1, k);
358 return detail::mapg1_erlang_mixture(m1, cv2);
362 return detail::mapg1_exponential(m1);
365 return detail::mapg1_hyperexp2(m1, cv2);
368 return detail::mapg1_exponential(m1);
383 std::size_t dist_size, std::size_t numQLMoms, std::size_t numSTMoms) {
385 "qsys_mapg1 requires transcendental arithmetic");
411 std::size_t dist_size) {
412 return qsys_mapg1(arrival, moments, dist_size,
static_cast<std::size_t
>(3),
413 static_cast<std::size_t
>(3));
419 return qsys_mapg1(arrival, moments,
static_cast<std::size_t
>(100));
Minimal-order acyclic phase-type fit of the first three moments (matlab/lib/kpctoolbox/aph/aph_fit....
Base error for the multiprecision C++ port.
NumericError(const std::string &what)
The exception types the port throws.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
Dense matrix and non-owning view.
AphFitResult< T > aph_fit(const T &e1, const T &e2, const T &e3, unsigned nmax, const T &tol)
Fit an APH(n) with n <= nmax to the raw moments e1, e2, e3.
T map_lambda(const Map< T > &m)
Stationary arrival rate, lambda = pi D1 e.
mam::Map< T > qsys_mapg1_service_fit(const std::vector< T > &moments, MapG1ServiceFit &kind)
Fit a general service time to a PH, following qsys_mapg1.m's fitServiceToPH.
MapG1ServiceFit
Which branch of fitServiceToPH was taken, for the caller and for tests.
@ Acyclic
three or more moments
@ ErlangMixture
0 < cv2 < 1, the moment-exact Tijms mixture
@ Hyperexponential
cv2 > 1
@ Exponential
one moment, or cv2 exactly 1
@ Erlang
the cv2 <= 0 branch (Erlang-100)
MapG1Result< T > qsys_mapg1(const mam::Map< T > &arrival, const std::vector< T > &moments, std::size_t dist_size, std::size_t numQLMoms, std::size_t numSTMoms)
The MAP/G/1 FCFS queue.
MapMap1Result< T > qsys_mapmap1(const mam::Map< T > &arrival, const mam::Map< T > &service, std::size_t dist_size, std::size_t numQLMoms, std::size_t numSTMoms)
MAP/MAP/1 FCFS queue with the service MAP honoured, correlation included.
Conservation laws of a layered queueing network, enumerated from its structure.
Number-type abstraction for the templated API port.
The MAP/MAP/1 FCFS queue: mean number in system, waiting time, sojourn time, utilization and the queu...
A MAP as the pair of matrices (D0, D1).
std::string analyzer
The reference's analyzer label ('BUTools:MMAPPH1FCFS'), kept for parity; the port computes the same q...
MapG1ServiceFit fitKind
which branch produced it
T meanWaitingTime
max(0, E[W] - m1), as in the reference
T meanQueueLength
E[N], number in system.
std::vector< T > sojournTimeMoments
raw moments E[W^k], k = 1..numSTMoms
std::size_t servicePhases
order of the fitted PH
std::vector< T > queueLengthMoments
raw moments E[N^k], k = 1..numQLMoms
mam::Map< T > serviceFit
the fitted PH as its renewal MAP
std::vector< T > queueLengthDist
P(N = n), n = 0, 1, ...
Return value of the MAP/MAP/1 family (qsys_mapmap1, qsys_mapph1, qsys_phph1), carrying the same quant...
std::vector< T > sojournTimeMoments
raw moments E[W^k], k = 1..numSTMoms
std::vector< T > queueLengthMoments
raw moments E[N^k], k = 1..numQLMoms
T meanQueueLength
E[N], number in system.
T meanSojournTime
E[W] = E[Wq] + E[S].
std::vector< T > queueLengthDist
P(N = n), n = 0, 1, ...