Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ParameterRegion.cpp
Go to the documentation of this file.
2
3#include <limits>
4
9
10namespace storm {
11namespace storage {
12
13template<typename ParametricType>
17
18template<typename ParametricType>
19ParameterRegion<ParametricType>::ParameterRegion(Valuation const& lowerBoundaries, Valuation const& upperBoundaries)
20 : lowerBoundaries(lowerBoundaries), upperBoundaries(upperBoundaries) {
21 init();
22}
23
24template<typename ParametricType>
26 : lowerBoundaries(lowerBoundaries), upperBoundaries(upperBoundaries) {
27 init();
30template<typename ParametricType>
32 // check whether both mappings map the same variables, check that lower boundary <= upper boundary, and pre-compute the set of variables
33 for (auto const& variableWithLowerBoundary : this->lowerBoundaries) {
34 auto variableWithUpperBoundary = this->upperBoundaries.find(variableWithLowerBoundary.first);
35 STORM_LOG_THROW((variableWithUpperBoundary != upperBoundaries.end()), storm::exceptions::InvalidArgumentException,
36 "Could not create region. No upper boundary specified for Variable " << variableWithLowerBoundary.first);
37 STORM_LOG_THROW((variableWithLowerBoundary.second <= variableWithUpperBoundary->second), storm::exceptions::InvalidArgumentException,
38 "Could not create region. The lower boundary for " << variableWithLowerBoundary.first << " is larger then the upper boundary");
39 this->variables.insert(variableWithLowerBoundary.first);
40 this->sortedOnDifference.insert({variableWithLowerBoundary.second - variableWithUpperBoundary->second, variableWithLowerBoundary.first});
41 }
42 for (auto const& variableWithBoundary : this->upperBoundaries) {
43 STORM_LOG_THROW((this->variables.find(variableWithBoundary.first) != this->variables.end()), storm::exceptions::InvalidArgumentException,
44 "Could not create region. No lower boundary specified for Variable " << variableWithBoundary.first);
45 }
46 this->splitThreshold = variables.size();
47}
48
49template<typename ParametricType>
50std::set<typename ParameterRegion<ParametricType>::VariableType> const& ParameterRegion<ParametricType>::getVariables() const {
51 return this->variables;
52}
53
54template<typename ParametricType>
55std::multimap<typename ParameterRegion<ParametricType>::CoefficientType, typename ParameterRegion<ParametricType>::VariableType> const&
57 return this->sortedOnDifference;
58}
60template<typename ParametricType>
62 auto const& result = lowerBoundaries.find(variable);
63 STORM_LOG_THROW(result != lowerBoundaries.end(), storm::exceptions::InvalidArgumentException,
64 "Tried to find a lower boundary for variable " << variable << " which is not specified by this region");
65 return (*result).second;
66}
67
68template<typename ParametricType>
70 for (auto itr = lowerBoundaries.begin(); itr != lowerBoundaries.end(); ++itr) {
71 if (itr->first.name().compare(varName) == 0) {
72 return (*itr).second;
73 }
74 }
75 STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException,
76 "Tried to find a lower boundary for variableName " << varName << " which is not specified by this region");
77}
78
79template<typename ParametricType>
81 auto const& result = upperBoundaries.find(variable);
82 STORM_LOG_THROW(result != upperBoundaries.end(), storm::exceptions::InvalidArgumentException,
83 "Tried to find an upper boundary for variable " << variable << " which is not specified by this region");
84 return (*result).second;
86
87template<typename ParametricType>
89 for (auto itr = upperBoundaries.begin(); itr != upperBoundaries.end(); ++itr) {
90 if (itr->first.name().compare(varName) == 0) {
91 return (*itr).second;
92 }
93 }
94 STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException,
95 "Tried to find an upper boundary for variableName " << varName << " which is not specified by this region");
96}
97
98template<typename ParametricType>
100 return getUpperBoundary(variable) - getLowerBoundary(variable);
101}
102
103template<typename ParametricType>
105 return getUpperBoundary(varName) - getLowerBoundary(varName);
106}
107
108template<typename ParametricType>
110 return (getLowerBoundary(variable) + getUpperBoundary(variable)) / 2;
111}
112
113template<typename ParametricType>
115 return (getLowerBoundary(varName) + getUpperBoundary(varName)) / 2;
116}
117
118template<typename ParametricType>
122
123template<typename ParametricType>
127
128template<typename ParametricType>
129std::vector<typename ParameterRegion<ParametricType>::Valuation> ParameterRegion<ParametricType>::getVerticesOfRegion(
130 std::set<VariableType> const& consideredVariables) const {
131 std::size_t const numOfVariables = consideredVariables.size();
132 STORM_LOG_THROW(numOfVariables <= std::numeric_limits<std::size_t>::digits, storm::exceptions::OutOfRangeException,
133 "Number of variables " << numOfVariables << " is too high.");
134 std::size_t const numOfVertices = std::pow(2, numOfVariables);
135 std::vector<Valuation> resultingVector(numOfVertices);
136
137 for (uint_fast64_t vertexId = 0; vertexId < numOfVertices; ++vertexId) {
138 // interprete vertexId as a bit sequence
139 // the consideredVariables.size() least significant bits of vertex will always represent the next vertex
140 //(00...0 = lower boundaries for all variables, 11...1 = upper boundaries for all variables)
141 uint_fast64_t variableIndex = 0;
142
143 for (auto variable : consideredVariables) {
144 if ((vertexId >> variableIndex) % 2 == 0) {
145 resultingVector[vertexId].insert(std::pair<VariableType, CoefficientType>(variable, getLowerBoundary(variable)));
146 } else {
147 resultingVector[vertexId].insert(std::pair<VariableType, CoefficientType>(variable, getUpperBoundary(variable)));
148 }
149 ++variableIndex;
150 }
151 }
152 return resultingVector;
153}
154
155template<typename ParametricType>
157 return this->getLowerBoundaries();
158}
159
160template<typename ParametricType>
162 Valuation result;
163 for (auto const& variable : this->variables) {
164 result.emplace(variable, getCenter(variable));
165 }
166 return result;
167}
168
169template<typename ParametricType>
171 CoefficientType result = storm::utility::one<CoefficientType>();
172 for (auto const& variable : this->variables) {
173 if (this->getUpperBoundary(variable) != this->getLowerBoundary(variable)) {
174 result *= (this->getUpperBoundary(variable) - this->getLowerBoundary(variable));
175 } else {
176 // HACK to get regions with zero area to work correctly
177 // (It's a hack but it's a harmless one for now, as regions with zero area do not exist without discrete parameters)
178 // This area represents half of the area of the region
179 result /= utility::convertNumber<CoefficientType>(2);
180 }
181 }
182 return result;
183}
184
185template<typename ParametricType>
187 for (auto const& variable : this->variables) {
188 STORM_LOG_ASSERT(point.count(variable) > 0,
189 "Tried to check if a point is in a region, but the point does not contain a value for variable " << variable);
190 auto pointEntry = point.find(variable)->second;
191 if (pointEntry < this->getLowerBoundary(variable) || pointEntry > this->getUpperBoundary(variable)) {
192 return false;
193 }
194 }
195 return true;
196}
197
198template<typename ParametricType>
199void ParameterRegion<ParametricType>::split(Valuation const& splittingPoint, std::vector<ParameterRegion<ParametricType>>& regionVector) const {
200 return split(splittingPoint, regionVector, variables, {});
201}
202
203template<typename ParametricType>
204void ParameterRegion<ParametricType>::split(Valuation const& splittingPoint, std::vector<storm::storage::ParameterRegion<ParametricType>>& regionVector,
205 const std::set<VariableType>& consideredVariables, const std::set<VariableType>& discreteVariables) const {
206 std::set<VariableType> vertexVariables = consideredVariables;
207 // Remove the discrete variables that are already unit from the considered
208 // variables set, so we don't split them again
209 for (auto const& var : discreteVariables) {
210 if (this->getDifference(var) == storm::utility::zero<CoefficientType>()) {
211 vertexVariables.erase(var);
212 }
213 }
214
215 auto vertices = getVerticesOfRegion(vertexVariables);
216
217 for (auto const& vertex : vertices) {
218 // The resulting subregion is the smallest region containing vertex and splittingPoint.
219 Valuation subLower, subUpper;
220 for (auto variableBound : this->lowerBoundaries) {
221 VariableType variable = variableBound.first;
222 auto vertexEntry = vertex.find(variable);
223 if (vertexEntry != vertex.end()) {
224 if (discreteVariables.find(variable) != discreteVariables.end()) {
225 // As this parameter is discrete, we set this parameter to the vertex entry (splitting point does not matter)
226 // e.g. we split the region p in [0,1], q in [0,1] at center with q being discrete
227 // then we get the regions [0,0.5]x[0,1] and [0.5,1]x[0,1]
228 subLower.insert(typename Valuation::value_type(variable, vertexEntry->second));
229 subUpper.insert(typename Valuation::value_type(variable, vertexEntry->second));
230 } else {
231 auto splittingPointEntry = splittingPoint.find(variable);
232 subLower.insert(typename Valuation::value_type(variable, std::min(vertexEntry->second, splittingPointEntry->second)));
233 subUpper.insert(typename Valuation::value_type(variable, std::max(vertexEntry->second, splittingPointEntry->second)));
234 }
235 } else {
236 subLower.insert(typename Valuation::value_type(variable, getLowerBoundary(variable)));
237 subUpper.insert(typename Valuation::value_type(variable, getUpperBoundary(variable)));
238 }
239 }
240
241 ParameterRegion<ParametricType> subRegion(std::move(subLower), std::move(subUpper));
242
243 if (!storm::utility::isZero(subRegion.area())) {
244 regionVector.push_back(std::move(subRegion));
245 }
246 }
247}
248
249template<typename ParametricType>
250std::string ParameterRegion<ParametricType>::toString(bool boundariesAsDouble) const {
251 std::stringstream regionstringstream;
252 if (boundariesAsDouble) {
253 for (auto var : this->getVariables()) {
254 regionstringstream << storm::utility::convertNumber<double>(this->getLowerBoundary(var));
255 regionstringstream << "<=";
256 regionstringstream << var;
257 regionstringstream << "<=";
258 regionstringstream << storm::utility::convertNumber<double>(this->getUpperBoundary(var));
259 regionstringstream << ",";
260 }
261 } else {
262 for (auto var : this->getVariables()) {
263 regionstringstream << this->getLowerBoundary(var);
264 regionstringstream << "<=";
265 regionstringstream << var;
266 regionstringstream << "<=";
267 regionstringstream << this->getUpperBoundary(var);
268 regionstringstream << ",";
269 }
270 }
271 std::string regionstring = regionstringstream.str();
272 // the last comma should actually be a semicolon
273 regionstring = regionstring.substr(0, regionstring.length() - 1) + ";";
274 return regionstring;
275}
276
277template<typename ParametricType>
279 auto varsRegion = getVariables();
280 auto varsSubRegion = subRegion.getVariables();
281 for (auto var : varsRegion) {
282 if (std::find(varsSubRegion.begin(), varsSubRegion.end(), var) != varsSubRegion.end()) {
283 if (getLowerBoundary(var) > subRegion.getLowerBoundary(var) || getUpperBoundary(var) < getUpperBoundary(var)) {
284 return false;
285 }
286 } else {
287 return false;
288 }
289 }
290 return true;
291}
292
293template<typename ParametricType>
296 auto val = this->getCenterPoint();
297 for (auto monResEntry : monRes.getMonotonicityResult()) {
298 if (monRes.isDoneForVar(monResEntry.first)) {
300 val[monResEntry.first] = storm::solver::minimize(dir) ? getLowerBoundary(monResEntry.first) : getUpperBoundary(monResEntry.first);
302 val[monResEntry.first] = storm::solver::maximize(dir) ? getLowerBoundary(monResEntry.first) : getUpperBoundary(monResEntry.first);
303 }
304 }
305 }
306 return val;
307}
308
309template<typename ParametricType>
311 std::set<VariableType> const& monIncrParameters,
312 std::set<VariableType> const& monDecrParameters) const {
313 auto val = this->getCenterPoint();
314 for (auto var : monIncrParameters) {
315 val[var] = storm::solver::minimize(dir) ? getLowerBoundary(var) : getUpperBoundary(var);
316 }
317 for (auto var : monDecrParameters) {
318 val[var] = storm::solver::maximize(dir) ? getLowerBoundary(var) : getUpperBoundary(var);
319 }
320
321 return val;
322}
323
324template<typename ParametricType>
328
329template<typename ParametricType>
331 parentBound = bound;
332}
333
334template<typename ParametricType>
335std::ostream& operator<<(std::ostream& out, ParameterRegion<ParametricType> const& region) {
336 out << region.toString();
337 return out;
338}
339
340#ifdef STORM_HAVE_CARL
341template class ParameterRegion<storm::RationalFunction>;
342template std::ostream& operator<<(std::ostream& out, ParameterRegion<storm::RationalFunction> const& region);
343#endif
344
345} // namespace storage
346} // namespace storm
std::map< VariableType, Monotonicity > const & getMonotonicityResult() const
Returns the results so far.
storm::utility::parametric::CoefficientType< ParametricType >::type CoefficientType
std::set< VariableType > const & getVariables() const
Valuation getCenterPoint() const
Returns the center point of this region.
CoefficientType area() const
Returns the area of this region.
void setBoundParent(CoefficientType bound)
std::string toString(bool boundariesAsDouble=false) const
storm::utility::parametric::Valuation< ParametricType > Valuation
Valuation const & getLowerBoundaries() const
CoefficientType getDifference(const std::string varName) const
storm::utility::parametric::VariableType< ParametricType >::type VariableType
Valuation getPoint(storm::solver::OptimizationDirection dir, storm::analysis::MonotonicityResult< VariableType > &monRes) const
bool contains(Valuation const &point) const
Returns whether the given point is in this region.
CoefficientType const & getLowerBoundary(VariableType const &variable) const
void split(Valuation const &splittingPoint, std::vector< ParameterRegion< ParametricType > > &regionVector) const
Splits the region at the given point and inserts the resulting subregions at the end of the given vec...
bool isSubRegion(ParameterRegion< ParametricType > region)
CoefficientType const & getUpperBoundary(VariableType const &variable) const
Valuation getSomePoint() const
Returns some point that lies within this region.
CoefficientType getCenter(const std::string varName) const
Valuation const & getUpperBoundaries() const
std::multimap< CoefficientType, VariableType > const & getVariablesSorted() const
std::vector< Valuation > getVerticesOfRegion(std::set< VariableType > const &consideredVariables) const
Returns a vector of all possible combinations of lower and upper bounds of the given variables.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
bool constexpr maximize(OptimizationDirection d)
bool constexpr minimize(OptimizationDirection d)
bool isZero(ValueType const &a)
Definition constants.cpp:42
LabParser.cpp.