[人工智能] 用C或C++,编写用Hopfield网络求解TSP问题?
我们下星期二要交一份计算智能作业啊!但是我的编程技术不行啊,有没有那位大哥可以帮我完成它啊?我会十分感激你的!用C或C++,编写用Hopfield网络求解TSP问题(旅行商问题)的程序(城市数目≥5)。希望能够有人帮到我啦! 连续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.=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.=size(V);31.t1=sumsqr(sum(V,2)-1);32.t2=sumsqr(sum(V,1)-1);33.PermitV=V(:,2:n);34.PermitV=;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.=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=;45.t3=d*PermitV;46.d_U=-dt*(A*t1+A*t2+D*t3);47.%检查V是否是有效路径48.function =RouteCheck(V)49.=size(V);50.NewV=zeros(rows,columns);51.=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();65.axis on;66.=max(V);67.NewCood=CityCood(:,order);68.NewCood=;69.plot(NewCood(1,:),NewCood(2,:),&#39;o-&#39;);70.%计算路径实际长度71.functionLen=TotalRouteLength(V,CityCood)72.=max(V);73.NewCood=CityCood(:,order);74.NewCood=;75.=size(NewCood);76.Len=0;77.for i=2:columns78. Len=Len+dist(NewCood(:,i-1)&#39;,NewCood(:,i));end
页:
[1]