-
Notifications
You must be signed in to change notification settings - Fork 0
/
defc_v2.c
508 lines (440 loc) · 16.1 KB
/
defc_v2.c
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <float.h>
#include <time.h>
#include <string.h>
FILE *out_V, *out_X, *out_U, *out_LOG, *out_LOG_RIS, *out_csv; //puntatori a file di output
double **X;
double m = 2.0; //fuzzification
double CR; //crossover rate
//bound del differential weight
double dw_lowerbound;
double dw_upperbound;
int dw_adattivo;
int tipo_dw;
int tipo_dataset;
#define num_pop 20
typedef struct el_pop {//individuo della popolazione
double **V_p;
double **U_p;
double fitness;
} el_pop;
el_pop *POP_NEW[num_pop];
el_pop *POP_NOW[num_pop];
//attiva e disattiva GnuPlot
int attivaGnuPlot = 0;
int num_pop_iniziale, numero_generazioni, i, j, k, pop_index, numero_generazione_attuale;
double esponente_U;
double xb_selezionato;
double best_xb;
int n, c, d;
int range_init_min, range_init_max;
void stampaMatrice(int righe, int col, double **mat) {
int i, j;
printf("\n\n");
for (i = 0; i < righe; i++) {
for (j = 0; j < col; j++) {
printf("%lf", mat[i][j]);
printf(" ");
}
puts("");
}
}
void stampaMatriceSuFile(int righe, int col, double **mat, FILE *punt_file) {
int i, j;
for (i = 0; i < righe; i++) {
for (j = 0; j < col; j++) {
fprintf(punt_file, "%lf", mat[i][j]);
fprintf(punt_file, " ");
}
fprintf(punt_file, "\n");
}
fflush(punt_file);
}
double calcDistanza(double a[d], double b[d]) {
double ris = 0;
int i;
for (i = 0; i < d; i++)
ris += pow(a[i] - b[i], 2.0);
if (ris == 0) {
puts("!!!CALCOLATA UNA DISTANZA NULLA!!!");
exit(-1);
}
return sqrt(ris);
}
void copiaVettore(int dim, double input[dim], double output[dim]) {
int i;
for (i = 0; i < dim; i++) {
output[i] = input[i];
}
}
///////////////////////////////////////////////////////////////////////////////
long random_at_most(long max) {
unsigned long
num_bins = (unsigned long) max + 1,
num_rand = (unsigned long) RAND_MAX + 1,
bin_size = num_rand / num_bins,
defect = num_rand % bin_size;
long x;
while (num_rand - defect <= (unsigned long) (x = random()));
return x / bin_size;
}
double dbl_rnd_inRange(double fMin, double fMax) {//random double in un range
double f = (double) lrand48() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
///////////////////////////////////////////////////////////////////////////////
double calcolaFitness(double **V, double **U, int debug) {
//CALCOLO SIGMA
double sigma = 0.0;
for (i = 0; i < n; i++) {
for (j = 0; j < c; j++) {
sigma += pow(U[j][i], m) * pow(calcDistanza(V[j], X[i]), 2.0);
}
}
return sigma;
}
double calcolaXB(double **V, double **U, int debug) {
/*
XB funzione del rapporto fra la variazione totale sigma
e la separazione minima fra i centroidi
*/
//if(debug == 1)
// puts("debug XB");
//CALCOLO MIN_SEP
double min_sep = DBL_MAX;
int i, j;
double dist_tmp = 0;
j = 0;
for (i = 0; i < c; i++) {
if (j == i)
j++;
while (j < c) {
if (j == i)
j++;
if (j < c) {
dist_tmp = pow(calcDistanza(V[i], V[j]), 2.0);
if (dist_tmp < min_sep)
min_sep = dist_tmp;
j++;
}
}
j = 0;
}
//CALCOLO SIGMA
double sigma = 0.0;
for (i = 0; i < n; i++) {
for (j = 0; j < c; j++) {
sigma += pow(U[j][i], m) * pow(calcDistanza(V[j], X[i]), 2.0);
}
}
return sigma / (n * min_sep);
}
void init(int n, int c, int d) {
puts("");
int row;
//###INIT POPOLAZIONE 0
//init V e calcolo U
for (pop_index = 0; pop_index < num_pop_iniziale; pop_index++) {
//alloc struttura
POP_NEW[pop_index] = malloc(sizeof (el_pop));
//alloc V_p
POP_NEW[pop_index] -> V_p = malloc(c * sizeof (double*));
for (row = 0; row < c; row++) {
POP_NEW[pop_index] -> V_p[row] = malloc(d * sizeof (double));
}
//init V_p
for (i = 0; i < c; i++) {
for (j = 0; j < d; j++) {
srand48(time(0));
int riga = random_at_most(n-1);
POP_NEW[pop_index] -> V_p[i][j] = X[riga][j]+drand48();
//POP_NEW[pop_index] -> V_p[i][j] = X[random_at_most(n-1)][random_at_most(d-1)];//+drand48();//;
//POP_NEW[pop_index] -> V_p[i][j] = drand48();
//POP_NEW[pop_index] -> V_p[i][j] = dbl_rnd_inRange(0,range_init_max);
//POP_NEW[pop_index] -> V_p[i][j] = random_at_most(range_init_max);
}
}
//alloc U_p
POP_NEW[pop_index] -> U_p = malloc(c * sizeof (double*));
for (row = 0; row < c; row++) {
POP_NEW[pop_index] -> U_p[row] = malloc(n * sizeof (double));
}
//init U
for (i = 0; i < c; i++) {
for (j = 0; j < n; j++) {
double denom = 0.0;
double dist_x_j__v_i = calcDistanza(X[j], POP_NEW[pop_index] -> V_p[i]);
int k;
for (k = 0; k < c; k++) {
double dist_xj_vk = calcDistanza(X[j], POP_NEW[pop_index] -> V_p[k]);
denom += pow((dist_x_j__v_i / dist_xj_vk), esponente_U);
}
POP_NEW[pop_index] -> U_p[i][j] = 1.0 / denom;
if (POP_NEW[pop_index] -> U_p[i][j] < 0) {
printf("ERR: INIT POP:inizializzato U di un membro con negativo::");
exit(-1);
}
}
}
//calcolo fitness
double xb = calcolaFitness(POP_NEW[pop_index]->V_p, POP_NEW[pop_index]->U_p, 0);
POP_NEW[pop_index]->fitness = xb;
}
//###END INIT POPOLAZIONE 0
puts("");
}
void lavora(int n, int c, int d) {
int row;
numero_generazione_attuale = 0;
do {//NUOVA GENERAZIONE
//SCAMBIO VETTORI POPOLAZIONE
numero_generazione_attuale++;
int i_target;
for (i_target = 0; i_target < num_pop_iniziale; i_target++) {
if (POP_NEW[i_target] != POP_NOW[i_target]) {
if (POP_NOW[i_target] != 0) {
for (row = 0; row < c; row++) {
free(POP_NOW[i_target] -> V_p[row]);
free(POP_NOW[i_target] -> U_p[row]);
}
free(POP_NOW[i_target]->V_p);
free(POP_NOW[i_target]->U_p);
free(POP_NOW[i_target]);
}
POP_NOW[i_target] = POP_NEW[i_target];
}
}
/////////////////DE////////////////////
for (i_target = 0; i_target < num_pop_iniziale; i_target++) {//PER OGNI COMPONENTE DELLA POP
/*if(indice_base == i_target){
printf("!");
continue;
}*/
//SCELTA CANDIDATI
//tre vettori devono essere scelti a caso nella popolazione
//diversi dal target (indice i) e mutualmente
int indice_1, indice_2, indice_base;
do {
indice_1 = random_at_most(((long) num_pop_iniziale) - 1);
} while (indice_1 == i_target); // || indice_1 == indice_base
do {
indice_2 = random_at_most(((long) num_pop_iniziale) - 1);
} while (indice_2 == i_target || indice_2 == indice_1); // || indice_2 == indice_base
do {
indice_base = random_at_most(((long) num_pop_iniziale) - 1);
} while (indice_base == i_target || indice_base == indice_1 || indice_base == indice_2);
//l'elemento mutante
el_pop *mutant = malloc(sizeof (el_pop));
//alloc V_p del mutante
mutant -> V_p = malloc(c * sizeof (double*));
for (row = 0; row < c; row++) {
mutant -> V_p[row] = malloc(d * sizeof (double));
}
//alloc U_p mutante
mutant -> U_p = malloc(c * sizeof (double*));
for (row = 0; row < c; row++) {
mutant -> U_p[row] = malloc(n * sizeof (double));
}
//MUTATION
int i1, j1;
for (i1 = 0; i1 < c; i1++) {
double f;
if (tipo_dw == 2)//dithering
f = dbl_rnd_inRange(dw_lowerbound, dw_upperbound);
else if (tipo_dw == 3) //dither + jitter
f = dbl_rnd_inRange(dw_lowerbound, dw_upperbound) + 0.001 * (dbl_rnd_inRange(0, 1) - 0.5);
else
f = dw_upperbound;
for (j1 = 0; j1 < d; j1++) {
mutant->V_p[i1][j1] = POP_NOW[indice_base]->V_p[i1][j1] + f * (POP_NOW[indice_1]->V_p[i1][j1] - POP_NOW[indice_2]->V_p[i1][j1]);
}
}
//CROSSOVER CON IL VETTORE TARGET (TIPO 1)
for (i1 = 0; i1 < c; i1++) {
double prob_crossover = dbl_rnd_inRange(0.0, 1.0);
for (j1 = 0; j1 < d; j1++) {
if (prob_crossover < CR) {
//prendo il cromosoma del target
mutant->V_p[i1][j1] = POP_NOW[i_target]->V_p[i1][j1];
}
}
}
//CROSSOVER CON IL VETTORE ATTUALE (TIPO 2) (BAD)
/*for (i1 = 0; i1 < c; i1++) {
for (j1 = 0; j1 < d; j1++) {
double prob_crossover = fRand(0.0, 1.0);
if (prob_crossover < CR) {
mutant->V_p[i1][j1] = POP_NOW[i_CPop]->V_p[i1][j1];
}
}
}*/
//CROSSOVER CON IL VETTORE ATTUALE (TIPO 3)
/*for (i1 = 0; i1 < c; i1++) {
double prob_crossover = fRand(0.0, 1.0);
if (prob_crossover < CR) {
//prendo tutto il vettore del compagno
copiaVettore(d,POP_NOW[i_CPop]->V_p[i1],mutant->V_p[i1]);
}
}*/
///CALCOLO FITNESS DEL MUTANTE
//calcolo U mutante
for (i = 0; i < c; i++) {
for (j = 0; j < n; j++) {
double denom = 0.0;
double dist_x_j__v_i = calcDistanza(X[j], mutant -> V_p[i]);
if (dist_x_j__v_i == 0) {
puts("calcolo U mutante, distanza nulla");
exit(-1);
}
int k;
for (k = 0; k < c; k++) {
double dist_xj_vk = calcDistanza(X[j], mutant -> V_p[k]);
if (dist_xj_vk == 0) {
puts("calcolo U mutante, distanza nulla");
exit(-1);
}
denom += pow((dist_x_j__v_i / dist_xj_vk), esponente_U);
if (denom == 0) {
puts("calcolo U mutante, denom nullo");
exit(-1);
}
}
mutant -> U_p[i][j] = 1.0 / denom;
}
}
//calcolo fitness mutante
mutant->fitness = calcolaFitness(mutant->V_p, mutant->U_p, 1);
//SELECTION
//TIPO 2 (STANDARD)
if (mutant->fitness < POP_NOW[i_target]->fitness) {
xb_selezionato = mutant->fitness;
POP_NEW[i_target] = mutant;
} else {
for (row = 0; row < c; row++) {
free(mutant -> V_p[row]);
free(mutant -> U_p[row]);
}
free(mutant->V_p);
free(mutant->U_p);
free(mutant);
POP_NEW[i_target] = POP_NOW[i_target];
xb_selezionato = POP_NOW[i_target]->fitness;
}
}//END DE
numero_generazioni--;
} while (numero_generazioni > 0);
//computazione fitness della popolazione finale
double best_fitness = DBL_MAX;
int indice_best;
for (pop_index = 0; pop_index < num_pop_iniziale; pop_index++) {
POP_NEW[pop_index]->fitness = calcolaFitness(POP_NEW[pop_index]->V_p, POP_NEW[pop_index]->U_p, 0);
if (POP_NEW[pop_index]->fitness < best_fitness) {
best_fitness = POP_NEW[pop_index]->fitness;
indice_best = pop_index;
}
}
//calcolo xb del migliore
best_xb = calcolaXB(POP_NEW[indice_best]->V_p, POP_NEW[indice_best]->U_p, 0);
printf("miglior XB:%lf\n", best_xb);
fprintf(out_LOG_RIS, "\nmiglior XB:%lf\n\n", best_xb);
stampaMatriceSuFile(c, d, POP_NEW[indice_best]->V_p, out_LOG_RIS);
puts("matrice V:");
stampaMatrice(c, d, POP_NEW[indice_best]->V_p);
stampaMatriceSuFile(c, d, POP_NEW[indice_best]->V_p, out_V);
stampaMatriceSuFile(c, n, POP_NEW[indice_best]->U_p, out_U);
puts("***********************************************");
}
void plot() {
if ((d == 2 || d == 3) && attivaGnuPlot) {
char *commandsForGnuplot[] = {"set key off", "set term x11 1", "set title \"matrice X\"", "", "set term x11 2", "set key off", "set title \"DEFC - matrice V\"", ""};
if (d == 2) {
commandsForGnuplot[3] = "plot 'x.dat' pointtype 3";
commandsForGnuplot[7] = "plot 'v_defc.dat' pointtype 3";
} else if (d == 3) {
commandsForGnuplot[3] = "splot 'x.dat' pointtype 3";
commandsForGnuplot[7] = "splot 'v_defc.dat' pointtype 3";
}
FILE * gnuplotPipe = popen("gnuplot -persistent", "w");
int i;
for (i = 0; i < 8; i++) {
fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]);
fflush(gnuplotPipe);
}
}
}
int main(int argc, char** argv) {
esponente_U = 2.0 / (m - 1.0);
num_pop_iniziale = num_pop;
puts("v2: dw upperbound fisso, CR fisso");
if (argc == 0) {
//letture da utente
printf("tipo dataset: ");
scanf("%d", &tipo_dataset);
printf("numero di punti in input: ");
scanf("%d", &n);
printf("numero di dimensioni: ");
scanf("%d", &d);
printf("numero di centroidi: ");
scanf("%d", &c);
printf("numero di generazioni: ");
scanf("%d", &numero_generazioni);
printf("tipo diff. weight: 1=costante 2=dithered 3=dithered/jittered: ");
scanf("%d", &tipo_dw);
} else if (argc == 7) {
tipo_dataset = atoi(argv[1]);
n = atoi(argv[2]);
d = atoi(argv[3]);
c = atoi(argv[4]);
numero_generazioni = atoi(argv[5]);
tipo_dw = atoi(argv[6]);
} else {
puts("./defc2 tipo_ds n d c num_gen tipo_dw");
exit(0);
}
dw_upperbound = 0.7;
CR = 0.5;
//stream file
out_X = fopen("dataset/gauss4.data", "r");
out_V = fopen("v_defc2.dat", "w");
out_U = fopen("u_defc2.dat", "w");
out_LOG_RIS = fopen("log_ris", "a");
out_csv = fopen("csv/output_defcv2.csv", "a");
int ngenIniziali = numero_generazioni;
dw_lowerbound = 0.001;
fputs("\n######\n", out_LOG_RIS); //nuova log entry
//allocazione X
int row;
X = malloc(n * sizeof (double*));
for (row = 0; row < n; row++) {
X[row] = malloc(d * sizeof (double));
}
//lettura X da file
for (i = 0; i < n; i++) {
for (j = 0; j < d; j++) {
if (!fscanf(out_X, "%lf", &X[i][j]))
break;
}
}
fclose(out_X);
init(n, c, d);
lavora(n, c, d);
plot();
//scrittura csv
fprintf(out_csv,"%d,",tipo_dataset);
fprintf(out_csv, "%d,", n);
fprintf(out_csv, "%d,", c);
fprintf(out_csv, "%d,", d);
fprintf(out_csv, "%lf,", CR);
fprintf(out_csv, "%d,", num_pop);
fprintf(out_csv, "%d,", ngenIniziali);
fprintf(out_csv, "%d,", tipo_dw);
fprintf(out_csv, "%lf,", dw_lowerbound);
fprintf(out_csv, "%lf,", dw_upperbound);
//fprintf(out_csv, "%d,", range_init_min);
//fprintf(out_csv, "%d,", range_init_max);
fprintf(out_csv, "%lf\n", best_xb);
return (0);
}