本帖隐藏的内容
- function SA_GA_ExampleI()
- clc;
- clear all
- ub_lb=[-5 -5;5 5];
- % ub_lb的设置其中第一行是下界第二行是上界
- Population=10;
- MaxIteration=10;
- SelectionProbability=0.9;
- CrossoverProbability=0.7;
- MutationProbability=0.01;
- NumberofIndividuals=2;
- traceX=zeros(MaxIteration,NumberofIndividuals);
- traceY=zeros(1,MaxIteration);
- Precision=8;
- % 以上为参数的设置
- FielD=[repmat(Precision,1,NumberofIndividuals);ub_lb;repmat([1;0;1;1],1,NumberofIndividuals)];
- Iteration=0;
- Chromsome=CRTBP(Population,NumberofIndividuals*Precision);
- X=BS2RV(Chromsome,FielD);
- ObjV=ObjVfun(X);
- while Iteration<MaxIteration
- FitV=RANKING(ObjV);
- SelCh=SELECT('SUS',Chromsome,FitV,SelectionProbability);
- SelCh=RECOMBIN('XOVSP',SelCh,CrossoverProbability);
- SelCh=MUT(SelCh,MutationProbability);
-
- X=BS2RV(SelCh,FielD);
- ObjSelV=ObjVfun(X);
-
- [Chromsome,ObjV]=REINS(Chromsome,SelCh,1,1,ObjV,ObjSelV);
- X=BS2RV(Chromsome,FielD);
-
- Iteration=Iteration+1;
- [fval,index]=min(ObjV);
- traceY(Iteration)=fval;
- traceX(Iteration,:)=X(index,:);
- end
- figure(1)
- set(figure(1),'color','w')
- hold on
- plot(traceY,'ro','markerfacecolor','r','markersize',12,'markeredgecolor','k')
- grid on
- legend('The Best FvalPath')
- hold off
- YY=traceY(end);
- disp(['The Best Fval= ',num2str(YY)])
- end
- % 目标函数与建立最优初始值结果
- function Obj=ObjVfun(X)
- % X是初始值矩阵
- N=size(X,1);
- ObjectiveFunction = @fun;
- lb = [-5 -5];
- ub = [5 5];
- options = saoptimset('MaxIter',300,'StallIterLim',200,'TolFun',1e-20,'AnnealingFcn',@annealingfast,...
- 'InitialTemperature',100,'TemperatureFcn',@temperatureexp,'ReannealInterval',500);
- Obj=zeros(N,1);
- for i=1:N
- [~,fval]=simulannealbnd(ObjectiveFunction,X(i,:),lb,ub,options);
- Obj(i)=fval;
- end
- end
- % 目标函数
- % y = (4 - 2.1*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + (-4 +
- % 4*x(2)^2)*x(2)^2;
- % Rastrigin's function
- function y=fun(x)
- y = 20 + x(1)^2 + x(2)^2 - 10*(cos(2*pi*x(1))+cos(2*pi*x(2)));
- end
最终:The Best Fval= 2.0843e-007
二,基于遗传算法的模拟退火算法例子应用
- function SA_GA_Example()
- clc;
- clear all
- ub_lb=[-5 -5;5 5];
- % 种群
- % 基于遗传算法的参数设置
- Population=40;
- MaxIteration=20;
- SelectionProbability=0.9;
- CrossoverProbability=0.7;
- MutationProbability=0.01;
- NumberofIndividuals=2;
- Precision=20;
- % 基于模拟退火算法的参数设置
- Temp=90;
- a=0.97;
- T_end=3;
- % 用于跟踪遗传算法最优的变量和解值
- TraceY=zeros(MaxIteration,1);
- TraceX=zeros(MaxIteration,NumberofIndividuals);
- % 用于跟踪模拟退火的最优变量和解值
- BestY=100;
- BestX=100*ones(1,2);
- % 建立区域扫描区间
- FielD=[repmat(Precision,1,NumberofIndividuals);ub_lb;repmat([1;0;1;1],1,NumberofIndividuals)];
- % 初始化染色体
- Chromsome=CRTBP(Population,NumberofIndividuals*Precision);
- % 将二进制数转化为十进制数
- X=BS2RV(Chromsome,FielD);
- ObjV=ObjVfun(X);
- while Temp>=T_end
- Iteration=0;
- while Iteration<MaxIteration
- FitV=RANKING(ObjV);
- SelCh=SELECT('SUS',Chromsome,FitV,SelectionProbability);
- SelCh=RECOMBIN('XOVSP',SelCh,CrossoverProbability);
- SelCh=MUT(SelCh,MutationProbability);
-
- X=BS2RV(SelCh,FielD);
- ObjSelV=ObjVfun(X);
- [Chromsome,ObjV]=REINS(Chromsome,SelCh,1,1,ObjV,ObjSelV);
- X=BS2RV(Chromsome,FielD);
-
- Iteration=Iteration+1;
- [Fval,Index]=min(ObjV);
- TraceY(Iteration)=Fval;
- TraceX(Iteration,:)=X(Index,:);
- end
-
- [FvalY,I]=min(TraceY);
- if FvalY<=BestY
- BestY=[BestY FvalY];
- BestX=TraceX(I,:);
- elseif rand<exp(-(FvalY-BestY)/Temp)
- BestY=[BestY FvalY];
- BestX=TraceX(I,:);
- else
- BestY=[BestY BestY(end)];
- end
- Temp=Temp*a;
- end
- figure(1)
- set(figure(1),'color','w')
- hold on
- plot(2*(1:length(BestY)),BestY,'ro','markerfacecolor','r','markersize',8,'markeredgecolor','k')
- grid on
- legend('The BestY FvalPath')
- hold off
- X1=BestX(1);
- X2=BestX(2);
- disp(['The X1 value is',num2str(X1),' and The X2 value is',num2str(X2)])
- end
- % 列向量目标函数
- function Obj=ObjVfun(X)
- N=size(X,1);
- Obj=zeros(N,1);
- for i=1:N
- Obj(i)=fun(X(i,:));
- end
- end
- % 目标函数
- % y = (4 - 2.1*x(1)^2 + x(1)^4/3)*x(1)^2 + x(1)*x(2) + (-4 +
- % 4*x(2)^2)*x(2)^2;
- % Rastrigin's function
- function y=fun(x)
- y = 20 + x(1)^2 + x(2)^2 - 10*(cos(2*pi*x(1))+cos(2*pi*x(2)));
- end