Loading...
Searching...
No Matches
DecisionTreeFactor.h
Go to the documentation of this file.
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
#pragma once
#include <gtsam/discrete/AlgebraicDecisionTree.h>
#include <gtsam/discrete/DiscreteFactor.h>
#include <gtsam/discrete/DiscreteKey.h>
#include <gtsam/inference/Ordering.h>
#include <algorithm>
#include <boost/shared_ptr.hpp>
#include <map>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
namespace gtsam {
class DiscreteConditional;
class HybridValues;
class GTSAM_EXPORT DecisionTreeFactor : public DiscreteFactor,
public AlgebraicDecisionTree<Key> {
public:
// typedefs needed to play nice with gtsam
typedef DecisionTreeFactor This;
typedef DiscreteFactor Base;
typedef boost::shared_ptr<DecisionTreeFactor> shared_ptr;
typedef AlgebraicDecisionTree<Key> ADT;
protected:
std::map<Key, size_t> cardinalities_;
public:
DecisionTreeFactor();
DecisionTreeFactor(const DiscreteKeys& keys, const ADT& potentials);
DecisionTreeFactor(const DiscreteKeys& keys,
const std::vector<double>& table);
DecisionTreeFactor(const DiscreteKeys& keys, const std::string& table);
template <class SOURCE>
DecisionTreeFactor(const DiscreteKey& key, SOURCE table)
: DecisionTreeFactor(DiscreteKeys{key}, table) {}
DecisionTreeFactor(const DiscreteKey& key, const std::vector<double>& row)
: DecisionTreeFactor(DiscreteKeys{key}, row) {}
explicit DecisionTreeFactor(const DiscreteConditional& c);
bool equals(const DiscreteFactor& other, double tol = 1e-9) const override;
// print
void print(
const std::string& s = "DecisionTreeFactor:\n",
const KeyFormatter& formatter = DefaultKeyFormatter) const override;
double evaluate(const DiscreteValues& values) const {
return ADT::operator()(values);
}
double operator()(const DiscreteValues& values) const override {
return ADT::operator()(values);
}
double error(const DiscreteValues& values) const;
DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override {
return apply(f, ADT::Ring::mul);
}
static double safe_div(const double& a, const double& b);
size_t cardinality(Key j) const { return cardinalities_.at(j); }
DecisionTreeFactor operator/(const DecisionTreeFactor& f) const {
return apply(f, safe_div);
}
DecisionTreeFactor toDecisionTreeFactor() const override { return *this; }
shared_ptr sum(size_t nrFrontals) const {
return combine(nrFrontals, ADT::Ring::add);
}
shared_ptr sum(const Ordering& keys) const {
return combine(keys, ADT::Ring::add);
}
shared_ptr max(size_t nrFrontals) const {
return combine(nrFrontals, ADT::Ring::max);
}
shared_ptr max(const Ordering& keys) const {
return combine(keys, ADT::Ring::max);
}
DecisionTreeFactor apply(const DecisionTreeFactor& f, ADT::Binary op) const;
shared_ptr combine(size_t nrFrontals, ADT::Binary op) const;
shared_ptr combine(const Ordering& keys, ADT::Binary op) const;
std::vector<std::pair<DiscreteValues, double>> enumerate() const;
DiscreteKeys discreteKeys() const;
DecisionTreeFactor prune(size_t maxNrAssignments) const;
void dot(std::ostream& os,
const KeyFormatter& keyFormatter = DefaultKeyFormatter,
bool showZero = true) const;
void dot(const std::string& name,
const KeyFormatter& keyFormatter = DefaultKeyFormatter,
bool showZero = true) const;
std::string dot(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
bool showZero = true) const;
std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const Names& names = {}) const override;
std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const Names& names = {}) const override;
double error(const HybridValues& values) const override;
private:
friend class boost::serialization::access;
template <class ARCHIVE>
void serialize(ARCHIVE& ar, const unsigned int /*version*/) {
ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(ADT);
ar& BOOST_SERIALIZATION_NVP(cardinalities_);
}
};
// traits
template <>
struct traits<DecisionTreeFactor> : public Testable<DecisionTreeFactor> {};
} // namespace gtsam
DiscreteKey.h
specialized key for discrete variables
AlgebraicDecisionTree.h
Algebraic Decision Trees.
DiscreteFactor.h
Ordering.h
Variable ordering for the elimination algorithm.
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Key type for discrete variables.
Definition DiscreteKey.h:36
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
gtsam::row
const MATRIX::ConstRowXpr row(const MATRIX &A, size_t j)
Extracts a row view from a matrix that avoids a copy.
Definition Matrix.h:249
gtsam::markdown
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition DiscreteValues.cpp:129
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:156
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::apply
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const typename DecisionTree< L, Y >::Unary &op)
free versions of apply
Definition DecisionTree.h:413
gtsam::dot
double dot(const V1 &a, const V2 &b)
Dot product.
Definition Vector.h:205
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
gtsam::traits
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
gtsam::equals
Template to create a binary predicate.
Definition Testable.h:111
gtsam::Testable
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:151
gtsam::DecisionTree::operator()
const Y & operator()(const Assignment< L > &x) const
evaluate
Definition DecisionTree-inl.h:884
gtsam::DecisionTreeFactor
A discrete probabilistic factor.
Definition DecisionTreeFactor.h:45
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:173
gtsam::DecisionTreeFactor::operator()
double operator()(const DiscreteValues &values) const override
Evaluate probability density, sugar.
Definition DecisionTreeFactor.h:143
gtsam::DecisionTreeFactor::operator*
DecisionTreeFactor operator*(const DecisionTreeFactor &f) const override
multiply two factors
Definition DecisionTreeFactor.h:151
gtsam::DecisionTreeFactor::combine
shared_ptr combine(size_t nrFrontals, ADT::Binary op) const
Combine frontal variables using binary operator "op".
Definition DecisionTreeFactor.cpp:106
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:178
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:183
gtsam::DecisionTreeFactor::evaluate
double evaluate(const DiscreteValues &values) const
Calculate probability for given values x, is just look up in AlgebraicDecisionTree.
Definition DecisionTreeFactor.h:138
gtsam::DecisionTreeFactor::apply
DecisionTreeFactor apply(const DecisionTreeFactor &f, ADT::Binary op) const
Apply binary operator (*this) "op" f.
Definition DecisionTreeFactor.cpp:90
gtsam::DecisionTreeFactor::Base
DiscreteFactor Base
Typedef to base class.
Definition DecisionTreeFactor.h:49
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:168
gtsam::DecisionTreeFactor::DecisionTreeFactor
DecisionTreeFactor(const DiscreteKey &key, SOURCE table)
Single-key specialization.
Definition DecisionTreeFactor.h:110
gtsam::DecisionTreeFactor::toDecisionTreeFactor
DecisionTreeFactor toDecisionTreeFactor() const override
Convert into a decisiontree.
Definition DecisionTreeFactor.h:165
gtsam::DecisionTreeFactor::DecisionTreeFactor
DecisionTreeFactor()
Default constructor for I/O.
Definition DecisionTreeFactor.cpp:34
gtsam::DecisionTreeFactor::operator/
DecisionTreeFactor operator/(const DecisionTreeFactor &f) const
divide by factor f (safely)
Definition DecisionTreeFactor.h:160
gtsam::DecisionTreeFactor::DecisionTreeFactor
DecisionTreeFactor(const DiscreteKey &key, const std::vector< double > &row)
Single-key specialization, with vector of doubles.
Definition DecisionTreeFactor.h:114
gtsam::DiscreteConditional
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition DiscreteConditional.h:40
gtsam::DiscreteFactor
Base class for discrete probabilistic factors The most general one is the derived DecisionTreeFactor.
Definition DiscreteFactor.h:38
gtsam::DiscreteFactor::DiscreteFactor
DiscreteFactor()
Default constructor creates empty factor.
Definition DiscreteFactor.h:55
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition DiscreteKey.h:39
gtsam::DiscreteValues
A map from keys to values.
Definition DiscreteValues.h:34
gtsam::HybridValues
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:38
gtsam::Factor::keys
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:140
gtsam::Ordering
Definition Ordering.h:34