Storm 1.14.0.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
BeliefManager.h
Go to the documentation of this file.
1#pragma once
2
3#include <boost/container/flat_map.hpp>
4#include <boost/container/flat_set.hpp>
5#include <optional>
6#include <unordered_map>
7#include <vector>
8
14
15namespace storm {
16namespace storage {
17// Forward declaration
18template<typename ValueType>
19class SparseMatrix;
20
21template<typename PomdpType, typename BeliefValueType = typename PomdpType::ValueType, typename StateType = uint64_t>
23 public:
24 typedef typename PomdpType::ValueType ValueType;
25 typedef boost::container::flat_map<StateType, BeliefValueType> BeliefType; // iterating over this shall be ordered (for correct hash computation)
26 typedef boost::container::flat_set<StateType> BeliefSupportType;
27 typedef uint64_t BeliefId;
28
30
31 BeliefManager(PomdpType const &pomdp, BeliefValueType const &precision, TriangulationMode const &triangulationMode);
32
33 void setRewardModel(std::optional<std::string> rewardModelName = std::nullopt);
34
36
38 std::vector<BeliefId> gridPoints;
39 std::vector<BeliefValueType> weights;
40 uint64_t size() const;
41 };
42
51
52 BeliefId noId() const;
53
54 bool isEqual(BeliefId const &first, BeliefId const &second) const;
55
56 std::string toString(BeliefId const &beliefId) const;
57
58 std::string toString(Triangulation const &t) const;
59
60 ValueType getWeightedSum(BeliefId const &beliefId, std::vector<ValueType> const &summands);
61
62 std::pair<bool, ValueType> getWeightedSum(BeliefId const &beliefId, std::unordered_map<StateType, ValueType> const &summands);
63
64 BeliefId const &getInitialBelief() const;
65
66 ValueType getBeliefActionReward(BeliefId const &beliefId, uint64_t const &localActionIndex) const;
67
68 uint32_t getBeliefObservation(BeliefId beliefId);
69
71
77 uint64_t getRepresentativeState(BeliefId const &beliefId);
78
79 Triangulation triangulateBelief(BeliefId beliefId, BeliefValueType resolution);
80
81 template<typename DistributionType>
82 void addToDistribution(DistributionType &distr, StateType const &state, BeliefValueType const &value);
83
84 void joinSupport(BeliefId const &beliefId, BeliefSupportType &support);
85
87
88 std::vector<std::pair<BeliefId, ValueType>> expandAndTriangulate(storm::Environment const &env, BeliefId const &beliefId, uint64_t actionIndex,
89 std::vector<BeliefValueType> const &observationResolutions);
90
91 std::vector<std::pair<BeliefId, ValueType>> expandAndClip(storm::Environment const &env, BeliefId const &beliefId, uint64_t actionIndex,
92 std::vector<uint64_t> const &observationResolutions);
93
94 std::vector<std::pair<BeliefId, ValueType>> expand(storm::Environment const &env, BeliefId const &beliefId, uint64_t actionIndex);
95
96 BeliefClipping clipBeliefToGrid(storm::Environment const &env, BeliefId const &beliefId, uint64_t resolution,
98
99 std::string getObservationLabel(BeliefId const &beliefId);
100
101 std::vector<BeliefValueType> computeMatrixBeliefProduct(BeliefId const &beliefId, storm::storage::SparseMatrix<BeliefValueType> &matrix);
102
103 private:
104 std::vector<BeliefValueType> getBeliefAsVector(BeliefId const &beliefId);
105
106 std::vector<BeliefValueType> getBeliefAsVector(const BeliefType &belief);
107
108 BeliefClipping clipBeliefToGrid(storm::Environment const &env, BeliefType const &belief, uint64_t resolution, const storm::storage::BitVector &isInfinite);
109
110 template<typename DistributionType>
111 void adjustDistribution(DistributionType &distr);
112
113 struct BeliefHash {
114 std::size_t operator()(const BeliefType &belief) const;
115 };
116
117 struct Belief_equal_to {
118 bool operator()(const BeliefType &lhBelief, const BeliefType &rhBelief) const;
119 };
120
121 struct FreudenthalDiff {
122 FreudenthalDiff(StateType const &dimension, BeliefValueType diff);
123
124 StateType dimension; // i
125 BeliefValueType diff; // d[i]
126 bool operator>(FreudenthalDiff const &other) const;
127 };
128
129 BeliefType const &getBelief(BeliefId const &id) const;
130
131 BeliefId getId(BeliefType const &belief) const;
132
133 std::string toString(BeliefType const &belief) const;
134
135 bool isEqual(BeliefType const &first, BeliefType const &second) const;
136
137 bool assertBelief(BeliefType const &belief) const;
138
139 bool assertTriangulation(BeliefType const &belief, Triangulation const &triangulation) const;
140
141 uint32_t getBeliefObservation(BeliefType belief) const;
142
143 void triangulateBeliefFreudenthal(BeliefType const &belief, BeliefValueType const &resolution, Triangulation &result);
144
145 void triangulateBeliefDynamic(BeliefType const &belief, BeliefValueType const &resolution, Triangulation &result);
146
147 Triangulation triangulateBelief(BeliefType const &belief, BeliefValueType const &resolution);
148
149 std::vector<std::pair<BeliefId, ValueType>> expandInternal(
150 storm::Environment const &env, BeliefId const &beliefId, uint64_t actionIndex,
151 std::optional<std::vector<BeliefValueType>> const &observationTriangulationResolutions = std::nullopt,
152 std::optional<std::vector<uint64_t>> const &observationGridClippingResolutions = std::nullopt);
153
154 BeliefId computeInitialBelief();
155
156 BeliefId getOrAddBeliefId(BeliefType const &belief);
157
158 PomdpType const &pomdp;
159 std::vector<ValueType> pomdpActionRewardVector;
160
161 std::vector<BeliefType> beliefs;
162 std::vector<std::unordered_map<BeliefType, BeliefId, BeliefHash, Belief_equal_to>> beliefToIdMap;
163 BeliefId initialBeliefId;
164
166
167 std::shared_ptr<storm::solver::LpSolver<BeliefValueType>> lpSolver;
168
169 TriangulationMode triangulationMode;
170};
171} // namespace storage
172} // namespace storm
std::vector< BeliefValueType > computeMatrixBeliefProduct(BeliefId const &beliefId, storm::storage::SparseMatrix< BeliefValueType > &matrix)
std::pair< bool, ValueType > getWeightedSum(BeliefId const &beliefId, std::unordered_map< StateType, ValueType > const &summands)
void joinSupport(BeliefId const &beliefId, BeliefSupportType &support)
boost::container::flat_set< uint64_t > BeliefSupportType
BeliefManager(PomdpType const &pomdp, BeliefValueType const &precision, TriangulationMode const &triangulationMode)
boost::container::flat_map< uint64_t, BeliefValueType > BeliefType
std::string toString(Triangulation const &t) const
uint32_t getBeliefObservation(BeliefId beliefId)
void setRewardModel(std::optional< std::string > rewardModelName=std::nullopt)
std::string toString(BeliefId const &beliefId) const
uint64_t getBeliefNumberOfChoices(BeliefId beliefId)
uint64_t getRepresentativeState(BeliefId const &beliefId)
Returns the first state in the belief as a representative.
BeliefId const & getInitialBelief() const
ValueType getBeliefActionReward(BeliefId const &beliefId, uint64_t const &localActionIndex) const
std::vector< std::pair< BeliefId, ValueType > > expandAndTriangulate(storm::Environment const &env, BeliefId const &beliefId, uint64_t actionIndex, std::vector< BeliefValueType > const &observationResolutions)
BeliefId getNumberOfBeliefIds() const
void addToDistribution(DistributionType &distr, StateType const &state, BeliefValueType const &value)
std::string getObservationLabel(BeliefId const &beliefId)
BeliefClipping clipBeliefToGrid(storm::Environment const &env, BeliefId const &beliefId, uint64_t resolution, storm::storage::BitVector isInfinite=storm::storage::BitVector())
ValueType getWeightedSum(BeliefId const &beliefId, std::vector< ValueType > const &summands)
std::vector< std::pair< BeliefId, ValueType > > expandAndClip(storm::Environment const &env, BeliefId const &beliefId, uint64_t actionIndex, std::vector< uint64_t > const &observationResolutions)
bool isEqual(BeliefId const &first, BeliefId const &second) const
std::vector< std::pair< BeliefId, ValueType > > expand(storm::Environment const &env, BeliefId const &beliefId, uint64_t actionIndex)
Triangulation triangulateBelief(BeliefId beliefId, BeliefValueType resolution)
A bit vector that is internally represented as a vector of 64-bit values.
Definition BitVector.h:16
A class that holds a possibly non-square matrix in the compressed row storage format.
std::vector< BeliefValueType > weights