📄 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 + -