用浮点数编码解决遗传算法问题

上传人:人*** 文档编号:500087804 上传时间:2023-11-03 格式:DOC 页数:8 大小:97.50KB
返回 下载 相关 举报
用浮点数编码解决遗传算法问题_第1页
第1页 / 共8页
用浮点数编码解决遗传算法问题_第2页
第2页 / 共8页
用浮点数编码解决遗传算法问题_第3页
第3页 / 共8页
用浮点数编码解决遗传算法问题_第4页
第4页 / 共8页
用浮点数编码解决遗传算法问题_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《用浮点数编码解决遗传算法问题》由会员分享,可在线阅读,更多相关《用浮点数编码解决遗传算法问题(8页珍藏版)》请在金锄头文库上搜索。

1、遗传算法作业一:用浮点数编码解决以下问题。f(x,x)=,icos01)Xi*,icos01)Xi1212i=1i=1其中一10X,X1012求minf(x,x)12二:构造过程。第一步:确定决策变量和约束条件。题目中已给出了该问题的决策变量及约束条件。第二步:建立优化模型。既是题目给出所要求的。第三步:确定编码方法。第四步:确定解码方法。第五步:确定个体评价方法。这里可将个体的适应度直接取为对应的目标函数值,并且不再对它做其他变化处理,即有F(X)=f(x1,x2)第六步:设计遗传算子。选择运算使用比例运算算子;交叉运算是用单点交叉算子;变异运算使用基本位变异算子。第七步:确定遗传算法的运行

2、参数。对于本例,设计基本遗传算法的运行参数如下MAXGENS=80POPSIZE=20NVARS=2PXOVER=0.2PMUTATION=0.1三:代码如下。/*最大世代数*/*种群大小*/*变量个数*/*交叉概率*/*变异概率*/#includestdafx.h#include#include#include#defineMAXGENS80#definePOPSIZE20#defineNVARS2#definePXOVER0.2#definePMUTATION0.1#defineFALSE0#defineTRUE1intgeneration;/*最大世代数*/*种群大小*/*变量个数*/*

3、交叉概率*/*变异概率*/*当前世代*/intcur_best;/*最佳个体*/structgenotypedoublegeneNVARS;doublefitness;doubleupperNVARS;doublelowerNVARS;doublerfitness;doublecfitness;structgenotypepopulationPOPSIZE+1;structgenotypenewpopulationPOPSIZE+1;voidinitialize(void);doublerandval(double,double);voidevaluate(void);voidkeep_the

4、_best(void);voidelitist(void);voidselect(void);voidcrossover(void);voidXover(int,int);voidswap(double*,double*);voidmutate(void);voidreport(void);voidinitialize(void)inti,j;doublelbound,ubound;for(i=0;iNVARS;i+)scanf(%lf,&lbound);scanf(%lf,&ubound);for(j=0;jPOPSIZE;j+)populationj.fitness=0;populatio

5、nj.rfitness=0;populationj.cfitness=0;populationj.loweri=lbound;populationj.upperi=ubound;populationj.genei=randval(populationj.loweri,populationj.upperi);doublerandval(doublelow,doublehigh)doubleval;val=(double)(rand()%1000)/1000.0)*(high-low)+low;return(val);voidevaluate(void)intmem;inti;intX=0;int

6、Y=0;doublexNVARS+1;for(mem=0;memPOPSIZE;mem+)for(i=0;iNVARS;i+)xi+1=populationmem.genei;for(i=1;i=5;i+)X=X+(i*cos(i+1)*x1+i);Y=Y+(i*cos(i+1)*x2+i);populationmem.fitness=(-1)*X*Y;voidkeep_the_best()intmem;inti;cur_best=0;for(mem=0;mempopulationPOPSIZE.fitness)cur_best=mem;populationPOPSIZE.fitness=po

7、pulationmem.fitness;for(i=0;iNVARS;i+)populationPOPSIZE.genei=populationcur_best.genei;voidelitist()inti;doublebest,worst;intbest_mem,worst_mem;best=population0.fitness;worst=population0.fitness;for(i=0;ipopulationi+1.fitness)if(populationi.fitness=best)best=populationi.fitness;best_mem=i;if(populat

8、ioni+1.fitness=worst)worst=populationi+1.fitness;worst_mem=i+1;elseif(populationi.fitness=best)best=populationi+1.fitness;best_mem=i+1;if(best=populationPOPSIZE.fitness)for(i=0;iNVARS;i+)populationPOPSIZE.genei=populationbest_mem.genei;populationPOPSIZE.fitness=populationbest_mem.fitness;elsefor(i=0

9、;iNVARS;i+)populationworst_mem.genei=populationPOPSIZE.genei;populationworst_mem.fitness=populationPOPSIZE.fitness;voidselect(void)intmem,i,j,k;doublesum=0;doublep;for(mem=0;memPOPSIZE;mem+)sum+=populationmem.fitness;for(mem=0;memPOPSIZE;mem+)populationmem.rfitness=populationmem.fitness/sum;populati

10、on0.cfitness=population0.rfitness;for(mem=1;memPOPSIZE;mem+)populationmem.cfitness=populationmem-1.cfitness+populationmem.rfitness;for(i=0;iPOPSIZE;i+)p=rand()%1000/1000.0;if(ppopulation0.cfitness)newpopulationi=population0;elsefor(j=0;j=populationj.cfitness&ppopulationj+1.cfitness)newpopulationi=po

11、pulationj+1;for(i=0;iPOPSIZE;i+)populationi=newpopulationi;voidcrossover(void)inti,mem,one;intfirst=0;doublex;for(mem=0;memPOPSIZE;+mem)x=rand()%1000/1000.0;if(x1)if(NVARS=2)point=1;elsepoint=(rand()%(NVARS-1)+1;for(i=0;ipoint;i+)swap(&populationone.genei,&populationtwo.genei);voidswap(double*x,doub

12、le*y)doubletemp;temp=*x;*x=*y;*y=temp;voidmutate(void)inti,j;doublelbound,hbound;doublex;for(i=0;iPOPSIZE;i+)for(j=0;jNVARS;j+)x=rand()%1000/1000.0;if(xPMUTATION)lbound=populationi.lowerj;hbound=populationi.upperj;populationi.genej=randval(lbound,hbound);voidreport(void)inti;doublebest_val;doubleavg;doublestddev;doublesum_square;doublesquare_sum;doublesum;sum=0.0;sum_square=0.0;for(i=0;iPOPSIZE;i+)sum+=populationi.fitness;sum_square+=populationi.fitness*populationi.

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

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号