65inline QsysRes qsys_mg1kk(
double alpha,
unsigned K,
const std::function<
double(
double)>& lst,
double ES,
66 bool means_only =
false) {
68 if (!(alpha > 0.0))
throw Error(
"qsys_mg1kk: alpha must be a positive real scalar");
69 if (K < 1)
throw Error(
"qsys_mg1kk: K must be a positive integer");
70 if (!lst)
throw Error(
"qsys_mg1kk: svc must be a Distribution or an LST function handle");
71 if (!(ES > 0.0))
throw Error(
"qsys_mg1kk: with an LST handle the mean service time ES must be given");
73 std::vector<double> Bs(K + 1);
74 for (
unsigned k = 0; k <= K; ++k) Bs[k] = lst(k * alpha);
75 double Bn = 1.0, S = 1.0;
77 const std::vector<double> cK = binom_row(K - 1);
78 for (
unsigned nn = 1; nn + 1 <= K; ++nn) {
79 Bn = Bn * (1.0 - Bs[nn]) / Bs[nn];
83 const double P0 = 1.0 / (1.0 + K * alpha * ES * S);
84 const double X = (1.0 - P0) / ES;
85 const double ET = K / X - 1.0 / alpha;
86 const double EW = ET - ES;
87 const double nan = std::numeric_limits<double>::quiet_NaN();
91 r.
set(
"meanQueueLength", X * ET);
92 r.
set(
"varQueueLength", nan);
93 r.
set(
"meanQueueLengthQ", X * EW);
94 r.
set(
"meanWaitingTime", EW);
95 r.
set(
"meanSojournTime", ET);
96 r.
set(
"throughput", X);
97 r.
set(
"utilization", 1.0 - P0);
98 r.
set(
"meanOperating", K - X * ET);
99 r.
set(
"queueLengthDist", std::vector<double>(K + 1, nan));
100 r.
set(
"arrivalDist", std::vector<double>(K, nan));
101 r.
analyzer =
"exact:M/G/1/" + std::to_string(K) +
"/" + std::to_string(K);
102 if (means_only)
return r;
105 std::vector<std::vector<double>> C(K);
106 for (
unsigned n = 0; n < K; ++n) C[n] = binom_row(n);
108 for (
unsigned j = 0; j < K; ++j) {
109 const unsigned nop = j == 0 ? K - 1 : K - j;
110 const unsigned base = j == 0 ? 0 : j - 1;
111 for (
unsigned i = 0; i <= nop; ++i) {
113 for (
unsigned l = 0; l <= i; ++l)
114 acc += ((l % 2) ? -1.0 : 1.0) * C[i][l] * Bs[nop - i + l];
115 P(j, base + i) += C[nop][i] * acc;
118 for (
unsigned j = 0; j < K; ++j) {
120 for (
unsigned i = 0; i < K; ++i) {
121 P(j, i) = std::max(P(j, i), 0.0);
124 for (
unsigned i = 0; i < K; ++i) P(j, i) /= rs;
128 std::vector<double> b(K, 0.0);
129 for (
unsigned row = 0; row + 1 < K; ++row)
130 for (
unsigned cc = 0; cc < K; ++cc) A(row, cc) = P(cc, row) - (row == cc ? 1.0 : 0.0);
131 for (
unsigned cc = 0; cc < K; ++cc) A(K - 1, cc) = 1.0;
133 std::vector<double> Pi =
solve(A, b);
135 std::vector<double> pn(K + 1, 0.0);
137 for (
unsigned n = 0; n < K; ++n) {
138 pn[n] = X * Pi[n] / ((
static_cast<double>(K) - n) * alpha);
142 if (std::fabs(pn[0] - P0) > 1e-6 * std::max(P0, 1e-12) || pn[K] < -1e-9) {
144 std::snprintf(buf,
sizeof buf,
145 "qsys_mg1kk: the embedded-chain distribution disagrees with the Takacs P_0 (%g vs %g): "
146 "the alternating arrival sums lost accuracy at K=%u; queueLengthDist, arrivalDist "
147 "and varQueueLength are returned as NaN, the means are unaffected.",
152 std::cerr <<
"[LINE] Warning: " << buf << std::endl;
153 std::fill(pn.begin(), pn.end(), nan);
154 std::fill(Pi.begin(), Pi.end(), nan);
156 pn[K] = std::max(pn[K], 0.0);
158 double EN = 0.0, EN2 = 0.0;
159 for (
unsigned n = 0; n <= K; ++n) {
161 EN2 +=
static_cast<double>(n) * n * pn[n];
163 r.
set(
"varQueueLength", EN2 - EN * EN);
164 r.
set(
"queueLengthDist", pn);
165 r.
set(
"arrivalDist", Pi);
std::vector< double > binom_row(unsigned n)
The row C(n, 0..n) by the multiplicative recurrence C(n,k) = C(n,k-1)(n-k+1)/k, each entry rounded to...
QsysRes qsys_mg1kk(double alpha, unsigned K, const std::function< double(double)> &lst, double ES, bool means_only=false)
Exact M/G/1/K/K finite-source queue, service given by its LST.
std::vector< T > solve(const Matrix< T > &A, const std::vector< T > &b)
Convenience: solve Ax = b, leaving A and b untouched.