Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
ReachabilityRewardCertificate.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <sstream>
5
18
21
22namespace storm::modelchecker {
23
24template<typename ValueType>
26 std::vector<ValueType> stateActionRewardVector, std::string targetLabel,
27 std::string rewardModelName)
29 targetStates(std::move(targetStates)),
30 stateActionRewardVector(std::move(stateActionRewardVector)),
31 targetLabel(targetLabel),
32 rewardModelName(rewardModelName),
33 dir(dir) {}
34
35template<typename ValueType>
37 if (!model.isSparseModel()) {
38 STORM_LOG_WARN("Certificate invalid because the given model is not sparse.");
39 return false;
40 }
42 if (auto const* ctmcPtr = dynamic_cast<storm::models::sparse::Ctmc<ValueType> const*>(&model)) {
43 return checkValidity(ctmcPtr->computeProbabilityMatrix());
44 }
47 if (auto const* sparseModelPtr = dynamic_cast<storm::models::sparse::Model<ValueType> const*>(&model)) {
48 return checkValidity(sparseModelPtr->getTransitionMatrix());
49 }
50 }
51 STORM_LOG_WARN("Certificate invalid because the given model is of unsupported type.");
52 return false;
53}
54
55template<typename ValueType, OptimizationDirection Dir>
56bool checkUpperBoundCertificate(storm::storage::SparseMatrix<ValueType> const& transitionProbabilityMatrix, storm::storage::BitVector const& targetStates,
57 std::vector<ValueType> const& stateActionRewardVector, std::vector<storm::utility::ExtendedValueType<ValueType>> const& values,
58 std::vector<RankingType> const& ranks) {
59 for (uint64_t state = 0; state < targetStates.size(); ++state) {
60 if (storm::utility::isNegativeInfinity(values[state])) {
61 STORM_LOG_WARN("Certificate invalid because state " << state << " has negative infinity as value.");
62 return false;
63 }
64 // Handle states that do not almost surely reach target
65 if (storm::utility::isFinite(values[state]) && ranks[state] == InfRank) {
66 STORM_LOG_WARN("Certificate invalid because state " << state << " has infinite rank but finite value " << values[state] << ".");
67 return false;
68 }
69 if (targetStates.get(state)) {
70 // Handle target state
71 if (values[state] < storm::utility::zero<ValueType>()) {
72 STORM_LOG_WARN("Certificate invalid because target state " << state << " has negative upper bound " << values[state]);
73 return false;
74 }
75 // The rank of a target state must be non-negative, too.
76 // However, a negative rank (which is an unsigned integer) is not possible, anyway.
77 // So, we skip checking for the rank
78 continue;
79 }
80
81 // Apply Bellman and Distance operator for non-target states
82 // Following Propositions 8 and 9 of https://doi.org/10.1007/978-3-031-90653-4_7 (see also the arXiv version), the rank only considers
83 // the x-decreasing (i.e. inductive) choices if we minimize and all choices if we maximize.
86 for (auto choice : transitionProbabilityMatrix.getRowGroupIndices(state)) {
87 auto const currentChoiceValue = transitionProbabilityMatrix.multiplyRowWithVector(choice, values, stateActionRewardVector[choice]);
88 stateValue = storm::solver::maximize(Dir) ? std::max(stateValue, currentChoiceValue) : std::min(stateValue, currentChoiceValue);
89 // Compute rank for this choice if we're maximizing or if the choice is inductive
90 if (storm::solver::maximize(Dir) || currentChoiceValue <= values[state]) {
92 for (auto const& entry : transitionProbabilityMatrix.getRow(choice)) {
93 if (storm::utility::isZero(entry.getValue())) {
94 continue;
95 }
96 choiceRank &= ranks[entry.getColumn()];
97 }
98 STORM_LOG_ASSERT(!choiceRank.empty(),
99 "Ranking operator failed to compute rank for state " << state << " since the row " << choice << " is empty.");
100 stateRankMinusOne &= *choiceRank;
101 }
102 }
103 if (values[state] < stateValue) {
104 double const approxDiff = storm::utility::convertNumber<double>(stateValue - values[state]);
105 STORM_LOG_WARN("Certificate invalid because upper bound is not inductive. At state " << state << " the Bellman operator yields " << stateValue
106 << " but the certificate has smaller value " << values[state]
107 << ". Approx. diff is " << approxDiff << ".");
108 return false;
109 }
110 if (stateRankMinusOne.empty()) {
111 STORM_LOG_WARN("Certificate invalid because there is no valid (i.e. decreasing) action at state " << state << ", where the certificate has rank "
112 << ranks[state] << ".");
113 return false;
114 }
115 RankingType const stateRank = *stateRankMinusOne == InfRank ? InfRank : *stateRankMinusOne + 1; // avoid integer overflow!
116 if (stateRank > ranks[state]) {
117 STORM_LOG_WARN("Certificate invalid because ranks for upper bound is not inductive. At state "
118 << state << " the rank operator yields " << stateRank << " but the certificate has smaller rank " << ranks[state] << ".");
119 return false;
120 }
121 }
122 return true;
123}
124
125template<typename ValueType, OptimizationDirection Dir>
126bool checkLowerBoundCertificate(storm::storage::SparseMatrix<ValueType> const& transitionProbabilityMatrix, storm::storage::BitVector const& targetStates,
127 std::vector<ValueType> const& stateActionRewardVector, std::vector<storm::utility::ExtendedValueType<ValueType>> const& values,
128 std::vector<RankingType> const& ranks) {
129 for (uint64_t state = 0; state < targetStates.size(); ++state) {
130 // Handle target state
131 if (targetStates.get(state)) {
132 if (!storm::utility::isFinite(values[state]) || !storm::utility::isZero(storm::utility::getFinite(values[state]))) {
133 STORM_LOG_WARN("Certificate invalid because target state " << state << " has non-zero lower bound " << values[state] << ".");
134 return false;
135 }
136 if (ranks[state] != InfRank) {
137 STORM_LOG_WARN("Certificate invalid because target state " << state << " has finite rank" << ranks[state] << ".");
138 return false;
139 }
140 continue;
141 }
142 if (storm::utility::isInfinity(values[state]) && ranks[state] == InfRank) {
143 STORM_LOG_WARN("Certificate invalid because state " << state << " has infinite rank and value.");
144 return false;
145 }
146 // Apply ranking- and Bellman operators for non-target states
147 // Following Proposition 7 of https://doi.org/10.1007/978-3-031-90653-4_7 (see also the arXiv version), the (complementary distance) rank
148 // considers all choices, no matter whether we minimize or maximize. Unlike for lower bounds on maximal reachability probabilities, there is no
149 // restriction to inductive choices.
152
153 for (auto choice : transitionProbabilityMatrix.getRowGroupIndices(state)) {
154 auto const currentChoiceValue = transitionProbabilityMatrix.multiplyRowWithVector(choice, values, stateActionRewardVector[choice]);
155 stateValue = storm::solver::maximize(Dir) ? std::max(stateValue, currentChoiceValue) : std::min(stateValue, currentChoiceValue);
156 // Compute rank for this choice
158 bool allSuccessorsEqual = true;
159 for (auto const& entry : transitionProbabilityMatrix.getRow(choice)) {
160 if (storm::utility::isZero(entry.getValue())) {
161 continue;
162 }
163 if (!minSuccessorRank.empty() && ranks[entry.getColumn()] != *minSuccessorRank) {
164 allSuccessorsEqual = false;
165 }
166 minSuccessorRank &= ranks[entry.getColumn()];
167 }
168 STORM_LOG_ASSERT(!minSuccessorRank.empty(),
169 "Ranking operator failed to compute rank for state " << state << " since the row " << choice << " is empty.");
170 auto choiceRank = *minSuccessorRank;
171 if (choiceRank != InfRank && !allSuccessorsEqual) {
172 ++choiceRank;
173 }
174 stateRank &= choiceRank;
175 }
176
177 if (stateValue < values[state]) {
178 double const approxDiff = storm::utility::convertNumber<double>(values[state] - stateValue);
179 STORM_LOG_WARN("Certificate invalid because lower bound is not inductive. At state " << state << " the Bellman operator yields " << stateValue
180 << " but the certificate has larger value " << values[state]
181 << ". Approx. diff is " << approxDiff << ".");
182 return false;
183 }
184 STORM_LOG_ASSERT(!stateRank.empty(), "Failed to compute ranks for state " << state << " since the row group is empty.");
185 if (*stateRank > ranks[state]) {
186 STORM_LOG_WARN("Certificate invalid because ranks for lower bound is not inductive. At state "
187 << state << " the rank operator yields " << *stateRank << " but the certificate has smaller rank " << ranks[state] << ".");
188 return false;
189 }
190 }
191 return true;
192}
193
194template<typename ValueType>
196 if (!dir.has_value() && !transitionProbabilityMatrix.hasTrivialRowGrouping()) {
197 STORM_LOG_WARN("Certificate invalid because Matrix has non-trivial row grouping but no optimization direction given.");
198 return false;
199 }
200 if (transitionProbabilityMatrix.getRowGroupCount() != targetStates.size() || transitionProbabilityMatrix.getColumnCount() != targetStates.size()) {
201 STORM_LOG_WARN("Certificate invalid because Matrix has invalid dimensions (target states).");
202 return false;
203 }
204 if (transitionProbabilityMatrix.getRowCount() != stateActionRewardVector.size()) {
205 STORM_LOG_WARN("Certificate invalid because Matrix has invalid dimensions (reward vector).");
206 return false;
207 }
208
209 if (dir.has_value() && storm::solver::maximize(*dir)) {
210 return checkValidityInternal<storm::OptimizationDirection::Maximize>(transitionProbabilityMatrix);
211 } else {
212 return checkValidityInternal<storm::OptimizationDirection::Minimize>(transitionProbabilityMatrix);
213 }
214}
215
216template<typename ValueType>
217template<storm::OptimizationDirection Dir>
218bool ReachabilityRewardCertificate<ValueType>::checkValidityInternal(storm::storage::SparseMatrix<ValueType> const& transitionProbabilityMatrix) const {
219 if (hasUpperBoundsCertificate() && !checkUpperBoundCertificate<ValueType, Dir>(transitionProbabilityMatrix, targetStates, stateActionRewardVector,
220 upperBoundsCertificate.values, upperBoundsCertificate.ranks)) {
221 STORM_LOG_WARN("Certificate invalid because upper bound certificate is violated.");
222 return false;
223 }
224 if (hasLowerBoundsCertificate() && !checkLowerBoundCertificate<ValueType, Dir>(transitionProbabilityMatrix, targetStates, stateActionRewardVector,
225 lowerBoundsCertificate.values, lowerBoundsCertificate.ranks)) {
226 STORM_LOG_WARN("Certificate invalid because lower bound certificate is violated.");
227 return false;
228 }
229 return true;
230}
231
232template<typename ValueType>
235 result["kind"] = "reachability-reward";
236 if (dir.has_value()) {
237 result["direction"] = storm::solver::minimize(*dir) ? "min" : "max";
238 }
239 result["reward-model"] = rewardModelName;
240 result["target"] = detail::stateSetToJson<ValueType>(targetLabel, targetStates);
241 result["choice-rewards"] = detail::valuesToJson<ValueType, ValueType>(stateActionRewardVector);
243 result["lower"]["values"] = detail::valuesToJson<ValueType, ExtendedValueType>(lowerBoundsCertificate.values);
244 result["lower"]["ranks"] = detail::ranksToJson<ValueType>(lowerBoundsCertificate.ranks);
245 }
247 result["upper"]["values"] = detail::valuesToJson<ValueType, ExtendedValueType>(upperBoundsCertificate.values);
248 result["upper"]["ranks"] = detail::ranksToJson<ValueType>(upperBoundsCertificate.ranks);
249 }
250 return result;
251}
252
253template<typename ValueType>
255 std::stringstream ss;
256 auto const numStates = relevantStates.getNumberOfSetBits();
257 STORM_LOG_THROW(numStates > 0, storm::exceptions::InvalidOperationException,
258 "Tried to summarize reachability reward certificate but no state is relevant.");
259
260 auto getLowerUpperBound = [this](uint64_t state) -> std::pair<ExtendedValueType, ExtendedValueType> {
261 return {hasLowerBoundsCertificate() ? lowerBoundsCertificate.values[state] : storm::utility::negativeInfinity<ValueType>(),
262 hasUpperBoundsCertificate() ? upperBoundsCertificate.values[state] : storm::utility::positiveInfinity<ValueType>()};
263 };
264 auto const [lowerBound, upperBound] = getLowerUpperBound(relevantStates.getNextSetIndex(0));
265 ss << "Certified reachability reward: [" << lowerBound << ", " << upperBound << "]";
267 ss << " (approx. [" << storm::utility::convertNumber<double>(lowerBound) << ", " << storm::utility::convertNumber<double>(upperBound) << "])";
268 }
269 if (numStates > 1) {
270 ss << " (only showing first of " << numStates << " relevant states)";
271 }
273 ExtendedValueType maxAbsDiff(zero), maxRelDiff(zero);
274 for (auto const state : relevantStates) {
275 auto const [lower, upper] = getLowerUpperBound(state);
276 ExtendedValueType absDiff = (upper == lower) ? zero : (upper - lower); // inf-inf undefined!
277 ExtendedValueType relDiff;
278 if (absDiff == zero) {
279 relDiff = zero;
280 } else if (lower == zero || storm::utility::isInfinity(absDiff)) {
282 } else {
283 ValueType relDiffValue = storm::utility::getFinite(absDiff) / storm::utility::getFinite(lower);
284 relDiff = ExtendedValueType(relDiffValue < storm::utility::zero<ValueType>() ? -relDiffValue : relDiffValue);
285 }
286 maxAbsDiff = std::max(maxAbsDiff, absDiff);
287 maxRelDiff = std::max(maxRelDiff, relDiff);
288 }
289 auto printIntervalWidth([&](auto const& value, auto const& name) {
290 ss << "\n\t" << name << " interval width: " << value;
292 ss << " (approx. " << storm::utility::convertNumber<double>(value) << ")";
293 }
294 if (numStates > 1) {
295 ss << " (showing maximum over " << numStates << " relevant states)";
296 }
297 });
298 printIntervalWidth(maxAbsDiff, "Absolute");
299 printIntervalWidth(maxRelDiff, "Relative");
300 return ss.str();
301}
302
303template<typename ValueType>
304std::unique_ptr<Certificate<ValueType>> ReachabilityRewardCertificate<ValueType>::clone() const {
305 auto cloned = std::make_unique<ReachabilityRewardCertificate<ValueType>>(dir, targetStates, stateActionRewardVector, targetLabel, rewardModelName);
307 auto v = lowerBoundsCertificate.values;
308 auto r = lowerBoundsCertificate.ranks;
309 cloned->setLowerBoundsCertificate(std::move(v), std::move(r));
310 }
312 auto v = upperBoundsCertificate.values;
313 auto r = upperBoundsCertificate.ranks;
314 cloned->setUpperBoundsCertificate(std::move(v), std::move(r));
315 }
316 return cloned;
317}
318
319template<typename ValueType>
320void ReachabilityRewardCertificate<ValueType>::setLowerBoundsCertificate(std::vector<ExtendedValueType>&& values, std::vector<RankingType>&& ranks) {
321 STORM_LOG_ASSERT(values.size() == targetStates.size(), "Values for lower bound certificate have incorrect dimension.");
322 STORM_LOG_ASSERT(ranks.size() == targetStates.size(), "Ranks for lower bound certificate have incorrect dimension.");
323 lowerBoundsCertificate.values = std::move(values);
324 lowerBoundsCertificate.ranks = std::move(ranks);
325}
326
327template<typename ValueType>
328void ReachabilityRewardCertificate<ValueType>::setUpperBoundsCertificate(std::vector<ExtendedValueType>&& values, std::vector<RankingType>&& ranks) {
329 STORM_LOG_ASSERT(values.size() == targetStates.size(), "Values for upper bound certificate have incorrect dimension.");
330 STORM_LOG_ASSERT(ranks.size() == targetStates.size(), "Ranks for lower bound certificate have incorrect dimension.");
331 upperBoundsCertificate.values = std::move(values);
332 upperBoundsCertificate.ranks = std::move(ranks);
333}
334
335template<typename ValueType>
337 return !lowerBoundsCertificate.values.empty();
338}
339
340template<typename ValueType>
342 return !upperBoundsCertificate.values.empty();
343}
344
347
348} // namespace storm::modelchecker
Certificate(CertificateKind kind)
Definition Certificate.h:44
virtual bool checkValidity(storm::models::Model< ValueType > const &model) const override
virtual storm::json< ValueType > toJson() const override
void setLowerBoundsCertificate(std::vector< ExtendedValueType > &&values, std::vector< RankingType > &&ranks)
virtual std::string summaryString(storm::storage::BitVector const &relevantStates) const override
virtual std::unique_ptr< Certificate< ValueType > > clone() const override
storm::utility::ExtendedValueType< ValueType > ExtendedValueType
ReachabilityRewardCertificate(std::optional< storm::OptimizationDirection > dir, storm::storage::BitVector targetStates, std::vector< ValueType > stateActionRewardVector, std::string targetLabel="goal", std::string rewardModelName="")
void setUpperBoundsCertificate(std::vector< ExtendedValueType > &&values, std::vector< RankingType > &&ranks)
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.
index_type getRowCount() const
Returns the number of rows of the 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)
ExtendedValueType< ValueType > positiveInfinity()
Extremum< storm::OptimizationDirection::Minimize, ValueType > Minimum
Definition Extremum.h:168
bool isFinite(ExtendedNumber< ValueType > const &value)
ExtendedValueType< ValueType > negativeInfinity()
bool isNegativeInfinity(ExtendedNumber< ValueType > const &number)
bool isZero(ValueType const &a)
Definition constants.cpp:44
ValueType zero()
Definition constants.cpp:26
std::conditional_t< storm::NumberTraits< ValueType >::HasInfinity, ValueType, ExtendedNumber< ValueType > > ExtendedValueType
The type to use for values that may be infinite.
bool isInfinity(ValueType const &a)
ValueType const & getFinite(ExtendedNumber< ValueType > const &value)
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