5#ifndef LINE_SOLVERS_MAM_SOLVER_MAM_DT_H
6#define LINE_SOLVERS_MAM_SOLVER_MAM_DT_H
56void assert_scope(
const qn::NetworkStruct<T>& sn) {
60 "SolverMAM: the discrete-time path supports open models only. A closed slotted model "
61 "needs a level-dependent discrete chain, which the Q-MAM discrete-time catalogue does "
65 "SolverMAM: the discrete-time path supports one class only. Independent per-class "
66 "lattice sources fire in the same slot with positive probability, and a batch of "
67 "simultaneous arrivals of different classes is not an MMAP[K], which is what "
68 "Q_DT_MMAPK_PHK_1 consumes.");
69 for (std::size_t ist = 0; ist < sn.nstations; ++ist) {
71 if (sched == SchedStrategy::EXT)
continue;
72 if (sched != SchedStrategy::FCFS)
74 "SolverMAM: the discrete-time path supports FCFS single-server stations and the "
76 if (sn.stations[ist].nservers > 1)
78 "SolverMAM: the discrete-time path models one server per station; a slotted "
79 "multiserver queue needs the level-dependent boundary of Geo/Geo/c.");
92Dmap<T> dmap_stretch(
const Dmap<T>& D, std::size_t m) {
94 const T
zero = num_traits<T>::from_int(0);
95 const T one = num_traits<T>::from_int(1);
96 const std::size_t n = D.D0.rows();
100 for (std::size_t i = 0; i < n; ++i) {
101 const std::size_t base = i * m;
102 for (std::size_t s = 0; s + 1 < m; ++s) out.D0(base + s, base + s + 1) = one;
103 for (std::size_t j = 0; j < n; ++j) {
104 out.D0(base + m - 1, j * m) = D.D0(i, j);
105 out.D1(base + m - 1, j * m) = D.D1(i, j);
116double coarsest_slot(
const qn::NetworkStruct<T>& sn,
double slot_length) {
119 for (std::size_t ist = 0; ist < sn.nstations; ++ist)
120 for (std::size_t r = 0; r < sn.nclasses; ++r) {
122 const double rate = num_traits<T>::to_double(sn.rates(ist, r));
123 if (pt == ProcessType::DISABLED || pt == ProcessType::NONE || sn.disabled[ist][r] ||
124 !std::isfinite(rate) || rate <= 0)
126 const long k = pt == ProcessType::DET ? std::lround(1.0 / (rate * slot_length))
127 : std::lround(1.0 / slot_length);
130 return g > 1 ? slot_length *
static_cast<double>(g) : slot_length;
144Dmap<T> station_law(
const qn::NetworkStruct<T>& sn, std::size_t ist, std::size_t r,
145 double slot_length) {
148 const double mean_time = 1.0 / num_traits<T>::to_double(sn.rates(ist, r));
149 const std::size_t unit_slots =
static_cast<std::size_t
>(std::lround(1.0 / slot_length));
150 if (pt == ProcessType::DMAP) {
153 d.D0 = sn.service[ist][r].D0;
154 d.D1 = sn.service[ist][r].D1;
155 return dmap_stretch(d, unit_slots);
157 if (pt == ProcessType::DET)
159 dph_from_dist<T>(pt, num_traits<T>::from_double(mean_time / slot_length), sn.scv(ist, r)));
167std::vector<T> solve_single_station(
const Dmap<T>& arv,
const Dmap<T>& svc,
168 std::size_t max_num_comp) {
182Matrix<T> routing(
const qn::NetworkStruct<T>& sn, std::size_t source_idx,
183 const std::vector<std::size_t>& queue_idx) {
185 const T
zero = num_traits<T>::from_int(0);
186 const std::size_t I = sn.nof_nodes(), K = sn.nclasses;
187 Matrix<T> Pn(I, I, zero);
188 for (std::size_t a = 0; a < I; ++a)
189 for (std::size_t b = 0; b < I; ++b) Pn(a, b) = sn.rtnodes(a * K, b * K);
191 std::vector<std::size_t> sink_nodes;
192 for (std::size_t ind = 0; ind < I; ++ind)
193 if (sn.nodes[ind].nodetype == NodeType::Sink) {
194 sink_nodes.push_back(ind);
195 for (std::size_t b = 0; b < I; ++b) Pn(ind, b) =
zero;
198 std::vector<std::size_t> station_nodes;
199 station_nodes.push_back(sn.station_to_node[source_idx] - 1);
200 for (std::size_t idx = 0; idx < queue_idx.size(); ++idx)
201 station_nodes.push_back(sn.station_to_node[queue_idx[idx]] - 1);
203 std::vector<std::size_t> inter_nodes;
204 for (std::size_t ind = 0; ind < I; ++ind) {
205 const bool is_station =
206 std::find(station_nodes.begin(), station_nodes.end(), ind) != station_nodes.end();
207 const bool is_sink = std::find(sink_nodes.begin(), sink_nodes.end(), ind) != sink_nodes.end();
208 if (!is_station && !is_sink) inter_nodes.push_back(ind);
211 auto sub = [&](
const std::vector<std::size_t>& rows,
const std::vector<std::size_t>& cols) {
212 Matrix<T> out(rows.size(), cols.size(), zero);
213 for (std::size_t a = 0; a < rows.size(); ++a)
214 for (std::size_t b = 0; b < cols.size(); ++b) out(a, b) = Pn(rows[a], cols[b]);
218 const std::size_t ns = station_nodes.size();
219 Matrix<T> full = sub(station_nodes, station_nodes);
220 if (!inter_nodes.empty()) {
221 Matrix<T> Psn = sub(station_nodes, inter_nodes);
222 Matrix<T> Pnn = sub(inter_nodes, inter_nodes);
223 Matrix<T> Pns = sub(inter_nodes, station_nodes);
224 Matrix<T> Inn = detail::dt_eye<T>(inter_nodes.size());
225 Matrix<T>
inv = detail::dt_left_solve(detail::dt_sub(Inn, Pnn), Inn);
226 full = detail::dt_add(full, detail::dt_mul(detail::dt_mul(Psn, inv), Pns));
229 Matrix<T>
P(ns, ns - 1, zero);
230 for (std::size_t a = 0; a < ns; ++a)
231 for (std::size_t b = 1; b < ns; ++b)
P(a, b - 1) = full(a, b);
238 std::size_t idx, std::size_t space_max) {
241 if (num_traits<T>::to_double(
P(0, idx)) > 0) {
245 for (std::size_t j = 0; j < dep.size(); ++j) {
246 const T p =
P(1 + j, idx);
247 if (num_traits<T>::to_double(p) <= 0)
continue;
259 "SolverMAM: a queue receives no arrivals in the discrete-time routing matrix.");
265T visit_ratio(
const qn::NetworkStruct<T>& sn, std::size_t ist) {
266 T v = num_traits<T>::from_int(0);
267 for (std::size_t c = 0; c < sn.visits.size(); ++c) {
268 const Matrix<T>& vm = sn.visits[c];
269 if (ist >= vm.rows())
continue;
270 for (std::size_t r = 0; r < vm.cols(); ++r) v = v + vm(ist, r);
283 double slot_length) {
286 dt_detail::assert_scope(
sn);
288 const std::size_t M =
sn.nstations, K =
sn.nclasses;
292 slot_length = dt_detail::coarsest_slot(
sn, slot_length);
293 std::vector<Dmap<T>> law(M);
294 for (std::size_t ist = 0; ist < M; ++ist) law[ist] = dt_detail::station_law(
sn, ist, 0, slot_length);
296 std::size_t source_idx = M;
297 std::vector<std::size_t> queue_idx;
298 for (std::size_t ist = 0; ist < M; ++ist) {
299 if (
sn.stations[ist].sched == SchedStrategy::EXT)
302 queue_idx.push_back(ist);
305 throw UnsupportedError(
ErrorKind{
"ClosedClass"},
"SolverMAM: the discrete-time path requires an open model with a Source.");
307 const std::size_t nq = queue_idx.size();
308 std::vector<T> QN(nq, zero), UN(nq, zero), TN(nq, zero);
313 src_batch.push_back(law[source_idx].D0);
314 src_batch.push_back(law[source_idx].D1);
318 const std::vector<T> ql =
319 dt_detail::solve_single_station(law[source_idx], law[queue_idx[0]], 1000);
320 for (std::size_t i = 0; i < ql.size(); ++i)
328 Matrix<T> P = dt_detail::routing(
sn, source_idx, queue_idx);
329 std::vector<DBatch<T>> dep(nq);
330 for (std::size_t idx = 0; idx < nq; ++idx) {
331 const T p = lambda_slot * dt_detail::visit_ratio(
sn, queue_idx[idx]);
338 std::vector<T> QNprev(nq, zero), QNprev2(nq, zero), UNprev(nq, zero), TNprev(nq, zero);
339 const int iter_max =
opt.iter_max > 0 ?
opt.iter_max : 100;
340 const double iter_tol =
opt.tol > 0 ?
opt.tol : 1e-3;
341 auto max_rel = [&](
const std::vector<T>& a,
const std::vector<T>& b) {
343 for (std::size_t i = 0; i < a.size(); ++i) {
350 for (
int it = 1; it <= iter_max; ++it) {
352 for (std::size_t idx = 0; idx < nq; ++idx) {
353 DBatch<T> arv = dt_detail::arrivals(src_batch, dep, P, idx,
opt.space_max);
359 d.push_back(r.
dep.D0);
360 d.push_back(r.
dep.D1);
363 if (it > 1 && max_rel(QN, QNprev) < iter_tol)
break;
364 if (it > 2 && max_rel(QN, QNprev2) < iter_tol) {
371 for (std::size_t idx = 0; idx < nq; ++idx) {
372 QN[idx] = (QN[idx] + QNprev[idx]) * half;
373 UN[idx] = (UN[idx] + UNprev[idx]) * half;
374 TN[idx] = (TN[idx] + TNprev[idx]) * half;
396 for (std::size_t idx = 0; idx < nq; ++idx) {
397 const std::size_t ist = queue_idx[idx];
398 s.
Q(ist, 0) = QN[idx];
399 s.
U(ist, 0) = UN[idx];
400 s.
Tp(ist, 0) = TN[idx] / slot;
403 const T lambda = lambda_slot / slot;
404 s.
Tp(source_idx, 0) = lambda;
407 for (std::size_t idx = 0; idx < nq; ++idx) qtot = qtot + QN[idx];
408 s.
C[0] = qtot / lambda;
UnsupportedError(const std::string &what)
A network plus its refreshed NetworkStruct.
Discrete-time (slotted) matrix-analytic primitives and queues.
The exception types the port throws.
Enumerations and the minimal distribution descriptor shared by the model layer of the C++ port.
The option and result types SolverMAM shares with its analyzers.
bool sn_is_open_model(const qn::NetworkStruct< T > &sn)
all(isinf(sn.njobs)): EVERY class is open, which a mixed model fails.
SchedStrategy
Scheduling disciplines, with the values of MATLAB SchedStrategy.
ProcessType
Distribution kinds, with the values of MATLAB ProcessType.
NodeType
Node kinds, with the values of MATLAB NodeType.
DBatch< T > dmap_compress_batch(const DBatch< T > &A, std::size_t max_order)
Order reduction of a BATCH stream.
std::vector< T > q_dt_map_map_1(const Dmap< T > &arv, const Dmap< T > &svc, std::size_t max_num_comp=1000)
Queue length distribution of a discrete-time D-MAP/D-MAP/1/FCFS queue, the queue-length half of Q_DT_...
std::vector< Matrix< T > > DBatch
A discrete batch arrival stream, entry k carrying the slots with k events.
T dmap_lambda_batch(const DBatch< T > &A)
Mean number of EVENTS per slot, pi sum_k k A_k e.
std::vector< T > q_dt_ph_ph_1(const Dph< T > &arv, const Dph< T > &svc, std::size_t max_num_comp=1000)
Queue length of a discrete-time DPH/DPH/1/FCFS queue, via the D-MAP route.
DtQueueResult< T > mg1_dt_queue(const DBatch< T > &arv, const Dmap< T > &svc, std::size_t max_num_comp=1000, bool want_departure=false)
Discrete-time single-server queue with batch D-MAP arrivals, DBMAP/D-MAP/1.
Dph< T > dph_from_dist(lang::ProcessType type, const T &mean_slots, const T &scv)
Exact discrete phase-type representation of a lattice-valued law.
Dmap< T > dph_to_dmap(const Dph< T > &d)
Renewal D-MAP (A, a alpha) of a discrete phase-type law.
MamSolution< T > solver_mam_dt(const qn::NetworkStruct< T > &sn, const MamOptions &opt, double slot_length)
Discrete-time analysis of sn, returning the same metric tuple as every other MAM analyzer.
Dph< T > dmap_to_dph(const Dmap< T > &d)
Discrete phase-type law underlying a renewal D-MAP.
DBatch< T > dmap_super(const DBatch< T > &A, const DBatch< T > &B)
Superposition, E_k = sum_{i+j=k} kron(A_i, B_j).
bool dmap_is_renewal(const Dmap< T > &d)
True when D1 has rank one, i.e.
DBatch< T > dmap_thin(const DBatch< T > &A, const T &p)
Bernoulli thinning, B_k = sum_{n>=k} C(n,k) p^k (1-p)^(n-k) A_n.
Conservation laws of a layered queueing network, enumerated from its structure.
A queueing network and its refreshed NetworkStruct.
Port of matlab/src/api/sn/sn_is_discrete_time.m.
Ports of the sn_has_* / sn_is_* predicate family of matlab/src/api/sn.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
Outcome of a slotted station solve.
The options SolverMAM reads.
What the MAM dispatch returns: the metrics plus the algorithm that ran.
std::string actualmethod
The concrete algorithm, as the reference's actualmethod.
mva::MvaSolution< T > sol
Class-level results, the [Q,U,R,T,C,X] of the MATLAB analyzers.