您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > 粒子群算法和蚁群算法优化路径问题MATLAB程序源代码
粒子群算法和蚁群算法优化路径问题程序源代码---以旅行商问题(TSP)为例代码用MATLAB编写,两种算法分开编写,读者可以混合编写!一、%%该文件演示基于TSP-PSO(粒子群)算法clc;clear%%下载数据data=load('pr76.txt');cityCoor=[data(:,2)data(:,3)];%城市坐标矩阵figureplot(cityCoor(:,1),cityCoor(:,2),'ms','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g')legend('城市位置')title('城市分布图','fontsize',12)xlabel('km','fontsize',12)ylabel('km','fontsize',12)%ylim([min(cityCoor(:,2))-1max(cityCoor(:,2))+1])gridon%%计算城市间距离n=size(cityCoor,1);%城市数目cityDist=zeros(n,n);%城市距离矩阵fori=1:nforj=1:nifi~=jcityDist(i,j)=((cityCoor(i,1)-cityCoor(j,1))^2+...(cityCoor(i,2)-cityCoor(j,2))^2)^0.5;endcityDist(j,i)=cityDist(i,j);endendnMax=200;%进化次数indiNumber=1000;%个体数目individual=zeros(indiNumber,n);%^初始化粒子位置fori=1:indiNumberindividual(i,:)=randperm(n);end%%计算种群适应度indiFit=fitness(individual,cityCoor,cityDist);[value,index]=min(indiFit);tourPbest=individual;%当前个体最优tourGbest=individual(index,:);%当前全局最优recordPbest=inf*ones(1,indiNumber);%个体最优记录recordGbest=indiFit(index);%群体最优记录xnew1=individual;%%循环寻找最优路径L_best=zeros(1,nMax);forN=1:nMaxN%计算适应度值indiFit=fitness(individual,cityCoor,cityDist);%更新当前最优和历史最优fori=1:indiNumberifindiFit(i)recordPbest(i)recordPbest(i)=indiFit(i);tourPbest(i,:)=individual(i,:);endifindiFit(i)recordGbestrecordGbest=indiFit(i);tourGbest=individual(i,:);endend[value,index]=min(recordPbest);recordGbest(N)=recordPbest(index);%%交叉操作fori=1:indiNumber%与个体最优进行交叉c1=unidrnd(n-1);%产生交叉位c2=unidrnd(n-1);%产生交叉位whilec1==c2c1=round(rand*(n-2))+1;c2=round(rand*(n-2))+1;endchb1=min(c1,c2);chb2=max(c1,c2);cros=tourPbest(i,chb1:chb2);ncros=size(cros,2);%删除与交叉区域相同元素forj=1:ncrosfork=1:nifxnew1(i,k)==cros(j)xnew1(i,k)=0;fort=1:n-ktemp=xnew1(i,k+t-1);xnew1(i,k+t-1)=xnew1(i,k+t);xnew1(i,k+t)=temp;endendendend%插入交叉区域xnew1(i,n-ncros+1:n)=cros;%新路径长度变短则接受dist=0;forj=1:n-1dist=dist+cityDist(xnew1(i,j),xnew1(i,j+1));enddist=dist+cityDist(xnew1(i,1),xnew1(i,n));ifindiFit(i)distindividual(i,:)=xnew1(i,:);end%与全体最优进行交叉c1=round(rand*(n-2))+1;%产生交叉位c2=round(rand*(n-2))+1;%产生交叉位whilec1==c2c1=round(rand*(n-2))+1;c2=round(rand*(n-2))+1;endchb1=min(c1,c2);chb2=max(c1,c2);cros=tourGbest(chb1:chb2);ncros=size(cros,2);%删除与交叉区域相同元素forj=1:ncrosfork=1:nifxnew1(i,k)==cros(j)xnew1(i,k)=0;fort=1:n-ktemp=xnew1(i,k+t-1);xnew1(i,k+t-1)=xnew1(i,k+t);xnew1(i,k+t)=temp;endendendend%插入交叉区域xnew1(i,n-ncros+1:n)=cros;%新路径长度变短则接受dist=0;forj=1:n-1dist=dist+cityDist(xnew1(i,j),xnew1(i,j+1));enddist=dist+cityDist(xnew1(i,1),xnew1(i,n));ifindiFit(i)distindividual(i,:)=xnew1(i,:);end%%变异操作c1=round(rand*(n-1))+1;%产生变异位c2=round(rand*(n-1))+1;%产生变异位whilec1==c2c1=round(rand*(n-2))+1;c2=round(rand*(n-2))+1;endtemp=xnew1(i,c1);xnew1(i,c1)=xnew1(i,c2);xnew1(i,c2)=temp;%新路径长度变短则接受dist=0;forj=1:n-1dist=dist+cityDist(xnew1(i,j),xnew1(i,j+1));enddist=dist+cityDist(xnew1(i,1),xnew1(i,n));ifindiFit(i)distindividual(i,:)=xnew1(i,:);endend[value,index]=min(indiFit);L_best(N)=indiFit(index);tourGbest=individual(index,:);end%%结果作图figureplot(L_best)title('算法训练过程')xlabel('迭代次数')ylabel('适应度值')gridonfigureholdonplot([cityCoor(tourGbest(1),1),cityCoor(tourGbest(n),1)],[cityCoor(tourGbest(1),2),...cityCoor(tourGbest(n),2)],'ms-','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g')holdonfori=2:nplot([cityCoor(tourGbest(i-1),1),cityCoor(tourGbest(i),1)],[cityCoor(tourGbest(i-1),2),...cityCoor(tourGbest(i),2)],'ms-','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g')holdonendlegend('规划路径')scatter(cityCoor(:,1),cityCoor(:,2));title('规划路径','fontsize',10)xlabel('km','fontsize',10)ylabel('km','fontsize',10)gridonylim([480])functiondist=dist(x,D)n=size(x,2);dist=0;fori=1:n-1dist=dist+D(x(i),x(i+1));enddist=dist+D(x(1),x(n));functionindiFit=fitness(x,cityCoor,cityDist)%%该函数用于计算个体适应度值%xinput个体%cityCoorinput城市坐标%cityDistinput城市距离%indiFitoutput个体适应度值m=size(x,1);n=size(cityCoor,1);indiFit=zeros(m,1);fori=1:mforj=1:n-1indiFit(i)=indiFit(i)+cityDist(x(i,j),x(i,j+1));endindiFit(i)=indiFit(i)+cityDist(x(i,1),x(i,n));end------------------------------------------------------二、%%蚁群算法的优化计算——旅行商问题(TSP)优化%%清空环境变量clearallclc%%导入数据loadcitys_data.mat%%计算城市间相互距离n=size(citys,1);D=zeros(n,n);fori=1:nforj=1:nifi~=jD(i,j)=sqrt(sum((citys(i,:)-citys(j,:)).^2));elseD(i,j)=1e-4;endendend%%初始化参数m=50;%蚂蚁数量alpha=1;%信息素重要程度因子beta=5;%启发函数重要程度因子rho=0.1;%信息素挥发因子Q=1;%常系数Eta=1./D;%启发函数Tau=ones(n,n);%信息素矩阵Table=zeros(m,n);%路径记录表iter=1;%迭代次数初值iter_max=200;%最大迭代次数Route_best=zeros(iter_max,n);%各代最佳路径Length_best=zeros(iter_max,1);%各代最佳路径的长度Length_ave=zeros(iter_max,1);%各代路径的平均长度%%迭代寻找最佳路径whileiter=iter_max%随机产生各个蚂蚁的起点城市start=zeros(m,1);fori=1:mtemp=randperm(n);start(i)=temp(1);endTable(:,1)=start;%构建解空间citys_index=1:n;%逐个蚂蚁路径选择fori=1:m%逐个城市路径选择forj=2:ntabu=Table(i,1:(j-1));%已访问的城市集合(禁忌表)allow_index=~ismember(citys_index,tabu);allow=citys_index(allow_index);%待访问的城市集合P=allow;%计算城市间转移概率fork=1:length(allow)P(k)=Tau(tabu(end),allow(k))^alpha*Eta(tabu(end),allow(k))^beta;endP=P/sum(P);%轮盘赌法选择下一个访问城市Pc=cumsum(P);target_index=find(Pc=rand);target=allow(target_index(1));Table(i,j)=target;endend%计算各个蚂蚁的路径距离Length=zeros(m,1);fori=1:m
本文标题:粒子群算法和蚁群算法优化路径问题MATLAB程序源代码
链接地址:https://www.777doc.com/doc-6713270 .html