|
连续Hopfield网络解决TSP(这个是matlab程序)1.%连续Hopfield网络解决TSP2.function HopfieldTsp()3.clc;4.N=10; %城市数5.A=1.5; %系数A6.D=1; %系数D7.u0=0.02; %神经元函数斜率8.Step_t=0.1; %计算步长9.MaxEpochs=20000;%迭代次数10.%得到城市间距离矩阵11.CityCood=rand(2,N); %城市坐标12.DistanceMat=dist(CityCood&#39;,CityCood); %城市间距离矩阵13.U=0.2*rand(N,N)-0.1;%神经元输入初始值在0附近产生14.for Count=1:MaxEpochs15. V=(1+tansig(U/u0))/2;16. E=CacuEnergy(V,DistanceMat,A,D);%计算能量17. delta_U=CacuDeltaU(V,DistanceMat,A,D,Step_t);%计算U的增量18. U=U+delta_U*Step_t;19.end20.[NewV,CheckRes]=RouteCheck(V);%检查V是否是有效路径21.if(CheckRes<1)22. FinalE=CacuEnergy(NewV,DistanceMat,A,D);23. RouteLen=TotalRouteLength(NewV,CityCood);%计算路径的真实长度24. PlotRoute(NewV,CityCood);%绘制路径25.else26. disp(&#39;路径无效!!&#39;);27.end28.%能量计算29.function E=CacuEnergy(V,d,A,D)30.[n,n]=size(V);31.t1=sumsqr(sum(V,2)-1);32.t2=sumsqr(sum(V,1)-1);33.PermitV=V(:,2:n);34.PermitV=[PermitV V(:,1)];35.temp=d*PermitV;36.t3=sum(sum(V.*temp));37.E=0.5*(A*t1+A*t2+D+t3);38.%计算U的增量39.function d_U=CacuDeltaU(V,d,A,D,dt)40.[n,n]=size(V);41.t1=repmat(sum(V,2)-1,1,n);42.t2=repmat(sum(V,1)-1,n,1);43.PermitV=V(:,2:n);44.PermitV=[PermitV V(:,1)];45.t3=d*PermitV;46.d_U=-dt*(A*t1+A*t2+D*t3);47.%检查V是否是有效路径48.function [NewV,CheckRes]=RouteCheck(V)49.[rows,columns]=size(V);50.NewV=zeros(rows,columns);51.[XC,Order]=max(V);52.for j=1:columns53. NewV(Order(j),j)=1;54.end55.SC=sum(NewV);56.SR=sum(NewV&#39;);57.CheckRes=sumsqr(SC-SR);58.%绘制路径59.function PlotRoute(V,CityCood)60.figure;61.title(&#39;连续Hopfield网络解决TSP&#39;);62.xlabel(&#39;X坐标&#39;);63.ylabel(&#39;Y坐标&#39;);64.axis([0,1,0,1]);65.axis on;66.[xxx,order]=max(V);67.NewCood=CityCood(:,order);68.NewCood=[NewCood NewCood(:,1)];69.plot(NewCood(1,,NewCood(2,,&#39;o-&#39;);70.%计算路径实际长度71.functionLen=TotalRouteLength(V,CityCood)72.[xxx,order]=max(V);73.NewCood=CityCood(:,order);74.NewCood=[NewCood NewCood(:,1)];75.[rows,columns]=size(NewCood);76.Len=0;77.for i=2:columns78. Len=Len+dist(NewCood(:,i-1)&#39;,NewCood(:,i));end |
|
共 1 个关于本帖的回复 最后回复于 2013-8-16 09:35