Fix formatting
parent
d0ff3ab97e
commit
9317e94452
|
@ -31,14 +31,14 @@ using namespace boost::assign;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace gtsam;
|
using namespace gtsam;
|
||||||
|
|
||||||
template<typename T>
|
template <typename T>
|
||||||
void dot(const T&f, const string& filename) {
|
void dot(const T& f, const string& filename) {
|
||||||
#ifndef DISABLE_DOT
|
#ifndef DISABLE_DOT
|
||||||
f.dot(filename);
|
f.dot(filename);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#define DOT(x)(dot(x,#x))
|
#define DOT(x) (dot(x, #x))
|
||||||
|
|
||||||
struct Crazy {
|
struct Crazy {
|
||||||
int a;
|
int a;
|
||||||
|
@ -65,14 +65,15 @@ struct CrazyDecisionTree : public DecisionTree<string, Crazy> {
|
||||||
|
|
||||||
// traits
|
// traits
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
template<> struct traits<CrazyDecisionTree> : public Testable<CrazyDecisionTree> {};
|
template <>
|
||||||
}
|
struct traits<CrazyDecisionTree> : public Testable<CrazyDecisionTree> {};
|
||||||
|
} // namespace gtsam
|
||||||
|
|
||||||
GTSAM_CONCEPT_TESTABLE_INST(CrazyDecisionTree)
|
GTSAM_CONCEPT_TESTABLE_INST(CrazyDecisionTree)
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
// Test string labels and int range
|
// Test string labels and int range
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
struct DT : public DecisionTree<string, int> {
|
struct DT : public DecisionTree<string, int> {
|
||||||
using Base = DecisionTree<string, int>;
|
using Base = DecisionTree<string, int>;
|
||||||
|
@ -98,30 +99,21 @@ struct DT : public DecisionTree<string, int> {
|
||||||
|
|
||||||
// traits
|
// traits
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
template<> struct traits<DT> : public Testable<DT> {};
|
template <>
|
||||||
}
|
struct traits<DT> : public Testable<DT> {};
|
||||||
|
} // namespace gtsam
|
||||||
|
|
||||||
GTSAM_CONCEPT_TESTABLE_INST(DT)
|
GTSAM_CONCEPT_TESTABLE_INST(DT)
|
||||||
|
|
||||||
struct Ring {
|
struct Ring {
|
||||||
static inline int zero() {
|
static inline int zero() { return 0; }
|
||||||
return 0;
|
static inline int one() { return 1; }
|
||||||
}
|
static inline int id(const int& a) { return a; }
|
||||||
static inline int one() {
|
static inline int add(const int& a, const int& b) { return a + b; }
|
||||||
return 1;
|
static inline int mul(const int& a, const int& b) { return a * b; }
|
||||||
}
|
|
||||||
static inline int id(const int& a) {
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
static inline int add(const int& a, const int& b) {
|
|
||||||
return a + b;
|
|
||||||
}
|
|
||||||
static inline int mul(const int& a, const int& b) {
|
|
||||||
return a * b;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
// test DT
|
// test DT
|
||||||
TEST(DecisionTree, example) {
|
TEST(DecisionTree, example) {
|
||||||
// Create labels
|
// Create labels
|
||||||
|
@ -139,20 +131,20 @@ TEST(DecisionTree, example) {
|
||||||
|
|
||||||
// A
|
// A
|
||||||
DT a(A, 0, 5);
|
DT a(A, 0, 5);
|
||||||
LONGS_EQUAL(0,a(x00))
|
LONGS_EQUAL(0, a(x00))
|
||||||
LONGS_EQUAL(5,a(x10))
|
LONGS_EQUAL(5, a(x10))
|
||||||
DOT(a);
|
DOT(a);
|
||||||
|
|
||||||
// pruned
|
// pruned
|
||||||
DT p(A, 2, 2);
|
DT p(A, 2, 2);
|
||||||
LONGS_EQUAL(2,p(x00))
|
LONGS_EQUAL(2, p(x00))
|
||||||
LONGS_EQUAL(2,p(x10))
|
LONGS_EQUAL(2, p(x10))
|
||||||
DOT(p);
|
DOT(p);
|
||||||
|
|
||||||
// \neg B
|
// \neg B
|
||||||
DT notb(B, 5, 0);
|
DT notb(B, 5, 0);
|
||||||
LONGS_EQUAL(5,notb(x00))
|
LONGS_EQUAL(5, notb(x00))
|
||||||
LONGS_EQUAL(5,notb(x10))
|
LONGS_EQUAL(5, notb(x10))
|
||||||
DOT(notb);
|
DOT(notb);
|
||||||
|
|
||||||
// Check supplying empty trees yields an exception
|
// Check supplying empty trees yields an exception
|
||||||
|
@ -162,34 +154,34 @@ TEST(DecisionTree, example) {
|
||||||
|
|
||||||
// apply, two nodes, in natural order
|
// apply, two nodes, in natural order
|
||||||
DT anotb = apply(a, notb, &Ring::mul);
|
DT anotb = apply(a, notb, &Ring::mul);
|
||||||
LONGS_EQUAL(0,anotb(x00))
|
LONGS_EQUAL(0, anotb(x00))
|
||||||
LONGS_EQUAL(0,anotb(x01))
|
LONGS_EQUAL(0, anotb(x01))
|
||||||
LONGS_EQUAL(25,anotb(x10))
|
LONGS_EQUAL(25, anotb(x10))
|
||||||
LONGS_EQUAL(0,anotb(x11))
|
LONGS_EQUAL(0, anotb(x11))
|
||||||
DOT(anotb);
|
DOT(anotb);
|
||||||
|
|
||||||
// check pruning
|
// check pruning
|
||||||
DT pnotb = apply(p, notb, &Ring::mul);
|
DT pnotb = apply(p, notb, &Ring::mul);
|
||||||
LONGS_EQUAL(10,pnotb(x00))
|
LONGS_EQUAL(10, pnotb(x00))
|
||||||
LONGS_EQUAL( 0,pnotb(x01))
|
LONGS_EQUAL(0, pnotb(x01))
|
||||||
LONGS_EQUAL(10,pnotb(x10))
|
LONGS_EQUAL(10, pnotb(x10))
|
||||||
LONGS_EQUAL( 0,pnotb(x11))
|
LONGS_EQUAL(0, pnotb(x11))
|
||||||
DOT(pnotb);
|
DOT(pnotb);
|
||||||
|
|
||||||
// check pruning
|
// check pruning
|
||||||
DT zeros = apply(DT(A, 0, 0), notb, &Ring::mul);
|
DT zeros = apply(DT(A, 0, 0), notb, &Ring::mul);
|
||||||
LONGS_EQUAL(0,zeros(x00))
|
LONGS_EQUAL(0, zeros(x00))
|
||||||
LONGS_EQUAL(0,zeros(x01))
|
LONGS_EQUAL(0, zeros(x01))
|
||||||
LONGS_EQUAL(0,zeros(x10))
|
LONGS_EQUAL(0, zeros(x10))
|
||||||
LONGS_EQUAL(0,zeros(x11))
|
LONGS_EQUAL(0, zeros(x11))
|
||||||
DOT(zeros);
|
DOT(zeros);
|
||||||
|
|
||||||
// apply, two nodes, in switched order
|
// apply, two nodes, in switched order
|
||||||
DT notba = apply(a, notb, &Ring::mul);
|
DT notba = apply(a, notb, &Ring::mul);
|
||||||
LONGS_EQUAL(0,notba(x00))
|
LONGS_EQUAL(0, notba(x00))
|
||||||
LONGS_EQUAL(0,notba(x01))
|
LONGS_EQUAL(0, notba(x01))
|
||||||
LONGS_EQUAL(25,notba(x10))
|
LONGS_EQUAL(25, notba(x10))
|
||||||
LONGS_EQUAL(0,notba(x11))
|
LONGS_EQUAL(0, notba(x11))
|
||||||
DOT(notba);
|
DOT(notba);
|
||||||
|
|
||||||
// Test choose 0
|
// Test choose 0
|
||||||
|
@ -204,10 +196,10 @@ TEST(DecisionTree, example) {
|
||||||
|
|
||||||
// apply, two nodes at same level
|
// apply, two nodes at same level
|
||||||
DT a_and_a = apply(a, a, &Ring::mul);
|
DT a_and_a = apply(a, a, &Ring::mul);
|
||||||
LONGS_EQUAL(0,a_and_a(x00))
|
LONGS_EQUAL(0, a_and_a(x00))
|
||||||
LONGS_EQUAL(0,a_and_a(x01))
|
LONGS_EQUAL(0, a_and_a(x01))
|
||||||
LONGS_EQUAL(25,a_and_a(x10))
|
LONGS_EQUAL(25, a_and_a(x10))
|
||||||
LONGS_EQUAL(25,a_and_a(x11))
|
LONGS_EQUAL(25, a_and_a(x11))
|
||||||
DOT(a_and_a);
|
DOT(a_and_a);
|
||||||
|
|
||||||
// create a function on C
|
// create a function on C
|
||||||
|
@ -219,16 +211,16 @@ TEST(DecisionTree, example) {
|
||||||
|
|
||||||
// mul notba with C
|
// mul notba with C
|
||||||
DT notbac = apply(notba, c, &Ring::mul);
|
DT notbac = apply(notba, c, &Ring::mul);
|
||||||
LONGS_EQUAL(125,notbac(x101))
|
LONGS_EQUAL(125, notbac(x101))
|
||||||
DOT(notbac);
|
DOT(notbac);
|
||||||
|
|
||||||
// mul now in different order
|
// mul now in different order
|
||||||
DT acnotb = apply(apply(a, c, &Ring::mul), notb, &Ring::mul);
|
DT acnotb = apply(apply(a, c, &Ring::mul), notb, &Ring::mul);
|
||||||
LONGS_EQUAL(125,acnotb(x101))
|
LONGS_EQUAL(125, acnotb(x101))
|
||||||
DOT(acnotb);
|
DOT(acnotb);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
// test Conversion of values
|
// test Conversion of values
|
||||||
bool bool_of_int(const int& y) { return y != 0; };
|
bool bool_of_int(const int& y) { return y != 0; };
|
||||||
typedef DecisionTree<string, bool> StringBoolTree;
|
typedef DecisionTree<string, bool> StringBoolTree;
|
||||||
|
@ -249,11 +241,9 @@ TEST(DecisionTree, ConvertValuesOnly) {
|
||||||
EXPECT(!f2(x00));
|
EXPECT(!f2(x00));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
// test Conversion of both values and labels.
|
// test Conversion of both values and labels.
|
||||||
enum Label {
|
enum Label { U, V, X, Y, Z };
|
||||||
U, V, X, Y, Z
|
|
||||||
};
|
|
||||||
typedef DecisionTree<Label, bool> LabelBoolTree;
|
typedef DecisionTree<Label, bool> LabelBoolTree;
|
||||||
|
|
||||||
TEST(DecisionTree, ConvertBoth) {
|
TEST(DecisionTree, ConvertBoth) {
|
||||||
|
@ -281,7 +271,7 @@ TEST(DecisionTree, ConvertBoth) {
|
||||||
EXPECT(!f2(x11));
|
EXPECT(!f2(x11));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
// test Compose expansion
|
// test Compose expansion
|
||||||
TEST(DecisionTree, Compose) {
|
TEST(DecisionTree, Compose) {
|
||||||
// Create labels
|
// Create labels
|
||||||
|
@ -292,7 +282,7 @@ TEST(DecisionTree, Compose) {
|
||||||
|
|
||||||
// Create from string
|
// Create from string
|
||||||
vector<DT::LabelC> keys;
|
vector<DT::LabelC> keys;
|
||||||
keys += DT::LabelC(A,2), DT::LabelC(B,2);
|
keys += DT::LabelC(A, 2), DT::LabelC(B, 2);
|
||||||
DT f2(keys, "0 2 1 3");
|
DT f2(keys, "0 2 1 3");
|
||||||
EXPECT(assert_equal(f2, f1, 1e-9));
|
EXPECT(assert_equal(f2, f1, 1e-9));
|
||||||
|
|
||||||
|
@ -302,13 +292,13 @@ TEST(DecisionTree, Compose) {
|
||||||
DOT(f4);
|
DOT(f4);
|
||||||
|
|
||||||
// a bigger tree
|
// a bigger tree
|
||||||
keys += DT::LabelC(C,2);
|
keys += DT::LabelC(C, 2);
|
||||||
DT f5(keys, "0 4 2 6 1 5 3 7");
|
DT f5(keys, "0 4 2 6 1 5 3 7");
|
||||||
EXPECT(assert_equal(f5, f4, 1e-9));
|
EXPECT(assert_equal(f5, f4, 1e-9));
|
||||||
DOT(f5);
|
DOT(f5);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
// Check we can create a decision tree of containers.
|
// Check we can create a decision tree of containers.
|
||||||
TEST(DecisionTree, Containers) {
|
TEST(DecisionTree, Containers) {
|
||||||
using Container = std::vector<double>;
|
using Container = std::vector<double>;
|
||||||
|
@ -330,7 +320,7 @@ TEST(DecisionTree, Containers) {
|
||||||
StringContainerTree converted(stringIntTree, container_of_int);
|
StringContainerTree converted(stringIntTree, container_of_int);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
// Test visit.
|
// Test visit.
|
||||||
TEST(DecisionTree, visit) {
|
TEST(DecisionTree, visit) {
|
||||||
// Create small two-level tree
|
// Create small two-level tree
|
||||||
|
@ -342,7 +332,7 @@ TEST(DecisionTree, visit) {
|
||||||
EXPECT_DOUBLES_EQUAL(6.0, sum, 1e-9);
|
EXPECT_DOUBLES_EQUAL(6.0, sum, 1e-9);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
// Test visit, with Choices argument.
|
// Test visit, with Choices argument.
|
||||||
TEST(DecisionTree, visitWith) {
|
TEST(DecisionTree, visitWith) {
|
||||||
// Create small two-level tree
|
// Create small two-level tree
|
||||||
|
@ -354,7 +344,7 @@ TEST(DecisionTree, visitWith) {
|
||||||
EXPECT_DOUBLES_EQUAL(6.0, sum, 1e-9);
|
EXPECT_DOUBLES_EQUAL(6.0, sum, 1e-9);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
// Test fold.
|
// Test fold.
|
||||||
TEST(DecisionTree, fold) {
|
TEST(DecisionTree, fold) {
|
||||||
// Create small two-level tree
|
// Create small two-level tree
|
||||||
|
@ -365,7 +355,7 @@ TEST(DecisionTree, fold) {
|
||||||
EXPECT_DOUBLES_EQUAL(6.0, sum, 1e-9);
|
EXPECT_DOUBLES_EQUAL(6.0, sum, 1e-9);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ******************************************************************************** */
|
/* ************************************************************************** */
|
||||||
// Test retrieving all labels.
|
// Test retrieving all labels.
|
||||||
TEST(DecisionTree, labels) {
|
TEST(DecisionTree, labels) {
|
||||||
// Create small two-level tree
|
// Create small two-level tree
|
||||||
|
|
Loading…
Reference in New Issue