• 大小: 9KB
    文件类型: .m
    金币: 1
    下载: 0 次
    发布日期: 2021-05-23
  • 语言: Matlab
  • 标签: matlab  

资源简介

matlab萤火虫代码,萤火虫搜索算法源代码

资源截图

代码片段和文件信息

% Group Search Optimizer algorithm in Matlab
% Copyright (C) 2004-2005 Department of Electricity 
% Engineering and Electronics The University of Liverpool UK
% Last modifed 12-May-05

function [fbestvalbestmemberhistory] = GSO(fnameMaxIter)

% function [fbestvalbestmember] = GSO(fnameNDimMaxIter)
%
%   Run a Group Search Optimizer algorithm
%
% Input Arguments:
%   fname       - the name of the evaluation .m function
%   NDim        - dimension of the evalation function
%   MaxIter     - maximum iteration
close all
tic%计算程序使用时间,与程序结尾的toc相呼应
global NDim Acnumb
flag=0;
iteration = 0;
%-----------------------------
%MaxIter=10;
Best=zeros(MaxIter1);
%fname=‘DShapeweightplane10‘;
Bound=eval(fname);
PopSize=48;     % population of members
angle=pi/4.*ones(NDim-1PopSize);   % Initialize head angle
%leftangle=angle; 
%rightangle=angle;

% Defined lower bound and upper bound.
LowerBound = zeros(NDimPopSize);
UpperBound = zeros(NDimPopSize);
for i=1:PopSize
    LowerBound(:i)=Bound(:1);
    UpperBound(:i)=Bound(:2);
end
%DResult = 1e-1;    % Desired results
%-----------------------------%
vmax = ones(NDimPopSize);
for i=1:NDim
    vmax(i:)=(UpperBound(i:)-LowerBound(i:));
end
r=norm(vmax(:1));
distance=r*repmat(ones(1PopSize)NDim1);
a= round(((NDim+1)^.5));
direction=zeros(NDimPopSize);
for j=1:PopSize
    direction(1j)=(cos(angle(1j)));
    for i=2:NDim-1
        direction(ij)=cos(angle(ij)).*prod(sin(angle(i:NDim-1j)));
    end
    direction(NDimj)=prod(sin(angle(1:NDim-1j)));
end
initialization=1;
while initialization==1
    initialization=0;
    %---------包含初始设计值-------%
    population =  rand(NDim PopSize).*(UpperBound-LowerBound) + LowerBound;     % Initialize swarm population
    %population(:1)
    %-----------------------------%
    %-----------add---------------%
    population(1:Acnumb:)=round(population(1:Acnumb:)*100000)/100000;%
    population(Acnumb+1:NDim:)=round(population(Acnumb+1:NDim:)*1000)/1000;%
    exexutefunction=strcat(fname‘(population)‘);
    % Evaluate initial population
    fvalue = eval(exexutefunction);
    if min(fvalue) == inf
        initialization=1;
    end
end

% Finding best member in initial population
[fbestvalindex] = min(fvalue);    
bestmember=population(:index);
oldangle=angle;
oldindex=index; %#ok
badcounter=0;
while(flag == 0) && (iteration < MaxIter)
    %rand(‘state‘sum(100*clock))
    iteration = iteration +1;

     for j=1:PopSize
        R1=randn(1);
        R2=rand(NDim-11);
        R3 = rand(NDim 1);

        if j==index % Stop and search around

            SamplePosition=zeros(NDim4);
            SampleAngle=zeros(NDim-14);
            SampleValue=zeros(14);
            leftangle=(pi/(a^2)).*R2+angle(:j);  %trun left the angle range between new and old direction is \in [0pi/a^2]
            rightangle=-(pi/(a^2)).*R2+angle(:j); %trun right the angle range between new and old direction is \in [0pi/a^2]
            
            distance(:j)=

评论

共有 条评论