5#ifndef LINE_API_MAM_MMAPPH1FCFS_H
6#define LINE_API_MAM_MMAPPH1FCFS_H
77namespace mmapph1_detail {
89 std::vector<Matrix<T>> Dk;
90 std::vector<std::size_t> Nsk;
91 std::size_t N = 0, Ns = 0;
97Matrix<T> blkdiag(
const std::vector<PhService<T>>& svc, std::size_t Ns) {
101 for (
const PhService<T>& s : svc) {
102 for (std::size_t i = 0; i < s.S.rows(); ++i)
103 for (std::size_t j = 0; j < s.S.cols(); ++j) Sa(off + i, off + j) = s.S(i, j);
121std::vector<T> ph_equilibrium(
const PhService<T>& s) {
122 const T
zero = num_traits<T>::from_int(0);
123 const std::size_t n = s.S.rows();
124 Matrix<T> G(n, n, zero);
125 for (std::size_t i = 0; i < n; ++i) {
127 for (std::size_t j = 0; j < n; ++j) rowsum += s.S(i, j);
128 for (std::size_t j = 0; j < n; ++j) G(i, j) = s.S(i, j) - rowsum * s.sigma[j];
135AgeProcess<T> build(
const Mmap<T>& arrival,
const std::vector<PhService<T>>& svc) {
136 static_assert(num_traits<T>::has_transcendental,
137 "mmapph1fcfs runs the ADDA doubling iteration of mfq_fundamental");
138 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
139 const std::size_t K = svc.size();
140 if (arrival.classes() != K)
141 throw InputError(
"mmapph1fcfs: the arrival MMAP and the service list disagree on the "
142 "number of classes");
144 a.N = arrival.order();
148 for (std::size_t k = 0; k < K; ++k)
149 for (std::size_t i = 0; i < a.N; ++i)
150 for (std::size_t j = 0; j < a.N; ++j) a.Da(i, j) += a.Dk[k](i, j);
152 Matrix<T> Q(a.N, a.N, zero);
153 for (std::size_t i = 0; i < a.N; ++i)
154 for (std::size_t j = 0; j < a.N; ++j) Q(i, j) = a.D0(i, j) + a.Da(i, j);
159 for (std::size_t k = 0; k < K; ++k) {
160 a.Nsk[k] = svc[k].S.rows();
163 std::vector<T> lambda(K, zero), mu(K, zero);
164 std::vector<std::vector<T>> beta(K);
166 for (std::size_t k = 0; k < K; ++k) {
167 const std::vector<T> td =
vecmul(theta, a.Dk[k]);
168 for (
const T& v : td) lambda[k] += v;
169 beta[k] = ph_equilibrium(svc[k]);
170 for (std::size_t i = 0; i < a.Nsk[k]; ++i)
171 for (std::size_t j = 0; j < a.Nsk[k]; ++j) mu[k] -= beta[k][i] * svc[k].S(i, j);
172 if (!(mu[k] > zero))
throw NumericError(ErrorKind{
"Degenerate"},
"mmapph1fcfs: a service law has zero rate");
173 a.rho += lambda[k] / mu[k];
176 const Matrix<T> Sa = blkdiag(svc, a.Ns);
177 const Matrix<T> Ia =
eye<T>(a.N);
182 std::vector<std::vector<T>> sa(K, std::vector<T>(a.Ns, zero));
183 std::vector<std::vector<T>> ba(K, std::vector<T>(a.Ns, zero));
185 for (std::size_t k = 0; k < K; ++k) {
186 for (std::size_t i = 0; i < a.Nsk[k]; ++i) {
187 sa[k][off + i] = svc[k].sigma[i];
188 ba[k][off + i] = beta[k][i];
194 const Matrix<T> Fpp =
kron(Ia, Sa);
195 Matrix<T> sexit(a.Ns, 1, zero);
196 for (std::size_t i = 0; i < a.Ns; ++i) {
198 for (std::size_t j = 0; j < a.Ns; ++j) s += Sa(i, j);
201 const Matrix<T> Fpm =
kron(Ia, sexit);
202 Matrix<T> iVec(a.N, a.N * a.Ns, zero);
203 for (std::size_t k = 0; k < K; ++k) {
204 Matrix<T> row(1, a.Ns, zero);
205 for (std::size_t i = 0; i < a.Ns; ++i) row(0, i) = sa[k][i];
206 const Matrix<T> blk =
kron(a.Dk[k], row);
207 for (std::size_t i = 0; i < iVec.rows(); ++i)
208 for (std::size_t j = 0; j < iVec.cols(); ++j) iVec(i, j) += blk(i, j);
211 const FluidFundamental<T> ff =
212 mfq_fundamental(Fpp, Fpm, iVec, a.D0, T(num_traits<T>::from_double(1e-14)), 150u,
214 const Matrix<T> Y0 = ff.Psi;
217 for (std::size_t i = 0; i < a.T_.rows(); ++i)
218 for (std::size_t j = 0; j < a.T_.cols(); ++j) a.T_(i, j) += Fpp(i, j);
220 a.pi0.assign(a.N * a.Ns, zero);
221 for (std::size_t k = 0; k < K; ++k) {
222 const std::vector<T> tD =
vecmul(theta, a.Dk[k]);
223 for (std::size_t i = 0; i < a.N; ++i)
224 for (std::size_t j = 0; j < a.Ns; ++j)
225 a.pi0[i * a.Ns + j] += tD[i] * T(ba[k][j] / mu[k]);
227 const std::vector<T> tmp =
vecmul(a.pi0, a.T_);
228 for (std::size_t i = 0; i < a.pi0.size(); ++i) a.pi0[i] = -tmp[i];
235Matrix<T> class_indicator(
const AgeProcess<T>& a, std::size_t k,
bool complement) {
236 const T
zero = num_traits<T>::from_int(0), one = num_traits<T>::from_int(1);
237 Matrix<T> jm(a.Ns, 1, complement ? one : zero);
239 for (std::size_t q = 0; q < k; ++q) off += a.Nsk[q];
240 for (std::size_t i = 0; i < a.Nsk[k]; ++i) jm(off + i, 0) = complement ?
zero : one;
246std::vector<T> lift(
const AgeProcess<T>& a,
const Matrix<T>& jm) {
247 std::vector<T> out(a.N * a.Ns);
248 for (std::size_t i = 0; i < a.N; ++i)
249 for (std::size_t j = 0; j < a.Ns; ++j) out[i * a.Ns + j] = jm(j, 0);
255T contract(
const std::vector<T>& pi0,
const Matrix<T>& M,
const std::vector<T>& v) {
256 const std::vector<T> row =
vecmul(pi0, M);
257 T s = num_traits<T>::from_int(0);
258 for (std::size_t i = 0; i < row.size(); ++i) s += row[i] * v[i];
264Matrix<T> class_exit(
const AgeProcess<T>& a,
const std::vector<PhService<T>>& svc, std::size_t k) {
265 const T
zero = num_traits<T>::from_int(0);
266 Matrix<T> sv(a.Ns, 1, zero);
268 for (std::size_t q = 0; q < k; ++q) off += a.Nsk[q];
269 for (std::size_t i = 0; i < a.Nsk[k]; ++i) {
271 for (std::size_t j = 0; j < a.Nsk[k]; ++j) s += svc[k].S(i, j);
305 double precision = 1e-14) {
306 using namespace mmapph1_detail;
308 const AgeProcess<T> a = build(arrival, svc);
309 const std::size_t K = svc.size(), n = a.N * a.Ns;
312 for (std::size_t i = 0; i < n; ++i)
313 for (std::size_t j = 0; j < n; ++j) negT(i, j) = -a.T_(i, j);
315 const std::vector<T> vv =
vecmul(a.pi0, iT);
319 std::vector<std::size_t> nz;
320 for (std::size_t i = 0; i < n; ++i)
323 throw NumericError(
ErrorKind{
"Degenerate"},
"mmapph1fcfs_stdistr_ph: the age density has empty support");
325 std::vector<StDistrPh<T>> out(K);
326 for (std::size_t k = 0; k < K; ++k) {
327 const std::vector<T> clo =
mulvec(iT, lift(a, class_exit(a, svc, k)));
329 for (std::size_t i = 0; i < n; ++i) norm += a.pi0[i] * clo[i];
331 throw NumericError(
ErrorKind{
"Degenerate"},
"mmapph1fcfs_stdistr_ph: class " + std::to_string(k + 1) +
332 " carries no sojourn-time mass");
334 const std::vector<T> Tclo =
mulvec(a.T_, clo);
335 std::vector<T> cl(n);
336 for (std::size_t i = 0; i < n; ++i) cl[i] = T(-Tclo[i] / norm);
338 out[k].alpha.assign(nz.size(), zero);
339 for (std::size_t i = 0; i < nz.size(); ++i) out[k].alpha[i] = T(cl[nz[i]] * vv[nz[i]]);
340 out[k].A =
Matrix<T>(nz.size(), nz.size(), zero);
341 for (std::size_t i = 0; i < nz.size(); ++i)
342 for (std::size_t j = 0; j < nz.size(); ++j)
344 out[k].A(i, j) = T(a.T_(nz[j], nz[i]) * vv[nz[j]] / vv[nz[i]]);
357 using namespace mmapph1_detail;
359 const AgeProcess<T> a = build(arrival, svc);
360 const std::size_t K = svc.size();
363 for (std::size_t i = 0; i < a.N; ++i)
364 for (std::size_t j = 0; j < a.N; ++j) QA(i, j) = a.D0(i, j) + a.Da(i, j);
368 std::vector<T> out(K, zero);
369 for (std::size_t k = 0; k < K; ++k) {
373 const std::vector<T> row =
vecmul(a.pi0, EL2);
375 for (
const T& v : row) s += v;
376 const Matrix<T> jm = class_indicator(a, k,
false);
377 out[k] = T(s + contract(a.pi0, EL1, lift(a, jm)));
392 std::size_t levels) {
393 using namespace mmapph1_detail;
395 if (levels == 0)
throw InputError(
"mmapph1fcfs_ncdistr: at least one level is required");
396 const AgeProcess<T> a = build(arrival, svc);
397 const std::size_t K = svc.size();
401 std::vector<std::vector<T>> out(K, std::vector<T>(levels, zero));
402 for (std::size_t k = 0; k < K; ++k) {
406 for (std::size_t i = 0; i < a.N; ++i)
407 for (std::size_t j = 0; j < a.N; ++j)
408 B(i, j) = a.D0(i, j) + a.Da(i, j) - a.Dk[k](i, j);
411 const std::vector<T> ejm = lift(a, class_indicator(a, k,
false));
412 const std::vector<T> ejmc = lift(a, class_indicator(a, k,
true));
415 out[k][0] = T(one - a.rho + contract(a.pi0, LmCurr, ejmc));
416 for (std::size_t i = 1; i < levels; ++i) {
419 out[k][i] = T(contract(a.pi0, LmCurr, ejmc) + contract(a.pi0, LmPrev, ejm));
NumericError(const std::string &what)
The Kronecker operator of a FIXED (A,B) pair, factorized once.
Matrix< T > solve_lyap(const Matrix< T > &C) const
Solve A X + X B + C = 0, MATLAB's lyap(A,B,C).
Steady-state distribution of a continuous-time Markov chain.
The exception types the port throws.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
Dense matrix and non-owning view.
Core of the Markovian fluid queue: the fundamental matrices Psi, K, U and the matrix-exponential stat...
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
std::vector< T > mmapph1fcfs_ncmean(const Mmap< T > &arrival, const std::vector< PhService< T > > &svc)
Per-class mean number of customers in the system, BUTools' 'ncMoms', 1.
Matrix< T > kron(const Matrix< T > &A, const Matrix< T > &B)
Kronecker product.
FluidFundamental< T > mfq_fundamental(const Matrix< T > &Fpp, const Matrix< T > &Fpm, const Matrix< T > &Fmp, const Matrix< T > &Fmm, const T &precision, unsigned maxNumIt, RiccatiMethod method)
Psi, K and U of a fluid queue whose drifts have been normalized to +-1.
std::vector< std::vector< T > > mmapph1fcfs_ncdistr(const Mmap< T > &arrival, const std::vector< PhService< T > > &svc, std::size_t levels)
Per-class queue-length distribution, BUTools' 'ncDistr', n: P(N_k = 0..n-1).
std::vector< StDistrPh< T > > mmapph1fcfs_stdistr_ph(const Mmap< T > &arrival, const std::vector< PhService< T > > &svc, double precision=1e-14)
Per-class SOJOURN TIME as a continuous phase-type law, BUTools' 'stDistrPH'.
std::vector< T > ctmc_solve(const Matrix< T > &Qin, const std::string &linsolver=std::string())
Steady-state distribution of a continuous-time Markov chain.
Conservation laws of a layered queueing network, enumerated from its structure.
std::vector< T > vecmul(const std::vector< T > &v, const Matrix< T > &A)
Row vector times matrix, v A.
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.
Matrix< T > eye(std::size_t n)
Identity of order n.
Number-type abstraction for the templated API port.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
An MMAP: the underlying MAP plus the per-class arrival matrices.
One class's phase-type service law, He's (sigma_k, S_k).
Matrix< T > S
transient generator
std::vector< T > sigma
initial probability row vector
A phase-type law (alpha, A) as BUTools' 'stDistrPH' returns it.
The Sylvester equation A X + X B = C, and MATLAB's lyap(A,B,C).