177 "solver_ncld: the load-dependent normalizing-constant analyzer forms "
178 "X = exp(lG(N-1_c) - lG(N)) and needs transcendental arithmetic; this backend has "
184 const std::size_t M =
sn.nstations, K =
sn.nclasses, C =
sn.nchains;
186 std::vector<double> nservers(M, 1.0);
187 std::vector<bool> isFCFS(M,
false);
188 bool anyFCFS =
false, anyMulti =
false;
189 for (std::size_t i = 0; i < M; ++i) {
190 nservers[i] =
sn.stations[i].nservers;
191 isFCFS[i] =
sn.stations[i].sched == SchedStrategy::FCFS;
192 if (isFCFS[i]) anyFCFS =
true;
193 if (std::isfinite(nservers[i]) && nservers[i] > 1.0) anyMulti =
true;
197 bool allClosed =
true;
204 const std::size_t Nt =
static_cast<std::size_t
>(
205 std::max<double>(1.0, std::ceil(std::isfinite(Ntot_d) ? Ntot_d : 1.0)));
215 for (std::size_t i = 0; i < M; ++i)
216 if (
static_cast<bool>(
sn.stations[i].cdscaling) ||
217 static_cast<bool>(
sn.stations[i].jdscaling))
221 for (std::size_t i = 0; i < M; ++i)
222 if (!
sn.stations[i].lldscaling.empty()) anyLld =
true;
230 bool msFolded =
false;
231 if (anyLld && anyMulti && !detail::lld_encodes_multiserver(
sn)) {
232 detail::ncld_fold_servers(
sn);
237 if (!anyLld && M == 2 && allClosed) {
240 for (std::size_t i = 0; i < M; ++i) {
241 std::vector<T> lld(Nt, one);
242 for (std::size_t n = 1; n <= Nt; ++n)
244 std::min<double>(
static_cast<double>(n), nservers[i]));
245 sn.stations[i].lldscaling = lld;
248 }
else if (!msFolded && !detail::lld_encodes_multiserver(
sn)) {
250 "solver_ncld: the load-dependent solver does not support multi-server "
251 "stations unless they are expressed as limited load dependence mu(n) = "
261 std::size_t Wlld = Nt;
262 for (std::size_t i = 0; i < M; ++i)
263 Wlld = std::max(Wlld,
sn.stations[i].lldscaling.size());
265 for (std::size_t i = 0; i < M; ++i) {
266 const std::vector<T>& lld =
sn.stations[i].lldscaling;
267 if (lld.empty())
continue;
268 for (std::size_t n = 0; n < Wlld; ++n) lldscaling(i, n) = n < lld.size() ? lld[n] : lld.back();
275 for (std::size_t i = 0; i < M; ++i)
276 for (std::size_t k = 0; k < K; ++k)
277 if (
sn.disabled[i][k])
281 const std::vector<double> Nchain = detail::chain_population(
sn);
282 std::vector<std::size_t> openChains, closedChains;
283 for (std::size_t c = 0; c < C; ++c)
284 (std::isinf(Nchain[c]) ? openChains : closedChains).push_back(c);
285 std::vector<int> Nnc = detail::nc_population(Nchain);
287 for (std::size_t c : closedChains) Ncl +=
static_cast<std::size_t
>(Nnc[c]);
294 const std::string& pmethod =
opt.method;
303 std::vector<T> gamma(M, zero), nserv_t(M, one);
304 for (std::size_t i = 0; i < M; ++i) nserv_t[i] = num_traits<T>::from_double(nservers[i]);
305 std::vector<T> eta(M, one), eta_1(M, zero);
306 const int iter_max = anyFCFS ?
opt.iter_max : 1;
309 std::string actualmethod =
opt.method;
311 std::vector<T> Xchain(C, zero);
313 while (it < iter_max) {
319 for (std::size_t i = 0; i < M; ++i) {
322 if (!(v <= dev)) dev = v;
324 if (!(dev >
opt.iter_tol))
break;
330 for (std::size_t c = 0; c < C; ++c) {
331 const bool open = std::isinf(Nchain[c]);
332 const std::size_t rst =
sn.classes[
sn.inchain[c][0] - 1].refstat;
333 for (std::size_t i = 0; i < M; ++i) {
335 for (std::size_t k :
sn.inchain[c]) st += ST(i, k - 1) * d.
alpha(i, k - 1);
337 if (open && i + 1 == rst) {
341 for (std::size_t k :
sn.inchain[c])
342 if (!
sn.disabled[i][k - 1] &&
351 for (std::size_t i = 0; i < M; ++i)
352 for (std::size_t c = 0; c < C; ++c) {
359 std::vector<T> lambda(C, zero);
360 for (std::size_t c : openChains) {
361 const std::size_t rst =
sn.classes[
sn.inchain[c][0] - 1].refstat;
362 if (d.
STchain(rst - 1, c) > zero) lambda[c] = T(one / d.
STchain(rst - 1, c));
365 Matrix<T> L(M, C, zero), Z(M, C, zero), mu(M, Nt, one);
366 std::vector<std::size_t> infServers;
367 for (std::size_t i = 0; i < M; ++i) {
368 for (std::size_t c = 0; c < C; ++c) L(i, c) = d.
Lchain(i, c);
369 if (std::isinf(nservers[i])) {
370 infServers.push_back(i);
371 for (std::size_t c = 0; c < C; ++c) Z(i, c) = d.
Lchain(i, c);
372 for (std::size_t n = 1; n <= Nt; ++n)
375 for (std::size_t n = 0; n < Nt; ++n) mu(i, n) = lldscaling(i, n);
380 Xchain.assign(C, zero);
382 if (!openChains.empty()) {
389 std::vector<bool> isSource(M,
false), isDelay(M,
false);
390 for (std::size_t c : openChains)
391 isSource[
sn.classes[
sn.inchain[c][0] - 1].refstat - 1] =
true;
392 for (std::size_t i : infServers)
393 if (!isSource[i]) isDelay[i] =
true;
394 std::vector<std::size_t> queueStations;
395 for (std::size_t i = 0; i < M; ++i)
396 if (!isSource[i] && !isDelay[i]) queueStations.push_back(i);
397 const std::size_t nq = queueStations.size();
400 for (std::size_t i = 0; i < M; ++i)
402 for (std::size_t c = 0; c < C; ++c) Zvec(0, c) += d.
Lchain(i, c);
411 std::size_t ncol = std::max<std::size_t>(1, Ncl);
412 for (std::size_t qi = 0; qi < nq; ++qi)
413 ncol = std::max(ncol, detail::lld_saturation_level(lldscaling, queueStations[qi]));
414 Matrix<T> Dq(nq, C, zero), muq(nq, ncol, one);
415 const std::size_t Wq = lldscaling.
cols();
416 for (std::size_t qi = 0; qi < nq; ++qi) {
417 for (std::size_t c = 0; c < C; ++c) Dq(qi, c) = d.
Lchain(queueStations[qi], c);
418 for (std::size_t n = 0; n < ncol; ++n)
419 muq(qi, n) = lldscaling(queueStations[qi], n < Wq ? n : Wq - 1);
421 std::vector<int> Nmx(C, 0);
422 for (std::size_t c = 0; c < C; ++c)
428 for (std::size_t qi = 0; qi < nq; ++qi)
429 for (std::size_t c = 0; c < C; ++c) Qchain(queueStations[qi], c) = mx.
QN(qi, c);
430 for (std::size_t i = 0; i < M; ++i)
432 for (std::size_t c = 0; c < C; ++c)
433 Qchain(i, c) = T(d.
Lchain(i, c) * Xchain[c]);
434 actualmethod =
"ncldmx";
440 actualmethod = base.
method;
442 const bool repairman = (M == 2 && !infServers.empty());
443 std::size_t firstDelay = infServers.empty() ? 0 : infServers[0];
444 for (std::size_t r = 0; r < C; ++r) {
445 const std::vector<int> Nr = detail::oner(Nnc, r);
446 const double lGr =
pfqn::pfqn_ncld(L, Nr, Zzero, mu, pmethod, atol, nopt).lG;
449 const T qd = T(d.
Lchain(firstDelay, r) * Xchain[r]);
450 Qchain(firstDelay, r) = qd;
451 for (std::size_t i = 0; i < M; ++i)
456 std::size_t nfinite = 0;
457 for (std::size_t i = 0; i < M; ++i)
458 if (std::isfinite(nservers[i])) ++nfinite;
459 for (std::size_t i = 0; i < M; ++i) {
460 if (!(d.
Lchain(i, r) > zero))
continue;
461 if (std::isinf(nservers[i])) {
462 Qchain(i, r) = T(d.
Lchain(i, r) * Xchain[r]);
465 if (i + 1 == M && nfinite == 1) {
469 for (std::size_t i2 : infServers) acc += d.
Lchain(i2, r);
471 for (std::size_t i2 = 0; i2 + 1 < M; ++i2)
472 if (std::isfinite(nservers[i2])) q -= Qchain(i2, r);
473 Qchain(i, r) = q < zero ? zero : q;
481 for (std::size_t n = 0; n < muhati.
cols(); ++n) muhati_row(0, n) = muhati(i, n);
483 Matrix<T> Lhat(M + 1, C, zero), muhat(M + 1, muhati.
cols(), zero);
484 for (std::size_t i2 = 0; i2 < M; ++i2) {
485 for (std::size_t c = 0; c < C; ++c) Lhat(i2, c) = L(i2, c);
486 for (std::size_t n = 0; n < muhati.
cols(); ++n)
487 muhat(i2, n) = muhati(i2, n);
489 for (std::size_t c = 0; c < C; ++c) Lhat(M, c) = L(i, c);
490 for (std::size_t n = 0; n < fnc.
mu.cols(); ++n) muhat(M, n) = fnc.
mu(0, n);
491 Matrix<T> Lms_i(M - 1, C, zero), mu_i(M - 1, mu.
cols(), zero);
493 for (std::size_t i2 = 0; i2 < M; ++i2) {
494 if (i2 == i)
continue;
495 for (std::size_t c = 0; c < C; ++c) Lms_i(row, c) = L(i2, c);
496 for (std::size_t n = 0; n < mu.
cols(); ++n) mu_i(row, n) = mu(i2, n);
499 const double lGhat_fnci =
501 const double lGhatir =
505 const double dlGa = lGhat_fnci - lGhatir;
506 const double dlG_i = lGr_i - lGhatir;
513 Xchain[r] * (one + CQ));
518 Matrix<T> Rchain(M, C, zero), Tchain(M, C, zero);
519 for (std::size_t i = 0; i < M; ++i)
520 for (std::size_t c = 0; c < C; ++c) {
521 if (Xchain[c] != zero && d.
Vchain(i, c) != zero)
522 Rchain(i, c) = T(Qchain(i, c) / Xchain[c] / d.
Vchain(i, c));
523 Tchain(i, c) = T(Xchain[c] * d.
Vchain(i, c));
525 for (std::size_t i : infServers)
526 for (std::size_t c = 0; c < C; ++c)
529 for (std::size_t c = 0; c < C; ++c) {
530 if (Nnc[c] != 0)
continue;
532 for (std::size_t i = 0; i < M; ++i) {
538 for (std::size_t i = 0; i < M; ++i)
539 for (std::size_t c = 0; c < C; ++c) {
556 auto capapx = nserv_t;
557 for (std::size_t i = 0; i < M; ++i) {
558 if (!(std::isfinite(nservers[i]) && nservers[i] <= 1.0))
continue;
560 for (std::size_t n = 0; n < lldscaling.
cols(); ++n)
562 if (!(std::isfinite(pk) && pk > 0.0))
continue;
564 for (std::size_t k = 0; k < Uapx.
cols(); ++k) Uapx(i, k) = T(Uapx(i, k) / pkT);
568 opt.highvar, isFCFS,
sn.rates, ST0, V, SCVnan, cls.
Tp, Uapx, gamma, capapx);
575 std::vector<T> X = cls.
X;
577 for (std::size_t i = 0; i < A.rows(); ++i)
578 for (std::size_t j = 0; j < A.cols(); ++j)
579 if (A(i, j) < zero) A(i, j) = T(-A(i, j));
585 if (x < zero) x = T(-x);
590 for (std::size_t i = 0; i < M; ++i) {
591 const bool multi = std::isfinite(nservers[i]) && nservers[i] > 1.0;
592 if (multi || std::isinf(nservers[i])) {
593 const double div = multi ? nservers[i] : 1.0;
594 for (std::size_t k = 0; k < K; ++k) {
596 for (std::size_t cc = 0; cc < C; ++cc)
597 if (
sn.chains[cc][k]) c = cc;
598 if (c == C)
continue;
599 const std::size_t rs =
sn.classes[k].refstat;
600 const T vref =
sn.visits[c](
sn.stateful_of_station(rs) - 1, k);
601 if (vref == zero)
continue;
602 const T vi =
sn.visits[c](
sn.stateful_of_station(i + 1) - 1, k);
603 const bool open = std::isinf(
sn.classes[k].population);
607 const std::size_t src =
sn.classes[k].refstat;
608 if (!
sn.disabled[src - 1][k]) rate =
sn.rates(src - 1, k);
612 if (!(rate > zero))
continue;
626 const std::vector<T>& lldrow =
sn.stations[i].lldscaling;
627 if (lldrow.empty()) {
628 for (std::size_t n = 0; n < Nt; ++n)
629 if (lldscaling(i, n) > mx) mx = lldscaling(i, n);
631 for (std::size_t n = 0; n < lldrow.size(); ++n)
632 if (lldrow[n] > mx) mx = lldrow[n];
635 for (std::size_t k = 0; k < K; ++k) U(i, k) = T(U(i, k) / mx);
637 for (std::size_t k = 0; k < K; ++k) s += U(i, k);
639 for (std::size_t k = 0; k < K; ++k) U(i, k) = T(U(i, k) / s);
643 auto clear_nonfinite = [&](
Matrix<T>& A) {
644 for (std::size_t i = 0; i < A.rows(); ++i)
645 for (std::size_t j = 0; j < A.cols(); ++j)
654 for (std::size_t c = 0; c < C; ++c) {
655 if (std::isinf(Nchain[c]))
continue;
657 for (std::size_t k :
sn.inchain[c])
658 for (std::size_t i = 0; i < M; ++i) qden += Q(i, k - 1);
661 for (std::size_t k :
sn.inchain[c]) {
662 X[k - 1] = T(ratio * X[k - 1]);
663 for (std::size_t i = 0; i < M; ++i) {
664 Q(i, k - 1) = T(ratio * Q(i, k - 1));
665 Tp(i, k - 1) = T(ratio * Tp(i, k - 1));
666 U(i, k - 1) = T(ratio * U(i, k - 1));
667 R(i, k - 1) = Tp(i, k - 1) == zero ? zero : T(Q(i, k - 1) / Tp(i, k - 1));
677 out.
sol.C.assign(K, zero);
678 for (std::size_t k = 0; k < K; ++k) {
679 const double njobs =
sn.classes[k].population;
680 out.
sol.C[k] = (std::isfinite(njobs) && X[k] != zero)
686 out.
sol.method = actualmethod;