Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
RobustParameterLifter.cpp
Go to the documentation of this file.
2
3#include <carl/core/rootfinder/RootFinder.h>
4#include <algorithm>
5#include <cmath>
6#include <map>
7#include <memory>
8#include <optional>
9#include <set>
10#include <vector>
11
12#include <boost/functional/hash.hpp>
13
27
28std::unordered_map<storm::RationalFunction, storm::transformer::Annotation> storm::transformer::BigStep::lastSavedAnnotations;
29
30namespace storm {
31namespace transformer {
32
34
35template<typename ParametricType, typename ConstantType>
37 std::vector<ParametricType> const& pVector,
38 storm::storage::BitVector const& selectedRows,
39 storm::storage::BitVector const& selectedColumns, bool generateRowLabels,
40 bool useMonotonicity) {
41 oldToNewColumnIndexMapping = std::vector<uint64_t>(selectedColumns.size(), selectedColumns.size());
42 uint64_t newIndexColumns = 0;
43 for (uint64_t oldColumn : selectedColumns) {
44 oldToNewColumnIndexMapping[oldColumn] = newIndexColumns++;
45 }
46
47 oldToNewRowIndexMapping = std::vector<uint64_t>(selectedRows.size(), selectedRows.size());
48 uint64_t newIndexRows = 0;
49 for (uint64_t oldRow : selectedRows) {
50 oldToNewRowIndexMapping[oldRow] = newIndexRows++;
51 }
52
53 // Stores which entries of the original matrix/vector are non-constant. Entries for non-selected rows/columns are omitted
54 auto nonConstMatrixEntries = storm::storage::BitVector(pMatrix.getEntryCount(), false); // this vector has to be resized later
55 auto nonConstVectorEntries = storm::storage::BitVector(selectedRows.getNumberOfSetBits(), false);
56 // Counters for selected entries in the pMatrix and the pVector
57 uint64_t pMatrixEntryCount = 0;
58 uint64_t pVectorEntryCount = 0;
59
60 // The matrix builder for the new matrix. The correct number of rows and entries is not known yet.
61 storm::storage::SparseMatrixBuilder<Interval> builder(newIndexRows, newIndexColumns, 0, true, false);
62
63 this->occurringVariablesAtState.resize(pMatrix.getRowCount());
64
65 for (uint64_t row = 0; row < pMatrix.getRowCount(); row++) {
66 if (!selectedRows.get(row)) {
67 continue;
68 }
69 std::set<VariableType> occurringVariables;
70 for (auto const& entry : pMatrix.getRow(row)) {
71 auto column = entry.getColumn();
72 if (!selectedColumns.get(column)) {
73 continue;
74 }
75
76 auto transition = entry.getValue();
77
78 auto variables = transition.gatherVariables();
79 occurringVariables.insert(variables.begin(), variables.end());
80
81 if (storm::utility::isConstant(transition)) {
82 builder.addNextValue(oldToNewColumnIndexMapping[row], oldToNewColumnIndexMapping[column], utility::convertNumber<double>(transition));
83 } else {
84 nonConstMatrixEntries.set(pMatrixEntryCount, true);
85 auto valuation = RobustAbstractValuation(transition);
86 builder.addNextValue(oldToNewColumnIndexMapping[row], oldToNewColumnIndexMapping[column], Interval());
87 Interval& placeholder = functionValuationCollector.add(valuation);
88 matrixAssignment.push_back(std::pair<typename storm::storage::SparseMatrix<Interval>::iterator, Interval&>(
90 }
91 pMatrixEntryCount++;
92 }
93
94 // Save the occuringVariables of a state, needed if we want to use monotonicity
95 for (auto& var : occurringVariables) {
96 occuringStatesAtVariable[var].insert(row);
97 }
98 occurringVariablesAtState[row] = std::move(occurringVariables);
99 }
100
101 for (uint64_t i = 0; i < pVector.size(); i++) {
102 auto const transition = pVector[i];
103 if (!selectedRows.get(i)) {
104 continue;
105 }
106 if (storm::utility::isConstant(transition)) {
107 vector.push_back(utility::convertNumber<double>(transition));
108 } else {
109 nonConstVectorEntries.set(pVectorEntryCount, true);
110 auto valuation = RobustAbstractValuation(transition);
111 vector.push_back(Interval());
112 Interval& placeholder = functionValuationCollector.add(valuation);
113 vectorAssignment.push_back(std::pair<typename std::vector<Interval>::iterator, Interval&>(typename std::vector<Interval>::iterator(), placeholder));
114 for (auto const& var : valuation.getParameters()) {
115 occuringStatesAtVariable[var].insert(i);
116 occurringVariablesAtState[i].emplace(var);
117 }
118 }
119 pVectorEntryCount++;
120 }
121
122 matrix = builder.build();
123 vector.shrink_to_fit();
124 matrixAssignment.shrink_to_fit();
125 vectorAssignment.shrink_to_fit();
126 nonConstMatrixEntries.resize(pMatrixEntryCount);
127
128 // Now insert the correct iterators for the matrix and vector assignment
129 auto matrixAssignmentIt = matrixAssignment.begin();
130 uint64_t startEntryOfRow = 0;
131 for (uint64_t group = 0; group < matrix.getRowGroupCount(); ++group) {
132 uint64_t startEntryOfNextRow = startEntryOfRow + matrix.getRow(group, 0).getNumberOfEntries();
133 for (uint64_t matrixRow = matrix.getRowGroupIndices()[group]; matrixRow < matrix.getRowGroupIndices()[group + 1]; ++matrixRow) {
134 auto matrixEntryIt = matrix.getRow(matrixRow).begin();
135 for (uint64_t nonConstEntryIndex = nonConstMatrixEntries.getNextSetIndex(startEntryOfRow); nonConstEntryIndex < startEntryOfNextRow;
136 nonConstEntryIndex = nonConstMatrixEntries.getNextSetIndex(nonConstEntryIndex + 1)) {
137 matrixAssignmentIt->first = matrixEntryIt + (nonConstEntryIndex - startEntryOfRow);
138 ++matrixAssignmentIt;
139 }
140 }
141 startEntryOfRow = startEntryOfNextRow;
142 }
143 STORM_LOG_ASSERT(matrixAssignmentIt == matrixAssignment.end(), "Unexpected number of entries in the matrix assignment.");
144
145 auto vectorAssignmentIt = vectorAssignment.begin();
146 for (uint64_t nonConstVectorEntry : nonConstVectorEntries) {
147 for (uint64_t vectorIndex = matrix.getRowGroupIndices()[nonConstVectorEntry]; vectorIndex != matrix.getRowGroupIndices()[nonConstVectorEntry + 1];
148 ++vectorIndex) {
149 vectorAssignmentIt->first = vector.begin() + vectorIndex;
150 ++vectorAssignmentIt;
151 }
152 }
153 STORM_LOG_ASSERT(vectorAssignmentIt == vectorAssignment.end(), "Unexpected number of entries in the vector assignment.");
154}
155
156template<typename ParametricType, typename ConstantType>
158 storm::solver::OptimizationDirection const& dirForParameters) {
159 // write the evaluation result of each function,evaluation pair into the placeholders
160 this->currentRegionAllIllDefined = functionValuationCollector.evaluateCollectedFunctions(region, dirForParameters);
161
162 // TODO Return if currentRegionAllIllDefined? Or write to matrix?
163
164 // apply the matrix and vector assignments to write the contents of the placeholder into the matrix/vector
165 for (auto& assignment : matrixAssignment) {
166 assignment.first->setValue(assignment.second);
167 }
168
169 for (auto& assignment : vectorAssignment) {
170 *assignment.first = assignment.second;
171 }
172}
173
174template<typename ParametricType, typename ConstantType>
175const std::vector<std::set<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType>>&
179
180template<typename ParametricType, typename ConstantType>
181std::map<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType, std::set<uint_fast64_t>> const&
185
186template<typename ParametricType, typename ConstantType>
187std::optional<std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>>
188RobustParameterLifter<ParametricType, ConstantType>::RobustAbstractValuation::zeroesSMT(
190 std::shared_ptr<storm::expressions::ExpressionManager> expressionManager = std::make_shared<storm::expressions::ExpressionManager>();
191
192 auto smtSolver = utility::solver::getSmtSolver(*expressionManager);
193
195
196 auto expression = rfte.toExpression(function) == expressionManager->rational(0);
197
198 auto variables = expressionManager->getVariables();
199 // Sum the summands together directly in the expression so we pass this info to the solver
200 expressions::Expression exprBounds = expressionManager->boolean(true);
201 for (auto const& var : variables) {
202 exprBounds = exprBounds && expressionManager->rational(0) <= var && var <= expressionManager->rational(1);
203 }
204
205 smtSolver->setTimeout(50);
206
207 smtSolver->add(exprBounds);
208 smtSolver->add(expression);
209
210 std::set<CoefficientType> zeroes = {};
211
212 while (true) {
213 auto checkResult = smtSolver->check();
214
215 if (checkResult == solver::SmtSolver::CheckResult::Sat) {
216 auto model = smtSolver->getModel();
217
218 STORM_LOG_ERROR_COND(variables.size() == 1, "Should be one variable.");
219 if (variables.size() != 1) {
220 return {};
221 }
222 auto const var = *variables.begin();
223
224 double value = model->getRationalValue(var);
225
226 zeroes.emplace(utility::convertNumber<CoefficientType>(value));
227
228 // Add new constraint so we search for the next zero in the polynomial
229 // Get another model (or unsat)
230 // For some reason, this only really works when we then make a new
231 smtSolver->addNotCurrentModel();
232 } else if (checkResult == solver::SmtSolver::CheckResult::Unknown) {
233 return std::nullopt;
234 break;
235 } else {
236 // Unsat => found all zeroes :)
237 break;
238 }
239 }
240 return zeroes;
241}
242
243template<typename ParametricType, typename ConstantType>
244std::optional<std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>>
248 auto const& carlRoots = carl::rootfinder::realRoots<CoefficientType, CoefficientType>(
249 polynomial, carl::Interval<CoefficientType>(utility::zero<CoefficientType>(), utility::one<CoefficientType>()),
250 carl::rootfinder::SplittingStrategy::ABERTH);
251 std::set<CoefficientType> zeroes = {};
252 for (carl::RealAlgebraicNumber<CoefficientType> const& root : carlRoots) {
253 CoefficientType rootCoefficient;
254 if (root.isNumeric()) {
255 rootCoefficient = CoefficientType(root.value());
256 } else {
257 rootCoefficient = CoefficientType((root.upper() + root.lower()) / 2);
258 }
259 zeroes.emplace(rootCoefficient);
260 }
261 return zeroes;
262}
263
264template<typename ParametricType, typename ConstantType>
265std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>
268 if (polynomial.isConstant()) {
269 return {};
270 }
271 STORM_LOG_ERROR_COND(polynomial.gatherVariables().size() == 1, "Multi-variate polynomials currently not supported");
272 // Polynomial is a*p^3 + b*p^2 + c*p + d
273
274 // Recover factors from polynomial
275 CoefficientType a = utility::zero<CoefficientType>(), b = a, c = a, d = a;
277 for (auto const& term : polynomial.getTerms()) {
278 STORM_LOG_ASSERT(term.getNrVariables() <= 1, "No terms with more than one variable allowed but " << term << " has " << term.getNrVariables());
279 if (!term.isConstant() && term.getSingleVariable() != parameter) {
280 continue;
281 }
282 CoefficientType coefficient = term.coeff();
283 switch (term.tdeg()) {
284 case 0:
285 d = coefficient;
286 break;
287 case 1:
288 c = coefficient;
289 break;
290 case 2:
291 b = coefficient;
292 break;
293 case 3:
294 a = coefficient;
295 break;
296 default:
297 STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Transitions are only allowed to have have a maximum degree of four.");
298 break;
299 }
300 }
301 // Translated from https://stackoverflow.com/questions/27176423/function-to-solve-cubic-equation-analytically
302
303 // Quadratic case
304 if (utility::isZero(a)) {
305 a = b;
306 b = c;
307 c = d;
308 // Linear case
309 if (utility::isZero(a)) {
310 a = b;
311 b = c;
312 // Constant case
313 if (utility::isZero(a)) {
314 return {};
315 }
316 return {-b / a};
317 }
318
319 CoefficientType D = b * b - 4 * a * c;
320 if (utility::isZero(D)) {
321 return {-b / (2 * a)};
322 } else if (D > 0) {
323 return {(-b + utility::sqrt(D)) / (2 * a), (-b - utility::sqrt(D)) / (2 * a)};
324 }
325 return {};
326 }
327 std::set<CoefficientType> roots;
328
329 // Convert to depressed cubic t^3+pt+q = 0 (subst x = t - b/3a)
330 CoefficientType p = (3 * a * c - b * b) / (3 * a * a);
331 CoefficientType q = (2 * b * b * b - 9 * a * b * c + 27 * a * a * d) / (27 * a * a * a);
332 double pDouble = utility::convertNumber<ConstantType>(p);
333 double qDouble = utility::convertNumber<ConstantType>(q);
334
335 if (utility::isZero(p)) { // p = 0 -> t^3 = -q -> t = -q^1/3
336 roots = {utility::convertNumber<CoefficientType>(std::cbrt(-qDouble))};
337 } else if (utility::isZero(q)) { // q = 0 -> t^3 + pt = 0 -> t(t^2+p)=0
338 roots = {0};
339 if (p < 0) {
342 }
343 } else {
344 // These are all coefficients (we also plug the values into RationalFunctions later), i.e., they are rational numbers,
345 // but some of these operations are strictly real, so we convert to double and back (i.e., approximate).
346 CoefficientType D = q * q / 4 + p * p * p / 27;
347 if (utility::isZero(D)) { // D = 0 -> two roots
348 roots = {-3 * q / (p * 2), 3 * q / p};
349 } else if (D > 0) { // Only one real root
350 double Ddouble = utility::convertNumber<ConstantType>(D);
351 CoefficientType u = utility::convertNumber<CoefficientType>(std::cbrt(-qDouble / 2 - utility::sqrt(Ddouble)));
352 roots = {u - p / (3 * u)};
353 } else { // D < 0, three roots, but needs to use complex numbers/trigonometric solution
354 double u = 2 * utility::sqrt(-pDouble / 3);
355 double t = std::acos(3 * qDouble / pDouble / u) / 3; // D < 0 implies p < 0 and acos argument in [-1..1]
356 double k = 2 * M_PI / 3;
357
358 roots = {utility::convertNumber<CoefficientType>(u * std::cos(t)), utility::convertNumber<CoefficientType>(u * std::cos(t - k)),
359 utility::convertNumber<CoefficientType>(u * std::cos(t - 2 * k))};
360 }
361 }
362
363 return roots;
364}
365
366template<typename ParametricType, typename ConstantType>
368 : transition(transition) {
369 STORM_LOG_ERROR_COND(transition.denominator().isConstant(), "Robust PLA only supports transitions with constant denominators.");
370 transition.simplify();
371 std::set<VariableType> occurringVariables;
372 storm::utility::parametric::gatherOccurringVariables(transition, occurringVariables);
373 for (auto const& var : occurringVariables) {
374 parameters.emplace(var);
375 }
376}
377
378template<typename ParametricType, typename ConstantType>
382
383template<typename ParametricType, typename ConstantType>
385 return vector;
386}
387
388template<typename ParametricType, typename ConstantType>
390 return currentRegionAllIllDefined;
391}
392
393template<typename ParametricType, typename ConstantType>
395 return this->transition == other.transition;
396}
397
398template<typename ParametricType, typename ConstantType>
399std::set<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType> const&
403
404template<typename ParametricType, typename ConstantType>
408
409template<typename ParametricType, typename ConstantType>
411 // TODO This function is a mess
412 if (this->extrema || this->annotation) {
413 // Extrema already initialized
414 return;
415 }
416
417 if (BigStep::lastSavedAnnotations.count(transition)) {
418 auto& annotation = BigStep::lastSavedAnnotations.at(transition);
419
420 auto const& terms = annotation.getTerms();
421
422 // Try to find all zeroes of all derivatives with the SMT solver.
423 // TODO: Are we even using that this is a sum of terms?
424
425 std::optional<std::set<CoefficientType>> carlResult;
426
427 if (terms.size() < 5) {
428 carlResult = zeroesCarl(annotation.getProbability().derivative(), annotation.getParameter());
429 }
430
431 if (carlResult) {
432 // Hooray, we found the zeroes with the SMT solver / CARL
433 this->extrema = std::map<VariableType, std::set<CoefficientType>>();
434 (*this->extrema)[annotation.getParameter()];
435 for (auto const& root : *carlResult) {
436 (*this->extrema).at(annotation.getParameter()).emplace(utility::convertNumber<CoefficientType>(root));
437 }
438 } else {
439 // TODO make evaluation depth configurable
440 annotation.computeDerivative(4);
441 }
442 this->annotation.emplace(annotation);
443 } else {
444 this->extrema = std::map<VariableType, std::set<CoefficientType>>();
445
446 for (auto const& p : transition.gatherVariables()) {
447 (*this->extrema)[p] = {};
448
449 auto const& derivative = transition.derivative(p);
450
451 if (derivative.isConstant()) {
452 continue;
453 }
454
455 // There is no annotation for this transition:
456 auto nominatorAsUnivariate = derivative.nominator().toUnivariatePolynomial();
457 // Constant denominator is now distributed in the factors, not in the denominator of the rational function
458 nominatorAsUnivariate /= derivative.denominator().coefficient();
459
460 // Compute zeros of derivative (= maxima/minima of function) and emplace those between 0 and 1 into the maxima set
461 std::optional<std::set<CoefficientType>> zeroes;
462 // Find zeroes with straight-forward method for degrees <4, find them with SMT for degrees above that
463 if (derivative.nominator().totalDegree() < 4) {
464 zeroes = cubicEquationZeroes(RawPolynomial(derivative.nominator()), p);
465 } else {
466 zeroes = zeroesSMT(derivative, p);
467 if (!zeroes) {
468 // The SMT solver could not find the zeroes, e.g., because of a timeout. Fall back to carl's root finder.
469 zeroes = zeroesCarl(nominatorAsUnivariate, p);
470 }
471 }
472 STORM_LOG_ERROR_COND(zeroes, "Zeroes of " << derivative << " could not be found.");
473 for (auto const& zero : *zeroes) {
475 this->extrema->at(p).emplace(zero);
476 }
477 }
478 }
479 }
480}
481
482template<typename ParametricType, typename ConstantType>
483std::optional<std::map<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType,
484 std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>>> const&
488
489template<typename ParametricType, typename ConstantType>
491 return this->annotation;
492}
493
494template<typename ParametricType, typename ConstantType>
496 std::size_t seed = 0;
497 boost::hash_combine(seed, transition);
498 return seed;
499}
500
501template<typename ParametricType, typename ConstantType>
502Interval& RobustParameterLifter<ParametricType, ConstantType>::FunctionValuationCollector::add(RobustAbstractValuation& valuation) {
503 // If no valuation like this is present in the collectedValuations, initialize the extrema
504 if (!collectedValuations.count(valuation)) {
505 valuation.initialize();
506 this->regionsAndBounds.emplace(valuation, std::vector<std::pair<Interval, Interval>>());
507 }
508 // insert the function and the valuation
509 // Note that references to elements of an unordered map remain valid after calling unordered_map::insert.
510 auto insertionRes = collectedValuations.insert(std::pair<RobustAbstractValuation, Interval>(std::move(valuation), storm::Interval(0, 1)));
511 return insertionRes.first->second;
512}
513
514Interval evaluateExtremaAnnotations(std::map<UniPoly, std::set<double>> extremaAnnotations, Interval input) {
515 Interval sumOfTerms(0.0, 0.0);
516 for (auto const& [poly, roots] : extremaAnnotations) {
517 std::set<double> potentialExtrema = {input.lower(), input.upper()};
518 for (auto const& root : roots) {
519 if (root >= input.lower() && root <= input.upper()) {
520 potentialExtrema.emplace(root);
521 }
522 }
523
526
527 for (auto const& potentialExtremum : potentialExtrema) {
528 auto value = utility::convertNumber<double>(poly.evaluate(utility::convertNumber<RationalFunctionCoefficient>(potentialExtremum)));
529 maxValue &= value;
530 minValue &= value;
531 }
532 STORM_LOG_ASSERT(!minValue.empty(), "Expected at least one potential extremum.");
533 sumOfTerms += Interval(*minValue, *maxValue);
534 }
535 return sumOfTerms;
536}
537
538template<typename ParametricType, typename ConstantType>
539bool RobustParameterLifter<ParametricType, ConstantType>::FunctionValuationCollector::evaluateCollectedFunctions(
540 storm::storage::ParameterRegion<ParametricType> const& region, storm::solver::OptimizationDirection const& dirForUnspecifiedParameters) {
541 std::unordered_map<RobustAbstractValuation, Interval, RobustAbstractValuationHash> insertThese;
542 for (auto& [abstrValuation, placeholder] : collectedValuations) {
543 // Results of our computations go here, we use different methods
544 ConstantType lowerBound = utility::zero<ConstantType>();
545 ConstantType upperBound = utility::zero<ConstantType>();
546
547 if (abstrValuation.getExtrema()) {
548 // We know the extrema of this abstract valuation => we can get the exact bounds easily
549
550 // If an annotation exists:
551 // Evaluating the annotation is cheaper than evaluating the RationalFunction, which isn't prime-factorized
552 // If no annotation exists:
553 // The RationalFunction is hopefully prime-factorized
554 auto const& maybeAnnotation = abstrValuation.getAnnotation();
555
556 if (maybeAnnotation) {
557 // We only have one parameter and can evaluate the annotation directly
558 auto p = maybeAnnotation->getParameter();
559
560 CoefficientType lowerP = region.getLowerBoundary(p);
561 CoefficientType upperP = region.getUpperBoundary(p);
562 std::set<CoefficientType> potentialExtrema = {lowerP, upperP};
563 for (auto const& maximum : abstrValuation.getExtrema()->at(p)) {
564 if (maximum >= lowerP && maximum <= upperP) {
565 potentialExtrema.emplace(maximum);
566 }
567 }
568
571 for (auto const& potentialExtremum : potentialExtrema) {
572 // Possible optimization: evaluate all transitions together, keeping track of intermediate results
573 auto value = maybeAnnotation->evaluate(utility::convertNumber<double>(potentialExtremum));
574 maximum &= value;
575 minimum &= value;
576 }
577 STORM_LOG_ASSERT(!minimum.empty(), "Expected at least one potential extremum.");
578 lowerBound = *minimum;
579 upperBound = *maximum;
580 } else {
581 // We may have multiple parameters, but the derivatives w.r.t. each parameter only contain that parameter
582 // We first figure out the positions of the lower and upper bounds per parameter
583 // Lower/upper bound of every parameter is independent because the transitions are sums of terms with one parameter each
584 // At the end, we compute the value
585 std::map<VariableType, CoefficientType> lowerPositions;
586 std::map<VariableType, CoefficientType> upperPositions;
587
588 for (auto const& p : abstrValuation.getParameters()) {
589 CoefficientType lowerP = region.getLowerBoundary(p);
590 CoefficientType upperP = region.getUpperBoundary(p);
591
592 std::set<CoefficientType> potentialExtrema = {lowerP, upperP};
593 for (auto const& maximum : abstrValuation.getExtrema()->at(p)) {
594 if (maximum >= lowerP && maximum <= upperP) {
595 potentialExtrema.emplace(maximum);
596 }
597 }
598
599 CoefficientType minPosP;
600 CoefficientType maxPosP;
603
604 auto instantiation = std::map<VariableType, CoefficientType>(region.getLowerBoundaries());
605
606 for (auto const& potentialExtremum : potentialExtrema) {
607 // We modify the instantiation to have value potentialExtremum at p, keeping other parameters the same
608 instantiation[p] = potentialExtremum;
609 auto value = abstrValuation.getTransition().evaluate(instantiation);
610 if (maxValue &= value) {
611 maxPosP = potentialExtremum;
612 }
613 if (minValue &= value) {
614 minPosP = potentialExtremum;
615 }
616 }
617 STORM_LOG_ASSERT(!minValue.empty(), "Expected at least one potential extremum.");
618
619 lowerPositions[p] = minPosP;
620 upperPositions[p] = maxPosP;
621 }
622
623 // Compute function values at left and right ends
624 lowerBound = utility::convertNumber<ConstantType>(abstrValuation.getTransition().evaluate(lowerPositions));
625 upperBound = utility::convertNumber<ConstantType>(abstrValuation.getTransition().evaluate(upperPositions));
626 }
627
628 if (upperBound < utility::zero<ConstantType>() || lowerBound > utility::one<ConstantType>()) {
629 // Current region is entirely ill-defined (partially ill-defined is fine:)
630 return true;
631 }
632 } else {
633 STORM_LOG_ASSERT(abstrValuation.getAnnotation(), "Needs to have annotation if no zeroes.");
634 auto& regionsAndBounds = this->regionsAndBounds.at(abstrValuation);
635 auto const& annotation = *abstrValuation.getAnnotation();
636
637 auto plaRegion = Interval(region.getLowerBoundary(annotation.getParameter()), region.getUpperBoundary(annotation.getParameter()));
638
639 bool refine = false;
640 do {
641 lowerBound = 1.0;
642 upperBound = 0.0;
643 std::vector<uint64_t> regionsInPLARegion;
644 for (uint64_t i = 0; i < regionsAndBounds.size(); i++) {
645 auto const& [region, bound] = regionsAndBounds[i];
647 i == 0 ? true : (!(region.upper() < regionsAndBounds[i - 1].first.lower() || region.lower() > regionsAndBounds[i - 1].first.upper())),
648 "Regions next to each other need to intersect.");
649 if (region.upper() <= plaRegion.lower() || region.lower() >= plaRegion.upper()) {
650 if (regionsInPLARegion.empty()) {
651 continue;
652 } else {
653 // Regions are sorted => we've walked past the interesting part
654 break;
655 }
656 }
657 lowerBound = utility::min(lowerBound, bound.lower());
658 upperBound = utility::max(upperBound, bound.upper());
659 regionsInPLARegion.push_back(i);
660 }
661
662 // TODO make this configurable
663 uint64_t regionsRefine = std::max((uint64_t)10, annotation.maxDegree());
664 refine = regionsInPLARegion.size() < regionsRefine;
665
666 if (refine) {
667 std::vector<Interval> newIntervals;
668 auto diameter = plaRegion.diameter();
669 // If we have no regions at all, initialize with the entire region
670 if (regionsAndBounds.empty()) {
671 regionsAndBounds.emplace_back(plaRegion, Interval(0, 1));
672 regionsInPLARegion.push_back(0);
673 }
674 // Add start (old regions might be larger than currently considered region)
675 if (regionsAndBounds[regionsInPLARegion.front()].first.lower() < plaRegion.lower()) {
676 newIntervals.push_back(Interval(regionsAndBounds[regionsInPLARegion.front()].first.lower(), plaRegion.lower()));
677 }
678 // Split up considered region
679 for (uint64_t i = 0; i < regionsRefine; i++) {
680 newIntervals.push_back(Interval(plaRegion.lower() + ((double)i / (double)regionsRefine) * diameter,
681 plaRegion.lower() + ((double)(i + 1) / (double)regionsRefine) * diameter));
682 }
683 // Add end
684 if (regionsAndBounds[regionsInPLARegion.back()].first.upper() > plaRegion.upper()) {
685 newIntervals.push_back(Interval(plaRegion.upper(), regionsAndBounds[regionsInPLARegion.back()].first.upper()));
686 }
687 // Remember everything that comes after what we changed
688 std::vector<std::pair<Interval, Interval>> regionsAndBoundsAfter;
689 for (uint64_t i = regionsInPLARegion.back() + 1; i < regionsAndBounds.size(); i++) {
690 regionsAndBoundsAfter.push_back(regionsAndBounds[i]);
691 }
692 // Remove previous results
693 regionsAndBounds.erase(regionsAndBounds.begin() + *regionsInPLARegion.begin(), regionsAndBounds.end());
694
695 // Compute region results using interval arithmetic
696 for (auto const& region : newIntervals) {
697 regionsAndBounds.emplace_back(region, annotation.evaluateOnIntervalMidpointTheorem(region));
698 }
699 // Emplace back remembered stuff
700 for (auto const& item : regionsAndBoundsAfter) {
701 regionsAndBounds.emplace_back(item);
702 }
703 }
704 } while (refine);
705 }
706
707 const ConstantType epsilon = 0;
708 // We want to check in the realm of feasible instantiations, even if our not our entire parameter space is feasible
709 lowerBound = utility::max(utility::min(lowerBound, utility::one<ConstantType>() - epsilon), epsilon);
710 upperBound = utility::max(utility::min(upperBound, utility::one<ConstantType>() - epsilon), epsilon);
711
712 STORM_LOG_ASSERT(lowerBound <= upperBound, "Whoops.");
713
714 placeholder = Interval(lowerBound, upperBound);
715 }
716 for (auto& key : insertThese) {
717 this->collectedValuations.insert(std::move(insertThese.extract(key.first)));
718 }
719 return false;
720}
721
723} // namespace transformer
724} // namespace storm
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
uint64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
size_t size() const
Retrieves the number of bits this bit vector can store.
bool get(uint64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
Valuation const & getLowerBoundaries() const
CoefficientType const & getLowerBoundary(VariableType const &variable) const
CoefficientType const & getUpperBoundary(VariableType const &variable) const
A class that can be used to build a sparse matrix by adding value by value.
A class that holds a possibly non-square matrix in the compressed row storage format.
const_rows getRow(index_type row) const
Returns an object representing the given row.
index_type getEntryCount() const
Returns the number of entries in the matrix.
std::vector< MatrixEntry< index_type, value_type > >::iterator iterator
index_type getRowCount() const
Returns the number of rows of the matrix.
static std::unordered_map< RationalFunction, Annotation > lastSavedAnnotations
Definition BigStep.h:207
std::optional< std::map< VariableType, std::set< CoefficientType > > > const & getExtrema() const
storm::storage::SparseMatrix< Interval > const & getMatrix() const
std::map< VariableType, std::set< uint_fast64_t > > const & getOccuringStatesAtVariable() const
std::vector< Interval > const & getVector() const
storm::utility::parametric::CoefficientType< ParametricType >::type CoefficientType
std::vector< std::set< VariableType > > const & getOccurringVariablesAtState() const
void specifyRegion(storm::storage::ParameterRegion< ParametricType > const &region, storm::solver::OptimizationDirection const &dirForParameters)
storm::utility::parametric::VariableType< ParametricType >::type VariableType
RobustParameterLifter(storm::storage::SparseMatrix< ParametricType > const &pMatrix, std::vector< ParametricType > const &pVector, storm::storage::BitVector const &selectedRows, storm::storage::BitVector const &selectedColumns, bool generateRowLabels=false, bool useMonotonicity=false)
Lifts the parameter choices to nondeterminisim.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_ERROR_COND(cond, message)
Definition macros.h:68
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
Expression maximum(Expression const &first, Expression const &second)
Expression minimum(Expression const &first, Expression const &second)
storm::utility::parametric::CoefficientType< storm::RationalFunction >::type CoefficientType
Interval evaluateExtremaAnnotations(std::map< UniPoly, std::set< double > > extremaAnnotations, Interval input)
carl::UnivariatePolynomial< RationalFunctionCoefficient > UniPoly
Definition BigStep.cpp:33
void gatherOccurringVariables(FunctionType const &function, std::set< typename VariableType< FunctionType >::type > &variableSet)
Add all variables that occur in the given function to the the given set.
std::unique_ptr< storm::solver::SmtSolver > getSmtSolver(storm::expressions::ExpressionManager &manager)
Creates a new SMT solver instance.
Definition solver.cpp:215
ValueType max(ValueType const &first, ValueType const &second)
Extremum< storm::OptimizationDirection::Maximize, ValueType > Maximum
Definition Extremum.h:166
Extremum< storm::OptimizationDirection::Minimize, ValueType > Minimum
Definition Extremum.h:168
bool isConstant(ValueType const &)
ValueType min(ValueType const &first, ValueType const &second)
bool isZero(ValueType const &a)
Definition constants.cpp:44
ValueType zero()
Definition constants.cpp:26
ValueType one()
Definition constants.cpp:21
ValueType sqrt(ValueType const &number)
TargetType convertNumber(SourceType const &number)
The contract of a DD backend.
carl::Interval< double > Interval
Interval type.
carl::RationalFunction< Polynomial, true > RationalFunction
carl::MultivariatePolynomial< RationalFunctionCoefficient > RawPolynomial