5#ifndef LINE_SOLVERS_CTMC_SOLVER_CTMC_REWARD_H
6#define LINE_SOLVERS_CTMC_SOLVER_CTMC_REWARD_H
53 std::vector<Matrix<T>>
V;
68 std::size_t tmax = 1000) {
69 if (
sn.reward.empty())
71 "solver_ctmc_reward: no rewards are defined; declare one with set_reward(name, fn) "
72 "before asking for a reward analysis");
76 const std::size_t n = out.
chain.chain.space.size();
77 const std::size_t nr =
sn.reward.size();
81 out.
chain.chain.space);
87 for (std::size_t r = 0; r < nr; ++r) {
88 out.
names[r] =
sn.reward[r].name;
89 for (std::size_t s = 0; s < n; ++s) {
91 for (std::size_t c = 0; c < row.size(); ++c) row[c] = out.
state_space_aggr(s, c);
92 R(r, s) =
sn.reward[r].fn(row);
97 for (std::size_t r = 0; r < nr; ++r)
98 for (std::size_t s = 0; s < n; ++s)
105 for (std::size_t s = 0; s < n; ++s)
111 for (std::size_t a = 0; a < n; ++a)
112 for (std::size_t b = 0; b < n; ++b)
113 P(a, b) = a == b ? T(out.
chain.chain.Q(a, b) / qq + one)
114 : T(out.
chain.chain.Q(a, b) / qq);
116 out.
V.assign(nr,
Matrix<T>(tmax + 1, n, zero));
117 for (std::size_t r = 0; r < nr; ++r) {
118 std::vector<T> prev(n, zero);
119 for (std::size_t k = 1; k <= tmax; ++k) {
120 std::vector<T> next(n, zero);
125 for (std::size_t s = 0; s < n; ++s) {
127 for (std::size_t sp = 0; sp < n; ++sp) acc += T(P(s, sp) * prev[sp]);
130 for (std::size_t s = 0; s < n; ++s) out.
V[r](k, s) = next[s];
135 out.
t.resize(tmax + 1);
136 for (std::size_t k = 0; k <= tmax; ++k)
159 const T& t1, std::vector<T>* tout =
nullptr,
160 std::vector<std::string>* names =
nullptr) {
162 "solver_ctmc_tran_reward integrates the forward equation, which needs "
163 "transcendental arithmetic; use --arith double or real");
164 if (
sn.reward.empty())
166 "solver_ctmc_tran_reward: no rewards are defined; declare one with "
167 "set_reward(name, fn) before asking for a transient reward");
170 "solver_ctmc_tran_reward: a finite timespan is required; an unbounded one has no "
171 "transient to report, so ask for the steady-state reward instead");
175 const std::size_t n =
tr.chain.chain.space.size(), nr =
sn.reward.size(), nt =
tr.t.size();
182 if (names) names->clear();
183 for (std::size_t r = 0; r < nr; ++r) {
184 if (names) names->push_back(
sn.reward[r].name);
185 for (std::size_t s = 0; s < n; ++s) {
186 std::vector<T> row(A.
cols());
187 for (std::size_t c = 0; c < row.size(); ++c) row[c] = A(s, c);
188 R(r, s) =
sn.reward[r].fn(row);
192 std::vector<std::vector<T>> out(nr, std::vector<T>(nt, zero));
193 for (std::size_t r = 0; r < nr; ++r)
194 for (std::size_t i = 0; i < nt; ++i)
195 for (std::size_t s = 0; s < n; ++s) out[r][i] += T(
tr.pit(i, s) * R(r, s));
196 if (tout) *tout =
tr.t;
206 std::vector<std::vector<T>>
avg;
226 const T& t0,
const T& t1) {
228 "solver_ctmc_acc_reward uses Poisson weights exp(-q t); use --arith double or real");
229 if (sn_arg.
reward.empty())
231 "solver_ctmc_acc_reward: no rewards are defined; declare one with "
232 "set_reward(name, fn) before asking for an accumulated reward");
234 if (!std::isfinite(t0d) || !std::isfinite(t1d) || t1d < t0d)
236 "solver_ctmc_acc_reward: a finite timespan [t0, t1] with t1 >= t0 is required");
241 if (!analyzer_detail::init_state_distribution(
sn, chain.
chain.space, pi0))
243 "solver_ctmc_acc_reward: the initial state is not contained in the state space");
246 if (
opt.timestep > 0.0) {
247 const long K =
static_cast<long>(std::floor((t1d - t0d) /
opt.timestep + 1e-9));
248 for (
long i = 0; i <= K; ++i) {
250 if (ti <= t1) out.
t.push_back(ti);
252 if (out.
t.empty() || out.
t.back() < t1) out.
t.push_back(t1);
254 for (
long i = 0; i <= 50; ++i)
258 std::vector<T> h(out.
t.size());
259 for (std::size_t i = 0; i < h.size(); ++i) h[i] = T(out.
t[i] - t0);
263 const std::size_t n = chain.
chain.space.size(), nr = sn_arg.
reward.size(), nt = out.
t.size();
265 out.
metric.assign(nr, std::vector<T>(nt, zero));
266 out.
avg.assign(nr, std::vector<T>(nt, zero));
267 for (std::size_t r = 0; r < nr; ++r) {
271 for (std::size_t s = 0; s < n; ++s) {
272 std::vector<T> row(A.
cols());
273 for (std::size_t c = 0; c < row.size(); ++c) row[c] = A(s, c);
274 R[s] = sn_arg.
reward[r].fn(row);
275 r0 += T(pi0[s] * R[s]);
277 for (std::size_t i = 0; i < nt; ++i) {
279 for (std::size_t s = 0; s < n; ++s) m += T(acc.
L(i, s) * R[s]);
281 out.
avg[r][i] = h[i] > zero ? T(m / h[i]) : r0;
290 std::vector<std::string>* names =
nullptr) {
292 if (names) *names = r.
names;
A network plus its refreshed NetworkStruct.
std::vector< Reward > reward
Accumulated state probabilities L(t) = int_0^t pi(s) ds of a CTMC by uniformisation (Reibman and Triv...
The exception types the port throws.
Dense matrix and non-owning view.
CtmcSolution< T > solver_ctmc_analyzer(const NetworkStruct< T > &sn_in, const CtmcOptions &opt, const CtmcStationaryHook< T > &stationary=CtmcStationaryHook< T >())
Port of solver_ctmc_analyzer.m plus the fork-join wrapper of @@SolverCTMC/runAnalyzer....
std::vector< T > solver_ctmc_avg_reward(const NetworkStruct< T > &sn, const CtmcOptions &opt, std::vector< std::string > *names=nullptr)
Port of @@SolverCTMC/getAvgReward: the steady-state expected rewards.
CtmcTransient< T > solver_ctmc_transient_analyzer(const NetworkStruct< T > &sn_arg, const CtmcOptions &opt, const T &t0, const T &t1, const std::vector< T > &grid=std::vector< T >())
Port of solver_ctmc_transient_analyzer.m.
CtmcAccReward< T > solver_ctmc_acc_reward(const NetworkStruct< T > &sn_arg, const CtmcOptions &opt, const T &t0, const T &t1)
Port of @@SolverCTMC/getAccReward: the reward ACCUMULATED over [t0, t], E[int_{t0}...
Matrix< T > ctmc_state_space_aggr(const NetworkStruct< T > &sn, const std::vector< NetState< T > > &space)
Port of StateSpaceAggr: the per-(station, class) job counts of every state, as an (nstates x nstation...
std::vector< std::vector< T > > solver_ctmc_tran_reward(const NetworkStruct< T > &sn, const CtmcOptions &opt, const T &t0, const T &t1, std::vector< T > *tout=nullptr, std::vector< std::string > *names=nullptr)
Port of @@SolverCTMC/getTranReward: E[r(X(t))] = sum_s pi_t(s) r(s).
CtmcReward< T > solver_ctmc_reward(const NetworkStruct< T > &sn, const CtmcOptions &opt, std::size_t tmax=1000)
Port of solver_ctmc_reward.m.
AccumulatedResult< T > ctmc_accumulated(const std::vector< T > &pi0, const Matrix< T > &Q, const std::vector< T > &t, double tol=1e-12, long maxiter=-1)
Accumulated state probabilities int_0^t pi(s) ds at each time point.
Conservation laws of a layered queueing network, enumerated from its structure.
A queueing network and its refreshed NetworkStruct.
Port of solver_ctmc_analyzer.m and the parts of @@SolverCTMC/runAnalyzer.m that surround one solve: t...
Port of solver_ctmc_transient_analyzer.m: the time-dependent counterpart of solver_ctmc_analyzer,...
What solver_ctmc_acc_reward produces.
std::vector< T > t
absolute time points, t[0] = t0
std::vector< std::string > names
std::vector< std::vector< T > > metric
metric[r][i] = E[int_{t0}^{t_i} r(X(s)) ds]
std::vector< std::vector< T > > avg
metric / (t_i - t0); E[r(X(t0))] at t_i = t0
The SolverCTMC knobs this port honours.
What the reward analyzer produces, per declared reward.
std::vector< T > steady_state
E[r] per reward.
std::vector< Matrix< T > > V
V[r] is (Tmax+1 x nstates).
std::vector< T > t
iteration index / q
Matrix< T > state_space_aggr
the rows the reward saw
std::vector< std::string > names
Everything one CTMC solve produces.
std::shared_ptr< const NetworkStruct< T > > solved_sn
The struct the chain was built on, when it differs from the caller's in its state LAYOUT: set when he...
What one transient CTMC solve produces.
Matrix< T > L
row k is int_0^{t_k} pi(s) ds