>> function Main()
%定义全局变量
global VariableNum POPSIZE MaxGens PXOVER PMutation
VariableNum=3 %变量个数
POPSIZE=50 %种群大小
MaxGens=1000 %种群代数
PXOVER=0.8 %交叉概率
PMutation=0.2 %变异概率
%读取数据文件
load E:\现代优化算法\遗传算法\bound.txt
VarBound=bound(:,1:2);
global Pop newPop
Pop=zeros(POPSIZE+1,VariableNum);
newPop=zeros(POPSIZE+1,VariableNum);
%初始化种群
for i=1:POPSIZE
for j=1:VariableNum
Pop(i,j)=VarBound(j,1)+rand()*(VarBound(j,2)-VarBound(j,1));
end
end
%计算适应值
fitnessList=zeros(POPSIZE,1);
for i=1:POPSIZE
fitnessList(i,1)=fitness(Pop(i,1:VariableNum));
end
%保存最好值和最坏值
Best=zeros(1,VariableNum+1);
Worst=zeros(1,VariableNum+1);
maxvalue=max(fitnessList);
indexMax=find(fitnessList==maxvalue,1,'first');
Best(1,1:VariableNum)=Pop(indexMax,1:VariableNum);
Best(1,VariableNum+1)=maxvalue;
minvalue=min(fitnessList);
indexMin=find(fitnessList==minvalue,1,'first');
Worst(1,1:VariableNum)=Pop(indexMin,1:VariableNum);
Worst(1,VariableNum+1)=minvalue;
genetation=1;
while genetation<MaxGens
%计算适应度区间
sumfit=sum(abs(fitnessList));
relativeFitness=zeros(POPSIZE,1);
relativeFitness=abs(fitnessList)/sumfit;
for i=2:POPSIZE
relativeFitness(i)=relativeFitness(i-1)+relativeFitness(i);
end
%选择操作
newPop=Select(Pop,relativeFitness);
%交叉操作
newPop=Xcross(newPop,VariableNum,PXOVER);
%变异操作
newPop=Mutation(newPop,VariableNum,PMutation,VarBound);
%计算新种群适应值
for i=1:POPSIZE
fitnessList(i,1)=fitness(newPop(i,1:VariableNum));
end
%保存最好值和替换最坏值
maxvalue=max(fitnessList);
indexMax=find(fitnessList==maxvalue,1,'first');
minvalue=min(fitnessList);
indexMin=find(fitnessList==minvalue,1,'first');
if Best<maxvalue
Best(1,1:VariableNum)=newPop(indexMax,1:VariableNum);
Best(1,VariableNum+1)=maxvalue;
else
newPop(indexMin,1:VariableNum)=Best(1,1:VariableNum);
fitnessList(indexMin,1)=Best(1,VariableNum+1);
end
%用子代替换父代
Pop=newPop;
genetation=genetation+1;
end
Best
=========================================================
%选择操作
function newPop=Select(Pop,Rfitness)
for i=1:length(Rfitness)
r=rand();
index=1;
for j=1:length(Rfitness)
if r<=Rfitness(j,1)
index=j;
break;
end
end
newPop(i,:)=Pop(index,:);
end
======================================
%交叉操作
function newPop=Xcross(Pop,VariableNUM,CrossRate)
point=1;
sizePop=length(Pop);
for i=0:sizePop/2
Xrate=rand();
if Xrate<CrossRate %如果交叉
first_index=round(rand()*(sizePop-2)+1);
second_index=round(rand()*(sizePop-2)+1);
while first_index==second_index %排除两个个体一样的情况
second_index=round(rand()*(sizePop-2)+1);
end
if VariableNUM>1
if VariableNUM==2
point=1;
else
point=round(rand()*(VariableNUM-2)+1);
end
tempOne=zeros(1,point);
tempOne(1,1:point)=Pop(first_index,1:point);
Pop(first_index,1:point)=Pop(second_index,1:point);
Pop(second_index,1:point)=tempOne(1,1:point);
end
end
end
newPop=zeros(size(Pop),1);
newPop=Pop;
====================================================
%变异操作
function newPop=Mutation(Pop,VariableNUM,MutationRate,bound)
point=1;
sizePop=length(Pop);
for i=1:sizePop
for j=1:VariableNUM
Mrate=rand();
if Mrate<MutationRate %如果发生变异
Pop(i,j)= rand()*(bound(j,2)-bound(j,1))+ bound(j,1);
end
end
end
newPop=zeros(size(Pop),1);
newPop=Pop;
=================================================
%适应值函数或目标函数
%函数 x1^2-x1*x2+x3
function value=fitness(varargin)
n=varargin{1,1};
value=n(1,1)^2-n(1,1)*n(1,2)+n(1,3);
================================================
实例:bound文件
1 10
0 5
10 20
评论0