gtsam
Loading...
Searching...
No Matches
TableDistribution.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
17
18#pragma once
19
22#include <gtsam/inference/Conditional-inst.h>
23
24#include <memory>
25#include <string>
26#include <vector>
27
28namespace gtsam {
29
39class GTSAM_EXPORT TableDistribution : public DiscreteConditional {
40 private:
41 TableFactor table_;
42
43 typedef Eigen::SparseVector<double>::InnerIterator SparseIt;
44
45 public:
46 // typedefs needed to play nice with gtsam
48 typedef std::shared_ptr<This> shared_ptr;
51
53
56
59
62
67 const std::vector<double>& potentials);
68
73 const std::vector<double>& potentials)
74 : TableDistribution(DiscreteKeys(key), potentials) {}
75
79 TableDistribution(const DiscreteKeys& keys, const std::string& potentials);
80
84 TableDistribution(const DiscreteKey& key, const std::string& potentials)
85 : TableDistribution(DiscreteKeys(key), potentials) {}
86
90
92 void print(
93 const std::string& s = "Table Distribution: ",
94 const KeyFormatter& formatter = DefaultKeyFormatter) const override;
95
97 bool equals(const DiscreteFactor& other, double tol = 1e-9) const override;
98
102
104 TableFactor table() const { return table_; }
105
106 using BaseConditional::evaluate; // HybridValues version
107
109 virtual double evaluate(const Assignment<Key>& values) const override {
110 return table_.evaluate(values);
111 }
112
114 DiscreteFactor::shared_ptr sum(size_t nrFrontals) const override;
115
117 DiscreteFactor::shared_ptr sum(const Ordering& keys) const override;
118
120 double max() const override { return table_.max(); }
121
123 DiscreteFactor::shared_ptr max(size_t nrFrontals) const override;
124
126 DiscreteFactor::shared_ptr max(const Ordering& keys) const override;
127
129 DiscreteFactor::shared_ptr operator*(double s) const override;
130
133 const DiscreteFactor::shared_ptr& f) const override;
134
140 DiscreteValues argmax() const;
141
148 virtual size_t sample(const DiscreteValues& parentsValues,
149 std::mt19937_64* rng = nullptr) const override;
150
154
156 virtual void prune(size_t maxNrAssignments) override;
157
160 return table_.toDecisionTreeFactor();
161 }
162
164 TableFactor toTableFactor() const override { return table_; }
165
167 uint64_t nrValues() const override { return table_.sparseTable().nonZeros(); }
168
170
171 private:
172#if GTSAM_ENABLE_BOOST_SERIALIZATION
174 friend class boost::serialization::access;
175 template <class Archive>
176 void serialize(Archive& ar, const unsigned int /*version*/) {
177 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseConditional);
178 ar& BOOST_SERIALIZATION_NVP(table_);
179 }
180#endif
181};
182// TableDistribution
183
184// traits
185template <>
186struct traits<TableDistribution> : public Testable<TableDistribution> {};
187
188} // namespace gtsam
std::pair< Key, size_t > DiscreteKey
Key type for discrete variables.
Definition DiscreteKey.h:38
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition Point2.h:52
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
Template to create a binary predicate.
Definition Testable.h:112
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
An assignment from labels to value index (size_t).
Definition Assignment.h:37
A discrete probabilistic factor.
Definition DecisionTreeFactor.h:42
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decision tree.
Definition DecisionTreeFactor.h:194
DiscreteConditional()
Default constructor needed for serialization.
Definition DiscreteConditional.h:55
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition DiscreteFactor.h:41
std::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
Definition DiscreteFactor.h:46
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition DiscreteKey.h:41
A map from keys to values.
Definition DiscreteValues.h:34
Distribution which uses a SparseVector as the internal representation, similar to the TableFactor.
Definition TableDistribution.h:39
TableDistribution This
Typedef to this class.
Definition TableDistribution.h:47
DiscreteValues Values
backwards compatibility
Definition TableDistribution.h:52
DiscreteConditional BaseConditional
Typedef to our conditional base class.
Definition TableDistribution.h:50
TableFactor table() const
Return the underlying TableFactor.
Definition TableDistribution.h:104
TableFactor toTableFactor() const override
Get the sparse table representation.
Definition TableDistribution.h:164
virtual double evaluate(const Assignment< Key > &values) const override
Evaluate the conditional given the values.
Definition TableDistribution.h:109
TableDistribution()
Default constructor needed for serialization.
Definition TableDistribution.h:58
double max() const override
Find the maximum value in the factor.
Definition TableDistribution.h:120
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition TableDistribution.h:48
TableDistribution(const DiscreteKey &key, const std::string &potentials)
Construct from single DiscreteKey and std::string.
Definition TableDistribution.h:84
uint64_t nrValues() const override
Get the number of non-zero values.
Definition TableDistribution.h:167
TableDistribution(const DiscreteKey &key, const std::vector< double > &potentials)
Construct from single DiscreteKey and std::vector.
Definition TableDistribution.h:72
DecisionTreeFactor toDecisionTreeFactor() const override
Get a DecisionTreeFactor representation.
Definition TableDistribution.h:159
A discrete probabilistic factor optimized for sparsity.
Definition TableFactor.h:51
Definition Ordering.h:33