LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
sn_reducible_chain.h
Go to the documentation of this file.
1#pragma once
2/**
3 * @file sn_reducible_chain.h
4 * @brief Port of `sn_reducible_chain_refusal.m`: refusal of a closed chain whose
5 * (station,class) routing is reducible.
6 *
7 * A chain is a set of CLASSES (a connected component of class switching), but a
8 * job moves over (stateful node, class) PAIRS. Class switching can join two
9 * classes into one chain while splitting their pairs into disjoint closed sets;
10 * jobs then never cross between the sets, so each set holds a population fixed
11 * by the initial state, whereas the product-form solvers normalize the whole
12 * chain population over every set at once and return a stationary mixture that
13 * no initial state produces (test_CQN_Reduc_1: X = 0.48327 in every cell
14 * against the exact 0.282609 and 0.695652). The exact answer is the product
15 * form with one chain per closed set, which the class-based chain cannot
16 * express, so SolverMVA and SolverNC refuse the model instead.
17 *
18 * Copyright (c) 2012-2026, Imperial College London
19 * All rights reserved.
20 */
21
22#include <cmath>
23#include <cstddef>
24#include <string>
25#include <vector>
26
29#include "line/num/number.h"
30#include "line/util/matrix.h"
31
32namespace line {
33namespace api {
34
35/**
36 * The by-name refusal of the first closed chain whose (station,class) routing
37 * has more than one closed communicating class reachable from the reference
38 * stations, or "" when there is none. The pairs searched are those reachable
39 * from (refstat(r), r) over the classes r of the chain that hold jobs; a closed
40 * set counts only if it carries routing. A chain holding a single job is never
41 * refused (absorption-weighted mixture, exact). Chains are numbered from 1.
42 */
43template <class T>
45 const std::size_t K = sn.nclasses;
46 const std::size_t n = sn.rt.rows();
47 if (n == 0 || sn.rt.cols() != n || sn.chains.empty()) return std::string();
48 const T zero = num_traits<T>::from_int(0);
49 const std::vector<double> njobs = sn.njobs();
50 const std::vector<double> st2sf = sn.station_to_stateful();
51 for (std::size_t c = 0; c < sn.nchains && c < sn.chains.size(); ++c) {
52 bool any = false, closed = true;
53 for (std::size_t r = 0; r < K && r < sn.chains[c].size(); ++r)
54 if (sn.chains[c][r]) {
55 any = true;
56 if (!std::isfinite(njobs[r])) closed = false;
57 }
58 if (!any || !closed) continue; // open or mixed chain: populations are not conserved
59 // One job has no population to split: its time average is the mixture of
60 // the closed sets weighted by its absorption probabilities from the
61 // reference pair, which the visit ratios already hold (cs_transient_class).
62 double pop = 0.0;
63 for (std::size_t r = 0; r < K && r < sn.chains[c].size(); ++r)
64 if (sn.chains[c][r]) pop += njobs[r];
65 if (pop <= 1.0) continue;
66 std::vector<bool> reach(n, false);
67 std::vector<std::size_t> frontier;
68 for (std::size_t r = 0; r < K && r < sn.chains[c].size(); ++r) {
69 if (!sn.chains[c][r] || !(njobs[r] > 0)) continue;
70 const double isf = st2sf[sn.classes[r].refstat - 1];
71 const std::size_t v = (static_cast<std::size_t>(isf) - 1) * K + r;
72 if (v < n && !reach[v]) {
73 reach[v] = true;
74 frontier.push_back(v);
75 }
76 }
77 if (frontier.empty()) continue;
78 while (!frontier.empty()) {
79 std::vector<std::size_t> next;
80 for (std::size_t u : frontier)
81 for (std::size_t w = 0; w < n; ++w)
82 if (!reach[w] && sn.rt(u, w) != zero) {
83 reach[w] = true;
84 next.push_back(w);
85 }
86 frontier.swap(next);
87 }
88 std::vector<std::size_t> idx;
89 for (std::size_t v = 0; v < n; ++v)
90 if (reach[v]) idx.push_back(v);
91 const std::size_t m = idx.size();
92 Matrix<double> sub(m, m);
93 std::vector<bool> routed(m, false);
94 for (std::size_t a = 0; a < m; ++a) {
95 for (std::size_t w = 0; w < n; ++w)
96 if (sn.rt(idx[a], w) != zero) {
97 routed[a] = true;
98 break;
99 }
100 for (std::size_t b = 0; b < m; ++b) sub(a, b) = (sn.rt(idx[a], idx[b]) != zero) ? 1.0 : 0.0;
101 }
102 const mc::SccResult scc = mc::stronglyconncomp(sub);
103 std::size_t nclosed = 0;
104 for (std::size_t s = 0; s < scc.numSCC(); ++s) {
105 if (!scc.recurrent[s]) continue;
106 for (std::size_t a : scc.members[s])
107 if (routed[a]) {
108 ++nclosed;
109 break;
110 }
111 }
112 if (nclosed > 1)
113 return "Chain " + std::to_string(c + 1) + " splits into " + std::to_string(nclosed) +
114 " closed sets of (station,class) pairs that jobs cannot move between, so each set "
115 "holds a population fixed by the initial state, while a chain is a set of classes "
116 "whose population is normalized over all of them. A reducible class-switching chain "
117 "is not supported by the solver; use SolverCTMC, SolverSSA or SolverJMT.";
118 }
119 return std::string();
120}
121
122} // namespace api
123} // namespace line
A network plus its refreshed NetworkStruct.
Dense matrix and non-owning view.
std::string sn_reducible_chain_refusal(const qn::NetworkStruct< T > &sn)
The by-name refusal of the first closed chain whose (station,class) routing has more than one closed ...
SccResult stronglyconncomp(const Matrix< T > &A)
Strongly connected components of a directed graph, and which of them are recurrent (closed under the ...
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
A queueing network and its refreshed NetworkStruct.
Number-type abstraction for the templated API port.
Strongly connected components of a directed graph, and which of them are recurrent (closed under the ...
std::size_t numSCC() const
std::vector< bool > recurrent
recurrent[c-1] is true when component c has no edge leaving it.
std::vector< std::vector< std::size_t > > members
Member states of each component, ascending.