您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 规章制度 > 遗传算法C语言源代码(一元函数和二元函数)
C语言遗传算法代码以下为遗传算法的源代码,计算一元代函数的代码和二元函数的代码以+++++++++++++++++++++++++++++++++++++为分割线分割开来,请自行选择适合的代码,使用时请略看完代码的注释,在需要更改的地方更改为自己需要的代码。+++++++++++++++++++++++++++++++一元函数代码++++++++++++++++++++++++++++#includestdio.h#includestdlib.h#includetime.h#includemath.h#definePOPSIZE1000#definemaximization1#defineminimization2#definecmax100#definecmin0#definelength120#definechromlengthlength1//染色体长度//注意,你是求最大值还是求最小值intfunctionmode=minimization;//变量的上下限的修改开始floatmin_x1=-2;//变量的下界floatmax_x1=-1;//变量的上界//变量的上下限的修改结束intpopsize;//种群大小intmaxgeneration;//最大世代数doublepc;//交叉率doublepm;//变异率structindividual{charchrom[chromlength+1];doublevalue;doublefitness;//适应度};intgeneration;//世代数intbest_index;intworst_index;structindividualbestindividual;//最佳个体structindividualworstindividual;//最差个体structindividualcurrentbest;structindividualpopulation[POPSIZE];//函数声明voidgenerateinitialpopulation();voidgeneratenextpopulation();voidevaluatepopulation();longdecodechromosome(char*,int,int);voidcalculateobjectvalue();voidcalculatefitnessvalue();voidfindbestandworstindividual();voidperformevolution();voidselectoperator();voidcrossoveroperator();voidmutationoperator();voidinput();voidoutputtextreport();voidgenerateinitialpopulation()//种群初始化{inti,j;for(i=0;ipopsize;i++){for(j=0;jchromlength;j++){population[i].chrom[j]=(rand()%2010)?'0':'1';}population[i].chrom[chromlength]='\0';}}voidgeneratenextpopulation()//生成下一代{selectoperator();crossoveroperator();mutationoperator();}voidevaluatepopulation()//评价个体,求最佳个体{calculateobjectvalue();calculatefitnessvalue();findbestandworstindividual();}longdecodechromosome(char*string,intpoint,intlength)//给染色体解码{inti;longdecimal=0;char*pointer;for(i=0,pointer=string+point;ilength;i++,pointer++)if(*pointer-'0'){decimal+=(long)pow(2,i);}return(decimal);}voidcalculateobjectvalue()//计算函数值{inti;longtemp1,temp2;doublex1;for(i=0;ipopsize;i++){temp1=decodechromosome(population[i].chrom,0,length1);x1=(max_x1-min_x1)*temp1/(1024*1024-1)+min_x1;//目标函数修改开始population[i].value=(pow(x1,5)-3*x1-1)*(pow(x1,5)-3*x1-1);//目标函数修改结束}}voidcalculatefitnessvalue()//计算适应度{inti;doubletemp;for(i=0;ipopsize;i++){if(functionmode==maximization){if((population[i].value+cmin)0.0){temp=cmin+population[i].value;}else{temp=0.0;}}elseif(functionmode==minimization){if(population[i].valuecmax){temp=cmax-population[i].value;}else{temp=0.0;}}population[i].fitness=temp;}}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;}if(generation==0){currentbest=bestindividual;}else{if(bestindividual.fitness=currentbest.fitness){currentbest=bestindividual;}}}voidperformevolution()//演示评价结果{if(bestindividual.fitnesscurrentbest.fitness){currentbest=population[best_index];}else{population[worst_index]=currentbest;}}voidselectoperator()//比例选择算法{inti,index;doublep,sum=0.0;doublecfitness[POPSIZE];structindividualnewpopulation[POPSIZE];for(i=0;ipopsize;i++){sum+=population[i].fitness;}for(i=0;ipopsize;i++){cfitness[i]=population[i].fitness/sum;}for(i=1;ipopsize;i++){cfitness[i]=cfitness[i-1]+cfitness[i];}for(i=0;ipopsize;i++){p=rand()%1000/1000.0;index=0;while(pcfitness[index]){index++;}newpopulation[i]=population[index];}for(i=0;ipopsize;i++){population[i]=newpopulation[i];}}voidcrossoveroperator()//交叉算法{inti,j;intindex[POPSIZE];intpoint,temp;doublep;charch;for(i=0;ipopsize;i++){index[i]=i;}for(i=0;ipopsize;i++){point=rand()%(popsize-i);temp=index[i];index[i]=index[point+i];index[point+i]=temp;}for(i=0;ipopsize-1;i+=2){p=rand()%1000/1000.0;if(ppc){point=rand()%(chromlength-1)+1;for(j=point;jchromlength;j++){ch=population[index[i]].chrom[j];population[index[i]].chrom[j]=population[index[i+1]].chrom[j];population[index[i+1]].chrom[j]=ch;}}}}voidmutationoperator()//变异操作{inti,j;doublep;for(i=0;ipopsize;i++){for(j=0;jchromlength;j++){p=rand()%1000/1000.0;if(ppm){population[i].chrom[j]=(population[i].chrom[j]=='0')?'1':'0';}}}}voidinput()//数据输入{//printf(初始化全局变量:\n);//printf(种群大小(50-500):);//scanf(%d,&popsize);popsize=500;if((popsize%2)!=0){//printf(种群大小已设置为偶数\n);popsize++;};//printf(最大世代数(100-300):);//scanf(%d,&maxgeneration);maxgeneration=200;//printf(交叉率(0.2-0.99):);//scanf(%f,&pc);pc=0.95;//printf(变异率(0.001-0.1):);//scanf(%f,&pm);pm=0.03;}voidoutputtextreport()//数据输出{inti;doublesum;doubleaverage;sum=0.0;for(i=0;ipopsize;i++){sum+=population[i].value;}average=sum/popsize;printf(当前世代=%d\n当前世代平均函数值=%f\n当前世代最优函数值=%f\n,generation,average,population[best_index].value);}voidmain()//主函数{inti;longtemp1,temp2;doublex1,x2;generation=0;input();generateinitialpopulation();evaluatepopulation();while(generationmaxgeneration){generation++;generatenextpopulation();evaluatepopulation();performevolution();outputtextreport();}printf(\n);printf(统计结果:);printf(\n);//pr
本文标题:遗传算法C语言源代码(一元函数和二元函数)
链接地址:https://www.777doc.com/doc-2009825 .html