% ----------------------- README ------------------------------------------
% ------------------- 原创声明 -------------------------------------------
% -------------- 最后一次修改:2023/6/11 ------------------------------------
% ------------------- 欢迎关注₍^.^₎♡ -------------------------------------
% -------------- 项目:改进GA对比标准GA应用于函数寻优 ---------------------
% -------------- B站:KAU的云实验台_s -------------------------------------
% -------------- CSDN:KAU的云实验台 ---------------------------------------
% -------------------------------------------------------------------------
%% 适应度越大越好
%% 清空环境
clc
clear
%% 遗传算法参数
maxgen=200; %进化代数
sizepop=300; %种群规模 务必是偶数 君主方案对半分
pcross=[0.6]; %交叉概率
pmutation=[0.01]; %变异概率
lenchrom=[1 1 ]; %变量字串长度
bound=[-3,4.1;12.1,5.8]'; %变量范围
times = 4;
% 记录每一代进化中最好的适应度和平均适应度 标准遗传算法
trace=zeros(times,maxgen);
% 记录每一代进化中最好的适应度和平均适应度 改进遗传算法
m_trace=zeros(times,maxgen);
%% 运算N次 防随机
for q=1:times
%% 个体初始化
% 标准遗传算法
individuals=struct('fitness',zeros(1,sizepop), 'chrom',[]); %种群结构体
avgfitness=[]; %种群平均适应度
bestfitness=[]; %种群最佳适应度
bestchrom=[]; %适应度最好染色体
% 改进的遗传算法
m_individuals=struct('fitness',zeros(1,sizepop), 'chrom',[]); %种群结构体
m_avgfitness=[]; %种群平均适应度
m_bestfitness=[]; %种群最佳适应度
m_bestchrom=[]; %适应度最好染色体
% 初始化种群 标准遗传算法
for i=1:sizepop
individuals.chrom(i,:)=Code(lenchrom,bound); %随机产生个体
x=individuals.chrom(i,:);
individuals.fitness(i)=fun(x); %个体适应度
end
% 初始化种群 改进遗传算法
for i=1:sizepop
m_individuals.chrom(i,:)=Code(lenchrom,bound); %随机产生个体
x = m_individuals.chrom(i,:);
m_individuals.fitness(i)=fun(x); %个体适应度
end
%找最好的染色体 标准遗传算法
[bestfitness bestindex]=max(individuals.fitness);
bestchrom=individuals.chrom(bestindex,:); %最好的染色体
avgfitness=sum(individuals.fitness)/sizepop; %染色体的平均适应度
%找最好的染色体 改进遗传算法
[m_bestfitness m_bestindex]=max(m_individuals.fitness);
m_bestchrom=m_individuals.chrom(m_bestindex,:); %最好的染色体
m_avgfitness=sum(m_individuals.fitness)/sizepop; %染色体的平均适应度
[SortFIT,Index]=sort(m_individuals.fitness,'descend'); % 越大越好 SortFIT 是按从大到小排序
Sortf=m_individuals.chrom(Index,:); % 按顺序排号
NU_fit = zeros(1,sizepop); % 存储每代交叉变异后的适应度
%% 进化开始 标准与改进一起运算 提高速度
for i=1:maxgen
% 选择操作 标准遗传算法
individuals=Select(individuals,sizepop);
avgfitness=sum(individuals.fitness)/sizepop;
% 交叉操作 标准遗传算法
individuals.chrom=Cross(pcross,lenchrom,individuals.chrom,sizepop,bound);
% 变异操作 标准遗传算法
individuals.chrom=Mutation(pmutation,lenchrom,individuals.chrom,sizepop,[i maxgen],bound);
% 标准遗传算法
% 计算适应度
for j=1:sizepop
x=individuals.chrom(j,:);
individuals.fitness(j)=fun(x);
end
%找到最小和最大适应度的染色体及它们在种群中的位置 标准遗传算法
[newbestfitness,newbestindex]=max(individuals.fitness);
[worestfitness,worestindex]=min(individuals.fitness);
% 代替上一次进化中最好的染色体 标准遗传算法
if bestfitness<newbestfitness
bestfitness=newbestfitness;
bestchrom=individuals.chrom(newbestindex,:);
end
individuals.chrom(worestindex,:)=bestchrom;
individuals.fitness(worestindex)=bestfitness;
avgfitness=sum(individuals.fitness)/sizepop;
% trace=[trace;avgfitness bestfitness]; %记录每一代进化中最好的适应度和平均适应度
trace(q,i) = bestfitness;
% 改进遗传算法 更新最优
[SortFIT,Index]=sort(m_individuals.fitness,'descend'); % 越小越好 SortFIT 是按从小到大排序
Sortf=m_individuals.chrom(Index,:); % 按顺序排号
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 改进部分 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 改进遗传算法 (君主方案进行选择交叉操作)
% 思路:
% 就是根据群体适应度值高低排序的基础上,用最优个体与其他偶数位的所有个体进行交叉每次交叉产生两个新个体,
% 交叉后对新产生的群体进行多点变异产生新群体,再计算其适应度值,然后和父群合并,按照适应度值进行排序,
% 选择前sizepop个个体为新群体。
ret = Emper_Select_Cross(Sortf,pcross,lenchrom,sizepop,bound);
% chorm = Sortf;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 变异操作 改进遗传算法
ret=Mutation(pmutation,lenchrom,ret,sizepop,[i maxgen],bound);
% 改进遗传算法
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 改进部分 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for ii=1:sizepop
NU_fit(ii) = fun(ret(ii,:));
end
[SortNU_fit Index] = sort(NU_fit,'descend'); % 适应度按从大到小排序
Sort_ret = ret(Index,:); % 染色体个体排序
% 产生新种群
clans_chrom = [Sortf ;Sort_ret]; % 子代父代染色体合并族群
clans_fit = [SortFIT SortNU_fit]; % 子代父代适应度合并族群
[Sort_clans_fit Index] = sort(clans_fit,'descend');% 排序
Sort_clans_chrom = clans_chrom(Index,:); % 个体换位
m_individuals.fitness = Sort_clans_fit(1:sizepop); % 将顺序前sizepop个赋予种群
m_individuals.chrom = Sort_clans_chrom(1:sizepop,:); % 将顺序前sizepop个赋予种群
m_avgfitness = sum(m_individuals.fitness)/sizepop; % 平均适应度
m_bestfitness = m_individuals.fitness(1); % 第一个是最优值
% m_trace = [m_trace;m_avgfitness m_bestfitness];
m_trace(q,i) = m_bestfitness;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
%进化结束
end
%% 结果显示
[r c]=size(trace);
figure
plot(1:c,trace(1,:),'Color','#1A3B30','LineWidth',3);
hold on
plot(1:c,trace(2,:),'-.','Color','#509579','LineWidth',3);
plot(1:c,trace(3,:),'--','Color','#99BDBD','LineWidth',3);
plot(1:c,trace(4,:),':','Color','#C6E3D8','LineWidth',3);
xlabel('Generation','FontName','Tim