欢迎来到天天文库
浏览记录
ID:57816911
大小:47.50 KB
页数:14页
时间:2020-03-30
《标准遗传算法-c++源程序.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#include#include#definePOPSIZE500#defineMAXIMIZATION1#defineMINIMIZATION2#definerandom(x)(rand()%(x))#defineCmax100/*最大值函数适应度的设置*/#defineCmin0/*最小值函数适应度的设置*/#defineLENGTH110#defineLENGTH210#defineCHROMLENG
2、THLENGTH1+LENGTH2intFunctionMode=MAXIMIZATION;/*optimizationtype即函数类型,是求最大值函数还是最小值函数*/inta=100;intPopSize=80;intMaxGeneration=200;doublePc=0.6;doublePm=0.001;structindividual{charchrom[CHROMLENGTH+1];doublevalue;doublefitness;};intgeneration;intbest_index;intworst_index;structi
3、ndividualbestindividual;structindividualworstindividual;structindividualcurrentbest;structindividualpopulation[POPSIZE];voidGenerateInitialPopulation(void);voidGenerateNextPopulation(void);voidEvaluatePopulation(void);longDecodeChromosome(char*,int,int);voidCaculateObjectValue(
4、void);voidCaculateFitnessValue(void);voidFindBestAndWorstIndividual(void);voidPerformEvolution(void);voidSelectionOperator(void);voidCrossoverOperator(void);voidMutationOperator(void);voidOutputTextReport(void);voidmain(){srand((unsigned)time(NULL));//随时间而改变随机数generation=0;Gene
5、rateInitialPopulation();EvaluatePopulation();while(generation6、n[i].chrom[j]=(random(10)<5)?'1':'0';}population[i].chrom[CHROMLENGTH]=' ';}}voidGenerateNextPopulation(void){SelectionOperator();CrossoverOperator();MutationOperator();}voidEvaluatePopulation(void){CaculateObjectValue();CaculateFitnessValue();FindBestAndWorstIndividual();}lon7、gDecodeChromosome(char*string,intpoint,intlength){inti;longdecimal=0;char*pointer;for(i=0,pointer=string+point;i8、emp1=DecodeChromosome(population[i].chrom,0,LENGTH1);t
6、n[i].chrom[j]=(random(10)<5)?'1':'0';}population[i].chrom[CHROMLENGTH]=' ';}}voidGenerateNextPopulation(void){SelectionOperator();CrossoverOperator();MutationOperator();}voidEvaluatePopulation(void){CaculateObjectValue();CaculateFitnessValue();FindBestAndWorstIndividual();}lon
7、gDecodeChromosome(char*string,intpoint,intlength){inti;longdecimal=0;char*pointer;for(i=0,pointer=string+point;i8、emp1=DecodeChromosome(population[i].chrom,0,LENGTH1);t
8、emp1=DecodeChromosome(population[i].chrom,0,LENGTH1);t
此文档下载收益归作者所有