gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
25 #include <boost/foreach.hpp>
26 #include <boost/shared_ptr.hpp>
27 
28 #include <vector>
29 #include <exception>
30 #include <stdexcept>
31 
32 namespace gtsam {
33 
34  class DiscreteConditional;
35 
39  class GTSAM_EXPORT DecisionTreeFactor: public DiscreteFactor, public Potentials {
40 
41  public:
42 
43  // typedefs needed to play nice with gtsam
44  typedef DecisionTreeFactor This;
45  typedef DiscreteFactor Base;
46  typedef boost::shared_ptr<DecisionTreeFactor> shared_ptr;
47 
48  public:
49 
52 
55 
57  DecisionTreeFactor(const DiscreteKeys& keys, const ADT& potentials);
58 
60  template<class SOURCE>
61  DecisionTreeFactor(const DiscreteKeys& keys, SOURCE table) :
62  DiscreteFactor(keys.indices()), Potentials(keys, table) {
63  }
64 
67 
71 
73  bool equals(const DiscreteFactor& other, double tol = 1e-9) const;
74 
75  // print
76  virtual void print(const std::string& s = "DecisionTreeFactor:\n",
77  const KeyFormatter& formatter = DefaultKeyFormatter) const;
78 
82 
84  virtual double operator()(const Values& values) const {
85  return Potentials::operator()(values);
86  }
87 
90  return apply(f, ADT::Ring::mul);
91  }
92 
95  return apply(f, safe_div);
96  }
97 
100  return *this;
101  }
102 
104  shared_ptr sum(size_t nrFrontals) const {
105  return combine(nrFrontals, ADT::Ring::add);
106  }
107 
109  shared_ptr sum(const Ordering& keys) const {
110  return combine(keys, ADT::Ring::add);
111  }
112 
114  shared_ptr max(size_t nrFrontals) const {
115  return combine(nrFrontals, ADT::Ring::max);
116  }
117 
121 
127  DecisionTreeFactor apply(const DecisionTreeFactor& f, ADT::Binary op) const;
128 
135  shared_ptr combine(size_t nrFrontals, ADT::Binary op) const;
136 
143  shared_ptr combine(const Ordering& keys, ADT::Binary op) const;
144 
145 
146 // /**
147 // * @brief Permutes the keys in Potentials and DiscreteFactor
148 // *
149 // * This re-implements the permuteWithInverse() in both Potentials
150 // * and DiscreteFactor by doing both of them together.
151 // */
152 //
153 // void permuteWithInverse(const Permutation& inversePermutation){
154 // DiscreteFactor::permuteWithInverse(inversePermutation);
155 // Potentials::permuteWithInverse(inversePermutation);
156 // }
157 //
158 // /**
159 // * Apply a reduction, which is a remapping of variable indices.
160 // */
161 // virtual void reduceWithInverse(const internal::Reduction& inverseReduction) {
162 // DiscreteFactor::reduceWithInverse(inverseReduction);
163 // Potentials::reduceWithInverse(inverseReduction);
164 // }
165 
167 };
168 // DecisionTreeFactor
169 
170 }// namespace gtsam
A base class for both DiscreteFactor and DiscreteConditional.
Definition: Potentials.h:31
virtual DecisionTreeFactor toDecisionTreeFactor() const
Convert into a decisiontree.
Definition: DecisionTreeFactor.h:99
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor...
Definition: DiscreteFactor.h:33
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:36
A discrete probabilistic factor.
Definition: DecisionTreeFactor.h:39
virtual double operator()(const Values &values) const
Value is just look up in AlgebraicDecisonTree.
Definition: DecisionTreeFactor.h:84
DecisionTreeFactor(const DiscreteKeys &keys, SOURCE table)
Constructor from Indices and (string or doubles)
Definition: DecisionTreeFactor.h:61
This is the base class for all factor types.
Definition: Factor.h:51
DecisionTreeFactor operator/(const DecisionTreeFactor &f) const
divide by factor f (safely)
Definition: DecisionTreeFactor.h:94
void print(const Matrix &A, const string &s, ostream &stream)
print a matrix
Definition: Matrix.cpp:183
const double & operator()(const Assignment< Key > &x) const
evaluate
double max(const Vector &a)
Return the max element of a vector.
Definition: Vector.cpp:238
An assignment from labels to value index (size_t).
Definition: Assignment.h:35
shared_ptr max(size_t nrFrontals) const
Create new factor by maximizing over all values with the same separator values.
Definition: DecisionTreeFactor.h:114
Template to create a binary predicate.
Definition: Testable.h:102
DecisionTreeFactor operator*(const DecisionTreeFactor &f) const
multiply two factors
Definition: DecisionTreeFactor.h:89
shared_ptr sum(size_t nrFrontals) const
Create new factor by summing all values with the same separator values.
Definition: DecisionTreeFactor.h:104
DiscreteFactor Base
Typedef to base class.
Definition: DecisionTreeFactor.h:45
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition: DiscreteConditional.h:33
Definition: Ordering.h:30
shared_ptr sum(const Ordering &keys) const
Create new factor by summing all values with the same separator values.
Definition: DecisionTreeFactor.h:109
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const typename DecisionTree< L, Y >::Unary &op)
free versions of apply
Definition: DecisionTree.h:216
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62