-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathILS_Modificada.c
1742 lines (1390 loc) · 57.5 KB
/
ILS_Modificada.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
//
//============================================================================
// Name : Main-ILS.c
// Author : Nicholas Richers
// E-mail : nicholasrichers@gmail.com
// Institution : Federal University of Rio de Janeiro
// Created by Nicholas Richers on 11/19/17.
//============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#define MAX 128
////////////////////////Choose path to read and write your instances:///////////////////////////////////////////////////////
char file_name_read[MAX]="/Users/nicholasrichers/Dropbox/UFRJ/MHOC/Trabalho/SPP-ILS-vfinal-Large/SPP-ILS-vfinal-Large/"; //
char file_list[MAX] = "Instances_list.txt"; //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int Panel_Configuration=0;
char instance_list[MAX];
char file_name_write[MAX];
bool cannot_read_file=false;
bool reset_lottery_bool=false;
bool reset_instance=false;
int reset_count=0;
bool check_solution=true;
typedef struct Criteria Criteria;
struct Criteria
{
int iterations_Optimal_zone;
int global_iterations;
int global_Max_Iter;
int global_Max_Iter2;
int Quit_operarions;
int reach_zone;
float acceptance_zone;
float acceptance_zone_relaxed;
float acceptance_zone_relaxed2;
int Max_Iter;
int best_solution_improvement;
int Optimal_solution_improvement;
float calibration;
float calibration_buffer;
float Lottery_Bounds;
float GlobalOptimal_value;
double cpu_time_used;
double global_cpu_time_used;
float Optimal_percentage;
};
typedef struct Set Set;
struct Set
{
int nrows; //first line, first element
int ncols; //first line, second element
int* subsetsCost; //following lines, first element
int* nrows_covered; //folowing lines, second element
int** subsets; //folowing lines, following elements [also includes cost and nrows]
int* index_frequency; //How many times index appear
int* survived; //column elimination
int nsurvived; //column elimination count
int* rank_frequency; //rank rows index frequency
};
typedef struct Solution Solution;
struct Solution
{
int SolutionCost;
int SolutionCost_prev;
int SolutionSize;
int elem_changed;
int elem_blocked;
int* subSets;
int* list_elements_covered;
int* SolutionIndex;
int* lottery_changed;
int* lottery_blocked;
int optimal_iteration;
};
Criteria* criteria;
Set* set;
Set* miniSet;
Set* nanoSet;
Solution* solution;
Solution* miniSolution;
Solution* bestSolution;
Solution* OptimalSolution;
//(1st block)
void Control_Panel(void);
void createCriteriaStruct(void);
void createSetStruct(void);
void createSolutionStruct(void);
//(2nd block)
void copySetToMini(void);
void copySetToNano(void);
void copyMiniToNano(void);
void copySolutionTominiSolution(void);
void copyminiSolution_To_Solution(void);
void copyBestSolution_To_Solution(void);
void copyOptimalSolution_To_Solution(void);
void copySolutionToBest(void);
void copyBestSolutionToOptimal(void);
void copyBestTosolution(void);
//(3rd block)
bool Get_File(FILE *ff, char f_row[]);
bool Get_Line(FILE *f, char f_row[]);
void read_file(void);
//(4th block)
int sort_index_frequency(Set *set, int try_index);
void sortSubsets_cost(Set *set);
//(5th block)
void add_subset(Set *nanoSet,int subSetIndex);
void remove_subset(Set *miniSet,int subSetIndex);
bool check_subset(int subSetIndex);
int subset_frequency(Set* nanoSet, int index);
int find_subset_index(Set* nanoSet, int index, int try);
void delete_set(void);
//(6th block)
bool checkSolution(Solution* solution);
void delete_solution(void);
void greedy_solution(void);
void matrix_reduction(void);
void greedy_solution_original(void);
//(7th block)
void Column_elimination(Set *set);
void Column_elimination_mini(Set *miniSet);
void Column_elimination_nano(Set *nanoSet);
void subsets_lottery(float cali);
int reset_lottery(void);
void local_search(void);
//(8th block)
void disturbance(void);
bool acceptance_criteria(void);
void press_results(void);
void delete_structs(void);
//main
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main(void) {
clock_t start_global, end_global;
start_global = clock();
FILE *List_Instances;
double global_cpu_time_used;
char f_row[1024];
char read[1024];
bool Not_EOF;
criteria = (Criteria *) malloc(sizeof(Criteria));
createCriteriaStruct();
strcpy(read, file_name_read);
strcat(read, file_list);
List_Instances = fopen(read, "r");
if(List_Instances == NULL) {
printf("Could not open file!");
exit(1);
}
cannot_read_file=false;
Not_EOF=Get_File(List_Instances, f_row);
while(Not_EOF==true) {
clock_t start, end;
start = clock();
read_file();
Control_Panel();
//ILS Begins for selected instance
///////////////////////////////////////////////////////////////////////////
if(cannot_read_file==false){
greedy_solution();
local_search();
///Iteration step to ILS
while (acceptance_criteria()==false) {
disturbance();
local_search();
if(cannot_read_file==true)break;
}
//////////////////////////////////////////////////////////////////////////////
//End Of ILS for Current Instance
end = clock();
criteria->cpu_time_used=((double) (end - start)) / CLOCKS_PER_SEC;
press_results();
delete_structs();
Not_EOF=Get_File(List_Instances, f_row);
}cannot_read_file=false;
}
end_global = clock();
global_cpu_time_used=((double) (end_global - start_global)) / CLOCKS_PER_SEC;
printf("cpu_time_used (s): %f\n", global_cpu_time_used);
return 0;
}
//(1st block)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Control_Panel(void){
///best configuraration to mini instances
if (Panel_Configuration==1){
criteria->acceptance_zone=0.99; //max=.99999
criteria->acceptance_zone_relaxed=0.95; //max=.99999
criteria->acceptance_zone_relaxed2=0.9; //max=.99999
criteria->reach_zone=1; //if==2;jumps straight to zone_relaxed
criteria->calibration_buffer=0.3; ///min = 0 less disturbance||max = 1 more disturbance
criteria->Lottery_Bounds=0.45; //max=.499999
criteria->global_Max_Iter=.5*(1000000); //.5M
criteria->global_Max_Iter2=.75*(1000000); //.75M
criteria->Quit_operarions=1*(1000000); //1M
}
///best configuraration to small instances
if (Panel_Configuration==2){
criteria->acceptance_zone=0.99; //max=.99999
criteria->acceptance_zone_relaxed=0.95; //max=.99999
criteria->acceptance_zone_relaxed2=0.9; //max=.99999
criteria->reach_zone=1; //if==2;jumps straight to zone_relaxed
criteria->calibration_buffer=0.8; ///min = 0 less disturbance||max = 1 more disturbance
criteria->Lottery_Bounds=0.4; //max=.499999
criteria->global_Max_Iter=.5*(1000000); //0.5M
criteria->global_Max_Iter2=.75*(1000000); //0.75M
criteria->Quit_operarions=1*(1000000); //1M
}
///best configuraration to medium instances
if (Panel_Configuration==3){
criteria->acceptance_zone=0.99; //max=.99999
criteria->acceptance_zone_relaxed=0.95; //max=.99999
criteria->acceptance_zone_relaxed2=0.9; //max=.99999
criteria->reach_zone=1; //if==2;jumps straight to zone_relaxed
criteria->calibration_buffer=0.8; ///min = 0 less disturbance||max = 1 more disturbance
criteria->Lottery_Bounds=0.3; //max=.499999
criteria->global_Max_Iter=0.1*(1000000); //1
criteria->global_Max_Iter2=0.2*(1000000); //1.5M
criteria->Quit_operarions=0.3*(1000000); //1M
}
///best configuraration to large instances
if (Panel_Configuration==4){
criteria->acceptance_zone=0.97; //max=.99999
criteria->acceptance_zone_relaxed=0.9; //max=.99999
criteria->acceptance_zone_relaxed2=0.8; //max=.99999
criteria->reach_zone=1; //if==2;jumps straight to zone_relaxed
criteria->calibration_buffer=0.8; ///min = 0 less disturbance||max = 1 more disturbance
criteria->Lottery_Bounds=0.3; //max=.499999
criteria->global_Max_Iter=0.3*(10000); //100k
criteria->global_Max_Iter2=0.5*(10000); //200k
criteria->Quit_operarions=0.7*(10000); //300k
}
///best configuraration to toy instances
if (Panel_Configuration==5){
criteria->acceptance_zone=0.99; //max=.99999
criteria->acceptance_zone_relaxed=0.95; //max=.99999
criteria->acceptance_zone_relaxed2=0.9; //max=.99999
criteria->reach_zone=1; //if==2;jumps straight to zone_relaxed
criteria->calibration_buffer=0.8; ///min = 0 less disturbance||max = 1 more disturbance
criteria->Lottery_Bounds=0.4; //max=.499999
criteria->global_Max_Iter=1*(1000000); //1M
criteria->global_Max_Iter2=1.5*(1000000); //1.5M
criteria->Quit_operarions=3*(1000000); //3M
}
}
void createCriteriaStruct(void){
criteria->iterations_Optimal_zone=0;
criteria->global_iterations=1;
Panel_Configuration=0;
}
void createSetStruct(void){
set->subsetsCost= (int *) malloc(sizeof(int) * set->ncols);
set->nrows_covered = (int *) malloc(sizeof(int) * set->ncols);
set->subsets = (int**) malloc(sizeof(int*) * set->ncols);
set->index_frequency= (int *) malloc(sizeof(int) * set->nrows+1);
set->survived = (int *) malloc(sizeof(int) * set->ncols);
set->rank_frequency=(int *) malloc(sizeof(int) * set->nrows+1);
miniSet->subsetsCost= (int *) malloc(sizeof(int) * set->ncols);
miniSet->nrows_covered = (int *) malloc(sizeof(int) * set->ncols);
miniSet->subsets = (int **) malloc(sizeof(int *) * set->ncols);
miniSet->index_frequency= (int *) malloc(sizeof(int) * set->nrows+1);
miniSet->survived = (int *) malloc(sizeof(int) * set->ncols);
miniSet->rank_frequency=(int *) malloc(sizeof(int) * set->nrows+1);
nanoSet->subsetsCost= (int *) malloc(sizeof(int) * set->ncols);
nanoSet->nrows_covered = (int *) malloc(sizeof(int) * set->ncols);
nanoSet->subsets = (int **) malloc(sizeof(int *) * set->ncols);
nanoSet->index_frequency= (int *) malloc(sizeof(int) * set->nrows+1);
nanoSet->survived = (int *) malloc(sizeof(int) * set->ncols);
nanoSet->rank_frequency=(int *) malloc(sizeof(int) * set->nrows+1);
int index;
for(index=0; index<=set->nrows; index++){
set->index_frequency[index]=0;
miniSet->index_frequency[index]=0;
nanoSet->index_frequency[index]=0;
set->rank_frequency[index]=0;
miniSet->rank_frequency[index]=0;
nanoSet->rank_frequency[index]=0;
}
set->index_frequency[0]=1000000;
miniSet->index_frequency[0]=1000000;
nanoSet->index_frequency[0]=1000000;
set->rank_frequency[0]=0;
miniSet->rank_frequency[0]=0;
nanoSet->rank_frequency[0]=0;
}
void createSolutionStruct() {
int i,j;
solution = (Solution *) malloc(sizeof(Solution));
miniSolution = (Solution *) malloc(sizeof(Solution));
bestSolution = (Solution *) malloc(sizeof(Solution));
OptimalSolution = (Solution *) malloc(sizeof(Solution));
solution->subSets = (int*) malloc(sizeof(int) * (set->ncols));
miniSolution->subSets = (int*) malloc(sizeof(int) * (set->ncols));
bestSolution->subSets = (int*) malloc(sizeof(int) * (set->ncols));
OptimalSolution->subSets = (int*) malloc(sizeof(int) * (set->ncols));
solution->list_elements_covered = (int*) malloc(sizeof(int) * (set->nrows+1));
miniSolution->list_elements_covered = (int*) malloc(sizeof(int) * (set->nrows+1));
bestSolution->list_elements_covered = (int*) malloc(sizeof(int) * (set->nrows+1));
OptimalSolution->list_elements_covered = (int*) malloc(sizeof(int) * (set->nrows+1));
for(i=0; i<(set->ncols); i++){
solution->subSets[i] = 0;
miniSolution->subSets[i] = 0;
bestSolution->subSets[i] = 0;
OptimalSolution->subSets[i] = 0;
miniSolution->SolutionSize += set->subsets[i][0];
bestSolution->SolutionSize += set->subsets[i][0];
OptimalSolution->SolutionSize+= set->subsets[i][0];
}
solution->SolutionSize=0; //here is zero!
solution->SolutionCost=0;
miniSolution->SolutionCost = 0;
bestSolution->SolutionCost = 0;
OptimalSolution->SolutionCost = 0;
OptimalSolution->SolutionCost_prev = 0;
for(j=0; j<=(set->nrows); j++){
solution->list_elements_covered[j] = 0;
miniSolution->list_elements_covered[j] = 0;
}
solution->list_elements_covered[0]=1;
miniSolution->list_elements_covered[0]=1;
}
//(2nd block)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void copySetToMini(void){
int i,k;
miniSet->nrows = set->nrows;
miniSet->ncols = set->ncols;
miniSet->nsurvived = set->nsurvived;
for(i=0; i<miniSet->ncols; i++){
miniSet->survived[i]= set->survived[i];
miniSet->subsetsCost[i]=set->subsetsCost[i];
miniSet->nrows_covered[i]= set->nrows_covered[i];
}
for(k=1;k<=miniSet->nrows;k++){
miniSet->index_frequency[k]=set->index_frequency[k];
miniSet->rank_frequency[k]=set->rank_frequency[k];
}
}
void copySetToNano(void){
int i,k;
nanoSet->nrows = set->nrows;
nanoSet->ncols = set->ncols;
nanoSet->nsurvived = set->nsurvived;
for(i=0; i<nanoSet->ncols; i++){
nanoSet->survived[i]= set->survived[i];
nanoSet->subsetsCost[i]=set->subsetsCost[i];
nanoSet->nrows_covered[i]= set->nrows_covered[i];
}
for(k=1;k<=nanoSet->nrows;k++){
nanoSet->index_frequency[k]=set->index_frequency[k];
nanoSet->rank_frequency[k]=set->rank_frequency[k];
}}
void copyMiniToNano(void){
int i,j,k;
nanoSet->nrows = miniSet->nrows;
nanoSet->ncols = miniSet->ncols;
nanoSet->nsurvived = miniSet->nsurvived;
for(i=0; i<nanoSet->ncols; i++){
nanoSet->survived[i]= miniSet->survived[i];
nanoSet->subsetsCost[i]=miniSet->subsetsCost[i];
nanoSet->nrows_covered[i]= miniSet->nrows_covered[i];
for(j=0; j<(nanoSet->nrows_covered[i]+2); j++){
nanoSet->subsets[i][j] = miniSet->subsets[i][j];
}}
for(k=1;k<=nanoSet->nrows;k++){
nanoSet->index_frequency[k]=miniSet->index_frequency[k];
nanoSet->rank_frequency[k]=miniSet->rank_frequency[k];
}}
void copySolutionTominiSolution() {
int i,k,j=0;
miniSolution->SolutionCost = solution->SolutionCost;
miniSolution->SolutionSize = solution->SolutionSize;
//free(miniSolution->SolutionIndex);
miniSolution->SolutionIndex =(int*) malloc(sizeof(int) * (miniSolution->SolutionSize));
for(i=0;i<set->ncols;i++){
miniSolution->subSets[i] = solution->subSets[i];
if(miniSolution->subSets[i]==1){
miniSolution->SolutionIndex[j]=i;
j++;
}
}
//printf("\nsolution to miniSolution:");
for(k=0;k<=set->nrows;k++)
miniSolution->list_elements_covered[k]=solution->list_elements_covered[k];
//printf("-%d", miniSolution->list_elements_covered[k]);}
}
void copyminiSolution_To_Solution(void){
int i,k,j=0;
solution->SolutionCost = miniSolution->SolutionCost;
solution->SolutionSize = miniSolution->SolutionSize;
//free(solution->SolutionIndex);
solution->SolutionIndex =(int*) malloc(sizeof(int) * (solution->SolutionSize));
for(i=0;i<set->ncols;i++){
solution->subSets[i] = miniSolution->subSets[i];
if(solution->subSets[i]==1){
solution->SolutionIndex[j]=i;
j++;
}
}//printf("\nmini to solution:");
for(k=0;k<=set->nrows;k++)
solution->list_elements_covered[k]=miniSolution->list_elements_covered[k];
// printf("-%d-", solution->list_elements_covered[k]);}
}
void copyBestSolution_To_Solution(void){
int i,k,j=0;
solution->SolutionCost = bestSolution->SolutionCost;
solution->SolutionSize = bestSolution->SolutionSize;
//free(miniSolution->SolutionIndex);
solution->SolutionIndex =(int*) malloc(sizeof(int) * (solution->SolutionSize));
for(i=0;i<set->ncols;i++){
solution->subSets[i] = bestSolution->subSets[i];
if(solution->subSets[i]==1){
solution->SolutionIndex[j]=i;
j++;
}
}//printf("\nmini to solution:");
for(k=0;k<=set->nrows;k++)
solution->list_elements_covered[k]=bestSolution->list_elements_covered[k];
// printf("-%d-", solution->list_elements_covered[k]);}
}
void copyOptimalSolution_To_Solution(void){
int i,k,j=0;
solution->SolutionCost = OptimalSolution->SolutionCost;
solution->SolutionSize = OptimalSolution->SolutionSize;
//free(solution->SolutionIndex);
solution->SolutionIndex =(int*) malloc(sizeof(int) * (solution->SolutionSize));
for(i=0;i<set->ncols;i++){
solution->subSets[i] = OptimalSolution->subSets[i];
if(solution->subSets[i]==1){
solution->SolutionIndex[j]=i;
j++;
}
}//printf("\nmini to solution:");
for(k=0;k<=set->nrows;k++)
solution->list_elements_covered[k]=OptimalSolution->list_elements_covered[k];
// printf("-%d-", solution->list_elements_covered[k]);}
}
void copySolutionToBest() {
int i,j=0;
criteria->best_solution_improvement++;
bestSolution->SolutionCost = solution->SolutionCost;
bestSolution->SolutionSize = solution->SolutionSize;
//printf("\ncost: %d:\n", bestSolution->SolutionCost);
//free(bestSolution->SolutionIndex);
bestSolution->SolutionIndex =(int*) malloc(sizeof(int) * (bestSolution->SolutionSize));
for(i=0;i<set->ncols;i++){
bestSolution->subSets[i] = solution->subSets[i];
if(bestSolution->subSets[i]==1){
bestSolution->SolutionIndex[j]=i;
j++;
}
}
}
void copyBestSolutionToOptimal() {
int i,j=0;
reset_count=0;
criteria->Optimal_solution_improvement++;
OptimalSolution->SolutionCost = bestSolution->SolutionCost;
criteria->Optimal_percentage=(criteria->GlobalOptimal_value/OptimalSolution->SolutionCost);
if( OptimalSolution->SolutionCost>0){
printf("Cost: %d ",OptimalSolution->SolutionCost);
printf("- %.2f%% ", (criteria->Optimal_percentage*100));
printf("Iterations: %d\n", criteria->global_iterations);
press_results();
}
OptimalSolution->optimal_iteration=criteria->global_iterations;
OptimalSolution->SolutionSize = bestSolution->SolutionSize;
OptimalSolution->SolutionIndex =(int *) malloc(sizeof(int) * (OptimalSolution->SolutionSize));
for(i=0;i<set->ncols;i++){
OptimalSolution->subSets[i] = bestSolution->subSets[i];
if(OptimalSolution->subSets[i]==1){
OptimalSolution->SolutionIndex[j]=i;
j++;
}
}
}
void copyBestTosolution(void){
int i,j=0;
solution->SolutionCost = bestSolution->SolutionCost;
solution->SolutionSize = bestSolution->SolutionSize;
//free(solution->SolutionIndex);
solution->SolutionIndex =(int *) malloc(sizeof(int) * (solution->SolutionSize));
for(i=0;i<set->ncols;i++){
solution->subSets[i] = bestSolution->subSets[i];
if(solution->subSets[i]==1){
solution->SolutionIndex[j]=i;
j++;
}
}
}
//(3rd block)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool Get_File(FILE *ff, char f_row[]) {
char *p;
bool not_eof = false;
char *elem_file;
char instance_name[MAX]="SPP";
strcpy(file_name_write,file_name_read);
strcat(file_name_write, "Results:_");
while (fgets(f_row, 1024, ff) != NULL) {
p=strchr(f_row, (int) '\n');
elem_file = strtok(f_row, " ");
strcat(instance_name, elem_file);
strcat(instance_name, ".txt");
strcpy(instance_list, instance_name);
elem_file = strtok(NULL, " ");
if(elem_file==NULL){
printf("End of Instances List");
exit(1);
}
criteria->GlobalOptimal_value=atoi(elem_file);
if ( p != NULL )
*p = '\0';
if (*f_row != '\0') {
not_eof = true;
break;
}
}
return (not_eof);
}
bool Get_Line(FILE *f, char f_row[]) {
/* Read a line of possibly commented input from the file *f.*/
char *p;
bool not_eof = false;
while ( fgets(f_row, 1024, f) != NULL) {
p=strchr(f_row, (int) '\n');
if ( p != NULL )
*p = '\0';
if (*f_row != '\0') {
not_eof = true;
break;
}
}
return (not_eof);
}
void read_file(void) {
FILE *Current_Instance;
int j=0, lin_file=0, i=0, index;
int subSetSize=0;
int nCurrSubSet=0;
char * element_file;
char f_row[1024];
char file_row[1024];
char read[MAX];
char file[MAX];
set = (Set *) malloc(sizeof(Set));
miniSet = (Set *) malloc(sizeof(Set));
nanoSet = (Set *) malloc(sizeof(Set));
strcpy(read,file_name_read);
strcpy(file,instance_list);
strcat(read, file);
Current_Instance = fopen(read, "r");
printf("File:%s\n", instance_list);
printf("Global Optimal Cost: %f\n", criteria->GlobalOptimal_value);
if(Current_Instance == NULL) {
printf("Could not open file - check line 20!");
cannot_read_file=true;
}
if(cannot_read_file==false){
while(Get_Line(Current_Instance, file_row)) {
if(lin_file==0) { //read first line of file
element_file = strtok(file_row, " ");
while (element_file != NULL) {
if(j==0){
set->nrows = atoi(element_file); //numero de linhas ok
}
if(j==1){
set->ncols=atoi(element_file); //numero de colunas ok
}
j++;
element_file = strtok(NULL, " "); // quebra a string ate o espaco e transforma o anterior em NULL;
}
printf("Instance Size: Col:%d x Rows:%d\n",set->ncols, set->nrows);
if(set->ncols>100&&set->ncols<=700)Panel_Configuration=1;
if(set->ncols>700&&set->ncols<=3200)Panel_Configuration=2;
if(set->ncols>3200&&set->ncols<=20000)Panel_Configuration=3;
if(set->ncols>20000)Panel_Configuration=4;
if(set->ncols<=100)Panel_Configuration=5;
printf("Panel Configuration: %d\n", Panel_Configuration);
createSetStruct();
}
else{
strcpy(f_row, strchr(file_row, (int) ' '));
element_file = strtok(f_row, " ");
subSetSize=atoi(element_file);
element_file = strtok(file_row, " ");
set->subsets[nCurrSubSet] = (int *) malloc(sizeof(int) * (subSetSize+2));
//miniSet->subsets[nCurrSubSet] = (int *) malloc(sizeof(int) * (subSetSize+2));
//nanoSet->subsets[nCurrSubSet] = (int *) malloc(sizeof(int) * (subSetSize+2));
//set->nrows_covered[nCurrSubSet] = subSetSize;
set->subsetsCost[nCurrSubSet] = atoi(file_row);
set->survived[nCurrSubSet] = 1;
while (element_file != NULL) {
set->subsets[nCurrSubSet][i] = atoi(element_file);
index=atoi(element_file);
if(i==1){
set->nrows_covered[nCurrSubSet] = set->subsets[nCurrSubSet][i];
}
if(i>1){
set->index_frequency[index]++;
miniSet->index_frequency[index]++;
nanoSet->index_frequency[index]++;
}
element_file = strtok(NULL, " ");
i++;
}
nCurrSubSet++;
i=0;
}
lin_file++;
}fclose(Current_Instance);
}
for(i=0;i<set->ncols; i++){
}
}
//(4th block)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
int sort_index_frequency(Set *nanoSet, int try_index){
int ind=0;
int j=0;
int k,i;
// which rows are already covered
for(k=0;k<=nanoSet->nrows;k++){
nanoSet->rank_frequency[k]=0;
// printf("[%d_%d] ",k, solution->list_elements_covered[k]);
if(solution->list_elements_covered[k]==1){
nanoSet->index_frequency[k]=1000000+k; //if the element is already covered;
//printf("(%d_%d) ",k, nanoSet->index_frequency[k]);
}}
// do the ranking
for(i=1; i<=nanoSet->nrows; i++){
for(j=1; j<=nanoSet->nrows; j++){
if(nanoSet->index_frequency[i] > nanoSet->index_frequency[j]){
nanoSet->rank_frequency[i]=nanoSet->rank_frequency[i]+1;
}}
nanoSet->rank_frequency[i]++;
}
//avoiding draws
for(i=1; i<=nanoSet->nrows; i++){
for(j=2; j<=nanoSet->nrows; j++){
if( nanoSet->rank_frequency[i] == nanoSet->rank_frequency[j] && i!=j){
nanoSet->rank_frequency[j]++;;
}}}
for(i=1; i<=nanoSet->nrows; i++){
if(nanoSet->rank_frequency[i]==try_index){
ind = i;
}
}
/////////////////////
//check - error
if(try_index>(set->nrows/4)){
if(cannot_read_file==false){
ind=reset_lottery();
}
//check
/*
printf("\n**Error: Lottery Bounds too low for this instance**\n\n");
printf("\n******\n try %d`th index: [%d] ",try_index, ind);
printf("\nRANK INDEX:\n");
for(j=1; j<=nanoSet->nrows; j++){
printf("%d] ", nanoSet->index_frequency[j]);
printf(" [%d] ", solution->list_elements_covered[j]);
printf(" || [%d] ", nanoSet->rank_frequency[j]);
printf("\n");
}*/
}
return ind;
}
//This sorts the subsets by cost
void sortSubsets_cost(Set *set) {
int i,j;
int size_temp;
int cost_temp;
int *vetor_temp=NULL;
for(i=set->ncols-1; i>=1;i--){
for(j=0; j<i; j++){
if(set->subsets[j][0] > set->subsets[j+1][0]) {
//sortsubsets by cost
vetor_temp = set->subsets[j];
set->subsets[j]= set->subsets[j+1];
set->subsets[j+1]= vetor_temp;
//adjusts size of vector
size_temp = set->nrows_covered[j];
set->nrows_covered[j]=set->nrows_covered[j+1];
set->nrows_covered[j+1]=size_temp;
//adjusts subset cost
cost_temp = set->subsetsCost[j];
set->subsetsCost[j]=set->subsetsCost[j+1];
set->subsetsCost[j+1]=cost_temp;
}}}
//check
/*
int h, l;
printf("\n\nSorted:\n");
for(h=0; h<set->ncols; h++){
printf("(%d)", set->subsets[h][0]);
printf("[%d] ", set->subsets[h][1]);
for(l=1; l<=set->nrows_covered[h]; l++){
printf("%d ", set->subsets[h][l+1]);
}
printf("\n");
}
*/
}
//(5th block)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void add_subset(Set* nanoSet, int subSetIndex){
int j, index;
solution->SolutionCost += nanoSet->subsetsCost[subSetIndex];
solution->SolutionSize++;
solution->subSets[subSetIndex]=1;
for(j=0; j<set->subsets[subSetIndex][1]; j++){
//printf("{%d} ", set->subsets[subSetIndex][j+2]);
index=set->subsets[subSetIndex][j+2];
solution->list_elements_covered[index]=1;
}
}
void remove_subset(Set *miniSet, int subSetIndex){
int j;
solution->SolutionCost -= miniSet->subsetsCost[subSetIndex];
solution->SolutionSize--;
solution->subSets[subSetIndex]=0;
for(j=0; j<set->nrows_covered[subSetIndex]; j++){
solution->list_elements_covered[miniSet->subsets[subSetIndex][j+2]]=0;
}
}
bool check_subset(int subSetIndex){
int j;
for(j=0; j<set->subsets[subSetIndex][1]; j++){
//printf("%d|", set->subsets[subSetIndex][j+2]);
// int k; for(k=0;k<=set->nrows;k++){printf("{{%d}}", solution->list_elements_covered[k]);} //check covered elments
if(solution->list_elements_covered[set->subsets[subSetIndex][j+2]]==1){
return false; //it's not a viable solution
}
}
return true;
}
int subset_frequency(Set* nanoset, int index){
int candidate,j,frequency=0;
for(candidate=0; candidate<nanoSet->ncols; candidate++){
if(nanoSet->survived[candidate]==1){
for(j=0; j<set->subsets[index][1]; j++){
if(nanoSet->subsets[candidate][j+2]==index){
frequency++;
}}}}
return frequency;
}
int find_subset_index(Set* nanoSet, int index, int try){
int candidate,j,temp=0;
try_again:
for(candidate=0; candidate<nanoSet->ncols; candidate++){
if(nanoSet->survived[candidate]==1){
for(j=0; j<set->subsets[candidate][1]; j++){
if(nanoSet->subsets[candidate][j+2]==index){
temp++;
if(temp==try)
return candidate;
}}}}
if(candidate==set->ncols){
reset_lottery_bool=true;
}
return candidate-1;
}
void delete_set(void){
int i,j,k;
set->nrows=0;
set->ncols=0;
set->nsurvived=0;
for(i=0; i<set->ncols; i++){
set->survived[i]=0;
set->subsetsCost[i]=0;
set->nrows_covered[i]=0;
for(j=0; j<(set->nrows_covered[i]+2); j++){
set->subsets[i][j]=0;