




已阅读5页,还剩6页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一 需求分析1本程序演示的是用简单遗传算法随机一个种群,然后根据所给的交叉率,变异率,世代数计算最大适应度所在的代数2演示程序以用户和计算机的对话方式执行,即在计算机终端上显示“提示信息”之后,由用户在键盘上输入演示程序中规定的命令;相应的输入数据和运算结果显示在其后。3测试数据输入初始变量后用y=100*(x1*x1-x2)*(x1*x2-x2)+(1-x1)*(1-x1)其中-2.048=x1,x2=2.048作适应度函数求最大适应度即为函数的最大值二 概要设计1程序流程图开始Gen=0编码随机产生M个初始个体满足终止条件?计算群体中各个体适应度从左至右依次执行遗传算子j = 0j = 0j = 0根据适应度选择复制个体选择两个交叉个体选择个体变异点执行变异执行交叉执行复制复制的个体添入新群体中交叉后添入新群体中变异后添入新群体中j = j+1j = j+2j = j+1Gen=Gen+1输出结果终止YNYYYNNNpcpm2类型定义int popsize; /种群大小 int maxgeneration; /最大世代数 double pc; /交叉率 double pm; /变异率struct individual char chromchromlength+1;double value; double fitness; /适应度;int generation; /世代数int best_index;int worst_index;struct individual bestindividual; /最佳个体struct individual worstindividual; /最差个体struct individual currentbest;struct individual populationPOPSIZE;3函数声明void generateinitialpopulation(); void generatenextpopulation();void evaluatepopulation();long decodechromosome(char *,int,int);void calculateobjectvalue();void calculatefitnessvalue();void findbestandworstindividual();void performevolution();void selectoperator();void crossoveroperator();void mutationoperator();void input();void outputtextreport();4程序的各函数的简单算法说明如下:(1)void generateinitialpopulation ()和void input ()初始化种群和遗传算法参数。input() 函数输入种群大小,染色体长度,最大世代数,交叉率,变异率等参数。(2) void calculateobjectvalue();计算适应度函数值 。根据给定的变量用适应度函数计算然后返回适度值。(3)选择函数selectoperator()在函数selectoperator()中首先用rand ()函数产生01间的选择算子,当适度累计值不为零时,比较各个体所占总的适应度百分比的累计和与选择算子,直到达到选择算子的值那个个体就被选出,即适应度为fi的个体以fi/fk的概率继续存在;显然,个体适应度愈高,被选中的概率愈大。但是,适应度小的个体也有可 能被选中,以便增加下一代群体的多样性。(4)染色体交叉函数crossoveroperator()这是遗传算法中的最重要的函数之一,它是对个体两个变量所合成的染色体进行交叉,而不是变量染色体的交叉,这要搞清楚。首先用rand ()函数产生随机概率,若小于交叉概率,则进行染色体交叉,同时交叉次数加1。这时又要用rand()函数随机产生一位交叉位,把染色体的交叉位的后面部分交叉即可;若大于交叉概率,则进行简单的染色体复制即可。(5)染色体变异函数mutation()变异是针对染色体字符变异的,而不是对个体而言,即个体变异的概率是一样。随机产生比较概率,若小于变异概率,则1变为0,0变为1,同时变异次数加1。(6)long decodechromosome(char *,int,int)本函数是染色体解码函数,它将以数组形式存储的二进制数转成十进制数,然后才能用适应度函数计算。(7)void findbestandworstindividual()本函数是求最大适应度个体的,每一代的所有个体都要和初始的最佳比较,如果大于就赋给最佳。(8)void outputtextreport () 输出种群统计结果输出每一代的种群的最大适应度和平均适应度,最后输出全局最大值三 运行环境本程序的开发工具是VC+,在VC+下运行。四 源代码#include #include#include#include#define POPSIZE 500#define maximization 1#define minimization 2#define 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 individual char chromchromlength+1;double value; double fitness; /适应度;int generation; /世代数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 decodechromosome(char *,int,int);void calculateobjectvalue();void calculatefitnessvalue();void findbestandworstindividual();void performevolution();void selectoperator();void crossoveroperator();void mutationoperator();void input();void outputtextreport();void generateinitialpopulation( ) /种群初始化int i,j;for (i=0;ipopsize; i+)for(j=0;jchromlength;j+)populationi.chromj=(rand()%105)?0:1;populationi.chromchromlength=0;void generatenextpopulation() /生成下一代selectoperator();crossoveroperator();mutationoperator();void evaluatepopulation() /评价个体,求最佳个体calculateobjectvalue();calculatefitnessvalue();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+)if(*pointer-0)decimal +=(long)pow(2,i);return (decimal);void calculateobjectvalue() /计算函数值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;populationi.value=100*(x1*x1-x2)* (x1*x1-x2)+(1-x1)*(1-x1);void calculatefitnessvalue()/计算适应度int i;double temp; for(i=0;i0.0) temp=cmin+populationi.value; else temp=0.0; else if (functionmode=minimization) if(populationi.valuecmax) temp=cmax-populationi.value; else temp=0.0;populationi.fitness=temp;void findbestandworstindividual( ) /求最佳个体和最差个体int i;double sum=0.0;bestindividual=population0;worstindividual=population0;for (i=1;ibestindividual.fitness)bestindividual=populationi;best_index=i;else if (populationi.fitness=currentbest.fitness)currentbest=bestindividual;void performevolution() /演示评价结果if (bestindividual.fitnesscurrentbest.fitness)currentbest=populationbest_index;elsepopulationworst_index=currentbest;void selectoperator() /比例选择算法int i,index;double p,sum=0.0;double cfitnessPOPSIZE;struct individual newpopulationPOPSIZE;for(i=0;ipopsize;i+)sum+=populationi.fitness;for(i=0;ipopsize; i+)cfitnessi=populationi.fitness/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() /交叉算法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;ipopsize-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;jchromlength;j+)p=rand()%1000/1000.0;if (ppm)populationi.chromj=(populationi.chromj=0)?1:0;void input() /数据输入 printf(初始化全局变量:n);printf( 种群大小(50-500):); scanf(%d, &popsize); if(popsize%2) != 0) printf( 种群大小已设置为偶数n);popsize+; printf( 最大世代数(100-300):); scanf(%d, &maxgeneration); printf( 交叉率(0.2-0.99):); scanf(%f, &pc); printf( 变异率(0.001-0.1):); scanf(%f, &pm);void outputtextreport()/数据输出int i;double sum
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《初中英语时态应用与语境学习》
- 电子商务网络安全防范措施知识点
- 癌痛患者的充分镇痛探讨
- 九年级物理动态电路分析
- 加油站安全风险管控手册
- 音乐与舞蹈的地方特色在全球舞台的呈现与设计
- 风力发电在办公楼宇中的应用及前景分析
- 顾客情感管理提升新零售体验的策略
- 音乐产业与数字技术的结合发展
- 非洲中小企业发展市场机遇与支持政策
- 大学武术智慧树知到期末考试答案章节答案2024年浙江大学
- 国家开放大学2022《土木工程力学(本)》形考作业1-5参考答案
- 河南省许昌市2023-2024学年高一下学期期末考试生物试题(无答案)
- 广东省金山中学、中山一中、佛山一中、宝安中学四校2023-2024学年高二下学期第一次联考数学试卷(无答案)
- 浙江省嘉兴市2023-2024学年八年级下学期6月期末语文试题
- 农产品购销合同范本版
- 语文五年级下册第六单元大单元整体教学设计
- 鸡毛信的故事-红色故事课件
- 中国古都西安英文介绍课件
- 国家开放大学《合同法》章节测试参考答案
- 初中英语七选五经典5篇(附带答案)
评论
0/150
提交评论