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 
18 #pragma once
19 
23 
24 #include <Eigen/Sparse>
25 #include <algorithm>
26 #include <map>
27 #include <memory>
28 #include <stdexcept>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 namespace gtsam {
34 
35 class DiscreteConditional;
36 class HybridValues;
37 
46 class GTSAM_EXPORT TableFactor : public DiscreteFactor {
47  protected:
50 
51  private:
53  std::map<Key, size_t> denominators_;
56 
71  size_t keyValueForIndex(Key target_key, uint64_t index) const;
72 
78  DiscreteKey discreteKey(size_t i) const {
79  return DiscreteKey(keys_[i], cardinalities_.at(keys_[i]));
80  }
81 
84  static Eigen::SparseVector<double> Convert(const std::vector<double>& table);
85 
87  static Eigen::SparseVector<double> Convert(const std::string& table);
88 
89  public:
90  // typedefs needed to play nice with gtsam
91  typedef TableFactor This;
92  typedef DiscreteFactor Base;
93  typedef std::shared_ptr<TableFactor> shared_ptr;
95  typedef std::vector<std::pair<DiscreteValues, double>> AssignValList;
96  using Unary = std::function<double(const double&)>;
97  using UnaryAssignment =
98  std::function<double(const Assignment<Key>&, const double&)>;
99  using Binary = std::function<double(const double, const double)>;
100 
101  public:
103  struct Ring {
104  static inline double zero() { return 0.0; }
105  static inline double one() { return 1.0; }
106  static inline double add(const double& a, const double& b) { return a + b; }
107  static inline double max(const double& a, const double& b) {
108  return std::max(a, b);
109  }
110  static inline double mul(const double& a, const double& b) { return a * b; }
111  static inline double div(const double& a, const double& b) {
112  return (a == 0 || b == 0) ? 0 : (a / b);
113  }
114  static inline double id(const double& x) { return x; }
115  };
116 
119 
121  TableFactor();
122 
124  TableFactor(const DiscreteKeys& keys, const TableFactor& potentials);
125 
129 
131  TableFactor(const DiscreteKeys& keys, const std::vector<double>& table)
132  : TableFactor(keys, Convert(table)) {}
133 
135  TableFactor(const DiscreteKeys& keys, const std::string& table)
136  : TableFactor(keys, Convert(table)) {}
137 
139  template <class SOURCE>
142 
144  TableFactor(const DiscreteKey& key, const std::vector<double>& row)
146 
148  TableFactor(const DiscreteKeys& keys, const DecisionTreeFactor& dtf);
149 
151  TableFactor(const DiscreteKeys& keys, const DecisionTree<Key, double>& dtree);
152 
154  explicit TableFactor(const DiscreteConditional& c);
155 
159 
161  bool equals(const DiscreteFactor& other, double tol = 1e-9) const override;
162 
163  // print
164  void print(
165  const std::string& s = "TableFactor:\n",
166  const KeyFormatter& formatter = DefaultKeyFormatter) const override;
167 
168  // /// @}
169  // /// @name Standard Interface
170  // /// @{
171 
174  double evaluate(const DiscreteValues& values) const {
175  return operator()(values);
176  }
177 
179  double operator()(const DiscreteValues& values) const override;
180 
182  double error(const DiscreteValues& values) const override;
183 
186  return apply(f, Ring::mul);
187  };
188 
190  DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override;
191 
192  static double safe_div(const double& a, const double& b);
193 
196  return apply(f, safe_div);
197  }
198 
200  DecisionTreeFactor toDecisionTreeFactor() const override;
201 
203  TableFactor choose(const DiscreteValues assignments,
204  DiscreteKeys parent_keys) const;
205 
207  shared_ptr sum(size_t nrFrontals) const {
208  return combine(nrFrontals, Ring::add);
209  }
210 
212  shared_ptr sum(const Ordering& keys) const {
213  return combine(keys, Ring::add);
214  }
215 
217  shared_ptr max(size_t nrFrontals) const {
218  return combine(nrFrontals, Ring::max);
219  }
220 
222  shared_ptr max(const Ordering& keys) const {
223  return combine(keys, Ring::max);
224  }
225 
229 
234  TableFactor apply(Unary op) const;
240  TableFactor apply(UnaryAssignment op) const;
241 
247  TableFactor apply(const TableFactor& f, Binary op) const;
248 
256  DiscreteKeys contractDkeys(const TableFactor& f) const;
257 
262  DiscreteKeys freeDkeys(const TableFactor& f) const;
263 
265  DiscreteKeys unionDkeys(const TableFactor& f) const;
266 
268  uint64_t unionRep(const DiscreteKeys& keys, const DiscreteValues& assign,
269  const uint64_t idx) const;
270 
273  std::unordered_map<uint64_t, AssignValList> createMap(
274  const DiscreteKeys& contract, const DiscreteKeys& free) const;
275 
277  uint64_t uniqueRep(const DiscreteKeys& keys, const uint64_t idx) const;
278 
280  uint64_t uniqueRep(const DiscreteValues& assignments) const;
281 
283  DiscreteValues findAssignments(const uint64_t idx) const;
284 
286  double findValue(const DiscreteValues& values) const;
287 
294  shared_ptr combine(size_t nrFrontals, Binary op) const;
295 
302  shared_ptr combine(const Ordering& keys, Binary op) const;
303 
305  std::vector<std::pair<DiscreteValues, double>> enumerate() const;
306 
325  TableFactor prune(size_t maxNrAssignments) const;
326 
330 
338  std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
339  const Names& names = {}) const override;
340 
348  std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
349  const Names& names = {}) const override;
350 
354 
359  double error(const HybridValues& values) const override;
360 
362 };
363 
364 // traits
365 template <>
366 struct traits<TableFactor> : public Testable<TableFactor> {};
367 } // namespace gtsam
gtsam::TableFactor
Definition: TableFactor.h:46
gtsam::TableFactor::Ring::zero
static double zero()
Definition: TableFactor.h:104
gtsam::markdown
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition: DiscreteValues.cpp:130
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:44
gtsam::TableFactor::max
shared_ptr max(size_t nrFrontals) const
Create new factor by maximizing over all values with the same separator.
Definition: TableFactor.h:217
gtsam::TableFactor::This
TableFactor This
Definition: TableFactor.h:91
gtsam::TableFactor::Ring::max
static double max(const double &a, const double &b)
Definition: TableFactor.h:107
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
gtsam::TableFactor::operator/
TableFactor operator/(const TableFactor &f) const
divide by factor f (safely)
Definition: TableFactor.h:195
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
gtsam::TableFactor::sparse_table_
Eigen::SparseVector< double > sparse_table_
SparseVector of nonzero probabilities.
Definition: TableFactor.h:49
gtsam::TableFactor::TableFactor
TableFactor(const DiscreteKeys &keys, const std::vector< double > &table)
Definition: TableFactor.h:131
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
gtsam::TableFactor::sum
shared_ptr sum(size_t nrFrontals) const
Create new factor by summing all values with the same separator values.
Definition: TableFactor.h:207
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::TableFactor::Ring
Definition: TableFactor.h:103
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
Ordering.h
Variable ordering for the elimination algorithm.
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
HybridValues
gtsam::TableFactor::Base
DiscreteFactor Base
Typedef to base class.
Definition: TableFactor.h:92
DiscreteFactor
Discrete values for.
Eigen::SparseCompressedBase::InnerIterator
Definition: SparseCompressedBase.h:158
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::operator*
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
gtsam::TableFactor::TableFactor
TableFactor(const DiscreteKeys &keys, const std::string &table)
Definition: TableFactor.h:135
gtsam::TableFactor::Ring::id
static double id(const double &x)
Definition: TableFactor.h:114
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
gtsam::TableFactor::sorted_dkeys_
DiscreteKeys sorted_dkeys_
Sorted DiscreteKeys to use internally.
Definition: TableFactor.h:55
table
ArrayXXf table(10, 4)
gtsam::row
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Definition: base/Matrix.h:221
gtsam::TableFactor::TableFactor
TableFactor(const DiscreteKey &key, SOURCE table)
Single-key specialization.
Definition: TableFactor.h:140
operator()
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
Definition: IndexedViewMethods.h:73
gtsam::KeyFormatter
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
DiscreteFactor.h
gtsam::TableFactor::AssignValList
std::vector< std::pair< DiscreteValues, double > > AssignValList
Definition: TableFactor.h:95
gtsam::Assignment< Key >
Ring::add
static int add(const int &a, const int &b)
Definition: testDecisionTree.cpp:131
gtsam::TableFactor::Ring::div
static double div(const double &a, const double &b)
Definition: TableFactor.h:111
gtsam::TableFactor::max
shared_ptr max(const Ordering &keys) const
Create new factor by maximizing over all values with the same separator.
Definition: TableFactor.h:222
gtsam::TableFactor::TableFactor
TableFactor(const DiscreteKey &key, const std::vector< double > &row)
Single-key specialization, with vector of doubles.
Definition: TableFactor.h:144
gtsam::TableFactor::sum
shared_ptr sum(const Ordering &keys) const
Create new factor by summing all values with the same separator values.
Definition: TableFactor.h:212
DiscreteKey.h
specialized key for discrete variables
key
const gtsam::Symbol key('X', 0)
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
process_shonan_timing_results.names
dictionary names
Definition: process_shonan_timing_results.py:175
gtsam::TableFactor::Binary
std::function< double(const double, const double)> Binary
Definition: TableFactor.h:99
gtsam::b
const G & b
Definition: Group.h:79
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
gtsam::TableFactor::Unary
std::function< double(const double &)> Unary
Definition: TableFactor.h:96
gtsam::TableFactor::UnaryAssignment
std::function< double(const Assignment< Key > &, const double &)> UnaryAssignment
Definition: TableFactor.h:98
gtsam::TableFactor::operator*
TableFactor operator*(const TableFactor &f) const
multiply two TableFactors
Definition: TableFactor.h:185
gtsam
traits
Definition: SFMdata.h:40
gtsam::Testable
Definition: Testable.h:152
gtsam::DiscreteFactor::shared_ptr
std::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
Definition: DiscreteFactor.h:44
error
static double error
Definition: testRot3.cpp:37
gtsam::traits
Definition: Group.h:36
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
Eigen::SparseVector< double >
gtsam::TableFactor::denominators_
std::map< Key, size_t > denominators_
Map of Keys and their denominators used in keyValueForIndex.
Definition: TableFactor.h:53
gtsam::TableFactor::evaluate
double evaluate(const DiscreteValues &values) const
Definition: TableFactor.h:174
gtsam::apply
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const typename DecisionTree< L, Y >::Unary &op)
Apply unary operator op to DecisionTree f.
Definition: DecisionTree.h:460
gtsam::TableFactor::Ring::one
static double one()
Definition: TableFactor.h:105
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::TableFactor::SparseIt
Eigen::SparseVector< double >::InnerIterator SparseIt
Definition: TableFactor.h:94
gtsam::TableFactor::discreteKey
DiscreteKey discreteKey(size_t i) const
Return ith key in keys_ as a DiscreteKey.
Definition: TableFactor.h:78
uint64_t
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
gtsam::html
string html(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of html.
Definition: DiscreteValues.cpp:135
gtsam::DiscreteFactor
Definition: DiscreteFactor.h:39
gtsam::TableFactor::Ring::mul
static double mul(const double &a, const double &b)
Definition: TableFactor.h:110
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
max
#define max(a, b)
Definition: datatypes.h:20
gtsam::Ordering
Definition: inference/Ordering.h:33
choose
static const T & choose(int layout, const T &col, const T &row)
Definition: cxx11_tensor_block_access.cpp:27
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
gtsam::TableFactor::Ring::add
static double add(const double &a, const double &b)
Definition: TableFactor.h:106
gtsam::TableFactor::shared_ptr
std::shared_ptr< TableFactor > shared_ptr
Definition: TableFactor.h:93
Ring::mul
static int mul(const int &a, const int &b)
Definition: testDecisionTree.cpp:132


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:05:22