《基于改进遗传算法的路径规划matlab实现.doc》由会员分享,可在线阅读,更多相关《基于改进遗传算法的路径规划matlab实现.doc(6页珍藏版)》请在金锄头文库上搜索。
1、基于遗传算法的路径规划MATLAB实现主程序:clear all;close all;t=23; %过程点个数=t1s=500; %种群规模pc=0.90; %交叉概率pm=0.20; %变异概率pop=zeros(s,t);for i=1:s pop(i,1:t-1)=randperm(t-1);endfor k=1:1:2000 %进化代次数kif mod(k,10)=1 kend pop=lujingdis(pop);c=15;%选择淘汰个数 pop=lujingselect(pop,c);p=rand;if p=pc pop=lujingcross(pop);endif p=pm po
2、p=lujingmutate(pop);Endendpopmin(pop(:,t)J=pop(:,t);fi=1./J;Oderfi,Indexfi=sort(fi); %安排fi从小到大BestS=pop(Indexfi(s),:); %使BestS=E(m),m即是属于max(fi)的IndexfiI=BestS;x=2 3 6 10 14 17 22 20 23 25 30 28 25 21 29 16 18 15 9 11 6 5 ; y=5 26 14 29 27 24 28 22 26 30 30 17 13 15 4 13 3 1 6 2 2 7;%过程点坐标% x=1 2 3
3、4 6 9 11 10 8 9 6 4; %12个过程点的坐标% y=1 2 3 4 8 10 11 9 5 2 1 2;for i=1:1:t-1x1(i)=x(I(i);y1(i)=y(I(i);endx(t)=x(I(1);y(t)=y(I(1);a = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1
4、 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1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 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
9、 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1;%31*31栅格%a = 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 0 0 1 0 1 0 0 1 1 1 1 1 0 0 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1
10、1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 0 1 1;%11*11栅格b = a;b(end+1,end+1) = 0;colormap(0 0 0;1 1 1),pcolor(b)axis image xy;%绘制栅格图hold on;figure(1);plot(x,y,-or);适应度函数程序:lujingdis.mfunction pop=qiujuli(pop)s,t=size(pop);for i=1:1:s dd=0; for j=1:1:t-2 dd=dd+lujingcalculate(pop(i,j),pop(i,j+1); end pop
11、(i,t)=dd;End距离计算程序:lujingcalculate.mfunction d=juli(m,n)x=2 3 6 10 14 17 22 20 23 25 30 28 25 21 29 16 18 15 9 11 6 5 ;y=5 26 14 29 27 24 28 22 26 30 30 17 13 15 4 13 3 1 6 2 2 7;%x=1 2 3 4 6 9 11 10 8 9 6 4; % y=1 2 3 4 8 10 11 9 5 2 1 2;d=sqrt(x(m)-x(n)2+(y(m)-y(n)2);选择算子程序:lujingselect.mfunction
12、pop=select(pop,k)s,t=size(pop);m11=(pop(:,t);m11=m11;mmax=zeros(1,k);mmin=zeros(1,k);num=1;while numk+1 a,mmax(num)=max(m11); m11(mmax(num)=a; num=num+1;endnum=1;while numk+1 b,mmin(num)=min(m11); m11(mmin(num)=a; num=num+1;endfor i=1:k pop(mmax(i),:)=pop(mmin(i),:);End交叉算子程序:lujingcross.mfunction p
13、op=cross(pop)s,t=size(pop);pop1=pop;for i=1:2:s m=randperm(t-3)+1; crosspoint(1)=min(m(1),m(2); crosspoint(2)=max(m(1),m(2); for j=1:crosspoint(1) while find(pop(i,crosspoint(1)+1:crosspoint(2)=pop(i,j) zhi=find(pop(i,crosspoint(1)+1:crosspoint(2)=pop(i,j); y=pop(i+1,crosspoint(1)+zhi); pop(i,j)=y;
14、end end for j=crosspoint(2)+1:t-1 while find(pop(i,crosspoint(1)+1:crosspoint(2)=pop(i,j) zhi=find(pop(i,crosspoint(1)+1:crosspoint(2)=pop(i,j); y=pop(i+1,crosspoint(1)+zhi); pop(i,j)=y; end endendpop=lujingdis(pop);for i=1:s if pop1(i,t)pop(i,t) pop(i,:)=pop1(i,:); endEnd变异算子程序:lujingmutate.mfunction pop =