⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fmaxga.m

📁 用matlab编写的二进制的遗传算法程序.rar
💻 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 + -