181 throw InputError(
"SolverLDES (native engine): the Replayer at " + where_ +
182 " carries no samples");
186 const double half = mean_ * std::sqrt(3.0 * scv_);
189 if (!(a_ >= 0.0) || !(b_ > a_))
190 throw InputError(
"SolverLDES (native engine): the Uniform at " + where_ +
191 " implies a support reaching below zero; a non-negative "
192 "uniform requires an SCV of at most 1/3");
202 const double c = std::sqrt(scv_);
203 a_ = std::pow(c, -1.086);
204 b_ = mean_ / std::tgamma(1.0 + 1.0 / a_);
205 if (!(a_ > 0.0) || !(b_ > 0.0))
206 throw InputError(
"SolverLDES (native engine): the Weibull at " + where_ +
207 " has unusable moments");
212 const double c2p1 = scv_ + 1.0;
213 a_ = std::log(mean_ / std::sqrt(c2p1));
214 b_ = std::sqrt(std::log(c2p1));
216 throw InputError(
"SolverLDES (native engine): the Lognormal at " + where_ +
217 " has a zero log-variance");
232 throw InputError(
"SolverLDES (native engine): the MassAtOrigin at " + where_ +
233 " carries no inner law");
234 inner_ = std::make_shared<Sampler>(*d.
inner, where_);
238 "SolverLDES (native engine): the process at " + where_ +
" is a Defective law, "
239 "which is infinite with probability pInf, so its mean is Inf and it cannot be "
240 "a service or interarrival time");
245 a_ = std::sqrt(1.0 + 1.0 / scv_) + 1.0;
246 b_ = mean_ * (a_ - 1.0) / a_;
254 if (!(a_ > 0.0) || a_ > 1.0)
255 throw InputError(
"SolverLDES (native engine): the Geometric at " + where_ +
256 " has a success probability outside (0,1]");
267 const double var = scv_ * mean_ * mean_;
268 b_ = std::floor(std::sqrt(12.0 * var + 1.0) + 0.5);
269 a_ = mean_ - (b_ - 1.0) / 2.0;
271 throw InputError(
"SolverLDES (native engine): the DiscreteUniform at " +
272 where_ +
" has moments no (min,max) pair realises");
274 throw InputError(
"SolverLDES (native engine): the DiscreteUniform at " +
275 where_ +
" reaches below zero");
282 if (!(mean_ >= 0.0) || !(mean_ <= 1.0))
283 throw InputError(
"SolverLDES (native engine): the Bernoulli at " + where_ +
284 " has a mean outside [0,1]");
288 const double p = 1.0 - scv_ * mean_;
289 if (!(p > 0.0) || p > 1.0)
290 throw InputError(
"SolverLDES (native engine): the Binomial at " + where_ +
291 " has moments no (n,p) pair realises");
293 b_ = std::floor(mean_ / p + 0.5);
308 throw InputError(
"SolverLDES (native engine): the NNPP at " + where_ +
309 " carries no rate function");
312 mean_ = nnpp_->mean();
332 " is of a family this engine does not sample");
338 double mean()
const {
return mean_; }
350 if (nnpp_)
return nnpp_gap(g, from);
351 if (!has_schedule_)
return next(g);
352 return schedule_sample(g, from);
355 bool time_varying()
const {
return has_schedule_ ||
static_cast<bool>(nnpp_); }
384 const std::size_t n = segs_.size();
385 const double b0 = bp_[0];
386 const double horizon = bp_[n] - b0;
388 for (std::size_t i = 0; i < n; ++i) total += op_rate(i) * (bp_[i + 1] - bp_[i]);
389 const double x = t - b0;
392 const double periods = std::floor(x / horizon);
393 rem = x - periods * horizon;
394 mass = periods * total;
395 }
else if (x <= 0.0) {
397 }
else if (x >= horizon) {
403 double seg_start = 0.0;
404 for (std::size_t i = 0; i < n; ++i) {
405 const double len = bp_[i + 1] - bp_[i];
406 if (rem <= seg_start + len) {
407 mass += op_rate(i) * (rem - seg_start);
410 mass += op_rate(i) * len;
418 if (t1 <= t0)
return 0.0;
428 if (work <= 0.0)
return t0;
429 const std::size_t n = segs_.size();
430 const double b0 = bp_[0];
431 const double horizon = bp_[n] - b0;
432 double offset = t0 - b0;
434 offset = std::fmod(offset, horizon);
435 if (offset < 0.0) offset += horizon;
436 }
else if (offset >= horizon) {
437 return std::numeric_limits<double>::infinity();
438 }
else if (offset < 0.0) {
441 double pos = b0 + offset;
443 while (idx + 1 < n && pos >= bp_[idx + 1]) ++idx;
444 double residual = work;
445 double elapsed = 0.0;
447 const double in_seg = bp_[idx + 1] - pos;
448 const double mass = op_rate(idx) * in_seg;
449 if (op_rate(idx) > 0.0 && mass >= residual)
return t0 + elapsed + residual / op_rate(idx);
454 if (!cyclic_)
return std::numeric_limits<double>::infinity();
476 if (nnpp_)
return nnpp_gap(g, nnpp_stream_.first ? 0.0 : nnpp_stream_.t_last);
490 if (v != 0.0)
return v;
509 double nnpp_gap(
Rng& g,
double from) {
510 auto unif = [&g]() {
return uniform01(g); };
511 const double t = nnpp_->next_arrival(nnpp_stream_, unif);
512 if (!std::isfinite(t))
return 0.0;
513 const double gap = t - from;
514 return gap > 0.0 ? gap : std::numeric_limits<double>::denorm_min();
517 double draw(Rng& g) {
528 const double v = trace_[trace_idx_];
529 trace_idx_ = (trace_idx_ + 1) % trace_.size();
532 return (v <= 0.0) ? 1e-9 : v;
550 return a_ * std::pow(u / (1.0 - u), 1.0 / b_);
554 return inner_->draw(g);
560 return std::ceil(std::log(1.0 -
uniform01(g)) / std::log(1.0 - a_));
577 if (erlang_k_ <= 0)
return next_markovian(g);
579 for (
int i = 0; i < erlang_k_; ++i)
584 return next_markovian(g);
594 int phase()
const {
return has_phase_ ?
static_cast<int>(phase_) : -1; }
600 if (map_.order() <= 1)
return 0;
601 return phase_known_ ?
static_cast<int>(phase_) : -1;
625 int last_batch()
const {
return last_batch_ > 0 ? last_batch_ : 1; }
626 bool marked()
const {
return !mark_.empty() || !seg_mark_.empty(); }
628 bool batched()
const {
return !batch_.empty() || !seg_batch_.empty(); }
630 if (has_phase_ && p >= 0 &&
static_cast<std::size_t
>(p) < map_.order()) {
631 phase_ =
static_cast<std::size_t
>(p);
637 void require_mean()
const {
638 if (!(mean_ > 0.0) || !std::isfinite(mean_))
639 throw InputError(
"SolverLDES (native engine): the process at " + where_ +
640 " has a mean that is neither finite nor positive");
642 void require_moments()
const {
644 if (!(scv_ > 0.0) || !std::isfinite(scv_))
645 throw InputError(
"SolverLDES (native engine): the process at " + where_ +
646 " has an SCV that is neither finite nor positive");
651 if (d.
D0.rows() == 0 || d.
D0.rows() != d.
D1.rows())
652 throw InputError(
"SolverLDES (native engine): the process at " + where_ +
653 " declares no (D0,D1) representation to sample");
654 const std::size_t K = d.
D0.rows();
658 for (std::size_t i = 0; i < K; ++i)
659 for (std::size_t j = 0; j < K; ++j) {
667 if (!(mean_ > 0.0)) {
670 throw InputError(
"SolverLDES (native engine): the process at " + where_ +
671 " has a non-positive mean under its (D0,D1) representation");
680 for (
const Matrix<T>& Dk : d.
Dmark) {
681 Matrix<double> m(K, K, 0.0);
682 for (std::size_t i = 0; i < K; ++i)
683 for (std::size_t j = 0; j < K; ++j)
684 m(i, j) = num_traits<T>::to_double(Dk(i, j));
696 for (
const Matrix<T>& Db : d.
Dmark) {
697 Matrix<double> m(K, K, 0.0);
698 for (std::size_t i = 0; i < K; ++i)
699 for (std::size_t j = 0; j < K; ++j)
700 m(i, j) = num_traits<T>::to_double(Db(i, j));
717 me_.reset(
new mam::MeSampler<double>(map_));
731 void recover_erlang() {
733 const std::size_t n = map_.order();
735 const double lambda = -map_.D0(0, 0);
736 if (!(lambda > 0.0))
return;
737 const double tol = 1e-12 * lambda;
738 for (std::size_t i = 0; i < n; ++i) {
739 if (std::fabs(-map_.D0(i, i) - lambda) > tol)
return;
740 for (std::size_t j = 0; j < n; ++j) {
741 if (j == i)
continue;
742 const double want = (j == i + 1) ? lambda : 0.0;
743 if (std::fabs(map_.D0(i, j) - want) > tol)
return;
746 for (std::size_t j = 0; j < n; ++j) {
747 const double want = (i + 1 == n && j == 0) ? lambda : 0.0;
748 if (std::fabs(map_.D1(i, j) - want) > tol)
return;
751 erlang_k_ =
static_cast<int>(n);
752 erlang_rate_ = lambda;
773 double next_map_java(Rng& g) {
774 const std::size_t n = map_.order();
775 if (ptrace_) ptrace_->clear();
779 const double lambda = map_.D1(0, 0);
780 const double x = -std::log(g.aux.next_double()) / lambda;
783 ptrace_->set_exit_phase(0);
790 const double r = g.aux.next_double();
792 for (std::size_t i = 0; i < n; ++i) {
802 std::vector<double> row(2 * n, 0.0);
805 const double rate = -map_.D0(phase_, phase_);
806 const double holding = -std::log(g.aux.next_double()) / rate;
808 if (ptrace_) ptrace_->add(
static_cast<int>(phase_), holding);
809 for (std::size_t k = 0; k < n; ++k) {
810 row[k] = map_.D0(phase_, k);
811 row[n + k] = map_.D1(phase_, k);
814 std::size_t next_state = 2 * n - 1;
816 const double r = g.aux.next_double();
817 for (std::size_t j = 0; j < 2 * n; ++j) {
818 sum += row[j] / rate;
823 if (ptrace_) ptrace_->set_exit_phase(
static_cast<int>(next_state));
831 if (next_state >= 2 * n - 1 && go) {
837 phase_ = (next_state < n) ? next_state : next_state - n;
848 int sample_mark(std::size_t i, std::size_t j, Rng& g) {
849 const std::size_t C = mark_.size();
850 if (C == 0)
return 1;
852 for (std::size_t c = 0; c < C; ++c) total += std::max(0.0, mark_[c](i, j));
853 if (!(total > 0.0))
return 1;
854 const double r = g.aux.next_double() * total;
856 for (std::size_t c = 0; c < C; ++c) {
857 sum += std::max(0.0, mark_[c](i, j));
858 if (r < sum)
return static_cast<int>(c) + 1;
860 return static_cast<int>(C);
875 double next_mmap_java(Rng& g) {
876 const std::size_t n = map_.order();
877 if (ptrace_) ptrace_->clear();
879 const double lambda = map_.D1(0, 0);
880 const double time = -std::log(g.aux.next_double()) / lambda;
882 ptrace_->add(0, time);
883 ptrace_->set_exit_phase(0);
885 last_mark_ = sample_mark(0, 0, g);
891 const double r = g.aux.next_double();
893 for (std::size_t i = 0; i < n; ++i) {
903 std::vector<double> row(2 * n, 0.0);
905 const double rate = -map_.D0(phase_, phase_);
906 const double holding = -std::log(g.aux.next_double()) / rate;
908 if (ptrace_) ptrace_->add(
static_cast<int>(phase_), holding);
909 for (std::size_t k = 0; k < n; ++k) {
910 row[k] = map_.D0(phase_, k);
911 row[n + k] = map_.D1(phase_, k);
914 std::size_t next_state = 2 * n - 1;
916 const double r = g.aux.next_double();
917 for (std::size_t j = 0; j < 2 * n; ++j) {
918 sum += row[j] / rate;
924 if (next_state >= n) {
925 const std::size_t dest = next_state - n;
926 if (ptrace_) ptrace_->set_exit_phase(
static_cast<int>(dest));
927 last_mark_ = sample_mark(phase_, dest, g);
947 double next_bmap_java(Rng& g) {
948 const std::size_t n = map_.order();
949 const std::size_t B = batch_.size();
951 if (ptrace_) ptrace_->clear();
953 const double lambda = map_.D1(0, 0);
954 const double time = -std::log(g.aux.next_double()) / lambda;
956 ptrace_->add(0, time);
957 ptrace_->set_exit_phase(0);
960 for (std::size_t b = 0; b < B; ++b) total += batch_[b](0, 0);
962 const double r = g.aux.next_double() * total;
964 for (std::size_t b = 0; b < B; ++b) {
965 cum += batch_[b](0, 0);
967 last_batch_ =
static_cast<int>(b) + 1;
977 const double r = g.aux.next_double();
979 for (std::size_t i = 0; i < n; ++i) {
988 const std::size_t cols = n + B * n;
989 std::vector<double> row(cols, 0.0);
992 const double rate = -map_.D0(phase_, phase_);
993 const double holding = -std::log(g.aux.next_double()) / rate;
995 if (ptrace_) ptrace_->add(
static_cast<int>(phase_), holding);
996 for (std::size_t k = 0; k < n; ++k) row[k] = map_.D0(phase_, k);
998 for (std::size_t b = 0; b < B; ++b)
999 for (std::size_t k = 0; k < n; ++k) row[n + b * n + k] = batch_[b](phase_, k);
1000 std::size_t next_state = phase_;
1003 const double r = g.aux.next_double();
1004 for (std::size_t j = 0; j < cols; ++j) {
1005 sum += row[j] / rate;
1010 const std::size_t idx = j - n;
1011 last_batch_ =
static_cast<int>(idx / n) + 1;
1012 next_state = idx % n;
1018 phase_ = next_state;
1020 if (ptrace_) ptrace_->set_exit_phase(
static_cast<int>(phase_));
1041 double next_ph_renewal_java(Rng& g) {
1042 const std::size_t n = map_.order();
1043 if (ptrace_) ptrace_->clear();
1045 const double lambda = map_.D1(0, 0);
1046 const double x = -std::log(g.aux.next_double()) / lambda;
1049 ptrace_->set_exit_phase(0);
1054 std::size_t state = 1;
1057 const double r = g.aux.next_double();
1058 for (std::size_t i = 0; i < n; ++i) {
1066 double sample = 0.0;
1067 std::vector<double> row(2 * n, 0.0);
1070 const double rate = -map_.D0(state, state);
1071 const double holding = -std::log(g.aux.next_double()) / rate;
1073 if (ptrace_) ptrace_->add(
static_cast<int>(state), holding);
1074 for (std::size_t k = 0; k < n; ++k) {
1075 row[k] = map_.D0(state, k);
1076 row[n + k] = map_.D1(state, k);
1079 std::size_t next_state = 2 * n - 1;
1081 const double r = g.aux.next_double();
1082 for (std::size_t j = 0; j < 2 * n; ++j) {
1083 sum += row[j] / rate;
1088 if (ptrace_) ptrace_->set_exit_phase(
static_cast<int>(next_state));
1098 state = (next_state < n) ? next_state : n - 1;
1103 double next_markovian(Rng& g) {
1104 std::vector<double> out;
1106 return next_map_java(g);
1111 return next_ph_renewal_java(g);
1114 return next_mmap_java(g);
1117 return next_bmap_java(g);
1125 return me_->next(g.mc);
1131 std::vector<double> a_next;
1135 mam::SampleTrace tr;
1136 std::vector<double> start;
1137 if (has_phase_ && phase_known_) {
1138 start.assign(map_.order(), 0.0);
1139 start[phase_] = 1.0;
1142 if (has_phase_ && !tr.last.empty()) {
1143 phase_ = tr.last[0];
1144 phase_known_ =
true;
1147 if (out.empty())
return 0.0;
1155 void load_schedule(
const lang::Distrib<T>& d) {
1156 if (!d.has_schedule())
1157 throw InputError(
"SolverLDES (native engine): the time-inhomogeneous process at " +
1158 where_ +
" carries no segment schedule");
1159 for (
const T& b : d.sched_bp) bp_.push_back(num_traits<T>::to_double(b));
1160 if (bp_.size() != d.sched_D0.size() + 1)
1161 throw InputError(
"SolverLDES (native engine): the schedule at " + where_ +
1162 " has a boundary vector that does not bound its segments");
1163 for (std::size_t k = 0; k < d.sched_D0.size(); ++k) {
1164 const std::size_t H = d.sched_D0[k].rows();
1165 mam::Map<double> seg;
1166 seg.D0 = Matrix<double>(H, H, 0.0);
1167 seg.D1 = Matrix<double>(H, H, 0.0);
1168 for (std::size_t a = 0; a < H; ++a)
1169 for (std::size_t b2 = 0; b2 < H; ++b2) {
1170 seg.D0(a, b2) = num_traits<T>::to_double(d.sched_D0[k](a, b2));
1171 seg.D1(a, b2) = num_traits<T>::to_double(d.sched_D1[k](a, b2));
1173 segs_.push_back(seg);
1178 if (d.has_marked_schedule()) {
1179 const std::size_t C = d.sched_Dmark.size();
1180 for (std::size_t k = 0; k < d.sched_D0.size(); ++k) {
1181 const std::size_t H = d.sched_D0[k].rows();
1182 std::vector<Matrix<double>> per_mark;
1183 for (std::size_t c = 0; c < C; ++c) {
1184 Matrix<double> m(H, H, 0.0);
1185 for (std::size_t a = 0; a < H; ++a)
1186 for (std::size_t b2 = 0; b2 < H; ++b2)
1187 m(a, b2) = num_traits<T>::to_double(d.sched_Dmark[c][k](a, b2));
1188 per_mark.push_back(m);
1190 seg_mark_.push_back(per_mark);
1199 if (d.has_batch_schedule()) {
1200 const std::size_t C = d.sched_Dbatch.size();
1201 const std::size_t B = d.sched_Dbatch[0].size();
1202 for (std::size_t k = 0; k < d.sched_D0.size(); ++k) {
1203 const std::size_t H = d.sched_D0[k].rows();
1204 std::vector<std::vector<Matrix<double>>> per_mark;
1205 for (std::size_t c = 0; c < C; ++c) {
1206 std::vector<Matrix<double>> per_batch;
1207 for (std::size_t b = 0; b < B; ++b) {
1208 Matrix<double> m(H, H, 0.0);
1209 for (std::size_t a = 0; a < H; ++a)
1210 for (std::size_t b2 = 0; b2 < H; ++b2)
1211 m(a, b2) = num_traits<T>::to_double(d.sched_Dbatch[c][b][k](a, b2));
1212 per_batch.push_back(m);
1214 per_mark.push_back(per_batch);
1216 seg_batch_.push_back(per_mark);
1219 cyclic_ = d.sched_cyclic;
1220 has_schedule_ =
true;
1222 if (!(mean_ > 0.0)) mean_ = num_traits<T>::to_double(d.mean);
1227 double period()
const {
return bp_.back() - bp_.front(); }
1230 double op_rate(std::size_t k)
const {
return segs_[k].D1(0, 0); }
1240 double offset_at(
double t)
const {
1241 double offset = t - bp_.front();
1242 const double per = period();
1244 offset = std::fmod(offset, per);
1245 if (offset < 0.0) offset += per;
1246 }
else if (offset < 0.0 || offset >= per) {
1253 int segment_of(
double offset)
const {
1254 const double pos = bp_.front() + offset;
1255 for (std::size_t k = 0; k + 1 < bp_.size(); ++k)
1256 if (pos < bp_[k + 1])
return static_cast<int>(k);
1257 return static_cast<int>(segs_.size()) - 1;
1261 int segment_at(
double t)
const {
1262 const double offset = offset_at(t);
1263 return (offset < 0.0) ? -1 : segment_of(offset);
1277 double schedule_sample(Rng& g,
double from) {
1278 const std::size_t H = segs_[0].order();
1279 double offset = offset_at(from);
1280 if (offset < 0.0)
return 0.0;
1281 std::size_t idx =
static_cast<std::size_t
>(segment_of(offset));
1283 for (
int guard = 0; guard < 1000000; ++guard) {
1284 const double to_boundary = (bp_[idx + 1] - bp_.front()) - offset;
1285 const mam::Map<double>& seg = segs_[idx];
1286 const double total = -seg.D0(phase_, phase_);
1288 const double holding = (total > 0.0) ? -std::log(
uniform01(g)) / total
1289 : std::numeric_limits<double>::infinity();
1290 if (holding >= to_boundary) {
1292 if (idx >= segs_.size()) {
1293 if (!cyclic_)
return 0.0;
1304 offset = bp_[idx] - bp_.front();
1320 if (!seg_mark_.empty()) {
1321 const std::vector<Matrix<double>>& marks = seg_mark_[idx];
1326 const bool batched_here = !seg_batch_.empty();
1327 const std::size_t B = batched_here ? seg_batch_[idx][0].size() : std::size_t(1);
1328 for (std::size_t j = 0; j < H; ++j) {
1329 for (std::size_t c = 0; c < marks.size(); ++c) {
1330 if (!batched_here) {
1331 cum += marks[c](phase_, j);
1333 last_mark_ =
static_cast<int>(c) + 1;
1340 for (std::size_t b = 0; b < B; ++b) {
1341 cum += seg_batch_[idx][c][b](phase_, j);
1343 last_mark_ =
static_cast<int>(c) + 1;
1344 last_batch_ =
static_cast<int>(b) + 1;
1351 for (std::size_t j = 0; j < H; ++j) {
1352 if (j == phase_)
continue;
1353 cum += seg.D0(phase_, j);
1355 chosen =
static_cast<int>(H + j);
1362 for (std::size_t j = H; j-- > 0;)
1363 for (std::size_t c = marks.size(); c-- > 0;) {
1364 if (!batched_here) {
1365 if (marks[c](phase_, j) > 0.0) {
1366 last_mark_ =
static_cast<int>(c) + 1;
1373 for (std::size_t b = B; b-- > 0;)
1374 if (seg_batch_[idx][c][b](phase_, j) > 0.0) {
1375 last_mark_ =
static_cast<int>(c) + 1;
1376 last_batch_ =
static_cast<int>(b) + 1;
1385 phase_ =
static_cast<std::size_t
>(chosen) - H;
1388 for (std::size_t j = 0; j < 2 * H; ++j) {
1389 const double w = (j < H) ? seg.D1(phase_, j)
1390 : ((j - H == phase_) ? 0.0 : seg.D0(phase_, j - H));
1393 chosen =
static_cast<int>(j);
1398 for (std::size_t j = 2 * H; j-- > 0;) {
1399 const double w = (j < H) ? seg.D1(phase_, j)
1400 : ((j - H == phase_) ? 0.0 : seg.D0(phase_, j - H));
1402 chosen =
static_cast<int>(j);
1406 if (chosen <
static_cast<int>(H)) {
1407 phase_ =
static_cast<std::size_t
>(chosen);
1410 phase_ =
static_cast<std::size_t
>(chosen) - H;
1417 double mean_ = 0.0, scv_ = 1.0, rate_ = 0.0;
1418 double a_ = 0.0, b_ = 0.0;
1420 std::shared_ptr<Sampler> inner_;
1422 std::vector<double> pie_;
1423 PhaseTrace* ptrace_ =
nullptr;
1424 double erlang_rate_ = 0.0;
1425 std::vector<double> trace_;
1426 std::size_t trace_idx_ = 0;
1427 mam::Map<double> map_;
1428 std::vector<double> entry_;
1429 std::shared_ptr<mam::MeSampler<double>> me_;
1431 std::vector<std::vector<Matrix<double>>> seg_mark_;
1433 std::vector<Matrix<double>> mark_;
1436 std::vector<Matrix<double>> batch_;
1438 std::vector<std::vector<std::vector<Matrix<double>>>> seg_batch_;
1442 int last_batch_ = 0;
1443 bool has_phase_ =
false, phase_known_ =
false;
1446 bool has_schedule_ =
false, cyclic_ =
false;
1447 std::vector<double> bp_;
1448 std::vector<mam::Map<double>> segs_;
1449 std::size_t phase_ = 0;
1451 std::shared_ptr<const lang::Nnpp> nnpp_;
1452 lang::Nnpp::Stream nnpp_stream_;