您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > 遗传算法(浮点数编码)
浮点数编码实现遗传算法遗传算法主要包括三个主要操作,选择、交叉和变异。用浮点数编码进行运算,三种操作方法如下:选择:1.计算if和niSf2.计算iinfPS3.累计概率1iijjgP4.产生均匀分布0~1的随机数r5.将r与ig比较,如果1iigrg,则选择个体i进入到下一代新群体6.反复执行4和5,直至新群体的个体数目等于父代群体规模交叉:11(1)(1)tttABAtttBABxxxxxx其中,1tAx和1tBx是交叉之后的个体,tAx和tBx是随机选择的两个个体,是交叉的一个常数,取值为(0,1]。变异:1maxmin()()%20()()%21tttAAAttAAxkxxrrandxxkxxrrand,,1tAx是变异之后的个体,tAx是变异之前的个体,k是变异的一个常数,取值为(0,1],maxx是个体的上限,minx是个体的下限,r是产生的随机数。适应度线性变换:FaFb其中F是原适应度,F是变换之后的适应度,a,b是变换的系数。适应度线性变换要满足下面两个条件:条件一:avgavgFF条件二:maxavgFCFC=1.2~2缩放时参数a,b的计算方法可以用如下方法:如果满足:maxmin1avgCFFFC就令:max(1)avgavgCaFFFmaxmaxavgavgavgFCFbFFF否则:minavgavgFaFFminminavgavgFFbFF实现代码如下:#includestdio.h#includestdlib.h#includemath.h#includetime.h#defineM80//种群数量#defineXMIN-1//下限#defineXMAX2//上限#definePI3.1415926#definePC0.8//交叉概率#definePM0.18//变异概率#definePA0.01//交叉因子structNode{doublePmember;doubleMyfitness;//Myfitness是适应度doubleMyfitsum;//Myfitsum是适应度占总体适应度的百分比,然后从第一个个体往后累加,主要用于选择操作}Nownode[M],Nextnode[M];//本代群体和下一代群体intnodeindex[M];//交叉时随机配对,存放配对的群体下标intT=0;doublefx(doublex)//根据x计算fx{doubley;y=x*sin(10*PI*x)+2;//y=100-(x-5)*(x-5);returny;}intcalfitness()//计算适应度值{inti;doubleminfitness,maxfitness,avefitness=0;doubleC=1.7,a,b;doubletemp;minfitness=Nownode[0].Myfitness=fx(Nownode[0].Pmember);maxfitness=minfitness;avefitness=maxfitness;for(i=1;iM;i++){Nownode[i].Myfitness=fx(Nownode[i].Pmember);avefitness+=Nownode[i].Myfitness;if(minfitnessNownode[i].Myfitness){minfitness=Nownode[i].Myfitness;}if(maxfitnessNownode[i].Myfitness){maxfitness=Nownode[i].Myfitness;}}if(minfitness0)//如果有负的适应度值,就把所以的适应度都加上一个数,使适应度全都为正数{temp=minfitness;Nownode[0].Myfitness+=-temp;avefitness=Nownode[0].Myfitness;maxfitness=Nownode[0].Myfitness;minfitness=Nownode[0].Myfitness;for(i=1;iM;i++){Nownode[i].Myfitness+=-temp;avefitness+=Nownode[i].Myfitness;if(minfitnessNownode[i].Myfitness){minfitness=Nownode[i].Myfitness;}if(maxfitnessNownode[i].Myfitness){maxfitness=Nownode[i].Myfitness;}}}//适应度线性变换avefitness=avefitness/M;//计算平均适应度if(minfitness(C*avefitness-maxfitness)/(C-1)){a=(C-1)*avefitness/(maxfitness-avefitness);b=(maxfitness-C*avefitness)*avefitness/(maxfitness-avefitness);}else{a=avefitness/(avefitness-minfitness);b=minfitness*avefitness/(avefitness-minfitness);}for(i=0;iM;i++){Nownode[i].Myfitness=a*Nownode[i].Myfitness+b;}Nownode[0].Myfitsum=Nownode[0].Myfitness;for(i=1;iM;i++){Nownode[i].Myfitsum=Nownode[i].Myfitness+Nownode[i-1].Myfitsum;//每一个Myfitsum都是自己的适应度加上前一个的Myfitsum}for(i=0;iM;i++){Nownode[i].Myfitsum=Nownode[i].Myfitsum/Nownode[M-1].Myfitsum;//每一个Myfitsum除以所有适应度之和,使Myfitsum为0~1之间}return0;}doublerandn()//产生XMIN到XMAX之间的随机数{returnXMIN+1.0*rand()/RAND_MAX*(XMAX-XMIN);}intinitpopulation()//初始化种群{inti;for(i=0;iM;i++){Nownode[i].Pmember=randn();}calfitness();//计算适应度return0;}intassignment(structNode*node1,structNode*node2)//把node2的值赋值给node1{node1-Pmember=node2-Pmember;node1-Myfitness=node2-Myfitness;node1-Myfitsum=node2-Myfitsum;return0;}intcopypopulation()//复制操作{inti,num=0;doubletemp;while(numM){temp=1.0*rand()/RAND_MAX;for(i=1;iM;i++){if(temp=Nownode[0].Myfitsum){assignment(&Nextnode[num++],&Nownode[0]);//把第一个个体复制到下一代break;}if(temp=Nownode[i-1].Myfitsum&&temp=Nownode[i].Myfitsum)//把第i个个体复制到下一代{assignment(&Nextnode[num++],&Nownode[i]);break;}}}for(i=0;iM;i++){assignment(&Nownode[i],&Nextnode[i]);//更新本代个体}calfitness();//计算适应度return0;}intisrepeat(inttemp,intn)//产生随机下标判断是否重复{inti;for(i=0;in;i++){if(nodeindex[i]==temp)return1;}return0;}intcrossover(){inti,temp;doubletemp_pc;for(i=0;iM;i++)//产生交叉点的下标{do{temp=rand()%M;}while(isrepeat(temp,i));nodeindex[i]=temp;}for(i=0;iM;i=i+2){temp_pc=1.0*rand()/RAND_MAX;//如果满足交叉的条件,就开始交叉if(temp_pc=PC){Nownode[nodeindex[i]].Pmember=PA*Nownode[nodeindex[i+1]].Pmember+(1-PA)*Nownode[nodeindex[i]].Pmember;Nownode[nodeindex[i+1]].Pmember=PA*Nownode[nodeindex[i]].Pmember+(1-PA)*Nownode[nodeindex[i+1]].Pmember;}}calfitness();//计算适应度return0;}intmutation()//变异操作{inti,temp;doublek=0.8,temp_pm;for(i=0;iM;i++){temp_pm=1.0*rand()/RAND_MAX;if(temp_pm=PM)//如果满足变异条件,就开始变异{temp=rand()%2;if(temp==0){Nownode[i].Pmember=Nownode[i].Pmember+k*(XMAX-Nownode[i].Pmember)*1.0*rand()/RAND_MAX;}else{Nownode[i].Pmember=Nownode[i].Pmember-k*(Nownode[i].Pmember-XMIN)*1.0*rand()/RAND_MAX;}}}calfitness();//计算适应度return0;}intfindmaxfit()//找到适应度最大的个体{inti,index=0;doubletemp=0;for(i=0;iM;i++){if(tempNownode[i].Myfitness){index=i;temp=Nownode[i].Myfitness;}}returnindex;}intmain(){inti=0,index;intnum=0,num1=0,num2=0;srand(time(NULL));while(num++1000){T=0;initpopulation();while(T++200){copypopulation();crossover();mutation();}index=findmaxfit();if(fabs(Nownode[index].Pmember-1.85)=0.1){num1++;}else{num2++;}}printf(正确的次数有%d次\n,num1);printf(错误的次数有%d次\n,num2);return0;}
本文标题:遗传算法(浮点数编码)
链接地址:https://www.777doc.com/doc-2009821 .html