《遗传算法matlab实现源程序(11页).doc》由会员分享,可在线阅读,更多相关《遗传算法matlab实现源程序(11页).doc(11页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-遗传算法matlab实现源程序-第 11 页附页:一遗传算法源程序:clc;clear;population;%评价目标函数值for uim=1:popsize vector=population(uim,:); obj(uim)=hanshu(hromlength,vector,phen);end%obj%min(obj)clear uim;objmin=min(obj);for sequ=1:popsize if obj(sequ)=objmin opti=population(sequ,:); endendclear sequ;fmax=22000;for gen=1:maxgen%选
2、择操作%将求最小值的函数转化为适应度函数for indivi=1:popsize obj1(indivi)=1/obj(indivi);endclear indivi;%适应度函数累加总合total=0;for indivi=1:popsize total=total+obj1(indivi);endclear indivi;%每条染色体被选中的几率for indivi=1:popsize fitness1(indivi)=obj1(indivi)/total;endclear indivi;%各条染色体被选中的范围for indivi=1:popsize fitness(indivi)=0;
3、 for j=1:indivi fitness(indivi)=fitness(indivi)+fitness1(j); endendclear j;fitness;%选择适应度高的个体for ranseti=1:popsize ran=rand; while (ran1|ran0) ran=rand; end ran; if ranfitness(fet-1)&(ran=fitness(fet) newpopulation(ranseti,:)=population(fet,:); end end endendclear ran;newpopulation;%交叉for int=1:2:po
4、psize-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 if cpoint1cpoint2 tem=cpoint1
5、; cpoint1=cpoint2; cpoint2=tem; end cpoint1; cpoint2; for term=cpoint1+1:cpoint2 for ss=1:hromlength if popcross(int,ss)=popfath(term) tem1=popcross(int,ss); popcross(int,ss)=popcross(int,term); popcross(int,term)=tem1; end end clear tem1; end for term=cpoint1+1:cpoint2 for ss=1:hromlength if popcro
6、ss(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 clear tem1; end end clear term;endclear randnum;popcross;%变异操作newpop=popcross;for int=1:popsize randnum=rand; if randnum cpoint12=round(rand*hromlength); cpoint22=round(ran
7、d*hromlength); if (cpoint12=0) cpoint12=1; end if (cpoint22=0) cpoint22=1; end while (cpoint22=cpoint12) cpoint22=round(rand*hromlength); if cpoint22=0; cpoint22=1; end end temp=newpop(int,cpoint12); newpop(int,cpoint12)=newpop(int,cpoint22); newpop(int,cpoint22)=temp; endendnewpop;clear cpoint12;cl
8、ear cpoint22;clear randnum;clear int;for ium=1:popsize vector1=newpop(ium,:); obj1(ium)=hanshu(hromlength,vector1,phen);endclear ium;obj1max=max(obj1);for ar=1:popsize if obj1(ar)=obj1max newpop(ar,:)=opti; endend%遗传操作结束二粒子群算法源程序:%-初始格式化-clear all;clc;format long;%-给定初始化条件-c1=1.4962;%学习因子1c2=1.4962;
9、%学习因子2w=0.7298;%惯性权重MaxDT=100;%最大迭代次数D=2;%搜索空间维数(未知数个数)N=40;%初始化群体个体数目eps=10(-6);%设置精度(在已知最小值时候用)%-初始化种群的个体(可以在这里限定位置和速度的范围)-for i=1:N for j=1:D x(i,j)=randn;%随机初始化位置 v(i,j)=randn;%随机初始化速度 endend%-先计算各个粒子的适应度,并初始化Pi和Pg-for i=1:N p(i)=fitness(x(i,:),D); y(i,:)=x(i,:);endpg=x(1,:);%Pg为全局最优for i=2:N if
10、 fitness(x(i,:),D)FITNESS(pg,D) pg=x(i,:); endend%-进入主要循环,按照公式依次迭代,直到满足精度要求-for t=1:MaxDT t for i=1:N v(i,:)=w*v(i,:)+c1*rand*(y(i,:)-x(i,:)+c2*rand*(pg-x(i,:); x(i,:)=x(i,:)+v(i,:); if fitness(x(i,:),D)p(i) p(i)=fitness(x(i,:),D); y(i,:)=x(i,:); end if p(i)FITNESS(pg,D) pg=y(i,:); end end Pbest(t)=
11、fitness(pg,D);end%-进入主要循环,按照公式依次迭代,直到满足精度要求-for t=1:MaxDT for i=1:N v(i,:)=w*v(i,:)+c1*rand*(y(i,:)-x(i,:)+c2*rand*(pg-x(i,:); x(i,:)=x(i,:)+v(i,:); if fitness(x(i,:),D)p(i) p(i)=fitness(x(i,:),D); y(i,:)=x(i,:); end if p(i)FITNESS(pg,D) pg=y(i,:); end end Pbest(t)=fitness(pg,D);end%-最后给出计算结果disp(*)disp(函数的全局最优位置为:)Solution=pgdisp(最后得到的优化极值为:)Result=fitness(pg,D)disp(*)X,Y=meshgrid(-500:2:500);Z=X.*sin(sqrt(X)+Y.*(sin(sqrt(Y);hold oncontour(X,Y,Z)plot(x(:,1),x(:,2),*);hold off