1、遗传算法求解fxxcosx2的最大值遗传算法求解f(x)=xcosx+2的最大值其中在尺度变换部分应用到了类似模拟退火算法部分,所有变量均使用汉语拼音很好懂/中国电子科技集团公司 /第一研究室 /呼文韬 / /随机初始种群 /编码方式为格雷码 /选择方法为随机遍历 /采用了精英保存策略 /采用了自适应的交叉率和变异率 /采用了与模拟退火算法相结合的尺度变换 /采用了均匀交叉法 #include #include #include #include #include #include #include #define IM1 63 #define IM2 99 #define AM (1.0/I
2、M1) #define IMM1 (IM1-1) #define IA1 40014 #define IA2 40692 #define IQ1 53668 #define IQ2 52774 #define IR1 12211 #define IR2 3791 #define NTAB 32 #define NDIV (1+IMM1/NTAB) #define EPS 1.2e-7 #define RNMX (1.0-EPS) #define zhizhenjuli 0.005 #define PI 3. #define T0 100000/温度要取得很高才行。 #define zhongq
3、unshu1 200 #define zuobianjie -2000 #define youbianjie 2000 unsigned int seed=0; /seed 为种子,要设为全局变量 void mysrand(long int i) /初始化种子 seed = -i; long a1; /double hundun; /double c=4; /设置全局变量 struct individual unsigned *chrom; /染色体; double geti;/变量值 double shiyingdu; /目标函数的值; double fitness; /变换后的适应度值;
4、; individual *zuiyougeti;/精英保存策略 int zhongqunshu; /种群大小 individual *nowpop;/当前代 individual *newpop;/新一代 double sumfitness;/当代的总适应度fitness double sumshiyingdu;/当代的总适应度shiyingdu double maxfitness;/最大适应度 double avefitness;/平均适应度 double maxshiyingdu;/最大适应度 double avgshiyingdu;/平均适应度 float pc;/交叉概率 float
5、 pm;/变异概率 int lchrom;/染色体长度 int maxgen;/最大遗传代数 int gen;/遗传代数 /函数 int flipc(double ,double );/判断是否交叉 int flipm(double );/判断是否变异 int rnd(int low,int high);/产生low与high之间的任意数 void initialize();/遗传算法初始化 void preselectfitness(); /计算sumfiness,avefitness,maxfitness void generation(); double suijibianli();/产
6、生随机遍历指针 int fuzhi(float );/选择要复制的个体 void crossover(individual ,individual ,individual &,individual &);/交叉 void bianyi(individual &);/变异 void mubiaohanshu(individual &);/计算适应度 void chidubianhuan(individual &);/对shiyingdu进行尺度变换赋给fitness double ran1(long *);/随机数初始 void bianma(double bianliang,unsigned
7、*p);/编码 double yima(unsigned *p); void guanjiancanshujisuan();/计算shiyingdu,根据shiyingdu计算sumshiyingdu,对shiyingdu进行尺度变换变成fitness,根据fitness计算sumfitness,avefitness,maxfitness void jingyingbaoliu(); void glp(int n,int s,int *,int (*)1,float (*)1);/glp生成函数 BOOL Exist(int Val, int Num, int *Array);/判断一个数在前
8、面是否出现过 int cmpfitness(const void *p1,const void *p2) float i=(individual *)p1)-shiyingdu;/现在是按照适应度排序,改成个体的话就是按照个体排序 float j=(individual *)p2)-shiyingdu; return ij ? -1:(i=j ? 0:1);/现在是按升序牌排列,将1和-1互换后就是按降序排列 void main() initialize(); coutgeti shiyingduendl;/ for(gen=1;genmaxgen;gen+) generation(); ji
9、ngyingbaoliu(); coutsetiosflags(ios:fixed)setprecision(6)geti setiosflags(ios:fixed)setprecision(6)shiyingdu)geti=0; zuiyougeti-fitness=0; zuiyougeti-shiyingdu=0; / glp(zhongqunshu,s,h,q,xx); /for(int i=0;izhongqunshu1;i+)/产生初始种群 / / for(int j=0;js;j+) / / nowpopi.geti=zuobianjie+(youbianjie-zuobian
10、jie)*xxij; / / for(int i=0;izhongqunshu1;i+)/产生初始种群 nowpopi.geti=zuobianjie+(youbianjie-(zuobianjie)*ran1(a); /nowpop0.geti=999;/ guanjiancanshujisuan(); jingyingbaoliu(); /精英保留的实现 guanjiancanshujisuan();/计算shiyingdu,根据shiyingdu计算sumshiyingdu,对shiyingdu进行尺度变换变成fitness,根据fitness计算sumfitness,avefitnes
11、s,maxfitness void jingyingbaoliu() /精英保留的实现 individual *zuiyougetiguodu; zuiyougetiguodu=new individualzhongqunshu1;/建立一个过渡数组 for(int i=0;izhongqunshu;i+)/将当代个体复制到过渡数组中 zuiyougetiguodui=nowpopi; qsort(zuiyougetiguodu,zhongqunshu1,sizeof(individual),&cmpfitness);/按fitness升序排序 / coutzuiyougetiguodu适应度
12、:zuiyougetiguoduzhongqunshu1-1.shiyingduendl;/ / coutzuiyougeti适应度:shiyingduzuiyougeti-shiyingdu) *zuiyougeti=zuiyougetiguoduzhongqunshu1-1;/如果最优个体的fitness比当代最大的fitness小则用当代的代替之 /coutzuiyougetiguodu个体:zuiyougetiguoduzhongqunshu1-1.getiendl;/ /coutzuiyougeti个体:getiendl;/ else nowpoprnd(0,(zhongqunshu
13、1-1)=*zuiyougeti;/否则的话从当代中随机挑选一个用最优个体代替之 delete zuiyougetiguodu;/释放过渡数组 void guanjiancanshujisuan()/计算shiyingdu,根据shiyingdu计算sumshiyingdu,对shiyingdu进行尺度变换变成fitness,根据fitness计算sumfitness,avefitness,maxfitness for(int i=0;izhongqunshu;i+)/计算shiyingdu mubiaohanshu(nowpopi); for(i=0;izhongqunshu;i+)/对sh
14、iyingdu进行尺度变换变成fitness chidubianhuan(nowpopi); preselectfitness();/根据fitness计算sumfitness,avefitness,maxfitness void mubiaohanshu(individual &bianliang)/计算shiyingdu bianliang.shiyingdu=(bianliang.geti*cos(bianliang.geti)+2.0);/目标函数 void chidubianhuan(individual &bianliang)/对shiyingdu进行尺度变换变成fitness d
15、ouble T;/退火温度 T=T0*(pow(0.99,(gen+1-1); double sum=0; for(int j=0;jzhongqunshu;j+) sum+=exp(nowpopj.shiyingdu/T); bianliang.fitness=exp(bianliang.shiyingdu/T)/sum;/算出fitness void preselectfitness()/根据fitness计算sumfitness,avefitness,maxfitness int j; sumfitness=0; for(j=0;jzhongqunshu;j+) sumfitness+=
16、nowpopj.fitness; individual *guodu; guodu=new individualzhongqunshu1; for(j=0;jzhongqunshu;j+) guoduj=nowpopj; qsort(guodu,zhongqunshu1,sizeof(individual),&cmpfitness); maxfitness=guoduzhongqunshu1-1.fitness; avefitness=sumfitness/zhongqunshu1; delete guodu; void generation() individual fuqin1,fuqin
17、2,*pipeiguodu,*pipeichi; int *peiduishuzu;/用来存放产生的随机配对 pipeiguodu=new individualzhongqunshu1; pipeichi=new individualzhongqunshu1; peiduishuzu=new intzhongqunshu1; int member1,member2,j=0,fuzhijishu=0,i=0,temp=0,tt=0; float zhizhen; /随机遍历的实现 for(zhizhen=suijibianli();zhizhen1;(zhizhen=zhizhen+zhizhe
18、njuli)/设定指针1/66 pipeichifuzhijishu=nowpopfuzhi(zhizhen); fuzhijishu+; /交叉与变异的实现 /交叉 for(i=0;izhongqunshu1;i+) peiduishuzui=-1; for (i=0; izhongqunshu1; i+) temp =rnd(0,zhongqunshu1-1); /产生值在0-zhongqunshu1-1的随机数 while(Exist(temp, i, peiduishuzu)/判断产生的随机数是否已经产生过,如果是,则再产生一个随机数 temp =rnd(0,zhongqunshu1-
19、1); /如果没有的话,则把产生的随机数放在peiduishuzu中 *(peiduishuzu+i) = temp; for(i=0;izhongqunshu1-1;i=i+2) fuqin1=pipeichipeiduishuzui; fuqin2=pipeichipeiduishuzui+1; crossover(fuqin1,fuqin2,newpopi,newpopi+1); for(j=0;jzhongqunshu1;j+) /if(newpopj.geti-1000) /cout个体数值小于下界了; nowpopj.geti=newpopj.geti; / guanjiancan
20、shujisuan(); /变异的实现 for(j=0;jzhongqunshu;j+) bianyi(nowpopj); / guanjiancanshujisuan(); /精英保留的实现 jingyingbaoliu(); / guanjiancanshujisuan(); delete peiduishuzu; delete pipeichi; delete pipeiguodu; void crossover(individual parent1,individual parent2,individual &child1,individual &child2)/交叉 int j; u
21、nsigned *panduan; panduan=new unsignedlchrom; parent1.chrom=new unsignedlchrom; parent2.chrom=new unsignedlchrom; child1.chrom=new unsignedlchrom; child2.chrom=new unsignedlchrom; /coutjiaochaendl;/ bianma(parent1.geti,parent1.chrom); bianma(parent2.geti,parent2.chrom); if(flipc(parent1.fitness,pare
22、nt2.fitness) for(j=0;jlchrom;j+) panduanj=rnd(0,1); /for(j=0;jlchrom;j+)/ / / coutpanduanj;/ / / coutendl;/ / system(pause);/ for(j=0;jlchrom;j+) if(panduanj=1) child1.chromj=parent1.chromj; else child1.chromj=parent2.chromj; for(j=0;jlchrom;j+) if(panduanj=0) child2.chromj=parent1.chromj; else chil
23、d2.chromj=parent2.chromj; /for(j=0;jlchrom;j+)/ / / coutchild1.chromj;/ / /coutendl;/ / system(pause);/ child1.geti=yima(child1.chrom); child2.geti=yima(child2.chrom); delete child2.chrom; delete child1.chrom; delete parent2.chrom; delete parent1.chrom; delete panduan; else for(j=0;jlchrom;j+) child
24、1.chromj=parent1.chromj; child2.chromj=parent2.chromj; child1.geti=yima(child1.chrom); child2.geti=yima(child2.chrom); delete child2.chrom; delete child1.chrom; delete parent2.chrom; delete parent1.chrom; delete panduan; void bianyi(individual &child)/变异 child.chrom=new unsignedlchrom; /cout变异endl;
25、bianma(child.geti,child.chrom); for(int i=0;ilchrom;i+) if(flipm(child.fitness) if(child.chromi=0) child.chromi=1; else child.chromi=0; child.geti=yima(child.chrom); delete child.chrom; void bianma(double bianliang,unsigned *p)/编码 unsigned *q; unsigned *gray; q=new unsignedlchrom; gray=new unsignedlchrom; int x=0; int i=0,j=0; if(bianliangzuobianjie)/ coutbianliang:bianliangendl;/ system(pause); /coutyoubianjie-(zuobianjie)endl; /system(pause); x=(bianliang-(zuobianjie)*(pow(2,lchrom)-1)/(youbianjie-(zuobianjie); /co
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1