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