-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTools.cpp
More file actions
553 lines (360 loc) · 10.7 KB
/
Tools.cpp
File metadata and controls
553 lines (360 loc) · 10.7 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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#include "Tools.h"
#include <iostream>
#include <algorithm>
#include <numeric>
#include <ilcplex/ilocplex.h>
#include <cstdlib> // std::rand, std::srand
#define PI 3.14159265
float Tools::euclidian_distance(vector<float> v1, vector<float> v2){
float dist = 0;
for(int i = 0; i < (int)v1.size(); i++)
dist += ((v1[i] - v2[i]) * (v1[i] - v2[i]));
dist = sqrt(dist);
return dist;
}
float Tools::get_ratio(vector<float> v, vector<float> v_opt, vector<float> weights){
float v_w = 0, v_w_opt = 0;
for(int i = 0; i < (int)weights.size(); i++){
v_w += weights[i] * v[i];
v_w_opt += weights[i] * v_opt[i];
}
return (v_w_opt - v_w)/v_w_opt; //VALEURS POSITIVE
}
bool Tools::equal_vectors(vector<float> v1, vector<float> v2){
for(int i = 0; i < (int)v1.size(); i++)
if ( abs(v1[i] - v2[i]) > 0.001 )
return false;
return true;
}
vector< float > Tools::decompose_line_to_float_vector(string line){
char *cline = new char[line.length() + 1];
int i = 0;
vector< float > vect;
strcpy(cline, line.c_str());
char * pch = strtok (cline," ,;");
while (pch != NULL ){
vect.push_back(atof(pch));
pch = strtok (NULL, " ,;");
i++;
}
return vect;
}
string Tools::print_vector(vector<float> v){
string str ="";
for(int i = 0 ; i < (int)v.size(); i++)
str += to_string(v[i]) + " ";
return str;
}
void Tools::shuffle_list(list< string > & unshuffled_L){
mt19937 g( rand() );
vector<string> shuffled_L (unshuffled_L.begin(), unshuffled_L.end());
shuffle(shuffled_L.begin(), shuffled_L.end(), g);
unshuffled_L.clear();
unshuffled_L.resize(0);
for(int i = 0; i < (int)shuffled_L.size(); i++){
unshuffled_L.push_back( shuffled_L[i] );
}
}
float Tools::compute_information_rate(vector< vector< float > > WS_matrix, int p_criteria){
int n_objective = WS_matrix[0].size();
vector<vector<float > > matrix = WS_matrix;
float uv = 0, u_norme = 0, v_norme = 0;
//compute angle
if(p_criteria == 2){
for(int i = 0; i < 2 ; i++){
uv += matrix[i][0] * matrix[i][1] ;
u_norme += matrix[i][0] * matrix[i][0];
v_norme += matrix[i][1] * matrix[i][1];
}
if( sqrt(u_norme * v_norme) == 0 ) return 0;
float degree = acos(uv*1.0 / sqrt(u_norme * v_norme)) * 180.0 / PI;
return degree;
}
//compute volume
if(p_criteria == 3){
vector< vector<float > > matrix = WS_matrix;
vector<float> lengths(3,0);
float maxus, minus;
for(int i = 0; i < 3; i++){
maxus = -1, minus = -1;
for(int j = 0; j < n_objective; j++){
if( WS_matrix[i][j] < minus or minus == -1 )
minus = WS_matrix[i][j];
if( WS_matrix[i][j] > maxus or maxus == -1)
maxus = WS_matrix[i][j];
}
lengths[i] = abs(maxus - minus);
}
return lengths[0] * lengths[1] * lengths[2];
}
//compute MONTE CARLO APPORIXMATION
return -1;
}
vector< map< string, float > > Tools::readOPTalgoFile(string datafile, int inst_name){
ifstream fic_read(datafile);
if (!(fic_read)){
cerr<<"Error occurred readEvaluationFile Tools class "<<endl;
}
vector< string > keys ={"Type", "Size", "Instance", "Budget", "PopSize", "Info", "nb_evaluation", "AVG_dist", "MaxMin", "PR", "Diversification"};
string line;
vector< float > vector_line;
vector< map< string, float > > lines_data;
int inst_id_index = 2;
getline(fic_read,line);
while(!fic_read.eof()){
getline(fic_read,line);
vector_line = Tools::decompose_line_to_float_vector(line);
if (line.size() == 0 or vector_line[inst_id_index] != inst_name)
continue;
map< string, float > param;
for(size_t i = 0; i < keys.size(); i++){
param[ keys[i] ] = vector_line[i];
}
lines_data.push_back(param);
}
fic_read.close();
return lines_data;
}
void Tools::separate_results(string filename, string separator){
ofstream fic_write(filename.c_str(), ios::app);
fic_write<<separator<<endl;
fic_write.close();
}
void Tools::copy_into(string src_filename, string dest_filename){
ifstream src(src_filename.c_str());
ofstream dest(dest_filename.c_str());
string line;
while(!src.eof()){
getline(src,line);
dest<<line<<endl;
}
src.close();
dest.close();
}
/***
* **************************************************************************************************
*/
vector< float > Tools::transform(vector< float > src, vector< vector< float > > ws_matrix){
vector< float > dest(src.size(),0);
for(int i = 0; i < (int)ws_matrix[0].size(); i++){
for(int j = 0; j < (int)src.size() ; j++){
dest[i] += ws_matrix[j][i] * src[j];
}
}
return dest;
}
bool Tools::in_search_space(vector<float> v,vector<float> minus, vector<float> maxus){
for(int j = 0; j < (int)v.size(); j++)
if( (v[j] < minus[j] ) or (v[j] > maxus[j]) )
return false;
return true;
}
/***
* **************************************************************************************************
*/
vector<float> Tools::generate_random_WS_aggregator(int n_w){
float sum = 0;
vector<float> weighted_sum;
// srand(time(NULL));
if(n_w == 2){
float wi = rand()*1.0/RAND_MAX;
weighted_sum.push_back(wi);
weighted_sum.push_back(1.0 - wi);
return weighted_sum;
}
for(int i =0; i < n_w - 1; i++){
float wi = rand()*1.0/RAND_MAX;
while(wi + sum > 1){
wi = rand();
}
sum += wi;
weighted_sum.push_back(wi);
}
weighted_sum.push_back(1.0 - sum);
return weighted_sum;
}
void Tools::generate_random_WS(string filename, int nb_criteria){
vector<float > weighted_sum = generate_random_WS_aggregator(nb_criteria);
ofstream fic(filename.c_str());
for(int i = 0; i < (int)weighted_sum.size(); i++)
fic<<weighted_sum[i]<<endl;
fic.close();
}
vector<float> Tools::generate_random_restricted_WS_aggregator(int p_criteria, vector< vector< float > > ws_matrix){
vector<float> weighted_sum(p_criteria);
//mono-objective
if(ws_matrix[0].size() == 1){
for(size_t i = 0; i < ws_matrix[0].size(); i++)
weighted_sum[i] = ws_matrix[0][i];
}
//more than one objective
else if(p_criteria == 2){
// const auto [minus, maxus] = minmax_element(begin(ws_matrix[0]), end(ws_matrix[0]));
float minus = *(min_element(ws_matrix[0].begin(), ws_matrix[0].end()) );
float maxus = *(max_element(ws_matrix[0].begin(), ws_matrix[0].end()) );
int rando = rand();
float wi = static_cast <float> (rando)*1.0 /( static_cast <float> (RAND_MAX / (maxus - minus ))) + minus ;
weighted_sum[0] = wi ;
weighted_sum[1] = 1.0 - wi ;
}
else{
vector< float > rand_value;
vector< float > feasible_ws;
for(int i = 0; i < p_criteria - 1; i++){
float rando_val = static_cast <float> (rand())*1.0 /( static_cast <float> (RAND_MAX) );
rand_value.push_back( rando_val );
}
rand_value.push_back(0); rand_value.push_back(1.);
sort(rand_value.begin(), rand_value.end());
for(size_t i = 0; i < rand_value.size() - 1; i++)
feasible_ws.push_back( rand_value[i+1] - rand_value[i]);
for(int i = 0; i < p_criteria; i++){
float val = 0.;
for(size_t j = 0; j < feasible_ws.size(); j++)
val += ws_matrix[i][j] * feasible_ws[j] * 1.0;
weighted_sum[i] = val ;
}
}
// cout<<print_vector(weighted_sum)<<endl;
return weighted_sum;
}
vector<float> Tools::readWS_DM(string WS_DM_preferences){
ifstream fic_read(WS_DM_preferences.c_str());
string line;
if (!(fic_read) or WS_DM_preferences.find(".ks") == std::string::npos){
cerr<<"Error occurred readWS_DM"<<endl;
}
//read WS_DMs_preference
getline(fic_read,line);
return Tools::decompose_line_to_float_vector(line);
}
vector< vector< float > > Tools::readMatrix(string filename){
char *cline, *pch;
vector< vector <float > >WS_matrix;
ifstream fic(filename.c_str());
string line;
if (!(fic) ){
cerr<<"Error occurred readMatrix Tools"<<endl;
}
while( !fic.eof() ){
getline(fic,line);
cline = new char[line.length() + 1]; // or
std::strcpy(cline, line.c_str());
pch = strtok (cline," ,;");
vector< float > ws_line;
while (pch != NULL){
ws_line.push_back(atof(pch));
pch = strtok (NULL, " ,;");
}
if(ws_line.size() > 0)
WS_matrix.push_back(ws_line);
}
return WS_matrix;
}
string Tools::decode_set_items(set<int> items, int nb_items){
string s ="";
for(int i = 0; i < nb_items; i++)
s+= (items.find(i) != items.end())?"1":"0";
return s;
}
int Tools::dominates(vector< float > e1, vector< float > e2){
bool dominated = false, dominates = false;
for(int i = 0; i < (int)e1.size() ; i++){
if(e1[i] < e2[i])
dominated = true;
else if (e1[i] > e2[i])
dominates = true;
if(dominates and dominated)
return 0;
}
if(dominated and !dominates)
return -1;
//dominates and !dominated OR !dominated and !dominates
return 1;
}
//void Tools::update_dist_time(float dist_min,float time){
// Tools::dist_time_avg[0] += dist_min*100;
// Tools::dist_time_avg[1] += time;
//
// Tools::cpt_count++;
//
//
//}
//
//void Tools::update_indicators(float D1, float D2, float D3){
//// cout<<"BEF D1 : "<<Tools::indicator_avg[0]<<endl;
//
// Tools::indicator_avg[0] += D1;
// Tools::indicator_avg[1] += D2;
// Tools::indicator_avg[2] += D3;
//
//
//}
//
//void Tools::add_dist_to_OPT(float ratio){
//
// ratios_dist_to_OPT.push_back(ratio);
//}
//void Tools::save_average_dist_time(string filename){
//
// ofstream fic_write(filename.c_str(), ios::app);
// for(int i = 0; i < (int)Tools::dist_time_avg.size(); i++){
// Tools::dist_time_avg[i] = Tools::dist_time_avg[i]*1.0/cpt_count;
// fic_write<<Tools::dist_time_avg[i]<<" ";
// }
// fic_write<<endl;
// fic_write.close();
//
// Tools::dist_time_avg.clear();
// Tools::dist_time_avg.resize(2,0);
//
//}
//void Tools::save_average_indicator(string filename){
//
// ofstream fic_write(filename.c_str(), ios::app);
//
// for(int i = 0; i < (int)indicator_avg.size(); i++){
// Tools::indicator_avg[i] = Tools::indicator_avg[i]*1.0/cpt_count;
// fic_write<<Tools::indicator_avg[i]<<" ";
// }
// fic_write<<endl;
// fic_write.close();
//
//
// Tools::indicator_avg.clear();
// Tools::indicator_avg.resize(3,0);
//
//
//
//}
//void Tools::save_std_deviation(string filename){
// ofstream fic_write(filename.c_str(), ios::app);
//
// float avg_ratio = accumulate(ratios_dist_to_OPT.begin(), ratios_dist_to_OPT.end(), 0) / ratios_dist_to_OPT.size() * 1.0;
//
// if(ratios_dist_to_OPT.size() == 0){
// fic_write<<"-nan"<<endl;
// fic_write.close();
// Tools::ratios_dist_to_OPT.clear();
// return;
// }
//
//
// float val = 1.0/ratios_dist_to_OPT.size();
//
// for(int i = 0; i < (int)ratios_dist_to_OPT.size(); i++)
// val += ( ratios_dist_to_OPT[i] - avg_ratio ) * ( ratios_dist_to_OPT[i] - avg_ratio );
//
// fic_write<<sqrt(val)<<" ";
//
//
// for(int i = 0; i < (int)Tools::dist_time_avg.size(); i++){
// Tools::dist_time_avg[i] = Tools::dist_time_avg[i]*1.0/cpt_count;
// fic_write<<Tools::dist_time_avg[i]<<" ";
// }
// fic_write<<endl;
//
// fic_write.close();
//
// Tools::ratios_dist_to_OPT.clear();
//}