遗传算法.doc

上传人:阿宝 文档编号:1815218 上传时间:2019-10-26 格式:DOC 页数:11 大小:119.50KB
返回 下载 相关 举报
遗传算法.doc_第1页
第1页 / 共11页
遗传算法.doc_第2页
第2页 / 共11页
点击查看更多>>
资源描述

《遗传算法.doc》由会员分享,可在线阅读,更多相关《遗传算法.doc(11页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、一 需求分析 1本程序演示的是用简单遗传算法随机一个种群,然后根据所给的交叉率,变异率,世代 数计算最大适应度所在的代数 2演示程序以用户和计算机的对话方式执行,即在计算机终端上显示“提示信息”之后, 由用户在键盘上输入演示程序中规定的命令;相应的输入数据和运算结果显示在其后。 3测试数据 输入初始变量后用 y=100*(x1*x1-x2)*(x1*x2-x2)+(1-x1)*(1-x1)其中-2.048 #include #include #include #define POPSIZE 500 #define maximization 1 #define minimization 2 #d

2、efine cmax 100 #define cmin 0 #define length1 10 #define length2 10 #define chromlength length1+length2 /染色体长度int functionmode=maximization;int popsize; /种群大小 int maxgeneration; /最大世代数double pc; /交叉率double pm; /变异率 struct individualchar chromchromlength+1; double value; double fitness; /适应度 ; int ge

3、neration; /世代数 int best_index; int worst_index; struct individual bestindividual; /最佳个体 struct individual worstindividual; /最差个体 struct individual currentbest;struct individual populationPOPSIZE; /函数声明 void generateinitialpopulation(); void generatenextpopulation(); void evaluatepopulation(); long d

4、ecodechromosome(char *,int,int); void calculateobjectvalue(); void calculatefitnessvalue(); void findbestandworstindividual(); void performevolution(); void selectoperator(); void crossoveroperator(); void mutationoperator(); void input(); void outputtextreport();void generateinitialpopulation( ) /种

5、群初始化 int i,j;for (i=0;i0.0)temp=cmin+populationi.value;elsetemp=0.0; else if (functionmode=minimization)if(populationi.valuebestindividual.fitness) bestindividual=populationi; best_index=i; else if (populationi.fitness=currentbest.fitness) currentbest=bestindividual; void performevolution() /演示评价结果

6、if (bestindividual.fitnesscurrentbest.fitness) currentbest=populationbest_index; else populationworst_index=currentbest; void selectoperator() /比例选择算法 int i,index; double p,sum=0.0; double cfitnessPOPSIZE;struct individual newpopulationPOPSIZE; for(i=0;icfitnessindex) index+; newpopulationi=populati

7、onindex; for(i=0;ipopsize; i+) populationi=newpopulationi; void crossoveroperator() /交叉算法 int i,j; int indexPOPSIZE; int point,temp; double p; char ch;for (i=0;ipopsize;i+) indexi=i; for (i=0;ipopsize;i+) point=rand()%(popsize-i); temp=indexi; indexi=indexpoint+i; indexpoint+i=temp; for (i=0;ipopsiz

8、e-1;i+=2) p=rand()%1000/1000.0; if (ppc) point=rand()%(chromlength-1)+1; for (j=point; jchromlength;j+) ch=populationindexi.chromj; populationindexi.chromj=populationindexi+1.chromj; populationindexi+1.chromj=ch; void mutationoperator() /变异操作 int i,j; double p;for (i=0;ipopsize;i+) for(j=0;jchromlen

9、gth;j+) p=rand()%1000/1000.0; if (ppm) populationi.chromj=(populationi.chromj=0)?1:0; void input() /数据输入 printf(“初始化全局变量:n“); printf(“ 种群大小(50-500):“);scanf(“%d“, if(popsize%2) != 0) printf( “ 种群大小已设置为偶数n“); popsize+;printf(“ 最大世代数(100-300):“);scanf(“%d“, printf(“ 交叉率(0.2-0.99):“);scanf(“%f“, printf

10、(“ 变异率(0.001-0.1):“);scanf(“%f“, void outputtextreport()/数据输出 int i; double sum; double average; sum=0.0; for(i=0;ipopsize;i+) sum+=populationi.value; average=sum/popsize;printf(“当前世代=%dn 当前世代平均函数值=%fn 当前世代最高函数值 =%fn“,generation,average,populationbest_index.value); void main() /主函数 int i; printf(“本程

11、序为求函数 y=100*(x1*x1-x2)*(x1*x2-x2)+(1-x1)*(1-x1)的最大值 n 其中- 2.048=x1,x2=2.048n“);generation=0;input();generateinitialpopulation(); evaluatepopulation(); while(generationmaxgeneration) generation+; generatenextpopulation(); evaluatepopulation(); performevolution(); outputtextreport(); printf(“n“); printf(“ 统计结果: “);printf(“n“); printf(“最大函数值等于:%fn“,currentbest.fitness); printf(“其染色体编码为:“); for (i=0;ichromlength;i+) printf(“%c“,currentbest.chromi);printf(“n“); 六 测试结果

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

当前位置:首页 > 技术资料 > 技术规范

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

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com