-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.cpp
executable file
·406 lines (359 loc) · 12.2 KB
/
display.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "display.h"
#include "booster.h"
#include "kernelperceptron.h"
#include <QFile>
#include <QVector>
#include <QTextStream>
#include <algorithm>
#include <ctime>
#define IRIS_PATH "/Users/naleliunas/Documents/421-Project-1/iris.data"
#define BLOOD_PATH "/Users/naleliunas/Documents/421-Project-1/transfusion.data"
//http://www.cplusplus.com/forum/general/4422/
//http://www.cplusplus.com/articles/1UqpX9L8/
vector<string>& splitstring::split(char delim, int rep) {
if (!flds.empty()) flds.clear(); // empty vector if necessary
string work = data();
string buf = "";
uint i = 0;
while (i < work.length()) {
if (work[i] != delim)
buf += work[i];
else if (rep == 1) {
flds.push_back(buf);
buf = "";
} else if (buf.length() > 0) {
flds.push_back(buf);
buf = "";
}
i++;
}
if (!buf.empty())
flds.push_back(buf);
return flds;
}
Display::Display(QWidget *parent) :
QGLWidget(QGLFormat(QGL::SampleBuffers), parent),
projectionMatrix(),
viewMatrix()
{
}
float Display::training_accuracy(QVector<SolvedDataPoint> points, int training_size)
{
int correct = 0;
for (int i=0; i<points.size(); i++)
{
//printf("%d%d", int(points[i].classification), int(points[i].calculatedClassification));
if (points[i].classification == points[i].calculatedClassification && points[i].trainingExample == true)
{
correct += 1;
}
}
return correct/float(training_size);
}
float Display::accuracy(QVector<SolvedDataPoint> points, int training_size)
{
int correct = 0;
for (int i=0; i<points.size(); i++)
{
//printf("%d%d", int(points[i].classification), int(points[i].calculatedClassification));
if (points[i].classification == points[i].calculatedClassification && points[i].trainingExample == false)
{
correct += 1;
}
}
return correct/float(points.size() - training_size);
}
void Display::initializeGL()
{
glClearColor(0.7, 0.7, 0.7, 1);
eye = QVector3D(-0.64, 1, -0.64);
at = QVector3D(-0.64, 0, -0.64);
up = QVector3D(0, 0, 1);
number = 0;
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
srand(time(NULL));
switch_algorithim = 2;
switch_data_set = 0;
switch_analysis = 1;
perceptron_degree = 2;
maximum_iterations = 10;
training_set_size = 0.25;
calc_finished = 0;
progress = 0;
}
void Display::start()
{
QVector<DataPoint> points;
QVector<SolvedDataPoint> solved_points;
switch(switch_analysis)
{
case 0:
printf("Analysis Started\n");
accuracy_points = analyse_accuracy();
calc_finished = 1;
break;
case 1:
printf("Single Use Started\n");
points = read_file();
switch(switch_algorithim)
{
case 0:
case 1:
random_shuffle(points.begin(), points.end());
break;
case 2:
points = randomize(points, points.size()*training_set_size);
break;
}
solved_points = run_algorithim(points, points.size()*training_set_size, maximum_iterations);
for (int i=0; i<solved_points.size(); i++)
{
solved_points[i].printString();
}
printf("Accuracy: %f",accuracy(solved_points,points.size()*training_set_size));
fflush(stdout);
break;
}
}
//calls read file, calls randomize, calls run_algorithm buttload of times
QVector< QVector<float> > Display::analyse_accuracy()
{
//read in file, take data points
QVector<DataPoint> points = read_file();
QVector< QVector<float> > accuracy_points;
int num_of_trials = 15;
for (int max_it=1; max_it<21; max_it++)
{
QVector<float> temp;
for (int training_size=1; training_size<points.size()/4; training_size++)
{
float trials_accuracy = 0;
float trails_training_accuracy = 0;
for (int trials=0; trials<num_of_trials; trials++)
{
switch(switch_algorithim)
{
case 0:
case 1:
random_shuffle(points.begin(), points.end());
break;
case 2:
points = randomize(points, training_size);
break;
}
//run algorithim
QVector<SolvedDataPoint> data_points = run_algorithim(points,training_size, max_it);
trials_accuracy += accuracy(data_points, training_size);
trails_training_accuracy += training_accuracy(data_points, training_size);
}
if(max_it % 5 == 0 || max_it == 1) {
printf("Completed: Iterations:[%i] Training set: [%f] with accuracy [%f] & training accuracy [%f] \n",max_it,(float)training_size/(float)points.size()*100,trials_accuracy*100/(float)num_of_trials, trails_training_accuracy*100/(float)num_of_trials);
}
fflush(stdout);
temp.push_back(trials_accuracy/(float)num_of_trials);
}
printf("Completed iteration [%i]\n",max_it);
accuracy_points.push_back(temp);
}
return accuracy_points;
}
//takes vector and training size, returns distributed randomized vector
QVector<DataPoint> Display::randomize(QVector<DataPoint> points, int training_size)
{
QVector<DataPoint> trues;
QVector<DataPoint> falses;
for(int i = 0; i < points.size(); i++) {
if (points[i].classification == true) {
trues.push_back(points[i]);
} else {
falses.push_back(points[i]);
}
}
float ratio = (float)trues.size()/(float)points.size();
int num_of_trues = ratio * training_size;
random_shuffle(trues.begin(), trues.end());
random_shuffle(falses.begin(), falses.end());
QVector<DataPoint> return_vector;
for(int i = 0; i < num_of_trues; i++) {
return_vector.push_back(trues[i]);
}
for(int i = 0; i < falses.size(); i++) {
return_vector.push_back(falses[i]);
}
for(int i = num_of_trues; i < trues.size(); i++) {
return_vector.push_back(trues[i]);
}
return return_vector;
}
//calls kperceptron/boost with QVector<DataPoints> and returns QVector<SolvedDataPoints> from respective functions
QVector<SolvedDataPoint> Display::run_algorithim(QVector<DataPoint> points, int training_size, int max_it)
{
QVector<SolvedDataPoint> data_points;
switch(switch_algorithim)
{
case 0:
data_points = kPerceptronSolver(points, training_size, max_it, perceptron_degree, 0);
break;
case 1:
data_points = kPerceptronSolver(points, training_size, max_it, perceptron_degree, 1);
break;
case 2:
data_points = Booster::boost(points, training_size, max_it);
break;
}
return data_points;
}
void Display::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
viewMatrix.setToIdentity();
viewMatrix.lookAt(QVector3D(eye.x()*1, eye.y()*2, eye.z()*1), // x15 moves camera away from origin
QVector3D(at.x(), at.y(), at.z()),
QVector3D(up.x(), up.y(), up.z()));
glLoadMatrixd(viewMatrix.data());
glColor4f(1,1,1, 1);
glBegin(GL_QUADS);
glVertex3f(0, 0.0f, 0);
glVertex3f(-1.27, 0.0f, 0);
glVertex3f(-1.27, 0.0f, -1.27);
glVertex3f(0, 0.0f, -1.27);
glEnd();
if (calc_finished == 0) {
return;
} else {
//THESE VALUES NEED TO BE DYNAMICALLY CALCULATED FROM 1.27 / 2 / MAXIT-1 AND POINTS.SIZE()/4-1
float xsize = 0.0669;
//float zsize = 0.0363;
float zsize = 1.27/(accuracy_points[0].size()-1);
//printf("accuracy_points[0].size():%d\n", accuracy_points[0].size());
//printf("zsize:%f\n", zsize);
float blue;
float green;
for (int i=0; i<20; i++)
{
for (int j=0; j<accuracy_points[i].size(); j++)
{
float x = i/19.0*-1.27;
float z = j/float(accuracy_points[i].size()-1)*-1.27;
green = accuracy_points[i][j];
//red =1;
blue = 1 - green;
glColor3f(0,green,blue);
glBegin(GL_QUADS);
glVertex3f(x, 0.0f, z);
glVertex3f(x-xsize, 0.0f, z);
glVertex3f(x-xsize, 0.0f, z-zsize);
glVertex3f(x, 0.0f, z-zsize);
glEnd();
}
}
}
}
void Display::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
projectionMatrix.setToIdentity();
projectionMatrix.perspective(60.0f, (float)width/(float)height, 0.01f, 50.0f);
glLoadMatrixd(projectionMatrix.data());
glMatrixMode(GL_MODELVIEW);
}
//reads file, returns QVector<DataPoints>
QVector<DataPoint> Display::read_file()
{
QVector<DataPoint> points;
QFile file;
if(switch_data_set == 0 || switch_data_set ==1 || switch_data_set ==2) { //set 0,1,2 to the three irises
//file.setFileName("/Users/naleliunas/Documents/421-Project-1/iris.data");
file.setFileName(IRIS_PATH);
} else if(switch_data_set == 3) { //blood data set
//file.setFileName("/Users/naleliunas/Documents/421-Project-1/transfusion.data");
file.setFileName(BLOOD_PATH);
}
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&file);
QString line = in.readLine();
DataPoint new_point;
if (switch_data_set == 3) line = in.readLine(); //skip the first line.
while (!line.isEmpty())
{
const char *cstring = line.toUtf8().constData();
splitstring s(cstring);
vector<string> flds = s.split(',');
QVector<float> attr;
char *a;
for (int k=0; k < flds.size(); k++){
a=new char[flds[k].size()+1];
a[flds[k].size()]=0;
memcpy(a,flds[k].c_str(),flds[k].size());
printf("%s ", a);
if (k < flds.size()-1)
{
//attr[k] = atof(a);
//printf("%f ", attr[k]);
attr.push_back(atof(a));
}
}
bool classification = false;
char *true_classification;
if(switch_data_set == 0 || switch_data_set ==1 || switch_data_set ==2) { //set 0,1,2 to the three irises
if(switch_data_set == 0) {
true_classification = "Iris-setosa";
} else if(switch_data_set == 1) {
true_classification = "Iris-versicolor";
} else if(switch_data_set == 2) {
true_classification = "Iris-virginica";
}
} else if(switch_data_set == 3) { //blood data set
true_classification = "1";
}
if (strcmp(a, true_classification) == 0)
{
classification = true;
}
new_point = DataPoint(attr, (flds.size()-1), classification);
points.push_back(new_point);
printf("\n");
line = in.readLine();
}
file.close();
return points;
}
//returns progress
int Display::return_progress()
{
return progress;
}
void Display::set_maximum_iterations(int max_it)
{
maximum_iterations = max_it;
}
void Display::set_perceptron_degree(int degree)
{
perceptron_degree = degree;
}
void Display::set_training_set_size(int size)
{
training_set_size = size/100.0;
}
void Display::set_algorithm(int n)
{
switch_algorithim = n;
}
void Display::set_analysis(bool onetime)
{
if (onetime)
{
switch_analysis = 1;
}
else
{
switch_analysis = 0;
}
}
void Display::set_data_set(int n)
{
switch_data_set = n;
}