Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
MonotonicityChecker.cpp
Go to the documentation of this file.
2
6
7namespace storm {
8namespace analysis {
9template<typename ValueType>
13
14template<typename ValueType>
16 std::shared_ptr<Order> const& order, uint_fast64_t state, VariableType const& var, storage::ParameterRegion<ValueType> const& region) {
17 // Create + fill Vector containing the Monotonicity of the transitions to the succs
18 auto row = matrix.getRow(state);
19 // Ignore if all entries are constant
20 bool ignore = true;
21
22 std::vector<uint_fast64_t> succs;
23 std::vector<Monotonicity> succsMonUnsorted;
24 std::vector<uint_fast64_t> statesIncr;
25 std::vector<uint_fast64_t> statesDecr;
26 bool checkAllow = true;
27 for (auto entry : row) {
28 auto succState = entry.getColumn();
29 auto mon = checkTransitionMonRes(entry.getValue(), var, region);
30 succsMonUnsorted.push_back(mon);
31 succs.push_back(succState);
32 ignore &= entry.getValue().isConstant();
33 if (mon == Monotonicity::Incr) {
34 statesIncr.push_back(succState);
35 } else if (mon == Monotonicity::Decr) {
36 statesDecr.push_back(succState);
37 } else if (mon == Monotonicity::Not) {
38 checkAllow = false;
39 }
40 }
41 if (ignore) {
42 return Monotonicity::Constant;
43 }
44 auto succsSorted = order->sortStates(&succs);
45
46 uint_fast64_t succSize = succs.size();
47 if (succsSorted[succSize - 1] == matrix.getColumnCount()) {
48 // Maybe we can still do something
49 // If one is decreasing and all others increasing, and this one is above all others or vice versa
50 if (checkAllow) {
51 if (statesIncr.size() == 1 && statesDecr.size() > 1) {
52 auto comp = order->allAboveBelow(statesDecr, statesIncr.back());
53 if (comp.first) {
54 // All decreasing states are above the increasing state, therefore decreasing
55 return Monotonicity::Decr;
56 } else if (comp.second) {
57 // All decreasing states are below the increasing state, therefore increasing
58 return Monotonicity::Incr;
59 }
60 } else if (statesDecr.size() == 1 && statesIncr.size() > 1) {
61 auto comp = order->allAboveBelow(statesDecr, statesIncr.back());
62 if (comp.first) {
63 // All increasing states are below the decreasing state, therefore increasing
64 return Monotonicity::Incr;
65 } else if (comp.second) {
66 // All increasing states are above the decreasing state, therefore decreasing
67 return Monotonicity::Decr;
68 }
69 }
70 }
71
72 return Monotonicity::Unknown;
73 }
74
75 if (succSize == 2) {
76 // In this case we can ignore the last entry, as this will have a probability of 1 - the other
77 succSize = 1;
78 }
79
80 // First check as long as it stays constant and either incr or decr
81 bool allowedToSwap = true;
82 Monotonicity localMonotonicity = Monotonicity::Constant;
83 uint_fast64_t index = 0;
84 while (index < succSize && localMonotonicity == Monotonicity::Constant) {
85 auto itr = std::find(succs.begin(), succs.end(), succsSorted[index]);
86 auto newIndex = std::distance(succs.begin(), itr);
87 auto transitionMon = succsMonUnsorted[newIndex];
88 localMonotonicity = transitionMon;
89 if (transitionMon == Monotonicity::Not && succSize != 1) {
90 localMonotonicity = Monotonicity::Unknown;
91 }
92 index++;
93 }
94
95 while (index < succSize && localMonotonicity != Monotonicity::Not && localMonotonicity != Monotonicity::Unknown) {
96 // We get here as soon as we have seen incr/decr once
97 auto itr = std::find(succs.begin(), succs.end(), succsSorted[index]);
98 auto newIndex = std::distance(succs.begin(), itr);
99 auto transitionMon = succsMonUnsorted[newIndex];
100
101 if (transitionMon == Monotonicity::Not || transitionMon == Monotonicity::Unknown) {
102 return Monotonicity::Unknown;
103 }
104 if (allowedToSwap) {
105 // So far we have only seen constant and either incr or decr, but not both
106 if (transitionMon != Monotonicity::Constant && transitionMon != localMonotonicity) {
107 allowedToSwap = false;
108 }
109 } else if (!allowedToSwap) {
110 // So we have been at the point where we changed from incr to decr (or decr to incr)
111 if (transitionMon == localMonotonicity || transitionMon == Monotonicity::Not || transitionMon == Monotonicity::Unknown) {
112 localMonotonicity = Monotonicity::Unknown;
113 }
114 }
115 index++;
116 }
117 return localMonotonicity;
118}
119
120template<typename ValueType>
122 if (derivative.isZero()) {
123 return std::pair<bool, bool>(true, true);
124 }
125 if (derivative.isConstant()) {
126 bool monIncr = derivative.constantPart() >= 0;
127 bool monDecr = derivative.constantPart() <= 0;
128 return std::pair<bool, bool>(monIncr, monDecr);
129 }
130 bool monIncr = false;
131 bool monDecr = false;
132
133 std::shared_ptr<expressions::ExpressionManager> manager(new expressions::ExpressionManager());
134 auto s = utility::solver::getSmtSolver(*manager);
135 std::set<VariableType> variables = derivative.gatherVariables();
136
137 expressions::Expression exprBounds = manager->boolean(true);
138 for (auto const& variable : variables) {
139 auto managerVariable = manager->declareRationalVariable(variable.name());
142 exprBounds = exprBounds && manager->rational(lb) <= managerVariable && managerVariable <= manager->rational(ub);
143 }
144
146
147 // < 0, so not monotone increasing. If this is unsat, then it should be monotone increasing.
148 expressions::Expression exprToCheck = converter.toExpression(derivative) < manager->rational(0);
149 s->add(exprBounds);
150 s->add(exprToCheck);
151 monIncr = s->check() == solver::SmtSolver::CheckResult::Unsat;
152
153 // > 0, so not monotone decreasing. If this is unsat it should be monotone decreasing.
154 exprToCheck = converter.toExpression(derivative) > manager->rational(0);
155 s->reset();
156 s->add(exprBounds);
157 s->add(exprToCheck);
158 monDecr = s->check() == solver::SmtSolver::CheckResult::Unsat;
159
160 STORM_LOG_ASSERT(!(monIncr && monDecr), "Error analyzing " << derivative);
161
162 return std::pair<bool, bool>(monIncr, monDecr);
163}
164
165/*** Private methods ***/
166template<typename ValueType>
167typename MonotonicityChecker<ValueType>::Monotonicity MonotonicityChecker<ValueType>::checkTransitionMonRes(
168 ValueType function, typename MonotonicityChecker<ValueType>::VariableType param, typename MonotonicityChecker<ValueType>::Region region) {
169 std::pair<bool, bool> res = MonotonicityChecker<ValueType>::checkDerivative(getDerivative(function, param), region);
170 if (res.first && !res.second) {
171 return Monotonicity::Incr;
172 } else if (!res.first && res.second) {
173 return Monotonicity::Decr;
174 } else if (res.first && res.second) {
175 return Monotonicity::Constant;
176 } else {
177 return Monotonicity::Not;
178 }
179}
180
181template<typename ValueType>
182ValueType& MonotonicityChecker<ValueType>::getDerivative(ValueType function, typename MonotonicityChecker<ValueType>::VariableType var) {
183 auto& derivativeMap = derivatives[function];
184 if (derivativeMap.find(var) == derivativeMap.end()) {
185 derivativeMap[var] = function.derivative(var);
186 }
187 return derivativeMap[var];
188}
189
191} // namespace analysis
192} // namespace storm
MonotonicityChecker(storage::SparseMatrix< ValueType > const &matrix)
Constructs a new MonotonicityChecker object.
utility::parametric::VariableType< ValueType >::type VariableType
storage::ParameterRegion< ValueType > Region
static std::pair< bool, bool > checkDerivative(ValueType const &derivative, storage::ParameterRegion< ValueType > const &reg)
Checks if a derivative >=0 or/and <=0.
MonotonicityResult< VariableType >::Monotonicity Monotonicity
Monotonicity checkLocalMonotonicity(std::shared_ptr< Order > const &order, uint_fast64_t state, VariableType const &var, storage::ParameterRegion< ValueType > const &region)
Checks for local monotonicity at the given state.
This class is responsible for managing a set of typed variables and all expressions using these varia...
CoefficientType const & getLowerBoundary(VariableType const &variable) const
CoefficientType const & getUpperBoundary(VariableType const &variable) const
A class that holds a possibly non-square matrix in the compressed row storage format.
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
SFTBDDChecker::ValueType ValueType
std::unique_ptr< storm::solver::SmtSolver > getSmtSolver(storm::expressions::ExpressionManager &manager)
Creates a new SMT solver instance.
Definition solver.cpp:215
TargetType convertNumber(SourceType const &number)
The contract of a DD backend.