Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
WinningRegionQueryInterface.cpp
Go to the documentation of this file.
5
6namespace storm {
7namespace pomdp {
8template<typename ValueType>
10 : pomdp(pomdp), winningRegion(winningRegion) {
11 uint64_t nrObservations = pomdp.getNrObservations();
12 for (uint64_t observation = 0; observation < nrObservations; ++observation) {
13 statesPerObservation.push_back(std::vector<uint64_t>());
14 }
15 for (uint64_t state = 0; state < pomdp.getNumberOfStates(); ++state) {
16 statesPerObservation[pomdp.getObservation(state)].push_back(state);
17 }
18}
19
20template<typename ValueType>
22 STORM_LOG_ASSERT(beliefSupport.getNumberOfSetBits() > 0, "One cannot think one is literally nowhere");
23 uint64_t observation = pomdp.getObservation(beliefSupport.getNextSetIndex(0));
24 // TODO consider optimizations after testing.
25 storm::storage::BitVector queryVector(statesPerObservation[observation].size());
26 auto stateWithObsIt = statesPerObservation[observation].begin();
27 uint64_t offset = 0;
28 for (uint64_t possibleState : beliefSupport) {
29 STORM_LOG_ASSERT(pomdp.getObservation(possibleState) == observation, "Support must be observation-consistent");
30 while (possibleState > *stateWithObsIt) {
31 stateWithObsIt++;
32 offset++;
33 }
34 if (possibleState == *stateWithObsIt) {
35 queryVector.set(offset);
36 }
37 }
38 return winningRegion.query(observation, queryVector);
39}
40
41template<typename ValueType>
42bool WinningRegionQueryInterface<ValueType>::staysInWinningRegion(storm::storage::BitVector const& currentBeliefSupport, uint64_t actionIndex) const {
43 STORM_LOG_ASSERT(currentBeliefSupport.getNumberOfSetBits() > 0, "One cannot think one is literally nowhere");
44 std::map<uint32_t, storm::storage::BitVector> successors;
45 STORM_LOG_DEBUG("Stays in winning region? (" << currentBeliefSupport << ", " << actionIndex << ")");
46 for (uint64_t oldState : currentBeliefSupport) {
47 uint64_t row = pomdp.getTransitionMatrix().getRowGroupIndices()[oldState] + actionIndex;
48 for (auto const& successor : pomdp.getTransitionMatrix().getRow(row)) {
49 assert(!storm::utility::isZero(successor.getValue()));
50 uint32_t obs = pomdp.getObservation(successor.getColumn());
51 if (successors.count(obs) == 0) {
52 successors[obs] = storm::storage::BitVector(pomdp.getNumberOfStates());
53 }
54 successors[obs].set(successor.getColumn(), true);
55 }
56 }
57
58 for (auto const& entry : successors) {
59 if (!isInWinningRegion(entry.second)) {
60 STORM_LOG_DEBUG("Belief support " << entry.second << " (obs " << entry.first << ") is not winning");
61 return false;
62 } else {
63 STORM_LOG_DEBUG("Belief support " << entry.second << " (obs " << entry.first << ") is winning");
64 }
65 }
66 return true;
67}
68
69template<typename ValueType>
71 for (uint64_t obs = 0; obs < pomdp.getNrObservations(); ++obs) {
72 for (auto const& winningBelief : winningRegion.getWinningSetsPerObservation(obs)) {
73 storm::storage::BitVector states(pomdp.getNumberOfStates());
74 for (uint64_t offset : winningBelief) {
75 states.set(statesPerObservation[obs][offset]);
76 }
77 bool safeActionExists = false;
78 for (uint64_t actionIndex = 0; actionIndex < pomdp.getTransitionMatrix().getRowGroupSize(statesPerObservation[obs][0]); ++actionIndex) {
79 if (staysInWinningRegion(states, actionIndex)) {
80 safeActionExists = true;
81 break;
82 }
83 }
84 STORM_LOG_THROW(safeActionExists, storm::exceptions::UnexpectedException, "Observation " << obs << " , support " << states);
85 }
86 }
87}
88
89template<typename ValueType>
91 for (uint64_t obs = 0; obs < pomdp.getNrObservations(); ++obs) {
92 STORM_LOG_DEBUG("Check listed belief supports for observation " << obs << " are maximal");
93 for (auto const& winningBelief : winningRegion.getWinningSetsPerObservation(obs)) {
94 storm::storage::BitVector remainders = ~winningBelief;
95 for (auto const additional : remainders) {
96 uint64_t addState = statesPerObservation[obs][additional];
97 if (badStates.get(addState)) {
98 continue;
99 }
100
101 storm::storage::BitVector states(pomdp.getNumberOfStates());
102 for (uint64_t offset : winningBelief) {
103 states.set(statesPerObservation[obs][offset]);
104 }
105 states.set(statesPerObservation[obs][additional]);
106 assert(states.getNumberOfSetBits() == winningBelief.getNumberOfSetBits() + 1);
107
108 bool safeActionExists = false;
109 for (uint64_t actionIndex = 0; actionIndex < pomdp.getTransitionMatrix().getRowGroupSize(statesPerObservation[obs][0]); ++actionIndex) {
110 if (staysInWinningRegion(states, actionIndex)) {
111 STORM_LOG_DEBUG("Action " << actionIndex << " from " << states << " is safe. ");
112 safeActionExists = true;
113 break;
114 }
115 }
116
117 STORM_LOG_THROW(!safeActionExists, storm::exceptions::UnexpectedException, "Observation " << obs << ", support " << states);
118 }
119 }
120 STORM_LOG_DEBUG("All listed belief supports for observation " << obs << " are maximal. Continue with single states.");
121
122 for (uint64_t offset = 0; offset < statesPerObservation[obs].size(); ++offset) {
123 if (winningRegion.isWinning(obs, offset)) {
124 continue;
125 }
126 uint64_t addState = statesPerObservation[obs][offset];
127 if (badStates.get(addState)) {
128 continue;
129 }
130 storm::storage::BitVector states(pomdp.getNumberOfStates());
131 states.set(addState);
132 bool safeActionExists = false;
133 for (uint64_t actionIndex = 0; actionIndex < pomdp.getTransitionMatrix().getRowGroupSize(statesPerObservation[obs][0]); ++actionIndex) {
134 if (staysInWinningRegion(states, actionIndex)) {
135 safeActionExists = true;
136 break;
137 }
138 }
139
140 STORM_LOG_THROW(!safeActionExists, storm::exceptions::UnexpectedException, "Observation " << obs << " , support " << states);
141 }
142 }
143}
144
147} // namespace pomdp
148
149} // namespace storm
virtual uint_fast64_t getNumberOfStates() const override
Returns the number of states of the model.
Definition Model.cpp:162
This class represents a partially observable Markov decision process.
Definition Pomdp.h:15
uint64_t getNrObservations() const
Definition Pomdp.cpp:68
uint32_t getObservation(uint64_t state) const
Definition Pomdp.cpp:63
bool isInWinningRegion(storm::storage::BitVector const &beliefSupport) const
bool staysInWinningRegion(storm::storage::BitVector const &beliefSupport, uint64_t actionIndex) const
WinningRegionQueryInterface(storm::models::sparse::Pomdp< ValueType > const &pomdp, WinningRegion const &winningRegion)
void validateIsMaximal(storm::storage::BitVector const &badStates) const
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:18
uint_fast64_t getNextSetIndex(uint_fast64_t startingIndex) const
Retrieves the index of the bit that is the next bit set to true in the bit vector.
void set(uint_fast64_t index, bool value=true)
Sets the given truth value at the given index.
uint_fast64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
bool get(uint_fast64_t index) const
Retrieves the truth value of the bit at the given index and performs a bound check.
#define STORM_LOG_DEBUG(message)
Definition logging.h:23
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
#define STORM_LOG_THROW(cond, exception, message)
Definition macros.h:30
bool isZero(ValueType const &a)
Definition constants.cpp:41
LabParser.cpp.
Definition cli.cpp:18