Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
Choice.cpp
Go to the documentation of this file.
2
4
6
11
12namespace storm {
13namespace generator {
14
15template<typename ValueType, typename StateType>
16Choice<ValueType, StateType>::Choice(uint_fast64_t actionIndex, bool markovian)
17 : markovian(markovian), actionIndex(actionIndex), distribution(), totalMass(storm::utility::zero<ValueType>()), rewards(), labels() {
18 // Intentionally left empty.
19}
20
21template<typename ValueType, typename StateType>
23 STORM_LOG_THROW(this->markovian == other.markovian, storm::exceptions::InvalidOperationException, "Type of choices do not match.");
24 STORM_LOG_THROW(this->actionIndex == other.actionIndex, storm::exceptions::InvalidOperationException, "Action index of choices do not match.");
25 STORM_LOG_THROW(this->rewards.size() == other.rewards.size(), storm::exceptions::InvalidOperationException, "Reward value sizes of choices do not match.");
26
27 // Add the elements to the distribution.
28 this->distribution.add(other.distribution);
29
30 // Update the total mass of the choice.
31 this->totalMass += other.totalMass;
32
33 // Add all reward values.
34 auto otherRewIt = other.rewards.begin();
35 for (auto& rewardValue : this->rewards) {
36 rewardValue += *otherRewIt;
37 }
38
39 // Join label sets and origin data if given.
40 if (other.labels) {
41 this->addLabels(other.labels.get());
42 }
43 if (other.originData) {
44 this->addOriginData(other.originData.get());
45 }
46}
47
48template<typename ValueType, typename StateType>
49StateType Choice<ValueType, StateType>::sampleFromDistribution(ValueType const& quantile) const {
50 return distribution.sampleFromDistribution(quantile);
51}
52
53template<typename ValueType, typename StateType>
57
58template<typename ValueType, typename StateType>
62
63template<typename ValueType, typename StateType>
67
68template<typename ValueType, typename StateType>
72
73template<typename ValueType, typename StateType>
74void Choice<ValueType, StateType>::addLabel(std::string const& newLabel) {
75 if (!labels) {
76 labels = std::set<std::string>();
77 }
78 labels->insert(newLabel);
79}
80
81template<typename ValueType, typename StateType>
82void Choice<ValueType, StateType>::addLabels(std::set<std::string> const& newLabels) {
83 if (labels) {
84 labels->insert(newLabels.begin(), newLabels.end());
85 } else {
86 labels = newLabels;
87 }
88}
89
90template<typename ValueType, typename StateType>
92 return labels.is_initialized();
93}
94
95template<typename ValueType, typename StateType>
96std::set<std::string> const& Choice<ValueType, StateType>::getLabels() const {
97 return labels.get();
98}
99
100template<typename ValueType, typename StateType>
102 this->playerIndex = playerIndex;
103}
104
105template<typename ValueType, typename StateType>
107 return playerIndex.is_initialized();
108}
109
110template<typename ValueType, typename StateType>
112 return playerIndex.get();
113}
114
115template<typename ValueType, typename StateType>
117 if (!this->originData || this->originData->empty()) {
118 this->originData = data;
119 } else {
120 if (!data.empty()) {
121 // Reaching this point means that the both the existing and the given data are non-empty
122
123 auto existingDataAsIndexSet = boost::any_cast<storm::storage::FlatSet<uint_fast64_t>>(&this->originData.get());
124 if (existingDataAsIndexSet != nullptr) {
125 auto givenDataAsIndexSet = boost::any_cast<storm::storage::FlatSet<uint_fast64_t>>(&data);
126 STORM_LOG_THROW(givenDataAsIndexSet != nullptr, storm::exceptions::InvalidOperationException,
127 "Types of existing and given choice origin data do not match.");
128 existingDataAsIndexSet->insert(givenDataAsIndexSet->begin(), givenDataAsIndexSet->end());
129 } else {
130 STORM_LOG_THROW(false, storm::exceptions::NotImplementedException,
131 "Type of choice origin data (aka " << data.type().name() << ") is not implemented.");
132 }
133 }
134 }
135}
136
137template<typename ValueType, typename StateType>
139 return originData.is_initialized();
140}
141
142template<typename ValueType, typename StateType>
144 return originData.get();
145}
146
147template<typename ValueType, typename StateType>
149 return actionIndex;
150}
151
152template<typename ValueType, typename StateType>
154 return totalMass;
155}
156
157template<typename ValueType, typename StateType>
158void Choice<ValueType, StateType>::addProbability(StateType const& state, ValueType const& value) {
159 totalMass += value;
160 distribution.addProbability(state, value);
161}
162
163template<typename ValueType, typename StateType>
164void Choice<ValueType, StateType>::addReward(ValueType const& value) {
165 rewards.push_back(value);
166}
167
168template<typename ValueType, typename StateType>
169void Choice<ValueType, StateType>::addRewards(std::vector<ValueType>&& values) {
170 this->rewards = std::move(values);
171}
172
173template<typename ValueType, typename StateType>
174std::vector<ValueType> const& Choice<ValueType, StateType>::getRewards() const {
175 return rewards;
176}
177
178template<typename ValueType, typename StateType>
180 return markovian;
181}
182
183template<typename ValueType, typename StateType>
185 return distribution.size();
186}
187
188template<typename ValueType, typename StateType>
189void Choice<ValueType, StateType>::reserve(std::size_t const& size) {
190 distribution.reserve(size);
191}
192
193template<typename ValueType, typename StateType>
194std::ostream& operator<<(std::ostream& out, Choice<ValueType, StateType> const& choice) {
195 out << "<";
196 for (auto const& stateProbabilityPair : choice) {
197 out << stateProbabilityPair.first << " : " << stateProbabilityPair.second << ", ";
198 }
199 out << ">";
200 return out;
201}
202
203template struct Choice<double>;
204
205#ifdef STORM_HAVE_CARL
206template struct Choice<storm::RationalNumber>;
207template struct Choice<storm::RationalFunction>;
208#endif
209} // namespace generator
210} // namespace storm
container_type::const_iterator const_iterator
container_type::iterator iterator
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
std::ostream & operator<<(std::ostream &out, Choice< ValueType, StateType > const &choice)
Definition Choice.cpp:194
uint64_t PlayerIndex
Definition PlayerIndex.h:7
LabParser.cpp.
Definition cli.cpp:18
bool hasLabels() const
Returns whether there are labels defined for this choice.
Definition Choice.cpp:91
bool isMarkovian() const
Retrieves whether the choice is Markovian.
Definition Choice.cpp:179
boost::any const & getOriginData() const
Returns the origin data associated with this choice.
Definition Choice.cpp:143
void addLabel(std::string const &label)
Adds the given label to the labels associated with this choice.
Definition Choice.cpp:74
uint_fast64_t getActionIndex() const
Retrieves the index of the action of this choice.
Definition Choice.cpp:148
storm::storage::Distribution< ValueType, StateType >::iterator end()
Returns an iterator past the end of the distribution associated with this choice.
Definition Choice.cpp:64
std::vector< ValueType > const & getRewards() const
Retrieves the rewards for this choice under selected reward models.
Definition Choice.cpp:174
std::set< std::string > const & getLabels() const
Retrieves the set of labels associated with this choice.
Definition Choice.cpp:96
void add(Choice const &other)
Adds the given choice to the current one.
Definition Choice.cpp:22
ValueType getTotalMass() const
Retrieves the total mass of this choice.
Definition Choice.cpp:153
void addOriginData(boost::any const &data)
Adds the given data that specifies the origin of this choice w.r.t.
Definition Choice.cpp:116
void setPlayerIndex(storm::storage::PlayerIndex const &playerIndex)
Sets the players index.
Definition Choice.cpp:101
storm::storage::PlayerIndex const & getPlayerIndex() const
Retrieves the players index associated with this choice.
Definition Choice.cpp:111
Choice(uint_fast64_t actionIndex=0, bool markovian=false)
Definition Choice.cpp:16
void addLabels(std::set< std::string > const &labels)
Adds the given label set to the labels associated with this choice.
Definition Choice.cpp:82
bool hasOriginData() const
Returns whether there is origin data defined for this choice.
Definition Choice.cpp:138
void addRewards(std::vector< ValueType > &&values)
Adds the given choices rewards to this choice.
Definition Choice.cpp:169
StateType sampleFromDistribution(ValueType const &quantile) const
Given a value q, find the event in the ordered distribution that corresponds to this prob.
Definition Choice.cpp:49
storm::storage::Distribution< ValueType, StateType >::iterator begin()
Returns an iterator to the distribution associated with this choice.
Definition Choice.cpp:54
void reserve(std::size_t const &size)
If the size is already known, reserves space in the underlying distribution.
Definition Choice.cpp:189
bool hasPlayerIndex() const
Returns whether there is an index for the player defined for this choice.
Definition Choice.cpp:106
std::size_t size() const
Retrieves the size of the distribution associated with this choice.
Definition Choice.cpp:184
void addProbability(StateType const &state, ValueType const &value)
Adds the given probability value to the given state in the underlying distribution.
Definition Choice.cpp:158
void addReward(ValueType const &value)
Adds the given value to the reward associated with this choice.
Definition Choice.cpp:164