-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
377 lines (316 loc) · 10.9 KB
/
Copy pathmain.cpp
File metadata and controls
377 lines (316 loc) · 10.9 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
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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include "include/functionality/Classes.h"
#include "include/results/Results.h"
#define MAC
#ifdef MAC
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <OpenGL/glext.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
// OpenGL constants
#define BASEWIDTH 400
#define BASEHEIGHT 400
#define BASEOPENGLFOVY 45.0
#define WINDOWTITLE "Self Organizing Maps"
// SOM constants
#define TOTALWEIGHTS 3
#define MAXEPOCHS 10
#define NORMALSIZE 75
#define INITIALLEARNINGRATE 0.1
#define SIGMA 1
// DataSet constants
#define CHUNCKTIMESIZE 5 // Given in minutes
#define CHUNCKTIMEINTERVAL 10 // Given in seconds
// Results constants
// Developing constants
//#define DEBUG
using namespace std;
// OpenGL variables
double _openGLFovy;
// SOM variables
int _trainDataSetSize;
int _sigma;
int _executionType;
int _width;
int _height;
int _countingSampling;
int _randInput;
int _version;
int _currentEpochs;
bool _training;
bool _isEvaluationDataSetInitialized;
SelfOrganizingMaps *_som;
// Results variables
int _totalUsersEvaluated;
int _maximumSamples;
int _initialSamples;
int _totalExperiments;
int _samplesIncrement;
// DataSet variables
vector<DataChunck *> _buildDataChunckSet;
vector<DataChunck *> _trainDataChunckSet;
vector<DataChunck *> _evaluateDataChunckSet;
vector<vector<DataChunck *> > _evaluateDataChunckSetCollection;
vector<int> _userIds;
// ===================== Local Method Headers =====================
// OpenGl methods
void display(void);
void reshape(int width, int height);
void keyboard(unsigned char key, int x, int y);
void idle(void);
void init();
// Algorithm methods
void initializeDataSetsForUser(int idUser, int chunckTimeSize, int chunckTimeInterval);
bool createEvaluationDataSets();
// ===================== Local Method Headers =====================
// ===================== Method Declaration =======================
void display(){
// Clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Compute eye position
glLoadIdentity();
// TODO: check what does this function do
gluLookAt(50,50,200, 50,50,0, 0,1,0);
// Draw the current map
_som->displayUsingNeuronColor();
// Draw to screen
glutSwapBuffers();
}
void reshape(int width, int height){
glViewport(0, 0, (GLsizei) width, (GLsizei) height);
}
void keyboard(unsigned char key, int mouseX, int mouseY){
switch (key){
// Training
case 't':
if(_executionType == 0){
cout << "Execution type is training" << endl;
cout << "Total samples: " << _trainDataSetSize << endl;
_training = !_training;
if(_training)
cout << "Training..." << endl;
else
cout << "Stopped training" << endl;
}else{
cout << "Train function is only valid for Train Matrix, current execution type is Load Matrix" << endl;
}
break;
// Results obtention
case 'r':
if(_executionType == 0){
cout << "Obtain results function is only valid for Load Matrix execution type, current execution type is Train Matrix" << endl;
}else{
cout << "Results obtention..." << endl;
Results::getResults(_initialSamples, _maximumSamples,
_samplesIncrement, _sigma, _totalExperiments,
_totalUsersEvaluated, _som, _evaluateDataChunckSetCollection, _userIds);
cout << "Results obtention finished" << endl;
glutPostRedisplay();
}
break;
// Stop training
case 's':
if(_executionType == 0){
if(_training){
_training = !_training;
}
cout << "Training has been stopped" << endl;
}else{
cout << "Stop training function is only valid for Train Matrix, current execution type is Load Matrix" << endl;
}
break;
// Export matrix
case 'e':
cout << "Export matrix" << endl;
Utils::exportMatrixToFile(_som->getMatrix(), _som->getEpochs(),
MAXEPOCHS, INITIALLEARNINGRATE, _som->getCurrenLearningRate());
break;
}
}
void idle(void){
if(_training){
_randInput = rand() % _trainDataSetSize;
_som->trainSegmentedFunctions(_trainDataChunckSet[_randInput]->dataChunckToVector());
glutPostRedisplay();
}
}
void init(){
// Initialize viewing system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(_openGLFovy, 1.0, 1.0, 1000.0);
glMatrixMode(GL_MODELVIEW);
// Initialize background color to black
glClearColor(0.0,0.0,0.0,0.0);
// Enable depth buffering
glEnable(GL_DEPTH_TEST);
}
/*
* Initialize build, train and evaluate dataSets for a specific user
* enables isEvaluationDataSetInitialized flag
*/
void initUserDataSet(int idUser, int chunckTimeSize, int chunckTimeInterval, int version){
cout << "Start building Build DataSet..." << endl;
_buildDataChunckSet = DataSet::createDataSetDataChunckFormat(idUser, Utils::BUILD, chunckTimeSize, chunckTimeInterval, version);
cout << "Finish building Build DataSet" << endl;
cout << "Start building Train DataSet..." << endl;
_trainDataChunckSet = DataSet::createDataSetDataChunckFormat(idUser, Utils::TRAIN, chunckTimeSize, chunckTimeInterval, version);
cout << "Finish building Train DataSet" << endl;
cout << "Start building Evaluation DataSet..." << endl;
_evaluateDataChunckSet = DataSet::createDataSetDataChunckFormat(idUser, Utils::EVALUATE, chunckTimeSize, chunckTimeInterval, version);
cout << "Finish building Evaluation DataSet" << endl;
_trainDataSetSize = _trainDataChunckSet.size();
_isEvaluationDataSetInitialized = true;
}
/*
* Create evaluation datasets for the three users
* if no data is available, Utils:BUILD data is used
*/
bool createEvaluationDataSets(){
if(_version == 1){
int dataSetType = Utils::BUILD;
for(int i=1; i<4; i++){
if(i == 3){
dataSetType = Utils::EVALUATE;
}
vector<DataChunck *> dataChunckSet =
DataSet::createDataSetDataChunckFormat(i, dataSetType, CHUNCKTIMESIZE, CHUNCKTIMEINTERVAL, _version);
_evaluateDataChunckSetCollection.push_back(dataChunckSet);
}
if(_evaluateDataChunckSetCollection.size() != 3){
cout << "Error initializing de evaluation datasets";
_evaluateDataChunckSetCollection.clear();;
return false;
}
return true;
}else{
for(int i=0; i<_totalUsersEvaluated; i++){
vector<DataChunck *> dataChunckSet =
DataSet::createDataSetDataChunckFormat(_userIds[i], Utils::EVALUATE, CHUNCKTIMESIZE, CHUNCKTIMEINTERVAL, _version);
_evaluateDataChunckSetCollection.push_back(dataChunckSet);
}
return true;
}
}
// ===================== Method Declaration =======================
// ===================== Main Declaration =========================
int main(int argc, char **argv){
if(argc < 3 ){
cout << "At least 4 parameters are required to use the program" << endl;
cout << "1: Program name" << endl;
cout << "2: Execution type [0 - Train Matrix | 1 - Load Matrix]" << endl;
cout << endl;
cout << "0 - Train Matrix mode" << endl;
cout << "3: User required to train matrix" << endl;
cout << "4: First/Second Version" << endl;
cout << endl;
cout << "1 - Load Matrix mode" << endl;
cout << "3: First/Second Version" << endl;
cout << "4: Initial amount of samples to evaluate" << endl;
cout << "5: Maximun amount of samples to evaluate" << endl;
cout << "6: Increment of samples unit" << endl;
cout << "7: Total experiments required" << endl;
cout << "To define the users selected to evaluate the format is as follows" << endl;
cout << "<savedTraining> <user_id>" << endl;
return 1;
}
// Variable initialization
_training = false;
_sigma = SIGMA;
_executionType = atoi(argv[1]);
_countingSampling = 0;
switch(_executionType){
case 0:{ // Analyze an user
cout << "Verifying that arguments are valid..." << endl;
if(argc < 4){
cout << "Missing arguments for Train matrix mode" << endl;
return 1;
}
int user = atoi(argv[2]);
_version = atoi(argv[3]);
cout << "Argumentos validos para la ejecucion por DataSet de usuario" << endl;
cout << "Se esta creando el dataset desde los archivos..." << endl;
initUserDataSet(user, CHUNCKTIMESIZE, CHUNCKTIMEINTERVAL, _version);
cout << "El dataset fue creado correctamente" << endl;
cout << "Inicializando el algoritmo de SOM..." << endl;
if(_trainDataSetSize > 0){
_som = new SelfOrganizingMaps(NORMALSIZE, TOTALWEIGHTS, MAXEPOCHS,
INITIALLEARNINGRATE, _buildDataChunckSet, _trainDataSetSize, user);
cout << "El algoritmo de SOM fue inicializado correctamente" << endl;
}else{
cout << "No fue posible inicial el algoritmo de SOM, el DataSet de entrenamiento esta vacio" << endl;
return 1;
}
_width = BASEWIDTH;
_height = BASEHEIGHT;
_openGLFovy = BASEOPENGLFOVY;
}
break;
case 1:{ // Get the matrix from a previous training
if(argc < 7){
cout << "For Load Matrix mode at least 7 parameters are required:" << endl;
cout << "0 Program name" << endl;
cout << "1 Execution type" << endl;
cout << "2 First/Second Version" << endl;
cout << "3 Initial amount of samples to evaluate" << endl;
cout << "4 Maximun amount of samples to evaluate" << endl;
cout << "5 Increment of samples unit" << endl;
cout << "6 Total experiments required" << endl;
cout << "7... N Files that conform the matrix" << endl;
}
vector<char *> fileNames;
vector<int> userIds;
_version = atoi(argv[2]);
_initialSamples = atoi(argv[3]);
_maximumSamples = atoi(argv[4]);
_samplesIncrement = atoi(argv[5]);
_totalExperiments = atoi(argv[6]);
for(int files=7; files<argc; files+=2){
fileNames.push_back(argv[files]);
_userIds.push_back(atoi(argv[(files+1)]));
}
_totalUsersEvaluated = fileNames.size();
cout << "Total evaluated users: " << _totalUsersEvaluated << endl;
int matrixComposition = sqrt(_totalUsersEvaluated);
cout << "Creating evaluation dataset..." << endl;
_isEvaluationDataSetInitialized = createEvaluationDataSets();
if(!_isEvaluationDataSetInitialized){
cout << "It was not possible to create the evaluation dataset" << endl;
}else{
cout << "Evaluation dataset was created correctly" << endl;
}
_width = BASEWIDTH * matrixComposition;
_height = BASEHEIGHT * matrixComposition;
_openGLFovy = BASEOPENGLFOVY * matrixComposition;
cout << _totalUsersEvaluated << " Are about to be exported..." << endl;
_som = Utils::importSOMFromFiles(fileNames, matrixComposition,
_totalUsersEvaluated);
cout << "Files were exported correctly" << endl;
}
break;
}
// OpenGL window configuration
glutInitWindowSize(_width, _height);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow(WINDOWTITLE);
// OpenGL register callback functions
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
// OpenGL initalize parameters
init();
// OpenGL loop
glutMainLoop();
// Release memory
delete _som;
return 0;
}
// ===================== Main Declaration =========================