diff --git a/src/storm/models/sparse/StandardRewardModel.cpp b/src/storm/models/sparse/StandardRewardModel.cpp index f73704b89..052a95af0 100644 --- a/src/storm/models/sparse/StandardRewardModel.cpp +++ b/src/storm/models/sparse/StandardRewardModel.cpp @@ -155,24 +155,20 @@ namespace storm { template template - ValueType StandardRewardModel::getTotalStateActionReward(uint_fast64_t stateIndex, uint_fast64_t choiceIndex, storm::storage::SparseMatrix const& transitionMatrix, MatrixValueType const& stateRewardWeight, MatrixValueType const& actionRewardWeight) const { - ValueType result = this->hasStateRewards() ? (this->hasStateActionRewards() ? (ValueType) (this->getStateReward(stateIndex) * stateRewardWeight + this->getStateActionReward(choiceIndex) * actionRewardWeight) - : (ValueType) (this->getStateReward(stateIndex) * stateRewardWeight)) - : (this->hasStateActionRewards() ? (ValueType) (this->getStateActionReward(choiceIndex) * actionRewardWeight) - : storm::utility::zero()); + ValueType StandardRewardModel::getStateActionAndTransitionReward(uint_fast64_t choiceIndex, storm::storage::SparseMatrix const& transitionMatrix) const { + ValueType result = this->hasStateActionRewards() ? this->getStateActionReward(choiceIndex) : storm::utility::zero(); if (this->hasTransitionRewards()) { - auto rewMatrixEntryIt = this->getTransitionRewardMatrix().begin(choiceIndex); - for (auto const& transitionEntry : transitionMatrix.getRow(choiceIndex)) { - assert(rewMatrixEntryIt != this->getTransitionRewardMatrix().end(choiceIndex)); - if (transitionEntry.getColumn() < rewMatrixEntryIt->getColumn()) { - continue; - } else { - // We assume that the transition reward matrix is a submatrix of the given transition matrix. Hence, the following must hold - assert(transitionEntry.getColumn() == rewMatrixEntryIt->getColumn()); - result += actionRewardWeight * rewMatrixEntryIt->getValue() * storm::utility::convertNumber(transitionEntry.getValue()); - ++rewMatrixEntryIt; - } - } + result += transitionMatrix.getPointwiseProductRowSum(getTransitionRewardMatrix(), choiceIndex); + } + return result; + } + + template + template + ValueType StandardRewardModel::getTotalStateActionReward(uint_fast64_t stateIndex, uint_fast64_t choiceIndex, storm::storage::SparseMatrix const& transitionMatrix, MatrixValueType const& stateRewardWeight, MatrixValueType const& actionRewardWeight) const { + ValueType result = actionRewardWeight * getStateActionAndTransitionReward(choiceIndex, transitionMatrix); + if (this->hasStateRewards()) { + result += stateRewardWeight * this->getStateReward(stateIndex); } return result; } diff --git a/src/storm/models/sparse/StandardRewardModel.h b/src/storm/models/sparse/StandardRewardModel.h index e4b0c6636..e84faee19 100644 --- a/src/storm/models/sparse/StandardRewardModel.h +++ b/src/storm/models/sparse/StandardRewardModel.h @@ -167,6 +167,15 @@ namespace storm { */ boost::optional> const& getOptionalTransitionRewardMatrix() const; + /*! + * @param choiceIndex The index of the considered choice + * @param transitionMatrix The matrix that is used to weight the values of the transition reward matrix. + * @return the sum of the action reward and the weighted transition rewards for the given choice, excluding potential state rewards + * @note returns zero if there is neither action nor transition reward. + */ + template + ValueType getStateActionAndTransitionReward(uint_fast64_t choiceIndex, storm::storage::SparseMatrix const& transitionMatrix) const; + /*! * Retrieves the total reward for the given state action pair (including (scaled) state rewards, action rewards and transition rewards *