('此例程总共包含3个文件:main.c(主函数);GA.c(包含3个所用函数);GA.h(头文件),3个文件截图如下:用visualc++或者visualstutio创建工程,然后将上述3个文件包含进工程,编译运行即可。亲测可行!!!3个文件代码分别如下:1、main.c:#include#include"GA.h"usingnamespacestd;/GAdemo求函数y=xsin(10paix)+2.0的最大值编码:浮点数,1位初始群体数:50变异概率:0.8进化代数:100取值范围:[0,4]变异步长:0.004注:因为是单数浮点数编码,所以未使用基因重组函数/intmain(){GenEnginegenEngine(50,0.8,0.8,1,100,0,4);genEngine.OnStartGenAlg();getchar();}2、GA.c:#include#include#include#include#include#include"GA.h"usingnamespacestd;//srand((unsigned)time(NULL));doublerandom(){doublerandNum;randNum=rand()1.0/RAND_MAX;returnrandNum;}GenAlg::GenAlg(){}voidGenAlg::init(intpopsize,doubleMutRate,doubleCrossRate,intGenLenght,doubleLeftPoint,doubleRightPoint){popSize=popsize;mutationRate=MutRate;crossoverRate=CrossRate;chromoLength=GenLenght;totalFitness=0;generation=0;//fittestGenome=0;bestFitness=0.0;worstFitness=99999999;averageFitness=0;maxPerturbation=0.004;leftPoint=LeftPoint;rightPoint=RightPoint;//清空种群容器,以初始化vecPop.clear();for(inti=0;i=bestFitness){bestFitness=vecPop[i].fitness;fittestGenome=vecPop[i];}if(vecPop[i].fitness<=worstFitness)worstFitness=vecPop[i].fitness;}averageFitness=totalFitness/popSize;}GenomeGenAlg::GetChromoRoulette(){//产生一个0到人口总适应性评分总和之间的随机数.//中m_dTotalFitness记录了整个种群的适应性分数总和)doubleSlice=(random())totalFitness;//这个基因将承载转盘所选出来的那个个体.GenomeTheChosenOne;//累计适应性分数的和.doubleFitnessSoFar=0;//遍历总人口里面的每一条染色体。for(inti=0;i=Slice){TheChosenOne=vecPop[i];break;}}//返回转盘选出来的个体基因returnTheChosenOne;}voidGenAlg::Mutate(vector&chromo){//遵循预定的突变概率,对基因进行突变for(inti=0;irightPoint){chromo[i]=leftPoint;}//以上代码非基因变异的一般性代码只是用来保证基因编码的可行性。}}}//此函数产生新的一代,见证着整个进化的全过程.//以父代种群的基因组容器作为参数传进去,该函数将往该容器里放入新一代的基因组(当然是经过了优胜劣汰的)voidGenAlg::Epoch(vector&vecNewPop){//用类的成员变量来储存父代的基因组(在此之前m_vecPop储存的是不带估值的所有基因组)vecPop=vecNewPop;//初始化相关变量Reset();//为相关变量赋值CalculateBestWorstAvTot();//清空装载新种群的容器vecNewPop.clear();//产生新一代的所有基因组while(vecNewPop.size()baby1,baby2;//先把他们分别设置成父方和母方的基因baby1=mum.vecGenome;baby2=dad.vecGenome;//使子代基因发生基因突变Mutate(baby1);Mutate(baby2);//把两个子代基因组放到新的基因组容器里面vecNewPop.push_back(Genome(baby1,0));vecNewPop.push_back(Genome(baby2,0));}//子代产生完毕//如果你设置的人口总数非单数的话,就会出现报错if(vecNewPop.size()!=popSize){//MessageBox("你的人口数目不是单数!!!");cout<<"error"<input;doubleoutput;input.push_back(0);for(intGeneration=0;Generation<=g_Generation;Generation++){//里面是对每一条染色体进行操作for(inti=0;i=bestFitness)bestSearch=genAlg.GetBestFitness().vecGenome[0];bestFitness=genAlg.GetBestFitness().fitness;averageFitness=genAlg.GetAverageFitness();//cout<usingnamespacestd;constdoublepai=3.1415926;classGenome{public:friendclassGenAlg;friendclassGenEngine;Genome():fitness(0){}Genome(vectorvec,doublef):vecGenome(vec),fitness(f){}//类的带参数初始化参数。private:vectorvecGenome;//dFitness用于存储对该基因的适应性评估。doublefitness;//类的无参数初始化参数。};//遗传算法classGenAlg{public://这个容器将储存每一个个体的染色体vectorvecPop;//人口(种群)数量intpopSize;//每一条染色体的基因的总数目intchromoLength;//所有个体对应的适应性评分的总和doubletotalFitness;//在所有个体当中最适应的个体的适应性评分doublebestFitness;//所有个体的适应性评分的平均值doubleaverageFitness;//在所有个体当中最不适应的个体的适应性评分doubleworstFitness;//最适应的个体在m_vecPop容器里面的索引号GenomefittestGenome;//基因突变的概率,一般介于0.05和0.3之间doublemutationRate;//基因交叉的概率一般设为0.7doublecrossoverRate;//代数的记数器intgeneration;//最大变异步长doublemaxPerturbation;doubleleftPoint;doublerightPoint;//构造函数GenAlg();//初始化变量voidReset();//初始化函数voidinit(intpopsize,doubleMutRate,doubleCrossRate,intGenLenght,doubleLeftPoint,doubleRightPoint);//计算TotalFitness,BestFitness,WorstFitness,AverageFitness等变量voidCalculateBestWorstAvTot();//轮盘赌选择函数GenomeGetChromoRoulette();//基因变异函数voidMutate(vector&chromo);//这函数产生新一代基因voidEpoch(vector&vecNewPop);GenomeGetBestFitness();doubleGetAverageFitness();};classCurve{public:doublefunction(constvector&input){doublex=input[0];doubleoutput;output=xsin(10paix)+2.0;returnoutput;}private:};//遗传运算引擎classGenEngine{public:GenEngine(constint&popsize,constdouble&mutationRate,constdouble&crossoverRate,constint&numGen,constint&generation,constdouble&leftPoint,constdouble&rightPoint):genAlg(),curve(),m_population(){g_popsize=popsize;g_dMutationRate=mutationRate;g_dCrossoverRate=crossoverRate;g_numGen=numGen;g_Generation=generation;g_LeftPoint=leftPoint;g_RightPoint=rightPoint;bestFitness=0;bestSearch=0;}voidOnStartGenAlg();//报告每一代的运行情况voidreport(constint&genNum);private:GenAlggenAlg;Curvecurve;vectorm_population;intg_popsize;doubleg_dMutationRate;doubleg_dCrossoverRate;intg_numGen;intg_Generation;doubleg_LeftPoint;doubleg_RightPoint;doublebestFitness;doublebestSearch;doubleaverageFitness;};',)