check.m

来自「Hopfield网络解决旅行商问题(TSP)」· M 代码 · 共 52 行

M
52 行
字号
% function [path,n_opti,n_hypo,n_ave,dis_ave,dis]=Check(ck,v,d,cycle,n)   
function [path,n_fre,n_ill,betterpath,sumdis,summin]=Check(tk,ck,v,d,cycle,n,betterpath,summin) 
n_ill=0;n_fre=0;sumdis=0;
vi=zeros(1,n);vj=zeros(1,n);path=ones(1,n+1);ispath=ones(1,n);
       for jk=1:n
           for k=1:n
               if (jk~=k)%&(ispath(k)~=0)
                    if ispath(k)~=0
                        if v(k,jk)>=vj(jk)
                             vj(jk)=v(k,jk);
                                path(jk)=k;
                        end
                    end    
                end
           end  
           ispath(path(jk))=0;
       end
       for k=1:n%检查是否有遗漏的点
           if(ispath(k)==1)%路径的第jk个点没有赋值
               for jk=1:n%查哪个点还没有经过
                   if path(jk)==1
                       path(jk)=k;
                       ispath(k)=0;
                       break;
                   end
               end
           end
       end
         
           for k=1:n
               if vi(k)<0.8
                   if ck>cycle
                       n_fre=n_fre+1;
                   else
                       n_ill=n_ill+1;
                   end
               end
           end
           path(n+1)=path(1);
 for k=1:n
sumdis=sumdis+d(path(k),path(k+1));
end
if tk==1
    summin=sumdis;
    betterpath=path;
end
if sumdis<summin
    summin=sumdis;
    betterpath=path;
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?