jcross417.m
来自「免疫遗传算法用于搜索全局最优解,经验证具有很好的效率和收敛性」· M 代码 · 共 67 行
M
67 行
% 交叉操作-见陈国良的书
function [A,B] = jcross417(oldpop,mate1,mate2,loccross,d)
popsize = length(oldpop);
lchrom = length(oldpop(1).chrom);
newpop1(1:lchrom-loccross+1) = oldpop(mate2).chrom(loccross:lchrom);
newpop2(1:lchrom-loccross+1) = oldpop(mate1).chrom(loccross:lchrom);
for i = 1:lchrom
if oldpop(mate1).chrom(i) == oldpop(mate2).chrom(loccross)
Tempoldpop1 = arrayshift(oldpop(mate1).chrom,i);
break
end
end
for i = 1:lchrom
if oldpop(mate2).chrom(i) == oldpop(mate1).chrom(loccross)
Tempoldpop2 = arrayshift(oldpop(mate2).chrom,i);
break
end
end
for i = 1:lchrom
already1 = 0;
already2 = 0;
% 查找存在的重复元素
for j = 1:lchrom-loccross+1
% 若找到 already1 置1
if Tempoldpop1(i) == newpop1(j)
already1 = 1;
break
end
end
for j = 1:lchrom-loccross+1
if Tempoldpop2(i) == newpop2(j)
already2 = 1;
break
end
end
% 若没有找到,置入个体
if already1 == 0;
newpop1(length(newpop1)+1) = Tempoldpop1(i);
end
if already2 == 0;
newpop2(length(newpop2)+1) = Tempoldpop2(i);
end
end
Afit = objfunc(newpop1,d);
Bfit = objfunc(newpop2,d);
% 在子代中取出适应度大的一个
if Afit > Bfit
A = newpop1;
else
A = newpop2;
end
% 在父代中取出适应度大的一个
if oldpop(mate1).fitness > oldpop(mate2).fitness
B = oldpop(mate1).chrom;
else
B = oldpop(mate2).chrom;
end
if sum(A) ~= sum(oldpop(mate1).chrom) | sum(B) ~= sum(oldpop(mate1).chrom)
A = oldpop(mate1).chrom;
B = oldpop(mate2).chrom;
end
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?