22#include <gtsam/discrete/DecisionTree-inl.h>
49 static std::string DefaultFormatter(
const L& x) {
58 AlgebraicDecisionTree(
double leaf = 1.0) : Base(leaf) {}
68 : Base(label, y1, y2) {}
85 : Base(labelC, y1, y2) {}
111 AlgebraicDecisionTree
112 (
const std::vector<typename Base::LabelC>& labelCs,
113 const std::vector<double>& ys) {
115 Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
126 AlgebraicDecisionTree
127 (
const std::vector<typename Base::LabelC>& labelCs,
128 const std::string& table) {
130 std::vector<double> ys;
131 std::istringstream iss(table);
132 std::copy(std::istream_iterator<double>(iss),
133 std::istream_iterator<double>(), std::back_inserter(ys));
137 Base::create(labelCs.begin(), labelCs.end(), ys.begin(), ys.end());
147 template <
typename Iterator>
150 this->
root_ = compose(begin, end, label);
159 template <
typename M>
161 const std::map<M, L>& map) {
163 std::function<L(
const M&)> L_of_M = [&map](
const M& label) -> L {
164 return map.at(label);
166 std::function<double(
const double&)> op = Ring::id;
181 template <
typename X,
typename Func>
186 AlgebraicDecisionTree
operator+(
const AlgebraicDecisionTree& g)
const {
187 return this->
apply(g, &Ring::add);
192 return this->
apply(&Ring::negate);
196 AlgebraicDecisionTree
operator-(
const AlgebraicDecisionTree& g)
const {
201 AlgebraicDecisionTree
operator*(
const AlgebraicDecisionTree& g)
const {
202 return this->
apply(g, &Ring::mul);
206 AlgebraicDecisionTree
operator/(
const AlgebraicDecisionTree& g)
const {
207 return this->
apply(g, &Ring::div);
213 auto visitor = [&](
double y) {
sum += y; };
214 this->
visit(visitor);
224 AlgebraicDecisionTree
normalize()
const {
return (*
this) / this->
sum(); }
228 double min = std::numeric_limits<double>::max();
229 auto visitor = [&](
double x) {
min = x <
min ? x :
min; };
230 this->
visit(visitor);
237 double max = -std::numeric_limits<double>::max();
238 auto visitor = [&](
double x) {
max = x >
max ? x :
max; };
239 this->
visit(visitor);
244 AlgebraicDecisionTree
sum(
const L& label,
size_t cardinality)
const {
245 return this->
combine(label, cardinality, &Ring::add);
250 return this->
combine(labelC, &Ring::add);
254 void print(
const std::string& s =
"",
255 const typename Base::LabelFormatter& labelFormatter =
256 &DefaultFormatter)
const {
257 auto valueFormatter = [](
const double& v) {
258 std::stringstream ss;
259 ss << std::setw(4) << std::setprecision(8) << v;
266 bool equals(
const AlgebraicDecisionTree& other,
double tol = 1e-9)
const {
268 auto compare = [tol](
double a,
double b) {
269 return std::abs(a - b) < tol;
271 return Base::equals(other, compare);
277 :
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 Group.h:37
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
An algebraic decision tree fixes the range of a DecisionTree to double.
Definition AlgebraicDecisionTree.h:41
AlgebraicDecisionTree operator/(const AlgebraicDecisionTree &g) const
division
Definition AlgebraicDecisionTree.h:206
AlgebraicDecisionTree sum(const L &label, size_t cardinality) const
sum out variable
Definition AlgebraicDecisionTree.h:244
AlgebraicDecisionTree(const typename Base::LabelC &labelC, double y1, double y2)
Create a new leaf function splitting on a variable.
Definition AlgebraicDecisionTree.h:83
void print(const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
print method customized to value type double.
Definition AlgebraicDecisionTree.h:254
AlgebraicDecisionTree sum(const typename Base::LabelC &labelC) const
sum out variable
Definition AlgebraicDecisionTree.h:249
double sum() const
Compute sum of all values.
Definition AlgebraicDecisionTree.h:211
AlgebraicDecisionTree(const AlgebraicDecisionTree< M > &other, const std::map< M, L > &map)
Convert labels from type M to type L.
Definition AlgebraicDecisionTree.h:160
double min() const
Find the minimum values amongst all leaves.
Definition AlgebraicDecisionTree.h:227
AlgebraicDecisionTree normalize() const
Helper method to perform normalization such that all leaves in the tree sum to 1.
Definition AlgebraicDecisionTree.h:224
AlgebraicDecisionTree(const L &label, double y1, double y2)
Create a new leaf function splitting on a variable.
Definition AlgebraicDecisionTree.h:67
AlgebraicDecisionTree(const DecisionTree< L, X > &other, Func f)
Create from an arbitrary DecisionTree<L, X> by operating on it with a functional f.
Definition AlgebraicDecisionTree.h:182
bool equals(const AlgebraicDecisionTree &other, double tol=1e-9) const
Equality method customized to value type double.
Definition AlgebraicDecisionTree.h:266
AlgebraicDecisionTree operator+(const AlgebraicDecisionTree &g) const
sum
Definition AlgebraicDecisionTree.h:186
AlgebraicDecisionTree(const typename Base::NodePtr root)
Constructor which accepts root pointer.
Definition AlgebraicDecisionTree.h:61
double max() const
Find the maximum values amongst all leaves.
Definition AlgebraicDecisionTree.h:235
AlgebraicDecisionTree operator-(const AlgebraicDecisionTree &g) const
subtract
Definition AlgebraicDecisionTree.h:196
AlgebraicDecisionTree operator-() const
negation
Definition AlgebraicDecisionTree.h:191
AlgebraicDecisionTree operator*(const AlgebraicDecisionTree &g) const
product
Definition AlgebraicDecisionTree.h:201
AlgebraicDecisionTree(Iterator begin, Iterator end, const L &label)
Create a range of decision trees, splitting on a single variable.
Definition AlgebraicDecisionTree.h:148
DecisionTree apply(const Unary &op) const
typename Node::Ptr NodePtr
Definition DecisionTree.h:146
NodePtr root_
A DecisionTree just contains the root. TODO(dellaert): make protected.
Definition DecisionTree.h:149
void print(const std::string &s, const LabelFormatter &labelFormatter, const ValueFormatter &valueFormatter) const
DecisionTree combine(const L &label, size_t cardinality, const Binary &op) const
static NodePtr convertFrom(const typename DecisionTree< L, X >::NodePtr &f, std::function< Y(const X &)> Y_of_X)
Convert from a DecisionTree<L, X> to DecisionTree<L, Y>.
Definition DecisionTree-inl.h:745
std::pair< L, size_t > LabelC
Definition DecisionTree.h:80
static NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY)