13template<
typename ValueType>
15 std::optional<std::vector<ValueType>>
const& optionalStateActionRewardVector,
17 : optionalStateRewardVector(optionalStateRewardVector),
18 optionalStateActionRewardVector(optionalStateActionRewardVector),
19 optionalTransitionRewardMatrix(optionalTransitionRewardMatrix) {
23template<
typename ValueType>
25 std::optional<std::vector<ValueType>>&& optionalStateActionRewardVector,
27 : optionalStateRewardVector(
std::move(optionalStateRewardVector)),
28 optionalStateActionRewardVector(
std::move(optionalStateActionRewardVector)),
29 optionalTransitionRewardMatrix(
std::move(optionalTransitionRewardMatrix)) {
33template<
typename ValueType>
35 return static_cast<bool>(this->optionalStateRewardVector);
38template<
typename ValueType>
40 return static_cast<bool>(this->optionalStateRewardVector) && !
static_cast<bool>(this->optionalStateActionRewardVector) &&
41 !
static_cast<bool>(this->optionalTransitionRewardMatrix);
44template<
typename ValueType>
47 return this->optionalStateRewardVector.value();
50template<
typename ValueType>
53 return this->optionalStateRewardVector.value();
56template<
typename ValueType>
58 return this->optionalStateRewardVector;
61template<
typename ValueType>
64 STORM_LOG_ASSERT(state < this->optionalStateRewardVector.value().size(),
"Invalid state.");
65 return this->optionalStateRewardVector.value()[state];
68template<
typename ValueType>
72 STORM_LOG_ASSERT(state < this->optionalStateRewardVector.value().size(),
"Invalid state.");
73 this->optionalStateRewardVector.value()[state] = newReward;
76template<
typename ValueType>
78 return static_cast<bool>(this->optionalStateActionRewardVector);
81template<
typename ValueType>
83 STORM_LOG_ASSERT(this->hasStateActionRewards(),
"No state action rewards available.");
84 return this->optionalStateActionRewardVector.value();
87template<
typename ValueType>
89 STORM_LOG_ASSERT(this->hasStateActionRewards(),
"No state action rewards available.");
90 return this->optionalStateActionRewardVector.value();
93template<
typename ValueType>
95 STORM_LOG_ASSERT(this->hasStateActionRewards(),
"No state action rewards available.");
96 STORM_LOG_ASSERT(choiceIndex < this->optionalStateActionRewardVector.value().size(),
"Invalid choiceIndex.");
97 return this->optionalStateActionRewardVector.value()[choiceIndex];
100template<
typename ValueType>
103 STORM_LOG_ASSERT(this->hasStateActionRewards(),
"No state action rewards available.");
104 STORM_LOG_ASSERT(choiceIndex < this->optionalStateActionRewardVector.value().size(),
"Invalid choiceIndex.");
105 this->optionalStateActionRewardVector.value()[choiceIndex] = newValue;
108template<
typename ValueType>
110 return this->optionalStateActionRewardVector;
113template<
typename ValueType>
115 return static_cast<bool>(this->optionalTransitionRewardMatrix);
118template<
typename ValueType>
120 return this->optionalTransitionRewardMatrix.value();
123template<
typename ValueType>
125 return this->optionalTransitionRewardMatrix.value();
128template<
typename ValueType>
130 return this->optionalTransitionRewardMatrix;
133template<
typename ValueType>
135 std::optional<std::vector<ValueType>> newStateRewardVector(this->getOptionalStateRewardVector());
136 std::optional<std::vector<ValueType>> newStateActionRewardVector;
137 if (this->hasStateActionRewards()) {
138 newStateActionRewardVector = std::vector<ValueType>(enabledActions.
getNumberOfSetBits());
141 std::optional<storm::storage::SparseMatrix<ValueType>> newTransitionRewardMatrix;
142 if (this->hasTransitionRewards()) {
143 newTransitionRewardMatrix = this->getTransitionRewardMatrix().restrictRows(enabledActions);
145 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
148template<
typename ValueType>
150 std::optional<std::vector<ValueType>> newStateRewardVector(this->getOptionalStateRewardVector());
151 std::optional<std::vector<ValueType>> newStateActionRewardVector;
152 if (this->hasStateActionRewards()) {
155 std::optional<storm::storage::SparseMatrix<ValueType>> newTransitionRewardMatrix;
156 if (this->hasTransitionRewards()) {
157 newTransitionRewardMatrix = this->getTransitionRewardMatrix().permuteRows(inversePermutation);
159 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
162template<
typename ValueType>
166 std::optional<std::vector<ValueType>> newStateRewardVector;
167 if (hasStateRewards()) {
170 std::optional<std::vector<ValueType>> newStateActionRewardVector;
171 if (this->hasStateActionRewards()) {
172 if (rowGroupIndices) {
173 newStateActionRewardVector =
176 STORM_LOG_ASSERT(inversePermutation.size() == this->getStateActionRewardVector().size(),
"Invalid permutation size.");
180 std::optional<storm::storage::SparseMatrix<ValueType>> newTransitionRewardMatrix;
181 if (this->hasTransitionRewards()) {
182 STORM_LOG_ASSERT(permutation,
"Permutation required for transition rewards.");
183 STORM_LOG_ASSERT(rowGroupIndices.has_value() != getTransitionRewardMatrix().hasTrivialRowGrouping(),
184 "Row group indices required for transition rewards.");
185 this->getTransitionRewardMatrix().permuteRowGroupsAndColumns(inversePermutation, permutation.value());
188 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
191template<
typename ValueType>
192template<
typename MatrixValueType>
195 ValueType result = this->hasStateActionRewards() ? this->getStateActionReward(choiceIndex) : storm::utility::zero<ValueType>();
196 if (this->hasTransitionRewards()) {
202template<
typename ValueType>
203template<
typename MatrixValueType>
206 MatrixValueType
const& stateRewardWeight, MatrixValueType
const& actionRewardWeight)
const {
207 ValueType result = actionRewardWeight * getStateActionAndTransitionReward(choiceIndex, transitionMatrix);
208 if (this->hasStateRewards()) {
209 result += stateRewardWeight * this->getStateReward(stateIndex);
214template<
typename ValueType>
215template<
typename MatrixValueType>
217 std::vector<MatrixValueType>
const* weights) {
218 if (this->hasTransitionRewards()) {
219 if (this->hasStateActionRewards()) {
220 storm::utility::vector::addVectors<ValueType>(this->getStateActionRewardVector(),
222 this->getStateActionRewardVector());
223 this->optionalTransitionRewardMatrix = std::nullopt;
229 if (reduceToStateRewards && this->hasStateActionRewards()) {
231 "The reduction to state rewards is only possible if the size of the action reward vector equals the number of states.");
233 if (this->hasStateRewards()) {
234 storm::utility::vector::applyPointwiseTernary<ValueType, MatrixValueType, ValueType>(
235 this->getStateActionRewardVector(), *weights, this->getStateRewardVector(),
238 this->optionalStateRewardVector = std::move(this->optionalStateActionRewardVector);
239 storm::utility::vector::applyPointwise<ValueType, MatrixValueType, ValueType, std::multiplies<>>(
240 this->optionalStateRewardVector.value(), *weights, this->optionalStateRewardVector.value());
243 if (this->hasStateRewards()) {
244 storm::utility::vector::addVectors<ValueType>(this->getStateActionRewardVector(), this->getStateRewardVector(), this->getStateRewardVector());
246 this->optionalStateRewardVector = std::move(this->optionalStateActionRewardVector);
249 this->optionalStateActionRewardVector = std::nullopt;
253template<
typename ValueType>
254template<
typename MatrixValueType>
257 : (this->hasStateActionRewards() ? this->getStateActionRewardVector()
258 : std::vector<ValueType>(transitionMatrix.
getRowCount()));
259 if (this->hasStateActionRewards() && this->hasTransitionRewards()) {
262 if (this->hasStateRewards()) {
268template<
typename ValueType>
269template<
typename MatrixValueType>
271 std::vector<MatrixValueType>
const& weights)
const {
272 std::vector<ValueType> result;
273 if (this->hasTransitionRewards()) {
275 storm::utility::vector::applyPointwiseTernary<MatrixValueType, ValueType, ValueType>(
276 weights, this->getStateActionRewardVector(), result,
277 [](MatrixValueType
const& weight,
ValueType const& rewardElement,
ValueType const& resultElement) {
278 return weight * (resultElement + rewardElement);
281 result = std::vector<ValueType>(transitionMatrix.
getRowCount());
282 if (this->hasStateActionRewards()) {
283 storm::utility::vector::applyPointwise<MatrixValueType, ValueType, ValueType>(
284 weights, this->getStateActionRewardVector(), result,
285 [](MatrixValueType
const& weight,
ValueType const& rewardElement) {
return weight * rewardElement; });
288 if (this->hasStateRewards()) {
294template<
typename ValueType>
295template<
typename MatrixValueType>
299 std::vector<ValueType> result(numberOfRows);
300 if (this->hasTransitionRewards()) {
305 if (this->hasStateActionRewards()) {
309 if (this->hasStateRewards()) {
315template<
typename ValueType>
316template<
typename MatrixValueType>
318 std::vector<MatrixValueType>
const& stateRewardWeights)
const {
319 std::vector<ValueType> result;
320 if (this->hasTransitionRewards()) {
323 result = std::vector<ValueType>(transitionMatrix.
getRowCount());
325 if (this->hasStateActionRewards()) {
328 if (this->hasStateRewards()) {
329 std::vector<ValueType> scaledStateRewardVector(transitionMatrix.
getRowGroupCount());
336template<
typename ValueType>
337template<
typename MatrixValueType>
339 return getStatesWithFilter(transitionMatrix, storm::utility::isZero<ValueType>);
342template<
typename ValueType>
343template<
typename MatrixValueType>
345 std::function<
bool(
ValueType const&)>
const& filter)
const {
348 if (this->hasStateActionRewards()) {
349 for (uint_fast64_t state = 0; state < transitionMatrix.
getRowGroupCount(); ++state) {
351 if (!filter(this->getStateActionRewardVector()[row])) {
352 result.
set(state,
false);
358 if (this->hasTransitionRewards()) {
359 for (uint_fast64_t state = 0; state < transitionMatrix.
getRowGroupCount(); ++state) {
360 for (
auto const& rewardMatrixEntry : this->getTransitionRewardMatrix().getRowGroup(state)) {
361 if (!filter(rewardMatrixEntry.getValue())) {
362 result.
set(state,
false);
371template<
typename ValueType>
372template<
typename MatrixValueType>
375 return getChoicesWithFilter(transitionMatrix, storm::utility::isZero<ValueType>);
378template<
typename ValueType>
379template<
typename MatrixValueType>
381 std::function<
bool(
ValueType const&)>
const& filter)
const {
383 if (this->hasStateActionRewards()) {
385 if (this->hasStateRewards()) {
389 if (this->hasStateRewards()) {
395 if (this->hasTransitionRewards()) {
396 for (uint_fast64_t row = 0; row < transitionMatrix.
getRowCount(); ++row) {
397 for (
auto const& rewardMatrixEntry : this->getTransitionRewardMatrix().getRow(row)) {
398 if (!filter(rewardMatrixEntry.getValue())) {
399 result.
set(row,
false);
408template<
typename ValueType>
409template<
typename MatrixValueType>
411 if (hasStateRewards()) {
412 getStateRewardVector()[state] = storm::utility::zero<ValueType>();
414 if (hasStateActionRewards()) {
416 getStateActionRewardVector()[choice] = storm::utility::zero<ValueType>();
419 if (hasTransitionRewards()) {
420 for (
auto& entry : getTransitionRewardMatrix().getRowGroup(state)) {
421 entry.setValue(storm::utility::zero<ValueType>());
426template<
typename ValueType>
428 return !(
static_cast<bool>(this->optionalStateRewardVector) ||
static_cast<bool>(this->optionalStateActionRewardVector) ||
429 static_cast<bool>(this->optionalTransitionRewardMatrix));
432template<
typename ValueType>
434 if (hasStateRewards() && !std::all_of(getStateRewardVector().begin(), getStateRewardVector().end(), storm::utility::isZero<ValueType>)) {
437 if (hasStateActionRewards() && !std::all_of(getStateActionRewardVector().begin(), getStateActionRewardVector().end(), storm::utility::isZero<ValueType>)) {
440 if (hasTransitionRewards() && !std::all_of(getTransitionRewardMatrix().begin(), getTransitionRewardMatrix().end(),
449template<
typename ValueType>
451 if (hasStateRewards()) {
452 if (optionalStateRewardVector.value().size() != nrStates)
455 if (hasStateActionRewards()) {
456 if (optionalStateActionRewardVector.value().size() != nrChoices)
462template<
typename ValueType>
465 if (hasStateRewards()) {
466 boost::hash_combine(seed, boost::hash_range(optionalStateRewardVector->begin(), optionalStateRewardVector->end()));
468 if (hasStateActionRewards()) {
469 boost::hash_combine(seed, boost::hash_range(optionalStateActionRewardVector->begin(), optionalStateActionRewardVector->end()));
471 if (hasTransitionRewards()) {
472 boost::hash_combine(seed, optionalTransitionRewardMatrix->hash());
477template<
typename ValueType>
479 out << std::boolalpha <<
"reward model [state reward: " << rewardModel.
hasStateRewards()
486 std::set<storm::RationalFunctionVariable> vars;
492 vars.insert(tmp.begin(), tmp.end());
496 vars.insert(tmp.begin(), tmp.end());
507 std::vector<double>
const& weights)
const;
509 std::vector<double>
const& stateRewardWeights)
const;
512 std::function<
bool(
double const&)>
const& filter)
const;
515 std::function<
bool(
double const&)>
const& filter)
const;
520 double const& stateRewardWeight,
double const& actionRewardWeight)
const;
523 std::vector<double>
const* weights);
526template class StandardRewardModel<double>;
529#ifdef STORM_HAVE_CARL
550 storm::RationalNumber
const& stateRewardWeight, storm::RationalNumber
const& actionRewardWeight)
const;
552 bool reduceToStateRewards,
553 std::vector<storm::RationalNumber>
const* weights);
586 std::vector<storm::RationalFunction>
const* weights);
598 std::vector<double>
const& weights)
const;
623 bool reduceToStateRewards, std::vector<double>
const* weights);
625 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.
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).
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...
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.
ValueType getStateActionAndTransitionReward(uint_fast64_t choiceIndex, storm::storage::SparseMatrix< MatrixValueType > const &transitionMatrix) const
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.
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,...
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.
void set(uint_fast64_t index, bool value=true)
Sets the given truth value at the given index.
uint_fast64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
value_type const & getValue() const
Retrieves the value of the matrix entry.
A class that holds a possibly non-square matrix in the compressed row storage format.
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.
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< 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<<< double >(std::ostream &out, StandardRewardModel< double > const &rewardModel)
std::ostream & operator<<(std::ostream &out, ChoiceLabeling const &labeling)
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)
bool isZero(ValueType const &a)