5#ifndef LINE_API_FES_FES_AGGREGATE_H
6#define LINE_API_FES_FES_AGGREGATE_H
152 const std::vector<std::size_t>& subsetIndices,
154 const std::vector<int>& cutoffs,
const FesOptions& options);
159inline std::string fes_mat2str(
const std::vector<int>& v) {
160 if (v.size() == 1)
return std::to_string(v[0]);
162 for (std::size_t k = 0; k < v.size(); ++k) s += (k ?
" " :
"") + std::to_string(v[k]);
173std::vector<std::vector<T>> fes_measured_table(
const std::vector<std::vector<double>>& tab,
174 const std::vector<int>& cutoffs, std::size_t K) {
175 std::size_t tableSize = 1;
176 for (
int c : cutoffs) tableSize *=
static_cast<std::size_t
>(c + 1);
179 "aggregateFES: options.throughputTable must be a cell array with one vector per class (" +
180 std::to_string(K) +
").");
181 std::vector<std::vector<T>> out(K);
182 for (std::size_t r = 0; r < K; ++r) {
183 const std::vector<double>& v = tab[r];
184 if (v.size() != tableSize)
186 "aggregateFES: options.throughputTable{" + std::to_string(r + 1) +
"} has " +
187 std::to_string(v.size()) +
" entries; the cutoffs " + fes_mat2str(cutoffs) +
188 " need prod(cutoffs+1) = " + std::to_string(tableSize) +
189 ", indexed by ljd_linearize.");
191 if (!std::isfinite(x) || x < 0.0)
193 "aggregateFES: options.throughputTable{" + std::to_string(r + 1) +
194 "} must hold finite non-negative throughputs.");
195 out[r].resize(tableSize);
198 for (std::size_t idx = 0; idx < tableSize; ++idx) {
200 out[r][idx] = nvec[r] == 0 ? num_traits<T>::from_int(0) : num_traits<T>::from_double(v[idx]);
207inline std::string fes_lower(std::string s) {
208 for (
char& c : s) c =
static_cast<char>(std::tolower(
static_cast<unsigned char>(c)));
233 const std::vector<std::size_t>& subsetIndices,
238 const std::size_t M =
sn.nstations, K =
sn.nclasses;
243 std::vector<std::size_t> complementIndices;
244 for (std::size_t i = 1; i <= M; ++i) {
245 bool inSubset =
false;
246 for (std::size_t a = 0; a < subsetIndices.size(); ++a)
247 if (subsetIndices[a] == i) inSubset =
true;
248 if (!inSubset) complementIndices.push_back(i);
251 const std::vector<double> N =
sn.njobs();
252 std::vector<int> cutoffs = options.cutoffs;
254 for (std::size_t r = 0; r < K; ++r) cutoffs.push_back(
static_cast<int>(N[r]));
255 if (cutoffs.size() != K)
256 throw InputError(
"aggregateFES: cutoffs must carry one entry per class");
257 const std::string solver = detail::fes_lower(options.solver);
258 if (options.throughputTable.empty() && solver !=
"mva" && solver !=
"ctmc" && solver !=
"ssa" &&
259 solver !=
"jmt" && solver !=
"ldes")
261 "aggregateFES: options.solver '" + solver +
262 "' is not supported: use 'mva', 'ctmc', 'ssa', 'jmt' or 'ldes'.");
267 std::vector<std::size_t> subsetRt, complementRt;
268 for (std::size_t a = 0; a < subsetIndices.size(); ++a) {
269 const std::size_t isf =
sn.stateful_of_station(subsetIndices[a]);
270 for (std::size_t r = 0; r < K; ++r) subsetRt.push_back((isf - 1) * K + r);
272 for (std::size_t a = 0; a < complementIndices.size(); ++a) {
273 const std::size_t isf =
sn.stateful_of_station(complementIndices[a]);
274 for (std::size_t r = 0; r < K; ++r) complementRt.push_back((isf - 1) * K + r);
280 const std::size_t nSub = subsetIndices.size();
282 std::vector<int> mi(nSub, 1);
283 std::vector<bool> isDelay(nSub,
false);
284 for (std::size_t a = 0; a < nSub; ++a) {
286 for (std::size_t r = 0; r < K; ++r) subRates(a, r) =
sn.rates(subsetIndices[a] - 1, r);
287 isDelay[a] = (st.
nodetype == qn::NodeType::Delay || st.
sched == SchedStrategy::INF);
294 std::vector<std::vector<T>> scalingTable;
295 std::vector<Matrix<T>> Qtab_iso, Utab_iso;
296 std::string fesSource;
297 if (!options.throughputTable.empty()) {
298 scalingTable = detail::fes_measured_table<T>(options.throughputTable, cutoffs, K);
299 fesSource =
"measured";
300 }
else if (solver ==
"mva") {
309 Qtab_iso = std::move(st.
Qtab);
310 Utab_iso = std::move(st.
Utab);
318 std::vector<T> escape(K, zero);
319 for (std::size_t r = 0; r < K; ++r) {
320 for (std::size_t a = 0; a < nSub; ++a) {
321 const std::size_t j = subsetIndices[a];
322 const T Vjr = T(iso.
L(a, r) *
sn.rates(j - 1, r));
323 const std::size_t isf_j =
sn.stateful_of_station(j);
325 for (std::size_t b = 0; b < complementIndices.size(); ++b) {
326 const std::size_t isf_i =
sn.stateful_of_station(complementIndices[b]);
327 pexit +=
sn.rt((isf_j - 1) * K + r, (isf_i - 1) * K + r);
334 for (std::size_t idx = 0; idx < scalingTable[r].size(); ++idx)
335 scalingTable[r][idx] *= escape[r];
342 std::vector<std::size_t> complementNode(M + 1, 0);
343 for (std::size_t b = 0; b < complementIndices.size(); ++b) {
344 const std::size_t i = complementIndices[b];
347 if (st.
nodetype == qn::NodeType::Delay) {
349 }
else if (st.
nodetype == qn::NodeType::Queue) {
352 if (st.
cap > 0.0 && std::isfinite(st.
cap))
355 throw InputError(
ErrorKind{
"Argument"},
"aggregateFES: unsupported station type in the complement");
357 complementNode[i] = nd;
359 const std::size_t fesNode = fesModel.
add_queue(
"FES", SchedStrategy::PS);
365 const std::size_t refNode =
366 complementIndices.empty() ? fesNode : complementNode[complementIndices[0]];
367 std::vector<std::size_t> newClass(K, 0);
368 for (std::size_t r = 0; r < K; ++r)
373 for (std::size_t b = 0; b < complementIndices.size(); ++b) {
374 const std::size_t i = complementIndices[b];
375 for (std::size_t r = 0; r < K; ++r)
376 fesModel.
set_service(complementNode[i], newClass[r],
sn.service[i - 1][r]);
380 for (std::size_t r = 0; r < K; ++r)
386 for (std::size_t r = 0; r < K; ++r)
387 for (std::size_t idx = 0; idx < scalingTable[r].size(); ++idx)
389 scalingTable[r][idx] = floorVal;
397 const CdScaling<T> cd = [beta](
const std::vector<T>& n) {
398 std::vector<int> ni(n.size(), 0);
399 for (std::size_t r = 0; r < n.size(); ++r)
407 for (std::size_t r = 0; r < K; ++r) {
408 const std::size_t I = complementIndices.size() + 1;
409 std::vector<std::vector<T>> Pk(I + 1, std::vector<T>(I + 1, zero));
411 for (std::size_t b = 0; b < complementIndices.size(); ++b) {
412 const std::size_t i = complementIndices[b];
413 const std::size_t iNode = complementNode[i];
414 const std::size_t isf_i =
sn.stateful_of_station(i);
418 for (std::size_t c = 0; c < complementIndices.size(); ++c) {
419 const std::size_t j = complementIndices[c];
420 const std::size_t isf_j =
sn.stateful_of_station(j);
421 const T p =
sn.rt((isf_i - 1) * K + r, (isf_j - 1) * K + r);
425 for (std::size_t a = 0; a < nSub; ++a) {
426 const std::size_t isf_j =
sn.stateful_of_station(subsetIndices[a]);
427 const T p =
sn.rt((isf_i - 1) * K + r, (isf_j - 1) * K + r);
435 for (std::size_t c = 0; c < complementIndices.size(); ++c) {
436 const std::size_t j = complementIndices[c];
437 const std::size_t isf_j =
sn.stateful_of_station(j);
439 for (std::size_t a = 0; a < nSub; ++a) {
440 const std::size_t isf_i =
sn.stateful_of_station(subsetIndices[a]);
441 probSum += T(iso.
visits(a, r) *
sn.rt((isf_i - 1) * K + r, (isf_j - 1) * K + r));
444 Pk[fesNode][complementNode[j]] = probSum;
449 for (std::size_t nd = 1; nd <= I; ++nd) {
451 for (std::size_t md = 1; md <= I; ++md) rowSum += Pk[nd][md];
453 for (std::size_t md = 1; md <= I; ++md) Pk[nd][md] /= rowSum;
455 for (std::size_t nd = 1; nd <= I; ++nd)
456 for (std::size_t md = 1; md <= I; ++md)
458 P.set(newClass[r], newClass[r], nd, md, Pk[nd][md]);
464 out.
deagg.subsetIndices = subsetIndices;
465 out.
deagg.complementIndices = complementIndices;
466 out.
deagg.throughputTable = scalingTable;
467 out.
deagg.cutoffs = cutoffs;
468 out.
deagg.stochCompSubset = stochCompSubset;
469 out.
deagg.stochCompComplement = stochCompComplement;
470 out.
deagg.isolatedDemands = iso.
L;
472 out.
deagg.isolatedServers = mi;
473 out.
deagg.isolatedIsDelay = isDelay;
474 out.
deagg.escape = escape;
475 out.
deagg.fesNode = fesNode;
476 out.
deagg.solver = fesSource;
477 out.
deagg.isolatedQtable = Qtab_iso;
478 out.
deagg.isolatedUtable = Utab_iso;
488#ifndef LINE_SOLVERS_CTMC_SOLVER_CTMC_ANALYZER_H
Peak of a class-dependence handle over the reachable population lattice.
A network plus its refreshed NetworkStruct.
A queueing network under construction.
std::size_t add_delay(const std::string &nm)
An infinite-server station (a Delay, MATLAB's Delay / DelayStation).
void set_number_of_servers(std::size_t node, double n)
queue.setNumberOfServers(n).
std::size_t add_queue(const std::string &nm, SchedStrategy sched=SchedStrategy::FCFS)
A queueing station.
std::size_t add_closed_class(const std::string &nm, double njobs, std::size_t refstat_node, int prio=0)
A closed class of the given population, referencing a station node.
void set_class_dependence(std::size_t node, const CdScaling< T > &fun, const std::vector< T > &peak=std::vector< T >())
station.setClassDependence(beta, peakRatePerClass).
void set_capacity(std::size_t node, double k)
station.setCapacity(k), the K of Kendall's notation.
void link(const RoutingMatrix< T > &Pm)
model.link(P): install the routing.
void set_service(std::size_t node, std::size_t cls, const Distrib< T > &d)
station.setService(class, dist).
The routing matrix a model script fills in, MATLAB's P cell array.
Stochastic complement of a DTMC partition, a port of matlab/lib/kpctoolbox/mc/dtmc_stochcomp....
The exception types the port throws.
Wraps a flow-equivalent-server throughput table as a per-class class-dependence function beta_{i,...
Service demands and visit ratios of an isolated subnetwork, from the stochastic complement of its rou...
Per-class throughput table of an isolated subnetwork, tabulated over the population lattice,...
FES rates from a non-product-form solve of the isolated subnetwork.
Input validation for Flow-Equivalent Server (FES) aggregation.
Linearized index of a per-class population vector, for Limited Joint Dependence (LJD) tables.
Dense matrix and non-owning view.
FesBetaFun< T > fes_beta_handle(const std::vector< std::vector< T > > &scalingTable, const std::vector< int > &cutoffs)
Wraps a flow-equivalent-server throughput table as a per-class class-dependence function beta_{i,...
std::vector< std::vector< T > > fes_compute_throughputs(const Matrix< T > &L, const std::vector< int > &mi, const std::vector< bool > &isDelay, const std::vector< int > &cutoffs)
Per-class throughput table of an isolated subnetwork, tabulated over the population lattice,...
FesIsolated< T > fes_build_isolated(const Matrix< T > &rates, const Matrix< T > &stochCompS)
Build the isolated subnetwork's demands and visit ratios.
std::function< std::vector< T >(const std::vector< int > &)> FesBetaFun
Class-dependence handle: population vector -> per-class beta.
std::vector< int > ljd_delinearize(std::size_t idx, const std::vector< int > &cutoffs)
Inverse of ljd_linearize: the population vector behind an index.
FesValidateResult fes_validate(const qn::NetworkStruct< T > &sn, const std::vector< std::size_t > &subsetIndices)
Input validation for Flow-Equivalent Server (FES) aggregation.
FesSolvedTables< T > fes_solved_tables(const qn::NetworkStruct< T > &sn, const std::vector< std::size_t > &subsetIndices, const std::vector< Matrix< T > > &Pk, const Matrix< T > &visits, const std::vector< int > &cutoffs, const FesOptions &options)
fes_solved_tables of aggregateFES.m: the FES rates from a solve of the isolated subnetwork by options...
FesAggregateResult< T > fes_aggregate(const qn::NetworkStruct< T > &sn, const std::vector< std::size_t > &subsetIndices, const FesOptions &options=FesOptions())
Flow-equivalent-server aggregation: replace a station subset by one station.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
std::function< std::vector< T >(const std::vector< T > &)> CdScaling
A class-dependent scaling map, sn.cdscaling.
Matrix< T > dtmc_stochcomp(const Matrix< T > &P, const std::vector< std::size_t > &keep)
Stochastic complement of a DTMC partition, a port of matlab/lib/kpctoolbox/mc/dtmc_stochcomp....
T cd_peak_scaling(const std::function< std::vector< T >(const std::vector< int > &)> &beta, const std::vector< int > &NK)
Peak of a class-dependence handle over the reachable population lattice.
Conservation laws of a layered queueing network, enumerated from its structure.
The Network constructor API: Queue, Delay, Source, Sink, Router, ClassSwitch, Cache,...
A queueing network and its refreshed NetworkStruct.
Number-type abstraction for the templated API port.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
What fes_aggregate returns.
std::size_t fesNode
1-based node index of the FES station
FesAggregateResult(const qn::Network< T > &m)
Everything needed to map an FES result back onto the original model.
Matrix< T > isolatedDemands
(M_sub x K)
std::vector< Matrix< T > > isolatedQtable
Per 0-based linearized population state, the (M_sub x K) mean queue lengths and utilizations of the i...
std::vector< Matrix< T > > isolatedUtable
std::vector< std::size_t > subsetIndices
1-based, as given
std::vector< std::vector< T > > throughputTable
per class, linearized on the lattice
std::vector< std::size_t > complementIndices
1-based
Matrix< T > isolatedVisits
(M_sub x K)
Matrix< T > stochCompComplement
std::vector< bool > isolatedIsDelay
Matrix< T > stochCompSubset
std::vector< int > isolatedServers
std::size_t fesNode
1-based node index of the FES in the new model
std::vector< T > escape
per-class Norton escape factor
std::string solver
Source of the rates: the solver name, or 'measured' for a given throughputTable.
std::vector< int > cutoffs
Demands and visit ratios of the isolated subnetwork.
std::vector< Matrix< T > > Pk
Per class, the row-stochastic (M_sub x M_sub) routing of the isolated subnetwork, whose stationary ve...
Matrix< T > visits
(M_sub x K) visit ratios, each class summing to one
Matrix< T > L
(M_sub x K) service demands
options of the reference.
std::string solver
options.solver: how the isolated subnetwork is solved.
std::vector< std::vector< double > > throughputTable
options.throughputTable: measured FES rates, one vector per class of length prod(cutoffs+1) indexed b...
std::vector< int > cutoffs
per-class population bound; empty means sn.njobs
long seed
options.solverOptions.seed for the 'ssa', 'jmt' and 'ldes' solves.
What a non-product-form solve of the isolated subnetwork returns: FES rates and conditional metrics.
std::vector< Matrix< T > > Qtab
std::vector< Matrix< T > > Utab
per 0-based state, (M_sub x K)
std::vector< std::vector< T > > scalingTable
per class, linearized on the lattice
The reference's (isValid, errorMsg) pair.
static Distrib exp_rate(const T &r)
static constexpr double FineTol
One station of the network.
double cap
Station capacity in Kendall's K, as setCapacity sets it.
double nservers
may be infinite (a Delay, or an inf-scheduled task)