标准遗传算法-c++源程序(共14页).doc

上传人:飞****2 文档编号:15186104 上传时间:2022-05-11 格式:DOC 页数:14 大小:27KB
返回 下载 相关 举报
标准遗传算法-c++源程序(共14页).doc_第1页
第1页 / 共14页
标准遗传算法-c++源程序(共14页).doc_第2页
第2页 / 共14页
点击查看更多>>
资源描述

《标准遗传算法-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);专心-专注-专业

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁