DiscreteFactor.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 
19 #pragma once
20 
21 #include <gtsam/base/Testable.h>
24 #include <gtsam/inference/Factor.h>
25 
26 #include <string>
27 namespace gtsam {
28 
29 class DecisionTreeFactor;
30 class DiscreteConditional;
31 class HybridValues;
32 
39 class GTSAM_EXPORT DiscreteFactor : public Factor {
40  public:
41  // typedefs needed to play nice with gtsam
42  typedef DiscreteFactor This;
43  typedef std::shared_ptr<DiscreteFactor>
45  typedef Factor Base;
46 
48 
49  using Unary = std::function<double(const double&)>;
50  using UnaryAssignment =
51  std::function<double(const Assignment<Key>&, const double&)>;
52  using Binary = std::function<double(const double, const double)>;
53 
54  protected:
56  std::map<Key, size_t> cardinalities_;
57 
58  public:
61 
64 
70  template <typename CONTAINER>
71  DiscreteFactor(const CONTAINER& keys,
72  const std::map<Key, size_t> cardinalities = {})
73  : Base(keys), cardinalities_(cardinalities) {}
74 
78 
80  virtual bool equals(const DiscreteFactor& lf, double tol = 1e-9) const;
81 
83  void print(
84  const std::string& s = "DiscreteFactor\n",
85  const KeyFormatter& formatter = DefaultKeyFormatter) const override {
87  }
88 
92 
94  DiscreteKeys discreteKeys() const;
95 
96  std::map<Key, size_t> cardinalities() const { return cardinalities_; }
97 
98  size_t cardinality(Key j) const { return cardinalities_.at(j); }
99 
109  virtual double evaluate(const Assignment<Key>& values) const = 0;
110 
112  double operator()(const DiscreteValues& values) const {
113  return evaluate(values);
114  }
115 
117  virtual double error(const DiscreteValues& values) const;
118 
123  double error(const HybridValues& c) const override;
124 
126  virtual AlgebraicDecisionTree<Key> errorTree() const;
127 
130  virtual DecisionTreeFactor operator*(const DecisionTreeFactor&) const = 0;
131 
132  virtual DecisionTreeFactor toDecisionTreeFactor() const = 0;
133 
137 
140 
148  virtual std::string markdown(
149  const KeyFormatter& keyFormatter = DefaultKeyFormatter,
150  const Names& names = {}) const = 0;
151 
159  virtual std::string html(
160  const KeyFormatter& keyFormatter = DefaultKeyFormatter,
161  const Names& names = {}) const = 0;
162 
164 
165  private:
166 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
167 
168  friend class boost::serialization::access;
169  template <class ARCHIVE>
170  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
171  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
172  ar& BOOST_SERIALIZATION_NVP(cardinalities_);
173  }
174 #endif
175 };
176 // DiscreteFactor
177 
178 // traits
179 template <>
180 struct traits<DiscreteFactor> : public Testable<DiscreteFactor> {};
181 
198 std::vector<double> expNormalize(const std::vector<double>& logProbs);
199 
200 } // namespace gtsam
Eigen::internal::print
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Definition: NEON/PacketMath.h:3115
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:45
gtsam::HybridValues
Definition: HybridValues.h:37
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
gtsam::DiscreteFactor::cardinalities
std::map< Key, size_t > cardinalities() const
Definition: DiscreteFactor.h:96
Testable.h
Concept check for values that can be used in unit tests.
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
gtsam::DiscreteFactor::cardinalities_
std::map< Key, size_t > cardinalities_
Map of Keys and their cardinalities.
Definition: DiscreteFactor.h:56
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
gtsam::Factor
Definition: Factor.h:70
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
HybridValues
DiscreteFactor
Discrete values for.
Factor.h
The base class for all factors.
gtsam::DiscreteFactor::DiscreteFactor
DiscreteFactor()
Definition: DiscreteFactor.h:63
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::AlgebraicDecisionTree< Key >
gtsam::operator*
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
AlgebraicDecisionTree.h
Algebraic Decision Trees.
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
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
gtsam::DiscreteFactor::print
void print(const std::string &s="DiscreteFactor\n", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
Definition: DiscreteFactor.h:83
gtsam::DiscreteFactor::This
DiscreteFactor This
This class.
Definition: DiscreteFactor.h:42
gtsam::Assignment< Key >
gtsam::DiscreteFactor::DiscreteFactor
DiscreteFactor(const CONTAINER &keys, const std::map< Key, size_t > cardinalities={})
Definition: DiscreteFactor.h:71
gtsam::DiscreteFactor::Binary
std::function< double(const double, const double)> Binary
Definition: DiscreteFactor.h:52
gtsam::DiscreteFactor::cardinality
size_t cardinality(Key j) const
Definition: DiscreteFactor.h:98
process_shonan_timing_results.names
dictionary names
Definition: process_shonan_timing_results.py:175
gtsam
traits
Definition: SFMdata.h:40
gtsam::Testable
Definition: Testable.h:152
DiscreteValues.h
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::DiscreteFactor::Names
DiscreteValues::Names Names
Translation table from values to strings.
Definition: DiscreteFactor.h:139
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::expNormalize
std::vector< double > expNormalize(const std::vector< double > &logProbs)
Normalize a set of log probabilities.
Definition: DiscreteFactor.cpp:75
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::DiscreteFactor::Base
Factor Base
Our base class.
Definition: DiscreteFactor.h:45
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::DiscreteValues::Names
std::map< Key, std::vector< std::string > > Names
Translation table from values to strings.
Definition: DiscreteValues.h:95
gtsam::DiscreteFactor::UnaryAssignment
std::function< double(const Assignment< Key > &, const double &)> UnaryAssignment
Definition: DiscreteFactor.h:51
Base
Definition: test_virtual_functions.cpp:156
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::DiscreteFactor::Unary
std::function< double(const double &)> Unary
Definition: DiscreteFactor.h:49
gtsam::DiscreteFactor::operator()
double operator()(const DiscreteValues &values) const
Find value for given assignment of values to variables.
Definition: DiscreteFactor.h:112


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:28