testDecisionTree.cpp
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 
12 /*
13  * @file testDecisionTree.cpp
14  * @brief Develop DecisionTree
15  * @author Frank Dellaert
16  * @author Can Erdogan
17  * @date Jan 30, 2012
18  */
19 
20 #include <boost/assign/std/vector.hpp>
21 using namespace boost::assign;
22 
24 #include <gtsam/base/Testable.h>
26 
27 //#define DT_DEBUG_MEMORY
28 //#define DT_NO_PRUNING
29 #define DISABLE_DOT
31 using namespace std;
32 using namespace gtsam;
33 
34 template<typename T>
35 void dot(const T&f, const string& filename) {
36 #ifndef DISABLE_DOT
37  f.dot(filename);
38 #endif
39 }
40 
41 #define DOT(x)(dot(x,#x))
42 
43 struct Crazy { int a; double b; };
44 typedef DecisionTree<string,Crazy> CrazyDecisionTree; // check that DecisionTree is actually generic (as it pretends to be)
45 
46 // traits
47 namespace gtsam {
48 template<> struct traits<CrazyDecisionTree> : public Testable<CrazyDecisionTree> {};
49 }
50 
51 /* ******************************************************************************** */
52 // Test string labels and int range
53 /* ******************************************************************************** */
54 
56 
57 // traits
58 namespace gtsam {
59 template<> struct traits<DT> : public Testable<DT> {};
60 }
61 
62 struct Ring {
63  static inline int zero() {
64  return 0;
65  }
66  static inline int one() {
67  return 1;
68  }
69  static inline int add(const int& a, const int& b) {
70  return a + b;
71  }
72  static inline int mul(const int& a, const int& b) {
73  return a * b;
74  }
75 };
76 
77 /* ******************************************************************************** */
78 // test DT
80 {
81  // Create labels
82  string A("A"), B("B"), C("C");
83 
84  // create a value
85  Assignment<string> x00, x01, x10, x11;
86  x00[A] = 0, x00[B] = 0;
87  x01[A] = 0, x01[B] = 1;
88  x10[A] = 1, x10[B] = 0;
89  x11[A] = 1, x11[B] = 1;
90 
91  // A
92  DT a(A, 0, 5);
93  LONGS_EQUAL(0,a(x00))
94  LONGS_EQUAL(5,a(x10))
95  DOT(a);
96 
97  // pruned
98  DT p(A, 2, 2);
99  LONGS_EQUAL(2,p(x00))
100  LONGS_EQUAL(2,p(x10))
101  DOT(p);
102 
103  // \neg B
104  DT notb(B, 5, 0);
105  LONGS_EQUAL(5,notb(x00))
106  LONGS_EQUAL(5,notb(x10))
107  DOT(notb);
108 
109  // apply, two nodes, in natural order
110  DT anotb = apply(a, notb, &Ring::mul);
111  LONGS_EQUAL(0,anotb(x00))
112  LONGS_EQUAL(0,anotb(x01))
113  LONGS_EQUAL(25,anotb(x10))
114  LONGS_EQUAL(0,anotb(x11))
115  DOT(anotb);
116 
117  // check pruning
118  DT pnotb = apply(p, notb, &Ring::mul);
119  LONGS_EQUAL(10,pnotb(x00))
120  LONGS_EQUAL( 0,pnotb(x01))
121  LONGS_EQUAL(10,pnotb(x10))
122  LONGS_EQUAL( 0,pnotb(x11))
123  DOT(pnotb);
124 
125  // check pruning
126  DT zeros = apply(DT(A, 0, 0), notb, &Ring::mul);
127  LONGS_EQUAL(0,zeros(x00))
128  LONGS_EQUAL(0,zeros(x01))
129  LONGS_EQUAL(0,zeros(x10))
130  LONGS_EQUAL(0,zeros(x11))
131  DOT(zeros);
132 
133  // apply, two nodes, in switched order
134  DT notba = apply(a, notb, &Ring::mul);
135  LONGS_EQUAL(0,notba(x00))
136  LONGS_EQUAL(0,notba(x01))
137  LONGS_EQUAL(25,notba(x10))
138  LONGS_EQUAL(0,notba(x11))
139  DOT(notba);
140 
141  // Test choose 0
142  DT actual0 = notba.choose(A, 0);
143  EXPECT(assert_equal(DT(0.0), actual0));
144  DOT(actual0);
145 
146  // Test choose 1
147  DT actual1 = notba.choose(A, 1);
148  EXPECT(assert_equal(DT(B, 25, 0), actual1));
149  DOT(actual1);
150 
151  // apply, two nodes at same level
152  DT a_and_a = apply(a, a, &Ring::mul);
153  LONGS_EQUAL(0,a_and_a(x00))
154  LONGS_EQUAL(0,a_and_a(x01))
155  LONGS_EQUAL(25,a_and_a(x10))
156  LONGS_EQUAL(25,a_and_a(x11))
157  DOT(a_and_a);
158 
159  // create a function on C
160  DT c(C, 0, 5);
161 
162  // and a model assigning stuff to C
163  Assignment<string> x101;
164  x101[A] = 1, x101[B] = 0, x101[C] = 1;
165 
166  // mul notba with C
167  DT notbac = apply(notba, c, &Ring::mul);
168  LONGS_EQUAL(125,notbac(x101))
169  DOT(notbac);
170 
171  // mul now in different order
172  DT acnotb = apply(apply(a, c, &Ring::mul), notb, &Ring::mul);
173  LONGS_EQUAL(125,acnotb(x101))
174  DOT(acnotb);
175 }
176 
177 /* ******************************************************************************** */
178 // test Conversion
179 enum Label {
180  U, V, X, Y, Z
181 };
183 bool convert(const int& y) {
184  return y != 0;
185 }
186 
187 TEST(DT, conversion)
188 {
189  // Create labels
190  string A("A"), B("B");
191 
192  // apply, two nodes, in natural order
193  DT f1 = apply(DT(A, 0, 5), DT(B, 5, 0), &Ring::mul);
194 
195  // convert
196  map<string, Label> ordering;
197  ordering[A] = X;
198  ordering[B] = Y;
199  boost::function<bool(const int&)> op = convert;
200  BDT f2(f1, ordering, op);
201  // f1.print("f1");
202  // f2.print("f2");
203 
204  // create a value
205  Assignment<Label> x00, x01, x10, x11;
206  x00[X] = 0, x00[Y] = 0;
207  x01[X] = 0, x01[Y] = 1;
208  x10[X] = 1, x10[Y] = 0;
209  x11[X] = 1, x11[Y] = 1;
210  EXPECT(!f2(x00));
211  EXPECT(!f2(x01));
212  EXPECT(f2(x10));
213  EXPECT(!f2(x11));
214 }
215 
216 /* ******************************************************************************** */
217 // test Compose expansion
218 TEST(DT, Compose)
219 {
220  // Create labels
221  string A("A"), B("B"), C("C");
222 
223  // Put two stumps on A together
224  DT f1(B, DT(A, 0, 1), DT(A, 2, 3));
225 
226  // Create from string
227  vector<DT::LabelC> keys;
228  keys += DT::LabelC(A,2), DT::LabelC(B,2);
229  DT f2(keys, "0 2 1 3");
230  EXPECT(assert_equal(f2, f1, 1e-9));
231 
232  // Put this AB tree together with another one
233  DT f3(keys, "4 6 5 7");
234  DT f4(C, f1, f3);
235  DOT(f4);
236 
237  // a bigger tree
238  keys += DT::LabelC(C,2);
239  DT f5(keys, "0 4 2 6 1 5 3 7");
240  EXPECT(assert_equal(f5, f4, 1e-9));
241  DOT(f5);
242 }
243 
244 /* ************************************************************************* */
245 int main() {
246  TestResult tr;
247  return TestRegistry::runAllTests(tr);
248 }
249 /* ************************************************************************* */
static int mul(const int &a, const int &b)
static int one()
Scalar * y
Scalar * b
Definition: benchVecAdd.cpp:17
Concept check for values that can be used in unit tests.
static int runAllTests(TestResult &result)
signatures for conditional densities
static enum @843 ordering
DecisionTree< L, Y > apply(const DecisionTree< L, Y > &f, const typename DecisionTree< L, Y >::Unary &op)
Definition: DecisionTree.h:216
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
Definition: Half.h:150
DecisionTree choose(const L &label, size_t index) const
Definition: DecisionTree.h:180
double f2(const Vector2 &x)
void dot(const T &f, const string &filename)
Matrix< SCALARB, Dynamic, Dynamic > B
Definition: bench_gemm.cpp:36
DecisionTree< string, int > DT
Array33i a
static int zero()
bool convert(const int &y)
#define EXPECT(condition)
Definition: Test.h:151
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
#define DOT(x)
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:37
#define LONGS_EQUAL(expected, actual)
Definition: Test.h:135
int main()
traits
Definition: chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
static int add(const int &a, const int &b)
Point2 f1(const Point3 &p, OptionalJacobian< 2, 3 > H)
double f4(double x, double y, double z)
float * p
double f3(double x1, double x2)
DecisionTree< string, Crazy > CrazyDecisionTree
const KeyVector keys
TEST(DT, example)
std::pair< L, size_t > LabelC
Definition: DecisionTree.h:45
DecisionTree< Label, bool > BDT


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:25