gtsam 4.2.0
gtsam
Loading...
Searching...
No Matches
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
25
26#include <algorithm>
27#include <boost/shared_ptr.hpp>
28#include <map>
29#include <stdexcept>
30#include <string>
31#include <utility>
32#include <vector>
33
34namespace gtsam {
35
36 class DiscreteConditional;
37 class HybridValues;
38
44 class GTSAM_EXPORT DecisionTreeFactor : public DiscreteFactor,
45 public AlgebraicDecisionTree<Key> {
46 public:
47 // typedefs needed to play nice with gtsam
50 typedef boost::shared_ptr<DecisionTreeFactor> shared_ptr;
52
53 protected:
54 std::map<Key, size_t> cardinalities_;
55
56 public:
59
62
64 DecisionTreeFactor(const DiscreteKeys& keys, const ADT& potentials);
65
86 const std::vector<double>& table);
87
106 DecisionTreeFactor(const DiscreteKeys& keys, const std::string& table);
107
109 template <class SOURCE>
110 DecisionTreeFactor(const DiscreteKey& key, SOURCE table)
111 : DecisionTreeFactor(DiscreteKeys{key}, table) {}
112
114 DecisionTreeFactor(const DiscreteKey& key, const std::vector<double>& row)
116
118 explicit DecisionTreeFactor(const DiscreteConditional& c);
119
123
125 bool equals(const DiscreteFactor& other, double tol = 1e-9) const override;
126
127 // print
128 void print(
129 const std::string& s = "DecisionTreeFactor:\n",
130 const KeyFormatter& formatter = DefaultKeyFormatter) const override;
131
135
138 double evaluate(const DiscreteValues& values) const {
139 return ADT::operator()(values);
140 }
141
143 double operator()(const DiscreteValues& values) const override {
144 return ADT::operator()(values);
145 }
146
148 double error(const DiscreteValues& values) const;
149
152 return apply(f, ADT::Ring::mul);
153 }
154
155 static double safe_div(const double& a, const double& b);
156
157 size_t cardinality(Key j) const { return cardinalities_.at(j); }
158
161 return apply(f, safe_div);
162 }
163
165 DecisionTreeFactor toDecisionTreeFactor() const override { return *this; }
166
168 shared_ptr sum(size_t nrFrontals) const {
169 return combine(nrFrontals, ADT::Ring::add);
170 }
171
173 shared_ptr sum(const Ordering& keys) const {
174 return combine(keys, ADT::Ring::add);
175 }
176
178 shared_ptr max(size_t nrFrontals) const {
179 return combine(nrFrontals, ADT::Ring::max);
180 }
181
183 shared_ptr max(const Ordering& keys) const {
184 return combine(keys, ADT::Ring::max);
185 }
186
190
196 DecisionTreeFactor apply(const DecisionTreeFactor& f, ADT::Binary op) const;
197
204 shared_ptr combine(size_t nrFrontals, ADT::Binary op) const;
205
212 shared_ptr combine(const Ordering& keys, ADT::Binary op) const;
213
215 std::vector<std::pair<DiscreteValues, double>> enumerate() const;
216
218 DiscreteKeys discreteKeys() const;
219
238 DecisionTreeFactor prune(size_t maxNrAssignments) const;
239
243
245 void dot(std::ostream& os,
246 const KeyFormatter& keyFormatter = DefaultKeyFormatter,
247 bool showZero = true) const;
248
250 void dot(const std::string& name,
251 const KeyFormatter& keyFormatter = DefaultKeyFormatter,
252 bool showZero = true) const;
253
255 std::string dot(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
256 bool showZero = true) const;
257
265 std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
266 const Names& names = {}) const override;
267
275 std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
276 const Names& names = {}) const override;
277
281
286 double error(const HybridValues& values) const override;
287
289
290 private:
292 friend class boost::serialization::access;
293 template <class ARCHIVE>
294 void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
295 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
296 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(ADT);
297 ar& BOOST_SERIALIZATION_NVP(cardinalities_);
298 }
299 };
300
301// traits
302template <>
303struct traits<DecisionTreeFactor> : public Testable<DecisionTreeFactor> {};
304
305} // namespace gtsam
Algebraic Decision Trees.
specialized key for discrete variables
Variable ordering for the elimination algorithm.
std::pair< Key, size_t > DiscreteKey
Key type for discrete variables.
Definition DiscreteKey.h:36
Global functions in a separate testing namespace.
Definition chartTesting.h:28
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Extracts a row view from a matrix that avoids a copy.
Definition Matrix.h:222
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition DiscreteValues.cpp:129
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:156
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const typename DecisionTree< L, Y >::Unary &op)
free versions of apply
Definition DecisionTree.h:413
double dot(const V1 &a, const V2 &b)
Dot product.
Definition Vector.h:195
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
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
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
Template to create a binary predicate.
Definition Testable.h:111
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:151
An algebraic decision tree fixes the range of a DecisionTree to double.
Definition AlgebraicDecisionTree.h:38
A discrete probabilistic factor.
Definition DecisionTreeFactor.h:45
shared_ptr sum(const Ordering &keys) const
Create new factor by summing all values with the same separator values.
Definition DecisionTreeFactor.h:173
double operator()(const DiscreteValues &values) const override
Evaluate probability density, sugar.
Definition DecisionTreeFactor.h:143
DecisionTreeFactor operator*(const DecisionTreeFactor &f) const override
multiply two factors
Definition DecisionTreeFactor.h:151
shared_ptr max(size_t nrFrontals) const
Create new factor by maximizing over all values with the same separator.
Definition DecisionTreeFactor.h:178
shared_ptr max(const Ordering &keys) const
Create new factor by maximizing over all values with the same separator.
Definition DecisionTreeFactor.h:183
double evaluate(const DiscreteValues &values) const
Calculate probability for given values x, is just look up in AlgebraicDecisionTree.
Definition DecisionTreeFactor.h:138
DiscreteFactor Base
Typedef to base class.
Definition DecisionTreeFactor.h:49
shared_ptr sum(size_t nrFrontals) const
Create new factor by summing all values with the same separator values.
Definition DecisionTreeFactor.h:168
DecisionTreeFactor(const DiscreteKey &key, SOURCE table)
Single-key specialization.
Definition DecisionTreeFactor.h:110
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree.
Definition DecisionTreeFactor.h:165
DecisionTreeFactor operator/(const DecisionTreeFactor &f) const
divide by factor f (safely)
Definition DecisionTreeFactor.h:160
DecisionTreeFactor(const DiscreteKey &key, const std::vector< double > &row)
Single-key specialization, with vector of doubles.
Definition DecisionTreeFactor.h:114
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:38
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition DiscreteKey.h:39
A map from keys to values.
Definition DiscreteValues.h:34
Definition Ordering.h:34
the error.