140inline std::vector<std::pair<std::size_t, std::size_t>>
censor_fill(
141 const Pattern& P,
const std::vector<std::size_t>& kix, std::size_t k,
142 const std::vector<std::size_t>& setid,
const std::vector<bool>& alive, std::size_t& ncols,
143 std::size_t& nrows_into) {
146 std::map<std::size_t, std::vector<std::size_t>> direct;
147 std::set<std::size_t> cols;
148 for (std::size_t i : kix) {
149 std::vector<std::size_t>& d = direct[i];
150 for (std::size_t j : P.out[i])
151 if (alive[j] && setid[j] != k) {
157 std::set<std::size_t> rows;
158 for (std::size_t i : kix)
159 for (std::size_t b : P.in[i])
160 if (alive[b] && setid[b] != k) rows.insert(b);
161 nrows_into = rows.size();
162 std::vector<std::pair<std::size_t, std::size_t>> F;
163 if (cols.empty())
return F;
165 std::map<std::size_t, std::set<std::size_t>> X;
166 for (std::size_t i : kix) {
167 std::set<std::size_t>& xi = X[i];
168 std::set<std::size_t> seen;
169 std::vector<std::size_t> stack(1, i);
171 while (!stack.empty()) {
172 const std::size_t u = stack.back();
174 const std::vector<std::size_t>& du = direct[u];
175 xi.insert(du.begin(), du.end());
176 for (std::size_t v : P.out[u])
177 if (setid[v] == k && alive[v] && seen.insert(v).second) stack.push_back(v);
180 for (std::size_t b : rows) {
181 std::set<std::size_t> fr;
182 for (std::size_t i : P.out[b])
183 if (setid[i] == k && alive[i]) {
184 const std::set<std::size_t>& xi = X[i];
185 fr.insert(xi.begin(), xi.end());
187 for (std::size_t c : fr) F.emplace_back(b, c);
197 const std::vector<std::size_t>& lvl, std::size_t S,
198 const std::vector<std::vector<std::size_t>>& pcnt,
200 const std::size_t nr = setid.size();
201 cost = std::numeric_limits<double>::infinity();
203 for (std::size_t i = 0; i < nr; ++i)
204 for (std::size_t j : P.out[i])
205 if (
level_gap(setid, lvl, i, j))
return std::vector<std::size_t>();
206 std::vector<std::vector<std::size_t>> members(S);
207 for (std::size_t i = 0; i < nr; ++i) members[setid[i]].push_back(i);
208 std::vector<bool> alive(nr,
true);
209 std::vector<std::size_t> remaining(S), ord;
210 for (std::size_t s = 0; s < S; ++s) remaining[s] = s;
212 for (std::size_t step = 0; step + 1 < S; ++step) {
214 std::size_t best = 0, bestFill = std::numeric_limits<std::size_t>::max();
215 double bestWork = 0.0;
216 std::vector<std::pair<std::size_t, std::size_t>> bestF;
217 for (std::size_t k : remaining) {
218 const std::vector<std::size_t>& kix = members[k];
219 std::size_t ncols = 0, nrows = 0;
220 const std::vector<std::pair<std::size_t, std::size_t>> F =
221 censor_fill(P, kix, k, setid, alive, ncols, nrows);
223 for (
const std::pair<std::size_t, std::size_t>& e : F)
224 if (
level_gap(setid, lvl, e.first, e.second)) {
230 for (
const std::pair<std::size_t, std::size_t>& e : F)
231 if (!P.has(e.first, e.second)) ++nf;
232 if (!found || nf < bestFill) {
237 const std::size_t pmax = *std::max_element(pcnt[k].begin(), pcnt[k].end());
238 bestWork =
cube_sum(pcnt[k]) +
static_cast<double>(kix.size()) *
239 static_cast<double>(ncols) *
240 static_cast<double>(pmax + nrows);
243 if (!found)
return std::vector<std::size_t>();
245 for (
const std::pair<std::size_t, std::size_t>& e : bestF) P.add(e.first, e.second);
246 for (std::size_t i : members[best]) {
251 remaining.erase(std::find(remaining.begin(), remaining.end(), best));
253 ord.push_back(remaining.front());
254 total +=
cube_sum(pcnt[remaining.front()]);
288 const std::size_t n = Q.
rows();
295 const std::size_t states_max =
opt.censor_states_max == 0 ? 10000 :
opt.censor_states_max;
298 "SolverMAM: the censor method solves chains of at most "
299 "options.config.censor_states_max = " + std::to_string(states_max) +
300 " states, and this one has " + std::to_string(n) +
301 ". Raise the cap, lower options.cutoff, or use SolverCTMC.");
305 for (std::size_t i = 0; i < n; ++i)
306 for (std::size_t j = 0; j < n; ++j)
309 std::size_t nrec = 0;
314 "SolverMAM: the censor method needs a chain with one recurrent class; this one has " +
315 std::to_string(nrec) +
316 ", so its stationary distribution depends on the initial state");
317 std::vector<std::size_t> keep;
318 for (std::size_t i = 0; i < n; ++i)
320 const std::size_t nr = keep.size();
322 for (std::size_t a = 0; a < nr; ++a)
323 for (std::size_t b = 0; b < nr; ++b) Qr(a, b) = Q(keep[a], keep[b]);
327 const std::size_t K =
sn.nclasses, M =
sn.nstations;
328 std::vector<std::vector<double>> cnt(M, std::vector<double>(nr, 0.0));
329 for (std::size_t ist = 0; ist < M; ++ist)
330 for (std::size_t s = 0; s < nr; ++s) {
332 for (std::size_t k = 0; k < K; ++k)
338 std::vector<std::size_t> cand;
339 if (!
opt.censor_level.empty() ||
opt.censor_level_index.has_value()) {
342 std::vector<double>
rng(M, 0.0);
343 for (std::size_t ist = 0; ist < M; ++ist)
345 rng[ist] = *std::max_element(cnt[ist].begin(), cnt[ist].end()) -
346 *std::min_element(cnt[ist].begin(), cnt[ist].end());
349 for (
double &r :
rng)
350 if (!std::isfinite(r)) r = 0.0;
351 std::vector<std::size_t> idx(M);
352 for (std::size_t i = 0; i < M; ++i) idx[i] = i;
353 std::stable_sort(idx.begin(), idx.end(),
354 [&
rng](std::size_t a, std::size_t b) { return rng[a] > rng[b]; });
355 for (std::size_t i : idx)
356 if (
rng[i] > 0) cand.push_back(i);
357 if (cand.empty()) cand.push_back(0);
361 std::vector<std::pair<std::size_t, std::size_t>> edges;
362 for (std::size_t i = 0; i < nr; ++i)
363 for (std::size_t j = 0; j < nr; ++j)
364 if (i != j && !(Qr(i, j) == zero)) {
366 edges.emplace_back(i, j);
369 const std::size_t nstateful =
sn.stateful_nodes.size();
371 double cost = std::numeric_limits<double>::infinity();
372 std::size_t L = 0, S = 0;
373 std::vector<std::size_t> keyNodes, setid, lvl, ord;
375 std::size_t ncand = 0;
376 for (std::size_t L : cand) {
377 const std::vector<double>& lev = cnt[L];
380 for (
const std::pair<std::size_t, std::size_t>& e : edges)
381 if (std::fabs(lev[e.first] - lev[e.second]) > 1.0) {
386 const std::size_t lsf =
sn.stateful_of_station(L + 1);
387 std::vector<std::size_t> others;
388 for (std::size_t isf = 1; isf <= nstateful; ++isf)
389 if (isf != lsf) others.push_back(isf);
390 const std::size_t no = others.size();
391 for (std::size_t sz = 0; sz <= no; ++sz) {
393 std::vector<std::size_t> c(sz);
394 for (std::size_t q = 0; q < sz; ++q) c[q] = q;
396 std::vector<std::size_t> keyNodes(sz);
397 for (std::size_t q = 0; q < sz; ++q) keyNodes[q] = others[c[q]];
399 std::vector<std::size_t> setid(nr, 0);
401 if (!keyNodes.empty()) {
402 std::vector<std::vector<std::vector<double>>> rows(nr);
403 std::map<std::vector<std::vector<double>>, std::size_t> uniq;
404 for (std::size_t s = 0; s < nr; ++s) {
406 for (std::size_t isf : keyNodes) {
407 const std::vector<T>& loc = SS[keep[s]].local[isf - 1];
408 std::vector<double> d(loc.size());
409 for (std::size_t t = 0; t < loc.size(); ++t)
411 rows[s].push_back(d);
413 uniq.emplace(rows[s], 0);
415 std::size_t rank = 0;
416 for (
auto& kv : uniq) kv.second = rank++;
417 for (std::size_t s = 0; s < nr; ++s) setid[s] = uniq[rows[s]];
420 if (!(S >
kMaxSets || (S > 1 && S == nr))) {
421 std::vector<std::size_t> lvl(nr, 0);
422 std::vector<std::vector<std::size_t>> pcnt(S);
423 std::vector<std::vector<std::size_t>> members(S);
424 for (std::size_t s = 0; s < nr; ++s) members[setid[s]].push_back(s);
425 for (std::size_t q = 0; q < S; ++q) {
426 std::vector<double> u;
427 for (std::size_t s : members[q]) u.push_back(lev[s]);
428 std::sort(u.begin(), u.end());
429 u.erase(std::unique(u.begin(), u.end()), u.end());
430 pcnt[q].assign(u.size(), 0);
431 for (std::size_t s : members[q]) {
432 lvl[s] =
static_cast<std::size_t
>(
433 std::lower_bound(u.begin(), u.end(), lev[s]) - u.begin()) + 1;
434 ++pcnt[q][lvl[s] - 1];
438 for (
const std::vector<std::size_t>& p : pcnt) lb +=
cube_sum(p);
439 if (lb < best.cost) {
441 const std::vector<std::size_t> ord =
444 if (!ord.empty() && cost < best.cost) {
447 best.keyNodes = keyNodes;
458 while (q > 0 && c[q - 1] == no - sz + (q - 1)) --q;
461 for (std::size_t t = q; t < sz; ++t) c[t] = c[t - 1] + 1;
465 if (!std::isfinite(best.cost))
467 "SolverMAM: the censor method found no level station whose job count changes by at "
468 "most one job per transition; the chain is not a system of connected LDQBDs in any "
469 "station's queue length");
472 const std::size_t S = best.S;
473 std::vector<std::size_t>
perm;
475 std::vector<std::vector<std::size_t>> phases(S);
476 for (std::size_t j = 0; j < S; ++j) {
477 std::vector<std::size_t> ix;
478 for (std::size_t s = 0; s < nr; ++s)
479 if (best.setid[s] == best.ord[j]) ix.push_back(s);
480 std::stable_sort(ix.begin(), ix.end(), [&best](std::size_t a, std::size_t b) {
481 return best.lvl[a] < best.lvl[b];
483 std::size_t lmax = 0;
484 for (std::size_t s : ix) lmax = std::max(lmax, best.lvl[s]);
485 phases[j].assign(lmax, 0);
486 for (std::size_t s : ix) {
487 ++phases[j][best.lvl[s] - 1];
492 for (std::size_t a = 0; a < nr; ++a)
493 for (std::size_t b = 0; b < nr; ++b) Qp(a, b) = Qr(
perm[a],
perm[b]);
495 std::vector<T> pi(n, zero);
496 for (std::size_t a = 0; a < nr; ++a) pi[keep[
perm[a]]] = res.
pi[a];
501 info.
order.resize(S);
502 for (std::size_t j = 0; j < S; ++j) info.
order[j] = best.ord[j] + 1;
504 info.
cost = best.cost;