5#ifndef LINE_LANG_QN_MATCHING_STATION_H
6#define LINE_LANG_QN_MATCHING_STATION_H
57 return ist >= 1 && ist <=
sn.stations.size() &&
64 if (ind == 0 || ind >
sn.nodes.size())
return false;
71 const std::size_t R =
sn.nclasses;
72 std::vector<std::vector<bool>> G(R, std::vector<bool>(R,
false));
73 const std::vector<std::vector<bool>>& C =
sn.stations[ist - 1].compatibility;
74 for (std::size_t r = 0; r < R && r < C.size(); ++r)
75 for (std::size_t s = 0; s < R && s < C[r].size(); ++s) G[r][s] = C[r][s];
82 const double cap = ist <=
sn.cap.size() ?
sn.cap[ist - 1] : std::numeric_limits<double>::infinity();
83 if (!std::isfinite(cap) || cap >=
static_cast<double>(std::numeric_limits<int>::max()))
return -1;
84 return static_cast<long>(cap);
101 }
catch (
const Error&) {
103 "A Matching node supports only Markovian (phase-type) patience distributions.");
115 if (!
has_patience(
sn, ist, r))
return std::vector<std::size_t>(1, 1);
117 const std::size_t K = p.
D0.rows();
118 std::vector<std::size_t> ph;
120 for (std::size_t k = 0; k < K; ++k) ph.push_back(k + 1);
124 for (std::size_t k = 0; k < K; ++k)
132 std::size_t ist, std::size_t r) {
133 std::pair<std::vector<std::size_t>, std::vector<T>> out;
135 out.first.push_back(1);
142 for (std::size_t k = 0; k < alpha.size(); ++k)
144 for (std::size_t k = 0; k < alpha.size(); ++k)
146 out.first.push_back(k + 1);
147 out.second.push_back(T(alpha[k] / tot));
162 const std::vector<std::size_t>& n,
bool initial_only) {
163 const std::size_t R =
sn.nclasses;
165 std::size_t total = 0;
166 for (std::size_t r = 0; r < R && r < n.size(); ++r) total += n[r];
170 "A Matching node requires a finite capacity (setCap) for state-space generation.");
173 const std::size_t w2 = 2 *
static_cast<std::size_t
>(W);
174 std::vector<std::vector<T>> out;
179 if (total >
static_cast<std::size_t
>(W))
return out;
181 std::vector<std::size_t> supp;
182 for (std::size_t r = 0; r < R; ++r)
183 if (n[r] > 0) supp.push_back(r);
184 for (std::size_t a : supp)
185 for (std::size_t b : supp)
186 if (G[a][b])
return out;
187 std::vector<std::size_t> word;
188 for (std::size_t r : supp)
189 for (std::size_t j = 0; j < n[r]; ++j) word.push_back(r + 1);
190 std::vector<std::vector<std::size_t>> ph(R);
191 for (std::size_t r : supp) ph[r] =
phases(
sn, ist, r, initial_only);
196 for (std::size_t p = 0; p < total; ++p) {
197 const std::size_t cls = word[p];
198 std::vector<std::vector<T>> next;
199 for (
const std::vector<T>& base : partial)
200 for (std::size_t k : ph[cls - 1]) {
201 std::vector<T> row = base;
208 out.insert(out.end(), partial.begin(), partial.end());
209 }
while (std::next_permutation(word.begin(), word.end()));
216 const std::size_t R =
sn.nclasses;
217 const std::size_t V =
sn.nvars_of(ind);
218 const std::size_t w2 = row.size() > V ? row.size() - V : 0;
220 for (std::size_t c = 0; c < w2; c += 2) {
229std::vector<std::pair<std::size_t, std::size_t>>
word_of(
const std::vector<T>& row, std::size_t w2) {
230 std::vector<std::pair<std::size_t, std::size_t>> w;
231 for (std::size_t q = 0; q + 1 < w2 && q + 1 < row.size(); q += 2) {
234 w.push_back(std::make_pair(
static_cast<std::size_t
>(cls),
242std::vector<T>
pack(
const std::vector<std::pair<std::size_t, std::size_t>>& w,
const std::vector<T>& row,
245 for (std::size_t q = 0; q < w.size(); ++q) {
249 for (std::size_t c = w2; c < row.size(); ++c) out[c] = row[c];
260 lang::EventType event, std::size_t cls, std::vector<std::vector<T>>& rows,
261 std::vector<T>& rates, std::vector<T>& probs) {
262 const std::size_t ist =
sn.nodes[ind - 1].station;
265 throw InputError(
"A Matching node requires a finite capacity (setCap) for state-space generation.");
268 const std::size_t V =
sn.nvars_of(ind);
269 const std::size_t w2 = inspace.size() > V ? inspace.size() - V : 0;
270 const std::size_t r = cls - 1;
271 const std::vector<std::pair<std::size_t, std::size_t>> w =
word_of(inspace, w2);
272 const std::size_t m = w.size();
276 for (std::size_t q = 0; q < m; ++q)
277 if (G[r][w[q].first - 1]) {
282 std::vector<std::pair<std::size_t, std::size_t>> neww = w;
283 neww.erase(neww.begin() +
static_cast<long>(nb));
284 rows.push_back(
pack(neww, inspace, w2));
285 rates.push_back(passive);
286 probs.push_back(one);
287 }
else if (
static_cast<long>(m) < cap) {
288 const std::pair<std::vector<std::size_t>, std::vector<T>> e =
entry(
sn, ist, r);
289 for (std::size_t i = 0; i < e.first.size(); ++i) {
290 std::vector<std::pair<std::size_t, std::size_t>> neww = w;
291 neww.push_back(std::make_pair(cls, e.first[i]));
292 rows.push_back(
pack(neww, inspace, w2));
293 rates.push_back(passive);
294 probs.push_back(e.second[i]);
300 for (std::size_t q = 0; q < m; ++q) {
301 if (w[q].first != cls)
continue;
302 const std::size_t k = w[q].second - 1;
303 for (std::size_t j = 0; j < p.
D0.cols(); ++j) {
305 std::vector<std::pair<std::size_t, std::size_t>> neww = w;
306 neww[q].second = j + 1;
307 rows.push_back(
pack(neww, inspace, w2));
308 rates.push_back(p.
D0(k, j));
309 probs.push_back(one);
315 for (std::size_t q = 0; q < m; ++q) {
316 if (w[q].first != cls)
continue;
317 const std::size_t k = w[q].second - 1;
319 for (std::size_t j = 0; j < p.
D1.cols(); ++j) a += p.
D1(k, j);
321 std::vector<std::pair<std::size_t, std::size_t>> neww = w;
322 neww.erase(neww.begin() +
static_cast<long>(q));
323 rows.push_back(
pack(neww, inspace, w2));
325 probs.push_back(one);
333 const std::size_t R =
sn.nclasses;
334 std::vector<double> lam(R, 0.0);
335 for (std::size_t i = 1; i <=
sn.stations.size(); ++i) {
336 const std::size_t ind =
sn.node_of_station(i);
337 if (ind == 0 ||
sn.nodes[ind - 1].nodetype != NodeType::Source)
continue;
338 for (std::size_t r = 0; r < R; ++r) {
340 lam[r] = (std::isfinite(v) && v > 0) ? v : 0.0;
349 std::vector<double>
X;
362 const std::vector<double>& prob) {
363 const std::size_t R =
sn.nclasses;
364 const std::size_t ind =
sn.node_of_station(ist);
368 const std::size_t V =
sn.nvars_of(ind);
369 std::vector<bool> has_pat(R,
false);
370 std::vector<std::vector<double>> renrate(R);
371 for (std::size_t r = 0; r < R; ++r) {
373 if (!has_pat[r])
continue;
375 renrate[r].assign(p.
D1.rows(), 0.0);
376 for (std::size_t k = 0; k < p.
D1.rows(); ++k)
381 t.
acc.assign(R, 0.0);
382 t.
reneg.assign(R, 0.0);
383 for (std::size_t s = 0; s < rows.size(); ++s) {
384 const double p = prob[s];
385 if (!(p > 0))
continue;
386 const std::vector<T>& row = *rows[s];
387 const std::size_t w2 = row.size() > V ? row.size() - V : 0;
388 const std::vector<std::pair<std::size_t, std::size_t>> w =
word_of(row, w2);
389 const std::size_t m = w.size();
390 for (std::size_t r = 0; r < R; ++r) {
391 if (!(lam[r] > 0))
continue;
393 for (std::size_t q = 0; q < m; ++q)
394 if (G[r][w[q].first - 1]) {
399 t.
X[r] += p * lam[r];
400 t.
X[w[nb].first - 1] += p * lam[r];
401 t.
acc[r] += p * lam[r];
402 }
else if (cap < 0 ||
static_cast<long>(m) < cap) {
403 t.
acc[r] += p * lam[r];
406 for (
const std::pair<std::size_t, std::size_t>& job : w) {
407 const std::size_t c = job.first - 1;
408 if (has_pat[c] && job.second >= 1 && job.second <= renrate[c].size())
409 t.
reneg[c] += p * renrate[c][job.second - 1];
Base error for the multiprecision C++ port.
UnsupportedError(const std::string &what)
A network plus its refreshed NetworkStruct.
What refreshProcessRepresentations and refreshLST compute FROM a distribution: the (D0,...
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
mam::Map< T > dist_to_map(const Distrib< T > &d)
The (D0,D1) pair that reaches sn.proc.
EventType
The events a state can undergo, with the values of MATLAB EventType.
@ PHASE
service advances a phase WITHOUT departing
@ RENEGE
a waiting job abandons the queue (impatience)
std::vector< T > map_pie(const Map< T > &m)
Phase distribution seen by an arriving job, pie = pi D1 / (pi D1 e).
long capacity(const NetworkStruct< T > &sn, std::size_t ist)
Capacity of station ist (1-based), or -1 when the station is uncapped.
std::vector< T > count_waiting(const NetworkStruct< T > &sn, std::size_t ind, const std::vector< T > &row)
Per-class waiting counts of a Matching station's local state row (node ind, 1-based).
std::vector< double > source_rates(const NetworkStruct< T > &sn)
Arrival rate of each class at the Source (0 when the class has none).
std::vector< std::size_t > phases(const NetworkStruct< T > &sn, std::size_t ist, std::size_t r, bool initial_only)
Patience phases (1-based) a waiting class-r job can be in: {1} without patience, otherwise 1....
bool is_matching_node(const NetworkStruct< T > &sn, std::size_t ind)
True when node ind (1-based) is a Matching (FCFM) station.
void after_event(const NetworkStruct< T > &sn, std::size_t ind, const std::vector< T > &inspace, lang::EventType event, std::size_t cls, std::vector< std::vector< T > > &rows, std::vector< T > &rates, std::vector< T > &probs)
Successors of a Matching station (node ind, 1-based) after event on class cls (1-based): the rows,...
mam::Map< T > patience_map(const NetworkStruct< T > &sn, std::size_t ist, std::size_t r)
The (D0, D1) representation of the class-r patience at station ist.
std::vector< T > pack(const std::vector< std::pair< std::size_t, std::size_t > > &w, const std::vector< T > &row, std::size_t w2)
Pack a word back into a row of the width of row, keeping its trailing variables.
std::vector< std::pair< std::size_t, std::size_t > > word_of(const std::vector< T > &row, std::size_t w2)
The (class, phase) pairs of a word, both 1-based, oldest first.
std::vector< std::vector< T > > space(const NetworkStruct< T > &sn, std::size_t ist, const std::vector< std::size_t > &n, bool initial_only)
Local states of Matching station ist (1-based) with per-class waiting counts n, WITHOUT the trailing ...
bool has_patience(const NetworkStruct< T > &sn, std::size_t ist, std::size_t r)
Whether class r (0-based) reneges at station ist (1-based).
bool is_matching(const NetworkStruct< T > &sn, std::size_t ist)
True when station ist (1-based) is a Matching (FCFM) station.
std::vector< std::vector< bool > > compatibility(const NetworkStruct< T > &sn, std::size_t ist)
Compatibility graph of Matching station ist (1-based), R x R.
std::pair< std::vector< std::size_t >, std::vector< T > > entry(const NetworkStruct< T > &sn, std::size_t ist, std::size_t r)
Initial patience phases (1-based) of a new class-r job and their probabilities.
Tput matching_tput(const NetworkStruct< T > &sn, std::size_t ist, const std::vector< const std::vector< T > * > &rows, const std::vector< double > &prob)
Per-class rates at Matching station ist (1-based) from the probabilities prob of its local states row...
Conservation laws of a layered queueing network, enumerated from its structure.
A queueing network and its refreshed NetworkStruct.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
A MAP as the pair of matrices (D0, D1).
One station of the network.
std::vector< Distrib< T > > patience
Queue.setPatience(class, dist): the abandonment timer of a WAITING job, with impatience[r] naming whi...
std::vector< lang::ImpatienceType > impatience
Per-class rates at a Matching station: matched, accepted and reneging.
std::vector< double > reneg
class-r reneging rate
std::vector< double > acc
accepted class-r arrival rate (matched or admitted)
std::vector< double > X
rate at which class-r jobs leave the node matched