3#include <carl/core/rootfinder/RootFinder.h>
12#include <boost/functional/hash.hpp>
35template<
typename ParametricType,
typename ConstantType>
37 std::vector<ParametricType>
const& pVector,
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++;
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++;
57 uint64_t pMatrixEntryCount = 0;
58 uint64_t pVectorEntryCount = 0;
63 this->occurringVariablesAtState.resize(pMatrix.
getRowCount());
65 for (uint64_t row = 0; row < pMatrix.
getRowCount(); row++) {
66 if (!selectedRows.
get(row)) {
69 std::set<VariableType> occurringVariables;
70 for (
auto const& entry : pMatrix.
getRow(row)) {
71 auto column = entry.getColumn();
72 if (!selectedColumns.
get(column)) {
76 auto transition = entry.getValue();
78 auto variables = transition.gatherVariables();
79 occurringVariables.insert(variables.begin(), variables.end());
84 nonConstMatrixEntries.set(pMatrixEntryCount,
true);
86 builder.addNextValue(oldToNewColumnIndexMapping[row], oldToNewColumnIndexMapping[column],
Interval());
87 Interval& placeholder = functionValuationCollector.add(valuation);
95 for (
auto& var : occurringVariables) {
96 occuringStatesAtVariable[var].insert(row);
98 occurringVariablesAtState[row] = std::move(occurringVariables);
101 for (uint64_t i = 0; i < pVector.size(); i++) {
102 auto const transition = pVector[i];
103 if (!selectedRows.
get(i)) {
109 nonConstVectorEntries.set(pVectorEntryCount,
true);
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);
123 vector.shrink_to_fit();
124 matrixAssignment.shrink_to_fit();
125 vectorAssignment.shrink_to_fit();
126 nonConstMatrixEntries.resize(pMatrixEntryCount);
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;
141 startEntryOfRow = startEntryOfNextRow;
143 STORM_LOG_ASSERT(matrixAssignmentIt == matrixAssignment.end(),
"Unexpected number of entries in the matrix assignment.");
145 auto vectorAssignmentIt = vectorAssignment.begin();
146 for (uint64_t nonConstVectorEntry : nonConstVectorEntries) {
147 for (uint64_t vectorIndex = matrix.getRowGroupIndices()[nonConstVectorEntry]; vectorIndex != matrix.getRowGroupIndices()[nonConstVectorEntry + 1];
149 vectorAssignmentIt->first = vector.begin() + vectorIndex;
150 ++vectorAssignmentIt;
153 STORM_LOG_ASSERT(vectorAssignmentIt == vectorAssignment.end(),
"Unexpected number of entries in the vector assignment.");
156template<
typename ParametricType,
typename ConstantType>
160 this->currentRegionAllIllDefined = functionValuationCollector.evaluateCollectedFunctions(region, dirForParameters);
165 for (
auto& assignment : matrixAssignment) {
166 assignment.first->setValue(assignment.second);
169 for (
auto& assignment : vectorAssignment) {
170 *assignment.first = assignment.second;
174template<
typename ParametricType,
typename ConstantType>
175const std::vector<std::set<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType>>&
177 return occurringVariablesAtState;
180template<
typename ParametricType,
typename ConstantType>
181std::map<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType, std::set<uint_fast64_t>>
const&
183 return occuringStatesAtVariable;
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>();
196 auto expression = rfte.toExpression(function) == expressionManager->rational(0);
198 auto variables = expressionManager->getVariables();
201 for (
auto const& var : variables) {
202 exprBounds = exprBounds && expressionManager->rational(0) <= var && var <= expressionManager->rational(1);
205 smtSolver->setTimeout(50);
207 smtSolver->add(exprBounds);
208 smtSolver->add(expression);
210 std::set<CoefficientType> zeroes = {};
213 auto checkResult = smtSolver->check();
216 auto model = smtSolver->getModel();
219 if (variables.size() != 1) {
222 auto const var = *variables.begin();
224 double value = model->getRationalValue(var);
231 smtSolver->addNotCurrentModel();
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>(
250 carl::rootfinder::SplittingStrategy::ABERTH);
251 std::set<CoefficientType> zeroes = {};
252 for (carl::RealAlgebraicNumber<CoefficientType>
const& root : carlRoots) {
254 if (root.isNumeric()) {
259 zeroes.emplace(rootCoefficient);
264template<
typename ParametricType,
typename ConstantType>
265std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>
268 if (polynomial.isConstant()) {
271 STORM_LOG_ERROR_COND(polynomial.gatherVariables().size() == 1,
"Multi-variate polynomials currently not supported");
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) {
283 switch (term.tdeg()) {
297 STORM_LOG_THROW(
false, storm::exceptions::InvalidArgumentException,
"Transitions are only allowed to have have a maximum degree of four.");
321 return {-b / (2 * a)};
327 std::set<CoefficientType> roots;
331 CoefficientType q = (2 * b * b * b - 9 * a * b * c + 27 * a * a * d) / (27 * a * a * a);
348 roots = {-3 * q / (p * 2), 3 * q / p};
352 roots = {u - p / (3 * u)};
355 double t = std::acos(3 * qDouble / pDouble / u) / 3;
356 double k = 2 * M_PI / 3;
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;
373 for (
auto const& var : occurringVariables) {
374 parameters.emplace(var);
378template<
typename ParametricType,
typename ConstantType>
383template<
typename ParametricType,
typename ConstantType>
388template<
typename ParametricType,
typename ConstantType>
390 return currentRegionAllIllDefined;
393template<
typename ParametricType,
typename ConstantType>
395 return this->transition == other.transition;
398template<
typename ParametricType,
typename ConstantType>
399std::set<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType>
const&
404template<
typename ParametricType,
typename ConstantType>
406 return this->transition;
409template<
typename ParametricType,
typename ConstantType>
412 if (this->extrema || this->annotation) {
420 auto const& terms = annotation.getTerms();
425 std::optional<std::set<CoefficientType>> carlResult;
427 if (terms.size() < 5) {
428 carlResult = zeroesCarl(annotation.getProbability().derivative(), annotation.getParameter());
433 this->extrema = std::map<VariableType, std::set<CoefficientType>>();
434 (*this->extrema)[annotation.getParameter()];
435 for (
auto const& root : *carlResult) {
440 annotation.computeDerivative(4);
442 this->annotation.emplace(annotation);
444 this->extrema = std::map<VariableType, std::set<CoefficientType>>();
446 for (
auto const& p : transition.gatherVariables()) {
447 (*this->extrema)[p] = {};
449 auto const&
derivative = transition.derivative(p);
456 auto nominatorAsUnivariate =
derivative.nominator().toUnivariatePolynomial();
458 nominatorAsUnivariate /=
derivative.denominator().coefficient();
461 std::optional<std::set<CoefficientType>> zeroes;
463 if (
derivative.nominator().totalDegree() < 4) {
469 zeroes = zeroesCarl(nominatorAsUnivariate, p);
473 for (
auto const& zero : *zeroes) {
475 this->extrema->at(p).emplace(zero);
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&
486 return this->extrema;
489template<
typename ParametricType,
typename ConstantType>
491 return this->annotation;
494template<
typename ParametricType,
typename ConstantType>
496 std::size_t seed = 0;
497 boost::hash_combine(seed, transition);
501template<
typename ParametricType,
typename ConstantType>
504 if (!collectedValuations.count(valuation)) {
506 this->regionsAndBounds.emplace(valuation, std::vector<std::pair<Interval, Interval>>());
510 auto insertionRes = collectedValuations.insert(std::pair<RobustAbstractValuation, Interval>(std::move(valuation),
storm::Interval(0, 1)));
511 return insertionRes.first->second;
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);
527 for (
auto const& potentialExtremum : potentialExtrema) {
533 sumOfTerms +=
Interval(*minValue, *maxValue);
538template<
typename ParametricType,
typename ConstantType>
539bool RobustParameterLifter<ParametricType, ConstantType>::FunctionValuationCollector::evaluateCollectedFunctions(
541 std::unordered_map<RobustAbstractValuation, Interval, RobustAbstractValuationHash> insertThese;
542 for (
auto& [abstrValuation, placeholder] : collectedValuations) {
547 if (abstrValuation.getExtrema()) {
554 auto const& maybeAnnotation = abstrValuation.getAnnotation();
556 if (maybeAnnotation) {
558 auto p = maybeAnnotation->getParameter();
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);
571 for (
auto const& potentialExtremum : potentialExtrema) {
585 std::map<VariableType, CoefficientType> lowerPositions;
586 std::map<VariableType, CoefficientType> upperPositions;
588 for (
auto const& p : abstrValuation.getParameters()) {
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);
604 auto instantiation = std::map<VariableType, CoefficientType>(region.
getLowerBoundaries());
606 for (
auto const& potentialExtremum : potentialExtrema) {
608 instantiation[p] = potentialExtremum;
609 auto value = abstrValuation.getTransition().evaluate(instantiation);
610 if (maxValue &= value) {
611 maxPosP = potentialExtremum;
613 if (minValue &= value) {
614 minPosP = potentialExtremum;
617 STORM_LOG_ASSERT(!minValue.empty(),
"Expected at least one potential extremum.");
619 lowerPositions[p] = minPosP;
620 upperPositions[p] = maxPosP;
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();
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()) {
659 regionsInPLARegion.push_back(i);
663 uint64_t regionsRefine = std::max((uint64_t)10, annotation.maxDegree());
664 refine = regionsInPLARegion.size() < regionsRefine;
667 std::vector<Interval> newIntervals;
668 auto diameter = plaRegion.diameter();
670 if (regionsAndBounds.empty()) {
671 regionsAndBounds.emplace_back(plaRegion,
Interval(0, 1));
672 regionsInPLARegion.push_back(0);
675 if (regionsAndBounds[regionsInPLARegion.front()].first.lower() < plaRegion.lower()) {
676 newIntervals.push_back(
Interval(regionsAndBounds[regionsInPLARegion.front()].first.lower(), plaRegion.lower()));
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));
684 if (regionsAndBounds[regionsInPLARegion.back()].first.upper() > plaRegion.upper()) {
685 newIntervals.push_back(
Interval(plaRegion.upper(), regionsAndBounds[regionsInPLARegion.back()].first.upper()));
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]);
693 regionsAndBounds.erase(regionsAndBounds.begin() + *regionsInPLARegion.begin(), regionsAndBounds.end());
696 for (
auto const& region : newIntervals) {
697 regionsAndBounds.emplace_back(region, annotation.evaluateOnIntervalMidpointTheorem(region));
700 for (
auto const& item : regionsAndBoundsAfter) {
701 regionsAndBounds.emplace_back(item);
707 const ConstantType epsilon = 0;
714 placeholder =
Interval(lowerBound, upperBound);
716 for (
auto& key : insertThese) {
717 this->collectedValuations.insert(std::move(insertThese.extract(key.first)));
A bit vector that is internally represented as a vector of 64-bit values.
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.
#define STORM_LOG_ASSERT(cond, message)
#define STORM_LOG_ERROR_COND(cond, message)
#define STORM_LOG_THROW(cond, exception, message)
Expression maximum(Expression const &first, Expression const &second)
Expression minimum(Expression const &first, Expression const &second)
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.
ValueType max(ValueType const &first, ValueType const &second)
Extremum< storm::OptimizationDirection::Maximize, ValueType > Maximum
Extremum< storm::OptimizationDirection::Minimize, ValueType > Minimum
bool isConstant(ValueType const &)
ValueType min(ValueType const &first, ValueType const &second)
bool isZero(ValueType const &a)
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