Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
KnownProbabilityTransformer.cpp
Go to the documentation of this file.
2
3namespace storm {
4namespace pomdp {
5namespace transformer {
6
7template<typename ValueType>
11
12template<typename ValueType>
13std::shared_ptr<storm::models::sparse::Pomdp<ValueType>> KnownProbabilityTransformer<ValueType>::transform(storm::models::sparse::Pomdp<ValueType> const &pomdp,
14 storm::storage::BitVector &prob0States,
15 storm::storage::BitVector &prob1States) {
16 std::map<uint64_t, uint64_t> stateMap;
17 std::map<uint32_t, uint32_t> observationMap;
18
19 uint64_t nrNewStates = prob0States.empty() ? 1 : 2;
20
21 storm::models::sparse::StateLabeling newLabeling(pomdp.getNumberOfStates() - prob0States.getNumberOfSetBits() - prob1States.getNumberOfSetBits() +
22 nrNewStates);
23
24 std::vector<uint32_t> newObservations;
25
26 // New state 0 represents all states with probability 1
27 for (auto const iter : prob1States) {
28 stateMap[iter] = 0;
29
30 std::set<std::string> labelSet = pomdp.getStateLabeling().getLabelsOfState(iter);
31 for (auto const &label : labelSet) {
32 if (!newLabeling.containsLabel(label)) {
33 newLabeling.addLabel(label);
34 }
35 newLabeling.addLabelToState(label, 0);
36 }
37 }
38 // New state 1 represents all states with probability 0
39 for (auto const iter : prob0States) {
40 stateMap[iter] = 1;
41 for (auto const &label : pomdp.getStateLabeling().getLabelsOfState(iter)) {
42 if (!newLabeling.containsLabel(label)) {
43 newLabeling.addLabel(label);
44 }
45 newLabeling.addLabelToState(label, 1);
46 }
47 }
48
49 storm::storage::BitVector unknownStates = ~(prob1States | prob0States);
50 // If there are no states with probability 0 we set the next new state id to be 1, otherwise 2
51 uint64_t newId = prob0States.empty() ? 1 : 2;
52 uint64_t nextObservation = prob0States.empty() ? 1 : 2;
53 for (auto const iter : unknownStates) {
54 stateMap[iter] = newId;
55 if (observationMap.count(pomdp.getObservation(iter)) == 0) {
56 observationMap[pomdp.getObservation(iter)] = nextObservation;
57 ++nextObservation;
58 }
59 for (auto const &label : pomdp.getStateLabeling().getLabelsOfState(iter)) {
60 if (!newLabeling.containsLabel(label)) {
61 newLabeling.addLabel(label);
62 }
63 newLabeling.addLabelToState(label, newId);
64 }
65 ++newId;
66 }
67
68 uint64_t newNrOfStates = pomdp.getNumberOfStates() - (prob1States.getNumberOfSetBits() + prob0States.getNumberOfSetBits());
69
70 uint64_t currentRow = 0;
71 uint64_t currentRowGroup = 0;
72 storm::storage::SparseMatrixBuilder<ValueType> smb(0, 0, 0, false, true);
73 // new row for prob 1 state
74 smb.newRowGroup(currentRow);
75 smb.addNextValue(currentRow, 0, storm::utility::one<ValueType>());
76 newObservations.push_back(0);
77 ++currentRowGroup;
78 ++currentRow;
79 if (!prob0States.empty()) {
80 smb.newRowGroup(currentRow);
81 smb.addNextValue(currentRow, 1, storm::utility::one<ValueType>());
82 ++currentRowGroup;
83 ++currentRow;
84 newObservations.push_back(1);
85 }
86
87 auto transitionMatrix = pomdp.getTransitionMatrix();
88
89 for (auto const iter : unknownStates) {
90 smb.newRowGroup(currentRow);
91 // First collect all transitions
92 // auto rowGroup = transitionMatrix.getRowGroup(iter);
93 for (uint64_t row = 0; row < transitionMatrix.getRowGroupSize(iter); ++row) {
94 std::map<uint64_t, ValueType> transitionsInAction;
95 for (auto const &entry : transitionMatrix.getRow(iter, row)) {
96 // here we use the state mapping to collect all probabilities to get to a state with prob 0/1
97 transitionsInAction[stateMap[entry.getColumn()]] += entry.getValue();
98 }
99 for (auto const &transition : transitionsInAction) {
100 smb.addNextValue(currentRow, transition.first, transition.second);
101 }
102 ++currentRow;
103 }
104 ++currentRowGroup;
105 newObservations.push_back(observationMap[pomdp.getObservation(iter)]);
106 }
107
108 auto newTransitionMatrix = smb.build(currentRow, newNrOfStates, currentRowGroup);
109 // STORM_PRINT(newTransitionMatrix)
110 storm::storage::sparse::ModelComponents<ValueType> components(newTransitionMatrix, newLabeling);
111 components.observabilityClasses = newObservations;
112
113 return std::make_shared<storm::models::sparse::Pomdp<ValueType>>(std::move(components), true);
114}
115
118} // namespace transformer
119} // namespace pomdp
120} // namespace storm
void addLabel(std::string const &label)
Adds a new label to the labelings.
bool containsLabel(std::string const &label) const
Checks whether a label is registered within this labeling.
storm::storage::SparseMatrix< ValueType > const & getTransitionMatrix() const
Retrieves the matrix representing the transitions of the model.
Definition Model.cpp:197
storm::models::sparse::StateLabeling const & getStateLabeling() const
Returns the state labeling associated with this model.
Definition Model.cpp:319
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
uint32_t getObservation(uint64_t state) const
Definition Pomdp.cpp:63
This class manages the labeling of the state space with a number of (atomic) labels.
void addLabelToState(std::string const &label, storm::storage::sparse::state_type state)
Adds a label to a given state.
std::set< std::string > getLabelsOfState(storm::storage::sparse::state_type state) const
Retrieves the set of labels attached to the given state.
std::shared_ptr< storm::models::sparse::Pomdp< ValueType > > transform(storm::models::sparse::Pomdp< ValueType > const &pomdp, storm::storage::BitVector &prob0States, storm::storage::BitVector &prob1States)
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:18
bool empty() const
Retrieves whether no bits are set to true in this bit vector.
uint_fast64_t getNumberOfSetBits() const
Returns the number of bits that are set to true in this bit vector.
A class that can be used to build a sparse matrix by adding value by value.
void addNextValue(index_type row, index_type column, value_type const &value)
Sets the matrix entry at the given row and column to the given value.
void newRowGroup(index_type startingRow)
Starts a new row group in the matrix.
SparseMatrix< value_type > build(index_type overriddenRowCount=0, index_type overriddenColumnCount=0, index_type overriddenRowGroupCount=0)
LabParser.cpp.
Definition cli.cpp:18
std::optional< std::vector< uint32_t > > observabilityClasses