Storm 1.11.1.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(), UncertaintyResolutionMode::Robust);
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 UncertaintyResolutionMode uncertaintyResolutionMode) const {}
91
92template<>
93void DiscountingHelper<storm::RationalNumber, true>::extractScheduler(std::vector<storm::RationalNumber>& x, std::vector<storm::RationalNumber> const& b,
94 OptimizationDirection const& dir,
95 UncertaintyResolutionMode uncertaintyResolutionMode) const {}
96
97template<typename ValueType, bool TrivialRowGrouping>
98void DiscountingHelper<ValueType, TrivialRowGrouping>::extractScheduler(std::vector<ValueType>& x, std::vector<ValueType> const& b,
99 OptimizationDirection const& dir,
100 UncertaintyResolutionMode uncertaintyResolutionMode) const {
101 // Make sure that storage for scheduler choices is available
102 if (!this->schedulerChoices) {
103 this->schedulerChoices = std::vector<uint64_t>(x.size(), 0);
104 } else {
105 this->schedulerChoices->resize(x.size(), 0);
106 }
107 // Set the correct choices.
108 STORM_LOG_WARN_COND(viOperator, "Expected VI operator to be initialized for scheduler extraction. Initializing now, but this is inefficient.");
109 if (!viOperator) {
110 setUpViOperator();
111 }
113 schedHelper.computeScheduler(x, b, dir, *this->schedulerChoices, uncertaintyResolutionMode, nullptr);
114}
115
116template<typename ValueType, bool TrivialRowGrouping>
118 this->trackScheduler = trackScheduler;
119 if (!this->trackScheduler) {
120 schedulerChoices = boost::none;
121 }
122}
123
124template<typename ValueType, bool TrivialRowGrouping>
126 return this->trackScheduler;
127}
128
129template class DiscountingHelper<double>;
131
134} // namespace helper
135} // namespace modelchecker
136} // 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: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:38
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
ValueType abs(ValueType const &number)