《标准遗传算法-c++源程序(共14页).doc》由会员分享,可在线阅读,更多相关《标准遗传算法-c++源程序(共14页).doc(14页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上#include#include#include#include#include#define POPSIZE 500#define MAXIMIZATION 1#define MINIMIZATION 2#define random(x) (rand()%(x)#define Cmax 100 /*最大值函数适应度的设置*/#define Cmin 0 /*最小值函数适应度的设置*/#define LENGTH1 10#define LENGTH2 10#define CHROMLENGTH LENGTH1+LENGTH2int FunctionMode=MAXIM
2、IZATION; /*optimization type即函数类型,是求最大值函数还是最小值函数*/int a=100;int PopSize=80;int MaxGeneration=200;double Pc=0.6;double Pm=0.001;struct individualchar chromCHROMLENGTH+1;double value; double fitness;int generation;int best_index;int worst_index;struct individual bestindividual;struct individual worsti
3、ndividual;struct individual currentbest;struct individual populationPOPSIZE;void GenerateInitialPopulation(void);void GenerateNextPopulation(void);void EvaluatePopulation(void);long DecodeChromosome(char *,int,int);void CaculateObjectValue(void);void CaculateFitnessValue(void);void FindBestAndWorstI
4、ndividual(void);void PerformEvolution(void);void SelectionOperator(void);void CrossoverOperator(void);void MutationOperator(void);void OutputTextReport(void);void main() srand(unsigned)time(NULL); /随时间而改变随机数generation=0; GenerateInitialPopulation(); EvaluatePopulation(); while(generationMaxGeneratio
5、n)generation+; GenerateNextPopulation(); EvaluatePopulation(); PerformEvolution(); OutputTextReport();void GenerateInitialPopulation(void)int i,j;for(i=0;iPopSize;i+)for(j=0;jCHROMLENGTH;j+)populationi.chromj=(random(10)5)?1:0;populationi.chromCHROMLENGTH=0;void GenerateNextPopulation(void)Selection
6、Operator();CrossoverOperator();MutationOperator();void EvaluatePopulation(void)CaculateObjectValue();CaculateFitnessValue();FindBestAndWorstIndividual();long DecodeChromosome(char *string,int point,int length)int i;long decimal=0; char *pointer;for(i=0,pointer=string+point;ilength;i+,pointer+)decima
7、l+=(*pointer-0)(length-1-i);return(decimal);void CaculateObjectValue(void)int i;long temp1,temp2;double x1,x2;for(i=0;iPopSize;i+)temp1=DecodeChromosome(populationi.chrom,0,LENGTH1);temp2=DecodeChromosome(populationi.chrom,LENGTH1,LENGTH2); x1=4.096*temp1/1023.0-2.048; x2=4.096*temp2/1023.0-2.048;po
8、pulationi.value=100*(x1*x1-x2)*(x1*x1-x2)+(1-x1)*(1-x1);void CaculateFitnessValue(void)int i;double temp;for(i=0;i0.0)temp=Cmin+populationi.value;else temp=0.0;if(FunctionMode=MINIMIZATION)if(populationi.valueCmax)temp=Cmax-populationi.value;else temp=0.0; populationi.fitness=temp;void FindBestAndWo
9、rstIndividual(void)int i;double sum=0.0;bestindividual=population0;worstindividual=population0;for(i=1;ibestindividual.fitness)bestindividual=populationi;best_index=i; if(populationi.fitnesscurrentbest.fitness) currentbest=bestindividual;void PerformEvolution(void)if(bestindividual.fitnesscurrentbes
10、t.fitness) currentbest=populationbest_index;else populationworst_index=currentbest;void SelectionOperator(void)int i,index;double p;double sum=0.0;double cfitnessPOPSIZE;struct individual newpopulationPOPSIZE;for(i=0;iPopSize;i+) sum+=populationi.fitness;for(i=0;iPopSize;i+) cfitnessi=populationi.fi
11、tness/sum;for(i=1;iPopSize;i+) cfitnessi=cfitnessi-1+cfitnessi;for(i=0;icfitnessindex) index+;newpopulationi=populationindex;for(i=0;iPopSize;i+) populationi=newpopulationi;void CrossoverOperator(void)int i,j;int indexPOPSIZE;int point,temp; double p; char ch;for(i=0;iPopSize;i+) indexi=i;for(i=0;iP
12、opSize;i+) point=random(PopSize-i);/*random(PopSize-1)=rand()%(PopSize-1)*/ temp=indexi; indexi=indexpoint+i; indexpoint+i=temp;/*循环实现种群内随机两两交换打乱种群顺序*/for(i=0;iPopSize;i+=2) p=(rand()/(double)(RAND_MAX);if(pPc) point=rand()%(CHROMLENGTH-1)+1;/random(CHROMLENGTH-1)+1;for(j=point;jCHROMLENGTH;j+)ch=po
13、pulationindexi.chromj;populationindexi.chromj=populationindexi+1.chromj; populationindexi+1.chromj=ch;void MutationOperator(void)int i,j; double p;for(i=0;iPopSize;i+) for(j=0;jCHROMLENGTH;j+) p=(rand()/(double)(RAND_MAX);if(pPm)populationi.chromj=(populationi.chromj=0)?1:0;void OutputTextReport(voi
14、d) int i=0;double average;fstream kan_table; kan_table.open(f:kan_table.txt,ios:app|ios:out);double sum;sum=0.0;for(i=0;iPopSize;i+) sum+=populationi.value;average=sum/PopSize; kan_tablegen=generation,; kan_tableave=average,;kan_tablecurrentbest.value,;kan_tableendl;printf(gen=%d,ave=%f,best=%f,generation,average,currentbest.value);printf(chromosome=);for(i=0;iCHROMLENGTH;i+)printf(%c,currentbest.chromi);printf(n);专心-专注-专业