Storm 1.11.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) {
15 progressMeasurement = storm::utility::ProgressMeasurement("iterations");
17 discountedMatrix.scaleRowsInPlace(std::vector<ValueType>(discountedMatrix.getRowCount(), discountFactor));
18 discountedA = discountedMatrix;
19}
20
21template<typename ValueType, bool TrivialRowGrouping>
23 bool trackScheduler)
24 : localA(nullptr), A(&A), discountFactor(discountFactor), trackScheduler(trackScheduler) {
25 progressMeasurement = storm::utility::ProgressMeasurement("iterations");
27 discountedMatrix.scaleRowsInPlace(std::vector<ValueType>(discountedMatrix.getRowCount(), discountFactor));
28 discountedA = discountedMatrix;
29}
30
31template<typename ValueType, bool TrivialRowGrouping>
33 if (!viOperator) {
34 viOperator = std::make_shared<solver::helper::ValueIterationOperator<ValueType, TrivialRowGrouping>>();
35 viOperator->setMatrixBackwards(this->discountedA);
36 }
37}
38
39template<typename ValueType, bool TrivialRowGrouping>
40void DiscountingHelper<ValueType, TrivialRowGrouping>::showProgressIterative(uint64_t iteration) const {
41 progressMeasurement->updateProgress(iteration);
42}
43
44template<typename ValueType, bool TrivialRowGrouping>
46 std::optional<OptimizationDirection> dir, std::vector<ValueType>& x,
47 std::vector<ValueType> const& b) const {
48 setUpViOperator();
49 // This is currently missing progress indications, we can add them later
51 uint64_t numIterations{0};
52 auto viCallback = [&](solver::SolverStatus const& current) { return current; };
53 auto maximalAbsoluteReward = storm::utility::zero<ValueType>();
54 for (auto const& entry : b) {
55 if (storm::utility::abs(entry) > maximalAbsoluteReward) {
56 maximalAbsoluteReward = storm::utility::abs(entry);
57 }
58 }
59 progressMeasurement->startNewMeasurement(0);
60 auto status = viHelper.DiscountedVI(x, b, numIterations, env.solver().minMax().getRelativeTerminationCriterion(),
61 storm::utility::convertNumber<ValueType>(env.solver().minMax().getPrecision()), discountFactor, maximalAbsoluteReward,
62 dir, viCallback, env.solver().minMax().getMultiplicationStyle());
63
64 // If requested, we store the scheduler for retrieval.
65 if (this->isTrackSchedulerSet()) {
66 this->extractScheduler(x, b, dir.value(), true);
67 }
69}
70
71template<typename ValueType, bool TrivialRowGrouping>
73 STORM_LOG_THROW(hasScheduler(), storm::exceptions::IllegalFunctionCallException, "Cannot retrieve scheduler, because none was generated.");
74 storm::storage::Scheduler<ValueType> result(schedulerChoices->size());
75 uint_fast64_t state = 0;
76 for (auto const& schedulerChoice : schedulerChoices.get()) {
77 result.setChoice(schedulerChoice, state);
78 ++state;
79 }
80 return result;
81}
82
83template<typename ValueType, bool TrivialRowGrouping>
85 return static_cast<bool>(schedulerChoices);
86}
87
88template<>
89void DiscountingHelper<double, true>::extractScheduler(std::vector<double>& x, std::vector<double> const& b, OptimizationDirection const& dir,
90 bool robust) const {}
91
92template<>
93void DiscountingHelper<storm::RationalNumber, true>::extractScheduler(std::vector<storm::RationalNumber>& x, std::vector<storm::RationalNumber> const& b,
94 OptimizationDirection const& dir, bool robust) 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, bool robust) const {
99 // Make sure that storage for scheduler choices is available
100 if (!this->schedulerChoices) {
101 this->schedulerChoices = std::vector<uint64_t>(x.size(), 0);
102 } else {
103 this->schedulerChoices->resize(x.size(), 0);
104 }
105 // Set the correct choices.
106 STORM_LOG_WARN_COND(viOperator, "Expected VI operator to be initialized for scheduler extraction. Initializing now, but this is inefficient.");
107 if (!viOperator) {
108 setUpViOperator();
109 }
111 schedHelper.computeScheduler(x, b, dir, *this->schedulerChoices, robust, nullptr);
112}
113
114template<typename ValueType, bool TrivialRowGrouping>
116 this->trackScheduler = trackScheduler;
117 if (!this->trackScheduler) {
118 schedulerChoices = boost::none;
119 }
120}
121
122template<typename ValueType, bool TrivialRowGrouping>
124 return this->trackScheduler;
125}
126
127template class DiscountingHelper<double>;
129
132} // namespace helper
133} // namespace modelchecker
134} // namespace storm
SolverEnvironment & solver()
storm::solver::MultiplicationStyle const & getMultiplicationStyle() const
storm::RationalNumber const & getPrecision() const
bool const & getRelativeTerminationCriterion() const
MinMaxSolverEnvironment & minMax()
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
Helper class to extract optimal scheduler choices from a MinMax equation system solution.
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:37
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:38
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
ValueType abs(ValueType const &number)
LabParser.cpp.