5#ifndef LINE_API_QSYS_QSYS_MTMSTM_DETS_H
6#define LINE_API_QSYS_QSYS_MTMSTM_DETS_H
65#include <initializer_list>
81 std::vector<double>
Fc;
82 std::vector<double>
cdf;
91[[noreturn]]
inline void input(
const char* kind,
const std::string& msg) {
103 const std::size_t nq1 =
static_cast<std::size_t
>(qbar) + 1;
104 const std::size_t n = nq1 * (
static_cast<std::size_t
>(rbar) + 1);
106 for (
int r = 0; r <= rbar; ++r) {
107 for (
int q = 0; q <= qbar; ++q) {
108 const std::size_t i =
static_cast<std::size_t
>(r) * nq1 +
static_cast<std::size_t
>(q);
111 out = s * mu + q * theta;
112 if (q > 0) Qp(i, i - 1) = out;
114 out = (s + r) * mu + q * theta;
115 Qp(i, i - nq1) = (s + r) * mu;
116 if (q > 0) Qp(i, i - 1) = q * theta;
122 for (std::size_t i = 0; i < n; ++i) Qa(i, i) = Qp(i, i) - theta;
126inline std::vector<double>
dets_jump(
const std::vector<double>& v,
int qbar,
int rbar,
int add,
int rem,
127 const std::string& policy,
int&
qn,
int& rn) {
128 const int net = add - rem;
129 const int nq1 = qbar + 1;
130 if (policy ==
"pe") {
133 }
else if (policy ==
"ec") {
136 }
else if (net >= 0) {
137 qn = qbar - std::max(net - rbar, 0);
138 rn = std::max(rbar - net, 0);
143 if (
qn < 0)
return std::vector<double>();
144 std::vector<double> vnew(
static_cast<std::size_t
>(
qn + 1) *
static_cast<std::size_t
>(rn + 1), 0.0);
145 for (
int r = 0; r <= rbar; ++r) {
146 for (
int q = 0; q <= qbar; ++q) {
147 const double w = v[
static_cast<std::size_t
>(r * nq1 + q)];
148 if (w == 0.0)
continue;
150 if (policy ==
"pe") {
153 }
else if (policy ==
"ec") {
156 }
else if (net >= 0) {
157 q2 = q - std::max(net - r, 0);
158 r2 = std::max(r - net, 0);
164 const std::size_t j =
static_cast<std::size_t
>(r2 * (
qn + 1) + q2);
165 vnew[j] = vnew[j] + w;
172inline double vsum(
const std::vector<double>& x) {
174 for (
double v : x) s += v;
184 const std::vector<double>& tb,
const std::vector<char>& alive,
double theta,
int sK,
186 const std::size_t K = tb.size() - 1;
188 for (std::size_t k = 0; k <= K; ++k) {
189 if (!alive[k])
break;
191 const std::size_t n = Q.
rows();
192 const double f = std::exp(-theta * tb[k]);
193 std::vector<double> w(v[k].size());
195 for (std::size_t i = 0; i < w.size(); ++i) {
197 if (w[i] != 0.0) any =
true;
201 const double d = tb[k + 1] - tb[k];
203 for (std::size_t i = 0; i < n; ++i) {
204 for (std::size_t j = 0; j < n; ++j) B(i, j) = Q(i, j) * d;
209 for (std::size_t i = 0; i < n; ++i) acc += w[i] * E(i, n);
211 }
else if (sK == 0 && kill == 0.0) {
212 m = std::numeric_limits<double>::infinity();
215 for (std::size_t i = 0; i < n; ++i)
216 for (std::size_t j = 0; j < n; ++j) negQ(i, j) = -Q(i, j);
217 const std::vector<double> x =
line::solve(negQ, std::vector<double>(n, 1.0));
219 for (std::size_t i = 0; i < n; ++i) acc += w[i] * x[i];
226inline std::string
lower(
const std::string& s) {
228 for (
char& c : o) c =
static_cast<char>(std::tolower(
static_cast<unsigned char>(c)));
234 const std::vector<int>& add,
const std::vector<int>& rem,
int nq,
235 const std::vector<double>& tau,
const std::string& pol,
const std::string& wt) {
236 const std::size_t K = tk.size();
237 std::vector<int> s(K);
239 for (std::size_t k = 0; k < K; ++k)
240 if (add[k] < 0 || rem[k] < 0)
input(
"Argument",
"The staffing changes must be integers.");
241 for (std::size_t k = 0; k < K; ++k) {
242 cur +=
static_cast<long long>(add[k]) - rem[k];
243 if (cur < 0)
input(
"Argument",
"The staffing level becomes negative.");
244 s[k] =
static_cast<int>(cur);
246 bool tok = !tau.empty();
247 for (
double v : tau) tok = tok && std::isfinite(v) && v >= 0.0;
248 if (!tok)
input(
"Argument",
"TAU must be a non-empty vector of finite nonnegative times.");
251 std::vector<int> sk(K + 1);
252 std::vector<double> tb(K + 1);
255 for (std::size_t k = 0; k < K; ++k) {
259 std::vector<int> qbar(K + 1, 0), rbar(K + 1, 0);
261 std::vector<std::vector<double>> v(K + 1);
262 v[0].assign(
static_cast<std::size_t
>(nq) + 1, 0.0);
264 std::vector<Matrix<double>> Qp(K + 1), Qa(K + 1);
266 std::vector<char> alive(K + 1, 1);
267 for (std::size_t k = 0; k < K; ++k) {
276 v[k + 1] =
dets_jump(vend, qbar[k], rbar[k], add[k], rem[k], pol,
qn, rn);
288 res.
Fc.assign(tau.size(), 0.0);
289 for (std::size_t i = 0; i < tau.size(); ++i) {
291 for (std::size_t j = 0; j <= K; ++j)
292 if (tb[j] <= tau[i]) k = j;
306 res.
cdf.resize(res.
Fc.size());
307 for (std::size_t i = 0; i < res.
Fc.size(); ++i) res.
cdf[i] = 1.0 - res.
Fc[i];
314inline std::array<std::string, 2>
check_common(
double mu,
double theta,
int s0,
const std::vector<double>& tk,
315 int nq,
const std::string& policy,
const std::string& wait) {
316 const std::string wt =
lower(wait);
317 if (wt !=
"pwt" && wt !=
"awt")
input(
"Option",
"options.wait must be 'pwt' or 'awt'.");
318 const std::string pol =
lower(policy);
319 if (pol !=
"pe" && pol !=
"ec" && pol !=
"eh")
input(
"Argument",
"POLICY must be 'pe', 'ec' or 'eh'.");
320 if (!std::isfinite(mu) || !(mu > 0.0))
input(
"Argument",
"MU must be a positive finite rate.");
321 if (!std::isfinite(theta) || theta < 0.0)
input(
"Argument",
"THETA must be a nonnegative finite rate.");
322 if (s0 < 0)
input(
"Argument",
"S0 must be a nonnegative integer.");
323 if (nq < 0)
input(
"Argument",
"NQ must be a nonnegative integer.");
324 for (std::size_t k = 0; k < tk.size(); ++k) {
325 if (!std::isfinite(tk[k]) || !(tk[k] > 0.0) || (k > 0 && !(tk[k] - tk[k - 1] > 0.0)))
326 input(
"Argument",
"TK must be strictly increasing, finite and positive.");
350 const std::vector<int>& dsk,
int nq,
const std::vector<double>& tau,
351 const std::string& policy,
const std::string& wait =
"pwt") {
353 const std::size_t K = tk.size();
354 if (K == 0 && !dsk.empty())
detail_dets::input(
"Argument",
"DSK must be empty when TK is.");
357 "DSK must have one entry per change time, or be a K-by-2 [added, removed] matrix.");
358 std::vector<int> add(K), rem(K);
359 for (std::size_t k = 0; k < K; ++k) {
360 add[k] = std::max(dsk[k], 0);
361 rem[k] = std::max(-dsk[k], 0);
372 const std::vector<std::array<int, 2>>& dsk,
int nq,
373 const std::vector<double>& tau,
const std::string& policy,
374 const std::string& wait =
"pwt") {
376 const std::size_t K = tk.size();
377 if (K == 0 && !dsk.empty())
detail_dets::input(
"Argument",
"DSK must be empty when TK is.");
380 "DSK must have one entry per change time, or be a K-by-2 [added, removed] matrix.");
381 std::vector<int> add(K), rem(K);
382 for (std::size_t k = 0; k < K; ++k) {
395 std::initializer_list<int> dsk,
int nq,
const std::vector<double>& tau,
396 const std::string& policy,
const std::string& wait =
"pwt") {
397 return qsys_mtmstm_dets(mu, theta, s0, tk, std::vector<int>(dsk), nq, tau, policy, wait);
The exception types the port throws.
Matrix exponential by scaling and squaring with a diagonal Pade approximant.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
void dets_generator(int qbar, int rbar, int s, double mu, double theta, Matrix< double > &Qp, Matrix< double > &Qa)
Transient generator over (q, r), q = 0..qbar, r = 0..rbar, index r*(qbar+1) + q.
std::array< std::string, 2 > check_common(double mu, double theta, int s0, const std::vector< double > &tk, int nq, const std::string &policy, const std::string &wait)
The checks MATLAB runs before splitting DSK, in its order; returns {policy, wait} lower-cased.
DetsResult dets_core(double mu, double theta, int s0, const std::vector< double > &tk, const std::vector< int > &add, const std::vector< int > &rem, int nq, const std::vector< double > &tau, const std::string &pol, const std::string &wt)
The common body once the changes are split into added and removed servers.
std::string lower(const std::string &s)
void input(const char *kind, const std::string &msg)
double vsum(const std::vector< double > &x)
std::vector< double > dets_jump(const std::vector< double > &v, int qbar, int rbar, int add, int rem, const std::string &policy, int &qn, int &rn)
The staffing change at t_k applied to the row vector v over (q, r); qn < 0 means every state was abso...
double dets_mean(const std::vector< std::vector< double > > &v, const std::vector< Matrix< double > > &Qs, const std::vector< double > &tb, const std::vector< char > &alive, double theta, int sK, double kill)
Integral over [0, inf) of v_k exp(Q_k (x - t_k)) e exp(-theta t_k) on each piece: a finite piece by t...
DetsResult qsys_mtmstm_dets(double mu, double theta, int s0, const std::vector< double > &tk, const std::vector< int > &dsk, int nq, const std::vector< double > &tau, const std::string &policy, const std::string &wait="pwt")
DETS waiting-time CCDF with the staffing changes given as net signed changes.
Conservation laws of a layered queueing network, enumerated from its structure.
std::vector< T > vecmul(const std::vector< T > &v, const Matrix< T > &A)
Row vector times matrix, v A.
std::vector< T > solve(const Matrix< T > &A, const std::vector< T > &b)
Convenience: solve Ax = b, leaving A and b untouched.
Matrix< T > expm(const Matrix< T > &A)
Matrix exponential exp(A).
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
std::vector< double > Fc
P(W > tau) at each tau.
double poa
probability of abandonment, theta * meanAWT
std::vector< double > cdf
1 - Fc
std::vector< int > s
staffing level after each change
double meanAWT
E[min(W, A)].
double meanPWT
E[W]; Inf when the final staffing is 0 and theta = 0.