📄 my_sga.m
字号:
%**********************************************************************/
% 本程序实现求函数的最大值 */
% f(x1,x2)=100(x1*x1-x2)*(x1*x1-x2)+(1-x1)*(1-x1) -2.048<<xi<<2.048*/
%--------------------------% 主函数 SGA() %---------------------------------%
function SGA()
format long;
ChromosomeLength = 20; %染色体长度包含两个变量各占一半
PopSize =80; %种群大小
MaxGeneration = 200; %最大演化代数
Pc=0.6; %杂交概率
Pm=0.1; %变异概率
gen = 0; %演化代数统计
PopulationValue=[];
PopulationFitness=[];
BestGene= zeros(ChromosomeLength,1);%保存演化过程中最好个体
BestFitness= eps;
BestValue= 0;
Bestgen= 0;
gx1=0;
gx2=0;
Population = floor(rand(ChromosomeLength,PopSize)*2);%初始化种群为 20*80 的 0-1矩阵
while(gen< MaxGeneration)
gen=gen+1;
[x1,x2,PopulationValue] = ValueOfFunc(Population); %计算个体的函数值
PopulationFitness = CalculateFitnessValue(Population); %计算个体的适应度值
[CurrentBestFitness CurrentBestIndex] = max(PopulationFitness); %返回当前最好的个体的属性值
if CurrentBestFitness > BestFitness %根据适应度值找到最好的个体
Bestgen =gen;
BestGene= Population(:,CurrentBestIndex);
BestFitness= PopulationFitness(CurrentBestIndex);
BestValue= PopulationValue(CurrentBestIndex);
Bestchromosome= [Population(:,CurrentBestIndex)]'; %最好个体的染色体值
gx1=x1;
gx2=x2;
end
% 输出每一代演化结果
disp(strcat('gen=',num2str(gen),' bestvalue=',num2str(BestValue),' x1=',num2str(gx1),' x2=',num2str(gx2),' chromosome=',num2str( Bestchromosome)));
%------------进行遗传操作--------------%
Population = Selection(Population,PopulationFitness); %选择
Population = Crossover(Population,Pc); %交叉
Population = Mutation(Population,Pm); %变异
end
%----------------------------% 主函数 SGA() %----------------------------------%
%-------------------------------------------------------------------------------%
%---------计算函数值,并返回变量x1,x2的值---------%
function [x1,x2,value]=ValueOfFunc(p)
%Length = size(Chromosome,1)/2;
length1=10;
length2=10;
[x y] = size(p);
value=zeros(1,y);
for i=1:y
Chromosome=p(:,i);
temp1=0;
temp2=0;
for j=1:length2
temp1=temp1+Chromosome(j)*(2^(j-1)); % DecodeChromosome(population[i].chrom,0,length1);
temp2=temp2+Chromosome(j+length1)*(2^(j-1)); % DecodeChromosome(population[i].chrom,length1,length2);
end
x1=4.096*temp1/1023.0-2.048;
x2=4.096*temp2/1023.0-2.048;
value(i)=100*((x1^2-x2)^2)+(1-x1)^2;
end
%---------计算函数值,并返回变量x1,x2的值-----------%
%-----------求种群中每个个体的适应度值-------------%
function [fitness] = CalculateFitnessValue(p)
[x1,x2,value]=ValueOfFunc(p);
[x y] = size(p);
for i=1:y
fitness(i)= value(i);
end
%----------求种群中每个个体的适应度值-------------%
%--------------------------- 选择 ------------------------------%
function [NewPopulation] = Selection(Population,PopulationFitness)
%轮盘赌法进行选择
[ChromosomeLength PopSize] = size(Population);
SumFitness=sum(PopulationFitness); %计算个体适应度之和
cfitness=PopulationFitness/SumFitness; %计算个体的相对适应度
SelectPi=rand(1,PopSize); %产生选择随机数
for i=2:PopSize
cfitness(i)=cfitness(i)+cfitness(i-1); %计算累计概率:
end
NewPopulation=zeros(ChromosomeLength,PopSize);
for i=1:PopSize
for j=1:PopSize
if SelectPi(i) <= cfitness(j)
NewPopulation(:,i)=Population(:,j);
break;
end
end
end
%--------------------- 交叉 ------------------------%
function [NewPopulation] = Crossover(Population,Pc)
[ChromosomeLength PopSize] = size(Population);
for i=1:PopSize
index(i)=i;
end
% 随机选择两个父体,并记下位置
for i=1:PopSize
point=floor(rand(1)*(PopSize-i+1));
temp=index(i);
index(i)=index(point+i);
index(point+i)=temp;
end
% 进行单点交叉
for i=1:2:(PopSize-2)
p=rand(1);
if(p<Pc)
point=floor(rand(1)*(ChromosomeLength-1))+1; % 随机选择交叉点
for j=point:ChromosomeLength-1
temp=Population(j,index(i));
Population(j,index(i))=Population(j,index(i+1));
Population(j,index(i+1))=temp;
end
end
end
NewPopulation = Population;
%---------------------- 变异 ------------------ -%
function [NewPopulation] = Mutation(Population,Pm)
[ChromosomeLength PopSize]= size(Population);
for i=1:PopSize
if rand < Pm
for j=1:ChromosomeLength
if Population(j,i)==0
Population(j,i)==1;
else
Population(j,i)==0;
end
end
end
end
NewPopulation=Population;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -