LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
fj_xmax_erlang.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_FJ_XMAX_ERLANG_H
6#define LINE_API_FJ_XMAX_ERLANG_H
7
8/**
9 * @file
10 * @ingroup api_fj
11 * Expected maximum of K i.i.d. Erlang-k service times.
12 *
13 * Templated port of matlab/src/api/fj/fj_xmax_erlang.m, cross-checked against
14 * FJ_xmax.fj_xmax_erlang in jar/src/main/java/jline/api/fj/FJ_xmax.java
15 * (identical formulas; the JAR replaces MATLAB's adaptive `integral` with a
16 * 10001-point composite Simpson rule, which this port also does).
17 *
18 * MIXED ARITHMETIC. At k = 2 the MATLAB file uses the closed form
19 *
20 * X_K^max = (1/mu) sum_{n=1..K} C(K,n) (-1)^{n-1} sum_{m=0..n} C(n,m) m! / n^{m+1}
21 *
22 * the inner sum being int_0^inf S(t)^n dt for S(t) = e^{-mu t}(1 + mu t). Until
23 * 2026-11-14 every codebase started the inner sum at m = 1 and divided by an
24 * extra 2, giving 1/(2 mu) at K = 1 instead of the Erlang-2 mean 2/mu.
25 *
26 * which is rational and therefore exact in any field, cancellation included:
27 * the alternating outer sum is exactly the kind that double cannot carry past
28 * about K = 25. For any other k the mean is a quadrature of 1 - F(t)^K against
29 * the Erlang CDF and needs exp, so it is only available when T carries
30 * transcendental functions; asking for it at exact arithmetic throws
31 * UnsupportedError rather than silently substituting something else.
32 */
33
35#include "line/num/number.h"
36#include "line/util/error.h"
37
38namespace line {
39namespace fj {
40
41namespace detail {
42
43/** Erlang-k CDF, 1 - exp(-mu t) sum_{j<k} (mu t)^j / j!. */
44template <class T>
45T erlang_cdf(const T& t, unsigned k, const T& mu) {
46 static_assert(num_traits<T>::has_transcendental,
47 "erlang_cdf requires transcendental arithmetic");
48 const T zero = num_traits<T>::from_int(0);
49 if (t <= zero) return zero;
50 const T x = mu * t;
51 T S = zero;
52 for (unsigned j = 0; j < k; ++j) S += num_pow_int(x, j) / num_factorial<T>(j);
53 return num_traits<T>::from_int(1) - num_exp(T(-x)) * S;
54}
55
56/** Erlang-k survival function, exp(-mu x) sum_{j<k} (mu x)^j / j!. */
57template <class T>
58T erlang_survival(const T& x, unsigned k, const T& mu) {
59 static_assert(num_traits<T>::has_transcendental,
60 "erlang_survival requires transcendental arithmetic");
61 const T zero = num_traits<T>::from_int(0);
62 if (x <= zero) return num_traits<T>::from_int(1);
63 const T y = mu * x;
64 T S = zero;
65 for (unsigned j = 0; j < k; ++j) S += num_pow_int(y, j) / num_factorial<T>(j);
66 return num_exp(T(-y)) * S;
67}
68
69/** Quadrature branch of fj_xmax_erlang, for k != 2. */
70template <class T>
71T fj_xmax_erlang_quad(unsigned K, unsigned k, const T& mu) {
72 const T kk = num_traits<T>::from_int(static_cast<long>(k));
73 const T upper = kk / mu * num_traits<T>::from_int(10) + num_traits<T>::from_int(10) * num_sqrt(kk) / mu;
74 const T one = num_traits<T>::from_int(1);
75 return simpson<T>([&](const T& t) { return T(one - num_pow_int(erlang_cdf(t, k, mu), K)); },
76 num_traits<T>::from_int(0), upper);
77}
78
79} // namespace detail
80
81/**
82 * @brief Expected maximum of K i.i.d. Erlang-k service times.
83 *
84 * @param K number of branches, K >= 1
85 * @param k Erlang stages, k >= 1
86 * @param mu per-stage rate, > 0 (branch mean is k/mu)
87 * @return expected maximum of K Erlang-k samples
88 */
89template <class T>
90T fj_xmax_erlang(unsigned K, unsigned k, const T& mu) {
91 detail::require_positive_K(K, "fj_xmax_erlang");
92 if (k < 1) throw InputError("fj_xmax_erlang: the stage count k must be a positive integer");
93 if (mu <= num_traits<T>::from_int(0)) throw InputError("fj_xmax_erlang: the rate mu must be positive");
94
95 if (k == 2) {
96 T outer = num_traits<T>::from_int(0);
97 for (unsigned n = 1; n <= K; ++n) {
98 const T nn = num_traits<T>::from_int(static_cast<long>(n));
99 T inner = num_traits<T>::from_int(0);
100 for (unsigned m = 0; m <= n; ++m)
101 inner += detail::fj_binom<T>(n, m) * num_factorial<T>(m) / num_pow_int(nn, m + 1);
102 const T term = detail::fj_binom<T>(K, n) * inner;
103 if ((n - 1) % 2 == 0) outer += term;
104 else outer -= term;
105 }
106 return outer / mu;
107 }
108
109 if constexpr (num_traits<T>::has_transcendental) {
110 return detail::fj_xmax_erlang_quad<T>(K, k, mu);
111 } else {
112 throw UnsupportedError(ErrorKind{"Arithmetic"},
113 "fj_xmax_erlang: only k = 2 has a closed form; any other stage count needs a "
114 "quadrature of the Erlang CDF and therefore transcendental arithmetic");
115 }
116}
117
118} // namespace fj
119} // namespace line
120
121#endif // LINE_API_FJ_XMAX_ERLANG_H
InputError(const std::string &what)
Definition error.h:105
UnsupportedError(const std::string &what)
Definition error.h:119
The exception types the port throws.
Shared return types and arithmetic helpers for the templated fork-join port.
T fj_xmax_erlang(unsigned K, unsigned k, const T &mu)
Expected maximum of K i.i.d.
std::string upper(std::string s)
Definition srvn_writer.h:86
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
T num_factorial(unsigned n)
Factorial as a value of T.
Definition number.h:196
T num_pow_int(const T &base, unsigned e)
Integer power, valid in any field (no transcendental requirement).
Definition number.h:204
Number-type abstraction for the templated API port.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Definition error.h:37