gtsam
Loading...
Searching...
No Matches
TableFactor.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
23#include <gtsam/discrete/Ring.h>
24
25#include <Eigen/Sparse>
26#include <map>
27#include <memory>
28#include <string>
29#include <utility>
30#include <vector>
31
32#if GTSAM_ENABLE_BOOST_SERIALIZATION
34
35#include <boost/serialization/nvp.hpp>
36#endif
37
38namespace gtsam {
39
41class HybridValues;
42
51class GTSAM_EXPORT TableFactor : public DiscreteFactor {
52 protected:
54 Eigen::SparseVector<double> sparse_table_;
55
56 private:
58 std::map<Key, size_t> denominators_;
60 DiscreteKeys sorted_dkeys_;
61
76 size_t keyValueForIndex(Key target_key, uint64_t index) const;
77
83 DiscreteKey discreteKey(size_t i) const {
84 return DiscreteKey(keys_[i], cardinalities_.at(keys_[i]));
85 }
86
87 public:
92 static Eigen::SparseVector<double> Convert(const DiscreteKeys& keys,
93 const std::vector<double>& table);
94
96 static Eigen::SparseVector<double> Convert(const DiscreteKeys& keys,
97 const std::string& table);
98
99 // typedefs needed to play nice with gtsam
100 typedef TableFactor This;
102 typedef std::shared_ptr<TableFactor> shared_ptr;
103 typedef Eigen::SparseVector<double>::InnerIterator SparseIt;
104 typedef std::vector<std::pair<DiscreteValues, double>> AssignValList;
105
108
110 TableFactor();
111
113 TableFactor(const DiscreteKeys& keys, const TableFactor& potentials);
114
117 const Eigen::SparseVector<double>& table);
118
120 TableFactor(const DiscreteKeys& keys, const std::vector<double>& table)
121 : TableFactor(keys, Convert(keys, table)) {}
122
124 TableFactor(const DiscreteKeys& keys, const std::string& table)
125 : TableFactor(keys, Convert(keys, table)) {}
126
128 template <class SOURCE>
129 TableFactor(const DiscreteKey& key, SOURCE table)
130 : TableFactor(DiscreteKeys{key}, table) {}
131
133 TableFactor(const DiscreteKey& key, const std::vector<double>& row)
134 : TableFactor(DiscreteKeys{key}, row) {}
135
137 TableFactor(const DiscreteKeys& keys, const DecisionTreeFactor& dtf);
139
141 TableFactor(const DiscreteKeys& keys, const DecisionTree<Key, double>& dtree);
142
144 explicit TableFactor(const DiscreteConditional& c);
145
149
151 bool equals(const DiscreteFactor& other, double tol = 1e-9) const override;
152
153 // print
154 void print(
155 const std::string& s = "TableFactor:\n",
156 const KeyFormatter& formatter = DefaultKeyFormatter) const override;
157
158 // /// @}
159 // /// @name Standard Interface
160 // /// @{
161
163 Eigen::SparseVector<double> sparseTable() const { return sparse_table_; }
164
166 double evaluate(const Assignment<Key>& values) const override;
167
169 double error(const DiscreteValues& values) const override;
170
172 DiscreteFactor::shared_ptr operator*(double s) const override {
173 return std::make_shared<TableFactor>(
174 apply([s](const double& a) { return Ring::mul(a, s); }));
175 }
176
179 return apply(f, Ring::mul);
180 };
181
183 DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override;
184
199 virtual DiscreteFactor::shared_ptr multiply(
200 const DiscreteFactor::shared_ptr& f) const override;
201
202 static double safe_div(const double& a, const double& b);
203
206 return apply(f, safe_div);
207 }
208
211 const DiscreteFactor::shared_ptr& f) const override;
212
214 DecisionTreeFactor toDecisionTreeFactor() const override;
215
217 TableFactor toTableFactor() const override { return *this; }
218
220 TableFactor choose(const DiscreteValues parentAssignments,
221 DiscreteKeys parent_keys) const;
222
224 DiscreteFactor::shared_ptr sum(size_t nrFrontals) const override;
225
227 DiscreteFactor::shared_ptr sum(const Ordering& keys) const override;
228
230 double max() const override;
231
233 DiscreteFactor::shared_ptr max(size_t nrFrontals) const override;
234
236 DiscreteFactor::shared_ptr max(const Ordering& keys) const override;
237
241
246 TableFactor apply(Unary op) const;
252 TableFactor apply(UnaryAssignment op) const;
253
259 TableFactor apply(const TableFactor& f, Binary op) const;
260
262 uint64_t unionRep(const DiscreteKeys& keys, const DiscreteValues& assign,
263 const uint64_t idx) const;
264
267 std::unordered_map<uint64_t, AssignValList> createMap(
268 const DiscreteKeys& contract, const DiscreteKeys& free) const;
269
271 uint64_t uniqueRep(const DiscreteKeys& keys, const uint64_t idx) const;
272
274 uint64_t uniqueRep(const DiscreteValues& assignments) const;
275
277 DiscreteValues findAssignments(const uint64_t idx) const;
278
280 double findValue(const DiscreteValues& values) const;
281
288 shared_ptr combine(size_t nrFrontals, Binary op) const;
289
296 shared_ptr combine(const Ordering& keys, Binary op) const;
297
299 std::vector<std::pair<DiscreteValues, double>> enumerate() const;
300
319 TableFactor prune(size_t maxNrAssignments) const;
320
325 uint64_t nrValues() const override { return sparse_table_.nonZeros(); }
326
329 const DiscreteValues& assignment) const override;
330
334
342 std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
343 const Names& names = {}) const override;
344
352 std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
353 const Names& names = {}) const override;
354
358
363 double error(const HybridValues& values) const override;
364
366
367#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V43
370
373 DiscreteKeys contractDkeys(const TableFactor& factor) const;
374
377 DiscreteKeys freeDkeys(const TableFactor& factor) const;
378
381 DiscreteKeys unionDkeys(const TableFactor& factor) const;
382
384#endif
385
386 private:
387#if GTSAM_ENABLE_BOOST_SERIALIZATION
389 friend class boost::serialization::access;
390 template <class ARCHIVE>
391 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
392 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
393 ar& BOOST_SERIALIZATION_NVP(sparse_table_);
394 ar& BOOST_SERIALIZATION_NVP(denominators_);
395 ar& BOOST_SERIALIZATION_NVP(sorted_dkeys_);
396 }
397#endif
398};
399
400// traits
401template <>
402struct traits<TableFactor> : public Testable<TableFactor> {};
403} // namespace gtsam
Serialization for matrices.
specialized key for discrete variables
Real Ring definition.
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
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition DiscreteValues.cpp:155
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
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const typename DecisionTree< L, Y >::Unary &op)
free versions of apply
Definition DecisionTree.h:467
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
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 decision tree is a function from assignments to values.
Definition DecisionTree.h:62
A discrete probabilistic factor.
Definition DecisionTreeFactor.h:42
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition DiscreteConditional.h:40
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
std::map< Key, size_t > cardinalities_
Map of Keys and their cardinalities.
Definition DiscreteFactor.h:58
DiscreteFactor()
Default constructor creates empty factor.
Definition DiscreteFactor.h:65
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
A discrete probabilistic factor optimized for sparsity.
Definition TableFactor.h:51
TableFactor operator*(const TableFactor &f) const
multiply two TableFactors
Definition TableFactor.h:178
TableFactor(const DiscreteKey &key, SOURCE table)
Single-key specialization.
Definition TableFactor.h:129
TableFactor(const DiscreteKeys &keys, const std::string &table)
Constructor from string.
Definition TableFactor.h:124
TableFactor apply(Unary op) const
Apply unary operator op(*this) where op accepts the discrete value.
Definition TableFactor.cpp:506
TableFactor(const DiscreteKey &key, const std::vector< double > &row)
Single-key specialization, with vector of doubles.
Definition TableFactor.h:133
TableFactor(const DiscreteKeys &keys, const std::vector< double > &table)
Constructor from doubles.
Definition TableFactor.h:120
Eigen::SparseVector< double > sparse_table_
SparseVector of nonzero probabilities.
Definition TableFactor.h:54
TableFactor operator/(const TableFactor &f) const
divide by factor f (safely)
Definition TableFactor.h:205
DiscreteFactor::shared_ptr operator*(double s) const override
multiply with a scalar
Definition TableFactor.h:172
TableFactor()
Default constructor for I/O.
Definition TableFactor.cpp:118
DiscreteFactor Base
Typedef to base class.
Definition TableFactor.h:101
TableFactor toTableFactor() const override
Return this sparse table representation.
Definition TableFactor.h:217
uint64_t nrValues() const override
Get the number of non-zero values contained in this factor.
Definition TableFactor.h:325
Eigen::SparseVector< double > sparseTable() const
Getter for the underlying sparse vector.
Definition TableFactor.h:163
static Eigen::SparseVector< double > Convert(const DiscreteKeys &keys, const std::vector< double > &table)
Convert probability table given as doubles to SparseVector.
Definition TableFactor.cpp:257
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:37
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
KeyVector keys_
The keys involved in this factor.
Definition Factor.h:88
Definition Ordering.h:33