基于实数编码的参数⾃适应遗传算法(matlab代码)
实数编码的遗传算法寻优:
遗传算法的基本操作算⼦:
(1)选择算⼦
选择算⼦的作⽤主要是避免优良基因的丢失,使得性能⾼的个体能以更⼤的概率被选中,有机会作为⽗代繁殖下⼀代,从⽽提⾼遗传算法的全局收敛性及计算效率。常见的选择算⼦包括赌选择法、随机遍历抽样法、局部选择法及锦标赛选择法等。选择算⼦采⽤赌;
(2)交叉算⼦
在遗传算法中,交叉算⼦是区别于其它优化算法的本质特征,⽤于组合新的个体在解空间中快速有效地进⾏搜索,同时也降低了对有效模式的破坏程度,起到全局搜索寻优的效果。交叉算⼦直接影响着遗传算法的最终搜索效果,⼀定程度上决定了其发展前景。
其中alpha为参数,0<alpha<1
(3)变异算⼦
体基因的多样性是保证遗传算法寻到全局最优解的前提条件,然⽽在进化过程中,遗传选择操作削弱了体的多样性,上述交叉算⼦只有满⾜⼀定的条件才能保持体的多样性,⽽变异操作则是保持体多样性的有效算⼦,所以变异操作算⼦的选取也是必不可少的。变异尺度⾃适应变化的变异算⼦在进化初期采⽤较⼤的变异尺度来保持体的多样性,⽽在后期变异尺度将逐渐缩⼩以提⾼局部微调能⼒。本⽂在此基础上做些改进,改进后的变异算⼦具有原有算⼦的优点,且操作上⽐原有算⼦简单⽅便,有效地加快遗传算法的收敛速度,具体如下:
可以看出s(t) 决定了变异空间的⼤⼩,在迭代的初期,变异空间较⼤,在迭代的后期,变异空间缩⼩,算法的局部寻优能⼒变强。
变异算⼦参考⽂献:  [1] 管⼩艳. 实数编码下遗传算法的改进及其应⽤[D].重庆⼤学,2012.
参数⾃适应:
交叉概率Pc和变异概率Pm是遗传算法的两个重要的参数,这两个参数决定了每个个体进⾏交叉或者变异操作的概率。
⾃适应算⼦参考⽂献:
[2] M. Srinivas and L. M. Patnaik, "Adaptive probabilities of crossover and mutation in genetic algorithms," in IEEE Transactions on Systems, Man, and Cybernetics, vol. 24, no. 4, pp. 656-667, April 1994.doi: 10.1109/21.286385
上述部分翻译⾃⽂献[2]
按照论⽂描述,对算法的复现如下:
% 测试函数图像
% 测试函数图像
% 改进的⾃适应遗传算法:
% 参考⽂献:[7] M. Srinivas and L. M. Patnaik, "Adaptive probabilities of crossover and mutation in genetic algorithms,"
%              in IEEE Transactions on Systems, Man, and Cybernetics, vol. 24, no. 4, pp. 656-667, April 1994.
%              doi: 10.1109/21.286385
clc;
clear all;
mode = 'Schaffer';
% mode = 'self_define';
if strcmp(mode, 'Schaffer')
figure(1)
x = -4:0.1:4;
y = -4:0.1:4;
[X,Y] = meshgrid(x,y);
% Z = 3*cos(X.*Y)+X+Y.^2;
Z = 0.5-((sin(sqrt(X.^2+Y.^2)).^2)-0.5)./(1+0.001.*(X.^2+Y.^2)).^2;    surf(X,Y,Z);
title('Schaffer Function');
xlabel('X-轴');
ylabel('Y-轴');
zlabel('Z-轴');
figure(2);
contour(X, Y, Z, 8);
title('Schaffer函数等⾼线');
xlabel('X-轴');
ylabel('Y-轴');
end
if strcmp(mode, 'self_define')
figure(1);
x = -4:0.1:4;
y = -4:0.1:4;
[X,Y] = meshgrid(x,y);
% Z = 100.*(Y-X.^2).^2+(1-X).^2;
Z = (cos(X.^2+Y.^2)-0.1)./(1+0.3*(X.^2+Y.^2).^2)+3;
surf(X,Y,Z);
%title('Rosen Brock valley Function');
title('Self define Function');
xlabel('X-轴');
ylabel('Y-轴');
zlabel('Z-轴');
end
clc;
clearvars -except mode;
r = 0.2;
b = 3;
NP=400;
% Pc=0.65;  % 将Pc,Pm参数改进为⾃适应参数
% Pm=0.20;
G=520;    %        记得改
D=2;    % 变量个数
k1 = 1;
k3 = 1;
k2 = 0.5;
k4 = 0.5;
X_min=-4;
X_max=4;
Y_min=-4;
Y_max=4;
% optimization_trace = [];    % 三维数组, ⾏,列,叶
for count_1=1:NP  % 产⽣初始解
temp1 = X_min+rand()*(X_max-X_min);
temp2 = Y_min+rand()*(Y_max-Y_min);
x(count_1,:) = [temp1,temp2];
end
save_pic_cnt = 1;
A = figure(3);
for gen=1:G
%pause(0.2);
if rem(gen, 100)==1
scatter(x(:,1), x(:, 2));
axis([-4, 4, -4, 4]);
title(['第', num2str(gen), '次迭代']);
xlabel('变量X');
ylabel('变量Y');
base_path = 'C:\Users\18811\Desktop\graph\';
cnt = num2str(save_pic_cnt);
tail_path = '.jpg';
frame = getframe(A);
im=frame2im(frame);
path_img = [base_path, cnt, tail_path];
% imwrite(im, path_img);
% save_x(:, :, save_pic_cnt) = x;
save_pic_cnt = save_pic_cnt + 1;matlab等高线标注字体大小
% scatter(0, 0, 'o', 'r');
for count_2=1:NP
fitness(count_2)=func(x(count_2,:), mode);
end
%[fitness_min,index0] = min(fitness);
%fitness_max = max(fitness);
[fitness_max,index0] = max(fitness);
fitness_average = sum(fitness)/(length(fitness));  % 种的平均值
collect_fit_average(gen) = fitness_average;  % 保存适应度的平均值
collect_fitmax_subtract_fit_average(gen) = fitness_max - fitness_average;  % 保存f_max-f_average ;
fitness_min = min(fitness);
best_indiv = x(index0,:);  % 最优的个体
% optimization_trace(gen,: , global_count) = best_indiv;
% best_solution(gen) = fitness_min;
best_solution(gen) = fitness_max;
% 计算归⼀化的适应度值
fitness = (fitness - fitness_min)/(fitness_max - fitness_min);
fitness_sum = sum(fitness);
fitness = fitness./fitness_sum;
fitness = cumsum(fitness);
% 选择算⼦:
ms = sort(rand(NP,1));
fiti = 1;
newi = 1;
while newi<=NP
if ms(newi)<fitness(fiti)
clone_x(newi,:) = x(newi,:);
newi = newi + 1;
else
fiti = fiti + 1;
end
end
clone_x = clone_x(1:NP, :);
% 进⾏交叉,变异操作
% count=0;
for count=1:2:NP
% ⾃适应计算Pc.
% 选区两个交叉的个体的较⼤的适应度值
if fitness(count)>=fitness(count+1)
fitness_selected = fitness(count);
else
fitness_selected = fitness(count+1);
end
% 计算Pc
if fitness_selected >= fitness_average
Pc = k1*(fitness_max-fitness_selected)/(fitness_max-fitness_average);
else
Pc = k3;
end
collect_Pc(gen, count) = Pc;  % 保存Pc的值
temp_cross = rand();
if temp_cross < Pc
% 交叉算⼦注:这种交叉算⼦效果更好
temp_alpha = 0.6;
cross_x(count,:) = temp_alpha*clone_x(count,:)+(1-temp_alpha)*clone_x(count+1,:);
cross_x(count+1,:) = temp_alpha*clone_x(count+1,:)+(1-temp_alpha)*clone_x(count,:);
% 改进的交叉算⼦参考⽂献:管⼩艳. 实数编码下遗传算法的改进及其应⽤[D].重庆⼤学,2012.  注:但这种交叉算⼦实际的效果不理想
% temp_gama = rand();
% temp_alpha = 0.98;
% cross_x(count,:) = temp_alpha*clone_x(count,:)+(1-temp_alpha)*clone_x(count+1,:)+temp_gama*(clone_x(count,:)-clone_x(count+1,:));            % cross_x(count+1,:) = temp_alpha*clone_x(count+1,:)+(1-temp_alpha)*clone_x(count,:)+temp_gama*(clone_x(count,:)-clone_x(count+1,:));        else
cross_x(count,:)=clone_x(count,:);
cross_x(count+1,:)=clone_x(count+1,:);
end
% 边界条件检查
if cross_x(count,1)>X_max || cross_x(count,1)<X_min || cross_x(count,2)>Y_max || cross_x(count,2)<Y_min
temp1 = X_min+rand()*(X_max-X_min);
temp2 = Y_min+rand()*(Y_max-Y_min);
cross_x(count,:) = [temp1,temp2];
end
end
cross_x = cross_x(1:NP,:);
%  cross_x为完成交叉的个体;
% 变异操作
for count=1:1:NP
% 计算Pm
if fitness(count)>=fitness_average
Pm = k2*(fitness_max-fitness(count))/(fitness_max-fitness_average);
else
Pm = k4;
collect_Pm(gen,count) = Pm;    %  保存Pm的值
temp_mutation=rand();
if temp_mutation<Pm
%mutation_x(count,:) = (1+0.01).*cross_x(count,:);      %这种变异算⼦效果不理想
% 变异算⼦参考⽂献:管⼩艳. 实数编码下遗传算法的改进及其应⽤[D].重庆⼤学,2012
mutation_pos = randi(D);
if mutation_pos==1
low = X_min;
high = X_max;
else
low = Y_min;
high = Y_max;
end
s_t(gen) = 1-r^((1-gen/G)^b);
new_low = cross_x(count, mutation_pos)-s_t(gen)*(cross_x(count, mutation_pos)-low);
new_high = cross_x(count, mutation_pos)+s_t(gen)*(high-cross_x(count, mutation_pos));
mutation_x(count, :) = cross_x(count, :);
mutation_x(count, mutation_pos) = new_low+rand()*(new_high-new_low);
if mutation_x(count,1)>X_max || mutation_x(count,1)<X_min || mutation_x(count,2)>Y_max || mutation_x(count,2)<Y_min                temp1 = X_min+rand()*(X_max-X_min);
temp2 = Y_min+rand()*(Y_max-Y_min);
mutation_x(count,:) = [temp1,temp2];
end
else
mutation_x(count,:) = cross_x(count,:);
end
end
%边界条件处理
x=mutation_x(1:NP, :);
x(1,:)= best_indiv;
end
%% 作图
figure(4)
plot(best_solution);
%hold on;
xlabel('进化代数');
ylabel('适应度值');
title('适应度进化曲线');
figure(5);
plot(collect_fitmax_subtract_fit_average);
title('f_{max}-f_{average}曲线');
xlabel('进化代数');
ylabel('f_{max}-f_{average}');
% function f=func(buf)
%    f=0.5-((sin(sqrt(buf(1).^2+buf(2).^2)).^2)-0.5)./(1+0.001.*(buf(1).^2+buf(2).^2)).^2;
% end
function f=func(buf, md)
if strcmp(md, 'Schaffer')
f=0.5-((sin(sqrt(buf(1).^2+buf(2).^2)).^2)-0.5)./(1+0.001.*(buf(1).^2+buf(2).^2)).^2;
end
if strcmp(md,'self_define')
% f = 100*(buf(2)-buf(1).^2).^2+(1-buf(1)).^2;
f = (cos(buf(1).^2+buf(2).^2)-0.1)./(1+0.3*(buf(1).^2+buf(2).^2).^2)+3;
end
end
测试函数:
Schaffer函数:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。