您好,欢迎访问三七文档
实验五计算智能(2)1.实验目的理解遗传算法的求解思想,掌握遗传算法的原理,通过运用MATLAB编程(或相关编程语言)实现遗传算法,并求解实际问题,对求解结果进行分析。通过分析结果了解遗传算法在求解实际问题的特点和优势。2.实验内容编写一个遗传算法及其实际应用的程序,能运用遗传算法求解实际问题。3.实验报告要求(1)简述实验原理及方法,并请给出程序设计流程图。遗传算法本质上是对染色体模式所进行的一系列运算,即通过选择算子将当前种群中的优良模式遗传到下一代种群中,利用交叉算子进行模式重组,利用变异算子进行模式突变。通过这些遗传操作,模式逐步向较好的方向进化,最终得到问题的最优解基本组成为:a.编码(产生初始种群)b.适应度函数c.遗传算子(selection,crossover,mutation)d.运行参数求下述二元函数的最大值:f(x1,x2)=x1^2+x2^2,x1,x2取值1-7的自然数(2)源程序清单:#includestdio.h#includestdlib.h#includetime.h/////////////Thedefinitonofuserdata////#defineCmax100//certainmaximalvalue#defineCmin0//certainminimumvalue#defineLENGHT13//thechromosomelengthof1stvariable#defineLENGHT23//thechromosomelengthof2ndvariable//总染体长度#defineCHROMLENGTHLENGHT1+LENGHT2constintMaxGeneration=100;//最大代数constintPopSize=10;//样本大小constdoublePc=0.6;//交叉概率constdoublePm=0.001;//变异概率//////////////数据结构定义///////////////////structIndividual{charchrom[CHROMLENGTH+1];//一个个体的染色体doublevalue;//目标值doublefitness;//适应度};intgeneration;//进化次数intbestIndex;//最好个体的下标intworstIndex;//最坏个体的游标IndividualbestIndividual;//当前一代中的最好个体IndividualworstIndividual;///当前一代中的坏个体//bestindividualbynowIndividualcurrentBest;//到目前为止的最好个体Individualpopulation[PopSize];//样本///////////////////////voidgenerateInitialPopulation();voidgenerateNextPopulation();voidevalutePopulation();longdecomdeChromosome(char*,int,int);voidcalculateObjectValue();voidcalculateFitnessValue();voidfindBestAndWorstIndividual();voidperformEvolution();voidselectionOperator();voidcrossoverOperator();voidmutationOperator();voidoutputTextReport();//////////////////////////////////////////////intmain(){generation=0;generateInitialPopulation();evalutePopulation();while(generationMaxGeneration){generation++;generateNextPopulation();evalutePopulation();performEvolution();outputTextReport();}return0;}////////////////////////////////////////////////////////////////////产生第一代样本/////voidgenerateInitialPopulation(){inti,j;srand((unsigned)time(NULL));for(i=0;iPopSize;i++){for(j=0;jCHROMLENGTH;j++){population[i].chrom[j]=((rand()%10)5)?'0':'1';}population[i].chrom[CHROMLENGTH]='/0';}}////////产生下一代样本//////voidgenerateNextPopulation(){selectionOperator();crossoverOperator();mutationOperator();}//变异算子//voidmutationOperator(){inti,j;doublep;//bitmutationfor(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';}}}}//交叉算子///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;}}}}///选择算子/////////////voidselectionOperator(){inti,index;doublep,sum=0.0;doublecfitness[PopSize];Individualnewpopulation[PopSize];for(i=0;iPopSize;i++){sum+=population[i].fitness;}for(i=0;iPopSize;i++){cfitness[i]=population[i].fitness/sum;}//calculatecumulativefitnessfor(i=1;iPopSize;i++){cfitness[i]=cfitness[i]+cfitness[i-1];}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];}}/////依据某些公式对样本进行评价////voidevalutePopulation(){calculateObjectValue();calculateFitnessValue();findBestAndWorstIndividual();}//找出到目前为止最好的个体//////voidfindBestAndWorstIndividual(){inti;doublesum=0.0;bestIndividual=population[0];worstIndividual=population[0];for(i=0;iPopSize;i++){if(population[i].fitnessbestIndividual.fitness){bestIndividual=population[i];bestIndex=i;}elseif(population[i].fitnessworstIndividual.fitness){worstIndividual=population[i];worstIndex=i;}sum+=population[i].fitness;}if(generation==0){currentBest=bestIndividual;}else{if(bestIndividual.fitnesscurrentBest.fitness){currentBest=bestIndividual;}}}//计算适应度///voidcalculateFitnessValue(){inti;longtemp1,temp2;doublex1,x2;for(i=0;iPopSize;i++){temp1=decomdeChromosome(population[i].chrom,0,LENGHT1);temp2=decomdeChromosome(population[i].chrom,LENGHT1,LENGHT2);x1=temp1*temp1;x2=temp2*temp2;population[i].fitness=x1+x2;}}//计算目标值//目标函数为f(x)=x1*x1+x2*x2voidcalculateObjectValue(){inti;longtemp1,temp2;doublex1,x2;for(i=0;iPopSize;i++){temp1=decomdeChromosome(population[i].chrom,0,LENGHT1);temp2=decomdeChromosome(population[i].chrom,LENGHT1,LENGHT2);x1=temp1*temp1;x2=temp2*temp2;population[i].value=x1+x2;}}//把二进制转化为十进制longdecomdeChromosome(char*string,intpoint,intlength){inti;longdecimal=0L;char*pointer;for(i=0,pointer=string+point;ilength;i++,pointer++){decimal+=(*pointer-'0')(length-1-i);}returndecimal;}//进经同时把最坏个体用目前最好个人替代///voidperformEvolution(){if(bestIndividual.fitnesscurrentBest.fitness){currentBest=population[bestIndex];}else{population[worstIndex]=currentBest;}}//打印当前样本信息///voidoutputTextReport(){inti;doublesum;doubleaverage;sum=0.0;for(i
本文标题:遗传算法实验报告
链接地址:https://www.777doc.com/doc-4679032 .html