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>
85 return this->optionalStateActionRewardVector.value();
88template<
typename ValueType>
91 return this->optionalStateActionRewardVector.value();
94template<
typename ValueType>
97 STORM_LOG_ASSERT(choiceIndex < this->optionalStateActionRewardVector.value().size(),
"Invalid choiceIndex.");
98 return this->optionalStateActionRewardVector.value()[choiceIndex];
101template<
typename ValueType>
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>
137 std::optional<std::vector<ValueType>> newStateActionRewardVector;
139 newStateActionRewardVector = std::vector<ValueType>(enabledActions.
getNumberOfSetBits());
142 std::optional<storm::storage::SparseMatrix<ValueType>> newTransitionRewardMatrix;
146 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
149template<
typename ValueType>
152 std::optional<std::vector<ValueType>> newStateActionRewardVector;
156 std::optional<storm::storage::SparseMatrix<ValueType>> newTransitionRewardMatrix;
160 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
163template<
typename ValueType>
167 std::optional<std::vector<ValueType>> newStateRewardVector;
171 std::optional<std::vector<ValueType>> newStateActionRewardVector;
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;
183 STORM_LOG_ASSERT(permutation,
"Permutation required for transition rewards.");
185 "Row group indices required for transition rewards.");
189 return StandardRewardModel(std::move(newStateRewardVector), std::move(newStateActionRewardVector), std::move(newTransitionRewardMatrix));
192template<
typename ValueType>
193template<
typename MatrixValueType>
203template<
typename ValueType>
204template<
typename MatrixValueType>
207 MatrixValueType
const& stateRewardWeight, MatrixValueType
const& actionRewardWeight)
const {
215template<
typename ValueType>
216template<
typename MatrixValueType>
218 std::vector<MatrixValueType>
const* weights) {
221 "Transition reward matrix is not a submatrix of the transition matrix.");
225 this->getStateActionRewardVector());
229 this->optionalTransitionRewardMatrix = std::nullopt;
234 "The reduction to state rewards is only possible if the size of the action reward vector equals the number of states.");
241 this->optionalStateRewardVector = std::move(this->optionalStateActionRewardVector);
243 this->optionalStateRewardVector.value(), *weights, this->optionalStateRewardVector.value());
249 this->optionalStateRewardVector = std::move(this->optionalStateActionRewardVector);
252 this->optionalStateActionRewardVector = std::nullopt;
256template<
typename ValueType>
257template<
typename MatrixValueType>
261 : std::vector<ValueType>(transitionMatrix.
getRowCount()));
271template<
typename ValueType>
272template<
typename MatrixValueType>
274 std::vector<MatrixValueType>
const& weights)
const {
275 std::vector<ValueType> result;
280 [](MatrixValueType
const& weight,
ValueType const& rewardElement,
ValueType const& resultElement) {
281 return weight * (resultElement + rewardElement);
284 result = std::vector<ValueType>(transitionMatrix.
getRowCount());
288 [](MatrixValueType
const& weight,
ValueType const& rewardElement) {
return weight * rewardElement; });
297template<
typename ValueType>
298template<
typename MatrixValueType>
318template<
typename ValueType>
319template<
typename MatrixValueType>
321 std::vector<MatrixValueType>
const& stateRewardWeights)
const {
322 std::vector<ValueType> result;
326 result = std::vector<ValueType>(transitionMatrix.
getRowCount());
332 std::vector<ValueType> scaledStateRewardVector(transitionMatrix.
getRowGroupCount());
339template<
typename ValueType>
340template<
typename MatrixValueType>
345template<
typename ValueType>
346template<
typename MatrixValueType>
348 std::function<
bool(
ValueType const&)>
const& filter)
const {
352 for (uint_fast64_t state = 0; state < transitionMatrix.
getRowGroupCount(); ++state) {
355 result.
set(state,
false);
362 for (uint_fast64_t state = 0; state < transitionMatrix.
getRowGroupCount(); ++state) {
364 if (!filter(rewardMatrixEntry.getValue())) {
365 result.
set(state,
false);
374template<
typename ValueType>
375template<
typename MatrixValueType>
381template<
typename ValueType>
382template<
typename MatrixValueType>
384 std::function<
bool(
ValueType const&)>
const& filter)
const {
399 for (uint_fast64_t row = 0; row < transitionMatrix.
getRowCount(); ++row) {
401 if (!filter(rewardMatrixEntry.getValue())) {
402 result.
set(row,
false);
411template<
typename ValueType>
412template<
typename MatrixValueType>
429template<
typename ValueType>
431 return !(
static_cast<bool>(this->optionalStateRewardVector) ||
static_cast<bool>(this->optionalStateActionRewardVector) ||
432 static_cast<bool>(this->optionalTransitionRewardMatrix));
438template<
typename ValueType>
449 if (predicate(entry.getValue())) {
457template<
typename ValueType>
460 return !hasNonZeroReward;
463template<
typename ValueType>
465 if constexpr (std::is_same_v<ValueType, storm::RationalFunction>) {
467 STORM_LOG_THROW(
false, storm::exceptions::InvalidOperationException,
"Checking Rational functions for negativity is not possible.");
470 return anyOfRewardValues(*
this, [](
auto&& value) {
return value < storm::utility::zero<ValueType>(); });
474template<
typename ValueType>
476 if constexpr (std::is_same_v<ValueType, storm::RationalFunction>) {
478 STORM_LOG_THROW(
false, storm::exceptions::InvalidOperationException,
"Checking Rational functions for negativity is not possible.");
485template<
typename ValueType>
488 if (optionalStateRewardVector.value().size() != nrStates)
492 if (optionalStateActionRewardVector.value().size() != nrChoices)
498template<
typename ValueType>
502 boost::hash_combine(seed, boost::hash_range(optionalStateRewardVector->begin(), optionalStateRewardVector->end()));
505 boost::hash_combine(seed, boost::hash_range(optionalStateActionRewardVector->begin(), optionalStateActionRewardVector->end()));
508 boost::hash_combine(seed, optionalTransitionRewardMatrix->hash());
513template<
typename ValueType>
515 out << std::boolalpha <<
"reward model [state reward: " << rewardModel.
hasStateRewards()
522 std::set<storm::RationalFunctionVariable> vars;
528 vars.insert(tmp.begin(), tmp.end());
532 vars.insert(tmp.begin(), tmp.end());
543 std::vector<double>
const& weights)
const;
545 std::vector<double>
const& stateRewardWeights)
const;
548 std::function<
bool(
double const&)>
const& filter)
const;
551 std::function<
bool(
double const&)>
const& filter)
const;
556 double const& stateRewardWeight,
double const& actionRewardWeight)
const;
559 std::vector<double>
const* weights);
562template class StandardRewardModel<double>;
585 storm::RationalNumber
const& stateRewardWeight, storm::RationalNumber
const& actionRewardWeight)
const;
587 bool reduceToStateRewards,
588 std::vector<storm::RationalNumber>
const* weights);
621 std::vector<storm::RationalFunction>
const* weights);
633 std::vector<double>
const& weights)
const;
658 bool reduceToStateRewards, std::vector<double>
const* weights);
660 bool reduceToStateRewards, std::vector<storm::Interval>
const* weights);
702 std::vector<storm::RationalInterval>
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.
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.
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...
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...
index_type getRowGroupCount() const
Returns the number of row groups in the matrix.
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...
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)
template std::ostream & operator<<< storm::RationalInterval >(std::ostream &out, StandardRewardModel< storm::RationalInterval > const &rewardModel)
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)
std::ostream & operator<<(std::ostream &out, ChoiceLabeling const &labeling)
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 applyPointwiseTernary(std::vector< InValueType1 > const &firstOperand, std::vector< InValueType2 > const &secondOperand, std::vector< OutValueType > &target, Operation f=Operation())
Applies the given operation pointwise on the two given vectors and writes the result to the third vec...
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...
void applyPointwise(std::vector< InValueType1 > const &firstOperand, std::vector< InValueType2 > const &secondOperand, std::vector< OutValueType > &target, Operation f=Operation())
Applies the given operation pointwise on the two given vectors and writes the result to the third vec...
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)
carl::Interval< storm::RationalNumber > RationalInterval
carl::Interval< double > Interval
Interval type.
carl::RationalFunction< Polynomial, true > RationalFunction