您好,欢迎访问三七文档
#includestdafx.h#includestdio.h#includestdlib.h#includetime.h#includemath.h#includeiostreamusingnamespacestd;//全局变量intpopsize;//种群大小intmaxgeneration;//最大世代数doublepc=0.0;//交叉率doublepm=0.0;//变异率#definePOPSIZE500//种群大小#definechromlength8//染色体长度structindividual//定义染色体个体结构体{intchrom[chromlength];//定义染色体二进制表达形式,editbyppme将char转为intdoublevalue;//染色体的值doublefitness;//染色体的适应值};intgeneration;//当前执行的世代数intbest_index;//最好的染色体索引序号intworst_index;//最差的染色体索引序号structindividualbestindividual;//最佳染色体个体structindividualworstindividual;//最差染色体个体structindividualcurrentbest;//当前最好的染色体个体currentbeststructindividualpopulation[POPSIZE];//种群数组//函数声明voidinput();//输入接口voidgenerateinitialpopulation();//初始化当代种群voidevaluatepopulation();//评价种群voidcalculateobjectfitness();//计算种群适应度doubledecodechromosome(int,int);//染色体解码voidfindbestandworstindividual();//寻找最好的和最坏的染色体个体voidcurrent_result();//显示当前结果voidselectoperator();//选择操作voidcrossoveroperator();//交换操作voidmutationoperator();//变异操作voidgeneratenextpopulation();voidperformevolution();voidoutputtextreport();intmain(intargc,char*argv[]){//inti;srand((unsigned)time(NULL));//强制类型转化,以当前时间戳定义随机数种子printf(本程序为求函数y=x*x的最大值/n);intgeneration=0;//初始化generation当前执行的代input();//初始化种群大小、交叉率、变异率printf(popsize%d;maxgeneration%d;pc%f;pm%f/n/n,popsize,maxgeneration,pc,pm);generateinitialpopulation();//产生初始化种群evaluatepopulation();//评价当前种群,(A.计算种群/个体的适应度;B.找出最好和最差的个体)current_result();//显示当前的结果while(generationmaxgeneration){//小于最大世代数,执行循环体generation++;generatenextpopulation();//生成子代种群(A.选择;B.交叉;C.变异)evaluatepopulation();//评价新生子代种群performevolution();//进行子代进化outputtextreport();//输入当代最终种群current_result();//显示当前的结果}printf(/n);printf(统计结果:);printf(/n);printf(最大函数值等于:%f/n,currentbest.fitness);printf(其染色体编码为:);for(inti=0;ichromlength;i++)printf(%d,currentbest.chrom[i]);printf(值为:%d,currentbest.value);//fitness有问题return0;}voidinput()//数据输入{printf(初始化全局变量:/n);printf(种群大小(50-500偶数):);scanf(%d,&popsize);//输入种群大小,必须为偶数if((popsize%2)!=0){printf(种群大小已设置为偶数/n);popsize++;};printf(最大世代数(100-300):);//输入最大世代数scanf(%d,&maxgeneration);printf(交叉率(0.2-0.99):);//输入交叉率scanf(%lf,&pc);printf(变异率(0.001-0.1):);//输入变异率scanf(%lf,&pm);}voidgenerateinitialpopulation()//种群初始化{inti,j;srand((unsigned)time(NULL));//强制类型转化,以当前时间戳定义随机数种子for(i=0;ipopsize;i++){for(j=0;jchromlength;j++){population[i].chrom[j]=(rand()%105)?0:1;//rand()%10随机产生0-9的整数//,小于5标注0,否则标注1//printf(%d,population[i].chrom[j]);}//printf(/n);}}voidcalculateobjectfitness()//计算染色体个体适应值和适应度{inti;//printf(calculateobjectfitnessisexecuting!);for(i=0;ipopsize;i++){doubletemp;temp=decodechromosome(i,chromlength);//计算个体适应值population[i].value=(double)temp;population[i].fitness=population[i].value*population[i].value;}}voidevaluatepopulation()//评价种群{calculateobjectfitness();//计算种群每个个体的适应度findbestandworstindividual();//找到最好和最差的染色体个体}doubledecodechromosome(intpop_index,intlength)//给染色体解码{inti;doubledecimal=0;for(i=0;ilength;i++)decimal+=population[pop_index].chrom[i]*pow(2.0,i);//遍历染色体二进制编码,return(decimal);//并计算出其10进制的value值}voidcurrent_result(){inti,j;/*printf(显示当前种群结果:/n);printf(/n);printf(染色体,染色体值,适应度值/n);for(i=0;ipopsize;i++){for(j=0;jchromlength;j++){printf(%d,population[i].chrom[j]);}printf(%lf,population[i].value);printf(%lf,population[i].fitness);printf(/n);}printf(/n);printf(适应度最优值:%d/n,bestindividual.fitness);*/}voidfindbestandworstindividual()//求最佳个体和最差个体{inti;doublesum=0.0;bestindividual=population[0];worstindividual=population[0];for(i=1;ipopsize;i++){if(population[i].fitnessbestindividual.fitness)//依次比较,找出最佳个体{bestindividual=population[i];best_index=i;}elseif(population[i].fitnessworstindividual.fitness)//依次比较,找出最差个体{worstindividual=population[i];worst_index=i;}sum+=population[i].fitness;//sum存放种群总体适应值}//forif(generation==0){currentbest=bestindividual;//第一代最好的暂时存放在currentbest}else{if(bestindividual.fitness=currentbest.fitness)//第n代最好的,通过比较大于以往最好个体的话,{//暂时存放在currentbestcurrentbest=bestindividual;}}}voidgeneratenextpopulation()//生成下一代{selectoperator();//选择crossoveroperator();//交叉mutationoperator();//变异}voidselectoperator()//比例选择算法{inti,index;doublep,sum=0.0;//p存放随机概率,sum存放个体适应率和累计适应率doublecfitness[POPSIZE];//当代种群染色体个体的适应率structindividualnewpopulation[POPSIZE];//新种群srand((unsigned)time(NULL));//种下随机种子for(i=0;ipopsize;i++){//sum+=population[i].fitness;//sum存放种群适应值总和}for(i=0;ipopsize;i++){cfitness[i]=population[i].fitness/sum;//cfitness[]=fitness/sum得到个体适应率}for(i=1;ipopsize;i++){cfitness[i]=cfitness[i-1]+cfitness[i];//cfitness[]=cfitness[i-1]+cfitness[i]得到种群}//累计适应率for(i=0;ipopsize;i++)//for循环实现轮盘赌算法{p=rand()%1000/1000.0;//得到千分位小数index=0;while(pcfitness[index]){index++;}newpopulation[i]=population[index];//选出的个体组成新的一代,暂时存放于newpopulation[]中}for(i=0;ipopsize;i++){population[i]=newpopulation[i];//全局变量populaiton存放新的种群(有重复的值)}}voidcrossoveroperator()//交叉算法{inti,j;intindex[POPSIZE];intpoint,temp;doublep;srand((unsigned)time(NULL));//种下随机种子for(i=0;ipopsize;i++){//初始化index[]数组index[i]=i;}for(i=0;ipopsize;i++){//for循环实现种群内随机两两交换point=rand()%(popsize-i);//打乱种群顺序//随机产生0-pop
本文标题:遗传算法例子
链接地址:https://www.777doc.com/doc-2009839 .html