3#include <carl/core/rootfinder/RootFinder.h>
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>();
193 auto smtSolver = factory.
create(*expressionManager);
197 auto expression = rfte.toExpression(function) == expressionManager->rational(0);
199 auto variables = expressionManager->getVariables();
202 for (
auto const& var : variables) {
203 exprBounds = exprBounds && expressionManager->rational(0) <= var && var <= expressionManager->rational(1);
206 smtSolver->setTimeout(50);
208 smtSolver->add(exprBounds);
209 smtSolver->add(expression);
211 std::set<CoefficientType> zeroes = {};
214 auto checkResult = smtSolver->check();
217 auto model = smtSolver->getModel();
220 if (variables.size() != 1) {
223 auto const var = *variables.begin();
225 double value = model->getRationalValue(var);
232 smtSolver->addNotCurrentModel();
244template<
typename ParametricType,
typename ConstantType>
245std::optional<std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>>
249 auto const& carlRoots = carl::rootfinder::realRoots<CoefficientType, CoefficientType>(
251 carl::rootfinder::SplittingStrategy::ABERTH);
252 std::set<CoefficientType> zeroes = {};
253 for (carl::RealAlgebraicNumber<CoefficientType>
const& root : carlRoots) {
255 if (root.isNumeric()) {
260 zeroes.emplace(rootCoefficient);
265template<
typename ParametricType,
typename ConstantType>
266std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>
269 if (polynomial.isConstant()) {
272 STORM_LOG_ERROR_COND(polynomial.gatherVariables().size() == 1,
"Multi-variate polynomials currently not supported");
278 for (
auto const& term : polynomial.getTerms()) {
279 STORM_LOG_ASSERT(term.getNrVariables() <= 1,
"No terms with more than one variable allowed but " << term <<
" has " << term.getNrVariables());
280 if (!term.isConstant() && term.getSingleVariable() != parameter) {
284 switch (term.tdeg()) {
298 STORM_LOG_THROW(
false, storm::exceptions::InvalidArgumentException,
"Transitions are only allowed to have have a maximum degree of four.");
322 return {-b / (2 * a)};
328 std::set<CoefficientType> roots;
332 CoefficientType q = (2 * b * b * b - 9 * a * b * c + 27 * a * a * d) / (27 * a * a * a);
349 roots = {-3 * q / (p * 2), 3 * q / p};
353 roots = {u - p / (3 * u)};
356 double t = std::acos(3 * qDouble / pDouble / u) / 3;
357 double k = 2 * M_PI / 3;
367template<
typename ParametricType,
typename ConstantType>
369 : transition(transition) {
370 STORM_LOG_ERROR_COND(transition.denominator().isConstant(),
"Robust PLA only supports transitions with constant denominators.");
371 transition.simplify();
372 std::set<VariableType> occurringVariables;
374 for (
auto const& var : occurringVariables) {
375 parameters.emplace(var);
379template<
typename ParametricType,
typename ConstantType>
384template<
typename ParametricType,
typename ConstantType>
389template<
typename ParametricType,
typename ConstantType>
391 return currentRegionAllIllDefined;
394template<
typename ParametricType,
typename ConstantType>
396 return this->transition == other.transition;
399template<
typename ParametricType,
typename ConstantType>
400std::set<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType>
const&
405template<
typename ParametricType,
typename ConstantType>
407 return this->transition;
410template<
typename ParametricType,
typename ConstantType>
413 if (this->extrema || this->annotation) {
421 auto const& terms = annotation.getTerms();
426 std::optional<std::set<CoefficientType>> carlResult;
428 if (terms.size() < 5) {
429 carlResult = zeroesCarl(annotation.getProbability().derivative(), annotation.getParameter());
434 this->extrema = std::map<VariableType, std::set<CoefficientType>>();
435 (*this->extrema)[annotation.getParameter()];
436 for (
auto const& root : *carlResult) {
441 annotation.computeDerivative(4);
443 this->annotation.emplace(annotation);
445 this->extrema = std::map<VariableType, std::set<CoefficientType>>();
447 for (
auto const& p : transition.gatherVariables()) {
448 (*this->extrema)[p] = {};
450 auto const&
derivative = transition.derivative(p);
457 auto nominatorAsUnivariate =
derivative.nominator().toUnivariatePolynomial();
459 nominatorAsUnivariate /=
derivative.denominator().coefficient();
462 std::optional<std::set<CoefficientType>> zeroes;
464 if (
derivative.nominator().totalDegree() < 4) {
470 for (
auto const& zero : *zeroes) {
472 this->extrema->at(p).emplace(zero);
479template<
typename ParametricType,
typename ConstantType>
480std::optional<std::map<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType,
481 std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>>>
const&
483 return this->extrema;
486template<
typename ParametricType,
typename ConstantType>
488 return this->annotation;
491template<
typename ParametricType,
typename ConstantType>
493 std::size_t seed = 0;
494 carl::hash_add(seed, transition);
498template<
typename ParametricType,
typename ConstantType>
501 if (!collectedValuations.count(valuation)) {
503 this->regionsAndBounds.emplace(valuation, std::vector<std::pair<Interval, Interval>>());
507 auto insertionRes = collectedValuations.insert(std::pair<RobustAbstractValuation, Interval>(std::move(valuation),
storm::Interval(0, 1)));
508 return insertionRes.first->second;
513 for (
auto const& [poly, roots] : extremaAnnotations) {
514 std::set<double> potentialExtrema = {input.lower(), input.upper()};
515 for (
auto const& root : roots) {
516 if (root >= input.lower() && root <= input.upper()) {
517 potentialExtrema.emplace(root);
524 for (
auto const& potentialExtremum : potentialExtrema) {
530 sumOfTerms +=
Interval(*minValue, *maxValue);
535template<
typename ParametricType,
typename ConstantType>
536bool RobustParameterLifter<ParametricType, ConstantType>::FunctionValuationCollector::evaluateCollectedFunctions(
538 std::unordered_map<RobustAbstractValuation, Interval, RobustAbstractValuationHash> insertThese;
539 for (
auto& [abstrValuation, placeholder] : collectedValuations) {
544 if (abstrValuation.getExtrema()) {
551 auto const& maybeAnnotation = abstrValuation.getAnnotation();
553 if (maybeAnnotation) {
555 auto p = maybeAnnotation->getParameter();
559 std::set<CoefficientType> potentialExtrema = {lowerP, upperP};
560 for (
auto const& maximum : abstrValuation.getExtrema()->at(p)) {
561 if (maximum >= lowerP && maximum <= upperP) {
562 potentialExtrema.emplace(maximum);
568 for (
auto const& potentialExtremum : potentialExtrema) {
582 std::map<VariableType, CoefficientType> lowerPositions;
583 std::map<VariableType, CoefficientType> upperPositions;
585 for (
auto const& p : abstrValuation.getParameters()) {
589 std::set<CoefficientType> potentialExtrema = {lowerP, upperP};
590 for (
auto const& maximum : abstrValuation.getExtrema()->at(p)) {
591 if (maximum >= lowerP && maximum <= upperP) {
592 potentialExtrema.emplace(maximum);
601 auto instantiation = std::map<VariableType, CoefficientType>(region.
getLowerBoundaries());
603 for (
auto const& potentialExtremum : potentialExtrema) {
605 instantiation[p] = potentialExtremum;
606 auto value = abstrValuation.getTransition().evaluate(instantiation);
607 if (maxValue &= value) {
608 maxPosP = potentialExtremum;
610 if (minValue &= value) {
611 minPosP = potentialExtremum;
614 STORM_LOG_ASSERT(!minValue.empty(),
"Expected at least one potential extremum.");
616 lowerPositions[p] = minPosP;
617 upperPositions[p] = maxPosP;
630 STORM_LOG_ASSERT(abstrValuation.getAnnotation(),
"Needs to have annotation if no zeroes.");
631 auto& regionsAndBounds = this->regionsAndBounds.at(abstrValuation);
632 auto const& annotation = *abstrValuation.getAnnotation();
640 std::vector<uint64_t> regionsInPLARegion;
641 for (uint64_t i = 0;
i < regionsAndBounds.size();
i++) {
642 auto const& [region, bound] = regionsAndBounds[
i];
644 i == 0 ?
true : (!(region.upper() < regionsAndBounds[i - 1].first.lower() || region.lower() > regionsAndBounds[i - 1].first.upper())),
645 "Regions next to each other need to intersect.");
646 if (region.upper() <= plaRegion.lower() || region.lower() >= plaRegion.upper()) {
647 if (regionsInPLARegion.empty()) {
656 regionsInPLARegion.push_back(i);
660 uint64_t regionsRefine = std::max((uint64_t)10, annotation.maxDegree());
661 refine = regionsInPLARegion.size() < regionsRefine;
664 std::vector<Interval> newIntervals;
665 auto diameter = plaRegion.diameter();
667 if (regionsAndBounds.empty()) {
668 regionsAndBounds.emplace_back(plaRegion,
Interval(0, 1));
669 regionsInPLARegion.push_back(0);
672 if (regionsAndBounds[regionsInPLARegion.front()].first.lower() < plaRegion.lower()) {
673 newIntervals.push_back(
Interval(regionsAndBounds[regionsInPLARegion.front()].first.lower(), plaRegion.lower()));
676 for (uint64_t i = 0;
i < regionsRefine;
i++) {
677 newIntervals.push_back(
Interval(plaRegion.lower() + ((
double)i / (
double)regionsRefine) * diameter,
678 plaRegion.lower() + ((
double)(i + 1) / (
double)regionsRefine) * diameter));
681 if (regionsAndBounds[regionsInPLARegion.back()].first.upper() > plaRegion.upper()) {
682 newIntervals.push_back(
Interval(plaRegion.upper(), regionsAndBounds[regionsInPLARegion.back()].first.upper()));
685 std::vector<std::pair<Interval, Interval>> regionsAndBoundsAfter;
686 for (uint64_t i = regionsInPLARegion.back() + 1; i < regionsAndBounds.size(); i++) {
687 regionsAndBoundsAfter.push_back(regionsAndBounds[i]);
690 regionsAndBounds.erase(regionsAndBounds.begin() + *regionsInPLARegion.begin(), regionsAndBounds.end());
693 for (
auto const& region : newIntervals) {
694 regionsAndBounds.emplace_back(region, annotation.evaluateOnIntervalMidpointTheorem(region));
697 for (
auto const& item : regionsAndBoundsAfter) {
698 regionsAndBounds.emplace_back(item);
708 const ConstantType epsilon = 0;
715 placeholder =
Interval(lowerBound, upperBound);
717 for (
auto& key : insertThese) {
718 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.
virtual std::unique_ptr< storm::solver::SmtSolver > create(storm::expressions::ExpressionManager &manager) const
Creates a new SMT solver instance.
#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.
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)
carl::Interval< double > Interval
Interval type.
carl::RationalFunction< Polynomial, true > RationalFunction
carl::MultivariatePolynomial< RationalFunctionCoefficient > RawPolynomial