12#include <carl/core/rootfinder/RootFinder.h>
38namespace transformer {
42template<
typename ParametricType,
typename ConstantType>
44 std::vector<ParametricType>
const& pVector,
47 bool useMonotonicity) {
48 oldToNewColumnIndexMapping = std::vector<uint64_t>(selectedColumns.
size(), selectedColumns.
size());
49 uint64_t newIndexColumns = 0;
50 for (
auto const& oldColumn : selectedColumns) {
51 oldToNewColumnIndexMapping[oldColumn] = newIndexColumns++;
54 oldToNewRowIndexMapping = std::vector<uint64_t>(selectedRows.
size(), selectedRows.
size());
55 uint64_t newIndexRows = 0;
56 for (
auto const& oldRow : selectedRows) {
57 oldToNewRowIndexMapping[oldRow] = newIndexRows++;
64 uint64_t pMatrixEntryCount = 0;
65 uint64_t pVectorEntryCount = 0;
70 this->occurringVariablesAtState.resize(pMatrix.
getRowCount());
72 for (uint64_t row = 0; row < pMatrix.
getRowCount(); row++) {
73 if (!selectedRows.
get(row)) {
76 std::set<VariableType> occurringVariables;
77 for (
auto const& entry : pMatrix.
getRow(row)) {
78 auto column = entry.getColumn();
79 if (!selectedColumns.
get(column)) {
83 auto transition = entry.getValue();
85 auto variables = transition.gatherVariables();
86 occurringVariables.insert(variables.begin(), variables.end());
89 builder.
addNextValue(oldToNewColumnIndexMapping[row], oldToNewColumnIndexMapping[column], utility::convertNumber<double>(transition));
91 nonConstMatrixEntries.set(pMatrixEntryCount,
true);
93 builder.
addNextValue(oldToNewColumnIndexMapping[row], oldToNewColumnIndexMapping[column],
Interval());
94 Interval& placeholder = functionValuationCollector.add(valuation);
102 for (
auto& var : occurringVariables) {
103 occuringStatesAtVariable[var].insert(row);
105 occurringVariablesAtState[row] = std::move(occurringVariables);
108 for (uint64_t i = 0; i < pVector.size(); i++) {
109 auto const transition = pVector[i];
110 if (!selectedRows.
get(i)) {
114 vector.push_back(utility::convertNumber<double>(transition));
116 nonConstVectorEntries.set(pVectorEntryCount,
true);
119 Interval& placeholder = functionValuationCollector.add(valuation);
120 vectorAssignment.push_back(std::pair<
typename std::vector<Interval>::iterator,
Interval&>(
typename std::vector<Interval>::iterator(), placeholder));
121 for (
auto const& var : valuation.getParameters()) {
122 occuringStatesAtVariable[var].insert(i);
123 occurringVariablesAtState[i].emplace(var);
129 matrix = builder.
build();
130 vector.shrink_to_fit();
131 matrixAssignment.shrink_to_fit();
132 vectorAssignment.shrink_to_fit();
133 nonConstMatrixEntries.resize(pMatrixEntryCount);
136 auto matrixAssignmentIt = matrixAssignment.begin();
137 uint64_t startEntryOfRow = 0;
138 for (uint64_t group = 0; group < matrix.getRowGroupCount(); ++group) {
139 uint64_t startEntryOfNextRow = startEntryOfRow + matrix.getRow(group, 0).getNumberOfEntries();
140 for (uint64_t matrixRow = matrix.getRowGroupIndices()[group]; matrixRow < matrix.getRowGroupIndices()[group + 1]; ++matrixRow) {
141 auto matrixEntryIt = matrix.getRow(matrixRow).begin();
142 for (uint64_t nonConstEntryIndex = nonConstMatrixEntries.getNextSetIndex(startEntryOfRow); nonConstEntryIndex < startEntryOfNextRow;
143 nonConstEntryIndex = nonConstMatrixEntries.getNextSetIndex(nonConstEntryIndex + 1)) {
144 matrixAssignmentIt->first = matrixEntryIt + (nonConstEntryIndex - startEntryOfRow);
145 ++matrixAssignmentIt;
148 startEntryOfRow = startEntryOfNextRow;
150 STORM_LOG_ASSERT(matrixAssignmentIt == matrixAssignment.end(),
"Unexpected number of entries in the matrix assignment.");
152 auto vectorAssignmentIt = vectorAssignment.begin();
153 for (
auto const& nonConstVectorEntry : nonConstVectorEntries) {
154 for (uint64_t vectorIndex = matrix.getRowGroupIndices()[nonConstVectorEntry]; vectorIndex != matrix.getRowGroupIndices()[nonConstVectorEntry + 1];
156 vectorAssignmentIt->first = vector.begin() + vectorIndex;
157 ++vectorAssignmentIt;
160 STORM_LOG_ASSERT(vectorAssignmentIt == vectorAssignment.end(),
"Unexpected number of entries in the vector assignment.");
163template<
typename ParametricType,
typename ConstantType>
167 this->currentRegionAllIllDefined = functionValuationCollector.evaluateCollectedFunctions(region, dirForParameters);
172 for (
auto& assignment : matrixAssignment) {
173 assignment.first->setValue(assignment.second);
176 for (
auto& assignment : vectorAssignment) {
177 *assignment.first = assignment.second;
181template<
typename ParametricType,
typename ConstantType>
182const std::vector<std::set<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType>>&
184 return occurringVariablesAtState;
187template<
typename ParametricType,
typename ConstantType>
188std::map<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType, std::set<uint_fast64_t>>
const&
190 return occuringStatesAtVariable;
193template<
typename ParametricType,
typename ConstantType>
194std::optional<std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>>
197 std::shared_ptr<storm::expressions::ExpressionManager> expressionManager = std::make_shared<storm::expressions::ExpressionManager>();
200 auto smtSolver = factory.
create(*expressionManager);
204 auto expression = rfte.toExpression(function) == expressionManager->rational(0);
206 auto variables = expressionManager->getVariables();
209 for (
auto const& var : variables) {
210 exprBounds = exprBounds && expressionManager->rational(0) <= var && var <= expressionManager->rational(1);
213 smtSolver->setTimeout(50);
215 smtSolver->add(exprBounds);
216 smtSolver->add(expression);
218 std::set<CoefficientType> zeroes = {};
221 auto checkResult = smtSolver->check();
224 auto model = smtSolver->getModel();
227 if (variables.size() != 1) {
230 auto const var = *variables.begin();
232 double value = model->getRationalValue(var);
234 zeroes.emplace(utility::convertNumber<CoefficientType>(value));
239 smtSolver->addNotCurrentModel();
251template<
typename ParametricType,
typename ConstantType>
252std::optional<std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>>
253RobustParameterLifter<ParametricType, ConstantType>::RobustAbstractValuation::zeroesCarl(
256 auto const& carlRoots = carl::rootfinder::realRoots<CoefficientType, CoefficientType>(
258 carl::rootfinder::SplittingStrategy::ABERTH);
259 std::set<CoefficientType> zeroes = {};
260 for (carl::RealAlgebraicNumber<CoefficientType>
const& root : carlRoots) {
262 if (root.isNumeric()) {
267 zeroes.emplace(rootCoefficient);
272template<
typename ParametricType,
typename ConstantType>
273std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>
274RobustParameterLifter<ParametricType, ConstantType>::RobustAbstractValuation::cubicEquationZeroes(
276 if (polynomial.isConstant()) {
279 STORM_LOG_ERROR_COND(polynomial.gatherVariables().size() == 1,
"Multi-variate polynomials currently not supported");
283 CoefficientType a = utility::zero<CoefficientType>(), b = a, c = a, d = a;
284 utility::convertNumber<ConstantType>(a);
285 for (
auto const& term : polynomial.getTerms()) {
286 STORM_LOG_ASSERT(term.getNrVariables() <= 1,
"No terms with more than one variable allowed but " << term <<
" has " << term.getNrVariables());
287 if (!term.isConstant() && term.getSingleVariable() != parameter) {
291 STORM_LOG_ASSERT(term.tdeg() < 4,
"Transitions are only allowed to have a maximum degree of four.");
292 switch (term.tdeg()) {
327 return {-b / (2 * a)};
333 std::set<CoefficientType> roots;
337 CoefficientType q = (2 * b * b * b - 9 * a * b * c + 27 * a * a * d) / (27 * a * a * a);
338 double pDouble = utility::convertNumber<ConstantType>(p);
339 double qDouble = utility::convertNumber<ConstantType>(q);
342 roots = {utility::convertNumber<CoefficientType>(std::cbrt(-qDouble))};
346 roots.emplace(utility::convertNumber<CoefficientType>(
utility::sqrt(-pDouble)));
347 roots.emplace(utility::convertNumber<CoefficientType>(-
utility::sqrt(-pDouble)));
354 roots = {-3 * q / (p * 2), 3 * q / p};
356 double Ddouble = utility::convertNumber<ConstantType>(D);
358 roots = {u - p / (3 * u)};
361 double t = std::acos(3 * qDouble / pDouble / u) / 3;
362 double k = 2 * M_PI / 3;
364 roots = {utility::convertNumber<CoefficientType>(u * std::cos(t)), utility::convertNumber<CoefficientType>(u * std::cos(t - k)),
365 utility::convertNumber<CoefficientType>(u * std::cos(t - 2 * k))};
372template<
typename ParametricType,
typename ConstantType>
374 : transition(transition) {
375 STORM_LOG_ERROR_COND(transition.denominator().isConstant(),
"Robust PLA only supports transitions with constant denominators.");
376 transition.simplify();
377 std::set<VariableType> occurringVariables;
379 for (
auto const& var : occurringVariables) {
380 parameters.emplace(var);
384template<
typename ParametricType,
typename ConstantType>
389template<
typename ParametricType,
typename ConstantType>
394template<
typename ParametricType,
typename ConstantType>
396 return currentRegionAllIllDefined;
399template<
typename ParametricType,
typename ConstantType>
401 return this->transition == other.transition;
404template<
typename ParametricType,
typename ConstantType>
405std::set<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType>
const&
410template<
typename ParametricType,
typename ConstantType>
412 return this->transition;
415template<
typename ParametricType,
typename ConstantType>
418 if (this->extrema || this->annotation) {
426 auto const& terms = annotation.getTerms();
431 std::optional<std::set<CoefficientType>> carlResult;
433 if (terms.size() < 5) {
434 carlResult = zeroesCarl(annotation.getProbability().derivative(), annotation.getParameter());
439 this->extrema = std::map<VariableType, std::set<CoefficientType>>();
440 (*this->extrema)[annotation.getParameter()];
441 for (
auto const& root : *carlResult) {
442 (*this->extrema).at(annotation.getParameter()).emplace(utility::convertNumber<CoefficientType>(root));
446 annotation.computeDerivative(4);
448 this->annotation.emplace(annotation);
450 this->extrema = std::map<VariableType, std::set<CoefficientType>>();
452 for (
auto const& p : transition.gatherVariables()) {
453 (*this->extrema)[p] = {};
455 auto const& derivative = transition.derivative(p);
457 if (derivative.isConstant()) {
462 auto nominatorAsUnivariate = derivative.nominator().toUnivariatePolynomial();
464 nominatorAsUnivariate /= derivative.denominator().coefficient();
467 std::optional<std::set<CoefficientType>> zeroes;
469 if (derivative.nominator().totalDegree() < 4) {
470 zeroes = cubicEquationZeroes(
RawPolynomial(derivative.nominator()), p);
472 zeroes = zeroesSMT(derivative, p);
475 for (
auto const& zero : *zeroes) {
476 if (zero >= utility::zero<CoefficientType>() && zero <= utility::one<CoefficientType>()) {
477 this->extrema->at(p).emplace(zero);
484template<
typename ParametricType,
typename ConstantType>
485std::optional<std::map<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType,
486 std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>>>
const&
488 return this->extrema;
491template<
typename ParametricType,
typename ConstantType>
493 return this->annotation;
496template<
typename ParametricType,
typename ConstantType>
498 std::size_t seed = 0;
499 carl::hash_add(seed, transition);
503template<
typename ParametricType,
typename ConstantType>
506 if (!collectedValuations.count(valuation)) {
508 this->regionsAndBounds.emplace(valuation, std::vector<std::pair<Interval, Interval>>());
512 auto insertionRes = collectedValuations.insert(std::pair<RobustAbstractValuation, Interval>(std::move(valuation),
storm::Interval(0, 1)));
513 return insertionRes.first->second;
518 for (
auto const& [poly, roots] : extremaAnnotations) {
519 std::set<double> potentialExtrema = {input.lower(), input.upper()};
520 for (
auto const& root : roots) {
521 if (root >= input.lower() && root <= input.upper()) {
522 potentialExtrema.emplace(root);
526 double minValue = utility::infinity<double>();
527 double maxValue = -utility::infinity<double>();
529 for (
auto const& potentialExtremum : potentialExtrema) {
530 auto value = utility::convertNumber<double>(poly.evaluate(utility::convertNumber<RationalFunctionCoefficient>(potentialExtremum)));
531 if (value > maxValue) {
534 if (value < minValue) {
538 sumOfTerms +=
Interval(minValue, maxValue);
543template<
typename ParametricType,
typename ConstantType>
544bool RobustParameterLifter<ParametricType, ConstantType>::FunctionValuationCollector::evaluateCollectedFunctions(
546 std::unordered_map<RobustAbstractValuation, Interval, RobustAbstractValuationHash> insertThese;
547 for (
auto& [abstrValuation, placeholder] : collectedValuations) {
549 ConstantType lowerBound = utility::infinity<ConstantType>();
550 ConstantType upperBound = -utility::infinity<ConstantType>();
552 if (abstrValuation.getExtrema()) {
559 auto const& maybeAnnotation = abstrValuation.getAnnotation();
561 if (maybeAnnotation) {
563 auto p = maybeAnnotation->getParameter();
567 std::set<CoefficientType> potentialExtrema = {lowerP, upperP};
568 for (
auto const& maximum : abstrValuation.getExtrema()->at(p)) {
569 if (maximum >= lowerP && maximum <= upperP) {
570 potentialExtrema.emplace(maximum);
574 for (
auto const& potentialExtremum : potentialExtrema) {
576 auto value = maybeAnnotation->evaluate(utility::convertNumber<double>(potentialExtremum));
577 if (value > upperBound) {
580 if (value < lowerBound) {
589 std::map<VariableType, CoefficientType> lowerPositions;
590 std::map<VariableType, CoefficientType> upperPositions;
596 std::set<CoefficientType> potentialExtrema = {lowerP, upperP};
597 for (
auto const& maximum : abstrValuation.getExtrema()->at(p)) {
598 if (maximum >= lowerP && maximum <= upperP) {
599 potentialExtrema.emplace(maximum);
608 auto instantiation = std::map<VariableType, CoefficientType>(region.
getLowerBoundaries());
610 for (
auto const& potentialExtremum : potentialExtrema) {
612 instantiation[p] = potentialExtremum;
613 auto value = abstrValuation.getTransition().evaluate(instantiation);
614 if (value > maxValue) {
616 maxPosP = potentialExtremum;
618 if (value < minValue) {
620 minPosP = potentialExtremum;
624 lowerPositions[p] = minPosP;
625 upperPositions[p] = maxPosP;
629 lowerBound = utility::convertNumber<ConstantType>(abstrValuation.getTransition().evaluate(lowerPositions));
630 upperBound = utility::convertNumber<ConstantType>(abstrValuation.getTransition().evaluate(upperPositions));
633 if (upperBound < utility::zero<ConstantType>() || lowerBound > utility::one<ConstantType>()) {
638 STORM_LOG_ASSERT(abstrValuation.getAnnotation(),
"Needs to have annotation if no zeroes");
639 auto& regionsAndBounds = this->regionsAndBounds.at(abstrValuation);
640 auto const& annotation = *abstrValuation.getAnnotation();
648 std::vector<uint64_t> regionsInPLARegion;
649 for (uint64_t i = 0;
i < regionsAndBounds.size();
i++) {
650 auto const& [region, bound] = regionsAndBounds[
i];
652 i == 0 ?
true : (!(region.upper() < regionsAndBounds[
i - 1].first.lower() || region.lower() > regionsAndBounds[
i - 1].first.upper())),
653 "regions next to each other need to intersect");
654 if (region.upper() <= plaRegion.lower() || region.lower() >= plaRegion.upper()) {
655 if (regionsInPLARegion.empty()) {
664 regionsInPLARegion.push_back(i);
668 uint64_t regionsRefine = std::max((uint64_t)10, annotation.maxDegree());
669 refine = regionsInPLARegion.size() < regionsRefine;
672 std::vector<Interval> newIntervals;
673 auto diameter = plaRegion.diameter();
675 if (regionsAndBounds.empty()) {
676 regionsAndBounds.emplace_back(plaRegion,
Interval(0, 1));
677 regionsInPLARegion.push_back(0);
680 if (regionsAndBounds[regionsInPLARegion.front()].first.lower() < plaRegion.lower()) {
681 newIntervals.push_back(
Interval(regionsAndBounds[regionsInPLARegion.front()].first.lower(), plaRegion.lower()));
684 for (uint64_t i = 0;
i < regionsRefine;
i++) {
685 newIntervals.push_back(
Interval(plaRegion.lower() + ((
double)i / (
double)regionsRefine) * diameter,
686 plaRegion.lower() + ((
double)(i + 1) / (
double)regionsRefine) * diameter));
689 if (regionsAndBounds[regionsInPLARegion.back()].first.upper() > plaRegion.upper()) {
690 newIntervals.push_back(
Interval(plaRegion.upper(), regionsAndBounds[regionsInPLARegion.back()].first.upper()));
693 std::vector<std::pair<Interval, Interval>> regionsAndBoundsAfter;
694 for (uint64_t i = regionsInPLARegion.back() + 1;
i < regionsAndBounds.size();
i++) {
695 regionsAndBoundsAfter.push_back(regionsAndBounds[i]);
698 regionsAndBounds.erase(regionsAndBounds.begin() + *regionsInPLARegion.begin(), regionsAndBounds.end());
701 for (
auto const& region : newIntervals) {
702 regionsAndBounds.emplace_back(region, annotation.evaluateOnIntervalMidpointTheorem(region));
705 for (
auto const& item : regionsAndBoundsAfter) {
706 regionsAndBounds.emplace_back(item);
716 const ConstantType epsilon = 0;
723 placeholder =
Interval(lowerBound, upperBound);
725 for (
auto& key : insertThese) {
726 this->collectedValuations.insert(std::move(insertThese.extract(key.first)));
731template class RobustParameterLifter<storm::RationalFunction, double>;
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.
void addNextValue(index_type row, index_type column, value_type const &value)
Sets the matrix entry at the given row and column to the given value.
SparseMatrix< value_type > build(index_type overriddenRowCount=0, index_type overriddenColumnCount=0, index_type overriddenRowGroupCount=0)
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)
std::set< storm::RationalFunctionVariable > getParameters(DFT< storm::RationalFunction > const &dft)
Get all rate/probability parameters occurring in the DFT.
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)
bool isConstant(ValueType const &)
ValueType min(ValueType const &first, ValueType const &second)
bool isZero(ValueType const &a)
ValueType sqrt(ValueType const &number)
carl::Interval< double > Interval
Interval type.
carl::MultivariatePolynomial< RationalFunctionCoefficient > RawPolynomial