Storm 1.13.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Multiplier.cpp
Go to the documentation of this file.
2#include <type_traits>
3
17
18namespace storm {
19namespace solver {
20
21template<typename ValueType, typename SolutionType>
25
26template<typename ValueType, typename SolutionType>
30
31template<typename ValueType, typename SolutionType>
32void Multiplier<ValueType, SolutionType>::multiplyAndReduce(Environment const& env, OptimizationDirection const& dir, std::vector<SolutionType> const& x,
33 std::vector<ValueType> const* b, std::vector<SolutionType>& result,
34 UncertaintyResolutionMode const& uncertaintyResolutionMode,
35 std::vector<uint_fast64_t>* choices) const {
36 multiplyAndReduce(env, dir, this->matrix.getRowGroupIndices(), x, b, result, uncertaintyResolutionMode, choices);
37}
38
39template<typename ValueType, typename SolutionType>
41 std::vector<ValueType> const* b, std::vector<uint_fast64_t>* choices,
42 bool backwards) const {
43 multiplyAndReduceGaussSeidel(env, dir, this->matrix.getRowGroupIndices(), x, b, choices, backwards);
44}
45
46template<typename ValueType, typename SolutionType>
47void Multiplier<ValueType, SolutionType>::repeatedMultiply(Environment const& env, std::vector<SolutionType>& x, std::vector<ValueType> const* b,
48 uint64_t n) const {
49 storm::utility::ProgressMeasurement progress("multiplications");
50 progress.setMaxCount(n);
51 progress.startNewMeasurement(0);
52 for (uint64_t i = 0; i < n; ++i) {
53 progress.updateProgress(i);
54 multiply(env, x, b, x);
56 STORM_LOG_WARN("Aborting after " << i << " of " << n << " multiplications.");
57 break;
58 }
59 }
60}
61
62template<typename ValueType, typename SolutionType>
64 std::vector<ValueType> const* b, uint64_t n,
65 UncertaintyResolutionMode const& uncertaintyResolutionMode) const {
66 storm::utility::ProgressMeasurement progress("multiplications");
67 progress.setMaxCount(n);
68 progress.startNewMeasurement(0);
69 for (uint64_t i = 0; i < n; ++i) {
70 progress.updateProgress(i);
71 multiplyAndReduce(env, dir, x, b, x, uncertaintyResolutionMode);
73 STORM_LOG_WARN("Aborting after " << i << " of " << n << " multiplications");
74 break;
75 }
76 }
77}
78
79template<typename ValueType, typename SolutionType>
81 std::vector<SolutionType>& x, std::vector<ValueType> const* b, uint64_t n,
82 SolutionType factor,
83 UncertaintyResolutionMode const& uncertaintyResolutionMode) const {
84 storm::utility::ProgressMeasurement progress("multiplications");
85 progress.setMaxCount(n);
86 progress.startNewMeasurement(0);
87 for (uint64_t i = 0; i < n; ++i) {
88 progress.updateProgress(i);
89 std::transform(x.begin(), x.end(), x.begin(), [factor](SolutionType& c) { return c * factor; });
90 multiplyAndReduce(env, dir, x, b, x, uncertaintyResolutionMode);
92 STORM_LOG_WARN("Aborting after " << i << " of " << n << " multiplications");
93 break;
94 }
95 }
96}
97
98template<typename ValueType, typename SolutionType>
99void Multiplier<ValueType, SolutionType>::repeatedMultiplyWithFactor(Environment const& env, std::vector<SolutionType>& x, std::vector<ValueType> const* b,
100 uint64_t n, SolutionType factor) const {
101 storm::utility::ProgressMeasurement progress("multiplications");
102 progress.setMaxCount(n);
103 progress.startNewMeasurement(0);
104 for (uint64_t i = 0; i < n; ++i) {
105 progress.updateProgress(i);
106 std::transform(x.begin(), x.end(), x.begin(), [factor](SolutionType& c) { return c * factor; });
107 multiply(env, x, b, x);
109 STORM_LOG_WARN("Aborting after " << i << " of " << n << " multiplications");
110 break;
111 }
113}
114
115template<typename ValueType, typename SolutionType>
116std::vector<SolutionType>& Multiplier<ValueType, SolutionType>::provideCachedVector(uint64_t size) const {
117 if (this->cachedVector) {
118 this->cachedVector->resize(size);
119 } else {
120 this->cachedVector = std::make_unique<std::vector<SolutionType>>(size);
121 }
122 return *this->cachedVector;
123}
124
125template<typename ValueType, typename SolutionType>
126std::unique_ptr<Multiplier<ValueType, SolutionType>> MultiplierFactory<ValueType, SolutionType>::create(Environment const& env,
128 auto type = env.solver().multiplier().getType();
129
130 // Adjust the type if the ValueType is not supported
131 if (type == MultiplierType::ViOperator &&
132 (std::is_same_v<ValueType, storm::RationalFunction> || (storm::IsIntervalType<ValueType> && storm::IsIntervalType<SolutionType>))) {
133 STORM_LOG_INFO("Switching multiplier type from 'vioperator' to 'native' because the given ValueType is not supported by the VI Operator multiplier.");
134 type = MultiplierType::Native;
135 }
136
137 switch (type) {
138 case MultiplierType::ViOperator:
139 if constexpr (std::is_same_v<ValueType, storm::RationalFunction> || (storm::IsIntervalType<ValueType> && storm::IsIntervalType<SolutionType>)) {
140 throw storm::exceptions::NotImplementedException() << "VI Operator multiplier not supported with given value type.";
141 }
142 if (matrix.hasTrivialRowGrouping()) {
143 return std::make_unique<ViOperatorMultiplier<ValueType, true, SolutionType>>(matrix);
144 } else {
145 return std::make_unique<ViOperatorMultiplier<ValueType, false, SolutionType>>(matrix);
146 }
147 case MultiplierType::Native:
148 if constexpr (std::is_same_v<ValueType, SolutionType>) {
149 return std::make_unique<NativeMultiplier<ValueType>>(matrix);
150 } else {
151 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException, "Native multiplier not implemented for unequal ValueType and SolutionType.");
152 }
153 }
154 STORM_LOG_THROW(false, storm::exceptions::IllegalArgumentException, "Unknown MultiplierType");
155}
156
157template class Multiplier<double>;
158template class MultiplierFactory<double>;
SolverEnvironment & solver()
storm::solver::MultiplierType const & getType() const
MultiplierEnvironment & multiplier()
std::unique_ptr< Multiplier< ValueType, SolutionType > > create(Environment const &env, storm::storage::SparseMatrix< ValueType > const &matrix)
Multiplier(storm::storage::SparseMatrix< ValueType > const &matrix)
void repeatedMultiplyWithFactor(Environment const &env, std::vector< SolutionType > &x, std::vector< ValueType > const *b, uint64_t n, SolutionType factor) const
Performs repeated matrix-vector multiplication x' = A*(factor * x) + b.
void multiplyAndReduce(Environment const &env, OptimizationDirection const &dir, std::vector< SolutionType > const &x, std::vector< ValueType > const *b, std::vector< SolutionType > &result, UncertaintyResolutionMode const &uncertaintyResolutionMode=UncertaintyResolutionMode::Unset, std::vector< uint_fast64_t > *choices=nullptr) const
Performs a matrix-vector multiplication x' = A*x + b and then minimizes/maximizes over the row groups...
std::unique_ptr< std::vector< SolutionType > > cachedVector
Definition Multiplier.h:167
void repeatedMultiply(Environment const &env, std::vector< SolutionType > &x, std::vector< ValueType > const *b, uint64_t n) const
Performs repeated matrix-vector multiplication, using x[0] = x and x[i + 1] = A*x[i] + b.
void repeatedMultiplyAndReduceWithFactor(Environment const &env, OptimizationDirection const &dir, std::vector< SolutionType > &x, std::vector< ValueType > const *b, uint64_t n, SolutionType factor, UncertaintyResolutionMode const &uncertaintyResolutionMode=UncertaintyResolutionMode::Unset) const
Performs repeated matrix-vector multiplication x' = A*(factor * x) + b, minimizes/maximizes over the ...
void multiplyAndReduceGaussSeidel(Environment const &env, OptimizationDirection const &dir, std::vector< SolutionType > &x, std::vector< ValueType > const *b, std::vector< uint_fast64_t > *choices=nullptr, bool backwards=true) const
Performs a matrix-vector multiplication in gauss-seidel style and then minimizes/maximizes over the r...
std::vector< SolutionType > & provideCachedVector(uint64_t size) const
virtual void clearCache() const
storm::storage::SparseMatrix< ValueType > const & matrix
Definition Multiplier.h:168
virtual void multiply(Environment const &env, std::vector< SolutionType > const &x, std::vector< ValueType > const *b, std::vector< SolutionType > &result) const =0
Performs a matrix-vector multiplication x' = A*x + b.
void repeatedMultiplyAndReduce(Environment const &env, OptimizationDirection const &dir, std::vector< SolutionType > &x, std::vector< ValueType > const *b, uint64_t n, UncertaintyResolutionMode const &uncertaintyResolutionMode=UncertaintyResolutionMode::Unset) const
Performs repeated matrix-vector multiplication x' = A*x + b and then minimizes/maximizes over the row...
A class that holds a possibly non-square matrix in the compressed row storage format.
A class that provides convenience operations to display run times.
bool updateProgress(uint64_t count)
Updates the progress to the current count and prints it if the delay passed.
void setMaxCount(uint64_t maxCount)
Sets the maximal possible count.
void startNewMeasurement(uint64_t startCount)
Starts a new measurement, dropping all progress information collected so far.
#define STORM_LOG_INFO(message)
Definition logging.h:24
#define STORM_LOG_WARN(message)
Definition logging.h:25
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
bool isTerminate()
Check whether the program should terminate (due to some abort signal).
constexpr bool IsIntervalType
Helper to check if a type is an interval.