22#include <gtsam/discrete/DecisionTree-inl.h>
38 class GTSAM_EXPORT AlgebraicDecisionTree :
public DecisionTree<L, double> {
46 static std::string DefaultFormatter(
const L& x) {
57 static inline double zero() {
return 0.0; }
58 static inline double one() {
return 1.0; }
59 static inline double add(
const double& a,
const double& b) {
62 static inline double max(
const double& a,
const double& b) {
63 return std::max(a, b);
65 static inline double mul(
const double& a,
const double& b) {
68 static inline double div(
const double& a,
const double& b) {
71 static inline double id(
const double& x) {
return x; }
74 AlgebraicDecisionTree(
double leaf = 1.0) : Base(leaf) {}
81 : Base(label, y1, y2) {}
98 : Base(labelC, y1, y2) {}
124 AlgebraicDecisionTree
125 (
const std::vector<typename Base::LabelC>& labelCs,
126 const std::vector<double>& ys) {
128 Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
139 AlgebraicDecisionTree
140 (
const std::vector<typename Base::LabelC>& labelCs,
141 const std::string& table) {
143 std::vector<double> ys;
144 std::istringstream iss(table);
145 std::copy(std::istream_iterator<double>(iss),
146 std::istream_iterator<double>(), std::back_inserter(ys));
150 Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
160 template <
typename Iterator>
163 this->
root_ = compose(begin, end, label);
172 template <
typename M>
174 const std::map<M, L>& map) {
176 std::function<L(
const M&)> L_of_M = [&map](
const M& label) -> L {
177 return map.at(label);
179 std::function<double(
const double&)> op = Ring::id;
184 AlgebraicDecisionTree
operator+(
const AlgebraicDecisionTree& g)
const {
185 return this->
apply(g, &Ring::add);
189 AlgebraicDecisionTree
operator*(
const AlgebraicDecisionTree& g)
const {
190 return this->
apply(g, &Ring::mul);
194 AlgebraicDecisionTree
operator/(
const AlgebraicDecisionTree& g)
const {
195 return this->
apply(g, &Ring::div);
199 AlgebraicDecisionTree
sum(
const L& label,
size_t cardinality)
const {
200 return this->
combine(label, cardinality, &Ring::add);
205 return this->
combine(labelC, &Ring::add);
209 void print(
const std::string& s =
"",
210 const typename Base::LabelFormatter& labelFormatter =
211 &DefaultFormatter)
const {
212 auto valueFormatter = [](
const double& v) {
213 return (boost::format(
"%4.8g") % v).str();
219 bool equals(
const AlgebraicDecisionTree& other,
double tol = 1e-9)
const {
221 auto compare = [tol](
double a,
double b) {
222 return std::abs(a - b) < tol;
224 return Base::equals(other, compare);
230 :
public Testable<AlgebraicDecisionTree<T>> {};
Concept check for values that can be used in unit tests.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
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
AlgebraicDecisionTree operator/(const AlgebraicDecisionTree &g) const
division
Definition AlgebraicDecisionTree.h:194
AlgebraicDecisionTree sum(const L &label, size_t cardinality) const
sum out variable
Definition AlgebraicDecisionTree.h:199
AlgebraicDecisionTree(const typename Base::LabelC &labelC, double y1, double y2)
Create a new leaf function splitting on a variable.
Definition AlgebraicDecisionTree.h:96
void print(const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
print method customized to value type double.
Definition AlgebraicDecisionTree.h:209
AlgebraicDecisionTree sum(const typename Base::LabelC &labelC) const
sum out variable
Definition AlgebraicDecisionTree.h:204
AlgebraicDecisionTree(const AlgebraicDecisionTree< M > &other, const std::map< M, L > &map)
Convert labels from type M to type L.
Definition AlgebraicDecisionTree.h:173
AlgebraicDecisionTree(const L &label, double y1, double y2)
Create a new leaf function splitting on a variable.
Definition AlgebraicDecisionTree.h:80
bool equals(const AlgebraicDecisionTree &other, double tol=1e-9) const
Equality method customized to value type double.
Definition AlgebraicDecisionTree.h:219
AlgebraicDecisionTree operator+(const AlgebraicDecisionTree &g) const
sum
Definition AlgebraicDecisionTree.h:184
AlgebraicDecisionTree operator*(const AlgebraicDecisionTree &g) const
product
Definition AlgebraicDecisionTree.h:189
AlgebraicDecisionTree(Iterator begin, Iterator end, const L &label)
Create a range of decision trees, splitting on a single variable.
Definition AlgebraicDecisionTree.h:161
The Real ring with addition and multiplication.
Definition AlgebraicDecisionTree.h:56
DecisionTree apply(const Unary &op) const
apply Unary operation "op" to f
Definition DecisionTree-inl.h:889
NodePtr convertFrom(const typename DecisionTree< M, X >::NodePtr &f, std::function< L(const M &)> L_of_M, std::function< Y(const X &)> Y_of_X) const
Convert from a DecisionTree<M, X> to DecisionTree<L, Y>.
Definition DecisionTree-inl.h:671
NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY) const
Internal recursive function to create from keys, cardinalities, and Y values.
Definition DecisionTree-inl.h:630
NodePtr root_
A DecisionTree just contains the root. TODO(dellaert): make protected.
Definition DecisionTree.h:146
void print(const std::string &s, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter) const
GTSAM-style print.
Definition DecisionTree-inl.h:872
DecisionTree combine(const L &label, size_t cardinality, const Binary &op) const
combine subtrees on key with binary operation "op"
Definition DecisionTree-inl.h:937
std::pair< L, size_t > LabelC
A label annotated with cardinality.
Definition DecisionTree.h:79