Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
RewardModel.cpp
Go to the documentation of this file.
2
4
5namespace storm {
6namespace prism {
7RewardModel::RewardModel(std::string const& rewardModelName, std::vector<storm::prism::StateReward> const& stateRewards,
8 std::vector<storm::prism::StateActionReward> const& stateActionRewards,
9 std::vector<storm::prism::TransitionReward> const& transitionRewards, std::string const& filename, uint_fast64_t lineNumber)
10 : LocatedInformation(filename, lineNumber),
11 rewardModelName(rewardModelName),
12 stateRewards(stateRewards),
13 stateActionRewards(stateActionRewards),
14 transitionRewards(transitionRewards) {
15 // Nothing to do here.
16}
17
18std::string const& RewardModel::getName() const {
19 return this->rewardModelName;
20}
21
22bool RewardModel::empty() const {
23 return !this->hasStateRewards() && !this->hasTransitionRewards();
24}
25
27 return !this->stateRewards.empty();
28}
29
30std::vector<storm::prism::StateReward> const& RewardModel::getStateRewards() const {
31 return this->stateRewards;
32}
33
35 return !this->stateActionRewards.empty();
36}
37
38std::vector<storm::prism::StateActionReward> const& RewardModel::getStateActionRewards() const {
39 return this->stateActionRewards;
40}
41
43 return !this->transitionRewards.empty();
44}
45
46std::vector<storm::prism::TransitionReward> const& RewardModel::getTransitionRewards() const {
47 return this->transitionRewards;
48}
49
50RewardModel RewardModel::substitute(std::map<storm::expressions::Variable, storm::expressions::Expression> const& substitution) const {
51 std::vector<StateReward> newStateRewards;
52 newStateRewards.reserve(this->getStateRewards().size());
53 for (auto const& stateReward : this->getStateRewards()) {
54 newStateRewards.emplace_back(stateReward.substitute(substitution));
55 }
56
57 std::vector<StateActionReward> newStateActionRewards;
58 newStateActionRewards.reserve(this->getStateRewards().size());
59 for (auto const& stateActionReward : this->getStateActionRewards()) {
60 newStateActionRewards.emplace_back(stateActionReward.substitute(substitution));
61 }
62
63 std::vector<TransitionReward> newTransitionRewards;
64 newTransitionRewards.reserve(this->getTransitionRewards().size());
65 for (auto const& transitionReward : this->getTransitionRewards()) {
66 newTransitionRewards.emplace_back(transitionReward.substitute(substitution));
67 }
68 return RewardModel(this->getName(), newStateRewards, newStateActionRewards, newTransitionRewards, this->getFilename(), this->getLineNumber());
69}
70
71bool RewardModel::containsVariablesOnlyInRewardValueExpressions(std::set<storm::expressions::Variable> const& undefinedConstantVariables) const {
72 for (auto const& stateReward : this->getStateRewards()) {
73 if (stateReward.getStatePredicateExpression().containsVariable(undefinedConstantVariables)) {
74 return false;
75 }
76 }
77 for (auto const& stateActionReward : this->getStateActionRewards()) {
78 if (stateActionReward.getStatePredicateExpression().containsVariable(undefinedConstantVariables)) {
79 return false;
80 }
81 }
82 for (auto const& transitionReward : this->getTransitionRewards()) {
83 if (transitionReward.getSourceStatePredicateExpression().containsVariable(undefinedConstantVariables)) {
84 return false;
85 }
86 if (transitionReward.getTargetStatePredicateExpression().containsVariable(undefinedConstantVariables)) {
87 return false;
88 }
89 }
90 return true;
91}
92
94 std::vector<StateActionReward> newStateActionRewards;
95 for (auto const& stateActionReward : this->getStateActionRewards()) {
96 if (actionIndicesToKeep.find(stateActionReward.getActionIndex()) != actionIndicesToKeep.end()) {
97 newStateActionRewards.emplace_back(stateActionReward);
98 }
99 }
100
101 std::vector<TransitionReward> newTransitionRewards;
102 for (auto const& transitionReward : this->getTransitionRewards()) {
103 if (actionIndicesToKeep.find(transitionReward.getActionIndex()) != actionIndicesToKeep.end()) {
104 newTransitionRewards.emplace_back(transitionReward);
105 }
106 }
107
108 return RewardModel(this->getName(), this->getStateRewards(), newStateActionRewards, newTransitionRewards, this->getFilename(), this->getLineNumber());
109}
110
111RewardModel RewardModel::labelUnlabelledCommands(std::vector<std::pair<uint64_t, std::string>> const& newActions) const {
112 std::vector<StateActionReward> newStateActionRewards;
113 std::vector<TransitionReward> newTransitionRewards;
114
115 for (auto const& reward : getStateActionRewards()) {
116 if (reward.getActionIndex() == 0) {
117 for (auto const& newAction : newActions) {
118 newStateActionRewards.emplace_back(newAction.first, newAction.second, reward.getStatePredicateExpression(), reward.getRewardValueExpression(),
119 reward.getFilename(), reward.getLineNumber());
120 }
121 } else {
122 newStateActionRewards.push_back(reward);
123 }
124 }
125
126 STORM_LOG_ASSERT(transitionRewards.empty(), "Expected empty transition rewards."); // Not implemented.
127
128 return RewardModel(this->getName(), this->getStateRewards(), newStateActionRewards, newTransitionRewards, this->getFilename(), this->getLineNumber());
129}
130
131std::ostream& operator<<(std::ostream& stream, RewardModel const& rewardModel) {
132 stream << "rewards";
133 if (rewardModel.getName() != "") {
134 stream << " \"" << rewardModel.getName() << "\"";
135 }
136 stream << '\n';
137 for (auto const& reward : rewardModel.getStateRewards()) {
138 stream << reward << '\n';
139 }
140 for (auto const& reward : rewardModel.getStateActionRewards()) {
141 stream << reward << '\n';
142 }
143 for (auto const& reward : rewardModel.getTransitionRewards()) {
144 stream << reward << '\n';
145 }
146 stream << "endrewards\n";
147 return stream;
148}
149
150} // namespace prism
151} // namespace storm
uint_fast64_t getLineNumber() const
Retrieves the line number in which the information was found.
std::string const & getFilename() const
Retrieves the name of the file in which the information was found.
LocatedInformation(std::string const &filename, uint_fast64_t lineNumber)
Constructs a located information with the given filename and line number.
std::vector< storm::prism::StateReward > const & getStateRewards() const
Retrieves all state rewards associated with this reward model.
bool hasStateRewards() const
Retrieves whether there are any state rewards.
RewardModel labelUnlabelledCommands(std::vector< std::pair< uint64_t, std::string > > const &newActionNames) const
RewardModel substitute(std::map< storm::expressions::Variable, storm::expressions::Expression > const &substitution) const
Substitutes all variables in the reward model according to the given map.
bool containsVariablesOnlyInRewardValueExpressions(std::set< storm::expressions::Variable > const &undefinedConstantVariables) const
Checks whether any of the given variables only appear in the expressions defining the reward value.
bool hasTransitionRewards() const
Retrieves whether there are any transition rewards.
RewardModel restrictActionRelatedRewards(storm::storage::FlatSet< uint_fast64_t > const &actionIndicesToKeep) const
Restricts all action-related rewards of the reward model to the ones with an action index in the prov...
RewardModel(std::string const &rewardModelName, std::vector< storm::prism::StateReward > const &stateRewards, std::vector< storm::prism::StateActionReward > const &stateActionRewards, std::vector< storm::prism::TransitionReward > const &transitionRewards, std::string const &filename="", uint_fast64_t lineNumber=0)
Creates a reward model with the given name, state and transition rewards.
std::vector< storm::prism::TransitionReward > const & getTransitionRewards() const
Retrieves all transition rewards associated with this reward model.
std::string const & getName() const
Retrieves the name of the reward model.
bool empty() const
Checks whether the reward model is empty, i.e.
bool hasStateActionRewards() const
Retrieves whether there are any state-action rewards.
std::vector< storm::prism::StateActionReward > const & getStateActionRewards() const
Retrieves all state-action rewards associated with this reward model.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
std::ostream & operator<<(std::ostream &stream, Assignment const &assignment)
boost::container::flat_set< Key, std::less< Key >, boost::container::new_allocator< Key > > FlatSet
Redefinition of flat_set was needed, because from Boost 1.70 on the default allocator is set to void.
Definition BoostTypes.h:13