328 const std::vector<double>& lambda,
const std::vector<double>& mu,
double p,
331 const int K =
static_cast<int>(lambda.size());
332 if (K < 1 ||
static_cast<int>(mu.size()) != K)
333 throw InputError(
"sim_prio_tailquantile: lambda and mu must be nonempty vectors of equal length");
335 for (
int l = 0; l < K; ++l) {
336 if (!(lambda[l] > 0) || std::isinf(lambda[l]) || !(mu[l] > 0) || std::isinf(mu[l]))
337 throw InputError(
"sim_prio_tailquantile: the arrival and service rates must be positive and finite");
338 util += lambda[l] / mu[l];
340 if (
util >= 1)
throw InputError(
"sim_prio_tailquantile: the queue must be stable, utilization >= 1");
341 if (!(p > 0 && p < 1))
throw InputError(
"sim_prio_tailquantile: p must lie in (0,1)");
342 const int m1 =
opt.m1 > 0 ?
opt.m1 :
opt.cycles;
343 const int m2 =
opt.m2 > 0 ?
opt.m2 :
opt.cycles;
344 const int N =
opt.ceSamples;
345 const int r =
opt.batches;
346 if (r < 2)
throw InputError(
"sim_prio_tailquantile: batches must be an integer >= 2");
347 if (m1 < r || m2 < r)
348 throw InputError(
"sim_prio_tailquantile: m1 and m2 must be no smaller than the number of batches");
349 if (N < 1)
throw InputError(
"sim_prio_tailquantile: ceSamples must be positive");
350 if (!(
opt.rho > 0 &&
opt.rho < 1))
throw InputError(
"sim_prio_tailquantile: rho must lie in (0,1)");
351 if (
opt.maxIter < 1)
throw InputError(
"sim_prio_tailquantile: maxIter must be positive");
352 if (!(
opt.confidence > 0 &&
opt.confidence < 1))
353 throw InputError(
"sim_prio_tailquantile: confidence must lie in (0,1)");
354 if (
opt.seed < 0 ||
opt.seed >= (1LL << 31))
355 throw InputError(
"sim_prio_tailquantile: seed must be an integer in [0, 2^31)");
356 std::vector<int> classes =
opt.classes;
358 for (
int l = 1; l <= K; ++l) classes.push_back(l);
359 for (
int c : classes)
360 if (c < 1 || c > K)
throw InputError(
"sim_prio_tailquantile: classes must be indices in 1..K");
361 const int nc =
static_cast<int>(classes.size());
362 std::string importance =
opt.importance;
363 std::transform(importance.begin(), importance.end(), importance.begin(), ::tolower);
364 if (importance !=
"ce" && importance !=
"none")
365 throw InputError(
"sim_prio_tailquantile: importance must be 'ce' or 'none'");
366 std::vector<double> gmax;
367 if (!
opt.gammaMax.empty()) {
368 gmax =
opt.gammaMax.size() == 1 ? std::vector<double>(
nc,
opt.gammaMax[0]) :
opt.gammaMax;
369 if (
static_cast<int>(gmax.size()) !=
nc)
370 throw InputError(
"sim_prio_tailquantile: gammaMax must be a scalar or one value per target class");
371 for (
double gv : gmax)
372 if (!(gv > 0))
throw InputError(
"sim_prio_tailquantile: gammaMax must be positive");
374 std::vector<std::vector<double>> isr;
375 if (!
opt.isRates.empty()) {
376 if (!(
opt.isRates.size() == 1 ||
static_cast<int>(
opt.isRates.size()) ==
nc))
377 throw InputError(
"sim_prio_tailquantile: isRates must be 1 x 2K or one row per target class");
378 for (
const auto& row :
opt.isRates) {
379 if (
static_cast<int>(row.size()) != 2 * K)
380 throw InputError(
"sim_prio_tailquantile: isRates must be 1 x 2K or one row per target class");
382 if (!(v > 0) || std::isinf(v))
383 throw InputError(
"sim_prio_tailquantile: isRates must be positive and finite");
386 throw InputError(
"sim_prio_tailquantile: isRates needs gammaMax, the level at which the measure switches back");
387 for (
int i = 0; i <
nc; ++i) isr.push_back(
opt.isRates[
opt.isRates.size() == 1 ? 0 : i]);
388 importance =
"given";
391 const double tcrit =
sim_tinv((1 +
opt.confidence) / 2,
static_cast<double>(r - 1));
392 const double inf = std::numeric_limits<double>::infinity();
397 std::vector<std::vector<int>> alphaAll(m2);
398 for (
int i = 0; i < m2; ++i) {
399 cyc.run(lambda, mu, lambda, mu, 0, inf,
false, g);
400 alphaAll[i] = cyc.nArr;
403 std::vector<PrioTailQuantileResult> results;
404 for (
int ci = 0; ci <
nc; ++ci) {
405 const int kk = classes[ci];
406 const int k = kk - 1;
408 std::vector<double> alpha(m2);
410 for (
int i = 0; i < m2; ++i) {
411 alpha[i] = alphaAll[i][k];
412 sumAlpha += alpha[i];
414 const double abar = sumAlpha / m2;
416 throw InputError(
"sim_prio_tailquantile: no class-" + std::to_string(kk) +
417 " job was seen in the plain cycles; increase m2");
418 std::vector<double> lamIS = lambda, muIS = mu;
421 if (importance ==
"given") {
422 lamIS.assign(isr[ci].begin(), isr[ci].begin() + K);
423 muIS.assign(isr[ci].begin() + K, isr[ci].end());
426 }
else if (importance ==
"ce") {
429 bool reached =
false;
430 const bool hasTarget = !gmax.empty();
431 const double gTarget = hasTarget ? gmax[ci] : 0.0;
432 std::vector<std::vector<double>> jobsR, jobsL;
433 for (
int it = 1; it <=
opt.maxIter; ++it) {
438 for (
int l = 0; l < K; ++l) loadIS += lamIS[l] / muIS[l];
439 const double gswCE = hasTarget ? gTarget : (loadIS < 0.95 ? inf : gPrev);
440 std::vector<double> maxR(N, 0.0), T(N);
441 std::vector<std::vector<int>> nA(N), nS(N);
442 std::vector<std::vector<double>> sS(N);
445 for (
int i = 0; i < N; ++i) {
446 cyc.run(lambda, mu, lamIS, muIS, k, gswCE,
true, g);
448 jobsL[i].resize(cyc.logL.size());
449 for (std::size_t j = 0; j < cyc.logL.size(); ++j) jobsL[i][j] = std::exp(cyc.logL[j]);
450 for (
double v : cyc.R)
451 if (v > maxR[i]) maxR[i] = v;
457 std::vector<double> srt = maxR;
458 std::sort(srt.begin(), srt.end());
459 double gl = srt[
static_cast<std::size_t
>(std::ceil((1 -
opt.rho) * N)) - 1];
460 if (hasTarget) gl = std::min(gl, gTarget);
461 std::vector<double> w(N, 0.0);
462 for (
int i = 0; i < N; ++i) {
464 for (std::size_t j = 0; j < jobsR[i].size(); ++j)
465 if (jobsR[i][j] > gl) acc += jobsL[i][j];
470 for (
int i = 0; i < N; ++i) wT += w[i] * T[i];
471 for (
int l = 0; l < K; ++l) {
472 double na = 0, ns = 0,
ws = 0;
473 for (
int i = 0; i < N; ++i) {
474 na += w[i] * nA[i][l];
475 ns += w[i] * nS[i][l];
476 ws += w[i] * sS[i][l];
478 if (na > 0 && wT > 0) lamIS[l] = na / wT;
479 if (ns > 0 &&
ws > 0) muIS[l] = ns /
ws;
485 stop = gl >= gTarget;
488 for (
double v : w) sw += v;
489 const double ghat = (sw / N) / abar;
492 stop = ghat + z * se < 1 - p;
502 std::snprintf(buf,
sizeof(buf),
503 "class %d: the CE level did not pass Q(p) in %d iterations (last level %g); "
504 "the estimate stays unbiased but may be inefficient.", kk,
opt.maxIter, gPrev);
509 if (!hasTarget && reached) {
511 std::vector<double> Rp, Lp;
512 for (
int i = 0; i < N; ++i) {
513 Rp.insert(Rp.end(), jobsR[i].begin(), jobsR[i].end());
514 Lp.insert(Lp.end(), jobsL[i].begin(), jobsL[i].end());
521 std::vector<double> Rall, Lall;
522 std::vector<std::size_t> cycEnd(m1 + 1, 0);
523 for (
int i = 0; i < m1; ++i) {
524 cyc.run(lambda, mu, lamIS, muIS, k, gsw, useIS, g);
525 for (std::size_t j = 0; j < cyc.R.size(); ++j) {
526 Rall.push_back(cyc.R[j]);
527 Lall.push_back(std::exp(cyc.logL[j]));
529 cycEnd[i + 1] = Rall.size();
532 throw InputError(
"sim_prio_tailquantile: no class-" + std::to_string(kk) +
533 " job was seen in the importance-sampled cycles; increase m1");
535 const double q = qt.first;
536 std::vector<double> qb(r);
537 for (
int b = 0; b < r; ++b) {
538 const int i1 =
static_cast<int>(
static_cast<long long>(b) * m1 / r);
539 const int i2 =
static_cast<int>(
static_cast<long long>(b + 1) * m1 / r);
540 const int j1 =
static_cast<int>(
static_cast<long long>(b) * m2 / r);
541 const int j2 =
static_cast<int>(
static_cast<long long>(b + 1) * m2 / r);
542 if (cycEnd[i2] <= cycEnd[i1])
543 throw InputError(
"sim_prio_tailquantile: batch " + std::to_string(b + 1) +
" holds no class-" +
544 std::to_string(kk) +
" job; increase m1 or reduce batches");
546 for (
int j = j1; j < j2; ++j) sa += alpha[j];
550 for (
double v : qb) ss += (v - q) * (v - q);
551 const double hw = tcrit * std::sqrt(ss / (r - 1)) / std::sqrt(
static_cast<double>(r));
559 res.
tailProb = (qt.second / m1) / abar;
564 res.
numSamples =
static_cast<int>(Rall.size());
569 results.push_back(res);