22#include <gtsam/discrete/DecisionTree-inl.h>
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; }
81 :
Base(label, y1, y2) {}
98 :
Base(labelC, y1, y2) {}
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());
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;
185 return this->
apply(g, &Ring::add);
190 return this->
apply(g, &Ring::mul);
195 return this->
apply(g, &Ring::div);
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();
215 Base::print(s, labelFormatter, valueFormatter);
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
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const typename DecisionTree< L, Y >::Unary &op)
free versions of apply
Definition DecisionTree.h:413
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
a decision tree is a function from assignments to values.
Definition DecisionTree.h:61
NodePtr root_
A DecisionTree just contains the root. TODO(dellaert): make protected.
Definition DecisionTree.h:146
std::pair< L, size_t > LabelC
A label annotated with cardinality.
Definition DecisionTree.h:79