使用遗传算法解决TSP旅行商问题,下面代码是想用循环交叉方法实现交叉,运行老出现问题。

function [child1]=cross(parent1,parent2,citynum) %parent1、parent1被选择的两个个体 
  popsize=length(parent1);                                                       
child1=zeros(1,popsize);
child1(1)=parent1(1);
n=parent1(1);
flag=1;
while(flag==1)
      m=parent2(n);
          for z=1:popsize
             if child1(z)==m
                 flag=0;
                break;
             end   
          end     
     for i=1:popsize
         if parent1(i)==m
            child1(i)=m;
            n=i;
            break;
         end 
    end
 
end 


k1=0;
k2=0;  
for j=1:popsize
     if child1(j)~=0
         k1=k1+1;
         m1(k1)=j;
     else 
         k2=k2+1;
         m2(k2)=j; 
     end   
end   
b=0;
for j=1:k2[/size][/size][/size][/size][/size][/size][/size]  %for i=1:popsize
      if(find(child1)==parent2(m2(j)))
         b=find((child1)==parent2(m2(j)));
         parent2(m2(j))=parent2(b);
     end 
   end 
end     
for j=1:popsize
    if child1(j)==0
        child1(j)=parent2(j);
    end
end    [/size][/size][/size][/size][/size]