DecisionTreeFactor.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 
24 #include <gtsam/discrete/Ring.h>
26 
27 #include <algorithm>
28 #include <map>
29 #include <memory>
30 #include <stdexcept>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace gtsam {
36 
37  class DiscreteConditional;
38  class HybridValues;
39 
45  class GTSAM_EXPORT DecisionTreeFactor : public DiscreteFactor,
46  public AlgebraicDecisionTree<Key> {
47  public:
48  // typedefs needed to play nice with gtsam
50  typedef DiscreteFactor Base;
51  typedef std::shared_ptr<DecisionTreeFactor> shared_ptr;
53 
54  // Needed since we have definitions in both DiscreteFactor and DecisionTree
55  using Base::Binary;
56  using Base::Unary;
58 
61 
64 
66  DecisionTreeFactor(const DiscreteKeys& keys, const ADT& potentials);
67 
88  const std::vector<double>& table);
89 
108  DecisionTreeFactor(const DiscreteKeys& keys, const std::string& table);
109 
111  template <class SOURCE>
114 
116  DecisionTreeFactor(const DiscreteKey& key, const std::vector<double>& row)
118 
120  explicit DecisionTreeFactor(const DiscreteConditional& c);
121 
125 
127  bool equals(const DiscreteFactor& other, double tol = 1e-9) const override;
128 
129  // print
130  void print(
131  const std::string& s = "DecisionTreeFactor:\n",
132  const KeyFormatter& formatter = DefaultKeyFormatter) const override;
133 
137 
140  virtual double evaluate(const Assignment<Key>& values) const override {
141  return ADT::operator()(values);
142  }
143 
145  using DiscreteFactor::operator();
146 
148  double error(const DiscreteValues& values) const override;
149 
152  return apply(f, Ring::mul);
153  }
154 
155  static double safe_div(const double& a, const double& b);
156 
159  return apply(f, safe_div);
160  }
161 
163  DecisionTreeFactor toDecisionTreeFactor() const override { return *this; }
164 
166  shared_ptr sum(size_t nrFrontals) const {
167  return combine(nrFrontals, Ring::add);
168  }
169 
171  shared_ptr sum(const Ordering& keys) const {
172  return combine(keys, Ring::add);
173  }
174 
176  shared_ptr max(size_t nrFrontals) const {
177  return combine(nrFrontals, Ring::max);
178  }
179 
181  shared_ptr max(const Ordering& keys) const {
182  return combine(keys, Ring::max);
183  }
184 
188 
193  DecisionTreeFactor apply(Unary op) const;
194 
200  DecisionTreeFactor apply(UnaryAssignment op) const;
201 
207  DecisionTreeFactor apply(const DecisionTreeFactor& f, Binary op) const;
208 
215  shared_ptr combine(size_t nrFrontals, Binary op) const;
216 
223  shared_ptr combine(const Ordering& keys, Binary op) const;
224 
226  std::vector<std::pair<DiscreteValues, double>> enumerate() const;
227 
229  std::vector<double> probabilities() const;
230 
240  double computeThreshold(const size_t N) const;
241 
260  DecisionTreeFactor prune(size_t maxNrAssignments) const;
261 
265 
267  void dot(std::ostream& os,
268  const KeyFormatter& keyFormatter = DefaultKeyFormatter,
269  bool showZero = true) const;
270 
272  void dot(const std::string& name,
273  const KeyFormatter& keyFormatter = DefaultKeyFormatter,
274  bool showZero = true) const;
275 
277  std::string dot(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
278  bool showZero = true) const;
279 
287  std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
288  const Names& names = {}) const override;
289 
297  std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
298  const Names& names = {}) const override;
299 
303 
308  double error(const HybridValues& values) const override;
309 
311 
312  private:
313 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
314 
315  friend class boost::serialization::access;
316  template <class ARCHIVE>
317  void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
318  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
319  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(ADT);
320  }
321 #endif
322  };
323 
324 // traits
325 template <>
326 struct traits<DecisionTreeFactor> : public Testable<DecisionTreeFactor> {};
327 } // namespace gtsam
gtsam::markdown
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition: DiscreteValues.cpp:130
name
Annotation for function names.
Definition: attr.h:51
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:45
gtsam::DecisionTreeFactor::toDecisionTreeFactor
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decision tree.
Definition: DecisionTreeFactor.h:163
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
c
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
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
os
ofstream os("timeSchurFactors.csv")
gtsam::DecisionTreeFactor::shared_ptr
std::shared_ptr< DecisionTreeFactor > shared_ptr
Definition: DecisionTreeFactor.h:51
DiscreteFactor
Discrete values for.
Ring::mul
static double mul(const double &a, const double &b)
Definition: Ring.h:31
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::AlgebraicDecisionTree
Definition: AlgebraicDecisionTree.h:41
gtsam::DecisionTreeFactor::DecisionTreeFactor
DecisionTreeFactor(const DiscreteKey &key, const std::vector< double > &row)
Single-key specialization, with vector of doubles.
Definition: DecisionTreeFactor.h:116
AlgebraicDecisionTree.h
Algebraic Decision Trees.
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:156
gtsam::DecisionTreeFactor::Base
DiscreteFactor Base
Typedef to base class.
Definition: DecisionTreeFactor.h:50
gtsam::DecisionTreeFactor::sum
shared_ptr sum(const Ordering &keys) const
Create new factor by summing all values with the same separator values.
Definition: DecisionTreeFactor.h:171
ADT
AlgebraicDecisionTree< Key > ADT
Definition: testAlgebraicDecisionTree.cpp:32
table
ArrayXXf table(10, 4)
gtsam::row
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Definition: base/Matrix.h:221
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
Ring::max
static double max(const double &a, const double &b)
Definition: Ring.h:28
gtsam::DecisionTreeFactor::This
DecisionTreeFactor This
Definition: DecisionTreeFactor.h:49
gtsam::DecisionTreeFactor::operator*
DecisionTreeFactor operator*(const DecisionTreeFactor &f) const override
multiply two factors
Definition: DecisionTreeFactor.h:151
gtsam::dot
double dot(const V1 &a, const V2 &b)
Definition: Vector.h:196
gtsam::Assignment< Key >
gtsam::DecisionTreeFactor::evaluate
virtual double evaluate(const Assignment< Key > &values) const override
Definition: DecisionTreeFactor.h:140
gtsam::DecisionTreeFactor::operator/
DecisionTreeFactor operator/(const DecisionTreeFactor &f) const
divide by factor f (safely)
Definition: DecisionTreeFactor.h:158
gtsam::DecisionTreeFactor::sum
shared_ptr sum(size_t nrFrontals) const
Create new factor by summing all values with the same separator values.
Definition: DecisionTreeFactor.h:166
gtsam::DiscreteFactor::Binary
std::function< double(const double, const double)> Binary
Definition: DiscreteFactor.h:52
gtsam::DecisionTreeFactor::max
shared_ptr max(const Ordering &keys) const
Create new factor by maximizing over all values with the same separator.
Definition: DecisionTreeFactor.h:181
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::b
const G & b
Definition: Group.h:79
a
ArrayXXi a
Definition: Array_initializer_list_23_cxx11.cpp:1
Ring::add
static double add(const double &a, const double &b)
Definition: Ring.h:27
gtsam
traits
Definition: SFMdata.h:40
gtsam::Testable
Definition: Testable.h:152
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
gtsam::DecisionTreeFactor::max
shared_ptr max(size_t nrFrontals) const
Create new factor by maximizing over all values with the same separator.
Definition: DecisionTreeFactor.h:176
Ring.h
Real Ring definition.
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::tol
const G double tol
Definition: Group.h:79
gtsam::DecisionTreeFactor::ADT
AlgebraicDecisionTree< Key > ADT
Definition: DecisionTreeFactor.h:52
N
#define N
Definition: igam.h:9
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::DiscreteFactor::UnaryAssignment
std::function< double(const Assignment< Key > &, const double &)> UnaryAssignment
Definition: DiscreteFactor.h:51
Base
Definition: test_virtual_functions.cpp:156
gtsam::DiscreteFactor::Unary
std::function< double(const double &)> Unary
Definition: DiscreteFactor.h:49
gtsam::Ordering
Definition: inference/Ordering.h:33
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
gtsam::DecisionTreeFactor::DecisionTreeFactor
DecisionTreeFactor(const DiscreteKey &key, SOURCE table)
Single-key specialization.
Definition: DecisionTreeFactor.h:112


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:01:00