请写出下面代码的伪代码function [crosspop,crossfit] = mycrossover(pop,fit,caldata,pc0) %自适应的参数设置 a = 0.6; b = 0.2; fmean = mean(fit); fmax = max(fit); fmin = min(fit); alpha = fmean/fmax; beta = fmin/fmax; if alpha>a && beta>b pc = pc0*(1/beta); else pc = pc0; end %下面开始选择交叉的染色体 n = length(fit); poplength = length(pop(1,:)); temp = rand(n,1); choose = zeros(n,1); choose(temp<pc)=1; choose = find(choose==1); crossnum = length(choose); if mod(crossnum,2) %如果总数是奇数,就减去一个,保证两两交叉 choose(end)=[]; crossnum = crossnum-1; end crosspop = zeros(crossnum,poplength); crossfit = zeros(crossnum,1); for i = 1:2:crossnum h1 = [randi([1 poplength])]; h2 = [randi([1 poplength])]; k1 = min([h1,h2]); %选择交叉的位置 k2 = max([h1,h2]); x1 = pop(choose(i),:); x2 = pop(choose(i+1),:); y1 = x1(k1:k2); y2 = x2(k1:k2); x1(k1:k2) = y2; %x2的后一节y2放在x1后面 x2(k1:k2) = y1; %x1的后一节y1放在x2后面 以此方式表示交叉 %把交叉后的染色体和适应度分别保存 x1 = fixpop(x1,k1,k2); x2 = fixpop(x2,k1,k2); crosspop(i,:) = x1; crosspop(i+1,:) = x2; crossfit(i,:)=fitness(crosspop(i,:),caldata); crossfit(i+1,:)=fitness(crosspop(i+1,:),caldata); end
时间: 2023-12-08 16:04:17 浏览: 128
伪代码如下:
输入:种群 pop,适应度 fit,计算数据 caldata,交叉概率 pc0。
输出:交叉后的染色体 crosspop,交叉后的适应度 crossfit。
1. 自适应参数设置:
a = 0.6;
b = 0.2;
fmean = mean(fit);
fmax = max(fit);
fmin = min(fit);
alpha = fmean/fmax;
beta = fmin/fmax;
if alpha>a && beta>b
pc = pc0*(1/beta);
else
pc = pc0;
end
2. 选择交叉的染色体:
n = length(fit);
poplength = length(pop(1,:));
temp = rand(n,1);
choose = zeros(n,1);
choose(temp<pc)=1;
choose = find(choose==1);
crossnum = length(choose);
if mod(crossnum,2) %如果总数是奇数,就减去一个,保证两两交叉
choose(end)=[];
crossnum = crossnum-1;
end
3. 对选择的染色体进行交叉操作:
crosspop = zeros(crossnum,poplength);
crossfit = zeros(crossnum,1);
for i = 1:2:crossnum
h1 = [randi([1 poplength])];
h2 = [randi([1 poplength])];
k1 = min([h1,h2]); %选择交叉的位置
k2 = max([h1,h2]);
x1 = pop(choose(i),:);
x2 = pop(choose(i+1),:);
y1 = x1(k1:k2);
y2 = x2(k1:k2);
x1(k1:k2) = y2;
x2(k1:k2) = y1;
x1 = fixpop(x1,k1,k2); %修复交叉后的染色体
x2 = fixpop(x2,k1,k2);
crosspop(i,:) = x1;
crosspop(i+1,:) = x2;
crossfit(i,:)=fitness(crosspop(i,:),caldata); %计算交叉后染色体的适应度
crossfit(i+1,:)=fitness(crosspop(i+1,:),caldata);
end
4. 返回交叉后的染色体和适应度 crosspop 和 crossfit。
阅读全文