14template<
typename ValueType>
16 std::optional<std::vector<ValueType>>
const& optionalStateActionRewardVector,
18 : optionalStateRewardVector(optionalStateRewardVector),
19 optionalStateActionRewardVector(optionalStateActionRewardVector),
20 optionalTransitionRewardMatrix(optionalTransitionRewardMatrix) {
24template<
typename ValueType>
26 std::optional<std::vector<ValueType>>&& optionalStateActionRewardVector,
28 : optionalStateRewardVector(
std::move(optionalStateRewardVector)),
29 optionalStateActionRewardVector(
std::move(optionalStateActionRewardVector)),
30 optionalTransitionRewardMatrix(
std::move(optionalTransitionRewardMatrix)) {
34template<
typename ValueType>
36 return static_cast<bool>(this->optionalStateRewardVector);
39template<
typename ValueType>
41 return static_cast<bool>(this->optionalStateRewardVector) && !
static_cast<bool>(this->optionalStateActionRewardVector) &&
42 !
static_cast<bool>(this->optionalTransitionRewardMatrix);
45template<
typename ValueType>
48 return this->optionalStateRewardVector.value();
51template<
typename ValueType>
54 return this->optionalStateRewardVector.value();
57template<
typename ValueType>
59 return this->optionalStateRewardVector;
62template<
typename ValueType>
65 STORM_LOG_ASSERT(state < this->optionalStateRewardVector.value().size(),
"Invalid state.");
66 return this->optionalStateRewardVector.value()[state];
69template<
typename ValueType>
73 STORM_LOG_ASSERT(state < this->optionalStateRewardVector.value().size(),
"Invalid state.");
74 this->optionalStateRewardVector.value()[state] = newReward;
77template<
typename ValueType>
79 return static_cast<bool>(this->optionalStateActionRewardVector);
82template<
typename ValueType>
84 STORM_LOG_ASSERT(this->hasStateActionRewards(),
"No state action rewards available.");
85 return this->optionalStateActionRewardVector.value();
88template<
typename ValueType>
90 STORM_LOG_ASSERT(this->hasStateActionRewards(),
"No state action rewards available.");
91 return this->optionalStateActionRewardVector.value();
94template<
typename ValueType>
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];
101template<
typename ValueType>
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;
109template<
typename ValueType>
111 return this->optionalStateActionRewardVector;
114template<
typename ValueType>
116 return static_cast<bool>(this->optionalTransitionRewardMatrix);
119template<
typename ValueType>
121 return this->optionalTransitionRewardMatrix.value();
124template<
typename ValueType>
126 return this->optionalTransitionRewardMatrix.value();
129template<
typename ValueType>
131 return this->optionalTransitionRewardMatrix;
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());
142 std::optional<storm::storage::SparseMatrix<ValueType>> newTransitionRewardMatrix;
143 if (this->hasTransitionRewards()) {
144 newTransitionRewardMatrix = this->getTransitionRewardMatrix().restrictRows(enabledActions);
146 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
149template<
typename ValueType>
151 std::optional<std::vector<ValueType>> newStateRewardVector(this->getOptionalStateRewardVector());
152 std::optional<std::vector<ValueType>> newStateActionRewardVector;
153 if (this->hasStateActionRewards()) {
156 std::optional<storm::storage::SparseMatrix<ValueType>> newTransitionRewardMatrix;
157 if (this->hasTransitionRewards()) {
158 newTransitionRewardMatrix = this->getTransitionRewardMatrix().permuteRows(inversePermutation);
160 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
163template<
typename ValueType>
167 std::optional<std::vector<ValueType>> newStateRewardVector;
168 if (hasStateRewards()) {
171 std::optional<std::vector<ValueType>> newStateActionRewardVector;
172 if (this->hasStateActionRewards()) {
173 if (rowGroupIndices) {
174 newStateActionRewardVector =
177 STORM_LOG_ASSERT(inversePermutation.size() == this->getStateActionRewardVector().size(),
"Invalid permutation size.");
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());
189 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
192template<
typename ValueType>
193template<
typename MatrixValueType>
196 ValueType result = this->hasStateActionRewards() ? this->getStateActionReward(choiceIndex) : storm::utility::zero<ValueType>();
197 if (this->hasTransitionRewards()) {
203template<
typename ValueType>
204template<
typename MatrixValueType>
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);
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(),
223 this->getStateActionRewardVector());
224 this->optionalTransitionRewardMatrix = std::nullopt;
230 if (reduceToStateRewards && this->hasStateActionRewards()) {
232 "The reduction to state rewards is only possible if the size of the action reward vector equals the number of states.");
234 if (this->hasStateRewards()) {
235 storm::utility::vector::applyPointwiseTernary<ValueType, MatrixValueType, ValueType>(
236 this->getStateActionRewardVector(), *weights, this->getStateRewardVector(),
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());
244 if (this->hasStateRewards()) {
245 storm::utility::vector::addVectors<ValueType>(this->getStateActionRewardVector(), this->getStateRewardVector(), this->getStateRewardVector());
247 this->optionalStateRewardVector = std::move(this->optionalStateActionRewardVector);
250 this->optionalStateActionRewardVector = std::nullopt;
254template<
typename ValueType>
255template<
typename MatrixValueType>
258 : (this->hasStateActionRewards() ? this->getStateActionRewardVector()
259 : std::vector<ValueType>(transitionMatrix.
getRowCount()));
260 if (this->hasStateActionRewards() && this->hasTransitionRewards()) {
263 if (this->hasStateRewards()) {
269template<
typename ValueType>
270template<
typename MatrixValueType>
272 std::vector<MatrixValueType>
const& weights)
const {
273 std::vector<ValueType> result;
274 if (this->hasTransitionRewards()) {
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);
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; });
289 if (this->hasStateRewards()) {
295template<
typename ValueType>
296template<
typename MatrixValueType>
300 std::vector<ValueType> result(numberOfRows);
301 if (this->hasTransitionRewards()) {
306 if (this->hasStateActionRewards()) {
310 if (this->hasStateRewards()) {
316template<
typename ValueType>
317template<
typename MatrixValueType>
319 std::vector<MatrixValueType>
const& stateRewardWeights)
const {
320 std::vector<ValueType> result;
321 if (this->hasTransitionRewards()) {
324 result = std::vector<ValueType>(transitionMatrix.
getRowCount());
326 if (this->hasStateActionRewards()) {
329 if (this->hasStateRewards()) {
330 std::vector<ValueType> scaledStateRewardVector(transitionMatrix.
getRowGroupCount());
337template<
typename ValueType>
338template<
typename MatrixValueType>
340 return getStatesWithFilter(transitionMatrix, storm::utility::isZero<ValueType>);
343template<
typename ValueType>
344template<
typename MatrixValueType>
346 std::function<
bool(
ValueType const&)>
const& filter)
const {
349 if (this->hasStateActionRewards()) {
350 for (uint_fast64_t state = 0; state < transitionMatrix.
getRowGroupCount(); ++state) {
352 if (!filter(this->getStateActionRewardVector()[row])) {
353 result.
set(state,
false);
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);
372template<
typename ValueType>
373template<
typename MatrixValueType>
376 return getChoicesWithFilter(transitionMatrix, storm::utility::isZero<ValueType>);
379template<
typename ValueType>
380template<
typename MatrixValueType>
382 std::function<
bool(
ValueType const&)>
const& filter)
const {
384 if (this->hasStateActionRewards()) {
386 if (this->hasStateRewards()) {
390 if (this->hasStateRewards()) {
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);
409template<
typename ValueType>
410template<
typename MatrixValueType>
412 if (hasStateRewards()) {
413 getStateRewardVector()[state] = storm::utility::zero<ValueType>();
415 if (hasStateActionRewards()) {
417 getStateActionRewardVector()[choice] = storm::utility::zero<ValueType>();
420 if (hasTransitionRewards()) {
421 for (
auto& entry : getTransitionRewardMatrix().getRowGroup(state)) {
422 entry.setValue(storm::utility::zero<ValueType>());
427template<
typename ValueType>
429 return !(
static_cast<bool>(this->optionalStateRewardVector) ||
static_cast<bool>(this->optionalStateActionRewardVector) ||
430 static_cast<bool>(this->optionalTransitionRewardMatrix));
436template<
typename ValueType>
447 if (predicate(entry.getValue())) {
455template<
typename ValueType>
457 bool const hasNonZeroReward =
anyOfRewardValues(*
this, [](
auto&& value) {
return !storm::utility::isZero<ValueType>(value); });
458 return !hasNonZeroReward;
461template<
typename ValueType>
463 if constexpr (std::is_same_v<ValueType, storm::RationalFunction>) {
465 STORM_LOG_THROW(
false, storm::exceptions::InvalidOperationException,
"Checking Rational functions for negativity is not possible.");
468 return anyOfRewardValues(*
this, [](
auto&& value) {
return value < storm::utility::zero<ValueType>(); });
472template<
typename ValueType>
474 if constexpr (std::is_same_v<ValueType, storm::RationalFunction>) {
476 STORM_LOG_THROW(
false, storm::exceptions::InvalidOperationException,
"Checking Rational functions for negativity is not possible.");
479 return anyOfRewardValues(*
this, [](
auto&& value) {
return value > storm::utility::zero<ValueType>(); });
483template<
typename ValueType>
485 if (hasStateRewards()) {
486 if (optionalStateRewardVector.value().size() != nrStates)
489 if (hasStateActionRewards()) {
490 if (optionalStateActionRewardVector.value().size() != nrChoices)
496template<
typename ValueType>
499 if (hasStateRewards()) {
500 boost::hash_combine(seed, boost::hash_range(optionalStateRewardVector->begin(), optionalStateRewardVector->end()));
502 if (hasStateActionRewards()) {
503 boost::hash_combine(seed, boost::hash_range(optionalStateActionRewardVector->begin(), optionalStateActionRewardVector->end()));
505 if (hasTransitionRewards()) {
506 boost::hash_combine(seed, optionalTransitionRewardMatrix->hash());
511template<
typename ValueType>
513 out << std::boolalpha <<
"reward model [state reward: " << rewardModel.
hasStateRewards()
520 std::set<storm::RationalFunctionVariable> vars;
526 vars.insert(tmp.begin(), tmp.end());
530 vars.insert(tmp.begin(), tmp.end());
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;
554 double const& stateRewardWeight,
double const& actionRewardWeight)
const;
557 std::vector<double>
const* weights);
560template class StandardRewardModel<double>;
583 storm::RationalNumber
const& stateRewardWeight, storm::RationalNumber
const& actionRewardWeight)
const;
585 bool reduceToStateRewards,
586 std::vector<storm::RationalNumber>
const* weights);
619 std::vector<storm::RationalFunction>
const* weights);
631 std::vector<double>
const& weights)
const;
656 bool reduceToStateRewards, std::vector<double>
const* weights);
658 bool reduceToStateRewards, std::vector<storm::Interval>
const* weights);
Helper class that optionally holds a reference to an object of type T.
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 hasNegativeRewards() const
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.
bool hasPositiveRewards() const
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.
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)
#define STORM_LOG_THROW(cond, exception, message)
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...
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.
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...
void addFilteredVectorGroupsToGroupedVector(std::vector< T > &target, std::vector< T > const &source, storm::storage::BitVector const &filter, std::vector< uint_fast64_t > const &rowGroupIndices)
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...
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.
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...
std::vector< T > applyInversePermutation(std::vector< uint64_t > const &inversePermutation, std::vector< T > const &source)
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)