欢迎来到天天文库
浏览记录
ID:42055951
大小:73.00 KB
页数:7页
时间:2019-09-07
《这是我用vb写的遗传算法程序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、这是我用vb写的遗传算法程序。是一些通用代码,打具体问题对应的代码空出来,用的时候填进去。根据我的经验,要想用好遗传算法,代码必须白己写,因为问题不同,编码,杂交,变界算子可能都不一•样。特别是杂交算子,需要根据问题调整,甚至创造出新的杂交方法。而且适应度计算的代码也需要口己写,特别是带约束的优化问题see_moonlightsay:Attentiontoyour"Postformat",need"Title"OptionExplicit'遗传算法参数DimGeneLengthAsInteger'染色体长度Dimswarm
2、NumAsInteger'种群规模DimPcAsDouble'朵交概率DimPmAsDouble'突变概率DimmaxNumAsInteger'遗传算法循环次数DimpanelBoolAsBooleanDimtournamentBoolAsBoolean'种群适应度统计DimoptGeneAsInteger'ft佳个体的位置DimworstGeneAsInteger'最差个体的位置DimsumFtnessAsDouble'适应度总和DimmeanFitnessAsDouble'平均适M度DimmaxFitnessAsDou
3、ble'最人适应度DimminFtnessAsDouble'最小适应度DimstdevRtnessAsDouble'适应度标准差'DimOriPbol()AsByteDimOriRool()AsDouble'DimMateFbol()AsByteDimMatePbol()AsDoubleDimFtness()AsDoubleDimpanelFtness()AsDoubleDimFleNumAsInteger'窩斯分布随机数FunctionrandGauss()AsDoubleDimiAsIntegerrandGauss=0
4、Fori=1To20randGauss=randGauss+RndNextirandGauss=(randGauss・10)/(1.667)A0.5EndFunction'轮盘赌博选择算子FunctionpanelSelection(Ftness()AsDouble)AsIntegerDimindex,fir,las,iAsIntegerDimtemp,sum,sumFitnessAsDoublefir=LBound(Ftness)las=UBound(Fitness)sumFtness=0Fori=firTolassum
5、Ftness=sumFtness+Ftness(i)Nextitemp=Rnd*sumFtness'产生随机数index=fir-1sum=0DoWhilesum6、Integeri=lnt(swarmNum*Rnd+1)j=lnt(swarmNum*Rnd+1)IfFitness(i)>=Fitness(j)ThentournamentSele=iBsetournamentSele=jEndIfEndFunction'计算种群适应度PrivateSuboutFtness(oriPbol()AsDouble,swarmNumAsInteger)DimiAsIntegerDima,b,eAsDoubleFori=1ToswarmNum7/i★计算适应度语句***//Ftness(i)=07、结束***//Nextisumfitness=0maxFtness=Fitness(1)minFtness=Ftness(1)optGene=1worstGene=1Fori=1ToswarmNumsumfitness=sumFtness+Ftness(i)IfFitness(i)>maxFtnessThenmaxFitness=Fitness(i)optGene=iEndIfIfFtness(i)8、ess=sumFtness/swarmNumstdevFtness=0Fori=1ToswarmNumstdevFtness=stdevFitness+(Ftness(i)・meanFtness)A2NextistdevFtness=stdevFtness/swarmNumIfmaxFtness<>me
6、Integeri=lnt(swarmNum*Rnd+1)j=lnt(swarmNum*Rnd+1)IfFitness(i)>=Fitness(j)ThentournamentSele=iBsetournamentSele=jEndIfEndFunction'计算种群适应度PrivateSuboutFtness(oriPbol()AsDouble,swarmNumAsInteger)DimiAsIntegerDima,b,eAsDoubleFori=1ToswarmNum7/i★计算适应度语句***//Ftness(i)=0
7、结束***//Nextisumfitness=0maxFtness=Fitness(1)minFtness=Ftness(1)optGene=1worstGene=1Fori=1ToswarmNumsumfitness=sumFtness+Ftness(i)IfFitness(i)>maxFtnessThenmaxFitness=Fitness(i)optGene=iEndIfIfFtness(i)8、ess=sumFtness/swarmNumstdevFtness=0Fori=1ToswarmNumstdevFtness=stdevFitness+(Ftness(i)・meanFtness)A2NextistdevFtness=stdevFtness/swarmNumIfmaxFtness<>me
8、ess=sumFtness/swarmNumstdevFtness=0Fori=1ToswarmNumstdevFtness=stdevFitness+(Ftness(i)・meanFtness)A2NextistdevFtness=stdevFtness/swarmNumIfmaxFtness<>me
此文档下载收益归作者所有