function [childpath1,childpath2]=EXCrossover(parentpath1,parentpath2,Pc);
%边重组交叉,EX
tempP=rand(1);
if(tempP<Pc)
col=length(parentpath1);
adjtable=zeros(col,4);
for i=1:col
[rowindex,colindex]=find(i==parentpath1);
if(colindex==1)
adjtable(i,1)=parentpath1(col);
adjtable(i,2)=parentpath1(colindex+1);
elseif(colindex==col);
adjtable(i,1)=parentpath1(colindex-1);
adjtable(i,2)=parentpath1(1);
else
adjtable(i,1)=parentpath1(colindex-1);
adjtable(i,2)=parentpath1(colindex+1);
end
[rowindex,colindex]=find(i==parentpath2);
if(colindex==1)
if(not(find(parentpath2(col)==adjtable(i,:))))
adjtable(i,3)=parentpath2(col);
end
if(not(find(parentpath2(colindex+1)==adjtable(i,:))))
adjtable(i,4)=parentpath2(colindex+1);
end
elseif(colindex==col)
if(not(find(parentpath2(colindex-1)==adjtable(i,:))))
adjtable(i,3)=parentpath2(colindex-1);
end
if(not(find(parentpath2(1)==adjtable(i,:))))
adjtable(i,4)=parentpath2(1);
end
else
if(not(find(parentpath2(colindex-1)==adjtable(i,:))))
adjtable(i,3)=parentpath2(colindex-1);
end
if(not(find(parentpath2(colindex+1)==adjtable(i,:))))
adjtable(i,4)=parentpath2(colindex1);
end
end
end
adjtable1=adjtable;
childpath1(1)=parentpath1(1);
[rowindex,colindex]=find(parentpath1(1)==adjtable);
for j=1:length(rowindex)
adjtable(rowindex(j),colindex(j))=0;
end
randvector=randperm(4);
point=randvector(1);
while(adjtable(parentpath1(1),point)==0)
randvector=randperm(4);
point=randvector(1);
end
temp=adjtable(parentpath1(1),point);
for i=2:col-1
childpath1(i)=temp;
[rowindex,colindex]=find(temp==adjtable);
for j=1:length(rowindex)
adjtable(rowindex(j),colindex(j))=0;
end
randvector=randperm(4);
point=randvector(1);
while(adjtable(temp,point)==0)
randvector=randperm(4);
point=randvector(1);
end
temp=adjtable(temp,point);
end
childpath1(col)=temp;
childpath2(1)=parentpath2(1);
[rowindex,colindex]=find(parentpath2(1)==adjtable1);
for j=1:length(rowindex)
adjtable1(rowindex(j),colindex(j))=0;
end
randvector=randperm(4);
point=randvector(1);
while(adjtable1(parentpath2(1),point)==0)
randvector=randperm(4);
point=randvector(1);
end
temp=adjtable1(parentpath2(1),point);
for i=2:col-1
childpath2(i)=temp;
[rowindex,colindex]=find(temp==adjtable1);
for j=1:length(rowindex)
adjtable1(rowindex(j),colindex(j))=0;
end
randvector=randperm(4);
point=randvector(1);
while(adjtable1(temp,point)==0)
randvector=randperm(4);
point=randvector(1);
end
temp=adjtable1(temp,point);
end
childpath2(col)=temp;
else
childpath1=parentpath1;
childpath2=parentpath2;
end
评论1
最新资源