5#ifndef LINE_API_QSYS_QSYS_MM1_TRANSIENT_H
6#define LINE_API_QSYS_QSYS_MM1_TRANSIENT_H
68constexpr double kTransientEps = 2.220446049250313e-16;
69constexpr double kTransientPi = 3.14159265358979323846;
72inline double transient_stirlerr(
double n) {
73 if (n <= 15.0)
return std::lgamma(n + 1.0) - (n + 0.5) * std::log(n) + n - 0.5 * std::log(2.0 * kTransientPi);
74 const double nn = n * n;
75 const double S0 = 1.0 / 12, S1 = 1.0 / 360, S2 = 1.0 / 1260, S3 = 1.0 / 1680, S4 = 1.0 / 1188;
76 return (S0 - (S1 - (S2 - (S3 - S4 / nn) / nn) / nn) / nn) / n;
80inline double transient_bd0(
double k,
double x) {
81 if (std::fabs(k - x) < 0.1 * (k + x)) {
82 double v = (k - x) / (k + x);
83 double s = (k - x) * v;
84 double ej = 2.0 * k * v;
85 const double v2 = v * v;
86 for (
int jj = 1; jj <= 13; ++jj) {
88 s += ej / (2 * jj + 1);
92 return k * std::log(k / x) + x - k;
99inline double transient_lpois(
double k,
double x) {
100 if (k <= 0.0)
return -x;
101 return -transient_stirlerr(k) - transient_bd0(k, x) - 0.5 * std::log(2.0 * kTransientPi * k);
105inline double transient_poiswin_chernoff(
double x,
double m) {
106 const double u = (m - x) / x;
107 if (u <= -1.0)
return std::exp(-x);
108 return std::exp(-x * ((1.0 + u) * std::log1p(u) - u));
112struct PoissonWindow {
114 std::vector<double> w;
115 double bound = 0.0, left = 0.0, right = 0.0;
118inline PoissonWindow transient_poiswin(
double x,
double tol) {
124 const double a = 12.0 * std::sqrt(x) + 40.0;
125 const long lo =
static_cast<long>(std::max(0.0, std::floor(x - a)));
126 const long hi =
static_cast<long>(std::ceil(x + a));
127 const std::size_t nw =
static_cast<std::size_t
>(hi - lo + 1);
128 std::vector<double> w(nw);
129 for (std::size_t k = 0; k < nw; ++k) {
130 const double n =
static_cast<double>(lo +
static_cast<long>(k));
131 w[k] = std::exp(-x + n * std::log(x) - std::lgamma(n + 1.0));
133 double left = lo > 0 ? transient_poiswin_chernoff(x,
static_cast<double>(lo - 1)) : 0.0;
134 double right = transient_poiswin_chernoff(x,
static_cast<double>(hi + 1));
135 std::vector<double> cl(nw), cr(nw);
137 for (std::size_t k = 0; k < nw; ++k) cl[k] = (acc += w[k]);
139 for (std::size_t k = 0; k < nw; ++k) cr[k] = (acc += w[nw - 1 - k]);
140 std::size_t kL = 0, kR = 0;
141 for (std::size_t k = 0; k < nw; ++k)
142 if (cl[k] <= tol / 4) ++kL;
143 for (std::size_t k = 0; k < nw; ++k)
144 if (cr[k] <= tol / 4) ++kR;
145 kL = std::min(kL, nw - 1);
146 kR = std::min(kR, nw - 1 - kL);
147 if (kL > 0) left += cl[kL - 1];
148 if (kR > 0) right += cr[kR - 1];
149 pw.w.assign(w.begin() +
static_cast<std::ptrdiff_t
>(kL), w.end() -
static_cast<std::ptrdiff_t
>(kR));
150 pw.nL = lo +
static_cast<long>(kL);
151 pw.nR = hi -
static_cast<long>(kR);
154 pw.bound = left + right;
159inline std::vector<double> transient_logcumsumexp(
const std::vector<double>& a) {
160 std::vector<double> s = a;
161 for (std::size_t k = 1; k < a.size(); ++k) {
162 const double hi = std::max(s[k - 1], a[k]);
163 if (hi == -std::numeric_limits<double>::infinity())
166 s[k] = hi + std::log1p(std::exp(-std::fabs(s[k - 1] - a[k])));
172inline double transient_chernoff_upper(
double x,
double m) {
173 const double u = (m - x) / x;
174 return std::exp(-x * ((1.0 + u) * std::log1p(u) - u));
182inline std::vector<double> transient_log_ive(std::size_t kmax,
double z) {
183 const double ksd = std::max(
static_cast<double>(kmax), std::ceil(z + 40.0 * std::sqrt(z))) + 60.0;
184 const std::size_t ks =
static_cast<std::size_t
>(ksd);
185 std::vector<double> rr(ks + 1);
186 const double k1 =
static_cast<double>(ks + 1);
187 rr[ks] =
z / (k1 + std::sqrt(k1 * k1 + z * z));
188 for (std::size_t kk = ks; kk >= 1; --kk) rr[kk - 1] = 1.0 / (2.0 *
static_cast<double>(kk) / z + rr[kk]);
193 std::vector<double> c(ks + 1, 0.0);
194 double acc = 0.0, comp = 0.0;
195 for (std::size_t k = 1; k <= ks; ++k) {
196 const double y = std::log(rr[k - 1]) - comp;
197 const double tt = acc + y;
198 comp = (tt - acc) - y;
203 for (std::size_t k = ks; k >= 1; --k) s += std::exp(c[k]);
204 const double l0 = -std::log1p(2.0 * s);
205 std::vector<double> out(kmax + 1);
206 for (std::size_t k = 0; k <= kmax; ++k) out[k] = l0 + c[k];
211struct Mm1TransientRow {
212 std::vector<double> full;
213 double bound = 0.0, errmean = 0.0;
214 double nL = std::numeric_limits<double>::quiet_NaN(), nR = std::numeric_limits<double>::quiet_NaN();
218inline std::vector<double> mm1t_row(
long n,
long i,
double a,
double b,
double logrho) {
219 std::vector<double> row(
static_cast<std::size_t
>(i + n + 1), 0.0);
220 const long vmax = std::min(n, (n + i) / 2);
221 const std::size_t nvv =
static_cast<std::size_t
>(vmax + 1);
222 std::vector<double> keep(nvv, 1.0);
226 for (
long l = 0; l <= i; ++l)
227 logr0 += std::log1p(
static_cast<double>(2 * vmax - n - 1 - 2 * l) /
228 static_cast<double>(n - vmax + 1 + l));
229 const long nv = vmax - i;
231 keep[0] = -std::expm1(std::min(logr, 0.0));
232 for (
long q = 1; q < nv; ++q) {
233 const double vv =
static_cast<double>(vmax - (q - 1));
234 logr += std::log1p(-
static_cast<double>(i + 1) / vv) +
235 std::log1p(-
static_cast<double>(i + 1) / (
static_cast<double>(n) - vv +
static_cast<double>(i) + 2.0));
236 keep[
static_cast<std::size_t
>(q)] = -std::expm1(std::min(logr, 0.0));
239 for (std::size_t q = 0; q < nvv; ++q) {
240 const long v = vmax -
static_cast<long>(q);
241 const long u = n - v;
242 const long j = i + n - 2 * v;
243 const double kval = std::exp(transient_lpois(
static_cast<double>(u), a) +
244 transient_lpois(
static_cast<double>(v), b)) * keep[q];
245 row[
static_cast<std::size_t
>(j)] += kval;
248 const long kmax = (n - i - 1) / 2;
249 std::vector<double> logphi(
static_cast<std::size_t
>(kmax + 1));
250 for (
long k = 0; k <= kmax; ++k)
251 logphi[
static_cast<std::size_t
>(k)] = std::log(
static_cast<double>(n + 1 - 2 * k)) +
252 transient_lpois(
static_cast<double>(k), a) +
253 transient_lpois(
static_cast<double>(n + 1 - k), b) - std::log(b);
254 const std::vector<double> logS = transient_logcumsumexp(logphi);
255 for (
long jj = 0; jj <= n - i - 1; ++jj) {
256 const long kk = (n - i - 1 - jj) / 2;
257 row[
static_cast<std::size_t
>(jj)] +=
258 std::exp(
static_cast<double>(jj) * logrho + logS[
static_cast<std::size_t
>(kk)]);
264inline Mm1TransientRow mm1t_uniformization(
double lam,
double mu,
long i,
double t,
double tol) {
265 const double x = (lam + mu) * t;
266 const PoissonWindow pw = transient_poiswin(x, tol);
268 out.full.assign(
static_cast<std::size_t
>(i + pw.nR + 1), 0.0);
269 const double a = lam * t, b = mu * t, logrho = std::log(lam / mu);
270 for (
long n = pw.nL; n <= pw.nR; ++n) {
271 const std::vector<double> row = mm1t_row(n, i, a, b, logrho);
272 for (std::size_t k = 0; k < row.size(); ++k) out.full[k] += row[k];
274 out.bound = pw.bound;
275 out.errmean =
static_cast<double>(i + pw.nL) * pw.left +
static_cast<double>(i) * pw.right +
276 x * (pw.w.back() + pw.right);
277 out.nL =
static_cast<double>(pw.nL);
278 out.nR =
static_cast<double>(pw.nR);
282inline Mm1TransientRow mm1t_bessel(
double lam,
double mu,
long i,
double t) {
283 const double rho = lam / mu, logrho = std::log(rho);
284 const double z = 2.0 * t * std::sqrt(lam * mu);
285 const double delta = (std::sqrt(mu) - std::sqrt(lam)) * (std::sqrt(mu) - std::sqrt(lam));
287 const long J = i +
static_cast<long>(std::ceil(lam * t + 12.0 * std::sqrt(lam * t) + 40.0));
288 double bound = transient_chernoff_upper(lam * t,
static_cast<double>(J - i + 1));
289 std::size_t kmax =
static_cast<std::size_t
>(J + i + 2) +
290 static_cast<std::size_t
>(std::ceil(12.0 * std::sqrt(z) + 40.0));
291 std::vector<double> logI, logterm;
293 logI = transient_log_ive(kmax, z);
294 logterm.assign(kmax + 1, 0.0);
295 double mx = -std::numeric_limits<double>::infinity();
296 for (std::size_t k = 0; k <= kmax; ++k) {
297 logterm[k] = -0.5 *
static_cast<double>(k) * logrho + logI[k] - delta * t;
298 mx = std::max(mx, logterm[k]);
300 const bool tailok = logterm[kmax] < mx + std::log(kTransientEps) - 60.0 && logterm[kmax] <= logterm[kmax - 1];
301 if (tailok || !std::isfinite(logterm[kmax]))
break;
305 std::vector<double> rev(logterm.rbegin(), logterm.rend());
306 const std::vector<double> cs = transient_logcumsumexp(rev);
307 std::vector<double> logT(cs.rbegin(), cs.rend());
309 out.full.assign(
static_cast<std::size_t
>(J + 1), 0.0);
311 for (
long j = 0; j <= J; ++j) {
312 const long d = j > i ? j - i : i - j;
313 const double t1 = std::exp(0.5 *
static_cast<double>(j - i) * logrho + logI[
static_cast<std::size_t
>(d)] - delta * t);
314 const double t2 = std::exp(0.5 *
static_cast<double>(j - i - 1) * logrho +
315 logI[
static_cast<std::size_t
>(j + i + 1)] - delta * t);
316 const double t3 = (1.0 - rho) * std::exp(
static_cast<double>(j) * logrho + logT[
static_cast<std::size_t
>(j + i + 2)]);
317 out.full[
static_cast<std::size_t
>(j)] = std::max(t1 + t2 + t3, 0.0);
318 sum += out.full[
static_cast<std::size_t
>(j)];
320 const double defect = std::fabs(1.0 - sum);
321 out.errmean =
static_cast<double>(J) * defect + lam * t * bound;
322 out.bound = std::max(bound, defect);
326inline void transient_arg_error(
const char* fn,
const std::string& msg) {
327 throw InputError(ErrorKind{
"Argument"}, std::string(fn) +
": " + msg);
330inline bool transient_is_int(
double v) {
return std::isfinite(v) && v == std::round(v); }
337 std::vector<double>
EN;
338 std::vector<double>
P0;
360 double nmax = std::numeric_limits<double>::quiet_NaN(),
361 double tol = 1e-14,
const std::string& method_in =
"auto") {
362 using namespace detail;
363 const char* fn =
"qsys_mm1_transient";
364 if (!std::isfinite(lambda) || lambda <= 0) transient_arg_error(fn,
"lambda must be finite and positive");
365 if (!std::isfinite(mu) || mu <= 0) transient_arg_error(fn,
"mu must be finite and positive");
366 if (!transient_is_int(n0) || n0 < 0) transient_arg_error(fn,
"n0 must be an integer >= 0");
367 if (t.empty()) transient_arg_error(fn,
"t must be a nonempty vector of finite nonnegative times");
369 if (!std::isfinite(tv) || tv < 0)
370 transient_arg_error(fn,
"t must be a nonempty vector of finite nonnegative times");
371 if (!std::isfinite(tol) || tol <= 0 || tol >= 1) transient_arg_error(fn,
"tol must lie in (0, 1)");
372 std::string method = method_in;
373 std::transform(method.begin(), method.end(), method.begin(),
374 [](
unsigned char c) { return static_cast<char>(std::tolower(c)); });
375 if (method !=
"auto" && method !=
"uniformization" && method !=
"bessel")
376 transient_arg_error(fn,
"method must be 'auto', 'uniformization' or 'bessel'");
377 const long i =
static_cast<long>(n0);
378 const std::size_t nt = t.size();
381 res.
rho = lambda / mu;
383 res.
method.assign(nt,
"");
387 std::vector<std::vector<double> > fulls(nt);
388 for (std::size_t r = 0; r < nt; ++r) {
390 fulls[r].assign(
static_cast<std::size_t
>(i + 1), 0.0);
391 fulls[r][
static_cast<std::size_t
>(i)] = 1.0;
397 const double x = (lambda + mu) * t[r];
398 std::string m = method;
399 if (m ==
"auto") m = x <= 1000.0 ?
"uniformization" :
"bessel";
400 const Mm1TransientRow row = m ==
"uniformization" ? mm1t_uniformization(lambda, mu, i, t[r], tol)
401 : mm1t_bessel(lambda, mu, i, t[r]);
405 res.
window(r, 0) = row.nL;
406 res.
window(r, 1) = row.nR;
410 std::size_t width = 0;
411 for (
const auto& f : fulls) width = std::max(width, f.size());
412 const std::size_t Jall = width - 1;
414 for (std::size_t r = 0; r < nt; ++r)
415 for (std::size_t j = 0; j < fulls[r].size(); ++j) F(r, j) = fulls[r][j];
416 res.
EN.assign(nt, 0.0);
417 for (std::size_t r = 0; r < nt; ++r)
418 for (std::size_t j = 0; j <= Jall; ++j) res.
EN[r] += F(r, j) *
static_cast<double>(j);
420 for (std::size_t r = 0; r < nt; ++r) {
422 for (std::size_t j = Jall + 1; j-- > 0;) tails(r, j) = (acc += F(r, j));
425 if (std::isnan(nmax)) {
427 for (std::size_t j = Jall + 1; j-- > 0 && !found;) {
429 for (std::size_t r = 0; r < nt; ++r) mx = std::max(mx, tails(r, j));
437 if (!transient_is_int(nmax) || nmax < 0) transient_arg_error(fn,
"nmax must be an integer >= 0");
438 nm =
static_cast<std::size_t
>(nmax);
442 for (std::size_t r = 0; r < nt; ++r)
443 for (std::size_t j = 0; j <= std::min(nm, Jall); ++j) res.
P(r, j) = F(r, j);
445 for (std::size_t r = 0; r < nt; ++r) res.
tailMass[r] = tails(r, nm + 1);
446 res.
P0.assign(nt, 0.0);
447 for (std::size_t r = 0; r < nt; ++r) res.
P0[r] = res.
P(r, 0);
448 res.
nmax =
static_cast<long>(nm);
The exception types the port throws.
Dense matrix and non-owning view.
Mm1TransientResult qsys_mm1_transient(double lambda, double mu, double n0, const std::vector< double > &t, double nmax=std::numeric_limits< double >::quiet_NaN(), double tol=1e-14, const std::string &method_in="auto")
Transient distribution of the M/M/1 queue (Rubino 2020).
Conservation laws of a layered queueing network, enumerated from its structure.
Return value of qsys_mm1_transient: [P, EN, P0, res].
std::vector< double > tailMass
std::vector< double > errBoundMean
std::vector< double > errBound
std::vector< double > P0
(nt) empty probability
Matrix< double > window
(nt x 2) [nL nR], NaN for 'bessel'
std::vector< double > EN
(nt) mean over all states
std::vector< std::string > method
per time: 'uniformization', 'bessel' or 'exact'
Matrix< double > P
(nt x nmax+1) probabilities