#ifndef STORM_MODELS_SPARSE_STANDARDREWARDMODEL_H_ #define STORM_MODELS_SPARSE_STANDARDREWARDMODEL_H_ #include #include #include "src/storage/SparseMatrix.h" #include "src/utility/OsDetection.h" namespace storm { namespace models { namespace sparse { template class StandardRewardModel { public: typedef CValueType ValueType; /*! * Constructs a reward model by copying the given data. * * @param optionalStateRewardVector The reward values associated with the states. * @param optionalStateActionRewardVector The reward values associated with state-action pairs. * @param optionalTransitionRewardMatrix The reward values associated with the transitions of the model. */ StandardRewardModel(boost::optional> const& optionalStateRewardVector = boost::optional>(), boost::optional> const& optionalStateActionRewardVector = boost::optional>(), boost::optional> const& optionalTransitionRewardMatrix = boost::optional>()); /*! * Constructs a reward model by moving the given data. * * @param optionalStateRewardVector The reward values associated with the states. * @param optionalStateActionRewardVector The reward values associated with state-action pairs. * @param optionalTransitionRewardMatrix The reward values associated with the transitions of the model. */ StandardRewardModel(boost::optional>&& optionalStateRewardVector = boost::optional>(), boost::optional>&& optionalStateActionRewardVector = boost::optional>(), boost::optional>&& optionalTransitionRewardMatrix = boost::optional>()); StandardRewardModel(StandardRewardModel const& dtmc) = default; StandardRewardModel& operator=(StandardRewardModel const& dtmc) = default; #ifndef WINDOWS StandardRewardModel(StandardRewardModel&& dtmc) = default; StandardRewardModel& operator=(StandardRewardModel&& dtmc) = default; #endif /*! * Retrieves whether the reward model has state rewards. * * @return True iff the reward model has state rewards. */ bool hasStateRewards() const; /*! * Retrieves whether the reward model only has state rewards (and hence no other rewards). * * @return True iff the reward model only has state rewards. */ bool hasOnlyStateRewards() const; /*! * Retrieves the state rewards of the reward model. Note that it is illegal to call this function if the * reward model does not have state rewards. * * @return The state reward vector. */ std::vector const& getStateRewardVector() const; /*! * Retrieves the state rewards of the reward model. Note that it is illegal to call this function if the * reward model does not have state rewards. * * @return The state reward vector. */ std::vector& getStateRewardVector(); ValueType const& getStateReward(uint_fast64_t state) const; template void setStateReward(uint_fast64_t state, T const& newReward); //template>> //ValueType maximalStateReward(uint_fast64_t state) const; /*! * Retrieves an optional value that contains the state reward vector if there is one. * * @return The state reward vector if there is one. */ boost::optional> const& getOptionalStateRewardVector() const; /*! * Retrieves whether the reward model has state-action rewards. * * @return True iff the reward model has state-action rewards. */ bool hasStateActionRewards() const; /*! * Retrieves the state-action rewards of the reward model. Note that it is illegal to call this function * if the reward model does not have state-action rewards. * * @return The state-action reward vector. */ std::vector const& getStateActionRewardVector() const; /*! * Retrieves the state-action rewards of the reward model. Note that it is illegal to call this function * if the reward model does not have state-action rewards. * * @return The state-action reward vector. */ std::vector& getStateActionRewardVector(); /*! * Retrieves the state-action reward for the given choice. */ ValueType const& getStateActionReward(uint_fast64_t choiceIndex) const; /*! * Sets the state-action reward for the given choice */ template void setStateActionReward(uint_fast64_t choiceIndex, T const& newValue); //ValueType maximalStateActionReward() const; /*! * Retrieves an optional value that contains the state-action reward vector if there is one. * * @return The state-action reward vector if there is one. */ boost::optional> const& getOptionalStateActionRewardVector() const; /*! * Retrieves whether the reward model has transition rewards. * * @return True iff the reward model has transition rewards. */ bool hasTransitionRewards() const; /*! * Retrieves the transition rewards of the reward model. Note that it is illegal to call this function * if the reward model does not have transition rewards. * * @return The transition reward matrix. */ storm::storage::SparseMatrix const& getTransitionRewardMatrix() const; /*! * Retrieves the transition rewards of the reward model. Note that it is illegal to call this function * if the reward model does not have transition rewards. * * @return The transition reward matrix. */ storm::storage::SparseMatrix& getTransitionRewardMatrix(); /*! * Retrieves an optional value that contains the transition reward matrix if there is one. * * @return The transition reward matrix if there is one. */ boost::optional> const& getOptionalTransitionRewardMatrix() const; /*! * Creates a new reward model by restricting the actions of the action-based rewards. * * @param enabledActions A bit vector representing the enabled actions. * @return The restricted reward model. */ StandardRewardModel restrictActions(storm::storage::BitVector const& enabledActions) const; /*! * Reduces the transition-based rewards to state-action rewards by taking the average of each row. If * the corresponding flag is set, the state-action rewards and the state rewards are summed so the model * only has a state reward vector left. Note that this transformation only preserves expected rewards, * but not all reward-based properties. * * @param transitionMatrix The transition matrix that is used to weight the rewards in the reward matrix. */ template void reduceToStateBasedRewards(storm::storage::SparseMatrix const& transitionMatrix, bool reduceToStateRewards = false); /*! * Creates a vector representing the complete reward vector based on the state-, state-action- and * transition-based rewards in the reward model. * * @param transitionMatrix The matrix that is used to weight the values of the transition reward matrix. * @return The full state-action reward vector. */ template std::vector getTotalRewardVector(storm::storage::SparseMatrix const& transitionMatrix) const; /*! * Creates a vector representing the complete reward vector based on the state-, state-action- and * transition-based rewards in the reward model. * * @param transitionMatrix The matrix that is used to weight the values of the transition reward matrix. * @param weights A vector used for scaling the entries of the state-action rewards (if present). * @return The full state-action reward vector. */ template std::vector getTotalRewardVector(storm::storage::SparseMatrix const& transitionMatrix, std::vector const& weights) const; /*! * Creates a vector representing the complete reward vector based on the state-, state-action- and * transition-based rewards in the reward model. * * @param transitionMatrix The matrix that is used to weight the values of the transition reward matrix. * @param numberOfRows The total number of rows of the resulting vector. * @param filter A bit vector indicating which row groups to select. * @return The full state-action reward vector. */ template std::vector getTotalRewardVector(uint_fast64_t numberOfRows, storm::storage::SparseMatrix const& transitionMatrix, storm::storage::BitVector const& filter) const; /*! * Creates a vector representing the complete state action reward vector based on the state-, state-action- * and transition-based rewards in the reward model. * * @param numberOfRows The total number of rows of the resulting vector. * @param rowGroupIndices The starting indices of the row groups. * @return The full state-action reward vector. */ std::vector getTotalStateActionRewardVector(uint_fast64_t numberOfRows, std::vector const& rowGroupIndices) const; /*! * Creates a vector representing the complete state action reward vector based on the state- and * state-action rewards in the reward model. * * @param numberOfRows The total number of rows of the resulting vector. * @param rowGroupIndices The starting indices of the row groups. * @param filter A bit vector indicating which row groups to select. * @return The full state-action reward vector. */ std::vector getTotalStateActionRewardVector(uint_fast64_t numberOfRows, std::vector const& rowGroupIndices, storm::storage::BitVector const& filter) const; /*! * Sets the given value in the state-action reward vector at the given row. This assumes that the reward * model has state-action rewards. * * @param row The row at which to set the given value. * @param value The value to set. */ void setStateActionRewardValue(uint_fast64_t row, ValueType const& value); /*! * Retrieves whether the reward model is empty, i.e. contains no state-, state-action- or transition-based * rewards. * * @return True iff the reward model is empty. */ bool empty() const; /*! * Retrieves whether every reward defined by this reward model is zero * * @return True iff every reward defined by this reward model is zero. */ bool isAllZero() const; /*! * Checks whether the reward model is compatible with key model characteristics. * * In detail, the method checks * - if the state-rewards exist, whether the given number of states corresponds to the size of the vector. * - if the state-action-rewards exist, whether the given number of choices corresponds to the size of the vector. * * @param nrStates The number of states in the model * @param nrChoices The number of choices in the model */ bool isCompatible(uint_fast64_t nrStates, uint_fast64_t nrChoices) const; /*! * Retrieves (an approximation of) the size of the model in bytes. * * @return The size of the internal representation of the model measured in bytes. */ std::size_t getSizeInBytes() const; template friend std::ostream& operator<<(std::ostream& out, StandardRewardModel const& rewardModel); private: // An (optional) vector representing the state rewards. boost::optional> optionalStateRewardVector; // An (optional) vector representing the state-action rewards. boost::optional> optionalStateActionRewardVector; // An (optional) matrix representing the transition rewards. boost::optional> optionalTransitionRewardMatrix; }; template std::ostream& operator<<(std::ostream& out, StandardRewardModel const& rewardModel); } } } #endif /* STORM_MODELS_SPARSE_STANDARDREWARDMODEL_H_ */