遗传算法matlab实现源程序.docx
《遗传算法matlab实现源程序.docx》由会员分享,可在线阅读,更多相关《遗传算法matlab实现源程序.docx(9页珍藏版)》请在冰豆网上搜索。
遗传算法matlab实现源程序
附页:
一.遗传算法源程序:
clc;
clear;
population;
%评价目标函数值
foruim=1:
popsize
vector=population(uim,:
);
obj(uim)=hanshu(hromlength,vector,phen);
end
%obj
%min(obj)
clearuim;
objmin=min(obj);
forsequ=1:
popsize
ifobj(sequ)==objmin
opti=population(sequ,:
);
end
end
clearsequ;
fmax=22000;
%==
forgen=1:
maxgen
%选择操作
%将求最小值的函数转化为适应度函数
forindivi=1:
popsize
obj1(indivi)=1/obj(indivi);
end
clearindivi;
%适应度函数累加总合
total=0;
forindivi=1:
popsize
total=total+obj1(indivi);
end
clearindivi;
%每条染色体被选中的几率
forindivi=1:
popsize
fitness1(indivi)=obj1(indivi)/total;
end
clearindivi;
%各条染色体被选中的范围
forindivi=1:
popsize
fitness(indivi)=0;
forj=1:
indivi
fitness(indivi)=fitness(indivi)+fitness1(j);
end
end
clearj;
fitness;
%选择适应度高的个体
forranseti=1:
popsize
ran=rand;
while(ran>1||ran<0)
ran=rand;
end
ran;
ifran<=fitness
(1)
newpopulation(ranseti,:
)=population(1,:
);
else
forfet=2:
popsize
if(ran>fitness(fet-1))&&(ran<=fitness(fet))
newpopulation(ranseti,:
)=population(fet,:
);
end
end
end
end
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
cpoint1=round(rand*hromlength);
cpoint2=round(rand*hromlength);
while(cpoint2==cpoint1)
cpoint2=round(rand*hromlength);
end
ifcpoint1>cpoint2
tem=cpoint1;
cpoint1=cpoint2;
cpoint2=tem;
end
cpoint1;
cpoint2;
forterm=cpoint1+1:
cpoint2
forss=1:
hromlength
ifpopcross(int,ss)==popfath(term)
tem1=popcross(int,ss);
popcross(int,ss)=popcross(int,term);
popcross(int,term)=tem1;
end
end
cleartem1;
end
forterm=cpoint1+1:
cpoint2
forss=1:
hromlength
ifpopcross(int+1,ss)==popmoth(term)
tem1=popcross(int+1,ss);
popcross(int+1,ss)=popcross(int+1,term);
popcross(int+1,term)=tem1;
end
end
cleartem1;
end
end
clearterm;
end
clearrandnum;
popcross;
%变异操作
newpop=popcross;
forint=1:
popsize
randnum=rand;
ifrandnum
cpoint12=round(rand*hromlength);
cpoint22=round(rand*hromlength);
if(cpoint12==0)
cpoint12=1;
end
if(cpoint22==0)
cpoint22=1;
end
while(cpoint22==cpoint12)
cpoint22=round(rand*hromlength);
ifcpoint22==0;
cpoint22=1;
end
end
temp=newpop(int,cpoint12);
newpop(int,cpoint12)=newpop(int,cpoint22);
newpop(int,cpoint22)=temp;
end
end
newpop;
clearcpoint12;
clearcpoint22;
clearrandnum;
clearint;
forium=1:
popsize
vector1=newpop(ium,:
);
obj1(ium)=hanshu(hromlength,vector1,phen);
end
clearium;
obj1max=max(obj1);
forar=1:
popsize
ifobj1(ar)==obj1max
newpop(ar,:
)=opti;
end
end
%遗传操作结束
二.粒子群算法源程序:
%------初始格式化--------------------------------------------------
clearall;
clc;
formatlong;
%------给定初始化条件----------------------------------------------
c1=1.4962;%学习因子1
c2=1.4962;%学习因子2
w=0.7298;%惯性权重
MaxDT=100;%最大迭代次数
D=2;%搜索空间维数(未知数个数)
N=40;%初始化群体个体数目
eps=10^(-6);%设置精度(在已知最小值时候用)
%------初始化种群的个体(可以在这里限定位置和速度的范围)------------
fori=1:
N
forj=1:
D
x(i,j)=randn;%随机初始化位置
v(i,j)=randn;%随机初始化速度
end
end
%------先计算各个粒子的适应度,并初始化Pi和Pg----------------------
fori=1:
N
p(i)=fitness(x(i,:
),D);
y(i,:
)=x(i,:
);
end
pg=x(1,:
);%Pg为全局最优
fori=2:
N
iffitness(x(i,:
),D)pg=x(i,:
);
end
end
%------进入主要循环,按照公式依次迭代,直到满足精度要求------------
fort=1:
MaxDT
t
fori=1:
N
v(i,:
)=w*v(i,:
)+c1*rand*(y(i,:
)-x(i,:
))+c2*rand*(pg-x(i,:
));
x(i,:
)=x(i,:
)+v(i,:
);
iffitness(x(i,:
),D)
p(i)=fitness(x(i,:
),D);
y(i,:
)=x(i,:
);
end
ifp(i)pg=y(i,:
);
end
end
Pbest(t)=fitness(pg,D);
end
%------进入主要循环,按照公式依次迭代,直到满足精度要求------------
fort=1:
MaxDT
fori=1:
N
v(i,:
)=w*v(i,:
)+c1*rand*(y(i,:
)-x(i,:
))+c2*rand*(pg-x(i,:
));
x(i,:
)=x(i,:
)+v(i,:
);
iffitness(x(i,:
),D)
p(i)=fitness(x(i,:
),D);
y(i,:
)=x(i,:
);
end
ifp(i)pg=y(i,:
);
end
end
Pbest(t)=fitness(pg,D);
end
%------最后给出计算结果
disp('*************************************************************')
disp('函数的全局最优位置为:
')
Solution=pg'
disp('最后得到的优化极值为:
')
Result=fitness(pg,D)
disp('*************************************************************')
[X,Y]=meshgrid(-500:
2:
500);
Z=X.*sin(sqrt(X))+Y.*(sin(sqrt(Y)));
holdon
contour(X,Y,Z)
plot(x(:
1),x(:
2),'*');
holdoff