5#ifndef LINE_API_PFQN_PFQN_OI_MIXED_H
6#define LINE_API_PFQN_PFQN_OI_MIXED_H
99inline std::vector<int> oi_mixed_sub(std::size_t li,
const std::vector<std::size_t>& dims) {
100 std::vector<int> m(dims.size(), 0);
101 for (std::size_t j = 0; j < dims.size(); ++j) {
102 m[j] =
static_cast<int>(li % dims[j]);
109inline double oi_mixed_box(
const std::vector<std::size_t>& dims) {
111 for (std::size_t d : dims) p *=
static_cast<double>(d);
123std::vector<int> oi_mixed_cutoffs(
const std::function<T(
const std::vector<int>&)>& fAt,
124 const std::vector<bool>& openA,
const std::vector<int>& capA,
125 int taumax,
double maxstates,
bool& bounded) {
126 const std::size_t K = openA.size();
127 const double tolr = 1e-9;
128 std::vector<int> tau(K, 0);
129 bool any_open =
false;
130 for (std::size_t j = 0; j < K; ++j)
136 const auto box_of = [&](
const std::vector<int>& t) {
138 for (std::size_t j = 0; j < K; ++j)
139 p *= openA[j] ?
static_cast<double>(t[j] + 1) :
static_cast<double>(capA[j] + 1);
142 bool changed = any_open;
145 for (std::size_t r = 0; r < K; ++r) {
146 if (!openA[r])
continue;
147 std::vector<std::size_t> lim(K, 1);
148 for (std::size_t j = 0; j < K; ++j)
149 lim[j] = openA[j] ?
static_cast<std::size_t
>(tau[j] + 1)
150 :
static_cast<std::size_t
>(capA[j] + 1);
152 std::size_t total = 1;
153 for (std::size_t j = 0; j < K; ++j) total *= lim[j];
155 for (std::size_t li = 0; li < total && sat; ++li) {
156 std::vector<int> m(K, 0);
157 std::size_t rem = li;
158 for (std::size_t j = 0; j < K; ++j) {
162 m[j] =
static_cast<int>(rem % lim[j]);
166 std::vector<int> mp = m;
168 const double a = num_traits<T>::to_double(fAt(m));
169 const double b = num_traits<T>::to_double(fAt(mp));
170 if (std::fabs(b - a) > tolr * std::max(1.0, std::fabs(a))) sat =
false;
175 if (tau[r] > taumax || box_of(tau) > maxstates) {
188struct OiMacroTables {
200OiMacroTables<T> oi_mixed_macro(
const std::vector<T>& lam,
201 const std::function<T(
const std::vector<int>&)>& mu,
202 const std::vector<int>& tau,
const std::vector<bool>& openA,
203 const std::vector<int>& capA) {
204 const T
zero = num_traits<T>::from_int(0);
205 const std::size_t R = lam.size();
206 std::vector<std::size_t> dims(R), strides(R, 1);
207 for (std::size_t r = 0; r < R; ++r)
208 dims[r] = openA[r] ?
static_cast<std::size_t
>(tau[r] + 1)
209 :
static_cast<std::size_t
>(capA[r] + 1);
210 for (std::size_t r = 1; r < R; ++r) strides[r] = strides[r - 1] * dims[r - 1];
211 std::size_t ngrid = 1;
212 for (std::size_t d : dims) ngrid *= d;
213 std::vector<std::vector<int>> counts(ngrid);
214 std::vector<int>
tot(ngrid, 0);
215 for (std::size_t k = 0; k < ngrid; ++k) {
216 counts[k] = oi_mixed_sub(k, dims);
217 for (
int v : counts[k])
tot[k] += v;
219 std::vector<std::size_t> ord(ngrid);
220 for (std::size_t k = 0; k < ngrid; ++k) ord[k] = k;
221 std::stable_sort(ord.begin(), ord.end(),
222 [&tot](std::size_t a, std::size_t b) { return tot[a] < tot[b]; });
224 OiMacroTables<T> out;
225 out.G.assign(ngrid, zero);
228 std::vector<T> muv(ngrid, zero);
229 out.G[0] = num_traits<T>::from_int(1);
230 for (std::size_t kk = 1; kk < ngrid; ++kk) {
231 const std::size_t k = ord[kk];
232 const std::vector<int>& m = counts[k];
234 std::vector<bool> satm(R,
false);
237 for (std::size_t r = 0; r < R; ++r) {
238 if (m[r] <= 0)
continue;
239 satm[r] = openA[r] && (m[r] == tau[r]);
240 if (satm[r]) lamsat += lam[r];
241 numG += T(lam[r] * out.G[k - strides[r]]);
243 const T den = T(muv[k] - lamsat);
244 if (!(numG > zero))
continue;
245 const double dden = num_traits<T>::to_double(den);
246 if (dden <= 0.0 || !std::isfinite(dden)) {
248 for (std::size_t r = 0; r < R; ++r)
249 if (satm[r]) anysat =
true;
256 out.G[k] = T(numG / den);
257 const double dmuk = num_traits<T>::to_double(muv[k]);
258 for (std::size_t j = 0; j < R; ++j) {
260 for (std::size_t r = 0; r < R; ++r) {
261 if (m[r] <= 0)
continue;
262 hj += T(lam[r] * out.H(k - strides[r], j));
263 sj += T(lam[r] * out.S(k - strides[r], j));
266 const std::size_t kj = k - strides[j];
267 if (satm[j]) hj += T(lam[j] * out.G[k]);
268 hj += T(lam[j] * out.G[kj]);
269 if (dmuk - num_traits<T>::to_double(muv[kj]) > 1e-12 * std::max(1.0, std::fabs(dmuk)))
270 sj += T(lam[j] * out.G[kj]);
272 out.H(k, j) = T(hj / den);
273 out.S(k, j) = T(sj / den);
284void oi_mixed_marginalize(
const OiMacroTables<T>& mt,
const std::vector<int>& tau,
285 const std::vector<bool>& openA,
const std::vector<int>& capA,
286 const std::vector<std::size_t>& act,
287 const std::vector<std::size_t>& closedIdx,
288 const std::vector<std::size_t>& dimsC, std::size_t R,
289 OiMixedResult<T>& res) {
290 const T
zero = num_traits<T>::from_int(0);
291 const std::size_t Ra = tau.size();
292 std::vector<std::size_t> dims(Ra);
293 for (std::size_t a = 0; a < Ra; ++a)
294 dims[a] = openA[a] ?
static_cast<std::size_t
>(tau[a] + 1)
295 :
static_cast<std::size_t
>(capA[a] + 1);
296 std::size_t ngrid = 1;
297 for (std::size_t d : dims) ngrid *= d;
299 for (std::size_t d : dimsC) ncl *= d;
300 std::vector<std::size_t> stridesC(dimsC.size(), 1);
301 for (std::size_t a = 1; a < dimsC.size(); ++a) stridesC[a] = stridesC[a - 1] * dimsC[a - 1];
302 res.W.assign(ncl, zero);
305 std::vector<std::size_t> posC(Ra, 0);
306 for (std::size_t a = 0; a < Ra; ++a)
308 for (std::size_t q = 0; q < closedIdx.size(); ++q)
309 if (closedIdx[q] == act[a]) posC[a] = q;
310 for (std::size_t k = 0; k < ngrid; ++k) {
311 bool any = !(mt.G[k] ==
zero);
312 for (std::size_t a = 0; a < Ra && !any; ++a)
313 if (!(mt.H(k, a) == zero) || !(mt.S(k, a) == zero)) any =
true;
315 std::size_t li = k, lc = 0;
316 for (std::size_t a = 0; a < Ra; ++a) {
317 const std::size_t ma = li % dims[a];
319 if (!openA[a]) lc += ma * stridesC[posC[a]];
321 res.W[lc] += mt.G[k];
322 for (std::size_t a = 0; a < Ra; ++a) {
323 res.H(lc, act[a]) += mt.H(k, a);
324 res.S(lc, act[a]) += mt.S(k, a);
341 const std::function<T(
const std::vector<int>&)>& mu,
345 "pfqn_oi_mixed: mu must be a function mu(n) on the per-class count vector.");
346 const std::size_t R = lambda.size();
349 "pfqn_oi_mixed: lambda and N must have the same number of classes.");
350 for (std::size_t r = 0; r < R; ++r) {
352 if (!std::isfinite(l) || l < 0.0)
354 "pfqn_oi_mixed: lambda must be finite and nonnegative.");
357 std::vector<bool> isOpen(R,
false);
358 std::vector<int> Nc(R, 0);
359 std::vector<std::size_t> closedIdx, dimsC;
360 for (std::size_t r = 0; r < R; ++r) {
361 isOpen[r] = std::isinf(N[r]);
363 Nc[r] =
static_cast<int>(std::llround(N[r]));
364 closedIdx.push_back(r);
365 dimsC.push_back(
static_cast<std::size_t
>(Nc[r]) + 1);
369 for (std::size_t d : dimsC) ncl *= d;
372 std::vector<std::size_t> act;
373 for (std::size_t r = 0; r < R; ++r)
374 if (lambda[r] > zero && (isOpen[r] || Nc[r] > 0)) act.push_back(r);
377 res.
W.assign(ncl, zero);
380 res.
tau.assign(R, 0);
385 const std::size_t Ra = act.size();
386 std::vector<T> lamA(Ra);
387 std::vector<bool> openA(Ra);
388 std::vector<int> capA(Ra);
389 for (std::size_t a = 0; a < Ra; ++a) {
390 lamA[a] = lambda[act[a]];
391 openA[a] = isOpen[act[a]];
392 capA[a] = Nc[act[a]];
396 const std::function<T(
const std::vector<int>&)> muA = [mu, act, R](
const std::vector<int>& na) {
397 std::vector<int> n(R, 0);
398 for (std::size_t a = 0; a < act.size(); ++a) n[act[a]] = na[a];
406 std::vector<int> tauA = detail::oi_mixed_cutoffs<T>(muA, openA, capA,
opt.taumax,
407 opt.maxstates, bounded);
411 for (std::size_t a = 0; a < Ra; ++a)
412 if (openA[a]) Tc = std::max(Tc, tauA[a]);
413 bool havePrev =
false;
414 std::vector<T> prevW;
416 std::vector<int> tauT;
419 std::vector<std::size_t> dimsT(Ra);
420 for (std::size_t a = 0; a < Ra; ++a) {
421 if (openA[a]) tauT[a] = Tc;
422 dimsT[a] = openA[a] ?
static_cast<std::size_t
>(Tc + 1)
423 :
static_cast<std::size_t
>(capA[a] + 1);
425 if (detail::oi_mixed_box(dimsT) >
opt.maxstates) {
426 std::ostringstream os;
427 os <<
"pfqn_oi_mixed: the order-independent rate does not saturate in the open "
428 "classes and the truncated macrostate box (cutoff "
429 << Tc <<
") exceeds maxstates = " <<
opt.maxstates
430 <<
" before the tables settled to tol = " <<
opt.tol
431 <<
"; give the station a saturating rate.";
434 const std::vector<int> tauF = tauT;
435 const std::vector<bool> openF = openA;
436 const std::function<T(
const std::vector<int>&)> muT =
437 [muA, tauF, openF](
const std::vector<int>& na) {
438 std::vector<int> n = na;
439 for (std::size_t a = 0; a < n.size(); ++a)
440 if (openF[a]) n[a] = std::min(n[a], tauF[a]);
443 const detail::OiMacroTables<T> mt = detail::oi_mixed_macro<T>(lamA, muT, tauT, openA, capA);
447 detail::oi_mixed_marginalize<T>(mt, tauT, openA, capA, act, closedIdx, dimsC, R, cur);
449 double dW = 0.0, dH = 0.0;
450 for (std::size_t i = 0; i < ncl; ++i) {
453 dW = std::max(dW, std::fabs(wn - wp) / std::max(std::fabs(wn), 1e-300));
454 for (std::size_t r = 0; r < R; ++r) {
457 dH = std::max(dH, std::fabs(hn - hp) / std::max(std::fabs(hn), 1e-300));
460 if (dW <
opt.tol && dH <
opt.tol) {
475 const detail::OiMacroTables<T> mt = detail::oi_mixed_macro<T>(lamA, muA, tauA, openA, capA);
477 if (mt.stable) detail::oi_mixed_marginalize<T>(mt, tauA, openA, capA, act, closedIdx, dimsC, R, res);
480 res.
W.assign(ncl, zero);
484 for (std::size_t a = 0; a < Ra; ++a)
485 if (openA[a]) res.
tau[act[a]] = tauA[a];
NumericError(const std::string &what)
The exception types the port throws.
Dense matrix and non-owning view.
double tot(const std::vector< double > &pops)
OiMixedResult< T > pfqn_oi_mixed(const std::vector< T > &lambda, const std::vector< double > &N, const std::function< T(const std::vector< int > &)> &mu, const OiMixedOptions &opt=OiMixedOptions())
Closed-lattice station tables of an OI station with open classes summed out.
Conservation laws of a layered queueing network, enumerated from its structure.
Number-type abstraction for the templated API port.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
The tuning fields of pfqn_oi_mixed / pfqn_oi_open (OPTIONS in the reference).
double maxstates
macrostate budget of the cutoff search and 'oi.trunc'
int taumax
per-class cutoff search limit
double tol
relative tolerance of the 'oi.trunc' branch
Return value of pfqn_oi_mixed, mirroring [W, H, S, stable, tau, method].
std::vector< int > tau
(R) open cutoffs used (0 for closed and idle classes)
Matrix< T > S
(prod(Nc+1) x R) per-class in-service weights
std::vector< T > W
(prod(Nc+1)) column-major table over the closed lattice
std::string method
"oi" (exact) or "oi.trunc"
bool stable
false when some reachable macrostate has den(m) <= 0
Matrix< T > H
(prod(Nc+1) x R) per-class mean weights