complete.m

来自「用MATLAB编写的遗传算法求解TSP问题的M文件」· M 代码 · 共 43 行

M
43
字号
function [completed]=complete(arraymem,N);
%complete a individual

first=randint(1,1,[1 N]);         
%arraymem=arraymem+1;
temparray=zeros(3,N);                  %the first line is used to set eval/sum, the second line is used to set new,the third is flag
temparray(2,1)=first;
temparray(3,first)=-1;

for p=2:N

sum=0;                               
temp=0;
   for i=1:N
        sum=sum+arraymem(first,i);
   end
   for i=1:N
        temp=temp+arraymem(first,i)/sum;
        temparray(1,i)=temp;
   end                                   %have set the temparray's first line

 flag=1;
   while flag
    kk=rand(1,1);
       if temparray(1,1)>=kk & temparray(3,1)==0
          temparray(2,p)=1;
          temparray(3,1)=-1;
          flag=0;
       end
       for j=2:N
           if temparray(1,j)>=kk & temparray(1,j-1)<kk & temparray(3,j)==0
               temparray(2,p)=j;
               temparray(3,j)=-1;
               flag=0;
           end
       end
   end
first=temparray(2,p);
end

completed=temparray(2,:);
%completed=mygaeval1(completed,N);

⌨️ 快捷键说明

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