Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ReachabilityProbabilityCertificate.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <sstream>
5
16
19
20namespace storm::modelchecker {
21
22template<typename ValueType>
24 storm::storage::BitVector targetStates, std::string targetLabel)
25 : Certificate<ValueType>(CertificateKind::ReachabilityProbability), targetStates(std::move(targetStates)), targetLabel(targetLabel), dir(dir) {}
26
27template<typename ValueType>
29 storm::storage::BitVector targetStates,
30 storm::storage::BitVector constraintStates, std::string targetLabel,
31 std::string constraintLabel)
33 targetStates(std::move(targetStates)),
34 constraintStates(std::move(constraintStates)),
35 targetLabel(targetLabel),
36 constraintLabel(std::move(constraintLabel)),
37 dir(dir) {
38 STORM_LOG_ASSERT(this->constraintStates->size() == this->targetStates.size(), "Constraint set and target set consider a different state count.");
39}
40
41template<typename ValueType>
43 if (!model.isSparseModel()) {
44 STORM_LOG_WARN("Certificate invalid because the given model is not sparse.");
45 return false;
46 }
48 if (auto const* ctmcPtr = dynamic_cast<storm::models::sparse::Ctmc<ValueType> const*>(&model)) {
49 return checkValidity(ctmcPtr->computeProbabilityMatrix());
50 }
53 if (auto const* sparseModelPtr = dynamic_cast<storm::models::sparse::Model<ValueType> const*>(&model)) {
54 return checkValidity(sparseModelPtr->getTransitionMatrix());
55 }
56 }
57 STORM_LOG_WARN("Certificate invalid because the given model is of unsupported type.");
58 return false;
59}
60
61template<typename ValueType, OptimizationDirection Dir>
62bool checkUpperBoundCertificate(storm::storage::SparseMatrix<ValueType> const& transitionProbabilityMatrix, storm::storage::BitVector const& targetStates,
63 storm::OptionalRef<storm::storage::BitVector const> constraintStates, std::vector<ValueType> const& values) {
64 for (uint64_t state = 0; state < targetStates.size(); ++state) {
65 if (targetStates.get(state)) {
66 // Handle target state
67 if (values[state] < storm::utility::one<ValueType>()) {
68 STORM_LOG_WARN("Certificate invalid because target state " << state << " has upper bound " << values[state] << " < 1.");
69 return false;
70 }
71 continue;
72 }
73 // Probabilities are non-negative, so are upper bounds on probabilities
74 if (values[state] < storm::utility::zero<ValueType>()) {
75 STORM_LOG_WARN("Certificate invalid because state " << state << " has upper bound " << values[state] << " < 0.");
76 return false;
77 }
78 // Handle states that are not in the constraint set
79 if (constraintStates && !constraintStates->get(state)) {
80 continue;
81 }
82 // Apply Bellman operator for non-target state
84 for (auto choice : transitionProbabilityMatrix.getRowGroupIndices(state)) {
85 stateValue &= transitionProbabilityMatrix.multiplyRowWithVector(choice, values);
86 }
87 STORM_LOG_ASSERT(!stateValue.empty(), "Bellman operator failed to compute value for state " << state << " since the row group is empty.");
88 if (*stateValue > values[state]) {
89 double const approxDiff = storm::utility::convertNumber<double, ValueType>(*stateValue - values[state]);
90 STORM_LOG_WARN("Certificate invalid because upper bound is not inductive. At state " << state << " the Bellman operator yields " << *stateValue
91 << " but the certificate has smaller value " << values[state]
92 << ". Approx. diff is " << approxDiff << ".");
93 return false;
94 }
95 }
96 return true;
97}
98
99template<typename ValueType, OptimizationDirection Dir>
100bool checkLowerBoundCertificate(storm::storage::SparseMatrix<ValueType> const& transitionProbabilityMatrix, storm::storage::BitVector const& targetStates,
101 storm::OptionalRef<storm::storage::BitVector const> constraintStates, std::vector<ValueType> const& values,
102 std::vector<RankingType> const& ranks) {
103 for (uint64_t state = 0; state < targetStates.size(); ++state) {
104 // Handle target state
105 if (targetStates.get(state)) {
106 if (values[state] > storm::utility::one<ValueType>()) {
107 STORM_LOG_WARN("Certificate invalid because target state " << state << " has lower bound " << values[state] << " > 1.");
108 return false;
109 }
110 continue;
111 }
112 // Handle states that are not in the constraint set
113 if (constraintStates && !constraintStates->get(state)) {
114 if (values[state] > storm::utility::zero<ValueType>()) {
115 STORM_LOG_WARN("Certificate invalid because state " << state << " is not in the constraint set but has lower bound " << values[state]
116 << " > 0.");
117 return false;
118 }
119 if (ranks[state] != InfRank) {
120 STORM_LOG_WARN("Certificate invalid because state " << state << " is not in the constraint set but has finite rank " << ranks[state] << ".");
121 return false;
122 }
123 continue;
124 }
125 // Check that states with non-zero lower bound have finite rank
126 if (values[state] > storm::utility::zero<ValueType>() && ranks[state] == InfRank) {
127 STORM_LOG_WARN("Certificate invalid because infinite rank is assigned to a state " << state << " with non-zero lower bound " << values[state]
128 << ".");
129 return false;
130 }
131 // Apply ranking- and Bellman operators for non-target state
132 // Apply Bellman operator for non-target state
135
136 for (auto choice : transitionProbabilityMatrix.getRowGroupIndices(state)) {
137 auto currentChoiceValue = transitionProbabilityMatrix.multiplyRowWithVector(choice, values);
138 stateValue &= currentChoiceValue;
139 // Compute rank for this choice if we're minimizing or if the choice is inductive
140 if (storm::solver::minimize(Dir) || currentChoiceValue >= values[state]) {
142 for (auto const& entry : transitionProbabilityMatrix.getRow(choice)) {
143 if (storm::utility::isZero(entry.getValue())) {
144 continue;
145 }
146 choiceRank &= ranks[entry.getColumn()];
147 }
148 STORM_LOG_ASSERT(!choiceRank.empty(),
149 "Ranking operator failed to compute rank for state " << state << " since the row " << choice << " is empty.");
150 stateRankMinusOne &= *choiceRank;
151 }
152 }
153 STORM_LOG_ASSERT(!stateValue.empty(), "Bellman operator failed to compute value for state " << state << " since the row group is empty.");
154 if (*stateValue < values[state]) {
155 double const approxDiff = storm::utility::convertNumber<double, ValueType>(values[state] - *stateValue);
156 STORM_LOG_WARN("Certificate invalid because lower bound is not inductive. At state " << state << " the Bellman operator yields " << *stateValue
157 << " but the certificate has larger value " << values[state]
158 << ". Approx. diff is " << approxDiff << ".");
159 return false;
160 }
161 if (stateRankMinusOne.empty()) {
162 STORM_LOG_WARN("Certificate invalid because there is no valid (i.e. inductive) action at state " << state << ", where the certificate has rank "
163 << ranks[state] << ".");
164 return false;
165 }
166 STORM_LOG_ASSERT(!stateRankMinusOne.empty(), "Ranking operator failed to compute rank for state " << state << ".");
167 RankingType const stateRank = *stateRankMinusOne == InfRank ? InfRank : *stateRankMinusOne + 1; // avoid integer overflow!
168 if (stateRank > ranks[state]) {
169 STORM_LOG_WARN("Certificate invalid because ranks for lower bound is not inductive. At state "
170 << state << " the rank operator yields " << stateRank << " but the certificate has smaller rank " << ranks[state] << ".");
171 return false;
172 }
173 }
174 return true;
175}
176
177template<typename ValueType>
179 if (!dir.has_value() && !transitionProbabilityMatrix.hasTrivialRowGrouping()) {
180 STORM_LOG_WARN("Certificate invalid because Matrix has non-trivial row grouping but no optimization direction given.");
181 return false;
182 }
183 if (transitionProbabilityMatrix.getRowGroupCount() != targetStates.size() || transitionProbabilityMatrix.getColumnCount() != targetStates.size()) {
184 STORM_LOG_WARN("Certificate invalid because Matrix has invalid dimensions.");
185 return false;
186 }
187 if (dir.has_value() && storm::solver::maximize(*dir)) {
188 return checkValidityInternal<storm::OptimizationDirection::Maximize>(transitionProbabilityMatrix);
189 } else {
190 return checkValidityInternal<storm::OptimizationDirection::Minimize>(transitionProbabilityMatrix);
191 }
192}
193
194template<typename ValueType>
195template<storm::OptimizationDirection Dir>
196bool ReachabilityProbabilityCertificate<ValueType>::checkValidityInternal(storm::storage::SparseMatrix<ValueType> const& transitionProbabilityMatrix) const {
198 if (hasConstraintStates()) {
199 constraintStatesRef.reset(*constraintStates);
200 }
201 if (hasUpperBoundsCertificate() &&
202 !checkUpperBoundCertificate<ValueType, Dir>(transitionProbabilityMatrix, targetStates, constraintStatesRef, upperBoundsCertificate.values)) {
203 STORM_LOG_WARN("Certificate invalid because upper bound certificate is violated.");
204 return false;
205 }
206 if (hasLowerBoundsCertificate() && !checkLowerBoundCertificate<ValueType, Dir>(transitionProbabilityMatrix, targetStates, constraintStatesRef,
207 lowerBoundsCertificate.values, lowerBoundsCertificate.ranks)) {
208 STORM_LOG_WARN("Certificate invalid because lower bound certificate is violated.");
209 return false;
210 }
211 return true;
212}
213
214template<typename ValueType>
217 result["kind"] = "reachability-probability";
218 if (dir.has_value()) {
219 result["direction"] = storm::solver::minimize(*dir) ? "min" : "max";
220 }
221 result["target"] = detail::stateSetToJson<ValueType>(targetLabel, targetStates);
222 if (hasConstraintStates()) {
223 result["constraint"] = detail::stateSetToJson<ValueType>(constraintLabel, *constraintStates);
224 }
226 result["lower"]["values"] = detail::valuesToJson<ValueType, ValueType>(lowerBoundsCertificate.values);
227 result["lower"]["ranks"] = detail::ranksToJson<ValueType>(lowerBoundsCertificate.ranks);
228 }
230 result["upper"]["values"] = detail::valuesToJson<ValueType, ValueType>(upperBoundsCertificate.values);
231 }
232 return result;
233}
234
235template<typename ValueType>
237 std::stringstream ss;
238 auto const numStates = relevantStates.getNumberOfSetBits();
239 STORM_LOG_THROW(numStates > 0, storm::exceptions::InvalidOperationException,
240 "Tried to summarize reachability probability certificate but no state is relevant.");
241
242 auto getLowerUpperBound = [this](uint64_t state) -> std::pair<ValueType, ValueType> {
243 return {hasLowerBoundsCertificate() ? lowerBoundsCertificate.values[state] : storm::utility::zero<ValueType>(),
244 hasUpperBoundsCertificate() ? upperBoundsCertificate.values[state] : storm::utility::one<ValueType>()};
245 };
246 auto const [lowerBound, upperBound] = getLowerUpperBound(relevantStates.getNextSetIndex(0));
247 ss << "Certified reachability probability: [" << lowerBound << ", " << upperBound << "]";
249 ss << " (approx. [" << storm::utility::convertNumber<double>(lowerBound) << ", " << storm::utility::convertNumber<double>(upperBound) << "])";
250 }
251 if (numStates > 1) {
252 ss << " (only showing first of " << numStates << " relevant states)";
253 }
254 storm::utility::Maximum<ValueType> maxAbsDiff, maxRelDiff;
255 for (auto const state : relevantStates) {
256 auto const [lower, upper] = getLowerUpperBound(state);
257 ValueType absDiff = upper - lower;
258 ValueType relDiff;
259 if (storm::utility::isZero(lower)) {
261 } else {
262 relDiff = absDiff / lower;
263 }
264 maxAbsDiff &= absDiff;
265 maxRelDiff &= relDiff;
266 }
267 auto printIntervalWidth([&](auto const& value, auto const& name) {
268 ss << "\n\t" << name << " interval width: " << value;
270 ss << " (approx. " << storm::utility::convertNumber<double>(value) << ")";
271 }
272 if (numStates > 1) {
273 ss << " (showing maximum over " << numStates << " relevant states)";
274 }
275 });
276 printIntervalWidth(*maxAbsDiff, "Absolute");
277 printIntervalWidth(*maxRelDiff, "Relative");
278 return ss.str();
279}
280
281template<typename ValueType>
282std::unique_ptr<Certificate<ValueType>> ReachabilityProbabilityCertificate<ValueType>::clone() const {
283 auto cloned = hasConstraintStates()
284 ? std::make_unique<ReachabilityProbabilityCertificate<ValueType>>(dir, targetStates, *constraintStates, targetLabel, constraintLabel)
285 : std::make_unique<ReachabilityProbabilityCertificate<ValueType>>(dir, targetStates, targetLabel);
287 auto v = lowerBoundsCertificate.values;
288 auto r = lowerBoundsCertificate.ranks;
289 cloned->setLowerBoundsCertificate(std::move(v), std::move(r));
290 }
292 auto v = upperBoundsCertificate.values;
293 cloned->setUpperBoundsCertificate(std::move(v));
294 }
295 return cloned;
296}
297
298template<typename ValueType>
299void ReachabilityProbabilityCertificate<ValueType>::setLowerBoundsCertificate(std::vector<ValueType>&& values, std::vector<RankingType>&& ranks) {
300 STORM_LOG_ASSERT(values.size() == targetStates.size(), "Values for lower bound certificate have incorrect dimension.");
301 STORM_LOG_ASSERT(ranks.size() == targetStates.size(), "Ranks for lower bound certificate have incorrect dimension.");
302 lowerBoundsCertificate.values = std::move(values);
303 lowerBoundsCertificate.ranks = std::move(ranks);
304}
305
306template<typename ValueType>
308 STORM_LOG_ASSERT(values.size() == targetStates.size(), "Values for upper bound certificate have incorrect dimension.");
309 upperBoundsCertificate.values = std::move(values);
310}
311
312template<typename ValueType>
314 return !lowerBoundsCertificate.values.empty();
315}
316
317template<typename ValueType>
319 return !upperBoundsCertificate.values.empty();
320}
321
322template<typename ValueType>
323bool ReachabilityProbabilityCertificate<ValueType>::hasConstraintStates() const {
324 return constraintStates.has_value();
325}
326
327template class ReachabilityProbabilityCertificate<double>;
328template class ReachabilityProbabilityCertificate<storm::RationalNumber>;
329
330} // namespace storm::modelchecker
Helper class that optionally holds a reference to an object of type T.
Definition OptionalRef.h:48
void reset()
Unsets the reference.
Certificate(CertificateKind kind)
Definition Certificate.h:44
virtual std::string summaryString(storm::storage::BitVector const &relevantStates) const override
virtual std::unique_ptr< Certificate< ValueType > > clone() const override
virtual bool checkValidity(storm::models::Model< ValueType > const &model) const override
void setLowerBoundsCertificate(std::vector< ValueType > &&values, std::vector< RankingType > &&ranks)
ReachabilityProbabilityCertificate(std::optional< storm::OptimizationDirection > dir, storm::storage::BitVector targetStates, std::string targetLabel="goal")
virtual bool isSparseModel() const
Checks whether the model is a sparse model.
Definition ModelBase.cpp:11
bool isOfType(storm::models::ModelType const &modelType) const
Checks whether the model is of the given type.
Definition ModelBase.cpp:27
This class represents a continuous-time Markov chain.
Definition Ctmc.h:16
Base class for all sparse models.
Definition Model.h:34
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
uint64_t getNextSetIndex(uint64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to true in the bit vector.
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.
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.
ValueType multiplyRowWithVector(index_type row, std::vector< ValueType > const &vector, ValueType offset=storm::utility::zero< ValueType >()) const
Multiplies a single row of the matrix with the given vector and adds the given offset to the result.
index_type getRowGroupCount() const
Returns the number of row groups in the matrix.
index_type getColumnCount() const
Returns the number of columns of the matrix.
bool hasTrivialRowGrouping() const
Retrieves whether the matrix has a trivial row grouping.
std::vector< index_type > const & getRowGroupIndices() const
Returns the grouping of rows of this matrix.
Stores and manages an extremal (maximal or minimal) value.
Definition Extremum.h:17
#define STORM_LOG_WARN(message)
Definition logging.h:35
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:39
storm::json< ValueType > ranksToJson(std::vector< RankingType > const &ranks)
Converts the given ranks to a json array.
storm::json< ValueType > valuesToJson(std::vector< ElementType > const &values)
Converts the given values to a json array.
storm::json< ValueType > stateSetToJson(std::string const &label, storm::storage::BitVector const &states)
Converts the given set of states to a json object of the form {"label": <label>, "states": [<state in...
bool checkUpperBoundCertificate(storm::storage::SparseMatrix< ValueType > const &transitionProbabilityMatrix, storm::storage::BitVector const &targetStates, storm::OptionalRef< storm::storage::BitVector const > constraintStates, std::vector< ValueType > const &values)
static RankingType const InfRank
Definition RankingType.h:8
bool checkLowerBoundCertificate(storm::storage::SparseMatrix< ValueType > const &transitionProbabilityMatrix, storm::storage::BitVector const &targetStates, storm::OptionalRef< storm::storage::BitVector const > constraintStates, std::vector< ValueType > const &values, std::vector< RankingType > const &ranks)
bool constexpr maximize(OptimizationDirection d)
OptimizationDirection constexpr invert(OptimizationDirection d)
bool constexpr minimize(OptimizationDirection d)
Extremum< storm::OptimizationDirection::Maximize, ValueType > Maximum
Definition Extremum.h:166
Extremum< storm::OptimizationDirection::Minimize, ValueType > Minimum
Definition Extremum.h:168
bool isZero(ValueType const &a)
Definition constants.cpp:44
ValueType zero()
Definition constants.cpp:26
ValueType one()
Definition constants.cpp:21
TargetType convertNumber(SourceType const &number)
nlohmann::basic_json< std::map, std::vector, std::string, bool, int64_t, uint64_t, ValueType > json
Definition JsonForward.h:11
static const bool IsExact