Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
DiscountingHelper.cpp
Go to the documentation of this file.
1#include "DiscountingHelper.h"
7
8namespace storm {
9namespace modelchecker {
10namespace helper {
11
12template<typename ValueType, bool TrivialRowGrouping>
14 : localA(nullptr), A(&A), discountFactor(discountFactor) {
16 discountedMatrix.scaleRowsInPlace(std::vector<ValueType>(discountedMatrix.getRowCount(), discountFactor));
17 discountedA = discountedMatrix;
18}
19
20template<typename ValueType, bool TrivialRowGrouping>
22 bool trackScheduler)
23 : localA(nullptr), A(&A), discountFactor(discountFactor), trackScheduler(trackScheduler) {
25 discountedMatrix.scaleRowsInPlace(std::vector<ValueType>(discountedMatrix.getRowCount(), discountFactor));
26 discountedA = discountedMatrix;
27}
28
29template<typename ValueType, bool TrivialRowGrouping>
30void DiscountingHelper<ValueType, TrivialRowGrouping>::setUpViOperator() const {
31 if (!viOperator) {
32 viOperator = std::make_shared<solver::helper::ValueIterationOperator<ValueType, TrivialRowGrouping>>();
33 viOperator->setMatrixBackwards(this->discountedA);
34 }
35}
36
37template<typename ValueType, bool TrivialRowGrouping>
38void DiscountingHelper<ValueType, TrivialRowGrouping>::showProgressIterative(uint64_t iteration) const {
39 progressMeasurement->updateProgress(iteration);
40}
41
42template<typename ValueType, bool TrivialRowGrouping>
44 std::optional<OptimizationDirection> dir, std::vector<ValueType>& x,
45 std::vector<ValueType> const& b) const {
46 setUpViOperator();
47 // This is currently missing progress indications, we can add them later
49 uint64_t numIterations{0};
50 auto viCallback = [&](solver::SolverStatus const& current) { return current; };
51 auto maximalAbsoluteReward = storm::utility::zero<ValueType>();
52 for (auto const& entry : b) {
53 if (storm::utility::abs(entry) > maximalAbsoluteReward) {
54 maximalAbsoluteReward = storm::utility::abs(entry);
55 }
56 }
57 progressMeasurement = storm::utility::ProgressMeasurement("iterations", env.solver().getShowProgressDelay());
58 progressMeasurement->startNewMeasurement(0);
59 auto status = viHelper.DiscountedVI(x, b, numIterations, env.solver().minMax().getRelativeTerminationCriterion(),
60 storm::utility::convertNumber<ValueType>(env.solver().minMax().getPrecision()), discountFactor, maximalAbsoluteReward,
61 dir, viCallback, env.solver().minMax().getMultiplicationStyle());
62
63 // If requested, we store the scheduler for retrieval.
64 if (this->isTrackSchedulerSet()) {
65 this->extractScheduler(x, b, dir.value(), UncertaintyResolutionMode::Robust);
66 }
68}
69
70template<typename ValueType, bool TrivialRowGrouping>
72 STORM_LOG_THROW(hasScheduler(), storm::exceptions::IllegalFunctionCallException, "Cannot retrieve scheduler, because none was generated.");
73 storm::storage::Scheduler<ValueType> result(schedulerChoices->size());
74 uint_fast64_t state = 0;
75 for (auto const& schedulerChoice : schedulerChoices.get()) {
76 result.setChoice(schedulerChoice, state);
77 ++state;
78 }
79 return result;
80}
81
82template<typename ValueType, bool TrivialRowGrouping>
84 return static_cast<bool>(schedulerChoices);
85}
86
87template<>
88void DiscountingHelper<double, true>::extractScheduler(std::vector<double>& x, std::vector<double> const& b, OptimizationDirection const& dir,
89 UncertaintyResolutionMode uncertaintyResolutionMode) const {}
90
91template<>
92void DiscountingHelper<storm::RationalNumber, true>::extractScheduler(std::vector<storm::RationalNumber>& x, std::vector<storm::RationalNumber> const& b,
93 OptimizationDirection const& dir,
94 UncertaintyResolutionMode uncertaintyResolutionMode) const {}
95
96template<typename ValueType, bool TrivialRowGrouping>
97void DiscountingHelper<ValueType, TrivialRowGrouping>::extractScheduler(std::vector<ValueType>& x, std::vector<ValueType> const& b,
98 OptimizationDirection const& dir,
99 UncertaintyResolutionMode uncertaintyResolutionMode) const {
100 // Make sure that storage for scheduler choices is available
101 if (!this->schedulerChoices) {
102 this->schedulerChoices = std::vector<uint64_t>(x.size(), 0);
103 } else {
104 this->schedulerChoices->resize(x.size(), 0);
105 }
106 // Set the correct choices.
107 STORM_LOG_WARN_COND(viOperator, "Expected VI operator to be initialized for scheduler extraction. Initializing now, but this is inefficient.");
108 if (!viOperator) {
109 setUpViOperator();
110 }
111 storm::solver::helper::SchedulerTrackingHelper<ValueType> schedHelper(viOperator);
112 schedHelper.computeScheduler(x, b, dir, *this->schedulerChoices, uncertaintyResolutionMode, nullptr);
113}
114
115template<typename ValueType, bool TrivialRowGrouping>
117 this->trackScheduler = trackScheduler;
118 if (!this->trackScheduler) {
119 schedulerChoices = boost::none;
120 }
121}
122
123template<typename ValueType, bool TrivialRowGrouping>
125 return this->trackScheduler;
126}
127
128template class DiscountingHelper<double>;
130
133} // namespace helper
134} // namespace modelchecker
135} // namespace storm
SolverEnvironment & solver()
storm::solver::MultiplicationStyle const & getMultiplicationStyle() const
storm::RationalNumber const & getPrecision() const
bool const & getRelativeTerminationCriterion() const
MinMaxSolverEnvironment & minMax()
uint64_t getShowProgressDelay() const
bool hasScheduler() const
Retrieves whether the solver generated a scheduler.
storm::storage::Scheduler< ValueType > computeScheduler() const
Retrieves the generated scheduler.
bool solveWithDiscountedValueIteration(Environment const &env, std::optional< OptimizationDirection > dir, std::vector< ValueType > &x, std::vector< ValueType > const &b) const
DiscountingHelper(storm::storage::SparseMatrix< ValueType > const &A, ValueType discountFactor)
SolverStatus DiscountedVI(std::vector< ValueType > &operand, std::vector< ValueType > const &offsets, uint64_t &numIterations, ValueType const &precision, ValueType const &discountFactor, ValueType const &maximalAbsoluteReward, std::function< SolverStatus(SolverStatus const &)> const &iterationCallback={}, MultiplicationStyle mult=MultiplicationStyle::GaussSeidel) const
This class defines which action is chosen in a particular state of a non-deterministic model.
Definition Scheduler.h:18
void setChoice(SchedulerChoice< ValueType > const &choice, uint_fast64_t modelState, uint_fast64_t memoryState=0)
Sets the choice defined by the scheduler for the given state.
Definition Scheduler.cpp:38
A class that holds a possibly non-square matrix in the compressed row storage format.
index_type getRowCount() const
Returns the number of rows of the matrix.
void scaleRowsInPlace(std::vector< value_type > const &factors)
Scales each row of the matrix, i.e., multiplies each element in row i with factors[i].
A class that provides convenience operations to display run times.
#define STORM_LOG_WARN_COND(cond, message)
Definition macros.h:54
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
ValueType abs(ValueType const &number)
ValueType zero()
Definition constants.cpp:26
TargetType convertNumber(SourceType const &number)
solver::UncertaintyResolutionMode UncertaintyResolutionMode
solver::OptimizationDirection OptimizationDirection