5#ifndef LINE_API_NPFQN_SQD_H
6#define LINE_API_NPFQN_SQD_H
131bool sqd_is_inf(
const T& v) {
137T sqd_mm1k_blocking(
const T& K,
const T& rho) {
142 return T(one / T(K + one));
143 return T(T(one - rho) * pow(rho, K) / T(one - pow(rho, T(K + one))));
148void sqd_beta_gamma(
const T& K,
const T& pBlockDown,
int mode,
const T& calEps, T& beta,
152 const T one = num_traits<T>::from_int(1);
153 const T a = num_traits<T>::from_int(2);
154 if (K <= a || sqd_is_inf(K)) {
163 const T lo = num_traits<T>::from_double(1e-6);
164 const T hi = T(one - lo);
165 if (pK > hi) pK = hi;
166 if (pK < lo) pK = lo;
167 Va = T(one - pK * T(a - one) / T(b - one));
169 }
else if (mode == 1) {
170 Va = T(T(b - a) / b);
177 const T va_hi = num_traits<T>::from_double(0.999);
178 const T va_lo = num_traits<T>::from_double(0.01);
179 const T vb_lo = num_traits<T>::from_double(1e-6);
180 if (Va > va_hi) Va = va_hi;
181 if (Va < va_lo) Va = va_lo;
182 if (Vb < vb_lo) Vb = vb_lo;
188 const T lnVa = log(Va);
189 const T lnVb = log(Vb);
190 gamma = T(log(T(lnVa / lnVb)) / log(T(T(a - one) / T(b - one))));
191 const T g_lo = num_traits<T>::from_double(0.5);
192 const T g_hi = num_traits<T>::from_int(10);
193 if (gamma < g_lo) gamma = g_lo;
194 if (gamma > g_hi) gamma = g_hi;
195 beta = T(T(a - one) / pow(T(-lnVa), T(one / gamma)));
196 if (!std::isfinite(num_traits<T>::to_double(gamma)) ||
197 !std::isfinite(num_traits<T>::to_double(beta)) ||
198 beta <= num_traits<T>::from_int(0)) {
210Matrix<T> sqd_effective_routing(
const Matrix<T>& rt,
211 const std::vector<std::size_t>& stationToStateful,
212 std::size_t nclasses,
const std::vector<bool>& isDelay) {
213 const std::size_t M = isDelay.size();
214 const T
zero = num_traits<T>::from_int(0);
215 Matrix<T> p(M, M, zero);
216 const auto rtat = [&](std::size_t i, std::size_t j) {
218 const std::size_t ri = (stationToStateful[i] - 1) * nclasses;
219 const std::size_t rj = (stationToStateful[j] - 1) * nclasses;
220 if (ri >= rt.rows() || rj >= rt.cols())
221 throw InputError(
"npfqn_sqd: stationToStateful indexes outside the routing matrix");
224 for (std::size_t i = 0; i < M; ++i) {
225 if (isDelay[i])
continue;
226 for (std::size_t j = 0; j < M; ++j) {
227 const T p_ij = rtat(i, j);
228 if (p_ij <= zero)
continue;
232 for (std::size_t k = 0; k < M; ++k) {
233 if (isDelay[k])
continue;
234 const T p_jk = rtat(j, k);
235 if (p_jk > zero) p(i, k) += p_ij * p_jk;
249SqdResult<T> sqd_recursion(
const std::vector<T>& ST,
const std::vector<T>& V,
250 const std::vector<T>& cap,
const std::vector<bool>& isDelay,
251 const Matrix<T>& pEff,
int N,
const SqdOptions<T>& opt) {
254 const std::size_t M = ST.size();
255 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
256 const T INITIAL_V1 = num_traits<T>::from_double(692.192);
257 const T CALIBRATION_EPSILON = num_traits<T>::from_double(0.05);
258 const T tiny = num_traits<T>::from_double(1e-10);
259 const T eps15 = num_traits<T>::from_double(1e-15);
261 std::vector<T> V1(M, zero), v1init(M, zero), L_buf(M, zero), L_svr(M, zero);
262 for (std::size_t i = 0; i < M; ++i) {
263 v1init[i] = opt.initialV1.empty() ? INITIAL_V1 : opt.initialV1[i];
267 std::vector<T> W_buf(M, zero), W_svr(M, zero);
269 for (
int pop = 1; pop <= N; ++pop) {
271 for (std::size_t i = 0; i < M; ++i) {
278 if (opt.ownServerNeighbor) {
279 if (!detail::sqd_is_inf(cap[i]))
280 pBlockDown = detail::sqd_mm1k_blocking(cap[i], T(X * V[i] * ST[i]));
282 for (std::size_t j = 0; j < M; ++j)
283 if (!isDelay[j] && pEff(i, j) > zero && !detail::sqd_is_inf(cap[j]))
284 pBlockDown += pEff(i, j) *
285 detail::sqd_mm1k_blocking(cap[j], T(X * V[j] * ST[j]));
289 if (opt.ownServerNeighbor) {
292 for (std::size_t j = 0; j < M; ++j) n += pEff(i, j) * L_svr[j];
300 detail::sqd_beta_gamma(cap[i], pBlockDown, opt.calibrationMode,
301 CALIBRATION_EPSILON, beta, gamma);
302 T base = T(T(n - one) / beta);
303 if (base < zero) base =
zero;
304 const T expArg = pow(base, gamma);
305 mu_n = T(n * V1[i] * exp(T(-expArg)));
307 if (mu_n < tiny) mu_n = tiny;
309 W_buf[i] = T(one / mu_n * T(one + n));
310 W_svr[i] = T(ST[i] * T(one + L_svr[i]));
312 if (opt.serverBlockingTime) {
314 for (std::size_t j = 0; j < M; ++j) {
315 if (isDelay[j] || !(pEff(i, j) > zero) || detail::sqd_is_inf(cap[j]))
continue;
316 const T pBj = detail::sqd_mm1k_blocking(cap[j], T(X * V[j] * ST[j]));
317 const T denom = T(ST[i] + ST[j]);
319 if (denom > eps15) theta = T(ST[j] / denom);
320 bt += pEff(i, j) * pBj * ST[j] * theta;
328 for (std::size_t i = 0; i < M; ++i) sumVW += V[i] * T(W_buf[i] + W_svr[i]);
329 X = (sumVW > eps15) ? T(num_traits<T>::from_int(pop) / sumVW) :
zero;
332 for (std::size_t i = 0; i < M; ++i) {
333 L_buf[i] = X * V[i] * W_buf[i];
334 L_svr[i] = X * V[i] * W_svr[i];
339 for (std::size_t i = 0; i < M; ++i) {
340 if (isDelay[i])
continue;
342 if (opt.ownServerNeighbor) {
343 if (!detail::sqd_is_inf(cap[i]))
344 pBlock = detail::sqd_mm1k_blocking(cap[i], T(X * V[i] * ST[i]));
346 for (std::size_t j = 0; j < M; ++j)
347 if (!isDelay[j] && pEff(i, j) > zero && !detail::sqd_is_inf(cap[j]))
348 pBlock += pEff(i, j) *
349 detail::sqd_mm1k_blocking(cap[j], T(X * V[j] * ST[j]));
351 V1[i] = opt.freshV1 ? T(v1init[i] * T(one - pBlock))
352 : T(V1[i] * T(one - pBlock));
353 if (V1[i] < tiny) V1[i] = tiny;
359 res.X.assign(M, zero);
360 res.Q.assign(M, zero);
361 res.U.assign(M, zero);
362 res.R.assign(M, zero);
363 for (std::size_t i = 0; i < M; ++i) {
364 const T T_i = X * V[i];
365 const T Q_i = T(L_buf[i] + L_svr[i]);
366 const T W_tot = T(W_buf[i] + W_svr[i]);
369 res.R[i] = (T_i > eps15) ? T(Q_i / T_i) : W_tot;
370 const T u = T(T_i * ST[i]);
371 res.U[i] = (u < one) ? u : one;
381bool sqd_cyclic_tandem(
const Matrix<T>& pEff,
const std::vector<bool>& isDelay,
382 const std::vector<T>& cap, std::vector<std::size_t>& pred) {
383 const std::size_t M = isDelay.size();
385 if (M < 2)
return false;
386 for (std::size_t i = 0; i < M; ++i)
387 if (isDelay[i] || sqd_is_inf(cap[i]))
return false;
388 const T one = num_traits<T>::from_int(1);
389 const T tol = num_traits<T>::from_double(1e-9);
390 std::vector<std::size_t> succ(M, 0);
391 for (std::size_t i = 0; i < M; ++i) {
392 std::size_t hits = 0;
393 T rowsum = num_traits<T>::from_int(0);
394 for (std::size_t j = 0; j < M; ++j) {
395 rowsum += pEff(i, j);
396 if (pEff(i, j) > T(one - tol)) {
401 const T dev = T(rowsum - one);
402 if (hits != 1 || succ[i] == i || dev > tol || dev < T(-tol))
return false;
404 std::vector<bool> seen(M,
false);
406 for (std::size_t step = 0; step < M; ++step) {
407 if (seen[i])
return false;
433 const std::vector<T>& cap,
const std::vector<bool>& isDelay,
434 const Matrix<T>& rt,
const std::vector<std::size_t>& stationToStateful,
437 "npfqn_sqd requires transcendental arithmetic (the effective-rate "
438 "calibration uses exp, log and real powers)");
439 const std::size_t M = ST.size();
440 if (M == 0)
throw InputError(
"npfqn_sqd: empty station list");
441 if (V.size() != M || cap.size() != M || isDelay.size() != M ||
442 stationToStateful.size() != M)
443 throw InputError(
"npfqn_sqd: the per-station inputs disagree on the station count");
444 if (nclasses == 0)
throw InputError(
"npfqn_sqd: the class count must be positive");
445 if (N < 0)
throw InputError(
"npfqn_sqd: the population must be nonnegative");
446 if (!
opt.initialV1.empty() &&
opt.initialV1.size() != M)
447 throw InputError(
"npfqn_sqd: initialV1 must have one entry per station");
450 detail::sqd_effective_routing(rt, stationToStateful, nclasses, isDelay);
454 std::vector<std::size_t> pred;
455 if (detail::sqd_cyclic_tandem(pEff, isDelay, cap, pred)) {
457 for (std::size_t i = 0; i < M; ++i) capsum += cap[i];
460 const long Ndual = C -
static_cast<long>(N);
461 if (2L * N > C && Ndual >= 1) {
464 std::vector<T> STdual(M);
466 for (std::size_t j = 0; j < M; ++j) {
467 for (std::size_t k = 0; k < M; ++k) pDual(j, k) = pEff(k, j);
468 STdual[j] = ST[pred[j]];
469 if (!
opt.initialV1.empty()) optDual.
initialV1[j] =
opt.initialV1[pred[j]];
471 const SqdResult<T> d = detail::sqd_recursion(STdual, V, cap, isDelay, pDual,
472 static_cast<int>(Ndual), optDual);
475 res.
Q.assign(M, zero);
476 res.
U.assign(M, zero);
477 res.
R.assign(M, zero);
479 for (std::size_t i = 0; i < M; ++i) {
480 res.
Q[i] = T(T(cap[i] + one) - d.
Q[i]);
481 const T u = T(res.
X[i] * ST[i]);
482 res.
U[i] = (u < one) ? u : one;
483 res.
R[i] = (res.
X[i] > eps15) ? T(res.
Q[i] / res.
X[i]) : zero;
489 return detail::sqd_recursion(ST, V, cap, isDelay, pEff, N,
opt);
499 const std::vector<T>& cap,
const std::vector<bool>& isDelay,
500 const Matrix<T>& rt,
const std::vector<std::size_t>& stationToStateful,
501 std::size_t nclasses,
int N) {
The exception types the port throws.
Dense matrix and non-owning view.
SqdResult< T > npfqn_sqd(const std::vector< T > &ST, const std::vector< T > &V, const std::vector< T > &cap, const std::vector< bool > &isDelay, const Matrix< T > &rt, const std::vector< std::size_t > &stationToStateful, std::size_t nclasses, int N, const SqdOptions< T > &opt)
Smith Queue Decomposition (SQD): approximate MVA for closed networks under Blocking-After-Service (ma...
Conservation laws of a layered queueing network, enumerated from its structure.
Number-type abstraction for the templated API port.
The four option switches of the reference, with its defaults.
bool ownServerNeighbor
true = MATLAB neighborMode 'ownserver', false = 'downstream'
int calibrationMode
0 = base (beta = K, gamma = 1), 1 = fixed heuristic, 2 = blocking-aware
bool freshV1
true = MATLAB v1Policy 'fresh', false = 'compound'
bool serverBlockingTime
include the server-blocking-time term in W^svr (MATLAB serverBlockingTime)
std::vector< T > initialV1
per-station initial V1; empty selects INITIAL_V1 for every station
Return value of npfqn_sqd, mirroring [X, Q, U, R].
std::vector< T > R
(M) per-station residence time
std::vector< T > Q
(M) per-station queue length
std::vector< T > U
(M) per-station utilization, capped at one
std::vector< T > X
(M) per-station throughput