5#ifndef LINE_API_QSYS_QSYS_SIZEQUAD_H
6#define LINE_API_QSYS_QSYS_SIZEQUAD_H
50#include <boost/math/special_functions/gamma.hpp>
62inline std::vector<Comp>
sizefit(
double mu,
double cs) {
63 const double c2 = cs * cs, m = 1.0 / mu;
64 if (cs == 0.0)
return {{1.0, 0, m}};
65 if (std::abs(c2 - 1.0) < 1e-9)
return {{1.0, 1, mu}};
67 const double p = 0.5 * (1.0 + std::sqrt((c2 - 1.0) / (c2 + 1.0)));
68 return {{p, 1, 2.0 * p * mu}, {1.0 - p, 1, 2.0 * (1.0 - p) * mu}};
70 const int k =
static_cast<int>(std::ceil(1.0 / c2));
71 const double p = (1.0 / (1.0 + c2)) * (k * c2 - std::sqrt(k * (1.0 + c2) -
double(k) * k * c2));
72 const double a = (k - p) / m;
73 if (p > 0)
return {{p, k - 1, a}, {1.0 - p, k, a}};
77inline double gq(
double n,
double z) {
return z <= 0.0 ? 1.0 : boost::math::gamma_q(n, z); }
78inline double gp(
double n,
double z) {
return z <= 0.0 ? 0.0 : boost::math::gamma_p(n, z); }
82static const double kGlx[10] = {0.07652652113349734, 0.22778585114164507, 0.37370608871541955,
83 0.5108670019508271, 0.636053680726515, 0.7463319064601508,
84 0.8391169718222188, 0.912234428251326, 0.9639719272779138,
86static const double kGlw[10] = {0.15275338713072628, 0.14917298647260424, 0.1420961093183824,
87 0.1316886384491769, 0.1181945319615186, 0.1019301198172407,
88 0.08327674157670471, 0.06267204833410879, 0.040601429800386446,
89 0.017614007139150893};
91inline std::array<double, 3>
window(
double n,
double a) {
92 const double sd = std::sqrt(n) / a, mn = n / a;
93 return {std::max(0.0, mn - 15.0 * sd), mn + 15.0 * sd + 40.0 / a, sd};
97inline double rho_minus(
const std::vector<Comp>& comps,
double t) {
99 for (
const Comp& c : comps) {
101 if (c.a < t) r += c.w * c.a;
103 r += c.w * c.n / c.a *
gp(c.n + 1.0, c.a * t);
110inline double gl_sum(
const std::vector<Comp>& comps,
double u,
double v) {
111 const double hw = (v - u) / 2.0, ctr = (u + v) / 2.0;
113 for (
int g = 0; g < 10; ++g) {
127 std::vector<double>
P,
C;
129 explicit ResGrid(
const std::vector<Comp>& comps) {
130 std::vector<double> bp{0.0};
131 std::vector<std::array<double, 3>> erl;
132 for (
const Comp& c : comps) {
136 const auto w =
window(c.n, c.a);
142 std::sort(bp.begin(), bp.end());
143 bp.erase(std::unique(bp.begin(), bp.end()), bp.end());
145 for (std::size_t s = 0; s + 1 < bp.size(); ++s) {
146 const double u = bp[s], v = bp[s + 1];
147 double sc = std::numeric_limits<double>::infinity();
148 for (
const auto& e : erl)
149 if (e[0] < v && e[1] > u) sc = std::min(sc, e[2]);
150 const int npan = std::isinf(sc) ? 1 : std::max(1,
static_cast<int>(std::ceil((v - u) / sc)));
151 for (
int p = 1; p <= npan; ++p)
P.push_back(u + (v - u) * p / npan);
153 C.assign(
P.size(), 0.0);
154 for (std::size_t i = 1; i <
P.size(); ++i)
C[i] =
C[i - 1] +
gl_sum(comps,
P[i - 1],
P[i]);
158 double residence(
const std::vector<Comp>& comps,
double x)
const {
159 if (!(x > 0.0))
return 0.0;
161 while (i + 2 <
P.size() &&
P[i + 1] < x) ++i;
162 return C[i] +
gl_sum(comps,
P[i], x);
167 double I0 = 0, I1 = 0, M1m = 0, M1p = 0, M2p = 0, Tl = 0;
169 for (
const Comp& c : comps) {
170 const double lw = c.w, a = c.a;
172 const double t = std::min(x, a);
174 I1 += lw * t * t / 2.0;
175 if (a < x) M1m += lw * a;
181 W0 += lw * a * a / 2.0;
183 const double n = c.n, z = a * x;
184 const double Q =
gq(n, z), p1 =
gp(n + 1, z), p2 =
gp(n + 2, z);
185 I0 += lw * (x * Q + n / a * p1);
186 I1 += lw * (x * x / 2.0 * Q + n * (n + 1) / (2.0 * a * a) * p2);
187 M1m += lw * n / a * p1;
188 M1p += lw * n / a * p1;
189 M2p += lw * n * (n + 1) / (a * a) * p2;
191 W0 += lw * n * (n + 1) / (2.0 * a * a);
194 if (mode ==
Mode::FB)
return I1 / ((1.0 - I0) * (1.0 - I0)) + x / (1.0 - I0);
195 if (mode ==
Mode::SJF)
return W0 / ((1.0 - M1m) * (1.0 - M1p));
196 if (mode ==
Mode::SJFX)
return x * W0 / ((1.0 - M1m) * (1.0 - M1p));
198 return (M2p + x * x * Tl) / (2.0 * (1.0 - M1m) * (1.0 - M1p)) + grid->
residence(comps, x);
199 return x / (1.0 - M1m) + M2p / (2.0 * (1.0 - M1m) * (1.0 - M1p));
202inline double outer(
Mode mode,
const std::vector<Comp>& comps,
int n,
double a,
const ResGrid* grid) {
203 const double* glx =
kGlx;
204 const double* glw =
kGlw;
205 const auto win =
window(n, a);
206 const double lo = win[0], hi = win[1];
207 std::vector<double> bp{lo, hi};
208 std::vector<std::array<double, 3>> erl;
209 for (
const Comp& c : comps) {
211 if (c.a > lo && c.a < hi) bp.push_back(c.a);
213 const auto w =
window(c.n, c.a);
215 if (w[0] > lo && w[0] < hi) bp.push_back(w[0]);
216 if (w[1] > lo && w[1] < hi) bp.push_back(w[1]);
219 std::sort(bp.begin(), bp.end());
220 bp.erase(std::unique(bp.begin(), bp.end()), bp.end());
221 const double lg = std::lgamma(
double(n));
223 for (std::size_t s = 0; s + 1 < bp.size(); ++s) {
224 const double u = bp[s], v = bp[s + 1];
225 double sc = std::numeric_limits<double>::infinity();
226 for (
const auto& e : erl)
227 if (e[0] < v && e[1] > u) sc = std::min(sc, e[2]);
228 const int npan = std::max(1,
static_cast<int>(std::ceil((v - u) / sc)));
229 const double h = (v - u) / npan, hw = h / 2.0;
230 for (
int p = 0; p < npan; ++p) {
231 const double ctr = u + (p + 0.5) * h;
232 for (
int g = 0; g < 10; ++g) {
233 for (
int sgn = -1; sgn <= 1; sgn += 2) {
234 const double xs = ctr + sgn * hw * glx[g];
235 const double f = a * std::exp((n - 1) * std::log(a * xs) - a * xs - lg);
236 W += glw[g] * hw *
response(mode, comps, xs, grid) * f;
244inline std::vector<double>
solve(
const std::vector<double>& lambda,
const std::vector<double>& mu,
245 const std::vector<double>& cs,
Mode mode) {
246 const std::size_t K = lambda.size();
247 std::vector<std::vector<Comp>> fits(K);
248 std::vector<Comp> comps;
249 for (std::size_t i = 0; i < K; ++i) {
250 fits[i] =
sizefit(mu[i], cs[i]);
251 for (
const Comp& c : fits[i]) comps.push_back({lambda[i] * c.w, c.n, c.a});
253 std::unique_ptr<ResGrid> grid;
255 std::vector<double> W(K, 0.0);
256 for (std::size_t i = 0; i < K; ++i)
257 for (
const Comp& c : fits[i])
258 W[i] += c.w * (c.n == 0 ?
response(mode, comps, c.a, grid.get())
259 :
outer(mode, comps, c.n, c.a, grid.get()));
264inline std::vector<double>
solve(
const std::vector<double>& lambda,
const std::vector<double>& mu,
265 const std::vector<double>& cs,
bool fb) {
std::array< double, 3 > window(double n, double a)
double outer(Mode mode, const std::vector< Comp > &comps, int n, double a, const ResGrid *grid)
std::vector< double > solve(const std::vector< double > &lambda, const std::vector< double > &mu, const std::vector< double > &cs, Mode mode)
double gp(double n, double z)
double gq(double n, double z)
static const double kGlw[10]
double rho_minus(const std::vector< Comp > &comps, double t)
rho-(t) = lambda E[S; S < t] for the lambda-weighted components.
double response(Mode mode, const std::vector< Comp > &comps, double x, const ResGrid *grid)
static const double kGlx[10]
double gl_sum(const std::vector< Comp > &comps, double u, double v)
int_u^v dt/(1-rho-(t)), 20-point Gauss-Legendre.
std::vector< Comp > sizefit(double mu, double cs)
Conservation laws of a layered queueing network, enumerated from its structure.
double w
weight (lambda-weighted when used as a load component)
double a
Erlang rate, or the atom location when n == 0.
int n
Erlang order, 0 for a point mass.
Panel edges P and R(P) = int_0^P dt/(1-rho-(t)) for the SRPT residence integral: breakpoints at every...
double residence(const std::vector< Comp > &comps, double x) const
R(x): cumulative value at the start of the panel holding x plus a rule up to x.
ResGrid(const std::vector< Comp > &comps)