-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.cpp
239 lines (211 loc) · 5.16 KB
/
Model.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "Model.hpp"
#include "Feature.hpp"
#include <list>
#include <float.h>
#include <iostream>
#include <algorithm>
using namespace std;
Model::Model(Feature** features, int dim)
{
separating = true;
for (int i = 0; i < features[0]->get_misclassified_size(); i++)
{
bit_container check = features[0]->get_misclassified(0)[i];
for (int j = 1; j < dim; j++)
{
check &= features[j]->get_misclassified(0)[i];
}
if (check)
{
separating = false;
return;
}
}
this->dim = dim;
this->neg_sample_no = features[0]->get_data_size();
this->thresholds = new double[dim];
this->margins = new double[dim];
this->sorted_margins = new double[dim];
this->feature_ids = new int[dim];
this->features = (Feature**) malloc(dim*sizeof(Feature*));
for (int i = 0; i < dim; i++)
{
this->features[i] = features[i];
this->feature_ids[i] = features[i]->id;
}
}
Model::~Model()
{
if (separating)
{
delete[] this->feature_ids;
delete[] thresholds;
delete[] margins;
free(features);
}
}
void Model::train()
{
if (!separating)
{
return;
}
// Solve 1 dimensional problem immediately
if(dim == 1)
{
margins[0] = (features[0]->get_distances(0))[features[0]->get_index(0)[0]];
sorted_margins[0] = margins[0];
thresholds[0] = features[0]->get_bound(0) - 0.5*(margins[0]);
return;
}
// Initialize list of dimensions
// Initialize data
// Initialize index
// Initialize max_index
list<int> dim_list;
int* index[dim];
double* distances[dim];
int closest_index[dim];
for (int i = 0; i < dim; i++)
{
dim_list.push_back(i);
distances[i] = features[i]->get_distances(0);
index[i] = features[i]->get_index(0);
closest_index[i] = 0;
}
// Find maximum distance dimension for each point
int furthest_dim[neg_sample_no];
for (int i = 0; i < neg_sample_no; i++)
{
furthest_dim[i] = 0;
double max_dist = distances[0][i];
for (int j = 1; j < dim; j++)
{
double comp_dist = distances[j][i];
if (comp_dist > max_dist)
{
furthest_dim[i] = j;
max_dist = comp_dist;
}
}
}
// For each dimension find the closest point in the set of points with
// maximum distance in that dimension.
// Find the closest of these points to the positive boundary and set that as a
// negative boundary then trim every point it classifies and remove that dimension.
// Do this until all dimensions have been removed.
while (dim_list.size() > 0)
{
int closest_dimension = -1;
double min_distance = DBL_MAX;
list<int>::iterator closest_iter;
for (list<int>::iterator dimension = dim_list.begin(); dimension != dim_list.end();)
{
bool broke = false;
for (int i = closest_index[*dimension]; i < neg_sample_no; i++)
{
if (furthest_dim[index[*dimension][i]] == *dimension)
{
closest_index[*dimension] = i;
if (distances[*dimension][index[*dimension][i]] < min_distance)
{
closest_iter = dimension;
closest_dimension = *dimension;
min_distance = distances[*dimension][index[*dimension][i]];
}
dimension++;
broke = true;
break;
}
}
// If a dimension has no remaining points then remove it
if(!broke)
{
closest_index[*dimension] = -1;
dimension = dim_list.erase(dimension);
}
}
if (dim_list.size() == 0)
{
break;
}
// Remove the dimension containing the highest of the high points
// Remove every point below the highest of the high points
dim_list.erase(closest_iter);
for (int i = closest_index[closest_dimension]; i < neg_sample_no; i++)
{
furthest_dim[index[closest_dimension][i]] = -1;
}
}
for (int i = 0; i < dim; i++)
{
if(closest_index[i] >= 0)
{
margins[i] = distances[i][index[i][closest_index[i]]];
sorted_margins[i] = margins[i];
thresholds[i] = features[i]->get_bound(0) - 0.5*margins[i];
}
else
{
thresholds[i] = -DBL_MAX;
margins[i] = DBL_MAX;
sorted_margins[i] = DBL_MAX;
}
}
sort(sorted_margins, sorted_margins + dim);
return;
}
int Model::test(double* datapoint)
{
for (int i = 0; i < dim; i++)
{
if(datapoint[i] < thresholds[i])
{
return 0;
}
}
return 1;
}
double* Model::get_thresholds() const
{
return this->thresholds;
}
double* Model::get_margins() const
{
return this->margins;
}
double* Model::get_sorted_margins() const
{
return this->sorted_margins;
}
int* Model::get_feature_ids() const
{
return this->feature_ids;
}
int Model::get_dim() const
{
return this->dim;
}
bool model_comparator_asc (const Model* p1, const Model* p2)
{
int dim = p1->get_dim();
int dim2 = p2->get_dim();
if(dim2 < dim)
{
dim = dim2;
}
double* s_margins1 = p1->get_sorted_margins();
double* s_margins2 = p2->get_sorted_margins();
for (int i = 0; i < dim; i++)
{
if(s_margins1[i] > s_margins2[i])
{
return true;
}
else if (s_margins1[i] < s_margins2[i])
{
return false;
}
}
return dim != dim2;
}