-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhegel.h
1479 lines (1354 loc) · 29.3 KB
/
hegel.h
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
#ifndef HEGEL_H
#define HEGEL_H
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <general_includes.h>
#include <PERMUTATION/binom.h>
#include <HEAP/heap.h>
#include <SET/set.h>
using namespace std;
const int numberOfHeuristics=23;
const int rankCard=5;
#define INF 9999999
float sqr(float a){return a*a;}
int chooseDist(float* dis, int n)
{
if (n<=0) return -1;
long long* D = new long long[n];
float acc=0.;
for (int i=0; i<n; i++)
{
acc+=dis[i];
D[i]=(int) minimum(acc*1000000.,1000000.);
}
D[n-1]=1000000;
long long r=rand()%1000000;
int x;
for (x=0;x<n; x++)
if (r<D[x]) break;
assert(x<n);
delete [] D;
return x;
}
class Hegel
{
//instance data
int n,m; //numbers sets and items
float* c; //cost vector
int** set; //given sets
int* setCard; //cardinality of each set
int** appears; //for each item a list of sets the item appears in
int* appearCard; //in how many sets does each item appear
float maxCost;
float minCost;
//solution data
Set** covered; // for each item, a list of all sets currently covering it
int uncovered; // how many items are currently uncovered
float cost; // cost of all selected sets
Set selection; // which sets are currently selected
Set complement; // which sets are currently not selected
int* newCover; // for each set, how many new items would it cover if added
int* disCover; // for each set, how many items rely on it for being covered
Set best;
float bestVal;
float EverBest;
void alloc(void)
{
covered=new Set*[m];
disCover=new int[n];
newCover=new int[n];
appears=new int*[m];
appearCard=new int[m];
for (int i=0; i<m; i++)
{
appears[i]=new int[n];
covered[i]=new Set(n);
}
}
void print()
{
cout<<"Cost: "<<cost<<endl;
cout<<"Uncovered: "<<uncovered<<endl;
cout<<"Best(val: "<<bestVal<<" size: "<<best.card<<")"<<flush;
for(int i=0;i<best.card;i++)
cout<<best.list[i]<<" ";
cout<<endl;
cout<<"Selected("<<selection.card<<") "<<flush;
for(int i=0;i<selection.card;i++)
cout<<selection.list[i]<<" ";
cout<<endl;
cout<<"Complement("<<complement.card<<") "<<flush;
for(int i=0;i<complement.card;i++)
cout<<complement.list[i]<<" ";
cout<<endl;
cout<<"Item covered by: "<<flush;
for(int i=0;i<m;i++)
{
cout<<"Item"<<i<<"("<<flush;
for(int j=0;j<covered[i]->card;j++)
{
cout<<covered[i]->list[j]<<" ";
}
cout<<")"<<endl;
}
cout<<"NewCover: "<<flush;
for(int i=0;i<n;i++)
cout<<newCover[i]<<" ";
cout<<endl;
cout<<"DisCover: "<<flush;
for(int i=0;i<n;i++)
cout<<disCover[i]<<" ";
cout<<endl;
cout<<endl;
}
void free(void)
{
delete [] disCover;
delete [] newCover;
delete [] appearCard;
for (int i=0; i<m; i++)
{
delete [] appears[i];
delete covered[i];
}
delete [] covered;
delete [] appears;
}
void reset(void)
{
uncovered=m;
cost=0.;
for (int i=0; i<m; i++)
covered[i]->remAll();
for (int j=0; j<n; j++)
{
newCover[j]=setCard[j];
disCover[j]=0;
}
for (int l=0; l<selection.card; l++)
{
bool r=complement.add(selection.list[l]);
assert(r);
}
selection.remAll();
}
void init(void)
{
for (int i=0; i<m; i++)
appearCard[i]=0;
maxCost=-1;
minCost=INF;
for (int j=0; j<n; j++)
{
bool r=complement.add(j);
assert(r);
assert(c[j]>0);
maxCost=maximum(maxCost,c[j]);
minCost=minimum(minCost,c[j]);
for (int k=0; k<setCard[j]; k++)
{
int i=set[j][k];
appears[i][appearCard[i]++]=j;
}
}
reset();
}
int h0(void)
//uniform random
{
int numberOfCandidates=0;
int* candidates=new int[n];
int b=-1;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
candidates[numberOfCandidates++]=j;
}
if (numberOfCandidates==0)
{
delete [] candidates;
return -1;
}
b=candidates[rand()%numberOfCandidates];
delete [] candidates;
return b;
}
int h1(void)
//min cost
{
float minScore=INF;
int b=-1;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
float s=c[j];
if (minScore>s)
{
minScore=s;
b=j;
}
}
return b;
}
int h8(void)
//min cost random
{
int cand=0;
float* dis=new float[n];
int* col=new int[n];
float sum=0;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
dis[cand]=maxCost+1-c[j];
col[cand]=j;
sum+=dis[cand++];
}
for (int k=0; k<cand; k++)
dis[k]/=sum;
int b= chooseDist(dis,cand);
if (b>=0) b=col[b];
delete [] dis;
delete [] col;
return b;
}
int h16(void)
//min cost rank
{
HHeap<int,float> heap(n);
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
heap.ins(j,c[j]);
}
if (heap.getSize()==0) return -1;
int mi=minimum((long)rankCard,heap.getSize());
int r=rand()%mi;
for (int k=1; k<r; k++)
heap.del();
return heap.min();
}
int h9(void)
//min cost random
{
int cand=0;
float* dis=new float[n];
int* col=new int[n];
float sum=0;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
dis[cand]=1/c[j];
col[cand]=j;
sum+=dis[cand++];
}
for (int k=0; k<cand; k++)
dis[k]/=sum;
int b= chooseDist(dis,cand);
if (b>=0) b=col[b];
delete [] dis;
delete [] col;
return b;
}
int h2(void)
//max new cover
{
float maxScore=-1;
int b=-1;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
float s=newCover[j];
if (maxScore<s)
{
maxScore=s;
b=j;
}
}
return b;
}
int h10(void)
//max new cover random
{
int cand=0;
float* dis=new float[n];
int* col=new int[n];
float sum=0;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
dis[cand]=newCover[j];
col[cand]=j;
sum+=dis[cand++];
}
for (int k=0; k<cand; k++)
dis[k]/=sum;
int b= chooseDist(dis,cand);
if (b>=0) b=col[b];
delete [] dis;
delete [] col;
return b;
}
int h17(void)
//max new cover rank
{
HHeap<int,float> heap(n);
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
heap.ins(j,-newCover[j]);
}
if (heap.getSize()==0) return -1;
int mi=minimum((long)rankCard,heap.getSize());
int r=rand()%mi;
for (int k=1; k<r; k++)
heap.del();
return heap.min();
}
int h11(void)
//min cost over cover random
{
int cand=0;
float* dis=new float[n];
int* col=new int[n];
float sum=0;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
dis[cand]=newCover[j]/c[j];
col[cand]=j;
sum+=dis[cand++];
}
for (int k=0; k<cand; k++)
dis[k]/=sum;
int b= chooseDist(dis,cand);
if (b>=0) b=col[b];
delete [] dis;
delete [] col;
return b;
}
int h18(void)
//min cost over cover rank
{
HHeap<int,float> heap(n);
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
heap.ins(j,c[j]/newCover[j]);
}
if (heap.getSize()==0) return -1;
int mi=minimum((long)rankCard,heap.getSize());
int r=rand()%mi;
for (int k=1; k<r; k++)
heap.del();
return heap.min();
}
int h4(void)
//c/log new cover
{
float minScore=INF;
int b=-1;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
float s=c[j]/log(newCover[j]+1);
if (minScore>s)
{
minScore=s;
b=j;
}
}
return b;
}
int h12(void)
//c/log random
{
int cand=0;
float* dis=new float[n];
int* col=new int[n];
float sum=0;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
dis[cand]=log(newCover[j])/c[j];
col[cand]=j;
sum+=dis[cand++];
}
for (int k=0; k<cand; k++)
dis[k]/=sum;
int b= chooseDist(dis,cand);
if (b>=0) b=col[b];
delete [] dis;
delete [] col;
return b;
}
int h19(void)
//min cost over log rank
{
HHeap<int,float> heap(n);
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
heap.ins(j,c[j]/log(newCover[j]));
}
if (heap.getSize()==0) return -1;
int mi=minimum((long)rankCard,heap.getSize());
int r=rand()%mi;
for (int k=1; k<r; k++)
heap.del();
return heap.min();
}
int h5(void)
//c/new cover * log new cover
{
float minScore=INF;
int b=-1;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
float s=c[j]/(newCover[j]*log(newCover[j]+1));
if (minScore>s)
{
minScore=s;
b=j;
}
}
return b;
}
int h13(void)
//c/k log k random
{
int cand=0;
float* dis=new float[n];
int* col=new int[n];
float sum=0;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
dis[cand]=newCover[j]*log(newCover[j])/c[j];
col[cand]=j;
sum+=dis[cand++];
}
for (int k=0; k<cand; k++)
dis[k]/=sum;
int b= chooseDist(dis,cand);
if (b>=0) b=col[b];
delete [] dis;
delete [] col;
return b;
}
int h20(void)
//min cost over k log k rank
{
HHeap<int,float> heap(n);
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
heap.ins(j,c[j]/(newCover[j]*log(newCover[j])));
}
if (heap.getSize()==0) return -1;
int mi=minimum((long)rankCard,heap.getSize());
int r=rand()%mi;
for (int k=1; k<r; k++)
heap.del();
return heap.min();
}
int h6(void)
//c/new cover^2
{
float minScore=INF;
int b=-1;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
float s=c[j]/(newCover[j]*newCover[j]);
if (minScore>s)
{
minScore=s;
b=j;
}
}
return b;
}
int h14(void)
//c/k2 random
{
int cand=0;
float* dis=new float[n];
int* col=new int[n];
float sum=0;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
dis[cand]=sqr(newCover[j])/c[j];
col[cand]=j;
sum+=dis[cand++];
}
for (int k=0; k<cand; k++)
dis[k]/=sum;
int b= chooseDist(dis,cand);
if (b>=0) b=col[b];
delete [] dis;
delete [] col;
return b;
}
int h21(void)
//min cost over k2 rank
{
HHeap<int,float> heap(n);
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
heap.ins(j,c[j]/sqr(newCover[j]));
}
if (heap.getSize()==0) return -1;
int mi=minimum((long)rankCard,heap.getSize());
int r=rand()%mi;
for (int k=1; k<r; k++)
heap.del();
return heap.min();
}
int h7(void)
//sqrt(c)/new cover^2
{
float minScore=INF;
int b=-1;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
float s=sqrt(c[j])/(newCover[j]*newCover[j]);
if (minScore>s)
{
minScore=s;
b=j;
}
}
return b;
}
int h15(void)
//sqrt c/k2 random
{
int cand=0;
float* dis=new float[n];
int* col=new int[n];
float sum=0;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
dis[cand]=sqr(newCover[j])/sqrt(c[j]);
col[cand]=j;
sum+=dis[cand++];
}
for (int k=0; k<cand; k++)
dis[k]/=sum;
int b= chooseDist(dis,cand);
if (b>=0) b=col[b];
delete [] dis;
delete [] col;
return b;
}
int h22(void)
//min sqrt cost over k2 rank
{
HHeap<int,float> heap(n);
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
heap.ins(j,sqrt(c[j])/sqr(newCover[j]));
}
if (heap.getSize()==0) return -1;
int mi=minimum((long)rankCard,heap.getSize());
int r=rand()%mi;
for (int k=1; k<r; k++)
heap.del();
return heap.min();
}
bool moveH3(Set& pos, Set& neg)
{
bool res=false;
int b=h3(pos,neg);
while (b!=-1)
{
// cout << "suggested " << b << endl;
// cout << "pos -> " << pos << endl;
// cout << "neg -> " << neg << endl << flush;
if (cost<=bestVal+0.00001 && uncovered==0)
{
// if (cost<bestVal) cout << cost << " ";
// cout << cost << "8888888888888888";
res=cost<bestVal;
best=selection;
bestVal=cost;
}
if (pos.rem(b))
{
assert(!selection.isInSet(b));
commit(b);
//cout << "+" << flush;
b=h3(pos,neg);
continue;
}
if ((pos.card==0)&&(uncovered>0))
{
for (int l=0; l<neg.card; l++)
{
int b=neg.list[l];
//cout << "-" << flush;
assert(selection.isInSet(b));
unCommit(b);
}
neg.remAll();
break;
}
neg.rem(b);
assert(selection.isInSet(b));
unCommit(b);
//cout << "-" << flush;
b=h3(pos,neg);
}
//cout << endl;
return res;
}
void moveTo(Set& s)
{
for (int i=0; i<n; i++)
{
if (selection.isInSet(i)==s.isInSet(i)) continue;
if (selection.isInSet(i))
{
unCommit(i);
continue;
}
commit(i);
}
}
int h3(void)
//c/new cover
{
float minScore=INF;
int b=-1;
for (int j=0; j<n; j++)
{
if (newCover[j]<=0) continue;
float s=c[j]/newCover[j];
if (minScore>s)
{
minScore=s;
b=j;
}
}
return b;
}
int h3p(const Set& pos)
//c/new cover
{
if (pos.card==0) return -1;
float minScore;
float minCost=INF;
int b=-1;
int d=-1;
int l;
//Setin icinde
for (l=0; l<pos.card; l++)
{
int j=pos.list[l];
//selection a eklense faydasi olmuycak adam
if (newCover[j]<=0)
{
//minimum costluyu al
if (minCost>c[j])
{
d=j;
minCost=c[j];
}
continue;
}
//Faydasi olucak adam varsa
minScore=c[j]/newCover[j];
b=j;
break;
}
assert(l<pos.card||d>=0);
//eklense faydasi olmayan adami dondur
if (l==pos.card) return d;
//Faydasi olacak adam varsa
for (; l<pos.card; l++)
{
int j=pos.list[l];
if (newCover[j]<=0) continue;
float s=c[j]/newCover[j];
//daha ucuzu olabilir mi?
if (minScore>s)
{
minScore=s;
b=j;
}
}
assert(b>=0);
return b;
}
int h3n(const Set& neg)
//dis cover over c
{
if (neg.card==0) return -1;
float minScore=INF;
float maxC;
int b=-1;
int d=-1;
int l;
for (l=0; l<neg.card; l++)
{
int j=neg.list[l];
//Selectionda bu sete bagli adam varsa
if (disCover[j]>0)
{
//setin costuna bak
float s=disCover[j]/c[j];
if (minScore>s)
{
//minimum al
minScore=s;
d=j;
}
continue;
}
//set e kimse bagli degilse direk at.
b=j;
maxC=c[j];
break;
}
assert(l<neg.card||d>=0);
//hic gereksiz set yoksa minimum discover/c dondur
if (l==neg.card) return d;
for (; l<neg.card; l++)
{
int j=neg.list[l];
if (disCover[j]>0) continue;
//baska gereksiz varsa daha costlu
if (maxC<c[j])
{
maxC=c[j];
b=j;
}
}
assert(b>=0);
//onu dondur
return b;
}
int h3(const Set& pos, const Set& neg)
//c/new cover
{
if (pos.card==0)
{
if (neg.card==0) return -1;
return h3n(neg);
}
if (neg.card==0) return h3p(pos);
if (uncovered>0) return h3p(pos);
int b=h3n(neg);
if (disCover[b]==0) return b;
return h3p(pos);
}
public:
Hegel(int _n, int _m, float* _c, int** _set, int* _setCard) : n(_n), m(_m), c(_c), set(_set), setCard(_setCard), selection(_n), complement(_n), best(_n)
{
alloc();
init();
}
~Hegel(void)
{
free();
}
//Remove redundant sets from the current selection
void removeRedundant()
{
for(int i=0;i<selection.card;i++)
{
//redundant = no itmes depends on it
if(disCover[selection.list[i]]==0)
{
unCommit(selection.list[i]);
i=-1;
}
}
}
float dialectic(double &timeBest, double time_limit)
{
int i,j;
double start = now();
//compute initial cover
float* dist=new float[numberOfHeuristics];
for (i=0; i<numberOfHeuristics; i++)
dist[i]=0.;
dist[1]=0.05;
dist[3]=0.25;
dist[4]=0.15;
dist[5]=0.25;
dist[6]=0.15;
dist[7]=0.15;
//Find an initial solution according to the distribution
float r=upperBound(dist,best,50);
//Go to initial solution
moveTo(best);
//Remove redundant sets from the initial solution, if any
removeRedundant();
timeBest = now()-start;
bestVal = cost;
int noImp = 0;
int trial = 0;
int thesisVal = 0;
int synthesisVal = 0;
//Run for 10 seconds
while(now() - start < time_limit)
{
//Trial is randomized over m, number of items
trial = m/(rand()%3+2);
// trial = 100;
noImp=0;
//MOVE-1
//Until there is no improvement
while(noImp++<trial)
{
//Sets that are not in the current selection are available to add
Set available = complement;
Set thesis = selection;
thesisVal = cost;
//Choose a random subset size: 1/2 or 1/3 or 1/4 of selection
int numCandids = selection.card/(rand()%3+2);
//Remove sets from selection greedily: the set that least items rely on it for being covered
for(i=0;i<numCandids;i++)
{
//Randomize over 1st and 2nd set.
int leastCover2 = disCover[selection.list[0]];
int leastCover = disCover[selection.list[0]];
int leastCoverSet = selection.list[0];
int leastCoverSet2 = selection.list[0];
//Pick top 2 sets that least items rely on it for being covered
for (int j=1; j<selection.card; j++)
{
if(disCover[selection.list[j]] < leastCover)
{
leastCover2 = leastCover;
leastCoverSet2 = leastCoverSet;
leastCover = disCover[selection.list[j]];
leastCoverSet = selection.list[j];
}
}
//Remove the 1st or 2nd set that covers the least items
if(rand()%2==0) unCommit(leastCoverSet);
else unCommit(leastCoverSet2);
}
//Now selection is infeasible!
//Select sets from "available" greedily: pick the set that covers the most items
//Keep adding until we are feasible
while(uncovered > 0)
{
//pick the set that covers the most items
int mostCover = newCover[available.list[0]];
int mostCoverSet = available.list[0];
for (int j=1; j<available.card; j++)
{
if(newCover[available.list[j]] > mostCover)
{
mostCover = newCover[available.list[j]];
mostCoverSet = available.list[j];
}
}
//add the set that covers the most items
if(mostCover > 0)
{
commit(mostCoverSet);
available.rem(mostCoverSet);
}
else //if "available" is not enough to cover all items
{
//until reaching a cover
while(uncovered>0)
{
//pick the set that covers the most items from any unselected set
mostCover = -1;
for (int j=0; j<complement.card; j++)
{
if(newCover[complement.list[j]] > mostCover)
{
mostCoverSet = complement.list[j];
}
}
//add the set
commit(mostCoverSet);
}
}
}
//Now we have a cover!
//Remove any redundant set, if any
removeRedundant();
synthesisVal = cost;
//If <= update the best otherwise go back to thesis
if (thesisVal<synthesisVal)
{
//Go back to thesis
moveTo(thesis);
}
else
{
if(synthesisVal<bestVal)
{
bestVal = synthesisVal;
timeBest = now()-start;
best=selection;
}
if(synthesisVal<thesisVal)
{
noImp = 0;
}
}
}
//Local limit exceeded...GO TO THE ANTITHESIS!
//Sets that are not in the current selection are available to add
Set available = complement;
//Choose a random subset size: 1/2 or 1/3 or 1/4 of selection
int numCandids = selection.card/(rand()%3+2);
//Remove sets from selection greedily: the set that least items rely on it for being covered
for(i=0;i<numCandids;i++)
{
//Randomize over 1st and 2nd set.