73 const std::vector<int>& mi) {
74 const std::size_t M = L.
rows();
75 const std::size_t R = N.size();
77 throw InputError(
"pfqn_mva: demand matrix and population vector disagree on the class count");
78 if (!mi.empty() && mi.size() != M)
79 throw InputError(
"pfqn_mva: multiplicity vector has the wrong length");
85 res.
XN.assign(R, zero);
92 bool anyPositive =
false;
94 if (v < 0)
throw InputError(
"pfqn_mva: negative population");
95 if (v > 0) anyPositive =
true;
97 if (!anyPositive || M == 0)
return res;
99 std::vector<T> Zsum(R, zero);
101 if (Z.
cols() != R)
throw InputError(
"pfqn_mva: Z and N disagree on the class count");
102 for (std::size_t k = 0; k < Z.
rows(); ++k)
103 for (std::size_t r = 0; r < R; ++r) Zsum[r] += Z(k, r);
106 std::vector<T> multi(M, one);
109 const std::vector<std::size_t> prods =
plane_sizes(N);
113 std::vector<T> Q(total * M, zero);
115 std::vector<int> n(R, 0);
117 std::size_t visited = 0;
122 if ((++visited & 1023u) == 0)
125 for (
int v : n) npop += v;
127 const std::size_t idx =
pop_index(n, prods);
128 for (std::size_t s = 0; s < R; ++s) {
130 const std::size_t idx_1s = n[s] > 0 ? idx - prods[s] : 0;
132 for (std::size_t i = 0; i < M; ++i) {
133 const T qarv = Q[idx_1s * M + i];
134 res.
CN(i, s) = L(i, s) * (multi[i] + qarv);
135 ctot += res.
CN(i, s);
139 if (ctot == zero && n[s] > 0)
throw NumericError(
ErrorKind{
"Degenerate"},
"pfqn_mva: zero total residence time");
141 for (std::size_t i = 0; i < M; ++i) {
142 res.
QN(i, s) = res.
XN[s] * res.
CN(i, s);
143 Q[idx * M + i] += res.
QN(i, s);
149 for (
long r =
static_cast<long>(R) - 1; r >= 0; --r)
155 bool prefixFull =
true;
156 for (
long r = 0; r < last_nnz; ++r)
161 bool suffixEmpty =
true;
162 for (std::size_t r =
static_cast<std::size_t
>(last_nnz) + 1; r < R; ++r)
167 if (prefixFull && suffixEmpty) {
168 const T& x = res.
XN[
static_cast<std::size_t
>(last_nnz)];
169 if (x == zero)
throw NumericError(
ErrorKind{
"Degenerate"},
"pfqn_mva: zero throughput on the G path");
177 for (std::size_t i = 0; i < M; ++i)
178 for (std::size_t r = 0; r < R; ++r) res.
UN(i, r) = res.
XN[r] * L(i, r);
215 const std::vector<int>& mi,
const Matrix<T>& IL) {
216 const std::size_t M = L.
rows();
217 const std::size_t R = N.size();
219 throw InputError(
"pfqn_mva_ilock: demand matrix and population vector disagree on the class count");
220 if (!mi.empty() && mi.size() != M)
221 throw InputError(
"pfqn_mva_ilock: multiplicity vector has the wrong length");
227 res.
XN.assign(R, zero);
234 bool anyPositive =
false;
236 if (v < 0)
throw InputError(
"pfqn_mva_ilock: negative population");
237 if (v > 0) anyPositive =
true;
239 if (!anyPositive || M == 0)
return res;
241 std::vector<T> Zsum(R, zero);
243 if (Z.
cols() != R)
throw InputError(
"pfqn_mva_ilock: Z and N disagree on the class count");
244 for (std::size_t k = 0; k < Z.
rows(); ++k)
245 for (std::size_t r = 0; r < R; ++r) Zsum[r] += Z(k, r);
248 std::vector<T> multi(M, one);
252 throw InputError(
"pfqn_mva_ilock: an interlock matrix is required; use pfqn_mva for the standard arrival theorem");
255 if (IL.
rows() != R || IL.
cols() != R)
256 throw InputError(
"pfqn_mva_ilock: the interlock matrix must be nclasses x nclasses");
258 for (std::size_t r = 0; r < R; ++r)
259 for (std::size_t s = 0; s < R; ++s) {
260 T w = (r == s) ? one : T(one - IL(r, s));
261 if (w < zero) w = zero;
262 if (w > one) w = one;
267 const std::vector<std::size_t> prods =
plane_sizes(N);
271 std::vector<T> Q(total * M, zero);
273 std::vector<T> Qc(total * M * R, zero);
275 std::vector<T> Uc(total * M * R, zero);
277 std::vector<int> n(R, 0);
279 std::size_t visited = 0;
284 if ((++visited & 1023u) == 0)
287 for (
int v : n) npop += v;
289 const std::size_t idx =
pop_index(n, prods);
293 Matrix<T> qfull(M, R, zero), qdisc(M, R, zero);
294 for (std::size_t s = 0; s < R; ++s) {
296 const std::size_t idx_1s = n[s] > 0 ? idx - prods[s] : 0;
297 for (std::size_t i = 0; i < M; ++i)
298 for (std::size_t r = 0; r < R; ++r) {
299 const T qc = Qc[(idx_1s * M + i) * R + r];
300 const T inSvc = Uc[(idx_1s * M + i) * R + r];
301 const T wait = T(ILw(s, r) * qc - inSvc);
303 qdisc(i, s) += T((wait > zero ? wait : zero) + ILw(s, r) * inSvc);
311 auto level = [&](
const T& theta, std::vector<T>& X,
Matrix<T>& C) ->
bool {
312 for (std::size_t s = 0; s < R; ++s) {
314 for (std::size_t i = 0; i < M; ++i) {
315 C(i, s) = L(i, s) * (multi[i] + qfull(i, s) - theta * (qfull(i, s) - qdisc(i, s)));
322 for (std::size_t i = 0; i < M; ++i) {
324 for (std::size_t s = 0; s < R; ++s) u += X[s] * L(i, s);
330 std::vector<T> Xl(R, zero);
332 if (!level(one, Xl, Cl)) {
333 double lo = 0.0, hi = 1.0;
334 for (
int it = 0; it < 60; ++it) {
335 const double mid = 0.5 * (lo + hi);
341 for (std::size_t s = 0; s < R; ++s) {
343 for (std::size_t i = 0; i < M; ++i) {
344 res.
CN(i, s) = Cl(i, s);
345 res.
QN(i, s) = res.
XN[s] * res.
CN(i, s);
346 Q[idx * M + i] += res.
QN(i, s);
347 Qc[(idx * M + i) * R + s] = res.
QN(i, s);
348 Uc[(idx * M + i) * R + s] = res.
XN[s] * L(i, s);
358 for (std::size_t i = 0; i < M; ++i)
359 for (std::size_t r = 0; r < R; ++r) res.
UN(i, r) = res.
XN[r] * L(i, r);
MvaResult< T > pfqn_mva(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const std::vector< int > &mi)
Exact Mean Value Analysis for closed product-form networks (Reiser and Lavenberg 1980).
MvaResult< T > pfqn_mva_ilock(const Matrix< T > &L, const std::vector< int > &N, const Matrix< T > &Z, const std::vector< int > &mi, const Matrix< T > &IL)
Exact MVA recursion carrying the interlocked-flow correction.