Browse Source

Used the new reward information traverser wherever one needs to find out the reward kinds of a given rewardmodel

tempestpy_adaptions
TimQu 6 years ago
parent
commit
0a6122258c
  1. 42
      src/storm/generator/JaniNextStateGenerator.cpp
  2. 28
      src/storm/logic/RewardAccumulationEliminationVisitor.cpp
  3. 1
      src/storm/logic/RewardModelNameSubstitutionVisitor.cpp
  4. 37
      src/storm/storage/jani/JSONExporter.cpp

42
src/storm/generator/JaniNextStateGenerator.cpp

@ -15,7 +15,7 @@
#include "storm/storage/jani/CompositionInformationVisitor.h"
#include "storm/storage/jani/traverser/AssignmentLevelFinder.h"
#include "storm/storage/jani/traverser/ArrayExpressionFinder.h"
#include "storm/storage/jani/traverser/AssignmentsFinder.h"
#include "storm/storage/jani/traverser/RewardModelInformation.h"
#include "storm/storage/sparse/JaniChoiceOrigins.h"
@ -949,41 +949,13 @@ namespace storm {
template<typename ValueType, typename StateType>
void JaniNextStateGenerator<ValueType, StateType>::buildRewardModelInformation() {
// Prepare all reward model information structs and get a mapping from variables to rewardModels that use this variable
std::map<storm::expressions::Variable, std::vector<uint64_t>> variableToRewardIndices;
for (uint64_t i = 0; i < rewardExpressions.size(); ++i) {
rewardModelInformation.emplace_back(rewardExpressions[i].first, false, false, false);
auto varsInExpression = rewardExpressions[i].second.getVariables();
for (auto const& v : varsInExpression) {
auto mapIt = variableToRewardIndices.emplace(v, std::vector<uint64_t>()).first;
mapIt->second.push_back(i);
}
}
storm::jani::AssignmentsFinder finder;
for (auto const& varRewIndices : variableToRewardIndices) {
auto assignmentsFinderResult = finder.find(this->model, varRewIndices.first);
if (assignmentsFinderResult.hasLocationAssignment) {
for (auto const& rewModelIndex : varRewIndices.second) {
rewardModelInformation[rewModelIndex].setHasStateRewards();
}
}
if (assignmentsFinderResult.hasEdgeAssignment || (this->options.isScaleAndLiftTransitionRewardsSet() && assignmentsFinderResult.hasEdgeDestinationAssignment)) {
for (auto const& rewModelIndex : varRewIndices.second) {
rewardModelInformation[rewModelIndex].setHasStateActionRewards();
hasStateActionRewards = true;
}
}
STORM_LOG_THROW(this->options.isScaleAndLiftTransitionRewardsSet() || !assignmentsFinderResult.hasEdgeDestinationAssignment, storm::exceptions::NotSupportedException, "Transition rewards are not supported and a reduction to action-based rewards was not possible.");
}
// If the reward expression does not evaluate to zero, we set all reward types to true
for (uint64_t i = 0; i < rewardExpressions.size(); ++i) {
auto const& rewExpr = rewardExpressions[i].second;
if (rewExpr.containsVariables() || !storm::utility::isZero(this->evaluator->asRational(rewExpr))) {
rewardModelInformation[i].setHasStateRewards();
rewardModelInformation[i].setHasStateActionRewards();
for (auto const& rewardModel : rewardExpressions) {
storm::jani::RewardModelInformation info(this->model, rewardModel.second);
rewardModelInformation.emplace_back(rewardModel.first, info.hasStateRewards(), false, false);
STORM_LOG_THROW(this->options.isScaleAndLiftTransitionRewardsSet() || !info.hasTransitionRewards(), storm::exceptions::NotSupportedException, "Transition rewards are not supported and a reduction to action-based rewards was not possible.");
if (info.hasActionRewards() || (this->options.isScaleAndLiftTransitionRewardsSet() && info.hasTransitionRewards())) {
hasStateActionRewards = true;
rewardModelInformation.back().setHasStateActionRewards();
}
}
}

28
src/storm/logic/RewardAccumulationEliminationVisitor.cpp

@ -2,7 +2,7 @@
#include "storm/logic/Formulas.h"
#include "storm/storage/jani/Model.h"
#include "storm/storage/jani/traverser/AssignmentsFinder.h"
#include "storm/storage/jani/traverser/RewardModelInformation.h"
#include "storm/storage/jani/expressions/JaniExpressionSubstitutionVisitor.h"
#include "storm/utility/macros.h"
@ -128,32 +128,12 @@ namespace storm {
bool RewardAccumulationEliminationVisitor::canEliminate(storm::logic::RewardAccumulation const& accumulation, boost::optional<std::string> rewardModelName) const {
STORM_LOG_THROW(rewardModelName.is_initialized(), storm::exceptions::InvalidPropertyException, "Unable to find transient variable for unique reward model.");
storm::expressions::Expression rewardExpression = model.getRewardModelExpression(rewardModelName.get());
bool hasStateRewards = false;
bool hasActionOrTransitionRewards = false;
auto variablesInRewardExpression = rewardExpression.getVariables();
std::map<storm::expressions::Variable, storm::expressions::Expression> initialSubstitution;
for (auto const& v : variablesInRewardExpression) {
STORM_LOG_ASSERT(model.hasGlobalVariable(v.getName()), "Unable to find global variable " << v.getName() << " occurring in a reward expression.");
auto const& janiVar = model.getGlobalVariable(v.getName());
if (janiVar.hasInitExpression()) {
initialSubstitution.emplace(v, janiVar.getInitExpression());
}
auto assignmentKinds = storm::jani::AssignmentsFinder().find(model, v);
hasActionOrTransitionRewards = hasActionOrTransitionRewards || assignmentKinds.hasEdgeAssignment || assignmentKinds.hasEdgeDestinationAssignment;
hasStateRewards = hasStateRewards || assignmentKinds.hasLocationAssignment;
}
rewardExpression = storm::jani::substituteJaniExpression(rewardExpression, initialSubstitution);
if (rewardExpression.containsVariables() || !storm::utility::isZero(rewardExpression.evaluateAsRational())) {
hasStateRewards = true;
hasActionOrTransitionRewards = true;
}
storm::jani::RewardModelInformation info(model, rewardModelName.get());
if (hasActionOrTransitionRewards && !accumulation.isStepsSet()) {
if ((info.hasActionRewards() || info.hasTransitionRewards()) && !accumulation.isStepsSet()) {
return false;
}
if (hasStateRewards) {
if (info.hasStateRewards()) {
if (model.isDiscreteTimeModel()) {
if (!accumulation.isExitSet()) {
return false;

1
src/storm/logic/RewardModelNameSubstitutionVisitor.cpp

@ -2,7 +2,6 @@
#include "storm/logic/Formulas.h"
#include "storm/storage/jani/Model.h"
#include "storm/storage/jani/traverser/AssignmentsFinder.h"
#include "storm/utility/macros.h"
#include "storm/exceptions/UnexpectedException.h"

37
src/storm/storage/jani/JSONExporter.cpp

@ -30,7 +30,7 @@
#include "storm/storage/jani/AutomatonComposition.h"
#include "storm/storage/jani/ParallelComposition.h"
#include "storm/storage/jani/Property.h"
#include "storm/storage/jani/traverser/AssignmentsFinder.h"
#include "storm/storage/jani/traverser/RewardModelInformation.h"
#include "storm/storage/jani/expressions/JaniReduceNestingExpressionVisitor.h"
#include "storm/storage/jani/FunctionEliminator.h"
#include "storm/storage/jani/expressions/JaniExpressionSubstitutionVisitor.h"
@ -164,35 +164,12 @@ namespace storm {
}
modernjson::json FormulaToJaniJson::constructRewardAccumulation(storm::logic::RewardAccumulation const& rewardAccumulation, std::string const& rewardModelName) const {
bool steps = false;
bool time = false;
bool exit = false;
auto rewardExpression = storm::jani::eliminateFunctionCallsInExpression(model.getRewardModelExpression(rewardModelName), model);
auto variablesInRewardExpression = rewardExpression.getVariables();
std::map<storm::expressions::Variable, storm::expressions::Expression> initialSubstitution;
for (auto const& v : variablesInRewardExpression) {
STORM_LOG_ASSERT(model.hasGlobalVariable(v.getName()), "Unable to find global variable " << v.getName() << " occurring in a reward expression.");
auto const& janiVar = model.getGlobalVariable(v.getName());
if (janiVar.hasInitExpression()) {
initialSubstitution.emplace(v, janiVar.getInitExpression());
}
auto assignmentKinds = storm::jani::AssignmentsFinder().find(model, v);
steps = steps || assignmentKinds.hasEdgeAssignment || assignmentKinds.hasEdgeDestinationAssignment;
time = time || (!model.isDeterministicModel() && assignmentKinds.hasLocationAssignment);
exit = exit || assignmentKinds.hasLocationAssignment;
}
rewardExpression = storm::jani::substituteJaniExpression(rewardExpression, initialSubstitution);
if (rewardExpression.containsVariables() || !storm::utility::isZero(rewardExpression.evaluateAsRational())) {
steps = true;
time = true;
exit = true;
}
steps = steps && rewardAccumulation.isStepsSet();
time = time && rewardAccumulation.isTimeSet();
exit = exit && rewardAccumulation.isExitSet();
storm::jani::RewardModelInformation info(model, rewardModelName);
bool steps = rewardAccumulation.isStepsSet() && (info.hasActionRewards() || info.hasTransitionRewards());
bool time = rewardAccumulation.isTimeSet() && !model.isDeterministicModel() && info.hasStateRewards();
bool exit = rewardAccumulation.isExitSet() && info.hasStateRewards();
return constructRewardAccumulation(storm::logic::RewardAccumulation(steps, time, exit));
}

Loading…
Cancel
Save