3#include <carl/core/rootfinder/RootFinder.h>
29namespace transformer {
33template<
typename ParametricType,
typename ConstantType>
35 std::vector<ParametricType>
const& pVector,
38 bool useMonotonicity) {
39 oldToNewColumnIndexMapping = std::vector<uint64_t>(selectedColumns.
size(), selectedColumns.
size());
40 uint64_t newIndexColumns = 0;
41 for (
auto const& oldColumn : selectedColumns) {
42 oldToNewColumnIndexMapping[oldColumn] = newIndexColumns++;
45 oldToNewRowIndexMapping = std::vector<uint64_t>(selectedRows.
size(), selectedRows.
size());
46 uint64_t newIndexRows = 0;
47 for (
auto const& oldRow : selectedRows) {
48 oldToNewRowIndexMapping[oldRow] = newIndexRows++;
55 uint64_t pMatrixEntryCount = 0;
56 uint64_t pVectorEntryCount = 0;
61 this->occurringVariablesAtState.resize(pMatrix.
getRowCount());
63 for (uint64_t row = 0; row < pMatrix.
getRowCount(); row++) {
64 if (!selectedRows.
get(row)) {
67 std::set<VariableType> occurringVariables;
68 for (
auto const& entry : pMatrix.
getRow(row)) {
69 auto column = entry.getColumn();
70 if (!selectedColumns.
get(column)) {
74 auto transition = entry.getValue();
76 auto variables = transition.gatherVariables();
77 occurringVariables.insert(variables.begin(), variables.end());
80 builder.
addNextValue(oldToNewColumnIndexMapping[row], oldToNewColumnIndexMapping[column], utility::convertNumber<double>(transition));
82 nonConstMatrixEntries.set(pMatrixEntryCount,
true);
84 builder.
addNextValue(oldToNewColumnIndexMapping[row], oldToNewColumnIndexMapping[column],
Interval());
85 Interval& placeholder = functionValuationCollector.add(valuation);
93 for (
auto& var : occurringVariables) {
94 occuringStatesAtVariable[var].insert(row);
96 occurringVariablesAtState[row] = std::move(occurringVariables);
99 for (uint64_t i = 0; i < pVector.size(); i++) {
100 auto const transition = pVector[i];
101 if (!selectedRows.
get(i)) {
105 vector.push_back(utility::convertNumber<double>(transition));
107 nonConstVectorEntries.set(pVectorEntryCount,
true);
110 Interval& placeholder = functionValuationCollector.add(valuation);
111 vectorAssignment.push_back(std::pair<
typename std::vector<Interval>::iterator,
Interval&>(
typename std::vector<Interval>::iterator(), placeholder));
112 for (
auto const& var : valuation.getParameters()) {
113 occuringStatesAtVariable[var].insert(i);
114 occurringVariablesAtState[i].emplace(var);
120 matrix = builder.
build();
121 vector.shrink_to_fit();
122 matrixAssignment.shrink_to_fit();
123 vectorAssignment.shrink_to_fit();
124 nonConstMatrixEntries.resize(pMatrixEntryCount);
127 auto matrixAssignmentIt = matrixAssignment.begin();
128 uint64_t startEntryOfRow = 0;
129 for (uint64_t group = 0; group < matrix.getRowGroupCount(); ++group) {
130 uint64_t startEntryOfNextRow = startEntryOfRow + matrix.getRow(group, 0).getNumberOfEntries();
131 for (uint64_t matrixRow = matrix.getRowGroupIndices()[group]; matrixRow < matrix.getRowGroupIndices()[group + 1]; ++matrixRow) {
132 auto matrixEntryIt = matrix.getRow(matrixRow).begin();
133 for (uint64_t nonConstEntryIndex = nonConstMatrixEntries.getNextSetIndex(startEntryOfRow); nonConstEntryIndex < startEntryOfNextRow;
134 nonConstEntryIndex = nonConstMatrixEntries.getNextSetIndex(nonConstEntryIndex + 1)) {
135 matrixAssignmentIt->first = matrixEntryIt + (nonConstEntryIndex - startEntryOfRow);
136 ++matrixAssignmentIt;
139 startEntryOfRow = startEntryOfNextRow;
141 STORM_LOG_ASSERT(matrixAssignmentIt == matrixAssignment.end(),
"Unexpected number of entries in the matrix assignment.");
143 auto vectorAssignmentIt = vectorAssignment.begin();
144 for (
auto const& nonConstVectorEntry : nonConstVectorEntries) {
145 for (uint64_t vectorIndex = matrix.getRowGroupIndices()[nonConstVectorEntry]; vectorIndex != matrix.getRowGroupIndices()[nonConstVectorEntry + 1];
147 vectorAssignmentIt->first = vector.begin() + vectorIndex;
148 ++vectorAssignmentIt;
151 STORM_LOG_ASSERT(vectorAssignmentIt == vectorAssignment.end(),
"Unexpected number of entries in the vector assignment.");
154template<
typename ParametricType,
typename ConstantType>
158 this->currentRegionAllIllDefined = functionValuationCollector.evaluateCollectedFunctions(region, dirForParameters);
163 for (
auto& assignment : matrixAssignment) {
164 assignment.first->setValue(assignment.second);
167 for (
auto& assignment : vectorAssignment) {
168 *assignment.first = assignment.second;
172template<
typename ParametricType,
typename ConstantType>
173const std::vector<std::set<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType>>&
175 return occurringVariablesAtState;
178template<
typename ParametricType,
typename ConstantType>
179std::map<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType, std::set<uint_fast64_t>>
const&
181 return occuringStatesAtVariable;
184template<
typename ParametricType,
typename ConstantType>
185std::optional<std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>>
188 std::shared_ptr<storm::expressions::ExpressionManager> expressionManager = std::make_shared<storm::expressions::ExpressionManager>();
191 auto smtSolver = factory.
create(*expressionManager);
195 auto expression = rfte.toExpression(function) == expressionManager->rational(0);
197 auto variables = expressionManager->getVariables();
200 for (
auto const& var : variables) {
201 exprBounds = exprBounds && expressionManager->rational(0) <= var && var <= expressionManager->rational(1);
204 smtSolver->setTimeout(50);
206 smtSolver->add(exprBounds);
207 smtSolver->add(expression);
209 std::set<CoefficientType> zeroes = {};
212 auto checkResult = smtSolver->check();
215 auto model = smtSolver->getModel();
218 if (variables.size() != 1) {
221 auto const var = *variables.begin();
223 double value = model->getRationalValue(var);
225 zeroes.emplace(utility::convertNumber<CoefficientType>(value));
230 smtSolver->addNotCurrentModel();
242template<
typename ParametricType,
typename ConstantType>
243std::optional<std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>>
244RobustParameterLifter<ParametricType, ConstantType>::RobustAbstractValuation::zeroesCarl(
247 auto const& carlRoots = carl::rootfinder::realRoots<CoefficientType, CoefficientType>(
249 carl::rootfinder::SplittingStrategy::ABERTH);
250 std::set<CoefficientType> zeroes = {};
251 for (carl::RealAlgebraicNumber<CoefficientType>
const& root : carlRoots) {
253 if (root.isNumeric()) {
258 zeroes.emplace(rootCoefficient);
263template<
typename ParametricType,
typename ConstantType>
264std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>
265RobustParameterLifter<ParametricType, ConstantType>::RobustAbstractValuation::cubicEquationZeroes(
267 if (polynomial.isConstant()) {
270 STORM_LOG_ERROR_COND(polynomial.gatherVariables().size() == 1,
"Multi-variate polynomials currently not supported");
274 CoefficientType a = utility::zero<CoefficientType>(), b = a, c = a, d = a;
275 utility::convertNumber<ConstantType>(a);
276 for (
auto const& term : polynomial.getTerms()) {
277 STORM_LOG_ASSERT(term.getNrVariables() <= 1,
"No terms with more than one variable allowed but " << term <<
" has " << term.getNrVariables());
278 if (!term.isConstant() && term.getSingleVariable() != parameter) {
282 STORM_LOG_ASSERT(term.tdeg() < 4,
"Transitions are only allowed to have a maximum degree of four.");
283 switch (term.tdeg()) {
318 return {-b / (2 * a)};
324 std::set<CoefficientType> roots;
328 CoefficientType q = (2 * b * b * b - 9 * a * b * c + 27 * a * a * d) / (27 * a * a * a);
329 double pDouble = utility::convertNumber<ConstantType>(p);
330 double qDouble = utility::convertNumber<ConstantType>(q);
333 roots = {utility::convertNumber<CoefficientType>(std::cbrt(-qDouble))};
337 roots.emplace(utility::convertNumber<CoefficientType>(
utility::sqrt(-pDouble)));
338 roots.emplace(utility::convertNumber<CoefficientType>(-
utility::sqrt(-pDouble)));
345 roots = {-3 * q / (p * 2), 3 * q / p};
347 double Ddouble = utility::convertNumber<ConstantType>(D);
349 roots = {u - p / (3 * u)};
352 double t = std::acos(3 * qDouble / pDouble / u) / 3;
353 double k = 2 * M_PI / 3;
355 roots = {utility::convertNumber<CoefficientType>(u * std::cos(t)), utility::convertNumber<CoefficientType>(u * std::cos(t - k)),
356 utility::convertNumber<CoefficientType>(u * std::cos(t - 2 * k))};
363template<
typename ParametricType,
typename ConstantType>
365 : transition(transition) {
366 STORM_LOG_ERROR_COND(transition.denominator().isConstant(),
"Robust PLA only supports transitions with constant denominators.");
367 transition.simplify();
368 std::set<VariableType> occurringVariables;
370 for (
auto const& var : occurringVariables) {
371 parameters.emplace(var);
375template<
typename ParametricType,
typename ConstantType>
380template<
typename ParametricType,
typename ConstantType>
385template<
typename ParametricType,
typename ConstantType>
387 return currentRegionAllIllDefined;
390template<
typename ParametricType,
typename ConstantType>
392 return this->transition == other.transition;
395template<
typename ParametricType,
typename ConstantType>
396std::set<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType>
const&
401template<
typename ParametricType,
typename ConstantType>
403 return this->transition;
406template<
typename ParametricType,
typename ConstantType>
409 if (this->extrema || this->annotation) {
417 auto const& terms = annotation.getTerms();
422 std::optional<std::set<CoefficientType>> carlResult;
424 if (terms.size() < 5) {
425 carlResult = zeroesCarl(annotation.getProbability().derivative(), annotation.getParameter());
430 this->extrema = std::map<VariableType, std::set<CoefficientType>>();
431 (*this->extrema)[annotation.getParameter()];
432 for (
auto const& root : *carlResult) {
433 (*this->extrema).at(annotation.getParameter()).emplace(utility::convertNumber<CoefficientType>(root));
437 annotation.computeDerivative(4);
439 this->annotation.emplace(annotation);
441 this->extrema = std::map<VariableType, std::set<CoefficientType>>();
443 for (
auto const& p : transition.gatherVariables()) {
444 (*this->extrema)[p] = {};
446 auto const& derivative = transition.derivative(p);
448 if (derivative.isConstant()) {
453 auto nominatorAsUnivariate = derivative.nominator().toUnivariatePolynomial();
455 nominatorAsUnivariate /= derivative.denominator().coefficient();
458 std::optional<std::set<CoefficientType>> zeroes;
460 if (derivative.nominator().totalDegree() < 4) {
461 zeroes = cubicEquationZeroes(
RawPolynomial(derivative.nominator()), p);
463 zeroes = zeroesSMT(derivative, p);
466 for (
auto const& zero : *zeroes) {
467 if (zero >= utility::zero<CoefficientType>() && zero <= utility::one<CoefficientType>()) {
468 this->extrema->at(p).emplace(zero);
475template<
typename ParametricType,
typename ConstantType>
476std::optional<std::map<typename RobustParameterLifter<ParametricType, ConstantType>::VariableType,
477 std::set<typename storm::utility::parametric::CoefficientType<ParametricType>::type>>>
const&
479 return this->extrema;
482template<
typename ParametricType,
typename ConstantType>
484 return this->annotation;
487template<
typename ParametricType,
typename ConstantType>
489 std::size_t seed = 0;
490 carl::hash_add(seed, transition);
494template<
typename ParametricType,
typename ConstantType>
497 if (!collectedValuations.count(valuation)) {
499 this->regionsAndBounds.emplace(valuation, std::vector<std::pair<Interval, Interval>>());
503 auto insertionRes = collectedValuations.insert(std::pair<RobustAbstractValuation, Interval>(std::move(valuation),
storm::Interval(0, 1)));
504 return insertionRes.first->second;
509 for (
auto const& [poly, roots] : extremaAnnotations) {
510 std::set<double> potentialExtrema = {input.lower(), input.upper()};
511 for (
auto const& root : roots) {
512 if (root >= input.lower() && root <= input.upper()) {
513 potentialExtrema.emplace(root);
517 double minValue = utility::infinity<double>();
518 double maxValue = -utility::infinity<double>();
520 for (
auto const& potentialExtremum : potentialExtrema) {
521 auto value = utility::convertNumber<double>(poly.evaluate(utility::convertNumber<RationalFunctionCoefficient>(potentialExtremum)));
522 if (value > maxValue) {
525 if (value < minValue) {
529 sumOfTerms +=
Interval(minValue, maxValue);
534template<
typename ParametricType,
typename ConstantType>
535bool RobustParameterLifter<ParametricType, ConstantType>::FunctionValuationCollector::evaluateCollectedFunctions(
537 std::unordered_map<RobustAbstractValuation, Interval, RobustAbstractValuationHash> insertThese;
538 for (
auto& [abstrValuation, placeholder] : collectedValuations) {
540 ConstantType lowerBound = utility::infinity<ConstantType>();
541 ConstantType upperBound = -utility::infinity<ConstantType>();
543 if (abstrValuation.getExtrema()) {
550 auto const& maybeAnnotation = abstrValuation.getAnnotation();
552 if (maybeAnnotation) {
554 auto p = maybeAnnotation->getParameter();
558 std::set<CoefficientType> potentialExtrema = {lowerP, upperP};
559 for (
auto const& maximum : abstrValuation.getExtrema()->at(p)) {
560 if (maximum >= lowerP && maximum <= upperP) {
561 potentialExtrema.emplace(maximum);
565 for (
auto const& potentialExtremum : potentialExtrema) {
567 auto value = maybeAnnotation->evaluate(utility::convertNumber<double>(potentialExtremum));
568 if (value > upperBound) {
571 if (value < lowerBound) {
580 std::map<VariableType, CoefficientType> lowerPositions;
581 std::map<VariableType, CoefficientType> upperPositions;
587 std::set<CoefficientType> potentialExtrema = {lowerP, upperP};
588 for (
auto const& maximum : abstrValuation.getExtrema()->at(p)) {
589 if (maximum >= lowerP && maximum <= upperP) {
590 potentialExtrema.emplace(maximum);
599 auto instantiation = std::map<VariableType, CoefficientType>(region.
getLowerBoundaries());
601 for (
auto const& potentialExtremum : potentialExtrema) {
603 instantiation[p] = potentialExtremum;
604 auto value = abstrValuation.getTransition().evaluate(instantiation);
605 if (value > maxValue) {
607 maxPosP = potentialExtremum;
609 if (value < minValue) {
611 minPosP = potentialExtremum;
615 lowerPositions[p] = minPosP;
616 upperPositions[p] = maxPosP;
620 lowerBound = utility::convertNumber<ConstantType>(abstrValuation.getTransition().evaluate(lowerPositions));
621 upperBound = utility::convertNumber<ConstantType>(abstrValuation.getTransition().evaluate(upperPositions));
624 if (upperBound < utility::zero<ConstantType>() || lowerBound > utility::one<ConstantType>()) {
629 STORM_LOG_ASSERT(abstrValuation.getAnnotation(),
"Needs to have annotation if no zeroes");
630 auto& regionsAndBounds = this->regionsAndBounds.at(abstrValuation);
631 auto const& annotation = *abstrValuation.getAnnotation();
639 std::vector<uint64_t> regionsInPLARegion;
640 for (uint64_t i = 0;
i < regionsAndBounds.size();
i++) {
641 auto const& [region, bound] = regionsAndBounds[
i];
643 i == 0 ?
true : (!(region.upper() < regionsAndBounds[
i - 1].first.lower() || region.lower() > regionsAndBounds[
i - 1].first.upper())),
644 "regions next to each other need to intersect");
645 if (region.upper() <= plaRegion.lower() || region.lower() >= plaRegion.upper()) {
646 if (regionsInPLARegion.empty()) {
655 regionsInPLARegion.push_back(i);
659 uint64_t regionsRefine = std::max((uint64_t)10, annotation.maxDegree());
660 refine = regionsInPLARegion.size() < regionsRefine;
663 std::vector<Interval> newIntervals;
664 auto diameter = plaRegion.diameter();
666 if (regionsAndBounds.empty()) {
667 regionsAndBounds.emplace_back(plaRegion,
Interval(0, 1));
668 regionsInPLARegion.push_back(0);
671 if (regionsAndBounds[regionsInPLARegion.front()].first.lower() < plaRegion.lower()) {
672 newIntervals.push_back(
Interval(regionsAndBounds[regionsInPLARegion.front()].first.lower(), plaRegion.lower()));
675 for (uint64_t i = 0;
i < regionsRefine;
i++) {
676 newIntervals.push_back(
Interval(plaRegion.lower() + ((
double)i / (
double)regionsRefine) * diameter,
677 plaRegion.lower() + ((
double)(i + 1) / (
double)regionsRefine) * diameter));
680 if (regionsAndBounds[regionsInPLARegion.back()].first.upper() > plaRegion.upper()) {
681 newIntervals.push_back(
Interval(plaRegion.upper(), regionsAndBounds[regionsInPLARegion.back()].first.upper()));
684 std::vector<std::pair<Interval, Interval>> regionsAndBoundsAfter;
685 for (uint64_t i = regionsInPLARegion.back() + 1;
i < regionsAndBounds.size();
i++) {
686 regionsAndBoundsAfter.push_back(regionsAndBounds[i]);
689 regionsAndBounds.erase(regionsAndBounds.begin() + *regionsInPLARegion.begin(), regionsAndBounds.end());
692 for (
auto const& region : newIntervals) {
693 regionsAndBounds.emplace_back(region, annotation.evaluateOnIntervalMidpointTheorem(region));
696 for (
auto const& item : regionsAndBoundsAfter) {
697 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)));
722template 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