function [x,y,max_value]=GA
global M T Gen NextGen
pr=initialization(); %初始化遗传算法参数
[Gen NextGen]=initGeneration(pr); %产生父代及子代
for i=1:T
[Fitness x1 x2]=CalculateFitness(pr);
[OrderFitness,Index]=sort(Fitness); %将适应度从小到大进行排序
MaxFitness=OrderFitness(M); %找出适应度的最大值
AvgFitness=mean(Fitness); %求出适应度的平均值
Diff=MaxFitness - AvgFitness;
Best_x1=x1(Index(M));
Best_x2=x2(Index(M));
BetterFit=SaveElitist(Index,pr); %保护精英个体
Select(Fitness,pr); %选择操作
Cross(OrderFitness,AvgFitness,Diff,pr); %交叉操作
Mutation(OrderFitness,MaxFitness,AvgFitness,Diff,pr); %变异操作
RestoreElitist(BetterFit,pr); %恢复精英个体
Gen=NextGen; %产生新一代
end
max_value=MaxFitness; %输出结果
x=Best_x1;
y=Best_x2;
function pr=initialization()
global M T
M=80; %种群大小
T=200; %进化代数
pr.ChromLong=22; %染色体长度(二进制编码)
pr.Pcross1=0.9; %交叉概率常数
pr.Pcross2=0.6;
pr.Pmutation1=0.1; %变异概率常数
pr.Pmutation2=0.001;
pr.ElitistNumber=5; %精英个体数量
pr.VaribleNumber=2; %变量个数
pr.Lower1=-10; %设置变量定义域
pr.Upper1=10;
pr.Lower2=-10;
pr.Upper2=10;
%初始化父代及子代
function [Gen NextGen]=initGeneration(pr)
global M Gen NextGen
CL=pr.ChromLong;
VN=pr.VaribleNumber;
Gen=round(rand(M,CL*VN)); %随机产生父代
NextGen=zeros(M,CL*VN); %初始化子代
%计算适应度
function [Fitness x1 x2]=CalculateFitness(pr)
global M Gen
CL=pr.ChromLong;
Upper1=pr.Upper1;
Upper2=pr.Upper2;
Lower1=pr.Lower1;
Lower2=pr.Lower2;
for s=1:M
Individual=Gen(s,:);
decimal_x1=0;
decimal_x2=0;
Chrom_1=Individual(1:CL); %对x1进行编码
for i=1:CL
decimal_x1=decimal_x1+Chrom_1(i)*2^(i-1);
end
x1(s)=(Upper1-Lower1)*decimal_x1/(2^CL-1)+Lower1;
Chrom_2=Individual(CL+1:1:2*CL); %对x2进行解码
for i=1:CL
decimal_x2=decimal_x2+Chrom_2(i)*2^(i-1);
end
x2(s)=(Upper2-Lower2)*decimal_x2/(2^CL-1)+Lower2;
Fitness(s)=objFunction(x1(s),x2(s)); %计算适应度
end
%选择操作,采用轮盘赌选择法
function Select(Fitness,pr)
global M Gen NextGen
BFN=pr.ElitistNumber;
SumFitness=sum(Fitness); %计算适应度和
for i=1:(M-BFN)
Rd=rand;
s=1;
temp=0;
while(temp<Rd) & (s<=M)
temp=temp+Fitness(s)/SumFitness; %计算积累概率
s=s+1;
end
if s==1
s=1;
else
s=s-1;
end
NextGen(i,:)=Gen(s,:);
end
%交叉操作
function Cross(OrderFitness,AvgFitness,Diff,pr)
global M NextGen
CL=pr.ChromLong;
VN=pr.VaribleNumber;
Pc1=pr.Pcross1;
Pc2=pr.Pcross2;
EN=pr.ElitistNumber;
for i=1:2:(M-EN)
Rn=rand;
if OrderFitness(i+1)>=AvgFitness %计算自适应交叉概率
pcross=Pc1-(Pc1-Pc2)*(OrderFitness(i+1)-AvgFitness)/Diff;
else
pcross=Pc1;
end
if pcross>Rn %满足交叉条件时,进行交叉运算
CrossPoint=ceil(VN*CL*Rn);
if or(CrossPoint==0,CrossPoint>=VN*CL)
continue;
end
for j=CrossPoint:VN*CL %随机交换部分染色体基因
temp=NextGen(i,j);
NextGen(i,j)=NextGen(i+1,j);
NextGen(i+1,j)=temp;
end
end
end
%变异操作
function Mutation(OrderFitness,MaxFitness,AvgFitness,Diff,pr)
global M NextGen
EN=pr.ElitistNumber;
CL=pr.ChromLong;
VN=pr.VaribleNumber;
Pm1=pr.Pmutation1;
Pm2=pr.Pmutation2;
for i=1:(M-EN)
for j=1:VN*CL
if OrderFitness(i)>=AvgFitness %计算自适应变异概率
pmutation=Pm1-(Pm1-Pm2)*(MaxFitness-OrderFitness(i))/Diff;
else
pmutation=Pm1;
end
if pmutation>rand %满足变异条件时,进行变异运算
if NextGen(i,j)==0
NextGen(i,j)=1;
else
NextGen(i,j)=0;
end
end
end
end
%保护精英个体
function BetterFit=SaveElitist(Index,pr)
global M Gen
EN=pr.ElitistNumber;
for i=1:EN
BetterFit(i,:)=Gen(Index(M-i+1),:);
end
%恢复精英个体
function RestoreElitist(BetterFit,pr)
global M NextGen
EN=pr.ElitistNumber;
for i=1:EN
NextGen(M-i+1,:)=BetterFit(i,:);
end
%目标函数
function F=objFunction(x1,x2)
F=0.5-((sin(sqrt(x1.^2+x2.^2))).^2-0.5)./(1+0.001*(x1.^2+x2.^2).^2);
- 1
- 2
- 3
- 4
- 5
前往页