?? cross.asv
字號:
function [child1,child2]=cross(parent1,parent2,pc)
%進行兩點交叉,pc為交叉概率,部分映射交叉
if(rand(1)<pc)
vector1=randperm(length(parent1)-1);
position1=min(vector1(1),vector1(2));%確定兩點交叉的位置
position2=max(vector1(1),vector1(2));
child1=parent1;
child2=parent2;
for i=1:(position2-position1)%交換中間部分
child1(position1+i)=parent2(position1+i);
child2(position1+i)=parent1(position1+i);
end
for j=1:position1%調整兩子代position1之前部分
[var1,index1]=find(child1(j)==child1((position1+1):position2));
while(var1)
child1(j)=child2(index1+position1);
[var1,index1]=find(child1(j)==child1((position1+1):position2));
end
[var2,index2]=find(child2(j)==child2((position1+1):position2));
while(var2)
child2(j)=child1(index2+position1);
[var2,index2]=find(child2(j)==child2((position1+1):position2));
end
end
for j=(position2+1):length(parent1)%調整兩子代position2之后部分
[var1,index1]=find(child1(j)==child1((position1+1):position2));
while(var1)
child1(j)=child2(index1+position1);
[var1,index1]=find(child1(j)==child1((position1+1):position2));
end
[var2,index2]=find(child2(j)==child2((position1+1):position2));
while(var2)
child2(j)=child1(index2+position1);
[var2,index2]=find(child2(j)==child2((position1+1):position2));
end
end
else %隨機數大于交叉概率,不進行交叉操作
child1=parent1;
child2=parent2;
end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -