资源描述:
《bp神经元旅行商程序》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、clcglobalADcitys=[0.10.20.40.50.70.80.20.50.70.9;0.60.30.10.50.20.40.80.90.60.8]';distance=dist(citys,citys');%初始化网路N=size(citys,1);A=200;D=100;UO=0.1;step=0.0001;delta=2*rand(N,N)-1;U=UO*log(N-1)+delta;V=(1+tansig(U/UO))/2;iter_num=10000;E=zeros(1,iter_num);%%寻优迭代fork=1:iter_num%动态方程计算dU=d
2、iff_u(V,distance);%输入神经元状态更新U=U+dU*step;%输出神经元状态更新V=(1+tanh(U/UO))/2;%能量函数计算e=energy(V,distance);E(k)=e;end%%判断路径有效性[rows,cols]=size(V);V1=zeros(rows,cols);[V_max,V_ind]=max(V);forj=1:colsV1(V_ind(j),j)=1;endC=sum(V1,1);R=sum(V1,2);flag=isequal(C,ones(1,N))&isequal(R',ones(1,N));%%结果显示iffla
3、g==1%计算初始路径长度sort_rand=randperm(N);citys_rand=citys(sort_rand,:);Length_init=dist(citys_rand(1,:),citys_rand(end,:)');fori=2:size(citys_rand,1)Length_init=Length_init+dist(citys_rand(i-1,:),citys_rand(i,:)');end%绘制初始路径figure(1)plot([citys_rand(:,1);citys_rand(1,1)],[citys_rand(:,2);citys_ra
4、nd(1,2)],'o-')fori=1:length(citys)text(citys(i,1),citys(i,2),[''num2str(i)])endtext(citys_rand(1,1),citys_rand(1,2),['起点'])text(citys_rand(end,1),citys_rand(end,2),['终点'])title(['优化前路径(长度:'num2str(Length_init)')'])axis([0101])gridonxlabel('城市位置横坐标')ylabel('城市位置纵坐标')%计算最优路径长度[V1_max,V1_ind]=
5、max(V1);citys_end=citys(V1_ind,:);Length_end=dist(citys_end(1,:),citys_end(end,:)');fori=2:size(citys_end,1)Length_end=Length_end+dist(citys_end(i-1,:),citys_end(i,:)');enddisp('最优路径矩阵');V1%绘制最优路径figure(2)plot([citys_end(:,1);citys_end(1,1)],...[citys_end(:,2);citys_end(1,2)],'o-')fori=1:le
6、ngth(citys)text(citys(i,1),citys(i,2),[''num2str(i)])endtext(citys_end(1,1),citys_end(1,2),['起点'])text(citys_end(end,1),citys_end(end,2),['终点'])title(['优化后路径(长度:'num2str(Length_end)')'])axis([0101])gridonxlabel('城市位置横坐标')ylabel('城市位置纵坐标')%绘制能量函数变化曲线figure(3)plot(1:iter_num,E);ylim([02000])t
7、itle(['能量函数变化曲线(最优能量:'num2str(E(end))')']);xlabel('迭代次数');ylabel('能量函数');elsedisp('寻优路径无效');end%%%%计算能量函数functionE=energy(V,d)globalADn=size(V,1);sum_x=sumsqr(sum(V,2)-1);sum_i=sumsqr(sum(V,1)-1);V_temp=V(:,2:n);V_temp=[V_tempV(:,1)];sum_d=d*V_temp;sum_d