From 57dffbcd4fb69578c2cc43c587babb945715c4e3 Mon Sep 17 00:00:00 2001 From: sjunges Date: Thu, 1 Oct 2015 10:08:15 +0200 Subject: [PATCH] further operators on models and reward models Former-commit-id: 0146278302b27079218d8df40a08b9051c7737d8 --- src/models/sparse/Model.cpp | 19 +++++++++++++++++++ src/models/sparse/Model.h | 15 ++++++++++++++- src/models/sparse/StandardRewardModel.cpp | 17 ++++++++++++++++- src/models/sparse/StandardRewardModel.h | 15 ++++++++++++++- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/models/sparse/Model.cpp b/src/models/sparse/Model.cpp index 10e4ecbd8..791062b54 100644 --- a/src/models/sparse/Model.cpp +++ b/src/models/sparse/Model.cpp @@ -111,6 +111,25 @@ namespace storm { } return it->second; } + + template + void Model::addRewardModel(std::string const& rewardModelName, RewardModelType const& newRewardModel) { + if(this->hasRewardModel(rewardModelName)) { + STORM_LOG_THROW(!(this->hasRewardModel(rewardModelName)), storm::exceptions::IllegalArgumentException, "A reward model with the given name '" << rewardModelName << "' already exists."); + } + assert(newRewardModel.isCompatible(this->getNumberOfStates(), this->getTransitionMatrix().getRowCount())); + this->rewardModels.emplace(rewardModelName, newRewardModel); + } + + template + bool Model::removeRewardModel(std::string const& rewardModelName) { + auto it = this->rewardModels.find(rewardModelName); + bool res = (it != this->rewardModels.end()); + if(res) { + this->rewardModels.erase(it->first); + } + return res; + } template bool Model::hasUniqueRewardModel() const { diff --git a/src/models/sparse/Model.h b/src/models/sparse/Model.h index 79f2ef9e4..ce5a85b89 100644 --- a/src/models/sparse/Model.h +++ b/src/models/sparse/Model.h @@ -182,7 +182,20 @@ namespace storm { * @return The number of reward models associated with this model. */ uint_fast64_t getNumberOfRewardModels() const; - + + /*! + * Adds a reward model to the model. Notice that this operation is only valid if the reward model matches the number of + * states and/or choices of the model. + * Moreover, it is required that no reward model with the same name exists in the model. + */ + void addRewardModel(std::string const& rewardModelName, RewardModelType const& rewModel); + + /*! + * Removes the reward model with the given name from the model. + * @return true, iff such a reward model existed + */ + bool removeRewardModel(std::string const& rewardModelName); + /*! * Retrieves the labels for the choices of the model. Note that calling this method is only valid if the * model has a choice labeling. diff --git a/src/models/sparse/StandardRewardModel.cpp b/src/models/sparse/StandardRewardModel.cpp index a3c9eb491..738dd65dd 100644 --- a/src/models/sparse/StandardRewardModel.cpp +++ b/src/models/sparse/StandardRewardModel.cpp @@ -126,6 +126,7 @@ namespace storm { // return max; // } + template boost::optional> const& StandardRewardModel::getOptionalStateActionRewardVector() const { return this->optionalStateActionRewardVector; @@ -267,7 +268,21 @@ namespace storm { bool StandardRewardModel::empty() const { return !(static_cast(this->optionalStateRewardVector) || static_cast(this->optionalStateActionRewardVector) || static_cast(this->optionalTransitionRewardMatrix)); } - + + + + template + bool StandardRewardModel::isCompatible(uint_fast64_t nrStates, uint_fast64_t nrChoices) const { + if(hasStateRewards()) { + if(optionalStateRewardVector.get().size() != nrStates) return false; + } + if(hasStateActionRewards()) { + if(optionalStateActionRewardVector.get().size() != nrChoices) return false; + } + return true; + } + + template std::size_t StandardRewardModel::getSizeInBytes() const { std::size_t result = 0; diff --git a/src/models/sparse/StandardRewardModel.h b/src/models/sparse/StandardRewardModel.h index fb8ba4658..6e153fa07 100644 --- a/src/models/sparse/StandardRewardModel.h +++ b/src/models/sparse/StandardRewardModel.h @@ -59,6 +59,7 @@ namespace storm { */ 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. @@ -252,7 +253,19 @@ namespace storm { * @return True iff the reward model is empty. */ bool empty() 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. *