/
algorithm.c
1867 lines (1504 loc) · 44.7 KB
/
algorithm.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
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Description: The ANSI C code of the PRVNS approach
Programmer: Wesklei Migliorini
E-mail: wesklei.m@gmail.com
Date: 04/11/2014
Lisence: Free
Note: The system was developed using Linux.
To compile: Type: make
To run: ./algorithm input.in
*/
#ifndef max
#define max(x, y) ((x) > (y) ? (x) : (y))
#endif
#ifndef abss
#define abss(a) (a<0 ? (-a) : a)
#endif
typedef int bool;
#define true 1
#define false 0
#define FAIL 0
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "mersenne.h"
#include "functions.c"
#include "hooke.c"
#include "nelmin.c"
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include <unistd.h> /* sleep() */
#include <string.h>
typedef struct _VNS_SOLUTION
{
double bestfo; //best fo value
int c_aval; //avaliation count total
int c_aval_best; //alaviation count when found best
time_t stime;//start time
time_t etime;//end time
double t_total;//total time
}pVNS_SOLUTION;
typedef struct _VNS
{
int P; /*The metric used, l1,l2,l_inf */
int FUNCTION;
//Problem definitions
int DIM; //number of problem variables
int RUN; /*Algorithm can run many times in order to see its robustness*/
double *best; //[DIM]; //best solution found
double bestfo; //best fo value
double LB; //lower bound of the variables
double UB; //upper bound of the variables
int KMAX,TMAX,AVAL_MAX;
int RADII_T_FLAG;
int know;
double Q,RADII,RHO,EPSILON;
double *r;
int METHOD;
pVNS_SOLUTION solv;
int ECO_STEP;
int EVO_STEP;
int VNS_POP;
int POP_INDEX;
double DELTA_MUTATION;
int G_MAX;
double PC; //probabilidade de crossover
}pVNS;
//Testes Diversidade PRVNS
#define DEBUG 0 //1=> info, 2=> all
#define GRAFICO 0//1=> convergencia pop, 2=> k
double **fo_geracoes; //fo_geracoes[RUN][Ger]; Ger < G_MAX
double *fo_mediaGeracoes; //fo_mediaGeracoes[Ger]; Ger < G_MAX
double **bestfo_geracoes; //bestfo_geracoes[Ger]; Ger < G_MAX
double *bestfo_mediaGeracoes; //bestfo_mediaGeracoes[Ger]; Ger < G_MAX
int execucao = -1;
int geracao=-1;
//Functions declarations
double randon( double inferior, double superior);
//VNS
bool radiiBetween(double *max, double *r, int know);
bool radiiLess(double *max, double *r, int know);
bool checkisLbUb(double *x, const int DIM,double lb, double ub);
void getRange(double lb, double ub, double *lower, double *upper, double new_value);
void shake(double *x, double *y, double *r, int know, const int DIM,double lb, double ub, int *cont, int RADII_T_FLAG, int p, int max_changes, double delta_percent);
void shake_one(double *x, double *y, double *r, int know, const int DIM,double lb, double ub, int *cont, int RADII_T_FLAG, int p);
bool lp(double* x, double* y, double* r, int know, const int DIM, int RADII_T_FLAG, int p);
bool lpInf(double* x, double* y, double* r, int know, const int DIM,int RADII_T_FLAG);
void neighborhoodChange(double* x, double* y, double *fx, const int DIM, int *k, const int FUNCTION, int *best_aval, int *aval, double *fy, int lb, int ub);
void *HJ(void *arg);//Only Hooke and Jaevees/
void *BVNS_NM(void *arg);//VNSM com Nelder Mead
void *NM(void *arg);//VNSM com Nelder Mead
void *RVNS(void *arg);
void *BVNS(void *arg);
void *PRVNS(void *arg);
//END VNS
//aux functions
void printvector(double* x, const int DIM, char* seq);
void calculateStatistic(pVNS **sol, int maxr);
void freeArrays(int* POP_SIZE, double** pop, double* fo, double* best);
void AllocArrays(int* POP_SIZE, int* DIM, double*** pop, double** fo, double** best, double** U);
void grafico_linhas_x_y(char *data, char *xtitle, char *ytitle, char *title, char *legend, char *filename);
void grafico_duas_linhas(char *data1,char *data2, char *xtitle, char *ytitle, char *title, char *legend1, char *legend2, char *filename);
void printProgress(double fx,int now,int total);
int getParametersVNS(FILE *file, pVNS *vns);
void showParametersVNS(pVNS *vns);
int f(double x1, int FUNCTION);
double randon( double inferior, double superior)
{
// double aux = (float)inferior + ((superior - inferior)*rand()/(RAND_MAX+1.0));
double aux = (double)inferior + ((superior - inferior)*((double)MT_randInt(RAND_MAX)/((double)RAND_MAX+1.0)));
/* printf("min=%f max=%f rand=%f\n",inferior,superior,aux); */
return aux;
}
/*Main program of the search algorithm*/
int main(int argc, char **argv)
{
srand(time(NULL));
MT_seed();
//when teste flag is used
if(argc > 0 && strcmp(argv[1],"teste") == 0)
{
//put teste functions her
return 0;
}
/* Control Parameters of the search algorithm*/
int FUNCTION;
//Problem definitions
int RUN; /*Algorithm can run many times in order to see its robustness*/
int i, j;
double lb; //lower bound of the variables
double ub; //upper bound of the variables
pVNS *vns_run = (pVNS *) malloc (sizeof (pVNS));
//read input file
FILE *file = fopen( argv[1], "r" );
if (getParametersVNS(file,vns_run) == -1 ){ //read input file
return 0;
}
fclose(file);
showParametersVNS(vns_run);
RUN = vns_run->RUN;
FUNCTION = vns_run->FUNCTION;
prepararObjFunc(&FUNCTION,&lb,&ub);//initialize variables needed by function
vns_run->LB = lb;
vns_run->UB = ub;
int maxr=RUN;
pVNS **sol = (pVNS **) malloc (sizeof (pVNS*)*maxr);
for(i=0;i<maxr;i++){
sol[i] = (pVNS*) malloc (sizeof (pVNS));
memcpy(sol[i],vns_run,sizeof(pVNS));
sol[i]->RUN=i;
sol[i]->best = malloc(sizeof(double) * vns_run->DIM);
for (j=0; j<vns_run->DIM;j++) //each dimension
{
sol[i]->best[j] = randon(vns_run->LB,vns_run->UB);
}
}
//executa o metodo escolhido pelo arquivo de entrada
//aloca para graficos
if(GRAFICO){
fo_geracoes = (double**) malloc(sizeof(double*) * RUN + 2);
bestfo_geracoes = (double**) malloc(sizeof(double*) * RUN + 2);
for(i=0;i<RUN;i++){
if(vns_run->METHOD == 6) { //PRVNS
fo_geracoes[i] = (double*) malloc(sizeof(double) * vns_run->G_MAX + 2 );
bestfo_geracoes[i] = (double*) malloc(sizeof(double) * vns_run->G_MAX + 2);
}else{
fo_geracoes[i] = (double*) malloc(sizeof(double) * vns_run->TMAX + 2 );
bestfo_geracoes[i] = (double*) malloc(sizeof(double) * vns_run->TMAX + 2);
}
}
if(vns_run->METHOD == 6) { //PRVNS
fo_mediaGeracoes = (double*) malloc(sizeof(double) * vns_run->G_MAX + 2);
bestfo_mediaGeracoes = (double*) malloc(sizeof(double) * vns_run->G_MAX + 2);
}else{
fo_mediaGeracoes = (double*) malloc(sizeof(double) * vns_run->TMAX + 2);
bestfo_mediaGeracoes = (double*) malloc(sizeof(double) * vns_run->TMAX + 2);
}
}
double *r = (double*)malloc ( (vns_run->KMAX+1) *sizeof(double));
double radii = vns_run->RADII;
switch(vns_run->METHOD)
{
case 3:
case 4:
case 5:
printf("Valores de raio usando PG e razao %f:\n",vns_run->Q);
//set wich radii value
r[0] = 0.0f;
for(j=1;j<=vns_run->KMAX;j++){
r[j] = radii;
radii*=vns_run->Q;
}
for(j=1;j<=vns_run->KMAX;j++){
printf("k_%d => radii=%f \n",j,r[j]);
}
break;
case 6:
printf("Valores de raio usando PG e razao %f:\n",vns_run->Q);
r[0] = 0.1f;
r[1] = 0.3f;
r[2] = 0.5f;
r[3] = 0.7f;
r[4] = 0.9f;
for(j=0;j<vns_run->KMAX;j++){
printf("k_%d => radii=%f \n",j+1,r[j]);
}
break;
}
for(i=0;i<maxr;i++){
if(GRAFICO){
execucao++;
}
switch (vns_run->METHOD) {
case 2: //HJ
sol[i] = HJ((void*)sol[i]);
break;
case 3: //BVNS
sol[i] = BVNS((void*)sol[i]);
break;
case 4: //RVNS
sol[i] = RVNS((void*)sol[i]);
break;
case 5: //BVNS_NM
sol[i] = BVNS_NM((void*)sol[i]);
break;
case 6: //PRVNS
sol[i] = PRVNS((void*)sol[i]);
break;
case 7: //NM - Only Nelder Mead
sol[i] = NM((void*)sol[i]);
break;
default:
printf("Info: Invalid method.\n") ;
exit(0);
}
}
//GRAFICOS
//INICIO Grafico Convergencia inicializacao
#ifdef GRAFICO
char *vns_plot_best;
char *vns_plot_mean;
char file_name[100];
if(GRAFICO == 1){
vns_plot_best = (char *) malloc(sizeof(char) * vns_run->TMAX * 10);
vns_plot_mean = (char *) malloc(sizeof(char) * vns_run->TMAX * 10);
sprintf(file_name,"Convergencia/VNS_CONVERGENCIA_%d_%d_%d_%d",vns_run->METHOD,vns_run->DIM,vns_run->RUN,vns_run->FUNCTION);
}
//FIM Grafico Convergencia inicializacao
if(GRAFICO == 1 && vns_run->METHOD == 4){//para RVNS
for(i=0;i<=geracao;i++){
double med_best = 0.0f;
for(j=0;j<RUN;j++){
med_best += bestfo_geracoes[j][i];
}
bestfo_mediaGeracoes[i] = med_best/RUN;
//salva em string para gerar pelo gnuplot
sprintf(vns_plot_best, "%s %d %lf\n ",vns_plot_best,i,bestfo_mediaGeracoes[i]);
if(DEBUG){
/* printf("\nMedia melhor execucoes Geracao[%d] =>%f\n",i,bestfo_mediaGeracoes[i]); */
}
}
//escreve em arquivo
grafico_linhas_x_y(vns_plot_best,"FO", "Geracoes", "RVNS", "Melhor",file_name);
}else if(GRAFICO == 1 && vns_run->METHOD != 4){
for(i=0;i<=geracao;i++){
double med_med = 0.0f;
double med_best = 0.0f;
for(j=0;j<RUN;j++){
med_med += fo_geracoes[j][i];
med_best += bestfo_geracoes[j][i];
}
fo_mediaGeracoes[i] = med_med/RUN;
bestfo_mediaGeracoes[i] = med_best/RUN;
//salva em string para gerar pelo gnuplot
sprintf(vns_plot_mean, "%s %d %lf\n ",vns_plot_mean,i,fo_mediaGeracoes[i]);
sprintf(vns_plot_best, "%s %d %lf\n ",vns_plot_best,i,bestfo_mediaGeracoes[i]);
if(DEBUG){
printf("\nMedia execucoes Geracao[%d] =>%f\n",i,fo_mediaGeracoes[i]);
printf("\nMedia melhor execucoes Geracao[%d] =>%f\n",i,bestfo_mediaGeracoes[i]);
}
}
//escreve em arquivo
grafico_duas_linhas(vns_plot_best,vns_plot_mean,"Geracoes","FO","PRVNS","Melhor","Media",file_name);
}
#endif
//realiza os calculos necessarios
calculateStatistic(sol,maxr);
//liberar memoria
for(i=0;i<maxr;i++){
free(sol[i]);
}
free(sol);
free(vns_run);
return 0;
}//end MAIN
void calculateStatistic(pVNS **sol, int maxr){
//faz os calculos necessarios, como media, desvio padrao, etc
int i;
double fo_best = sol[0]->solv.bestfo;
double t_best = sol[0]->solv.t_total;
int run_best = sol[0]->solv.c_aval;
int run_when_best = sol[0]->solv.c_aval_best;
double fo_sum = 0.0;
double t_sum = 0.0;
double run_sum = 0.0;
double fo_mean = 0.0;
double t_mean = 0.0;
double run_mean = 0.0;
double fo_vari = 0.0;
double t_vari = 0.0;
double run_vari = 0.0;
double fo_devi = 0.0;
double t_devi = 0.0;
double run_devi = 0.0;
double fo_sdevi = 0.0;
double t_sdevi = 0.0;
double run_sdevi = 0.0;
for(i=0;i<maxr;i++){
fo_sum += sol[i]->solv.bestfo;
if(fo_best > sol[i]->solv.bestfo){
fo_best = sol[i]->solv.bestfo;
}
t_sum += sol[i]->solv.t_total;
if(t_best > sol[i]->solv.t_total){
t_best = sol[i]->solv.t_total;
}
run_sum += sol[i]->solv.c_aval;
if(run_best > sol[i]->solv.c_aval){
run_best = sol[i]->solv.c_aval;
run_when_best = sol[i]->solv.c_aval_best;
}
}
fo_mean = fo_sum/maxr;
t_mean = t_sum/maxr;
run_mean = run_sum/maxr;
for(i = 0; i < maxr; ++i){
fo_devi += pow((sol[i]->solv.bestfo - fo_mean),2);
t_devi += pow((sol[i]->solv.t_total - t_mean),2);
run_devi += pow((sol[i]->solv.c_aval - run_mean),2);
}
fo_vari = fo_devi / (maxr);
t_vari = t_devi / (maxr);
run_vari = run_devi / (maxr);
fo_sdevi = sqrt(fo_vari);
t_sdevi = sqrt(t_vari);
run_sdevi = sqrt(run_vari);
printf("FO Standard Deviation: %6.10f\n", fo_sdevi);
printf("Time Standard Deviation: %f\n", t_sdevi);
printf("Iteration Standard Deviation: %6.3f\n", run_sdevi);
printf("FO mean: %6.10f\n", fo_mean);
printf("Time mean: %f\n", t_mean);
printf("Iteration mean: %6.3f\n", run_mean);
printf("FO best: %6.10f\n", fo_best);
printf("Time best: %f\n", t_best);
printf("Iteration count when found best: %d\n", run_when_best);
printf("Iteration best: %d\n", run_best);
printf("End running\n");
}
void AllocArrays(int* POP_SIZE, int* DIM, double*** pop, double** fo, double** best, double** U)/* Dynamic array allocation */
/* mol and sites */
{
int j;
double** aux = malloc ((*POP_SIZE)*sizeof(double*));
for (j = 0;j < *POP_SIZE; j++){
aux[j] = (double*)malloc (*DIM * sizeof(double));
}
*pop = aux;
*fo = (double*)malloc (*POP_SIZE*sizeof(double));
*best = (double*)malloc (*DIM*sizeof(double));
*U = (double*)malloc(*DIM * sizeof(double));
}
void freeArrays(int* POP_SIZE, double** pop, double* fo, double* best)/* Free arrays */
{
int i;
free(fo);
free(best);
for (i = 0; i < *POP_SIZE; i++){
free(pop[i]);
}
free(pop);
}
int getParametersVNS(FILE *file, pVNS *vns)
{
int *P,*ECO_STEP,*EVO_STEP,*VNS_POP,*G_MAX,*FUNCTION,*DIM,*RUN;
int *KMAX,*TMAX,*AVAL_MAX,*METHOD;
int *RADII_T_FLAG;
double *Q,*RADII,*RHO,*EPSILON,*PC;
FUNCTION = (int*) malloc(sizeof(int));
DIM = (int*) malloc(sizeof(int));
RUN = (int*) malloc(sizeof(int));
ECO_STEP = (int*) malloc(sizeof(int));
EVO_STEP = (int*) malloc(sizeof(int));
VNS_POP = (int*) malloc(sizeof(int));
KMAX = (int*) malloc(sizeof(int));
TMAX = (int*) malloc(sizeof(int));
METHOD = (int*) malloc(sizeof(int));
AVAL_MAX = (int*) malloc(sizeof(int));
RADII_T_FLAG = (int*) malloc(sizeof(int));
Q = (double*) malloc(sizeof(double));
RADII = (double*) malloc(sizeof(double));
RHO = (double*) malloc(sizeof(double));
EPSILON = (double*) malloc(sizeof(double));
P = (int*) malloc(sizeof(int));
G_MAX = (int*) malloc(sizeof(int));
PC = (double*) malloc(sizeof(double));
if (file == 0)
{
printf( "Could not open ini file! Usage ./<exec> <file.in>\n" );
return -1;
}
else
{
ffscanf("RUN", file, "%d", &RUN);
ffscanf("DIM", file, "%d", &DIM);
ffscanf("FUNCTION",file, "%d", &FUNCTION);
ffscanf("KMAX", file, "%d", &KMAX);
ffscanf("TMAX", file, "%d", &TMAX);
ffscanf("Q", file, "%lf", &Q);
ffscanf("RADII", file, "%lf", &RADII);
ffscanf("RADII_T_FLAG", file, "%d", &RADII_T_FLAG);
ffscanf("AVAL_MAX", file, "%d", &AVAL_MAX);
ffscanf("RHO", file, "%lf", &RHO);
ffscanf("EPSILON", file, "%lf", &EPSILON);
ffscanf("METHOD", file, "%d", &METHOD);
ffscanf("P", file, "%d", &P);
ffscanf("VNS_POP", file, "%d", &VNS_POP);
ffscanf("ECO_STEP", file, "%d", &ECO_STEP);
ffscanf("EVO_STEP", file, "%d", &EVO_STEP);
ffscanf("G_MAX", file, "%d", &G_MAX);
ffscanf("PC", file, "%lf", &PC);
vns->RUN = *RUN;
vns->DIM = *DIM;
vns->FUNCTION = *FUNCTION;
vns->KMAX = *KMAX;
vns->TMAX = *TMAX;
vns->Q = *Q;
vns->RADII = *RADII;
vns->RADII_T_FLAG = *RADII_T_FLAG;
vns->AVAL_MAX = *AVAL_MAX;
vns->RHO = *RHO;
vns->EPSILON = *EPSILON;
vns->METHOD = *METHOD;
vns->P = *P;
vns->VNS_POP = *VNS_POP;
vns->ECO_STEP = *ECO_STEP;
vns->EVO_STEP = *EVO_STEP;
vns->G_MAX = *G_MAX;
vns->PC = *PC;
return 1;
}
}
void showParametersVNS(pVNS *vns)
{
printf("***VNS PARAMETERS***\n");
printf("RUNS = %d\n", vns->RUN);
printf("DIM = %d\n", vns->DIM);
printf("FUNCTION = %s\n", getFunctionName(vns->FUNCTION));
printf("METHOD = %d\n", vns->METHOD);
printf("****************\n");
printf("KMAX = %d\n",vns->KMAX);
printf("TMAX = %d\n", vns->TMAX);
printf("Q = %f\n", vns->Q);
printf("RADII = %f\n", vns->RADII);
printf("RADII_T_FLAG = %d\n", vns->RADII_T_FLAG);
printf("METRIC P = %d\n", vns->P);
printf("VNS_POP = %d\n", vns->VNS_POP);
printf("ECO_STEP = %d\n", vns->ECO_STEP);
printf("EVO_STEP = %d\n", vns->EVO_STEP);
printf("****************\n");
printf("***HOOKE AND JEEVES PARAMETERS***\n");
printf("AVAL_MAX = %d\n", vns->AVAL_MAX);
printf("RHO = %f\n", vns->RHO);
printf("EPSILON = %f\n", vns->EPSILON);
printf("****************\n");
}
int ffscanf(char *fieldname, FILE *fp, char *format, void** inbuffer)
{
char buffer[100];
int len;
int commentflag = 0;
char *pch;
char *pch2;
do
{
if(fgets(buffer, 99, fp) == 0){
return FAIL;
}
buffer[99] = '\0';
len = strlen(buffer);
if (buffer[len - 1] == '\n'){
buffer[len - 1] = '\0';
}
switch (commentflag)
{
case 0:
if (strstr(buffer, "/*") != 0)
{
commentflag = 1;
if (strstr(buffer, "*/") != 0){
commentflag = 2;
}
}
break;
case 1:
if (strstr(buffer, "*/") != 0){
commentflag = 2;
}
break;
case 2:
if (strstr(buffer, "/*") != 0)
{
commentflag = 1;
if (strstr(buffer, "*/") != 0){
commentflag = 2;
}
}
else{
commentflag = 0;
}
break;
}
}while(commentflag != 0);
//separate field name: token = "="
if (strstr (buffer, fieldname) != 0)
{
pch = strtok (buffer,"=");
pch2 = pch;
while (pch != NULL)
{
pch2 = pch;
pch = strtok (NULL, "= ");
}
sscanf(pch2, format, *inbuffer);
return 1;//ok
}
else{
return 0;
}
}
void *HJ(void *arg){//Only Hooke and Jaevees
pVNS *vns = arg;
int t = 0,i;
bool stop = false;
int avalmax_aux=0;
double *x;
double fx;
int run=vns->RUN;
x = (double*) malloc (vns->DIM * sizeof(double));
run = vns->RUN+1;
t=0;
avalmax_aux=0;
stop=false;
for (i=0; i<vns->DIM;i++) //each dimension
{
x[i] = randon(vns->LB,vns->UB);
}
fx = objfunc(x,&vns->FUNCTION,&vns->DIM,&t);//initialize fx
avalmax_aux = vns->AVAL_MAX;
time (&(vns->solv.stime));
while(t < vns->TMAX && !stop){
/* t++; */
if( (vns->TMAX - t) < avalmax_aux){
avalmax_aux = vns->TMAX -t;
}
hooke(vns->DIM, x, x, vns->RHO, vns->EPSILON, avalmax_aux, vns->FUNCTION,&t);
checkisLbUb(x,vns->DIM,vns->LB,vns->UB);
/* t = t + hooke_cont; */
//change neighborhood, x<= receive the best or the same
fx = objfunc(x,&vns->FUNCTION,&vns->DIM,&t);
/* if(fx == 0.0f){ */
/* if(vns->FUNCTION == 8){ */
/* if(-418.0 > fx){ */
/* printf("Find the best solution before finis2h!\n"); */
/* stop = true; */
/* }else{ */
/* continue; */
/* } */
/* }else if(fx < (1E-10)){ */
/* printf("Find the best solution before finish!\n"); */
/* stop = true; */
/* } */
/* printf("Improved with %d avaliation and %.20f value\n",t,fx); */
}
time (&(vns->solv.etime));
printf("\n==RUN: %d\n",run);
printf("Total number of avaliation with HJ: %d\n",t);
printf("Best solution found == %.10f\n",fx);
vns->solv.c_aval=t;
vns->solv.c_aval_best=t;
vns->solv.bestfo=fx;
printf("\n");
free(x);
vns->solv.t_total = difftime(vns->solv.etime,vns->solv.stime);
return (void*)arg;
}
void *NM(void *arg){//Only Nelder Mead
pVNS *vns = arg;
int t = 0,i;
bool stop = false;
double *x;
int nelmin_cont = 0;
double *simplex;
int num_restart = 0;
double stop_condition = 1E-20;
int ifault = 0;
double fx = 0;
int run=vns->RUN;
x = (double*) malloc (vns->DIM * sizeof(double));
simplex = (double*) malloc ((vns->DIM +1) * sizeof(double));
run = vns->RUN+1;
t=0;
nelmin_cont=0;
stop=false;
for (i=0; i<vns->DIM;i++) //each dimension
{
x[i] = randon(vns->LB,vns->UB);
}
time (&(vns->solv.stime));
while(t < vns->TMAX && !stop){
/* t++; */
for(i=0;i<vns->DIM;i++){
simplex[i] = x[i]; //inicializa o simplex com indice igual, menos o final
}
simplex[vns->DIM] = randon(vns->LB,vns->UB); //o ultimo faz aleatorio
nelmin(vns->DIM, x, x, &fx, stop_condition , simplex,vns->TMAX - t, &nelmin_cont, &num_restart, &ifault, vns->FUNCTION );
t+=nelmin_cont;
nelmin_cont = 0;
if(checkisLbUb(x,vns->DIM,vns->LB,vns->UB)){
fx = objfunc(x,&vns->FUNCTION,&vns->DIM,&t); //<= fx vem do nelmin, so atualiza se mudar pelo checkisLBUL
}
/* t = t + nelmin_cont; */
/* if(fx == 0.0f){ */
/* if(vns->FUNCTION == 8){ */
/* if(-418.0 > fx){ */
/* printf("Find the best solution before finis2h!\n"); */
/* stop = true; */
/* }else{ */
/* continue; */
/* } */
/* }else if(fx < (1E-10)){ */
/* printf("Find the best solution before finish!\n"); */
/* stop = true; */
/* } */
/* printf("Improved with %d avaliation and %.20f value\n",t,fx); */
}
time (&(vns->solv.etime));
vns->solv.t_total = difftime(vns->solv.etime,vns->solv.stime);
printf("\n==RUN: %d\n",run);
printf("Total number of avaliation with NM: %d\n",t);
printf("Best solution found == %.10f\n",fx);
vns->solv.c_aval=t;
vns->solv.c_aval_best=t;
vns->solv.bestfo=fx;
printf("\n");
free(x);
free(simplex);
return (void*)arg;
}
void *BVNS_NM(void *arg){//VNSM com NM
pVNS *vns = arg;
int t = 0,k=1,i,j;
int best_aval = 0;
bool stop = false;
int shake_cont =0;
int vns_cont = 0;
double *x,*x2,*y,*r;
double fx,fy;
int run=vns->RUN;
int p=vns->P;
int nelmin_cont = 0;
double *simplex;
int shake_one = 1;
int avalmax_aux=0;
int num_restart = 0;
int ifault = 0;
simplex = (double*) malloc ((vns->DIM +1) * sizeof(double));
x = (double*) malloc (vns->DIM * sizeof(double));
x2 = (double*) malloc (vns->DIM * sizeof(double));
y = (double*)malloc (vns->DIM * sizeof(double));
r = (double*)malloc ( (vns->KMAX+1) *sizeof(double));
//set wich radii value
r[0] = 0.0f;
for(j=vns->KMAX;j>=1;j--){
r[j] = vns->RADII;
vns->RADII*=vns->Q;
}
run = vns->RUN+1;
t=0;
k=1;
vns_cont=0;
int vns_geral_cont=0;
shake_cont=0;
stop=false;
for (i=0; i<vns->DIM;i++) //each dimension
{
x[i] = randon(vns->LB,vns->UB);
}
memcpy(x2,x,vns->DIM*sizeof(double));//set as start value, just for initialize
memcpy(y,x,vns->DIM*sizeof(double));//set as start value, just for initialize
fx = objfunc(x,&vns->FUNCTION,&vns->DIM,&t);//initialize fx
time (&(vns->solv.stime));
while(t < vns->TMAX && !stop){
k=1;
while(k<=vns->KMAX && t< vns->TMAX && !stop){
//do shake */
shake(x, y, r, k,vns->DIM,vns->LB,vns->UB,&shake_cont,vns->RADII_T_FLAG,p,shake_one,1.0f);
for(i=0;i<vns->DIM;i++){
simplex[i] = x[i]; //inicializa o simplex com indice igual, menos o final
}
simplex[vns->DIM] = randon(vns->LB,vns->UB); //o ultimo faz aleatorio
if( (vns->TMAX - t) < avalmax_aux){
avalmax_aux = vns->TMAX -t;
}
nelmin(vns->DIM, x, x, &fx, vns->EPSILON , simplex,avalmax_aux, &nelmin_cont, &num_restart, &ifault, vns->FUNCTION );
avalmax_aux = vns->AVAL_MAX;
/* printf("nelmin_cont => %d valor => %.2f\n",nelmin_cont,fx); */
t+=nelmin_cont;
nelmin_cont = 0;
if(checkisLbUb(x,vns->DIM,vns->LB,vns->UB)){
fx = objfunc(x,&vns->FUNCTION,&vns->DIM,&t); //<= fx vem do nelmin, so atualiza se mudar pelo checkisLBUL
}
//change neighborhood, x<= receive the best or the same
neighborhoodChange(x,x2,&fx,vns->DIM,&k,vns->FUNCTION,&best_aval,&vns_cont,&fy,vns->LB,vns->UB);
t+=vns_cont;
vns_geral_cont+=vns_cont;
vns_cont=0;
/* if(vns->FUNCTION == 8){ */
/* if(-418.0 > fx){ */
/* printf("Find the best solution before finis2h!\n"); */
/* stop = true; */
/* }else{ */
/* continue; */
/* } */
/* }else if(fx < (1E-10)){ */
/* printf("Find the best solution before finish!\n"); */
/* stop = true; */
/* } */
/* if(fx == fy){ */
/* printf("Improved with %d avaliation and %.20f value \n",t,fx); */
/* } */
}
}
t--;
time (&(vns->solv.etime));
vns->solv.t_total = difftime(vns->solv.etime,vns->solv.stime);
printf("\n==RUN: %d\n",run);
/* printf("VNS total number of avaliations : %d\n",vns_geral_cont); */
/* printf("NM total number of avaliations: %d\n",nm_geral_cont); */
printf("Total number of avaliation: %d\n",t);
printf("Best solution found == %.10f\n",fx);
printf("Time: == %.0f seconds\n",vns->solv.t_total);
vns->solv.c_aval=t;
vns->solv.c_aval_best=best_aval;
vns->solv.bestfo=fx;
printf("\n");
free(x);
free(x2);
free(y);
free(r);
return (void*)arg;
}
void *RVNS(void *arg){//Reduced VNS
pVNS *vns = arg;
int t = 0,k=1,j;
int best_aval = 0;
int shake_one = 1;
bool stop = false;
int avalmax_aux=0;
int shake_cont =0;
int vns_cont = 0;
double *x,*x2,*y,*r;
double fx,fy;
int run=vns->RUN;
int p = vns->P;
x = malloc (vns->DIM * sizeof(double));
x2 = malloc (vns->DIM * sizeof(double));
y = malloc (vns->DIM * sizeof(double));
//GRAFICOS
//INICIO Grafico Convergencia inicializacao
#ifdef GRAFICO
char *vns_plot_k;
/* char *vns_plot_best; */
char file_name[100];
if(GRAFICO == 1){
/* vns_plot_best = (char *) malloc(sizeof(char) * vns->TMAX * 1000); */
sprintf(file_name,"Convergencia/VNS_%d_%d_%d_%d",vns->METHOD,vns->DIM,vns->FUNCTION,vns->RUN);
}else if(GRAFICO == 2){
vns_plot_k = (char *) malloc(sizeof(char) * vns->TMAX * 1000);
sprintf(file_name,"Convergencia/VNS_K_%d_%d_%d_%d",vns->METHOD,vns->DIM,vns->FUNCTION,vns->RUN);
}
#endif
//FIM Grafico Convergencia inicializacao
r = (double*)malloc ( (vns->KMAX+1) *sizeof(double));
if(vns->r == NULL)
vns->r = (double*)malloc ( (vns->KMAX+1) *sizeof(double));
//set wich radii value
r[0] = 0.0f;
for(j=1;j<=vns->KMAX;j++){
r[j] = vns->RADII;
vns->r[j] = r[j];
vns->RADII*=vns->Q;
}
/* memcpy(vns->r,r,sizeof(double)* (vns->KMAX + 1)); */
run = vns->RUN+1;
t=0;
k=1;
vns_cont=0;
avalmax_aux=0;
shake_cont=0;
stop=false;
memcpy(x,vns->best,sizeof(double)*vns->DIM);
memcpy(x2,x,vns->DIM*sizeof(double));//set as start value, just for initialize
memcpy(y,x,vns->DIM*sizeof(double));//set as start value, just for initialize
fx = objfunc(x,&vns->FUNCTION,&vns->DIM,&t);//initialize fx
avalmax_aux = vns->AVAL_MAX;
//GRAFICOS