5#ifndef LINE_SOLVERS_CTMC_SOLVER_CTMC_REWARD_H
6#define LINE_SOLVERS_CTMC_SOLVER_CTMC_REWARD_H
52 std::vector<Matrix<T>>
V;
67 std::size_t tmax = 1000) {
68 if (
sn.reward.empty())
70 "solver_ctmc_reward: no rewards are defined; declare one with set_reward(name, fn) "
71 "before asking for a reward analysis");
75 const std::size_t n = out.
chain.chain.space.size();
76 const std::size_t nr =
sn.reward.size();
85 for (std::size_t r = 0; r < nr; ++r) {
86 out.
names[r] =
sn.reward[r].name;
87 for (std::size_t s = 0; s < n; ++s) {
89 for (std::size_t c = 0; c < row.size(); ++c) row[c] = out.
state_space_aggr(s, c);
90 R(r, s) =
sn.reward[r].fn(row);
95 for (std::size_t r = 0; r < nr; ++r)
96 for (std::size_t s = 0; s < n; ++s)
103 for (std::size_t s = 0; s < n; ++s)
109 for (std::size_t a = 0; a < n; ++a)
110 for (std::size_t b = 0; b < n; ++b)
111 P(a, b) = a == b ? T(out.
chain.chain.Q(a, b) / qq + one)
112 : T(out.
chain.chain.Q(a, b) / qq);
114 out.
V.assign(nr,
Matrix<T>(tmax + 1, n, zero));
115 for (std::size_t r = 0; r < nr; ++r) {
116 std::vector<T> prev(n, zero);
117 for (std::size_t k = 1; k <= tmax; ++k) {
118 std::vector<T> next(n, zero);
123 for (std::size_t s = 0; s < n; ++s) {
125 for (std::size_t sp = 0; sp < n; ++sp) acc += T(P(s, sp) * prev[sp]);
128 for (std::size_t s = 0; s < n; ++s) out.
V[r](k, s) = next[s];
133 out.
t.resize(tmax + 1);
134 for (std::size_t k = 0; k <= tmax; ++k)
157 const T& t1, std::vector<T>* tout =
nullptr,
158 std::vector<std::string>* names =
nullptr) {
160 "solver_ctmc_tran_reward integrates the forward equation, which needs "
161 "transcendental arithmetic; use --arith double or real");
162 if (
sn.reward.empty())
164 "solver_ctmc_tran_reward: no rewards are defined; declare one with "
165 "set_reward(name, fn) before asking for a transient reward");
168 "solver_ctmc_tran_reward: a finite timespan is required; an unbounded one has no "
169 "transient to report, so ask for the steady-state reward instead");
173 const std::size_t n =
tr.chain.chain.space.size(), nr =
sn.reward.size(), nt =
tr.t.size();
180 if (names) names->clear();
181 for (std::size_t r = 0; r < nr; ++r) {
182 if (names) names->push_back(
sn.reward[r].name);
183 for (std::size_t s = 0; s < n; ++s) {
184 std::vector<T> row(A.
cols());
185 for (std::size_t c = 0; c < row.size(); ++c) row[c] = A(s, c);
186 R(r, s) =
sn.reward[r].fn(row);
190 std::vector<std::vector<T>> out(nr, std::vector<T>(nt, zero));
191 for (std::size_t r = 0; r < nr; ++r)
192 for (std::size_t i = 0; i < nt; ++i)
193 for (std::size_t s = 0; s < n; ++s) out[r][i] += T(
tr.pit(i, s) * R(r, s));
194 if (tout) *tout =
tr.t;
201 std::vector<std::string>* names =
nullptr) {
203 if (names) *names = r.
names;
A network plus its refreshed NetworkStruct.
The exception types the port throws.
Dense matrix and non-owning view.
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.
CtmcSolution< T > solver_ctmc_analyzer(const NetworkStruct< T > &sn_in, const CtmcOptions &opt)
Port of solver_ctmc_analyzer.m plus the fork-join wrapper of @@SolverCTMC/runAnalyzer....
CtmcTransient< T > solver_ctmc_transient_analyzer(const NetworkStruct< T > &sn, const CtmcOptions &opt, const T &t0, const T &t1, const std::vector< T > &grid=std::vector< T >())
Port of solver_ctmc_transient_analyzer.m.
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.
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,...
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.
What one transient CTMC solve produces.