ImageVerifierCode 换一换
格式:DOCX , 页数:22 ,大小:55.12KB ,
资源ID:9996539      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/9996539.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(这是我在解决电梯动力学参数写的简单遗传算法程序带目标.docx)为本站会员(b****8)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

这是我在解决电梯动力学参数写的简单遗传算法程序带目标.docx

1、这是我在解决电梯动力学参数写的简单遗传算法程序带目标这是我在解决电梯动力学参数写的简单遗传算法(程序带目标函数值、适应度值计算,但是我的适应度函数因为目标函数的计算很特殊,一起放在了程序外面计算,在此不提供)。头文件:/ CMVSOGA.h : main header file for the CMVSOGA.cpp/ / 沈阳航空工业学院 动力工程系 / 作者:李立新 / 完成日期:2006.08.02 / / 本来想使用链表里面套链表的,程序调试比较麻烦,改为种群用链表表示/染色体固定为16的方法。#if !defined(AFX_CMVSOGA_H_45BECA_61EB_4A0E_97

2、46_9A94D1CCF767_INCLUDED_)#define AFX_CMVSOGA_H_45BECA_61EB_4A0E_9746_9A94D1CCF767_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER 1000#include Afxtempl.h#define variablenum 16class CMVSOGApublic:CMVSOGA();void selectionoperator();void crossoveroperator();void mutationoperator();void initial

3、population(int, int ,double ,double,double *,double *);/种群初始化void generatenextpopulation();/生成下一代种群void evaluatepopulation();/评价个体,求最佳个体void calculateobjectvalue();/计算目标函数值void calculatefitnessvalue();/计算适应度函数值void findbestandworstindividual();/寻找最佳个体和最差个体void performevolution();void GetResult(doubl

4、e *);void GetPopData(double *);void SetValueData(double *);void maxandexpectation();private:struct individualdouble chromosomevariablenum;/染色体编码长度应该为变量的个数double value; double fitness;/适应度;double variabletopvariablenum;/变量值double variablebottomvariablenum;/变量值int popsize;/种群大小/int generation;/世代数int

5、best_index;int worst_index;double crossoverrate;/交叉率double mutationrate;/变异率int maxgeneration;/最大世代数struct individual bestindividual;/最佳个体struct individual worstindividual;/最差个体struct individual current; /当前个体struct individual current1; /当前个体struct individual currentbest;/当前最佳个体CList population;/种群C

6、List newpopulation;/新种群CList cfitness;/存储适应度值/double maxfitness;/double minfitness;/double avefitness;/怎样使链表的数据是一个结构体?主要是想把种群作成链表。节省空间。;#endif执行文件/ CMVSOGA.cpp : implementation file/#include stdafx.h#include CMVSOGA.h#include math.h#include stdlib.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEs

7、tatic char THIS_FILE = _FILE_;#endif/ CMVSOGA.cppvoid CMVSOGA:initialpopulation(int ps, int gen ,double cr ,double mr,double *xtop,double *xbottom)/第一步,初始化。int i ,j;popsize=ps ;maxgeneration=gen;crossoverrate=cr;mutationrate =mr;for (i=0;ivariablenum;i+)variabletopi =xtopi;variablebottomi =xbottomi;

8、srand( (unsigned)time( NULL ) );for(i=0;ipopsize;i+)for (j=0;jvariablenum ;j+)current.chromosomej=double(rand()%1000)/1000*(variabletopj-variablebottomj)+variablebottomj;current.fitness=0;current.value=0;population.InsertAfter(population.FindIndex(i),current);/除了初始化使用insertafter外,其他的用setat命令。void CM

9、VSOGA:generatenextpopulation()/第三步,生成下一代。selectionoperator();crossoveroperator();mutationoperator();void CMVSOGA:evaluatepopulation() /第二步,评价个体,求最佳个体/calculateobjectvalue();calculatefitnessvalue();/在此步中因该按适应度值进行排序.链表的排序.findbestandworstindividual();void CMVSOGA: calculateobjectvalue() /计算函数值,应该由外部函数

10、实现。主要因为目标函数很复杂。int i,j; double xvariablenum;for (i=0; ipopsize; i+)current=population.GetAt(population.FindIndex(i); current.value=0;/使用外部函数进行,在此只做结果的传递。for (j=0;jvariablenum;j+)xj=current.chromosomej;current.value=current.value+(j+1)*pow(xj,4);/使用外部函数进行,在此只做结果的传递。population.SetAt(population.FindInd

11、ex(i),current);void CMVSOGA:mutationoperator()/对于浮点数编码,变异算子的选择具有决定意义。/需要guass正态分布函数,生成方差为sigma,均值为浮点数编码值c。int i,j;double r1,r2,p,sigma;/sigma高斯变异参数sigma=0.5;for (i=0;ipopsize;i+)current=population.GetAt(population.FindIndex(i);/生成均值为current.chromosome,方差为sigma的高斯分布数srand(unsigned int) time (NULL);fo

12、r(j=0; jvariablenum; j+)r1 =double( rand()%1001)/1000;r2 = double(rand()%1001)/1000;p=double(rand()%1000)/1000;if(pvariabletopj)current.chromosomej=variabletopj;if (current.chromosomejvariablebottom j)current.chromosomej=variablebottom j;population.SetAt(population.FindIndex(i),current);void CMVSOGA

13、:selectionoperator() /从当前个体中按概率选择新种群,应该加一个复制选择,提高种群的平均适应度/第二次循环出错int i,j,pindex=0;double p,pc,sum=0;i=0;j=0;pindex=0;p=0;pc=0;sum=0.001;newpopulation.RemoveAll();cfitness.RemoveAll();/链表排序/population.SetAt (population.FindIndex(0),current);/多余代码for (i=1;ipopsize;i+)current=population.GetAt(populatio

14、n.FindIndex(i);for(j=0;ji;j+)/从小到大用before排列。current1=population.GetAt(population.FindIndex(j);/临时借用变量if(current.fitness=current1.fitness)population.InsertBefore(population.FindIndex(j),current);population.RemoveAt(population.FindIndex(i+1);break;/m=population.GetCount();/链表排序for(i=0;ipopsize;i+)/求适应

15、度总值,以便归一化,是已经排序好的链。current=population.GetAt(population.FindIndex(i);sum+=current.fitness;for(i=0;ipopsize; i+)/归一化current=population.GetAt(population.FindIndex(i);current.fitness=current.fitness/sum;cfitness.InsertAfter (cfitness .FindIndex(i),current.fitness);for(i=1;ipopsize; i+)/概率值从小到大;current.f

16、itness=cfitness.GetAt (cfitness.FindIndex(i-1)+cfitness.GetAt(cfitness.FindIndex(i); /归一化cfitness.SetAt (cfitness .FindIndex(i),current.fitness);population.SetAt(population.FindIndex(i),current);for (i=0;i=pc&pindexpopsize)/问题所在。pc=cfitness.GetAt(cfitness .FindIndex(pindex);pindex+;/必须是从indexpopsize

17、,选择高概率的数。即大于概率p的数应该被选择,选择不满则进行下次选择。for (j=popsize-1;jpindex&ipopsize;j-)newpopulation.InsertAfter (newpopulation.FindIndex(0),population.GetAt (population.FindIndex(j);i+;for(i=0;ipopsize; i+)population.SetAt (population.FindIndex(i),newpopulation.GetAt (newpopulation.FindIndex(i);/j=newpopulation.G

18、etCount();/j=population.GetCount();newpopulation.RemoveAll();/current 变化后,以上没有问题了。void CMVSOGA: crossoveroperator() /非均匀算术线性交叉,浮点数适用,alpha ,beta是(0,1)之间的随机数/对种群中两两交叉的个体选择也是随机选择的。也可取beta=1-alpha;/current的变化会有一些改变。int i,j;double alpha,beta;CList index;int point,temp;double p;srand( (unsigned)time( NUL

19、L ) );for (i=0;ipopsize;i+)/生成序号index.InsertAfter (index.FindIndex(i),i);for (i=0;ipopsize;i+)/打乱序号point=rand()%(popsize-1);temp=index.GetAt(index.FindIndex(i);index.SetAt(index.FindIndex(i),index.GetAt(index.FindIndex(point);index.SetAt(index.FindIndex(point),temp);for (i=0;ipopsize-1;i+=2)/按顺序序号,按

20、序号选择两个母体进行交叉操作。p=double(rand()%1000)/1000.0;if (pcrossoverrate) alpha=double(rand()%1000)/1000.0;beta=double(rand()%1000)/1000.0;current=population.GetAt(population.FindIndex(index.GetAt(index.FindIndex(i);current1=population.GetAt(population.FindIndex(index.GetAt(index.FindIndex(i+1);/临时使用current1代

21、替for(j=0;jvariabletopj)/判断是否超界.current.chromosomej=variabletopj;if (current.chromosomejvariabletopj)current1.chromosomej=variabletopj;if (current1.chromosomejvariablebottom j)current1.chromosomej=variablebottom j;/回代newpopulation.InsertAfter (newpopulation.FindIndex(i),current);newpopulation.InsertA

22、fter (newpopulation.FindIndex(i),current1);j=newpopulation.GetCount();for (i=0;ipopsize;i+)population.SetAt (population.FindIndex(i),newpopulation.GetAt (newpopulation.FindIndex(i);newpopulation.RemoveAll();void CMVSOGA: findbestandworstindividual( )int i;bestindividual=population.GetAt(population.FindIndex(best_index);worstindividual=population.GetAt(population.FindIndex(worst_index);for (i=1;ibestindividual.fitness)bestindividual=current;best_index=i;else if (current.fitnessworstindividual.fitness)worstindividual=current;worst_index=i;population.SetAt(population.FindIndex(worst_index),

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1