欢迎来到天天文库
浏览记录
ID:27261131
大小:55.50 KB
页数:9页
时间:2018-12-02
《用浮点数编码解决遗传算法问题》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、遗传算法作业一:用浮点数编码解决以下问题。二:构造过程。第一步:确定决策变量和约束条件。题目中已给出了该问题的决策变量及约束条件。第二步:建立优化模型。既是题目给出所要求的。第三步:确定编码方法。第四步:确定解码方法。第五步:确定个体评价方法。这里可将个体的适应度直接取为对应的目标函数值,并且不再对它做其他变化处理,即有:F(X)=f(x1,x2)第六步:设计遗传算子。选择运算使用比例运算算子;交叉运算是用单点交叉算子;变异运算使用基本位变异算子。第七步:确定遗传算法的运行参数。对于本例,设计基本遗传算法的运行参数如下:MAXGENS=80/*最大世代数*/PO
2、PSIZE=20/*种群大小*/NVARS=2/*变量个数*/PXOVER=0.2/*交叉概率*/PMUTATION=0.1/*变异概率*/三:代码如下。#include"stdafx.h"#include#include#include#defineMAXGENS80/*最大世代数*/#definePOPSIZE20/*种群大小*/#defineNVARS2/*变量个数*/#definePXOVER0.2/*交叉概率*/#definePMUTATION0.1/*变异概率*/#defineFALSE0#def
3、ineTRUE1intgeneration;/*当前世代*/intcur_best;/*最佳个体*/structgenotype{doublegene[NVARS];doublefitness;doubleupper[NVARS];doublelower[NVARS];doublerfitness;doublecfitness;};structgenotypepopulation[POPSIZE+1];structgenotypenewpopulation[POPSIZE+1];voidinitialize(void);doublerandval(double,
4、double);voidevaluate(void);voidkeep_the_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;i5、f",&ubound);for(j=0;j6、{doubleval;val=((double)(rand()%1000)/1000.0)*(high-low)+low;return(val);}voidevaluate(void){intmem;inti;intX=0;intY=0;doublex[NVARS+1];for(mem=0;mem7、));}population[mem].fitness=(-1)*X*Y;}}}voidkeep_the_best(){intmem;inti;cur_best=0;for(mem=0;mempopulation[POPSIZE].fitness){cur_best=mem;population[POPSIZE].fitness=population[mem].fitness;}}for(i=0;i8、opulation[cu
5、f",&ubound);for(j=0;j6、{doubleval;val=((double)(rand()%1000)/1000.0)*(high-low)+low;return(val);}voidevaluate(void){intmem;inti;intX=0;intY=0;doublex[NVARS+1];for(mem=0;mem7、));}population[mem].fitness=(-1)*X*Y;}}}voidkeep_the_best(){intmem;inti;cur_best=0;for(mem=0;mempopulation[POPSIZE].fitness){cur_best=mem;population[POPSIZE].fitness=population[mem].fitness;}}for(i=0;i8、opulation[cu
6、{doubleval;val=((double)(rand()%1000)/1000.0)*(high-low)+low;return(val);}voidevaluate(void){intmem;inti;intX=0;intY=0;doublex[NVARS+1];for(mem=0;mem7、));}population[mem].fitness=(-1)*X*Y;}}}voidkeep_the_best(){intmem;inti;cur_best=0;for(mem=0;mempopulation[POPSIZE].fitness){cur_best=mem;population[POPSIZE].fitness=population[mem].fitness;}}for(i=0;i8、opulation[cu
7、));}population[mem].fitness=(-1)*X*Y;}}}voidkeep_the_best(){intmem;inti;cur_best=0;for(mem=0;mempopulation[POPSIZE].fitness){cur_best=mem;population[POPSIZE].fitness=population[mem].fitness;}}for(i=0;i8、opulation[cu
8、opulation[cu
此文档下载收益归作者所有