Storm 2.0.0.3
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
KnownProbabilityTransformer.cpp
Go to the documentation of this file.
2
4
5namespace storm {
6namespace pomdp {
7namespace transformer {
8
9template<typename ValueType>
13
14template<typename ValueType>
15std::shared_ptr<storm::models::sparse::Pomdp<ValueType>> KnownProbabilityTransformer<ValueType>::transform(storm::models::sparse::Pomdp<ValueType> const &pomdp,
16 storm::storage::BitVector &prob0States,
17 storm::storage::BitVector &prob1States) {
18 std::map<uint64_t, uint64_t> stateMap;
19 std::map<uint32_t, uint32_t> observationMap;
20
21 uint64_t nrNewStates = prob0States.empty() ? 1 : 2;
22
23 storm::models::sparse::StateLabeling newLabeling(pomdp.getNumberOfStates() - prob0States.getNumberOfSetBits() - prob1States.getNumberOfSetBits() +
24 nrNewStates);
25
26 std::vector<uint32_t> newObservations;
27
28 // New state 0 represents all states with probability 1
29 for (uint64_t iter : prob1States) {
30 stateMap[iter] = 0;
31
32 std::set<std::string> labelSet = pomdp.getStateLabeling().getLabelsOfState(iter);
33 for (auto const &label : labelSet) {
34 if (!newLabeling.containsLabel(label)) {
35 newLabeling.addLabel(label);
36 }
37 newLabeling.addLabelToState(label, 0);
38 }
39 }
40 // New state 1 represents all states with probability 0
41 for (uint64_t iter : prob0States) {
42 stateMap[iter] = 1;
43 for (auto const &label : pomdp.getStateLabeling().getLabelsOfState(iter)) {
44 if (!newLabeling.containsLabel(label)) {
45 newLabeling.addLabel(label);
46 }
47 newLabeling.addLabelToState(label, 1);
48 }
49 }
50
51 storm::storage::BitVector unknownStates = ~(prob1States | prob0States);
52 // If there are no states with probability 0 we set the next new state id to be 1, otherwise 2
53 uint64_t newId = prob0States.empty() ? 1 : 2;
54 uint64_t nextObservation = prob0States.empty() ? 1 : 2;
55 for (uint64_t iter : unknownStates) {
56 stateMap[iter] = newId;
57 if (observationMap.count(pomdp.getObservation(iter)) == 0) {
58 observationMap[pomdp.getObservation(iter)] = nextObservation;
59 ++nextObservation;
60 }
61 for (auto const &label : pomdp.getStateLabeling().getLabelsOfState(iter)) {
62 if (!newLabeling.containsLabel(label)) {
63 newLabeling.addLabel(label);
64 }
65 newLabeling.addLabelToState(label, newId);
66 }
67 ++newId;
68 }
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 (uint64_t 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 // The new model has one state per row group (the sink states for probability 1 and 0 as well as all unknown states).
109 auto newTransitionMatrix = smb.build(currentRow, currentRowGroup, currentRowGroup);
110 storm::storage::sparse::ModelComponents<ValueType> components(newTransitionMatrix, newLabeling);
111 components.observabilityClasses = newObservations;
112 if (pomdp.hasChoiceLabeling()) {
113 auto const &originalChoiceLabeling = pomdp.getChoiceLabeling();
114 components.choiceLabeling.emplace(newTransitionMatrix.getRowCount());
115 for (auto const &label : originalChoiceLabeling.getLabels()) {
116 components.choiceLabeling->addLabel(label);
117 }
118 // Retained states keep their local action order. The new absorbing states have synthetic, unlabeled self-loops.
119 for (uint64_t state : unknownStates) {
120 uint64_t const originalRow = transitionMatrix.getRowGroupIndices()[state];
121 uint64_t const newRow = newTransitionMatrix.getRowGroupIndices()[stateMap.at(state)];
122 for (uint64_t localAction = 0; localAction < transitionMatrix.getRowGroupSize(state); ++localAction) {
123 for (auto const &label : originalChoiceLabeling.getLabelsOfChoice(originalRow + localAction)) {
124 components.choiceLabeling->addLabelToChoice(label, newRow + localAction);
125 }
126 }
127 }
128 }
129
130 return std::make_shared<storm::models::sparse::Pomdp<ValueType>>(std::move(components), true);
131}
132
135} // namespace transformer
136} // namespace pomdp
137} // 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.
This class represents a partially observable Markov decision process.
Definition Pomdp.h:13
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::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:16
bool empty() const
Retrieves whether no bits are set to true in this bit vector.
uint64_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)
ValueType one()
Definition constants.cpp:21
The contract of a DD backend.
std::optional< storm::models::sparse::ChoiceLabeling > choiceLabeling
std::optional< std::vector< uint32_t > > observabilityClasses