Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
StandardRewardModel.cpp
Go to the documentation of this file.
2
10
11namespace storm {
12namespace models {
13namespace sparse {
14template<typename ValueType>
15StandardRewardModel<ValueType>::StandardRewardModel(std::optional<std::vector<ValueType>> const& optionalStateRewardVector,
16 std::optional<std::vector<ValueType>> const& optionalStateActionRewardVector,
17 std::optional<storm::storage::SparseMatrix<ValueType>> const& optionalTransitionRewardMatrix)
18 : optionalStateRewardVector(optionalStateRewardVector),
19 optionalStateActionRewardVector(optionalStateActionRewardVector),
20 optionalTransitionRewardMatrix(optionalTransitionRewardMatrix) {
21 // Intentionally left empty.
22}
23
24template<typename ValueType>
25StandardRewardModel<ValueType>::StandardRewardModel(std::optional<std::vector<ValueType>>&& optionalStateRewardVector,
26 std::optional<std::vector<ValueType>>&& optionalStateActionRewardVector,
27 std::optional<storm::storage::SparseMatrix<ValueType>>&& optionalTransitionRewardMatrix)
28 : optionalStateRewardVector(std::move(optionalStateRewardVector)),
29 optionalStateActionRewardVector(std::move(optionalStateActionRewardVector)),
30 optionalTransitionRewardMatrix(std::move(optionalTransitionRewardMatrix)) {
31 // Intentionally left empty.
32}
33
34template<typename ValueType>
36 return static_cast<bool>(this->optionalStateRewardVector);
37}
38
39template<typename ValueType>
41 return static_cast<bool>(this->optionalStateRewardVector) && !static_cast<bool>(this->optionalStateActionRewardVector) &&
42 !static_cast<bool>(this->optionalTransitionRewardMatrix);
43}
44
45template<typename ValueType>
46std::vector<ValueType> const& StandardRewardModel<ValueType>::getStateRewardVector() const {
47 STORM_LOG_ASSERT(this->hasStateRewards(), "No state rewards available.");
48 return this->optionalStateRewardVector.value();
49}
50
51template<typename ValueType>
53 STORM_LOG_ASSERT(this->hasStateRewards(), "No state rewards available.");
54 return this->optionalStateRewardVector.value();
55}
56
57template<typename ValueType>
58std::optional<std::vector<ValueType>> const& StandardRewardModel<ValueType>::getOptionalStateRewardVector() const {
59 return this->optionalStateRewardVector;
60}
61
62template<typename ValueType>
64 STORM_LOG_ASSERT(this->hasStateRewards(), "No state rewards available.");
65 STORM_LOG_ASSERT(state < this->optionalStateRewardVector.value().size(), "Invalid state.");
66 return this->optionalStateRewardVector.value()[state];
67}
68
69template<typename ValueType>
70template<typename T>
71void StandardRewardModel<ValueType>::setStateReward(uint_fast64_t state, T const& newReward) {
72 STORM_LOG_ASSERT(this->hasStateRewards(), "No state rewards available.");
73 STORM_LOG_ASSERT(state < this->optionalStateRewardVector.value().size(), "Invalid state.");
74 this->optionalStateRewardVector.value()[state] = newReward;
75}
76
77template<typename ValueType>
79 return static_cast<bool>(this->optionalStateActionRewardVector);
80}
81
82template<typename ValueType>
84 STORM_LOG_ASSERT(this->hasStateActionRewards(), "No state action rewards available.");
85 return this->optionalStateActionRewardVector.value();
86}
87
88template<typename ValueType>
90 STORM_LOG_ASSERT(this->hasStateActionRewards(), "No state action rewards available.");
91 return this->optionalStateActionRewardVector.value();
92}
93
94template<typename ValueType>
95ValueType const& StandardRewardModel<ValueType>::getStateActionReward(uint_fast64_t choiceIndex) const {
96 STORM_LOG_ASSERT(this->hasStateActionRewards(), "No state action rewards available.");
97 STORM_LOG_ASSERT(choiceIndex < this->optionalStateActionRewardVector.value().size(), "Invalid choiceIndex.");
98 return this->optionalStateActionRewardVector.value()[choiceIndex];
99}
100
101template<typename ValueType>
102template<typename T>
103void StandardRewardModel<ValueType>::setStateActionReward(uint_fast64_t choiceIndex, T const& newValue) {
104 STORM_LOG_ASSERT(this->hasStateActionRewards(), "No state action rewards available.");
105 STORM_LOG_ASSERT(choiceIndex < this->optionalStateActionRewardVector.value().size(), "Invalid choiceIndex.");
106 this->optionalStateActionRewardVector.value()[choiceIndex] = newValue;
107}
108
109template<typename ValueType>
110std::optional<std::vector<ValueType>> const& StandardRewardModel<ValueType>::getOptionalStateActionRewardVector() const {
111 return this->optionalStateActionRewardVector;
112}
113
114template<typename ValueType>
116 return static_cast<bool>(this->optionalTransitionRewardMatrix);
117}
118
119template<typename ValueType>
121 return this->optionalTransitionRewardMatrix.value();
122}
123
124template<typename ValueType>
126 return this->optionalTransitionRewardMatrix.value();
127}
128
129template<typename ValueType>
130std::optional<storm::storage::SparseMatrix<ValueType>> const& StandardRewardModel<ValueType>::getOptionalTransitionRewardMatrix() const {
131 return this->optionalTransitionRewardMatrix;
132}
133
134template<typename ValueType>
136 std::optional<std::vector<ValueType>> newStateRewardVector(this->getOptionalStateRewardVector());
137 std::optional<std::vector<ValueType>> newStateActionRewardVector;
138 if (this->hasStateActionRewards()) {
139 newStateActionRewardVector = std::vector<ValueType>(enabledActions.getNumberOfSetBits());
140 storm::utility::vector::selectVectorValues(newStateActionRewardVector.value(), enabledActions, this->getStateActionRewardVector());
141 }
142 std::optional<storm::storage::SparseMatrix<ValueType>> newTransitionRewardMatrix;
143 if (this->hasTransitionRewards()) {
144 newTransitionRewardMatrix = this->getTransitionRewardMatrix().restrictRows(enabledActions);
145 }
146 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
147}
148
149template<typename ValueType>
150StandardRewardModel<ValueType> StandardRewardModel<ValueType>::permuteActions(std::vector<uint64_t> const& inversePermutation) const {
151 std::optional<std::vector<ValueType>> newStateRewardVector(this->getOptionalStateRewardVector());
152 std::optional<std::vector<ValueType>> newStateActionRewardVector;
153 if (this->hasStateActionRewards()) {
154 newStateActionRewardVector = storm::utility::vector::applyInversePermutation(inversePermutation, this->getStateActionRewardVector());
155 }
156 std::optional<storm::storage::SparseMatrix<ValueType>> newTransitionRewardMatrix;
157 if (this->hasTransitionRewards()) {
158 newTransitionRewardMatrix = this->getTransitionRewardMatrix().permuteRows(inversePermutation);
159 }
160 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
161}
162
163template<typename ValueType>
165 storm::OptionalRef<std::vector<uint64_t> const> rowGroupIndices,
166 storm::OptionalRef<std::vector<uint64_t> const> permutation) const {
167 std::optional<std::vector<ValueType>> newStateRewardVector;
168 if (hasStateRewards()) {
169 newStateRewardVector = storm::utility::vector::applyInversePermutation(inversePermutation, getStateRewardVector());
170 }
171 std::optional<std::vector<ValueType>> newStateActionRewardVector;
172 if (this->hasStateActionRewards()) {
173 if (rowGroupIndices) {
174 newStateActionRewardVector =
175 storm::utility::vector::applyInversePermutationToGroupedVector(inversePermutation, this->getStateActionRewardVector(), rowGroupIndices.value());
176 } else {
177 STORM_LOG_ASSERT(inversePermutation.size() == this->getStateActionRewardVector().size(), "Invalid permutation size.");
178 newStateActionRewardVector = storm::utility::vector::applyInversePermutation(inversePermutation, this->getStateActionRewardVector());
179 }
180 }
181 std::optional<storm::storage::SparseMatrix<ValueType>> newTransitionRewardMatrix;
182 if (this->hasTransitionRewards()) {
183 STORM_LOG_ASSERT(permutation, "Permutation required for transition rewards.");
184 STORM_LOG_ASSERT(rowGroupIndices.has_value() != getTransitionRewardMatrix().hasTrivialRowGrouping(),
185 "Row group indices required for transition rewards.");
186 this->getTransitionRewardMatrix().permuteRowGroupsAndColumns(inversePermutation, permutation.value());
187 }
188
189 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
190}
191
192template<typename ValueType>
193template<typename MatrixValueType>
195 storm::storage::SparseMatrix<MatrixValueType> const& transitionMatrix) const {
196 ValueType result = this->hasStateActionRewards() ? this->getStateActionReward(choiceIndex) : storm::utility::zero<ValueType>();
197 if (this->hasTransitionRewards()) {
198 result += transitionMatrix.getPointwiseProductRowSum(getTransitionRewardMatrix(), choiceIndex);
199 }
200 return result;
201}
202
203template<typename ValueType>
204template<typename MatrixValueType>
205ValueType StandardRewardModel<ValueType>::getTotalStateActionReward(uint_fast64_t stateIndex, uint_fast64_t choiceIndex,
206 storm::storage::SparseMatrix<MatrixValueType> const& transitionMatrix,
207 MatrixValueType const& stateRewardWeight, MatrixValueType const& actionRewardWeight) const {
208 ValueType result = actionRewardWeight * getStateActionAndTransitionReward(choiceIndex, transitionMatrix);
209 if (this->hasStateRewards()) {
210 result += stateRewardWeight * this->getStateReward(stateIndex);
211 }
212 return result;
213}
214
215template<typename ValueType>
216template<typename MatrixValueType>
218 std::vector<MatrixValueType> const* weights) {
219 if (this->hasTransitionRewards()) {
220 if (this->hasStateActionRewards()) {
221 storm::utility::vector::addVectors<ValueType>(this->getStateActionRewardVector(),
222 transitionMatrix.getPointwiseProductRowSumVector(this->getTransitionRewardMatrix()),
223 this->getStateActionRewardVector());
224 this->optionalTransitionRewardMatrix = std::nullopt;
225 } else {
226 this->optionalStateActionRewardVector = transitionMatrix.getPointwiseProductRowSumVector(this->getTransitionRewardMatrix());
227 }
228 }
229
230 if (reduceToStateRewards && this->hasStateActionRewards()) {
231 STORM_LOG_THROW(transitionMatrix.getRowGroupCount() == this->getStateActionRewardVector().size(), storm::exceptions::InvalidOperationException,
232 "The reduction to state rewards is only possible if the size of the action reward vector equals the number of states.");
233 if (weights) {
234 if (this->hasStateRewards()) {
235 storm::utility::vector::applyPointwiseTernary<ValueType, MatrixValueType, ValueType>(
236 this->getStateActionRewardVector(), *weights, this->getStateRewardVector(),
237 [](ValueType const& sar, MatrixValueType const& w, ValueType const& sr) -> ValueType { return sr + w * sar; });
238 } else {
239 this->optionalStateRewardVector = std::move(this->optionalStateActionRewardVector);
240 storm::utility::vector::applyPointwise<ValueType, MatrixValueType, ValueType, std::multiplies<>>(
241 this->optionalStateRewardVector.value(), *weights, this->optionalStateRewardVector.value());
242 }
243 } else {
244 if (this->hasStateRewards()) {
245 storm::utility::vector::addVectors<ValueType>(this->getStateActionRewardVector(), this->getStateRewardVector(), this->getStateRewardVector());
246 } else {
247 this->optionalStateRewardVector = std::move(this->optionalStateActionRewardVector);
248 }
249 }
250 this->optionalStateActionRewardVector = std::nullopt;
251 }
252}
253
254template<typename ValueType>
255template<typename MatrixValueType>
257 std::vector<ValueType> result = this->hasTransitionRewards() ? transitionMatrix.getPointwiseProductRowSumVector(this->getTransitionRewardMatrix())
258 : (this->hasStateActionRewards() ? this->getStateActionRewardVector()
259 : std::vector<ValueType>(transitionMatrix.getRowCount()));
260 if (this->hasStateActionRewards() && this->hasTransitionRewards()) {
261 storm::utility::vector::addVectors(result, this->getStateActionRewardVector(), result);
262 }
263 if (this->hasStateRewards()) {
264 storm::utility::vector::addVectorToGroupedVector(result, this->getStateRewardVector(), transitionMatrix.getRowGroupIndices());
265 }
266 return result;
267}
268
269template<typename ValueType>
270template<typename MatrixValueType>
272 std::vector<MatrixValueType> const& weights) const {
273 std::vector<ValueType> result;
274 if (this->hasTransitionRewards()) {
275 result = transitionMatrix.getPointwiseProductRowSumVector(this->getTransitionRewardMatrix());
276 storm::utility::vector::applyPointwiseTernary<MatrixValueType, ValueType, ValueType>(
277 weights, this->getStateActionRewardVector(), result,
278 [](MatrixValueType const& weight, ValueType const& rewardElement, ValueType const& resultElement) {
279 return weight * (resultElement + rewardElement);
280 });
281 } else {
282 result = std::vector<ValueType>(transitionMatrix.getRowCount());
283 if (this->hasStateActionRewards()) {
284 storm::utility::vector::applyPointwise<MatrixValueType, ValueType, ValueType>(
285 weights, this->getStateActionRewardVector(), result,
286 [](MatrixValueType const& weight, ValueType const& rewardElement) { return weight * rewardElement; });
287 }
288 }
289 if (this->hasStateRewards()) {
290 storm::utility::vector::addVectorToGroupedVector(result, this->getStateRewardVector(), transitionMatrix.getRowGroupIndices());
291 }
292 return result;
293}
294
295template<typename ValueType>
296template<typename MatrixValueType>
297std::vector<ValueType> StandardRewardModel<ValueType>::getTotalRewardVector(uint_fast64_t numberOfRows,
298 storm::storage::SparseMatrix<MatrixValueType> const& transitionMatrix,
299 storm::storage::BitVector const& filter) const {
300 std::vector<ValueType> result(numberOfRows);
301 if (this->hasTransitionRewards()) {
302 std::vector<ValueType> pointwiseProductRowSumVector = transitionMatrix.getPointwiseProductRowSumVector(this->getTransitionRewardMatrix());
303 storm::utility::vector::selectVectorValues(result, filter, transitionMatrix.getRowGroupIndices(), pointwiseProductRowSumVector);
304 }
305
306 if (this->hasStateActionRewards()) {
307 storm::utility::vector::addFilteredVectorGroupsToGroupedVector(result, this->getStateActionRewardVector(), filter,
308 transitionMatrix.getRowGroupIndices());
309 }
310 if (this->hasStateRewards()) {
311 storm::utility::vector::addFilteredVectorToGroupedVector(result, this->getStateRewardVector(), filter, transitionMatrix.getRowGroupIndices());
312 }
313 return result;
314}
315
316template<typename ValueType>
317template<typename MatrixValueType>
319 std::vector<MatrixValueType> const& stateRewardWeights) const {
320 std::vector<ValueType> result;
321 if (this->hasTransitionRewards()) {
322 result = transitionMatrix.getPointwiseProductRowSumVector(this->getTransitionRewardMatrix());
323 } else {
324 result = std::vector<ValueType>(transitionMatrix.getRowCount());
325 }
326 if (this->hasStateActionRewards()) {
327 storm::utility::vector::addVectors(result, this->getStateActionRewardVector(), result);
328 }
329 if (this->hasStateRewards()) {
330 std::vector<ValueType> scaledStateRewardVector(transitionMatrix.getRowGroupCount());
331 storm::utility::vector::multiplyVectorsPointwise(this->getStateRewardVector(), stateRewardWeights, scaledStateRewardVector);
332 storm::utility::vector::addVectorToGroupedVector(result, scaledStateRewardVector, transitionMatrix.getRowGroupIndices());
333 }
334 return result;
335}
336
337template<typename ValueType>
338template<typename MatrixValueType>
340 return getStatesWithFilter(transitionMatrix, storm::utility::isZero<ValueType>);
341}
342
343template<typename ValueType>
344template<typename MatrixValueType>
346 std::function<bool(ValueType const&)> const& filter) const {
347 storm::storage::BitVector result = this->hasStateRewards() ? storm::utility::vector::filter(this->getStateRewardVector(), filter)
348 : storm::storage::BitVector(transitionMatrix.getRowGroupCount(), true);
349 if (this->hasStateActionRewards()) {
350 for (uint_fast64_t state = 0; state < transitionMatrix.getRowGroupCount(); ++state) {
351 for (uint_fast64_t row = transitionMatrix.getRowGroupIndices()[state]; row < transitionMatrix.getRowGroupIndices()[state + 1]; ++row) {
352 if (!filter(this->getStateActionRewardVector()[row])) {
353 result.set(state, false);
354 break;
355 }
356 }
357 }
358 }
359 if (this->hasTransitionRewards()) {
360 for (uint_fast64_t state = 0; state < transitionMatrix.getRowGroupCount(); ++state) {
361 for (auto const& rewardMatrixEntry : this->getTransitionRewardMatrix().getRowGroup(state)) {
362 if (!filter(rewardMatrixEntry.getValue())) {
363 result.set(state, false);
364 break;
365 }
366 }
367 }
368 }
369 return result;
370}
371
372template<typename ValueType>
373template<typename MatrixValueType>
375 storm::storage::SparseMatrix<MatrixValueType> const& transitionMatrix) const {
376 return getChoicesWithFilter(transitionMatrix, storm::utility::isZero<ValueType>);
377}
378
379template<typename ValueType>
380template<typename MatrixValueType>
382 std::function<bool(ValueType const&)> const& filter) const {
384 if (this->hasStateActionRewards()) {
385 result = storm::utility::vector::filter(this->getStateActionRewardVector(), filter);
386 if (this->hasStateRewards()) {
387 result &= transitionMatrix.getRowFilter(storm::utility::vector::filter(this->getStateRewardVector(), filter));
388 }
389 } else {
390 if (this->hasStateRewards()) {
391 result = transitionMatrix.getRowFilter(storm::utility::vector::filter(this->getStateRewardVector(), filter));
392 } else {
393 result = storm::storage::BitVector(transitionMatrix.getRowCount(), true);
394 }
395 }
396 if (this->hasTransitionRewards()) {
397 for (uint_fast64_t row = 0; row < transitionMatrix.getRowCount(); ++row) {
398 for (auto const& rewardMatrixEntry : this->getTransitionRewardMatrix().getRow(row)) {
399 if (!filter(rewardMatrixEntry.getValue())) {
400 result.set(row, false);
401 break;
402 }
403 }
404 }
405 }
406 return result;
407}
408
409template<typename ValueType>
410template<typename MatrixValueType>
412 if (hasStateRewards()) {
413 getStateRewardVector()[state] = storm::utility::zero<ValueType>();
414 }
415 if (hasStateActionRewards()) {
416 for (uint_fast64_t choice = transitions.getRowGroupIndices()[state]; choice < transitions.getRowGroupIndices()[state + 1]; ++choice) {
417 getStateActionRewardVector()[choice] = storm::utility::zero<ValueType>();
418 }
419 }
420 if (hasTransitionRewards()) {
421 for (auto& entry : getTransitionRewardMatrix().getRowGroup(state)) {
422 entry.setValue(storm::utility::zero<ValueType>());
423 }
424 }
425}
426
427template<typename ValueType>
429 return !(static_cast<bool>(this->optionalStateRewardVector) || static_cast<bool>(this->optionalStateActionRewardVector) ||
430 static_cast<bool>(this->optionalTransitionRewardMatrix));
431}
432
436template<typename ValueType>
437bool anyOfRewardValues(StandardRewardModel<ValueType> const& rewardModel, auto const& predicate) {
438 if (rewardModel.hasStateRewards() && std::any_of(rewardModel.getStateRewardVector().begin(), rewardModel.getStateRewardVector().end(), predicate)) {
439 return true;
440 }
441 if (rewardModel.hasStateActionRewards() &&
442 std::any_of(rewardModel.getStateActionRewardVector().begin(), rewardModel.getStateActionRewardVector().end(), predicate)) {
443 return true;
444 }
445 if (rewardModel.hasTransitionRewards()) {
446 for (auto const& entry : rewardModel.getTransitionRewardMatrix()) {
447 if (predicate(entry.getValue())) {
448 return true;
449 }
450 }
451 }
452 return false;
453}
454
455template<typename ValueType>
457 bool const hasNonZeroReward = anyOfRewardValues(*this, [](auto&& value) { return !storm::utility::isZero<ValueType>(value); });
458 return !hasNonZeroReward;
459}
460
461template<typename ValueType>
463 if constexpr (std::is_same_v<ValueType, storm::RationalFunction>) {
464 // Rational functions can never be negative.
465 STORM_LOG_THROW(false, storm::exceptions::InvalidOperationException, "Checking Rational functions for negativity is not possible.");
466 return false;
467 } else {
468 return anyOfRewardValues(*this, [](auto&& value) { return value < storm::utility::zero<ValueType>(); });
469 }
470}
471
472template<typename ValueType>
474 if constexpr (std::is_same_v<ValueType, storm::RationalFunction>) {
475 // Rational functions can never be negative.
476 STORM_LOG_THROW(false, storm::exceptions::InvalidOperationException, "Checking Rational functions for negativity is not possible.");
477 return false;
478 } else {
479 return anyOfRewardValues(*this, [](auto&& value) { return value > storm::utility::zero<ValueType>(); });
480 }
481}
482
483template<typename ValueType>
484bool StandardRewardModel<ValueType>::isCompatible(uint_fast64_t nrStates, uint_fast64_t nrChoices) const {
485 if (hasStateRewards()) {
486 if (optionalStateRewardVector.value().size() != nrStates)
487 return false;
488 }
489 if (hasStateActionRewards()) {
490 if (optionalStateActionRewardVector.value().size() != nrChoices)
491 return false;
492 }
493 return true;
494}
495
496template<typename ValueType>
498 size_t seed = 0;
499 if (hasStateRewards()) {
500 boost::hash_combine(seed, boost::hash_range(optionalStateRewardVector->begin(), optionalStateRewardVector->end()));
501 }
502 if (hasStateActionRewards()) {
503 boost::hash_combine(seed, boost::hash_range(optionalStateActionRewardVector->begin(), optionalStateActionRewardVector->end()));
504 }
505 if (hasTransitionRewards()) {
506 boost::hash_combine(seed, optionalTransitionRewardMatrix->hash());
507 }
508 return seed;
509}
510
511template<typename ValueType>
512std::ostream& operator<<(std::ostream& out, StandardRewardModel<ValueType> const& rewardModel) {
513 out << std::boolalpha << "reward model [state reward: " << rewardModel.hasStateRewards()
514 << ", state-action rewards: " << rewardModel.hasStateActionRewards() << ", transition rewards: " << rewardModel.hasTransitionRewards() << "]"
515 << std::noboolalpha;
516 return out;
517}
518
519std::set<storm::RationalFunctionVariable> getRewardModelParameters(StandardRewardModel<storm::RationalFunction> const& rewModel) {
520 std::set<storm::RationalFunctionVariable> vars;
521 if (rewModel.hasTransitionRewards()) {
523 }
524 if (rewModel.hasStateActionRewards()) {
525 std::set<storm::RationalFunctionVariable> tmp = storm::utility::vector::getVariables(rewModel.getStateActionRewardVector());
526 vars.insert(tmp.begin(), tmp.end());
527 }
528 if (rewModel.hasStateRewards()) {
529 std::set<storm::RationalFunctionVariable> tmp = storm::utility::vector::getVariables(rewModel.getStateRewardVector());
530 vars.insert(tmp.begin(), tmp.end());
531 }
532 return vars;
533}
534
535// Explicitly instantiate the class.
536template std::vector<double> StandardRewardModel<double>::getTotalRewardVector(storm::storage::SparseMatrix<double> const& transitionMatrix) const;
537template std::vector<double> StandardRewardModel<double>::getTotalRewardVector(uint_fast64_t numberOfRows,
538 storm::storage::SparseMatrix<double> const& transitionMatrix,
539 storm::storage::BitVector const& filter) const;
540template std::vector<double> StandardRewardModel<double>::getTotalRewardVector(storm::storage::SparseMatrix<double> const& transitionMatrix,
541 std::vector<double> const& weights) const;
543 std::vector<double> const& stateRewardWeights) const;
546 std::function<bool(double const&)> const& filter) const;
549 std::function<bool(double const&)> const& filter) const;
550template double StandardRewardModel<double>::getStateActionAndTransitionReward(uint_fast64_t stateIndex,
551 storm::storage::SparseMatrix<double> const& transitionMatrix) const;
552template double StandardRewardModel<double>::getTotalStateActionReward(uint_fast64_t stateIndex, uint_fast64_t choiceIndex,
553 storm::storage::SparseMatrix<double> const& transitionMatrix,
554 double const& stateRewardWeight, double const& actionRewardWeight) const;
555template void StandardRewardModel<double>::clearRewardAtState(uint_fast64_t state, storm::storage::SparseMatrix<double> const& transitionMatrix);
556template void StandardRewardModel<double>::reduceToStateBasedRewards(storm::storage::SparseMatrix<double> const& transitionMatrix, bool reduceToStateRewards,
557 std::vector<double> const* weights);
558template void StandardRewardModel<double>::setStateActionReward(uint_fast64_t choiceIndex, double const& newValue);
559template void StandardRewardModel<double>::setStateReward(uint_fast64_t state, double const& newValue);
560template class StandardRewardModel<double>;
561template std::ostream& operator<< <double>(std::ostream& out, StandardRewardModel<double> const& rewardModel);
562
563template std::vector<storm::RationalNumber> StandardRewardModel<storm::RationalNumber>::getTotalRewardVector(
564 uint_fast64_t numberOfRows, storm::storage::SparseMatrix<storm::RationalNumber> const& transitionMatrix, storm::storage::BitVector const& filter) const;
565template std::vector<storm::RationalNumber> StandardRewardModel<storm::RationalNumber>::getTotalRewardVector(
566 storm::storage::SparseMatrix<storm::RationalNumber> const& transitionMatrix) const;
567template std::vector<storm::RationalNumber> StandardRewardModel<storm::RationalNumber>::getTotalRewardVector(
568 storm::storage::SparseMatrix<storm::RationalNumber> const& transitionMatrix, std::vector<storm::RationalNumber> const& weights) const;
569template std::vector<storm::RationalNumber> StandardRewardModel<storm::RationalNumber>::getTotalActionRewardVector(
570 storm::storage::SparseMatrix<storm::RationalNumber> const& transitionMatrix, std::vector<storm::RationalNumber> const& stateRewardWeights) const;
572 storm::storage::SparseMatrix<storm::RationalNumber> const& transitionMatrix) const;
574 storm::storage::SparseMatrix<storm::RationalNumber> const& transitionMatrix, std::function<bool(storm::RationalNumber const&)> const& filter) const;
576 storm::storage::SparseMatrix<storm::RationalNumber> const& transitionMatrix) const;
578 storm::storage::SparseMatrix<storm::RationalNumber> const& transitionMatrix, std::function<bool(storm::RationalNumber const&)> const& filter) const;
580 uint_fast64_t stateIndex, storm::storage::SparseMatrix<storm::RationalNumber> const& transitionMatrix) const;
582 uint_fast64_t stateIndex, uint_fast64_t choiceIndex, storm::storage::SparseMatrix<storm::RationalNumber> const& transitionMatrix,
583 storm::RationalNumber const& stateRewardWeight, storm::RationalNumber const& actionRewardWeight) const;
585 bool reduceToStateRewards,
586 std::vector<storm::RationalNumber> const* weights);
589template void StandardRewardModel<storm::RationalNumber>::setStateActionReward(uint_fast64_t choiceIndex, storm::RationalNumber const& newValue);
590template void StandardRewardModel<storm::RationalNumber>::setStateReward(uint_fast64_t state, storm::RationalNumber const& newValue);
592template std::ostream& operator<< <storm::RationalNumber>(std::ostream& out, StandardRewardModel<storm::RationalNumber> const& rewardModel);
593
594template std::vector<storm::RationalFunction> StandardRewardModel<storm::RationalFunction>::getTotalRewardVector(
595 uint_fast64_t numberOfRows, storm::storage::SparseMatrix<storm::RationalFunction> const& transitionMatrix, storm::storage::BitVector const& filter) const;
596template std::vector<storm::RationalFunction> StandardRewardModel<storm::RationalFunction>::getTotalRewardVector(
597 storm::storage::SparseMatrix<storm::RationalFunction> const& transitionMatrix) const;
598template std::vector<storm::RationalFunction> StandardRewardModel<storm::RationalFunction>::getTotalRewardVector(
599 storm::storage::SparseMatrix<storm::RationalFunction> const& transitionMatrix, std::vector<storm::RationalFunction> const& weights) const;
601 storm::storage::SparseMatrix<storm::RationalFunction> const& transitionMatrix) const;
603 storm::storage::SparseMatrix<storm::RationalFunction> const& transitionMatrix, std::function<bool(storm::RationalFunction const&)> const& filter) const;
605 storm::storage::SparseMatrix<storm::RationalFunction> const& transitionMatrix) const;
607 storm::storage::SparseMatrix<storm::RationalFunction> const& transitionMatrix, std::function<bool(storm::RationalFunction const&)> const& filter) const;
610template std::vector<storm::RationalFunction> StandardRewardModel<storm::RationalFunction>::getTotalActionRewardVector(
611 storm::storage::SparseMatrix<storm::RationalFunction> const& transitionMatrix, std::vector<storm::RationalFunction> const& stateRewardWeights) const;
613 uint_fast64_t stateIndex, storm::storage::SparseMatrix<storm::RationalFunction> const& transitionMatrix) const;
615 uint_fast64_t stateIndex, uint_fast64_t choiceIndex, storm::storage::SparseMatrix<storm::RationalFunction> const& transitionMatrix,
616 storm::RationalFunction const& stateRewardWeight, storm::RationalFunction const& actionRewardWeight) const;
618 storm::storage::SparseMatrix<storm::RationalFunction> const& transitionMatrix, bool reduceToStateRewards,
619 std::vector<storm::RationalFunction> const* weights);
620template void StandardRewardModel<storm::RationalFunction>::setStateActionReward(uint_fast64_t choiceIndex, storm::RationalFunction const& newValue);
621template void StandardRewardModel<storm::RationalFunction>::setStateReward(uint_fast64_t state, storm::RationalFunction const& newValue);
623template std::ostream& operator<< <storm::RationalFunction>(std::ostream& out, StandardRewardModel<storm::RationalFunction> const& rewardModel);
624
625template std::vector<storm::Interval> StandardRewardModel<storm::Interval>::getTotalRewardVector(uint_fast64_t numberOfRows,
626 storm::storage::SparseMatrix<double> const& transitionMatrix,
627 storm::storage::BitVector const& filter) const;
628template std::vector<storm::Interval> StandardRewardModel<storm::Interval>::getTotalRewardVector(
629 storm::storage::SparseMatrix<double> const& transitionMatrix) const;
630template std::vector<storm::Interval> StandardRewardModel<storm::Interval>::getTotalRewardVector(storm::storage::SparseMatrix<double> const& transitionMatrix,
631 std::vector<double> const& weights) const;
632template std::vector<storm::Interval> StandardRewardModel<storm::Interval>::getTotalRewardVector(
633 uint_fast64_t numberOfRows, storm::storage::SparseMatrix<storm::Interval> const& transitionMatrix, storm::storage::BitVector const& filter) const;
634template std::vector<storm::Interval> StandardRewardModel<storm::Interval>::getTotalRewardVector(
635 storm::storage::SparseMatrix<storm::Interval> const& transitionMatrix) const;
636template std::vector<storm::Interval> StandardRewardModel<storm::Interval>::getTotalRewardVector(
637 storm::storage::SparseMatrix<storm::Interval> const& transitionMatrix, std::vector<storm::Interval> const& weights) const;
638template std::vector<storm::Interval> StandardRewardModel<storm::Interval>::getTotalActionRewardVector(
639 storm::storage::SparseMatrix<double> const& transitionMatrix, std::vector<double> const& stateRewardWeights) const;
641 std::function<bool(storm::Interval const&)> const& filter) const;
643 storm::storage::SparseMatrix<storm::Interval> const& transitionMatrix, std::function<bool(storm::Interval const&)> const& filter) const;
645 std::function<bool(storm::Interval const&)> const& filter) const;
647 storm::storage::SparseMatrix<storm::Interval> const& transitionMatrix, std::function<bool(storm::Interval const&)> const& filter) const;
648template void StandardRewardModel<storm::Interval>::setStateActionReward(uint_fast64_t choiceIndex, double const& newValue);
649template void StandardRewardModel<storm::Interval>::setStateActionReward(uint_fast64_t choiceIndex, storm::Interval const& newValue);
650template void StandardRewardModel<storm::Interval>::setStateReward(uint_fast64_t state, double const& newValue);
651template void StandardRewardModel<storm::Interval>::setStateReward(uint_fast64_t state, storm::Interval const& newValue);
652template void StandardRewardModel<storm::Interval>::clearRewardAtState(uint_fast64_t state, storm::storage::SparseMatrix<double> const& transitionMatrix);
653template void StandardRewardModel<storm::Interval>::clearRewardAtState(uint_fast64_t state,
654 storm::storage::SparseMatrix<storm::Interval> const& transitionMatrix);
656 bool reduceToStateRewards, std::vector<double> const* weights);
658 bool reduceToStateRewards, std::vector<storm::Interval> const* weights);
660 storm::storage::SparseMatrix<storm::Interval> const& transitionMatrix) const;
662 storm::storage::SparseMatrix<storm::Interval> const& transitionMatrix) const;
664template std::ostream& operator<< <storm::Interval>(std::ostream& out, StandardRewardModel<storm::Interval> const& rewardModel);
665} // namespace sparse
666
667} // namespace models
668} // namespace storm
Helper class that optionally holds a reference to an object of type T.
Definition OptionalRef.h:48
storm::storage::SparseMatrix< ValueType > const & getTransitionRewardMatrix() const
Retrieves the transition rewards of the reward model.
bool hasTransitionRewards() const
Retrieves whether the reward model has transition rewards.
StandardRewardModel< ValueType > restrictActions(storm::storage::BitVector const &enabledActions) const
Creates a new reward model by restricting the actions of the action-based rewards.
std::optional< storm::storage::SparseMatrix< ValueType > > const & getOptionalTransitionRewardMatrix() const
Retrieves an optional value that contains the transition reward matrix if there is one.
ValueType getStateActionAndTransitionReward(uint_fast64_t choiceIndex, storm::storage::SparseMatrix< MatrixValueType > const &transitionMatrix) const
void setStateReward(uint_fast64_t state, T const &newReward)
bool hasOnlyStateRewards() const
Retrieves whether the reward model only has state rewards (and hence no other rewards).
ValueType getTotalStateActionReward(uint_fast64_t stateIndex, uint_fast64_t choiceIndex, storm::storage::SparseMatrix< MatrixValueType > const &transitionMatrix, MatrixValueType const &stateRewardWeight=storm::utility::one< MatrixValueType >(), MatrixValueType const &actionRewardWeight=storm::utility::one< MatrixValueType >()) const
Retrieves the total reward for the given state action pair (including (scaled) state rewards,...
ValueType const & getStateReward(uint_fast64_t state) const
std::optional< std::vector< ValueType > > const & getOptionalStateRewardVector() const
Retrieves an optional value that contains the state reward vector if there is one.
std::vector< ValueType > const & getStateActionRewardVector() const
Retrieves the state-action rewards of the reward model.
std::vector< ValueType > const & getStateRewardVector() const
Retrieves the state rewards of the reward model.
storm::storage::BitVector getStatesWithZeroReward(storm::storage::SparseMatrix< MatrixValueType > const &transitionMatrix) const
Returns the set of states at which a all rewards (state-, action- and transition-rewards) are zero.
void clearRewardAtState(uint_fast64_t state, storm::storage::SparseMatrix< MatrixValueType > const &transitions)
Sets all available rewards at this state to zero.
ValueType const & getStateActionReward(uint_fast64_t choiceIndex) const
Retrieves the state-action reward for the given choice.
bool empty() const
Retrieves whether the reward model is empty, i.e.
bool isCompatible(uint_fast64_t nrStates, uint_fast64_t nrChoices) const
Checks whether the reward model is compatible with key model characteristics.
std::vector< ValueType > getTotalRewardVector(storm::storage::SparseMatrix< MatrixValueType > const &transitionMatrix) const
Creates a vector representing the complete reward vector based on the state-, state-action- and trans...
void reduceToStateBasedRewards(storm::storage::SparseMatrix< MatrixValueType > const &transitionMatrix, bool reduceToStateRewards=false, std::vector< MatrixValueType > const *weights=nullptr)
Reduces the transition-based rewards to state-action rewards by taking the average of each row.
std::vector< ValueType > getTotalActionRewardVector(storm::storage::SparseMatrix< MatrixValueType > const &transitionMatrix, std::vector< MatrixValueType > const &stateRewardWeights) const
Creates a vector representing the complete action-based rewards, i.e., state-action- and transition-b...
StandardRewardModel(std::optional< std::vector< ValueType > > const &optionalStateRewardVector=std::nullopt, std::optional< std::vector< ValueType > > const &optionalStateActionRewardVector=std::nullopt, std::optional< storm::storage::SparseMatrix< ValueType > > const &optionalTransitionRewardMatrix=std::nullopt)
Constructs a reward model by copying the given data.
bool hasStateRewards() const
Retrieves whether the reward model has state rewards.
bool hasStateActionRewards() const
Retrieves whether the reward model has state-action rewards.
storm::storage::BitVector getChoicesWithZeroReward(storm::storage::SparseMatrix< MatrixValueType > const &transitionMatrix) const
Returns the set of choices at which all rewards (state-, action- and transition-rewards) are zero.
bool isAllZero() const
Retrieves whether every reward defined by this reward model is zero.
storm::storage::BitVector getChoicesWithFilter(storm::storage::SparseMatrix< MatrixValueType > const &transitionMatrix, std::function< bool(ValueType const &)> const &filter) const
Returns the set of choices for which all associated rewards (state, action or transition rewards) sat...
void setStateActionReward(uint_fast64_t choiceIndex, T const &newValue)
Sets the state-action reward for the given choice.
storm::storage::BitVector getStatesWithFilter(storm::storage::SparseMatrix< MatrixValueType > const &transitionMatrix, std::function< bool(ValueType const &)> const &filter) const
Returns the set of states for which all associated rewards (state, action or transition rewards) sati...
StandardRewardModel< ValueType > permuteActions(std::vector< uint64_t > const &inversePermutation) const
Creates a new reward model by permuting the actions.
StandardRewardModel< ValueType > permuteStates(std::vector< uint64_t > const &inversePermutation, storm::OptionalRef< std::vector< uint64_t > const > rowGroupIndices=storm::NullRef, storm::OptionalRef< std::vector< uint64_t > const > permutation=storm::NullRef) const
Creates a new reward model by permuting the states.
std::optional< std::vector< ValueType > > const & getOptionalStateActionRewardVector() const
Retrieves an optional value that contains the state-action reward vector if there is one.
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
uint64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
void set(uint64_t index, bool value=true)
Sets the given truth value at the given index.
A class that holds a possibly non-square matrix in the compressed row storage format.
ResultValueType getPointwiseProductRowSum(storm::storage::SparseMatrix< OtherValueType > const &otherMatrix, index_type const &row) const
Performs a pointwise multiplication of the entries in the given row of this matrix and the entries of...
std::vector< ResultValueType > getPointwiseProductRowSumVector(storm::storage::SparseMatrix< OtherValueType > const &otherMatrix) const
Performs a pointwise matrix multiplication of the matrix with the given matrix and returns a vector c...
index_type getRowGroupCount() const
Returns the number of row groups in the matrix.
std::vector< index_type > const & getRowGroupIndices() const
Returns the grouping of rows of this matrix.
index_type getRowCount() const
Returns the number of rows of the matrix.
storm::storage::BitVector getRowFilter(storm::storage::BitVector const &groupConstraint) const
Returns a bitvector representing the set of rows, with all indices set that correspond to one of the ...
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
std::set< storm::RationalFunctionVariable > getRewardModelParameters(StandardRewardModel< storm::RationalFunction > const &rewModel)
template std::ostream & operator<<< storm::RationalFunction >(std::ostream &out, StandardRewardModel< storm::RationalFunction > const &rewardModel)
template std::ostream & operator<<< double >(std::ostream &out, StandardRewardModel< double > const &rewardModel)
template std::ostream & operator<<< storm::Interval >(std::ostream &out, StandardRewardModel< storm::Interval > const &rewardModel)
bool anyOfRewardValues(StandardRewardModel< ValueType > const &rewardModel, auto const &predicate)
Auxiliary function to check if the given predicate is true for any of the reward values in the given ...
template std::ostream & operator<<< storm::RationalNumber >(std::ostream &out, StandardRewardModel< storm::RationalNumber > const &rewardModel)
std::set< storm::RationalFunctionVariable > getVariables(SparseMatrix< storm::RationalFunction > const &matrix)
storm::storage::BitVector filter(std::vector< T > const &values, std::function< bool(T const &value)> const &function)
Retrieves a bit vector containing all the indices for which the value at this position makes the give...
Definition vector.h:486
void addVectors(std::vector< InValueType1 > const &firstOperand, std::vector< InValueType2 > const &secondOperand, std::vector< OutValueType > &target)
Adds the two given vectors and writes the result to the target vector.
Definition vector.h:399
void addVectorToGroupedVector(std::vector< T > &target, std::vector< T > const &source, std::vector< uint_fast64_t > const &rowGroupIndices)
Adds the source vector to the target vector in a way such that the i-th entry is added to all element...
Definition vector.h:305
void addFilteredVectorGroupsToGroupedVector(std::vector< T > &target, std::vector< T > const &source, storm::storage::BitVector const &filter, std::vector< uint_fast64_t > const &rowGroupIndices)
Definition vector.h:284
void selectVectorValues(std::vector< T > &vector, storm::storage::BitVector const &positions, std::vector< T > const &values)
Selects the elements from a vector at the specified positions and writes them consecutively into anot...
Definition vector.h:184
void multiplyVectorsPointwise(std::vector< InValueType1 > const &firstOperand, std::vector< InValueType2 > const &secondOperand, std::vector< OutValueType > &target)
Multiplies the two given vectors (pointwise) and writes the result to the target vector.
Definition vector.h:423
void addFilteredVectorToGroupedVector(std::vector< T > &target, std::vector< T > const &source, storm::storage::BitVector const &filter, std::vector< uint_fast64_t > const &rowGroupIndices)
Adds the source vector to the target vector in a way such that the i-th selected entry is added to al...
Definition vector.h:330
std::vector< T > applyInversePermutation(std::vector< uint64_t > const &inversePermutation, std::vector< T > const &source)
Definition vector.h:1114
std::set< storm::RationalFunctionVariable > getVariables(std::vector< storm::RationalFunction > const &vector)
std::vector< T > applyInversePermutationToGroupedVector(std::vector< uint64_t > const &inversePermutation, std::vector< T > const &source, std::vector< uint64_t > const &rowGroupIndices)
Definition vector.h:1124