From 7946483f096206f85bf648e10443b77bc7999b43 Mon Sep 17 00:00:00 2001 From: dehnert Date: Tue, 1 Sep 2015 21:53:27 +0200 Subject: [PATCH] added function to modify state-action rewards in nondeterministic model Former-commit-id: d3a4c3870722278f84160137976f3459381078af --- src/models/sparse/NondeterministicModel.cpp | 18 ++++++++++++++++++ src/models/sparse/NondeterministicModel.h | 9 +++++++++ src/models/sparse/StandardRewardModel.cpp | 5 +++++ src/models/sparse/StandardRewardModel.h | 13 ++++++++++++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/models/sparse/NondeterministicModel.cpp b/src/models/sparse/NondeterministicModel.cpp index 340400dde..c5e3e9ecf 100644 --- a/src/models/sparse/NondeterministicModel.cpp +++ b/src/models/sparse/NondeterministicModel.cpp @@ -4,6 +4,8 @@ #include "src/adapters/CarlAdapter.h" +#include "src/exceptions/InvalidOperationException.h" + namespace storm { namespace models { namespace sparse { @@ -45,6 +47,22 @@ namespace storm { return indices[state+1] - indices[state]; } + template + void NondeterministicModel::modifyStateActionRewards(RewardModelType& rewardModel, std::map, typename RewardModelType::ValueType> const& modifications) const { + STORM_LOG_THROW(rewardModel.hasStateActionRewards(), storm::exceptions::InvalidOperationException, "Cannot modify state-action rewards, because the reward model does not have state-action rewards."); + STORM_LOG_THROW(this->hasChoiceLabeling(), storm::exceptions::InvalidOperationException, "Cannot modify state-action rewards, because the model does not have an action labeling."); + std::vector const& choiceLabels = this->getChoiceLabeling(); + for (auto const& modification : modifications) { + uint_fast64_t stateIndex = modification.first.first; + for (uint_fast64_t row = this->getNondeterministicChoiceIndices()[stateIndex]; row < this->getNondeterministicChoiceIndices()[stateIndex + 1]; ++row) { + // If the action label of the row matches the requested one, we set the reward value accordingly. + if (choiceLabels[row] == modification.first.second) { + rewardModel.setStateActionRewardValue(row, modification.second); + } + } + } + } + template void NondeterministicModel::reduceToStateBasedRewards() { for (auto& rewardModel : this->getRewardModels()) { diff --git a/src/models/sparse/NondeterministicModel.h b/src/models/sparse/NondeterministicModel.h index 9c5a01b05..5bacf6dd7 100644 --- a/src/models/sparse/NondeterministicModel.h +++ b/src/models/sparse/NondeterministicModel.h @@ -73,6 +73,15 @@ namespace storm { */ uint_fast64_t getNumberOfChoices(uint_fast64_t state) const; + /*! + * Modifies the state-action reward vector of the given reward model by setting the value specified in + * the map for the corresponding state-action pairs. + * + * @param rewardModel The reward model whose state-action rewards to modify. + * @param modifications A mapping from state-action pairs to the their new reward values. + */ + void modifyStateActionRewards(RewardModelType& rewardModel, std::map, typename RewardModelType::ValueType> const& modifications) const; + virtual void reduceToStateBasedRewards() override; virtual void printModelInformationToStream(std::ostream& out) const override; diff --git a/src/models/sparse/StandardRewardModel.cpp b/src/models/sparse/StandardRewardModel.cpp index b8fa2e3be..5788dae10 100644 --- a/src/models/sparse/StandardRewardModel.cpp +++ b/src/models/sparse/StandardRewardModel.cpp @@ -197,6 +197,11 @@ namespace storm { return result; } + template + void StandardRewardModel::setStateActionRewardValue(uint_fast64_t row, ValueType const& value) { + this->optionalStateActionRewardVector.get()[row] = value; + } + template bool StandardRewardModel::empty() const { return !(static_cast(this->optionalStateRewardVector) || static_cast(this->optionalStateActionRewardVector) || static_cast(this->optionalTransitionRewardMatrix)); diff --git a/src/models/sparse/StandardRewardModel.h b/src/models/sparse/StandardRewardModel.h index 457d4df5b..eb396d90a 100644 --- a/src/models/sparse/StandardRewardModel.h +++ b/src/models/sparse/StandardRewardModel.h @@ -10,9 +10,11 @@ namespace storm { namespace models { namespace sparse { - template + template class StandardRewardModel { public: + typedef CValueType ValueType; + /*! * Constructs a reward model by copying the given data. * @@ -213,6 +215,15 @@ namespace storm { */ 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.