这是我在解决电梯动力学参数写的简单遗传算法程序带目标Word格式文档下载.docx
- 文档编号:5827557
- 上传时间:2023-05-05
- 格式:DOCX
- 页数:22
- 大小:55.12KB
这是我在解决电梯动力学参数写的简单遗传算法程序带目标Word格式文档下载.docx
《这是我在解决电梯动力学参数写的简单遗传算法程序带目标Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《这是我在解决电梯动力学参数写的简单遗传算法程序带目标Word格式文档下载.docx(22页珍藏版)》请在冰点文库上搜索。
voidcrossoveroperator();
voidmutationoperator();
voidinitialpopulation(int,int,double,double,double*,double*);
//种群初始化
voidgeneratenextpopulation();
//生成下一代种群
voidevaluatepopulation();
//评价个体,求最佳个体
voidcalculateobjectvalue();
//计算目标函数值
voidcalculatefitnessvalue();
//计算适应度函数值
voidfindbestandworstindividual();
//寻找最佳个体和最差个体
voidperformevolution();
voidGetResult(double*);
voidGetPopData(double**);
voidSetValueData(double*);
voidmaxandexpectation();
private:
structindividual
doublechromosome[variablenum];
//染色体编码长度应该为变量的个数
doublevalue;
doublefitness;
//适应度
};
doublevariabletop[variablenum];
//变量值
doublevariablebottom[variablenum];
intpopsize;
//种群大小
//
intgeneration;
//世代数
intbest_index;
intworst_index;
doublecrossoverrate;
//交叉率
doublemutationrate;
//变异率
intmaxgeneration;
//最大世代数
structindividualbestindividual;
//最佳个体
structindividualworstindividual;
//最差个体
structindividualcurrent;
//当前个体
structindividualcurrent1;
structindividualcurrentbest;
//当前最佳个体
CList<
structindividual,structindividual&
>
population;
//种群
newpopulation;
//新种群
double,double>
cfitness;
//存储适应度值
doublemaxfitness;
doubleminfitness;
doubleavefitness;
//怎样使链表的数据是一个结构体?
?
主要是想把种群作成链表。
节省空间。
#endif
执行文件
//CMVSOGA.cpp:
implementationfile
//
stdafx.h"
CMVSOGA.h"
math.h"
stdlib.h"
#ifdef_DEBUG
#definenewDEBUG_NEW
#undefTHIS_FILE
staticcharTHIS_FILE[]=__FILE__;
/////////////////////////////////////////////////////////////////////////////
//CMVSOGA.cpp
voidCMVSOGA:
:
initialpopulation(intps,intgen,doublecr,doublemr,double*xtop,double*xbottom)
//第一步,初始化。
inti,j;
popsize=ps;
maxgeneration=gen;
crossoverrate=cr;
mutationrate=mr;
for(i=0;
i<
variablenum;
i++)
variabletop[i]=xtop[i];
variablebottom[i]=xbottom[i];
}
srand((unsigned)time(NULL));
for(i=0;
popsize;
for(j=0;
j<
variablenum;
j++)
current.chromosome[j]=double(rand()%1000)/1000*(variabletop[j]-variablebottom[j])+variablebottom[j];
current.fitness=0;
current.value=0;
population.InsertAfter(population.FindIndex(i),current);
//除了初始化使用insertafter外,其他的用setat命令。
generatenextpopulation()//第三步,生成下一代。
selectionoperator();
crossoveroperator();
mutationoperator();
evaluatepopulation()
//第二步,评价个体,求最佳个体
calculateobjectvalue();
calculatefitnessvalue();
//在此步中因该按适应度值进行排序.链表的排序.
findbestandworstindividual();
calculateobjectvalue()
//计算函数值,应该由外部函数实现。
主要因为目标函数很复杂。
inti,j;
doublex[variablenum];
i<
i++)
current=population.GetAt(population.FindIndex(i));
//使用外部函数进行,在此只做结果的传递。
x[j]=current.chromosome[j];
current.value=current.value+(j+1)*pow(x[j],4);
////使用外部函数进行,在此只做结果的传递。
population.SetAt(population.FindIndex(i),current);
mutationoperator()
//对于浮点数编码,变异算子的选择具有决定意义。
//需要guass正态分布函数,生成方差为sigma,均值为浮点数编码值c。
doubler1,r2,p,sigma;
//sigma高斯变异参数
sigma=0.5;
//生成均值为current.chromosome,方差为sigma的高斯分布数
srand((unsignedint)time(NULL));
for(j=0;
j<
j++)
r1=double(rand()%1001)/1000;
r2=double(rand()%1001)/1000;
p=double(rand()%1000)/1000;
if(p<
mutationrate)
//高斯变异
current.chromosome[j]=(current.chromosome[j]
+sigma*sqrt(-2*log(r1)/0.4323)*sin(2*3.1415926*r2));
if(current.chromosome[j]>
variabletop[j])
current.chromosome[j]=variabletop[j];
if(current.chromosome[j]<
variablebottom[j])
current.chromosome[j]=variablebottom[j];
selectionoperator()
//从当前个体中按概率选择新种群,应该加一个复制选择,提高种群的平均适应度
//第二次循环出错
inti,j,pindex=0;
doublep,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;
{
i;
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();
i++)//求适应度总值,以便归一化,是已经排序好的链。
sum+=current.fitness;
i++)//归一化
current.fitness=current.fitness/sum;
cfitness.InsertAfter(cfitness.FindIndex(i),current.fitness);
for(i=1;
i++)//概率值从小到大;
current.fitness=cfitness.GetAt(cfitness.FindIndex(i-1))
+cfitness.GetAt(cfitness.FindIndex(i));
//归一化
cfitness.SetAt(cfitness.FindIndex(i),current.fitness);
)//轮盘赌概率选择。
本段还有问题。
p=double(rand()%1000)/1000+0.0001;
//随机生成概率
//遍历索引
pc=cfitness.GetAt(cfitness.FindIndex(0));
while(p>
=pc&
&
pindex<
popsize)
//问题所在。
pc=cfitness.GetAt(cfitness.FindIndex(pindex));
pindex++;
//必须是从index~popsize,选择高概率的数。
即大于概率p的数应该被选择,选择不满则进行下次选择。
for(j=popsize-1;
pindex&
j--)
newpopulation.InsertAfter(newpopulation.FindIndex(0),
population.GetAt(population.FindIndex(j)));
i++;
population.SetAt(population.FindIndex(i),
newpopulation.GetAt(newpopulation.FindIndex(i)));
j=newpopulation.GetCount();
j=population.GetCount();
//current
变化后,以上没有问题了。
crossoveroperator()
//非均匀算术线性交叉,浮点数适用,alpha,beta是(0,1)之间的随机数
//对种群中两两交叉的个体选择也是随机选择的。
也可取beta=1-alpha;
//current的变化会有一些改变。
doublealpha,beta;
int,int>
index;
intpoint,temp;
doublep;
i++)//生成序号
index.InsertAfter(index.FindIndex(i),i);
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);
popsize-1;
i+=2)
{//按顺序序号,按序号选择两个母体进行交叉操作。
p=double(rand()%1000)/1000.0;
if(p<
crossoverrate)
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代替
//交叉
current.chromosome[j]=(1-alpha)*current.chromosome[j]+
beta*current1.chromosome[j];
variabletop[j])
//判断是否超界.
current1.chromosome[j]=alpha*current.chromosome[j]
+(1-beta)*current1.chromosome[j];
if(current1.chromosome[j]>
current1.chromosome[j]=variabletop[j];
if(current1.chromosome[j]<
current1.chromosome[j]=variablebottom[j];
//回代
newpopulation.InsertAfter
(newpopulation.FindIndex(i),current);
(newpopulation.FindIndex(i),current1);
findbestandworstindividual()
inti;
bestindividual=population.GetAt(population.FindIndex(best_index));
worstindividual=population.GetAt(population.FindIndex(worst_index));
if(current.fitness>
bestindividual.fitness)
bestindividual=current;
best_index=i;
elseif(current.fitness<
worstindividual.fitness)
worstindividual=current;
worst_index=i;
population.SetAt(population.FindIndex(worst_index),
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 解决 电梯 动力学 参数 简单 遗传 算法 程序 目标
![提示](https://static.bingdoc.com/images/bang_tan.gif)