遗传算法matlab实现源程序Word文件下载.docx

上传人:b****6 文档编号:19576747 上传时间:2023-01-07 格式:DOCX 页数:9 大小:46.10KB
下载 相关 举报
遗传算法matlab实现源程序Word文件下载.docx_第1页
第1页 / 共9页
遗传算法matlab实现源程序Word文件下载.docx_第2页
第2页 / 共9页
遗传算法matlab实现源程序Word文件下载.docx_第3页
第3页 / 共9页
遗传算法matlab实现源程序Word文件下载.docx_第4页
第4页 / 共9页
遗传算法matlab实现源程序Word文件下载.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

遗传算法matlab实现源程序Word文件下载.docx

《遗传算法matlab实现源程序Word文件下载.docx》由会员分享,可在线阅读,更多相关《遗传算法matlab实现源程序Word文件下载.docx(9页珍藏版)》请在冰豆网上搜索。

遗传算法matlab实现源程序Word文件下载.docx

opti=population(sequ,:

end

clearsequ;

fmax=22000;

%==

forgen=1:

maxgen

%选择操作

%将求最小值的函数转化为适应度函数

forindivi=1:

obj1(indivi)=1/obj(indivi);

clearindivi;

%适应度函数累加总合

total=0;

total=total+obj1(indivi);

%每条染色体被选中的几率

fitness1(indivi)=obj1(indivi)/total;

%各条染色体被选中的范围

fitness(indivi)=0;

forj=1:

indivi

fitness(indivi)=fitness(indivi)+fitness1(j);

clearj;

fitness;

%选择适应度高的个体

forranseti=1:

ran=rand;

while(ran>

1||ran<

0)

ran;

ifran<

=fitness

(1)

newpopulation(ranseti,:

)=population(1,:

else

forfet=2:

if(ran>

fitness(fet-1))&

&

(ran<

=fitness(fet))

)=population(fet,:

clearran;

newpopulation;

%交叉

forint=1:

2:

popsize-1

popmoth=newpopulation(int,:

popfath=newpopulation(int+1,:

popcross(int,:

)=popmoth;

popcross(int+1,:

)=popfath;

randnum=rand;

if(randnum<

P>

cpoint1=round(rand*hromlength);

cpoint2=round(rand*hromlength);

while(cpoint2==cpoint1) 

ifcpoint1>

cpoint2 

tem=cpoint1;

cpoint1=cpoint2;

cpoint2=tem;

cpoint1;

cpoint2;

forterm=cpoint1+1:

forss=1:

hromlength

ifpopcross(int,ss)==popfath(term)

tem1=popcross(int,ss);

popcross(int,ss)=popcross(int,term);

popcross(int,term)=tem1;

cleartem1;

ifpopcross(int+1,ss)==popmoth(term)

tem1=popcross(int+1,ss);

popcross(int+1,ss)=popcross(int+1,term);

popcross(int+1,term)=tem1;

 

cleartem1;

clearterm;

clearrandnum;

popcross;

%变异操作

newpop=popcross;

ifrandnum

cpoint12=round(rand*hromlength);

cpoint22=round(rand*hromlength);

if(cpoint12==0)

cpoint12=1;

if(cpoint22==0)

cpoint22=1;

while(cpoint22==cpoint12) 

ifcpoint22==0;

temp=newpop(int,cpoint12);

newpop(int,cpoint12)=newpop(int,cpoint22);

newpop(int,cpoint22)=temp;

newpop;

clearcpoint12;

clearcpoint22;

clearint;

forium=1:

vector1=newpop(ium,:

obj1(ium)=hanshu(hromlength,vector1,phen);

clearium;

obj1max=max(obj1);

forar=1:

ifobj1(ar)==obj1max

newpop(ar,:

)=opti;

%遗传操作结束

二.粒子群算法源程序:

%------初始格式化--------------------------------------------------

clearall;

formatlong;

%------给定初始化条件----------------------------------------------

c1=1.4962;

%学习因子1

c2=1.4962;

%学习因子2

w=0.7298;

%惯性权重

MaxDT=100;

%最大迭代次数

D=2;

%搜索空间维数(未知数个数)

N=40;

%初始化群体个体数目

eps=10^(-6);

%设置精度(在已知最小值时候用)

%------初始化种群的个体(可以在这里限定位置和速度的范围)------------

fori=1:

N

D

x(i,j)=randn;

%随机初始化位置

v(i,j)=randn;

%随机初始化速度

%------先计算各个粒子的适应度,并初始化Pi和Pg----------------------

p(i)=fitness(x(i,:

),D);

y(i,:

)=x(i,:

pg=x(1,:

%Pg为全局最优

fori=2:

iffitness(x(i,:

),D)<

FITNESS(pg,D)

pg=x(i,:

%------进入主要循环,按照公式依次迭代,直到满足精度要求------------

fort=1:

MaxDT

t

fori=1:

v(i,:

)=w*v(i,:

)+c1*rand*(y(i,:

)-x(i,:

))+c2*rand*(pg-x(i,:

));

x(i,:

)+v(i,:

p(i)

ifp(i)<

pg=y(i,:

Pbest(t)=fitness(pg,D);

%------最后给出计算结果

disp('

*************************************************************'

函数的全局最优位置为:

'

Solution=pg'

最后得到的优化极值为:

Result=fitness(pg,D)

[X,Y]=meshgrid(-500:

500);

Z=X.*sin(sqrt(X))+Y.*(sin(sqrt(Y)));

holdon

contour(X,Y,Z)

plot(x(:

1),x(:

2),'

*'

holdoff

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 幼儿教育 > 家庭教育

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

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