-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.h
1430 lines (1296 loc) · 44.7 KB
/
algorithm.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 __EVOLUTION_H_
#define __EVOLUTION_H_
#include "global.h"
#include "recomb.h"
#include "common.h"
#include "individual.h"
#include "stdio.h"
#include "math.h"
class CMOEAD
{
public:
CMOEAD();
virtual ~CMOEAD();
//void init_neighbourhood(); // calculate the neighbourhood of each subproblem
void update_neighbour_table(); //calculate the neighbourhood of each subproblem
void init_population(); // initialize the population
void init_prefer_parameter();
void load_parameter();
void update_reference(CIndividual &ind); // update ideal point which is used in Tchebycheff or NBI method
void update_nadpoint(); //????????????? update nad point which is used in Tchebycheff , NBI ,PBI method and so on
void update_problem(CIndividual &ind, int &id, int &type); // update current solutions in the neighbourhood
void evol_population(); // DE-based recombination?????????像论文中的吗??????
void mate_selection(vector<int> &list, int cid, int size, int type); // select mating parents
// execute MOEAD
void exec_emo(int run);
void save_front(vector <CSubproblem> pop,char savefilename[1024]); // save the pareto front into files
void save_exter_pop(char savefilename[1024]);
void save_pos(vector <CSubproblem> pop,char savefilename[1024]);
void tour_selection(int depth, vector<int> &selected);
void comp_utility();
void external_population_update();
void organize_merge_split();
//void use_exter_pop_to_update_evol_pop();
//vector<double> calculate_crowd_degree(vector<CIndividual> nondonimate_population);
int calculate_crowd_degree(vector<CSubproblem> nondonimate_population);
int calculate_crowd_degree(vector<CIndividual> nondonimate_population);
//int form_indiv_with_min_crowd_degree();
//int form_indiv_with_min_crowd_degree2();
void delete_undesired_subproblems();
void add_spare_subproblems_to_prefer_area();
int find_sparse_indiv_from_EP();
double Calculate_HV_for_RFC();
int Calculate_TOTAL_for_RFC();
vector <CSubproblem> population;
// vector <double> utility;//individual.h class CSubproblem public:double utility;
vector <CIndividual> external_population;
vector <CIndividual> new_population;
void operator=(const CMOEAD &moea);
void save_individual(char saveFilename_x[1024],char saveFilename_y[1024],vector<double> x,vector<double> y);
bool boundy_checkout(CIndividual child);
void save_HV(vector<vector<double>> hv_value, char saveFilename[1024]);
void save_TOTAL(vector<vector<double>> total_value, char saveFilename[1024]);
void save_Z(vector <CSubproblem> pop,char saveFilename[1024]);
public:
// algorithm parameters
int max_gen; // the maximal number of generations
int pops; // the population size
int niche; // the neighborhood size
int limit; // the maximal number of solutions replaced
double prob; // the neighboring selection probability
double rate; // the differential rate between solutions
int nfes; // the number of function evluations
};
CMOEAD::CMOEAD()
{
}
CMOEAD::~CMOEAD()
{
population.clear();
//utility.clear();
}
void CMOEAD::init_population()
{
int i,j;
idealpoint = vector<double>(nobj, 1.0e+30);
nad_point = vector<double>(nobj, -1.0e+30);
//utility = vector<double>(pops, 1.0);
char filename[1024];
// Read weight vectors from a data file
sprintf(filename,"ParameterSetting/Weight/W%dD_%d.dat", nobj, pops);//?????????
std::ifstream readf(filename);
for(i=0; i<pops; i++)
{
CSubproblem sub;
// Randomize and evaluate solution
sub.indiv.rnd_init();
sub.indiv.obj_eval();
sub.saved = sub.indiv;
// Initialize the reference point
update_reference(sub.indiv);
// Load weight vectors
for(int j=0; j<nobj; j++) readf>>sub.namda[j];
//sub.fitness = fitnessfunction(sub.indiv.y_obj, sub.namda);
// Save in the population
population.push_back(sub);//vector <CSubproblem> population; //CSubproblem sub;
nfes++;//函数评价加1,计数
//new_population.push_back(sub.indiv);
}
if (!strcmp("_TCH2",strFunctionType))
update_nadpoint();
readf.close();
}
void CMOEAD::init_prefer_parameter()
{
if(!strcmp("ankang20001012", strTestInstance))
{
midpoint.push_back(328.0);
midpoint.push_back(6600.0);
threshold.push_back(4.0);
threshold.push_back(2000);
}
else if (!strcmp("ankang20030828", strTestInstance))
{
midpoint.push_back(325.0);
midpoint.push_back(3800.0);
threshold.push_back(4.0);
threshold.push_back(2000);
}
else if(!strcmp("ankang20051001", strTestInstance))
{
midpoint.push_back(325.5);
midpoint.push_back(12000.0);
threshold.push_back(4.0);
threshold.push_back(2000.0);
}
else if(!strcmp("ankang20100715", strTestInstance))
{
midpoint.push_back(326.0);
midpoint.push_back(5500.0);
threshold.push_back(4.0);
threshold.push_back(2000.0);
}
else {}
}
void CMOEAD::update_neighbour_table()
{
int i,j,k;
//1. update the neighbour individual
vector<double> dist = vector<double>(population.size(), 0);
vector<int> indxxx = vector<int>(population.size(), 0);
for (i = 0; i < population.size(); i++) population[i].table.clear();
for (i = 0; i < population.size(); i++)
{
// calculate the distances based on weight vectors
for(j=0; j<population.size(); j++)
{
dist[j] = dist_vector(population[i].namda,population[j].namda);
indxxx[j] = j;
}
// find 'niche' nearest neighboring subproblems
minfastsort(dist,indxxx,population.size(),niche);
// save the indexes of the nearest 'niche' neighboring weight vectors
for(int k=0; k<niche; k++)
population[i].table.push_back(indxxx[k]);
}
dist.clear();
indxxx.clear();
}
void CMOEAD::operator=(const CMOEAD &alg)
{
//population = alg.population;
}
void CMOEAD::tour_selection(int depth, vector<int> &selected)
{
// selection based on utility
vector<int> candidate;
//for(int k=0; k<nobj; k++) selected.push_back(k); // select first m weights
for(int n=0; n<pops; n++) candidate.push_back(n); // set of unselected weights
while(selected.size()<int(pops/5.0))
{
int best_idd = int(rnd_uni(&rnd_uni_init)*candidate.size()), i2;
int best_sub = candidate[best_idd], s2;
for(int i=1; i<depth; i++)
{
i2 = int(rnd_uni(&rnd_uni_init)*candidate.size());
s2 = candidate[i2];
if(population[s2].utility>population[best_sub].utility)
{
best_idd = i2;
best_sub = s2;
}
}
selected.push_back(best_sub);
candidate.erase(candidate.begin()+best_idd);
}
candidate.clear();
}
void CMOEAD::comp_utility()
{
double f1, f2, uti, delta;
for(int n=0; n<pops; n++)
{
f1 = fitnessfunction(population[n].indiv.y_obj, population[n].namda);
f2 = fitnessfunction(population[n].saved.y_obj, population[n].namda);
delta = f2 - f1;
if(delta>0.001) population[n].utility = 1.0;
else{
/* uti = 0.95*(1.0+delta/0.001)*utility[n];
utility[n] = uti<1.0?uti:1.0;
*/
population[n].utility = (0.95+0.05*delta/0.001)*population[n].utility;
}
population[n].saved = population[n].indiv;
}
}
void CMOEAD::update_problem(CIndividual &indiv, int &id, int &type)
{
// indiv: child solution
// id: the id of current subproblem
// type: update solutions in - neighborhood (1) or whole population (otherwise)
int i,j,k;
int size, time = 0;
vector<bool> flag=vector<bool>(population.size(), true);
//---------the subproblems in boundy have poiry------------//
for (i = 0; i < nobj; i++)
{
double f1, f2;
f1 = fitnessfunction(population[i].indiv.y_obj, population[i].namda);
f2 = fitnessfunction(indiv.y_obj, population[i].namda);
if(f2<f1)
{
population[i].indiv = indiv;
flag[i] = false;
time++;
}
if(time>=limit)
{
flag.clear();
return;
}
}
//-----------------------------------------------------------//
if(type==1) size = population[id].table.size(); // from neighborhood
else size = population.size(); // from whole population
// a random order to update
std::vector<int> perm(std::vector<int>(size, 0));
for(k=0; k<size; k++) perm[k] = k;
random_shuffle(perm.begin(), perm.end());
for(i=0; i<size; i++)
{
// Pick a subproblem to update
if(type==1) k = population[id].table[perm[i]];
else k = perm[i];
flag[k] = false;
// calculate the values of objective function regarding the current subproblem
double f1, f2;
f1 = fitnessfunction(population[k].indiv.y_obj, population[k].namda);
f2 = fitnessfunction(indiv.y_obj, population[k].namda);
if(f2<f1)
{
population[k].indiv = indiv;
time++;
}
// the maximal number of solutions updated is not allowed to exceed 'limit'
if(time>=limit)
{
flag.clear();
perm.clear();
return;
}
}
//------consider such case: if &indiv can not update its neighbour,while it can update other subpreblems-----------//
if (type == 1)
{ // a random order to update
std::vector<int> perm2(std::vector<int>(population.size(), 0));
for(k=0; k<size; k++) perm2[k] = k;
random_shuffle(perm2.begin(), perm2.end());
for(i=0; i<population.size(); i++)
{
int k = perm2[i];
if (flag[k] == true)
{
double f1, f2;
f1 = fitnessfunction(population[k].indiv.y_obj, population[k].namda);
f2 = fitnessfunction(indiv.y_obj, population[k].namda);
if(f2<f1)
{
population[k].indiv = indiv;
time++;
}
// the maximal number of solutions updated is not allowed to exceed 'limit'
if(time>=limit)
{
flag.clear();
perm2.clear();
return;
}
}
}
perm2.clear();
}
flag.clear();
perm.clear();
}
void CMOEAD::update_reference(CIndividual &ind)
{
//ind: child solution
for(int n=0; n<nobj; n++)
{
if(ind.y_obj[n]<idealpoint[n])
{
idealpoint[n] = ind.y_obj[n];
}
}
}
void CMOEAD::update_nadpoint()
{
int i,j;
double temp_low = -1.0e30;
for (j=0; j < nobj; j++)
{
temp_low = -1e30;
for (i=0; i < population.size(); i++)
{
if (temp_low < population[i].indiv.y_obj[j])
temp_low = population[i].indiv.y_obj[j];
}
nad_point[j] = temp_low;
}
}
void CMOEAD::mate_selection(vector<int> &list, int cid, int size, int type){
// list : the set of the indexes of selected mating parents
// cid : the id of current subproblem
// size : the number of selected mating parents
// type : 1 - neighborhood; otherwise - whole population
int ss = population[cid].table.size(), id, parent;
while(list.size()<size)
{
if(type==1){
id = int(ss*rnd_uni(&rnd_uni_init));
parent = population[cid].table[id];
}
else
parent = int(population.size()*rnd_uni(&rnd_uni_init));
// avoid the repeated selection
bool flag = true;
for(int i=0; i<list.size(); i++)
{
if(list[i]==parent) // parent is in the list
{
flag = false;
break;
}
}
if(flag) list.push_back(parent);
}
}
int CMOEAD::calculate_crowd_degree(vector<CIndividual> nondonimate_population)
{
int i,j,k;
vector<double> crowd_distance;
//1.calculate the crowd degree
int min_index = -1;
double min_crowd_distance = 1e20, tmp_crowd_degree;
for (i = 0; i < nondonimate_population.size(); i++)
{
crowd_distance=vector<double>(nobj+1, 1.0e20);
for (j = 0; j < nondonimate_population.size(); j++)
{ //calculate distance to nobj-nearest neighbour
double distance_i_j = dist_vector(nondonimate_population[i].y_obj, nondonimate_population[j].y_obj);
if (distance_i_j < crowd_distance[nobj])
{
for (k=nobj; k >=1 && distance_i_j < crowd_distance[k-1]; k--)
{
crowd_distance[k] = crowd_distance[k-1];
}
crowd_distance[k] = distance_i_j;//????????????????????????????????????????????
}
}
//adapt multiple as crowd_degree
tmp_crowd_degree = 1.0;
for (k = 1; k <= nobj; k++) tmp_crowd_degree = tmp_crowd_degree * crowd_distance[k];
if (tmp_crowd_degree < min_crowd_distance)
{
min_index = i;
min_crowd_distance = tmp_crowd_degree;
}
crowd_distance.clear();
}
return min_index;
}
int CMOEAD::calculate_crowd_degree(vector<CSubproblem> nondonimate_population)
{
int i,j,k;
vector<double> crowd_distance;
//1.calculate the crowd degree
int min_index = -1;
double min_crowd_distance = 1e20, tmp_crowd_degree;
for (i = 0; i < nondonimate_population.size(); i++)
{
crowd_distance=vector<double>(nobj+1, 1.0e20);
for (j = 0; j < nondonimate_population.size(); j++)
{ //calculate distance to nobj-nearest neighbor
double distance_i_j = dist_vector(nondonimate_population[i].indiv.y_obj, nondonimate_population[j].indiv.y_obj);
if (distance_i_j < crowd_distance[nobj])
{
for (k=nobj; k >=1 && distance_i_j < crowd_distance[k-1]; k--)
{
crowd_distance[k] = crowd_distance[k-1];
}
crowd_distance[k] = distance_i_j;
}
}
//adapt multiple as crowd_degree
tmp_crowd_degree = 1.0;
for (k = 1; k <= nobj; k++) tmp_crowd_degree = tmp_crowd_degree * crowd_distance[k];
if (tmp_crowd_degree < min_crowd_distance)
{
min_index = i;
min_crowd_distance = tmp_crowd_degree;
}
crowd_distance.clear();
}
return min_index;
}
void CMOEAD::external_population_update()
{
int i,j,k;
//1. get non dominate solution from the union external_population and new_population
for (i = 0; i < new_population.size(); i++)
{
int doni_relation = 0;
for (j = 0; j < external_population.size(); j++)
{
doni_relation = donimate_judge(external_population[j].y_obj, new_population[i].y_obj);
if (doni_relation == -1)//external_population[j] dominate new_population[i]
break;
else if (doni_relation == 1)//new_population[i] dominate external_population[j]
{
external_population.erase(external_population.begin() + j);
j--;
}
}
if (doni_relation != -1) external_population.push_back(new_population[i]);
}
//*************************8.7*********************************
//**************step1选择范围内的解****************//
int sum=0;double xx; double tmp, tmp_threshold;
vector< vector<double> > InAreaPoint;
vector<double> point1,point;
vector<double> pointtoo;
double min_asf =1e30; int min_asf_index; double max_threshold = 0.0;
//vector<double> midpoint;//731*********************************
vector<bool> InArea = vector<bool>(external_population.size(), false);
vector<double> inxZ;
for (int m = 0; m < external_population.size(); m++)
{
if((ZUP-external_population[m].xZ[nvar-1] >= 0)&&(external_population[m].xZ[nvar-1]-ZBP >= 0) )
{ InArea[m] = true; sum++; }
}
if (sum >= NUM )
{
midpoint.clear();threshold.clear();
for (int i = 0; i < external_population.size(); i++)
{
if (InArea[i] == true)
{
tmp = fabs(external_population[i].xZ[nvar-1] - ZFL);
if (tmp < min_asf) { min_asf = tmp ; min_asf_index = i; }
for(int p=0 ; p<nobj ; p++)
{
point1.push_back(external_population[i].y_obj[p]);
}
InAreaPoint.push_back(point1);
point1.clear();
}
}
for(int p = 0 ; p < nobj ; p++) point.push_back(external_population[min_asf_index].y_obj[p]);
midpoint = point;
point.clear();
//cout<<"midpoint[0] = "<<midpoint[0]<<" midpoint[1] = "<<midpoint[1]<<endl;
for(int p = 0 ; p<nobj ; p++)
{
for (int q = 0; q < sum; q++)
{
tmp_threshold = fabs( InAreaPoint[q][p] - midpoint[p]);
if ( tmp_threshold >= max_threshold ) { max_threshold = tmp_threshold ; }
}
xx = relaxfactor1*max_threshold;
threshold.push_back(xx);
}
}
//*************************8.7********************************
//2. use prefer information to delete the individual in EP
if (external_population.size() > size_exter_pop)//moea.cpp size_exter_pop=1.5*MOEAD.pops=30
{
vector<bool> InPreferArea = vector<bool>(external_population.size(), false);
for (i = 0; i < external_population.size(); i++) external_population[i].crowd_rank = 1e30;
int min_asf_index;
double tmp_distance;
double min_asf =1e30;
//*************************** //
for (i=0; i<external_population.size(); i++) external_population[i].crowd_rank = 1e30;
//*************************** //
for (j = 0; j < numMultipeLightBeams; j++)
{ //find the middle individual in the external population
min_asf_index = -1;
min_asf = 1.0e30; double max_threshold = 0.0;
//*********************中心点**************************//
//**************step1选择范围内的解****************//
int sum=0;
vector< vector<double> > InAreaPoint;
//vector<double> midpoint;//731*********************************
vector<bool> InArea = vector<bool>(external_population.size(), false);
for (int m = 0; m < external_population.size(); m++)
{
if((ZUP-external_population[m].xZ[nvar-1] >= 0)&&(external_population[m].xZ[nvar-1]-ZBP >= 0) )
{ InArea[m] = true; sum++; }
}
if (sum < NUM ) break;
/*{
if(!strcmp("ankang20001012", strTestInstance))
{
midpoint.push_back(328.0);
midpoint.push_back(6600.0);
threshold.push_back(4.0);
threshold.push_back(2000);
}
else if (!strcmp("ankang20030828", strTestInstance))
{
midpoint.push_back(325.0);
midpoint.push_back(3800.0);
threshold.push_back(4.0);
threshold.push_back(2000);
}
else if(!strcmp("ankang20051001", strTestInstance))
{
midpoint.push_back(325.5);
midpoint.push_back(12000.0);
threshold.push_back(4.0);
threshold.push_back(2000.0);
}
else if(!strcmp("ankang20100715", strTestInstance))
{
midpoint.push_back(326.0);
midpoint.push_back(5500.0);
threshold.push_back(4.0);
threshold.push_back(2000.0);
}
else {}
}*/
//mark the outrank relationship between the middle individual
for (i = 0; i < external_population.size(); i++)
{
tmp_distance = 0.0;
for (k = 0; k < nobj; k++)
{
//double tmep = (external_population[i].y_obj[k] - external_population[min_asf_index].y_obj[k])/ VetoThreshold[j][k];
//double tmep = (external_population[i].y_obj[k] - midpoint[k])/ VetoThreshold[j][k];
double tmep = (external_population[i].y_obj[k] - midpoint[k])/threshold[k];
tmp_distance += tmep * tmep;
}
if (tmp_distance <= 1) InPreferArea[i] = true;
if (tmp_distance <= external_population[i].crowd_rank)
external_population[i].crowd_rank = tmp_distance;
}
//threshold.clear();//************************812
}
vector<int> index;
for (i = 0; i < external_population.size(); i++)
{ if (InPreferArea[i] == false) index.push_back(i);}
{//Select the solutions whose preference neighbor distances are top size_exter_pop small number
//from the preference neighborhoods set as new external population.
vector<double> CorwdRank;
for (i = 0; i < external_population.size(); i++) CorwdRank.push_back(external_population[i].crowd_rank);
index.clear();
for (i = 0; i < external_population.size(); i++) index.push_back(i);
minfastsort(CorwdRank, index, index.size(), size_exter_pop);//moea.cpp 160 size_exter_pop = 1.5 * MOEAD.pops;
sort(index.begin() + size_exter_pop, index.end());
for (i = index.size() - 1; i >= size_exter_pop; i--) external_population.erase(external_population.begin() + index[i]);
CorwdRank.clear();
}
index.clear();
InPreferArea.clear();
}
//3.diversity preserve to external_population
if (external_population.size() > size_exter_pop)
{
double min_distance = 0.0, dist_i_j = 0.0;
for (i = external_population.size() - 2; i >= 0; i--)
{ //get the min distance of individual in external population to the selected individuals
min_distance = 1e30;
for (j = external_population.size() - 1; j > i; j--)
{//calculate the distance of indiv[i] and indiv[j] in external population
dist_i_j = dist_vector_square(external_population[i].y_obj, external_population[j].y_obj);
if (dist_i_j < min_distance) min_distance = dist_i_j;
}
if (min_distance < EpsilonDistance * EpsilonDistance)
{
external_population.erase(external_population.begin() + i);
i++;
}
if (external_population.size() <= size_exter_pop) break;
}
}
//cout<<"size of EP: "<<external_population.size()<<endl;
//4.diversity preserve to external_population
int index_min;
if (external_population.size() > size_exter_pop)
{
while (external_population.size() > size_exter_pop)
{ //calculate crowd degree
index_min = calculate_crowd_degree(external_population);
//2.delete the individual
external_population.erase(external_population.begin() + index_min);
}
}
new_population.clear();
}
int CMOEAD::find_sparse_indiv_from_EP()
{
int i,j,k;
//3. calculate the crowd degree of each individual in exter_pop if it is added to evol_pop
vector<double> crowd_distance;
int index = 0;
double min_crowd_distance = -1.0,tmp,distance_i_j;
//3.1 calculate the crowd degree
for (j = 0; j < external_population.size(); j++)
{
crowd_distance=vector<double>(nobj+1, 1.0e20);
for (i = 0; i < population.size(); i++)
{
//calculate distance to nobj-nearest neighbor
distance_i_j = dist_vector(external_population[j].y_obj, population[i].indiv.y_obj);
if (distance_i_j < crowd_distance[nobj])
{
for (k=nobj; k >=1 && distance_i_j < crowd_distance[k-1]; k--)
{
crowd_distance[k] = crowd_distance[k-1];
}
crowd_distance[k] = distance_i_j;
}
}
//adapt multiple as crowd_degree
tmp = 1.0;
for (k = 1; k <= nobj; k++) tmp *= crowd_distance[k];
external_population[j].crowd_rank = tmp;
crowd_distance.clear();
if (external_population[j].rank < external_population[index].rank)
{
index = j;//一个在一个不在,选在范围内的那个
}
else if ( (external_population[j].rank == 0) && (external_population[index].rank == 0) )
{
if ( external_population[j].crowd_rank > external_population[index].crowd_rank )
index = j;
//都在范围内,选最稀疏的
}
else if ( (external_population[j].rank == 1) && (external_population[index].rank == 1) )
{
if ( external_population[j].crowd_rank < external_population[index].crowd_rank )
index = j;
//都不在时选密集处的那个
}
//else if ( (external_population[j].rank == 2) && (external_population[index].rank == 2) )
//{
// if ( external_population[j].crowd_rank > external_population[index].crowd_rank ) index = j;
//}
}
return index;
}
void CMOEAD::delete_undesired_subproblems()
{
int i,j,k;
// 1. prior avoid visiting the undesired areas;
vector<bool> InPreferArea = vector<bool>(population.size(), false); //record the solution whether in prefer area or not
for (i = 0; i < population.size(); i++) population[i].indiv.crowd_rank = 1e30; //record the distance to the middle individual
double tmp_distance;
//*****************
for (i = 0; i < population.size(); i++) population[i].indiv.crowd_rank = 1e30;
//*****************
for (j = 0; j < numMultipeLightBeams; j++)
{
//*****************************************选择范围内的解************************************************//
int sum=0;
vector< vector<double> > InAreaPoint;
//vector<double> midpoint;//731*********************************
vector<bool> InArea = vector<bool>(external_population.size(), false);
for (int m = 0; m < external_population.size(); m++)
{
if( (ZUP - external_population[m].xZ[nvar-1] >= 0)&&(external_population[m].xZ[nvar-1]-ZBP >= 0) )
{ InArea[m] = true; sum++; }
}
cout<<"__________waibu______________total="<<sum<<endl;
if (sum < NUM ) break;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
vector<bool> InA = vector<bool>(population.size(), false);
int num = 0;
for (int m = 0; m < population.size(); m++)
{
if( (ZUP - population[m].indiv.xZ[nvar-1] >= 0)&&(population[m].indiv.xZ[nvar-1]-ZBP >= 0) )
{ InA[m] = true; num++; }
}
cout<<"XXXXXXXXXXXXXXXXXXXXXXXXXtotal="<<num<<endl;
for (int i = 0; i < population.size(); i++)
{
if (InA[i] == true)
{
cout<<i<<" mm ";
}
}
//**********&&&&&&&&&&&&&&&&&&&&&&**********812&&&&&&&&&&&&&&&&&&&
cout<<endl;
for(int s = 0 ; s < nobj ; s++)
cout<<"midpoint"<<" = "<<midpoint[s]<<" ";
cout<<"threshold[0] = "<<threshold[0]<<" threshold[1] = "<<threshold[1]<<endl;
//midpoint.clear();//731*********************************
//******************************************中心点******************************************************//
for (i = 0; i < population.size(); i++)
{
tmp_distance = 0.0;
for (k = 0; k < nobj; k++)
{
//double tmep = (population[i].indiv.y_obj[k] - population[min_asf_index].indiv.y_obj[k])/ VetoThreshold[j][k];
//double tmep = (population[i].indiv.y_obj[k] - midpoint[k])/ VetoThreshold[j][k];
double tmep = (population[i].indiv.y_obj[k] - midpoint[k])/ (threshold[k]*relaxfactor);
tmp_distance += tmep * tmep;
}
tmp_distance = sqrt(tmp_distance);
if (tmp_distance <= 1) InPreferArea[i] = true;
if (tmp_distance < population[i].indiv.crowd_rank)
population[i].indiv.crowd_rank = tmp_distance;
}
//threshold.clear();//*****************************812
}
//save the subproblems with weight vectors (1,0,...,0),(0,1,0,...,0),...,(0,...,0,1)
//for (i = 0; i < nobj; i++) InPreferArea[i] = true;
vector<int> index;
vector<double> dist;//???????????????????????????????????????????????????
for (i = 0; i < population.size(); i++)
{
if (InPreferArea[i] == false)
{
index.push_back(i);
dist.push_back(population[i].indiv.crowd_rank);
}
}
//for (i = 0; i < index.size(); i++) cout<<population[index[i]].indiv.crowd_rank<<" ";
//cout<<endl;
if (index.size() >= num_update_weight)
{ //prior avoid visiting the undesired areas;
minfastsort(dist, index, index.size(), index.size() - num_update_weight);
sort(index.begin()+(index.size() - num_update_weight), index.end());
int num = (index.size() - num_update_weight);
//for (i = index.size() - 1; i >= (index.size() - num_update_weight); i--) cout<<population[index[i]].indiv.crowd_rank<<" ";
//cout<<endl;
for (i = index.size() - 1; i >= num; i--) population.erase(population.begin() + index[i]);
}
else
{ //prior avoid visiting the undesired areas;
//for (i = index.size() - 1; i >= 0; i--) cout<<population[index[i]].indiv.crowd_rank<<" ";
sort(index.begin(), index.end());
for (i = index.size() - 1; i >= 0; i--) population.erase(population.begin() + index[i]);
//then delete the subproblem whose optimal solutions are in the prefer area but have high crowd;
int num = num_update_weight - index.size();
for (i = 0; i < num; i++)
{ //find the subproblem with highest crowd degree
int min_index = calculate_crowd_degree(population);
//cout<<population[min_index].indiv.crowd_rank<<" ";
population.erase(population.begin() + min_index);
}
//cout<<endl;
}
InPreferArea.clear();
index.clear();
dist.clear();
}
void CMOEAD::add_spare_subproblems_to_prefer_area()
{
int i,j,k,m;
//1 calculate outrank relation
//*********************
for (i = 0 ; i < external_population.size(); i++) external_population[i].crowd_rank = 1e30;
//************************
for (i = 0 ; i < external_population.size(); i++)
{
int min_asf_index;
double min_asf, tmp_distance;
for (j = 0; j < numMultipeLightBeams; j++)
{ //find the middle individual in the external population
min_asf_index = -1;
min_asf = 1.0e30; double max_threshold = 0.0;
//******************************************选择范围内的解***********************************************//
int sum=0;
vector< vector<double> > InAreaPoint;
vector<bool> InArea = vector<bool>(external_population.size(), false);
vector<double> inxZ;
for (int m = 0; m < external_population.size(); m++)
{
if( (ZUP - external_population[m].xZ[nvar-1] >= 0)&&(external_population[m].xZ[nvar-1]-ZBP >= 0))
{ InArea[m] = true; sum++; }
}
if(sum < NUM ) break;
//***************************************中心点***********************************************************//
//mark the outrank relationship between the middle individual
for (int m = 0; m < external_population.size(); m++)
{
tmp_distance = 0.0;
for (k = 0; k < nobj; k++)
{
//double tmep = (external_population[m].y_obj[k] - external_population[min_asf_index].y_obj[k])/ VetoThreshold[j][k];
//double tmep = (external_population[m].y_obj[k] - midpoint[k])/ VetoThreshold[j][k];
double tmep = (external_population[m].y_obj[k] - midpoint[k])/(threshold[k]*relaxfactor2);
tmp_distance += tmep * tmep;
}
if (tmp_distance <= external_population[m].crowd_rank)
external_population[m].crowd_rank = tmp_distance;
}
//threshold.clear();//*********************************812
}
if (external_population[i].crowd_rank <= 1) external_population[i].rank = 0;
else external_population[i].rank = 1;
}
//2. reprocess--- not consider the boundary remote solution in exter_pop ----//
//for (i = 0 ; i < external_population.size(); i++)
//{
// for (j = 0; j < nobj; j++)
// {
// if ( fabs(external_population[i].y_obj[j] - idealpoint[j]) < 1e-6)
//
// }external_population[i].rank = 2;
//}
vector<double> direction=vector<double>(nobj, 0.0);
//use weight solution (WS)- transformation to calculate the optimal subproblem to the most sparse solution in EP
for (i = 0; i < num_update_weight; i++)
{ //3.1 calculate the min crowd_degree of individual in external_population to evol_pop
int index_min_crowd_degree = find_sparse_indiv_from_EP();
//3.2 add new subproblem
CSubproblem sub;
int rnd_index = int(rnd_uni(&rnd_uni_init)*population.size());
sub.saved = population[rnd_index].indiv;//let init delta max
sub.indiv = external_population[index_min_crowd_degree];//
sub.utility = 1.0;
//the direction of landa_t is same as the f-z* (external_population[index_min_crowd_degree].y_obj - idealpoint)
for (m = 0; m < nobj; m++) direction[m] = external_population[index_min_crowd_degree].y_obj[m] - idealpoint[m];
for (m = 0; m < nobj; m++)
{ if (direction[m] < 1e-20) direction[m] = 1e-7;}
double sum = 0.0;
for (m = 0; m < nobj; m++) sum += 1.0 / direction[m];
for (m = 0; m < nobj; m++) sub.namda[m] = (1.0 / direction[m]) / sum;
population.push_back(sub);
external_population[index_min_crowd_degree].rank = 3;
}
direction.clear();
update_neighbour_table();
}
void CMOEAD::organize_merge_split()//组织 合并 分离
{
external_population_update();
cout<<midpoint[0]<<" "<<midpoint[1]<<endl;
delete_undesired_subproblems();
add_spare_subproblems_to_prefer_area();
}
void CMOEAD::evol_population()
{
// random order of subproblems at each generation
//vector<int> order(vector<int>(pops,0));
//for(int i=0; i<pops; i++) order[i] = i;
//random_shuffle(order.begin(), order.end());shuffle:洗牌
vector<int> order; this->tour_selection(10, order);
for(int sub=0; sub<order.size(); sub++)
{
int c_sub = order[sub]; // random order
int type;
double rnd = rnd_uni(&rnd_uni_init);
// mating selection based on probability
if(rnd<prob) type = 1; // from neighborhood
else type = 2; // from population
// select the indexes of mating parents
vector<int> plist;
mate_selection(plist, c_sub, 2, type); // neighborhood selection
// produce a child solution
CIndividual child;
//double rate2 = 0.5; //rate + 0.25*(rnd_uni(&rnd_uni_init) - 0.5);
//diff_evo_xoverB(population[c_sub].indiv,population[plist[0]].indiv,population[plist[1]].indiv, child, rate2);
if (!strcmp("SBX",strCrossType))
{
real_sbx_xoverB(population[plist[0]].indiv,population[plist[1]].indiv,child);
}
else
{
double rate2 = 0.5; //rate + 0.25*(rnd_uni(&rnd_uni_init) - 0.5);
diff_evo_xoverB(population[c_sub].indiv,population[plist[0]].indiv,population[plist[1]].indiv, child, rate2);
}
plist.clear();
// apply polynomial mutation
realmutation(child, 1.0/nvar);
// evaluate the child solution
child.obj_eval();
// update the reference points and other solutions in the neighborhood or the whole population
update_reference(child);
update_problem(child, c_sub, type);
nfes++;//************************************评价次数加一&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&//
//if (nfes >= start_time_exter_pop_update)
new_population.push_back(child);
}
if (!strcmp("_TCH2",strFunctionType))
update_nadpoint();
order.clear();
}
void CMOEAD::exec_emo(int run)
{
int i,j; int frequence_update_weight;
char filename[1024];
seed = (seed + 23)%1377;
rnd_uni_init = -(long)seed;
// initialization
nfes = 0;
prob = 1;//?
rate_update_weight = 0.2;//
evol_rate = 0.4;//