5#ifndef LINE_API_QSYS_QSYS_BMAPPHNN_RETRIAL_H
6#define LINE_API_QSYS_QSYS_BMAPPHNN_RETRIAL_H
158inline std::vector<std::vector<int>> retrial_compositions(
int n,
int M) {
159 std::vector<std::vector<int>> out;
160 if (M <= 0)
return out;
162 out.push_back(std::vector<int>(1, n));
165 for (
int m1 = n; m1 >= 0; --m1) {
166 const std::vector<std::vector<int>> sub = retrial_compositions(n - m1, M - 1);
167 for (std::size_t s = 0; s < sub.
size(); ++s) {
168 std::vector<int> row;
169 row.reserve(
static_cast<std::size_t
>(M));
171 row.insert(row.end(), sub[s].begin(), sub[s].end());
179inline int retrial_find(
const std::vector<std::vector<int>>& comps,
const std::vector<int>& key) {
180 for (std::size_t j = 0; j < comps.size(); ++j)
181 if (comps[j] == key)
return static_cast<int>(j);
188 std::vector<Matrix<T>> D;
192 int M = 0, N = 0, V = 0, K = 0;
194 std::vector<std::size_t> Tn;
197 std::vector<std::vector<std::vector<int>>> comps;
202std::size_t retrial_offset(
const RetrialCtx<T>& c,
int n) {
204 for (
int i = 0; i < n; ++i) off += c.Tn[
static_cast<std::size_t
>(i)];
210Matrix<T> retrial_L(
const RetrialCtx<T>& c,
int n) {
211 const T
zero = num_traits<T>::from_int(0);
213 Matrix<T> L(c.Tn[
static_cast<std::size_t
>(n)], c.Tn[
static_cast<std::size_t
>(n - 1)], zero);
214 const std::vector<std::vector<int>>& cn = c.comps[
static_cast<std::size_t
>(n)];
215 const std::vector<std::vector<int>>& cm = c.comps[
static_cast<std::size_t
>(n - 1)];
216 for (std::size_t i = 0; i < cn.size(); ++i)
217 for (
int l = 0; l < c.M; ++l) {
218 if (cn[i][
static_cast<std::size_t
>(l)] <= 0)
continue;
219 std::vector<int> mp = cn[i];
220 --mp[
static_cast<std::size_t
>(l)];
221 const int j = retrial_find(cm, mp);
223 L(i,
static_cast<std::size_t
>(j)) +=
224 num_traits<T>::from_int(cn[i][
static_cast<std::size_t
>(l)]) *
225 c.S0[
static_cast<std::size_t
>(l)];
232Matrix<T> retrial_A(
const RetrialCtx<T>& c,
int n) {
233 const T
zero = num_traits<T>::from_int(0);
234 if (n == 0)
return Matrix<T>(1, 1, zero);
235 Matrix<T> A(c.Tn[
static_cast<std::size_t
>(n)], c.Tn[
static_cast<std::size_t
>(n)], zero);
236 const std::vector<std::vector<int>>& cn = c.comps[
static_cast<std::size_t
>(n)];
237 for (std::size_t i = 0; i < cn.size(); ++i)
238 for (
int l = 0; l < c.M; ++l) {
239 if (cn[i][
static_cast<std::size_t
>(l)] <= 0)
continue;
240 for (
int lp = 0; lp < c.M; ++lp) {
241 if (lp == l)
continue;
242 if (!(c.S(
static_cast<std::size_t
>(l),
static_cast<std::size_t
>(lp)) > zero))
244 std::vector<int> mp = cn[i];
245 --mp[
static_cast<std::size_t
>(l)];
246 ++mp[
static_cast<std::size_t
>(lp)];
247 const int j = retrial_find(cn, mp);
249 A(i,
static_cast<std::size_t
>(j)) +=
250 num_traits<T>::from_int(cn[i][
static_cast<std::size_t
>(l)]) *
251 c.S(
static_cast<std::size_t
>(l),
static_cast<std::size_t
>(lp));
259Matrix<T> retrial_P(
const RetrialCtx<T>& c,
int n) {
260 const T
zero = num_traits<T>::from_int(0);
262 Matrix<T>
P(c.Tn[
static_cast<std::size_t
>(n)], c.Tn[
static_cast<std::size_t
>(n + 1)], zero);
263 const std::vector<std::vector<int>>& cn = c.comps[
static_cast<std::size_t
>(n)];
264 const std::vector<std::vector<int>>& cp = c.comps[
static_cast<std::size_t
>(n + 1)];
265 for (std::size_t i = 0; i < cn.size(); ++i)
266 for (
int l = 0; l < c.M; ++l) {
267 if (!(c.beta[
static_cast<std::size_t
>(l)] > zero))
continue;
268 std::vector<int> mp = cn[i];
269 ++mp[
static_cast<std::size_t
>(l)];
270 const int j = retrial_find(cp, mp);
272 P(i,
static_cast<std::size_t
>(j)) += c.beta[
static_cast<std::size_t
>(l)];
279Matrix<T> retrial_Delta(
const RetrialCtx<T>& c,
int n) {
280 const T
zero = num_traits<T>::from_int(0);
281 if (n == 0)
return Matrix<T>(1, 1, zero);
282 Matrix<T> D(c.Tn[
static_cast<std::size_t
>(n)], c.Tn[
static_cast<std::size_t
>(n)], zero);
283 const std::vector<std::vector<int>>& cn = c.comps[
static_cast<std::size_t
>(n)];
284 for (std::size_t i = 0; i < cn.size(); ++i) {
286 for (
int l = 0; l < c.M; ++l)
287 total += num_traits<T>::from_int(cn[i][
static_cast<std::size_t
>(l)]) *
288 T(-c.S(
static_cast<std::size_t
>(l),
static_cast<std::size_t
>(l)));
296Matrix<T> retrial_Gamma(
const RetrialCtx<T>& c,
int nu) {
297 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
298 Matrix<T> G(c.d, c.d, zero);
300 for (
int n = 0; n <= c.N; ++n) {
301 const std::size_t width = c.Tn[
static_cast<std::size_t
>(n)];
302 if (
static_cast<long>(n) > c.R[
static_cast<std::size_t
>(nu)])
303 for (std::size_t t = 0; t < width; ++t) G(off + t, off + t) = one;
311T retrial_G_scalar(
const RetrialCtx<T>& c,
int n,
int nu,
int nuPrime) {
312 const T
zero = num_traits<T>::from_int(0);
313 if (n <= c.N - c.K)
return zero;
315 for (
int k = c.N - n + 1; k <= c.K; ++k)
317 total += c.D[
static_cast<std::size_t
>(k)](
static_cast<std::size_t
>(nu),
318 static_cast<std::size_t
>(nuPrime));
319 return T(c.p * total);
327Matrix<T> retrial_Pprod(
const RetrialCtx<T>& c,
const std::vector<Matrix<T>>& P,
int n,
int upto) {
328 Matrix<T> prod =
eye<T>(c.Tn[
static_cast<std::size_t
>(n)]);
329 for (
int j = n; j < upto; ++j)
330 if (j < c.N) prod =
matmul(prod, P[
static_cast<std::size_t
>(j)]);
336Matrix<T> retrial_B(
const RetrialCtx<T>& c,
const std::vector<Matrix<T>>& L,
337 const std::vector<Matrix<T>>& A,
const std::vector<Matrix<T>>& P,
338 const std::vector<Matrix<T>>& Delta,
int nu) {
339 const T
zero = num_traits<T>::from_int(0);
340 Matrix<T> B(c.d, c.d, zero);
341 for (
int n = 0; n <= c.N; ++n) {
342 const std::size_t rs = retrial_offset(c, n);
343 const std::size_t w = c.Tn[
static_cast<std::size_t
>(n)];
344 const T g = retrial_G_scalar(c, n, nu, nu);
348 for (std::size_t a = 0; a < w; ++a)
349 for (std::size_t b = 0; b < w; ++b)
350 B(rs + a, rs + b) = A[
static_cast<std::size_t
>(n)](a, b) +
351 Delta[
static_cast<std::size_t
>(n)](a, b) +
355 const std::size_t cs = retrial_offset(c, n - 1);
356 for (std::size_t a = 0; a < w; ++a)
357 for (std::size_t b = 0; b < c.Tn[
static_cast<std::size_t
>(n - 1)]; ++b)
358 B(rs + a, cs + b) = L[
static_cast<std::size_t
>(n)](a, b);
360 for (
int k = 1; k <= c.K; ++k) {
361 if (n + k > c.N)
continue;
362 const std::size_t cs = retrial_offset(c, n + k);
363 const T dk = c.D[
static_cast<std::size_t
>(k)](
static_cast<std::size_t
>(nu),
364 static_cast<std::size_t
>(nu));
365 const Matrix<T> pp = retrial_Pprod(c, P, n, n + k);
366 for (std::size_t a = 0; a < w; ++a)
367 for (std::size_t b = 0; b < c.Tn[
static_cast<std::size_t
>(n + k)]; ++b)
368 B(rs + a, cs + b) = dk * pp(a, b);
376Matrix<T> retrial_Bbar(
const RetrialCtx<T>& c,
const std::vector<Matrix<T>>& P,
int nu) {
377 const T
zero = num_traits<T>::from_int(0);
378 Matrix<T> B(c.d, c.d, zero);
379 const long cap = c.R[
static_cast<std::size_t
>(nu)] <
static_cast<long>(c.N - 1)
380 ? c.R[
static_cast<std::size_t
>(nu)]
381 : static_cast<long>(c.N - 1);
382 for (
long n = 0; n <= cap; ++n) {
383 const std::size_t rs = retrial_offset(c,
static_cast<int>(n));
384 const std::size_t cs = retrial_offset(c,
static_cast<int>(n) + 1);
385 const Matrix<T>& Pn =
P[
static_cast<std::size_t
>(n)];
386 for (std::size_t a = 0; a < Pn.rows(); ++a)
387 for (std::size_t b = 0; b < Pn.cols(); ++b) B(rs + a, cs + b) = Pn(a, b);
394Matrix<T> retrial_Btilde(
const RetrialCtx<T>& c,
const std::vector<Matrix<T>>& P,
int nu,
396 const T
zero = num_traits<T>::from_int(0);
397 Matrix<T> B(c.d, c.d, zero);
398 for (
int n = 0; n <= c.N; ++n) {
399 const std::size_t rs = retrial_offset(c, n);
400 const std::size_t w = c.Tn[
static_cast<std::size_t
>(n)];
401 const T g = retrial_G_scalar(c, n, nu, nuPrime);
402 for (std::size_t a = 0; a < w; ++a) B(rs + a, rs + a) = g;
403 for (
int k = 1; k <= c.K; ++k) {
404 if (n + k > c.N)
continue;
405 const std::size_t cs = retrial_offset(c, n + k);
406 const T dk = c.D[
static_cast<std::size_t
>(k)](
static_cast<std::size_t
>(nu),
407 static_cast<std::size_t
>(nuPrime));
408 const Matrix<T> pp = retrial_Pprod(c, P, n, n + k);
409 for (std::size_t a = 0; a < w; ++a)
410 for (std::size_t b = 0; b < c.Tn[
static_cast<std::size_t
>(n + k)]; ++b)
411 B(rs + a, cs + b) = dk * pp(a, b);
423Matrix<T> retrial_C(
const RetrialCtx<T>& c,
const std::vector<Matrix<T>>& P,
int n,
int k,
int nu,
425 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
426 if (n < c.N - c.K + k)
return Matrix<T>();
428 const int batch = c.N - n + k;
429 if (batch < 1 || batch > c.K)
return Matrix<T>();
430 const T db = c.D[
static_cast<std::size_t
>(batch)](
static_cast<std::size_t
>(nu),
431 static_cast<std::size_t
>(nuPrime));
432 Matrix<T> pp = retrial_Pprod(c, P, n, c.N);
433 for (std::size_t a = 0; a < pp.rows(); ++a)
434 for (std::size_t b = 0; b < pp.cols(); ++b) pp(a, b) = T(one - c.p) * db * pp(a, b);
437 if (k < 1 || k > c.K)
return Matrix<T>();
438 const T dk = c.D[
static_cast<std::size_t
>(k)](
static_cast<std::size_t
>(nu),
439 static_cast<std::size_t
>(nuPrime));
440 Matrix<T> out(c.Tn[
static_cast<std::size_t
>(c.N)], c.Tn[
static_cast<std::size_t
>(c.N)], zero);
441 for (std::size_t a = 0; a < out.rows(); ++a) out(a, a) = T(one - c.p) * dk;
463 const std::vector<T>& beta,
const Matrix<T>& S,
464 int N,
const T& alpha,
const T& gamma,
const T& p,
465 const std::vector<long>& R,
468 if (D.size() < 2)
throw InputError(
"qsys_bmapphnn_retrial: the BMAP needs D0 and at least D1");
469 if (N < 1)
throw InputError(
"qsys_bmapphnn_retrial: at least one server is required");
470 const int V =
static_cast<int>(D[0].rows());
471 if (V < 1)
throw InputError(
"qsys_bmapphnn_retrial: empty BMAP");
472 for (std::size_t k = 0; k < D.size(); ++k)
473 if (D[k].rows() !=
static_cast<std::size_t
>(V) || D[k].cols() !=
static_cast<std::size_t
>(V))
474 throw InputError(
"qsys_bmapphnn_retrial: every BMAP matrix must be V x V");
475 const int M =
static_cast<int>(S.
rows());
476 if (M < 1 || S.
cols() !=
static_cast<std::size_t
>(M) ||
477 beta.size() !=
static_cast<std::size_t
>(M))
478 throw InputError(
"qsys_bmapphnn_retrial: the PH service is malformed");
479 if (R.empty())
throw InputError(
"qsys_bmapphnn_retrial: no admission threshold given");
485 const double sqrtFineTol = std::sqrt(fineTol);
486 Matrix<T> DsumV(
static_cast<std::size_t
>(V),
static_cast<std::size_t
>(V), zero);
487 for (std::size_t k = 0; k < D.size(); ++k)
488 for (std::size_t i = 0; i < D[k].rows(); ++i)
489 for (std::size_t j = 0; j < D[k].cols(); ++j) {
491 if (!std::isfinite(v))
492 throw InputError(
"qsys_bmapphnn_retrial: BMAP matrix D{" +
493 std::to_string(k + 1) +
494 "} contains NaN or Inf entries. The arrival process is "
495 "disabled or not phase-type representable.");
496 if (k >= 1 && v < -fineTol)
497 throw InputError(
"qsys_bmapphnn_retrial: BMAP arrival matrix D{" +
498 std::to_string(k + 1) +
"} must be non-negative.");
499 DsumV(i, j) += D[k](i, j);
501 for (std::size_t i = 0; i < D[0].rows(); ++i) {
503 throw InputError(
"qsys_bmapphnn_retrial: BMAP matrix D0 must have non-positive "
504 "diagonal entries.");
506 for (std::size_t j = 0; j < DsumV.
cols(); ++j) rs += DsumV(i, j);
508 throw InputError(
"qsys_bmapphnn_retrial: BMAP matrices are inconsistent: sum_k D_k "
509 "must have zero row sums.");
512 for (std::size_t i = 0; i < beta.size(); ++i) {
514 if (!std::isfinite(v))
515 throw InputError(
"qsys_bmapphnn_retrial: Phase-type service vector beta is empty or "
516 "contains NaN/Inf. The service distribution is disabled or not "
517 "phase-type representable.");
519 throw InputError(
"qsys_bmapphnn_retrial: Phase-type service vector beta must be "
520 "non-negative and sum to one.");
524 throw InputError(
"qsys_bmapphnn_retrial: Phase-type service vector beta must be "
525 "non-negative and sum to one.");
526 for (std::size_t i = 0; i < S.
rows(); ++i) {
528 for (std::size_t j = 0; j < S.
cols(); ++j) {
530 throw InputError(
"qsys_bmapphnn_retrial: Phase-type service subgenerator S "
531 "contains NaN or Inf entries. The service distribution is "
532 "disabled or not phase-type representable.");
535 if (!(S(i, i) < zero))
536 throw InputError(
"qsys_bmapphnn_retrial: Phase-type service subgenerator S must "
537 "have strictly negative diagonal entries.");
539 throw InputError(
"qsys_bmapphnn_retrial: Phase-type service subgenerator S must "
540 "have non-negative exit rates.");
544 if (!std::isfinite(ad) || ad < 0.0)
545 throw InputError(
"qsys_bmapphnn_retrial: Retrial rate alpha must be a finite "
546 "non-negative scalar.");
547 if (!std::isfinite(gd) || gd < 0.0)
548 throw InputError(
"qsys_bmapphnn_retrial: Orbit impatience rate gamma must be a finite "
549 "non-negative scalar.");
550 if (!std::isfinite(pd) || pd < 0.0 || pd > 1.0)
551 throw InputError(
"qsys_bmapphnn_retrial: Batch rejection probability p must lie in "
553 for (std::size_t i = 0; i < R.size(); ++i)
554 if (R[i] < 0 || R[i] >
static_cast<long>(N))
555 throw InputError(
"qsys_bmapphnn_retrial: Admission threshold R must lie in [0," +
556 std::to_string(N) +
"].");
559 detail::RetrialCtx<T> c;
566 c.K =
static_cast<int>(D.size()) - 1;
570 c.R.assign(
static_cast<std::size_t
>(V), R[0]);
572 if (R.size() !=
static_cast<std::size_t
>(V))
573 throw InputError(
"qsys_bmapphnn_retrial: R must be a scalar or one entry per BMAP "
579 c.S0.assign(
static_cast<std::size_t
>(M), zero);
580 for (
int i = 0; i < M; ++i) {
582 for (
int j = 0; j < M; ++j)
583 s += S(
static_cast<std::size_t
>(i),
static_cast<std::size_t
>(j));
584 c.S0[
static_cast<std::size_t
>(i)] = -s;
588 c.Tn.assign(
static_cast<std::size_t
>(N) + 1, 0);
589 c.comps.resize(
static_cast<std::size_t
>(N) + 1);
591 for (
int n = 0; n <= N; ++n) {
592 c.comps[
static_cast<std::size_t
>(n)] = detail::retrial_compositions(n, M);
593 c.Tn[
static_cast<std::size_t
>(n)] = c.comps[
static_cast<std::size_t
>(n)].size();
594 c.d += c.Tn[
static_cast<std::size_t
>(n)];
598 Matrix<T> Dsum(
static_cast<std::size_t
>(V),
static_cast<std::size_t
>(V), zero);
599 Matrix<T> DkSum(
static_cast<std::size_t
>(V),
static_cast<std::size_t
>(V), zero);
600 for (std::size_t k = 0; k < D.size(); ++k)
601 for (std::size_t i = 0; i < Dsum.
rows(); ++i)
602 for (std::size_t j = 0; j < Dsum.
cols(); ++j) {
603 Dsum(i, j) += D[k](i, j);
609 for (std::size_t j = 0; j < A.
cols(); ++j) A(A.
rows() - 1, j) = one;
610 std::vector<T> rhs(A.
rows(), zero);
611 rhs[A.
rows() - 1] = one;
612 const std::vector<T> theta =
solve(A, rhs);
614 for (std::size_t i = 0; i < theta.size(); ++i)
615 for (std::size_t j = 0; j < DkSum.
cols(); ++j) lambda += theta[i] * DkSum(i, j);
618 for (std::size_t i = 0; i < negS.
rows(); ++i)
619 for (std::size_t j = 0; j < negS.
cols(); ++j) negS(i, j) = -negS(i, j);
622 for (
int i = 0; i < M; ++i) b1 += beta[static_cast<std::size_t>(i)] * tau[
static_cast<std::size_t
>(i)];
623 if (!(b1 > zero))
throw InputError(
"qsys_bmapphnn_retrial: the mean service time must be positive");
625 const std::size_t Vd =
static_cast<std::size_t
>(V) * c.d;
629 if (
static_cast<double>(Vd) >
opt.maxBlockSize)
630 throw InputError(
"qsys_bmapphnn_retrial: Per-level block size V*d = " + std::to_string(Vd) +
631 " exceeds MaxBlockSize = " +
632 std::to_string(
static_cast<long long>(std::llround(
opt.maxBlockSize))) +
633 ". The service distribution has " + std::to_string(M) +
634 " phases and the station has " + std::to_string(N) +
635 " servers, which yields " + std::to_string(c.d) +
636 " service configurations. Reduce the phase-type order (e.g. fit the "
637 "service distribution with fewer phases), reduce the number of servers, "
638 "or raise maxBlockSize if the memory cost is acceptable.");
642 std::vector<
Matrix<T>> L(
static_cast<std::size_t
>(N) + 1), Am(
static_cast<std::size_t
>(N) + 1),
643 P(
static_cast<std::size_t
>(N) + 1), Delta(
static_cast<std::size_t
>(N) + 1);
644 for (
int n = 0; n <= N; ++n) {
645 L[
static_cast<std::size_t
>(n)] = detail::retrial_L(c, n);
646 Am[
static_cast<std::size_t
>(n)] = detail::retrial_A(c, n);
647 P[
static_cast<std::size_t
>(n)] = detail::retrial_P(c, n);
648 Delta[
static_cast<std::size_t
>(n)] = detail::retrial_Delta(c, n);
650 std::vector<Matrix<T>> B(
static_cast<std::size_t
>(V)), Bbar(
static_cast<std::size_t
>(V)),
651 Gam(
static_cast<std::size_t
>(V));
652 for (
int nu = 0; nu < V; ++nu) {
653 B[
static_cast<std::size_t
>(nu)] = detail::retrial_B(c, L, Am, P, Delta, nu);
654 Bbar[
static_cast<std::size_t
>(nu)] = detail::retrial_Bbar(c, P, nu);
655 Gam[
static_cast<std::size_t
>(nu)] = detail::retrial_Gamma(c, nu);
657 std::vector<std::vector<Matrix<T>>> Btilde(
658 static_cast<std::size_t
>(V), std::vector<
Matrix<T>>(
static_cast<std::size_t
>(V)));
659 for (
int nu = 0; nu < V; ++nu)
660 for (
int nup = 0; nup < V; ++nup)
661 if (nu != nup) Btilde[
static_cast<std::size_t
>(nu)][
static_cast<std::size_t
>(nup)] =
662 detail::retrial_Btilde(c, P, nu, nup);
664 bool clipped =
false;
670 auto solveAtLevel = [&](std::size_t truncLevel) ->
Matrix<T> {
671 const std::size_t total = (truncLevel + 1) * Vd;
674 for (std::size_t i = 0; i <= truncLevel; ++i) {
678 for (
int nu = 0; nu < V; ++nu) {
679 const std::size_t rs = i * Vd +
static_cast<std::size_t
>(nu) * c.d;
680 for (
int nup = 0; nup < V; ++nup) {
681 const std::size_t cs = i * Vd +
static_cast<std::size_t
>(nup) * c.d;
682 const T d0 = D[0](
static_cast<std::size_t
>(nu),
static_cast<std::size_t
>(nup));
684 for (std::size_t a = 0; a < c.d; ++a)
685 for (std::size_t b = 0; b < c.d; ++b) {
686 T v = B[
static_cast<std::size_t
>(nu)](a, b) +
687 wT * alpha * Gam[
static_cast<std::size_t
>(nu)](a, b);
688 if (a == b) v += d0 - iT * gamma - wT * alpha;
689 Q(rs + a, cs + b) = v;
693 Btilde[
static_cast<std::size_t
>(nu)][
static_cast<std::size_t
>(nup)];
694 for (std::size_t a = 0; a < c.d; ++a)
695 for (std::size_t b = 0; b < c.d; ++b)
696 Q(rs + a, cs + b) = bt(a, b) + (a == b ? d0 : zero);
702 for (
int nu = 0; nu < V; ++nu) {
703 const std::size_t rs = i * Vd +
static_cast<std::size_t
>(nu) * c.d;
704 const std::size_t cs = (i - 1) * Vd +
static_cast<std::size_t
>(nu) * c.d;
705 for (std::size_t a = 0; a < c.d; ++a)
706 for (std::size_t b = 0; b < c.d; ++b) {
707 T v = wT * alpha * Bbar[
static_cast<std::size_t
>(nu)](a, b);
708 if (a == b) v += iT * gamma;
709 Q(rs + a, cs + b) = v;
714 for (
int k = 1; k <= c.K; ++k) {
715 const std::size_t j = i +
static_cast<std::size_t
>(k);
716 if (j > truncLevel)
break;
717 for (
int nu = 0; nu < V; ++nu) {
718 const std::size_t rs = i * Vd +
static_cast<std::size_t
>(nu) * c.d;
719 for (
int nup = 0; nup < V; ++nup) {
720 const std::size_t cs = j * Vd +
static_cast<std::size_t
>(nup) * c.d;
721 const std::size_t ncol = detail::retrial_offset(c, N);
722 for (
int n = 0; n <= N; ++n) {
723 const Matrix<T> Cnk = detail::retrial_C(c, P, n, k, nu, nup);
724 if (Cnk.
rows() == 0)
continue;
725 if (Cnk.
cols() != c.Tn[
static_cast<std::size_t
>(N)])
continue;
726 const std::size_t nrow = detail::retrial_offset(c, n);
727 for (std::size_t a = 0; a < Cnk.
rows(); ++a)
728 for (std::size_t b = 0; b < Cnk.
cols(); ++b)
729 Q(rs + nrow + a, cs + ncol + b) += Cnk(a, b);
737 for (std::size_t i = 0; i < total; ++i) {
739 for (std::size_t j = 0; j < total; ++j) s += Q(i, j);
740 Q(i, i) = Q(i, i) - s;
744 for (std::size_t i = 0; i < total; ++i) Q(i, total - 1) = one;
746 std::vector<T> rhs2(total, zero);
747 rhs2[total - 1] = one;
748 std::vector<T> pi =
solve(QT, rhs2);
754 for (std::size_t i = 0; i < total; ++i) {
755 if (pi[i] < warnTol) clipped =
true;
756 if (pi[i] < zero) pi[i] = zero;
759 if (!(mass > zero))
throw NumericError(
ErrorKind{
"Degenerate"},
"qsys_bmapphnn_retrial: the solution has no mass");
761 for (std::size_t i = 0; i <= truncLevel; ++i)
762 for (std::size_t j = 0; j < Vd; ++j) pim(i, j) = pi[i * Vd + j] / mass;
768 auto orbitTruncationError = [&](
const Matrix<T>& pim, std::size_t truncLevel) -> T {
769 T lorbit = zero, top = zero;
770 for (std::size_t i = 0; i <= truncLevel; ++i) {
772 for (std::size_t j = 0; j < Vd; ++j) lv += pim(i, j);
774 if (i == truncLevel) top = lv;
777 const T denom = lorbit > realmin ? lorbit : realmin;
783 std::size_t truncLevel =
opt.maxLevel;
786 bool converged =
true;
787 if (truncLevel > 0) {
788 pim = solveAtLevel(truncLevel);
789 truncError = orbitTruncationError(pim, truncLevel);
792 const double capped = rho < 0.99 ? rho : 0.99;
793 const double lv = std::ceil(50.0 / (1.0 - capped));
794 truncLevel = lv > 100.0 ?
static_cast<std::size_t
>(lv) :
static_cast<std::size_t
>(100);
797 pim = solveAtLevel(truncLevel);
798 truncError = orbitTruncationError(pim, truncLevel);
803 const std::size_t nextLevel = 2 * truncLevel;
804 if (
static_cast<double>(nextLevel + 1) *
static_cast<double>(Vd) >
opt.maxDim)
break;
805 truncLevel = nextLevel;
817 for (std::size_t i = 1; i <= truncLevel; ++i) {
819 for (std::size_t j = 0; j < Vd; ++j) lv += r.
pi(i, j);
824 for (std::size_t i = 0; i <= truncLevel; ++i)
825 for (
int nu = 0; nu < V; ++nu) {
826 const std::size_t base =
static_cast<std::size_t
>(nu) * c.d;
827 for (
int n = 0; n <= N; ++n) {
828 const std::size_t off = base + detail::retrial_offset(c, n);
829 for (std::size_t t = 0; t < c.Tn[
static_cast<std::size_t
>(n)]; ++t)
837 for (
int nu = 0; nu < V; ++nu) r.
P_empty_system += r.
pi(0,
static_cast<std::size_t
>(nu) * c.d);
839 for (std::size_t j = 0; j < Vd; ++j) r.
topLevelMass += r.
pi(truncLevel, j);
852 const std::vector<T>& beta,
const Matrix<T>& S,
853 int N,
const T& alpha,
const T& gamma,
const T& p,
NumericError(const std::string &what)
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
RetrialPolicy
Retrial admission policy (RetrialPolicy in MATLAB).
@ CONSTANT
the orbit retries at rate alpha whenever it is non-empty
@ LINEAR
each orbiting customer retries at rate alpha (orbit rate i*alpha)
BmapPhNnRetrialResult< T > qsys_bmapphnn_retrial(const std::vector< Matrix< T > > &D, const std::vector< T > &beta, const Matrix< T > &S, int N, const T &alpha, const T &gamma, const T &p, const std::vector< long > &R, const BmapPhNnRetrialOptions &opt)
The BMAP/PH/N/N bufferless retrial queue.
Conservation laws of a layered queueing network, enumerated from its structure.
Matrix< T > inverse(const Matrix< T > &A)
Inverse by LU with one factorization and n back substitutions.
Matrix< T > matmul(const Matrix< T > &A, const Matrix< T > &B)
Matrix product A B.
std::vector< T > mulvec(const Matrix< T > &A, const std::vector< T > &v)
Matrix times column vector, A v.
std::vector< T > solve(const Matrix< T > &A, const std::vector< T > &b)
Convenience: solve Ax = b, leaving A and b untouched.
std::vector< T > ones(std::size_t n)
Column vector of ones, the ubiquitous e in MAP algebra.
Matrix< T > eye(std::size_t n)
Identity of order n.
Number-type abstraction for the templated API port.
The retrial policy shared by the qsys retrial functions (MATLAB RetrialPolicy).
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
static constexpr double FineTol
Options of qsys_bmapphnn_retrial (the name-value pairs of the .m file).
double tolerance
'Tolerance' (parsed by the reference and, like there, not consumed).
std::size_t maxLevel
Fixed orbit truncation level ('MaxLevel'); 0 selects the reference's adaptive choice,...
double maxDim
'MaxDim': cap on the total generator dimension of the refinement.
double tailTolerance
'TailTolerance': relative orbit-truncation error target.
RetrialPolicy retrialPolicy
'RetrialPolicy'.
double maxBlockSize
'MaxBlockSize': cap on the per-level block size V*d; exceeding it is an error.
Result of qsys_bmapphnn_retrial.
bool converged
The adaptive refinement reached tailTolerance (always true for a fixed maxLevel).
T P_empty_orbit
probability that the orbit is empty
T L_orbit
mean number of customers in orbit
std::size_t truncLevel
truncation level used
T utilization
N_server / N.
T throughput
N_server / b1.
T P_idle
probability that every server is idle
T truncError
relative orbit-truncation error estimate at truncLevel
Matrix< T > pi
stationary law, (truncLevel + 1) x (V d)
T topLevelMass
mass at the highest retained level; see defect 2
T P_empty_system
probability that the system is empty
T L_system
L_orbit + N_server.
T N_server
mean number of busy servers
bool clipped
a negative probability had to be clipped to zero