-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecisionTree.cpp
More file actions
132 lines (106 loc) · 2.95 KB
/
DecisionTree.cpp
File metadata and controls
132 lines (106 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <boost/shared_ptr.hpp>
#include "buildTree.h"
#include "informationGain.h"
#include "ArffReader.h"
#include "saveTree.h"
#include "Internal.h"
#include "Leaf.h"
#include "global.h"
using namespace std;
int main(int argc, char* argv[])
{
arffReader(argv[1]);
m = e.size();
k = e[0].size() - 1;
ifstream costFile;
costFile.open(argv[2]);
if (costFile.is_open())
{
double cost;
while ( costFile >> cost )
costs.push_back(cost);
costFile.close();
}
//for(int z = 0; z < 4; ++z)
// cout << costs[z] << " ";
//cout << endl;
if(m < 1 || k < 2)
{
cout << "Not enough examples or attributes." << endl;
return -1;
}
//check if all the examples are already classified the same
unsigned int c;
for(c = 1; c < m && e[0][k] == e[c][k]; ++c);
if(c == m)
{
shared_ptr<Leaf> root = make_shared<Leaf>();
numberOfNodes = 1;
root->classification = e[0][k];
for(unsigned int i = 0; i < m; ++i)
root->examples.push_back(&e[i]);
root->printTree(0);
root.reset();
return 0;
}
shared_ptr<vector<shared_ptr<Internal> > > open = make_shared<vector<shared_ptr<Internal> > >();
shared_ptr<Internal> root = 0;
shared_ptr<Internal> bestTree = 0;
unsigned int numberOfFeatures = 1;
unsigned long long int attempts = 1;
while(difftime( time(0), start) < runTime && upperBound > 3)
{
numberOfNodes = 1;
costOfTree = 0;
root.reset();
root = make_shared<Internal>();
for(unsigned int i = 0; i < m; ++i)
root->examples.push_back(&e[i]);
for(unsigned int i = 0; i < k; ++i)
root->features.push_back(i);
double h;
for(vector<unsigned int>::iterator it = root->features.begin(); it != root->features.end(); ++it)
{
h = entropy(root,(*it));
//cout << "h: " << h << ", ";
h = h - w_sum(root,(*it),ent);
//cout << "h - w_sum: " << h << ", ";
h = h * h;
//cout << "I^2: " << h << ", C: " << costs[*it] << ", ";
h = h / costs[*it];
//cout << "I^2/C: " << h / costs[*it] << endl;
root->orderedGains.push_back(make_pair(h, (*it)));
}//cout << endl;
sort(root->orderedGains.begin(), root->orderedGains.end(), compare);
open->clear();
open->push_back(root);
if(buildTree(open, 0, costOfTree, attempts, numberOfFeatures) == -1)
{
//cout << "Random search complete" << endl;
attempts = attempts + 1;
}
else
{
upperBound = numberOfNodes;
upperBoundCost = costOfTree;
cout << difftime( time(0), start) << " - Tree of cost: " << upperBoundCost << " Size: " << upperBound << endl;
if(bestTree)
bestTree->cut();
bestTree = root;
}
if(numberOfFeatures != 3)
numberOfFeatures = 3;
}
cout << "Tree with cost " << upperBoundCost << " found." << endl;
if(bestTree != 0)
{
bestTree->printTree(0);
cout << endl;
saveTree(bestTree, argv[3]);
bestTree->cut();
bestTree.reset();
}
else
cout << "No tree found." << endl;
return 0;
}