Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ConstraintCollector.cpp
Go to the documentation of this file.
2
11
13
14namespace {
15
19template<typename PolynomialType>
20storm::expressions::Expression toExpression(PolynomialType const& polynomial, std::shared_ptr<storm::expressions::ExpressionManager> const& manager) {
21 return storm::expressions::polynomialToExpression(polynomial, manager);
22}
23
24} // namespace
25
27 : expressionManager(std::make_shared<storm::expressions::ExpressionManager>()) {
28 process(model);
29}
30
32 return this->wellformedConstraintSet;
33}
34
36 return this->graphPreservingConstraintSet;
37}
38
39std::set<storm::RationalFunctionVariable> const& ConstraintCollector::getVariables() const {
40 return this->variableSet;
41}
42
43storm::expressions::Expression ConstraintCollector::relateToZero(storm::RawPolynomial const& polynomial, storm::expressions::RelationType relation) const {
44 auto zero = expressionManager->rational(storm::utility::zero<storm::RationalNumber>());
45 switch (relation) {
47 return toExpression(polynomial, expressionManager) < zero;
49 return toExpression(polynomial, expressionManager) <= zero;
51 return toExpression(-polynomial, expressionManager) < zero;
53 return toExpression(-polynomial, expressionManager) <= zero;
55 return toExpression(polynomial, expressionManager) == zero;
57 return toExpression(polynomial, expressionManager) != zero;
58 }
59 STORM_LOG_THROW_UNCONDITIONALLY(storm::exceptions::UnexpectedException, "Unhandled relation.");
60}
61
62bool ConstraintCollector::addConstraint(ConstraintSet& constraintSet, ConstraintIndex& constraintIndex, storm::expressions::Expression const& constraint) {
63 auto& candidates = constraintIndex[hashVisitor.hash(constraint)];
64 for (auto const& candidate : candidates) {
65 if (candidate.isSyntacticallyEqual(constraint)) {
66 return false;
67 }
68 }
69 candidates.push_back(constraint);
70 constraintSet.push_back(constraint);
71 return true;
72}
73
74void ConstraintCollector::wellformedRequiresNonNegativeEntries(storm::RationalFunction const& value) {
75 if (storm::utility::isConstant(value)) {
76 // A constant entry does not involve any parameter, so it does not restrict the parameter region. A constant
77 // that violates the bound, however, makes the well-formedness constraints unsatisfiable, which we express
78 // by a trivially false constraint.
80 addConstraint(wellformedConstraintSet, wellformedConstraintIndex, expressionManager->boolean(false));
81 }
82 return;
83 }
84
85 auto valueVariables = value.gatherVariables();
86 variableSet.insert(valueVariables.begin(), valueVariables.end());
87
88 storm::RawPolynomial nominator = value.nominator().polynomialWithCoefficient();
89 storm::RawPolynomial denominator = value.denominator().polynomialWithCoefficient();
90
91 if (denominator.isConstant()) {
92 // The sign of the denominator is known, so non-negativity of the quotient reduces to a sign constraint on
93 // the nominator.
94 STORM_LOG_ASSERT(denominator.constantPart() != 0, "Denominator should not be zero.");
96 addConstraint(wellformedConstraintSet, wellformedConstraintIndex, relateToZero(nominator, relation));
97 } else {
98 // The denominator may change its sign, so we need to constrain that it never vanishes and that the sign of
99 // the nominator follows the sign of the denominator.
100 addConstraint(wellformedConstraintSet, wellformedConstraintIndex, relateToZero(denominator, storm::expressions::RelationType::NotEqual));
101 addConstraint(wellformedConstraintSet, wellformedConstraintIndex,
104 relateToZero(nominator, storm::expressions::RelationType::LessOrEqual)));
105 }
106}
107
108void ConstraintCollector::wellformedRequiresAtMostOne(storm::RationalFunction const& value) {
109 if (storm::utility::isConstant(value)) {
111 addConstraint(wellformedConstraintSet, wellformedConstraintIndex, expressionManager->boolean(false));
112 }
113 return;
114 }
115
116 auto denominator = value.denominator().polynomialWithCoefficient();
117 if (!denominator.isConstant()) {
118 // TODO: Assert: value <= 1 <==> if denom > 0 then nom - denom <= 0 else nom - denom >= 0
119 // This sign reasoning cannot be expressed with the relations available in the expression system.
120 return;
121 }
122
123 STORM_LOG_ASSERT(denominator.constantPart() != 0, "Denominator should not be zero.");
124 storm::RawPolynomial nominator = value.nominator().polynomialWithCoefficient();
125 // Express the bound as a relation of the difference to zero, so that it coincides with another constraint
126 // whenever the two are equivalent.
128 addConstraint(wellformedConstraintSet, wellformedConstraintIndex, relateToZero(nominator - denominator, relation));
129}
130
131void ConstraintCollector::graphPreservingRequiresNonZero(storm::RationalFunction const& value) {
132 addConstraint(graphPreservingConstraintSet, graphPreservingConstraintIndex,
133 relateToZero(value.nominator().polynomialWithCoefficient(), storm::expressions::RelationType::NotEqual));
134}
135
136void ConstraintCollector::process(storm::models::sparse::Model<storm::RationalFunction> const& model) {
137 bool const isCtmc = model.getType() == storm::models::ModelType::Ctmc;
138
139 if (!isCtmc) {
140 for (uint_fast64_t action = 0; action < model.getTransitionMatrix().getRowCount(); ++action) {
142
143 for (auto transitionIt = model.getTransitionMatrix().begin(action); transitionIt != model.getTransitionMatrix().end(action); ++transitionIt) {
144 auto const& value = transitionIt->getValue();
145 sum += value;
146 // Assert: 0 <= transition <= 1
147 wellformedRequiresNonNegativeEntries(value);
148 wellformedRequiresAtMostOne(value);
149 if (!storm::utility::isConstant(value)) {
150 // Assert: transition > 0, so that the underlying graph does not change.
151 graphPreservingRequiresNonZero(value);
152 }
153 }
154 STORM_LOG_ASSERT(!storm::utility::isConstant(sum) || storm::utility::isOne(sum), "If the sum is a constant, it must be equal to 1.");
155 if (!storm::utility::isConstant(sum)) {
156 auto sumVariables = sum.gatherVariables();
157 variableSet.insert(sumVariables.begin(), sumVariables.end());
158 // Assert: sum == 1
159 addConstraint(wellformedConstraintSet, wellformedConstraintIndex,
160 relateToZero(sum.nominator().polynomialWithCoefficient() - sum.denominator().polynomialWithCoefficient(),
162 }
163 }
164 } else {
165 for (auto const& transition : model.getTransitionMatrix()) {
166 // Assert: 0 <= transition
167 wellformedRequiresNonNegativeEntries(transition.getValue());
168 if (!storm::utility::isConstant(transition.getValue())) {
169 // Assert: transition > 0, so that the underlying graph does not change.
170 graphPreservingRequiresNonZero(transition.getValue());
171 }
172 }
173 }
174
175 if (isCtmc) {
176 auto const& exitRateVector = static_cast<storm::models::sparse::Ctmc<storm::RationalFunction> const&>(model).getExitRateVector();
177 for (auto const& exitRate : exitRateVector) {
178 wellformedRequiresNonNegativeEntries(exitRate);
179 }
181 auto const& exitRateVector = static_cast<storm::models::sparse::MarkovAutomaton<storm::RationalFunction> const&>(model).getExitRates();
182 for (auto const& exitRate : exitRateVector) {
183 wellformedRequiresNonNegativeEntries(exitRate);
184 }
185 }
186
187 for (auto const& rewModelEntry : model.getRewardModels()) {
188 if (rewModelEntry.second.hasStateRewards()) {
189 for (auto const& stateReward : rewModelEntry.second.getStateRewardVector()) {
190 wellformedRequiresNonNegativeEntries(stateReward);
191 }
192 }
193 if (rewModelEntry.second.hasStateActionRewards()) {
194 for (auto const& stateActionReward : rewModelEntry.second.getStateActionRewardVector()) {
195 wellformedRequiresNonNegativeEntries(stateActionReward);
196 }
197 }
198 if (rewModelEntry.second.hasTransitionRewards()) {
199 for (auto const& entry : rewModelEntry.second.getTransitionRewardMatrix()) {
200 wellformedRequiresNonNegativeEntries(entry.getValue());
201 }
202 }
203 }
204}
205
209
210} // namespace storm::pars::analysis
void gatherVariables(std::set< storm::expressions::Variable > &variables) const
Retrieves the set of all variables that appear in the expression.
std::size_t hash(storm::expressions::Expression const &expression)
Computes the hash of the given expression.
virtual ModelType getType() const
Return the actual type of the model.
Definition ModelBase.cpp:7
Base class for all sparse models.
Definition Model.h:34
storm::storage::SparseMatrix< ValueType > const & getTransitionMatrix() const
Retrieves the matrix representing the transitions of the model.
Definition Model.cpp:226
std::unordered_map< std::string, RewardModelType > const & getRewardModels() const
Retrieves the reward models.
Definition Model.cpp:728
std::vector< storm::expressions::Expression > ConstraintSet
The type of the collected constraints.
ConstraintCollector(storm::models::sparse::Model< storm::RationalFunction > const &model)
Constructs a constraint collector for the given model.
ConstraintSet const & getGraphPreservingConstraints() const
Returns the set of graph-preserving constraints.
std::set< storm::RationalFunctionVariable > const & getVariables() const
Returns the set of variables in the model.
ConstraintSet const & getWellformedConstraints() const
Returns the set of wellformed-ness constraints.
void operator()(storm::models::sparse::Model< storm::RationalFunction > const &model)
Constructs the constraints for the given model by calling the process method.
#define STORM_LOG_THROW_UNCONDITIONALLY(exception, message)
Definition macros.h:48
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
Expression ite(Expression const &condition, Expression const &thenExpression, Expression const &elseExpression)
Expression sum(std::vector< storm::expressions::Expression > const &expressions)
RelationType
An enum type specifying the different relations applicable.
Expression polynomialToExpression(PolynomialType const &polynomial, std::shared_ptr< ExpressionManager > const &manager)
Converts the given polynomial into a Storm expression.
bool isOne(ValueType const &a)
Definition constants.cpp:39
NumberTraits< RationalType >::IntegerType denominator(RationalType const &number)
bool isConstant(ValueType const &)
ValueType zero()
Definition constants.cpp:26
ValueType one()
Definition constants.cpp:21
The contract of a DD backend.
carl::RationalFunction< Polynomial, true > RationalFunction
carl::MultivariatePolynomial< RationalFunctionCoefficient > RawPolynomial
void process()