?? fmaxga.m
字號:
function [BestPop,Trace]=fmaxga(FUN,LB,UB,eranum,popsize,pcross,pmutation,options)
% [BestPop,Trace]=fmaxga(FUN,LB,UB,eranum,popsize,pcross,pmutation)
% Finds a maximum of a function of several variables.
% fmaxga solves problems of the form:
% max F(X) subject to: LB <= X <= UB
% BestPop--------最優的群體即為最優的染色體群
% Trace-----------最佳染色體所對應的目標函數值
% FUN------------目標函數
% LB--------------自變量下限
% UB--------------自變量上限
% eranum----------種群的代數,取100--1000(默認1000)
% popsize---------每一代種群的規模;此可取50--100(默認50)
% pcross-----------交叉的概率,此概率一般取0.5--0.85之間較好(默認0.8)
% pmutation------變異的概率,該概率一般取0.05-0.2左右較好(默認0.1)
% options--------1×2矩陣,options(1)=0二進制編碼(默認0),option(1)~=0十進制編碼,option(2)設定求解精度(默認1e-4)
% 如測試Shaffer's(f=0.5-((sin(sqrt(x(1)^2+x(2)^2)))^2-0.5)/(1+0.001*(x(1)^2+x(2)^2))^2)函數,自變量下限
% [-100,-100],上限[100,100],當x=[0 0]時,取得理想最優MaxF6=1
% 運行得到相當好的結果:自變量為 0.00033379-4.7684e-005 時,最優值 1.000000
% 對應染色體是:1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 1
%%%%%%%%%%%%%% 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
T1=clock;
if nargin<3, error('FMAXGA requires at least three input arguments'); end
if nargin==3, eranum=1000;popsize=50;pcross=0.8;pmutation=0.1;options=[0 1e-4];end
if nargin==4, popsize=50;pcross=0.8;pmutation=0.1;options=[0 1e-4];end
if nargin==5, pcross=0.8;pmutation=0.1;options=[0 1e-4];end
if nargin==6, pmutation=0.1;options=[0 1e-4];end
if nargin==7, options=[0 1e-4];end
if find((LB-UB)>0)
error('數據輸入錯誤,請重新輸入(LB<UB):');
end
s=sprintf('程序運行需要約%.4f 秒鐘時間,請稍等......',(eranum*popsize*40/(1000*50)));
disp(s);
bounds=[LB;UB]';bits=[];
precision=options(2);%由求解精度確定二進制編碼長度
bits=ceil(log2((bounds(:,2)-bounds(:,1))' ./ precision));
[Pop]=initpop(popsize,bits);%初始化種群
[m,n]=size(Pop);
for j=1:m
value(j)=feval(FUN(1,:),(b2f(Pop(j,:),bounds,bits)));%計算適應度
end
[MaxValue,Index]=max(value);
BestPop(1,:)=Pop(Index,:);
Trace(1,1)=MaxValue;
Trace(1,(2:length(bits)+1))=b2f(BestPop(1,:),bounds,bits);
pm0=pmutation;
BestPop=zeros(eranum,n);Trace=zeros(eranum,length(bits)+1);%分配初始解空間
i=2;
while i<=eranum
% for j=1:m
% value(j)=feval(FUN(1,:),(b2f(Pop(j,:),bounds,bits)));%計算適應度
% end
% [MaxValue,Index]=max(value);
% BestPop(i,:)=Pop(Index,:);
% Trace(i,1)=MaxValue;
% Trace(i,(2:length(bits)+1))=b2f(BestPop(i,:),bounds,bits);
[selectpop]=selectchrom(FUN,Pop,bounds,bits);%選擇
for j=1:m
v1(j)=feval(FUN(1,:),(b2f(selectpop(j,:),bounds,bits)));%計算適應度
end
[CrossOverPop]=CrossOver(selectpop,pcross);%交叉
[NewPop]=Mutation(CrossOverPop,pmutation);%變異
Pop=NewPop;%更新
for j=1:m
value(j)=feval(FUN(1,:),(b2f(Pop(j,:),bounds,bits)));%計算適應度
end
[MaxValue,Index]=max(value);
[MinValue,MinIndex]=min(value);
BestPop(i,:)=Pop(Index,:);
Trace(i,1)=MaxValue;
Trace(i,(2:length(bits)+1))=b2f(BestPop(i,:),bounds,bits);
%保留最優個體
if Trace(i,1)>Trace(i-1,1)
Pop(i,:)
for j=1:m
v2(j)=feval(FUN(1,:),(b2f(Pop(j,:),bounds,bits)));%計算適應度
end
pmutation=pm0+(i^4)*(0.6-pm0)/(eranum^4); %隨著種群向前進化,逐步增大變異率
p(i)=pmutation;
i=i+1;
end
t=1:eranum;
plot(t,Trace(:,1)');
title('函數優化的遺傳算法');xlabel('進化世代數(eranum)');ylabel('每一代最優適應度(maxfitness)');
[MaxFval,I]=max(Trace(:,1));
X=Trace(I,(2:length(bits)+1));
hold on; plot(I,MaxFval,'*');
text(I+5,MaxFval,['FMAX=' num2str(MaxFval)]);
str1=sprintf('進化到 %d 代 ,自變量為 %s 時,得本次求解的最優值 %f\n對應染色體是:%s',...
I,num2str(X),MaxFval,num2str(BestPop(I,:)));
disp(str1);
%figure(2);plot(t,p);%繪制變異值增大過程
T2=clock;
CostTime=T2-T1;
if CostTime(6)<0
CostTime(6)=CostTime(6)+60; CostTime(5)=CostTime(5)-1;
end
if CostTime(5)<0
CostTime(5)=CostTime(5)+60;CostTime(4)=CostTime(4)-1;
end %像這種程序當然不考慮運行上小時啦
str2=sprintf('程序運行耗時 %d 小時 %d 分鐘 %.4f 秒',CostTime(4),CostTime(5),CostTime(6));
disp(str2);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -