Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
shortestPaths.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <boost/optional/optional.hpp>
5#include <cassert>
6#include <cstdint>
7#include <iosfwd>
8#include <set>
9#include <string>
10#include <unordered_map>
11#include <unordered_set>
12#include <vector>
13
17
18namespace storm {
19namespace storage {
20template<typename ValueType>
21class SparseMatrix;
22
23namespace sparse {
24typedef uint_fast64_t state_type;
25}
26} // namespace storage
27
28namespace models {
29namespace sparse {
30template<typename ValueType>
32
33template<class CValueType, class CRewardModelType>
34class Model;
35} // namespace sparse
36} // namespace models
37
38namespace utility {
39namespace ksp {
41using OrderedStateList = std::vector<state_t>;
43
44// -- helper structs/classes -----------------------------------------------------------------------------
45
46template<typename T>
47struct Path {
48 boost::optional<state_t> predecessorNode;
49 unsigned long predecessorK;
51
52 // arbitrary order for std::set
53 bool operator<(const Path<T>& rhs) const {
56 }
57 return predecessorK < rhs.predecessorK;
58 }
59
60 bool operator==(const Path<T>& rhs) const {
61 return (predecessorNode == rhs.predecessorNode) && (predecessorK == rhs.predecessorK);
62 }
63};
64
65template<typename T>
66std::ostream& operator<<(std::ostream& out, Path<T> const& p);
67
68// when using the raw matrix/vector invocation, this enum parameter
69// forces the caller to declare whether the matrix has the evil I-P
70// format, which requires back-conversion of the entries
72
73// -------------------------------------------------------------------------------------------------------
74
75template<typename T>
77 public:
79 using StateProbMap = std::unordered_map<state_t, T>;
81
87 ShortestPathsGenerator(Model const& model, BitVector const& targetBV);
88
89 // allow alternative ways of specifying the target,
90 // all of which will be converted to BitVector and delegated to constructor above
91 ShortestPathsGenerator(Model const& model, state_t singleTarget);
92 ShortestPathsGenerator(Model const& model, std::vector<state_t> const& targetList);
93 ShortestPathsGenerator(Model const& model, std::string const& targetLabel = "target");
94
95 // a further alternative: use transition matrix of maybe-states
96 // combined with target vector (e.g., the instantiated matrix/vector from SamplingModel);
97 // in this case separately specifying a target makes no sense
98 ShortestPathsGenerator(Matrix const& transitionMatrix, std::vector<T> const& targetProbVector, BitVector const& initialStates, MatrixFormat matrixFormat);
99 ShortestPathsGenerator(Matrix const& maybeTransitionMatrix, StateProbMap const& targetProbMap, BitVector const& initialStates, MatrixFormat matrixFormat);
100
102
108 T getDistance(unsigned long k);
109
116 storage::BitVector getStates(unsigned long k);
117
123 OrderedStateList getPathAsList(unsigned long k);
124
125 private:
126 Matrix const& transitionMatrix;
127 state_t numStates; // includes meta-target, i.e. states in model + 1
128 state_t metaTarget;
129 BitVector initialStates;
130 StateProbMap targetProbMap;
131
132 MatrixFormat matrixFormat;
133
134 std::vector<OrderedStateList> graphPredecessors;
135 std::vector<boost::optional<state_t>> shortestPathPredecessors;
136 std::vector<OrderedStateList> shortestPathSuccessors;
137 std::vector<T> shortestPathDistances;
138
139 std::vector<std::vector<Path<T>>> kShortestPaths;
140 std::vector<std::set<Path<T>>> candidatePaths;
141
148 void computePredecessors();
149
155 void performDijkstra();
156
162 void computeSPSuccessors();
163
169 void initializeShortestPaths();
170
174 void computeNextPath(state_t node, unsigned long k);
175
180 void computeKSP(unsigned long k);
181
185 void printKShortestPath(state_t targetNode, unsigned long k, bool head = true) const;
186
190 T getEdgeDistance(state_t tailNode, state_t headNode) const;
191
192 // --- tiny helper fcts ---
193
194 inline bool isInitialState(state_t node) const {
195 return std::find(initialStates.begin(), initialStates.end(), node) != initialStates.end();
196 }
197
198 inline bool isMetaTargetPredecessor(state_t node) const {
199 return targetProbMap.count(node) == 1;
200 }
201
202 // I dislike this. But it is necessary if we want to handle those ugly I-P matrices
203 inline T convertDistance(state_t tailNode, state_t headNode, T distance) const {
204 if (matrixFormat == MatrixFormat::straight) {
205 return distance;
206 } else {
207 if (tailNode == headNode) {
208 // diagonal: 1-p = dist
209 return one<T>() - distance;
210 } else {
211 // non-diag: -p = dist
212 return zero<T>() - distance;
213 }
214 }
215 }
216
220 inline StateProbMap allProbOneMap(BitVector bitVector) const {
221 StateProbMap stateProbMap;
222 for (state_t node : bitVector) {
223 stateProbMap.emplace(node, one<T>()); // FIXME check rvalue warning (here and below)
224 }
225 return stateProbMap;
226 }
227
232 inline std::unordered_map<state_t, T> vectorToMap(std::vector<T> probVector) const {
233 // STORM_LOG_ASSERT(probVector.size() == numStates, "ProbVector has wrong size."); // numStates may not yet be initialized! // still true?
234
235 std::unordered_map<state_t, T> stateProbMap;
236
237 for (state_t i = 0; i < probVector.size(); i++) {
238 T probEntry = probVector[i];
239
240 // only non-zero entries (i.e. true transitions) are added to the map
241 if (probEntry != 0) {
242 STORM_LOG_ASSERT(0 < probEntry, "Probability entry should be positive.");
243 STORM_LOG_ASSERT(probEntry <= 1, "Probability entry should be at most 1.");
244 stateProbMap.emplace(i, probEntry);
245 }
246 }
247
248 return stateProbMap;
249 }
250
251 // -----------------------
252};
253} // namespace ksp
254} // namespace utility
255} // namespace storm
Base class for all sparse models.
Definition Model.h:30
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
const_iterator end() const
Returns an iterator pointing at the element past the back of the bit vector.
const_iterator begin() const
Returns an iterator to the indices of the set bits in the bit vector.
A class that holds a possibly non-square matrix in the compressed row storage format.
std::unordered_map< state_t, T > StateProbMap
models::sparse::Model< T, models::sparse::StandardRewardModel< T > > Model
ShortestPathsGenerator(Model const &model, BitVector const &targetBV)
Performs precomputations (including meta-target insertion and Dijkstra).
storage::BitVector getStates(unsigned long k)
Returns the states that occur in the KSP.
T getDistance(unsigned long k)
Returns distance (i.e., probability) of the KSP.
OrderedStateList getPathAsList(unsigned long k)
Returns the states of the KSP as back-to-front traversal.
ShortestPathsGenerator(Matrix const &maybeTransitionMatrix, StateProbMap const &targetProbMap, BitVector const &initialStates, MatrixFormat matrixFormat)
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:9
std::vector< state_t > OrderedStateList
storage::BitVector BitVector
storage::sparse::state_type state_t
std::ostream & operator<<(std::ostream &out, Path< T > const &p)
ValueType zero()
Definition constants.cpp:24
ValueType one()
Definition constants.cpp:19
bool operator==(const Path< T > &rhs) const
bool operator<(const Path< T > &rhs) const
boost::optional< state_t > predecessorNode