【优化求解】蝴蝶算法MBO matlab源码

摘要:蝴蝶优化算法 (Butterfly optimization algorithm,BOA)是由 Arora于基于蝴蝶BOA觅食过程提出的自然启发式算法。该算法具有较高的收敛精度。

 

1.算法原理

蝴蝶利用自身的感知器定位食物的来源。该算法中,假设每只蝴蝶产生一定强度的香味,这些香味会传播并被区域内的其它蝴蝶感知。每只蝴蝶释放出的香味与它的适应度有关。这就意味着当一只蝴蝶移动了位置,它的适应度也将随之变化。当蝴蝶感觉到另一只蝴蝶在这个区域散发出更多的香味时,就会去靠近,这个阶段被称为全局搜索。另外一种情况,当蝴蝶不能感知大于它自己的香味时,它会随机移动,这个阶段称为局部搜索阶段。

香味是根据 刺 激 的 物 理 强 度 来 表 述 的。其 计 算 如 式(1)所示:

【优化求解】蝴蝶算法MBO matlab源码_第1张图片

2.算法流程:

(1) 计算适应度函数f ( x ) , x = ( x 1 , . . . , x d i m ) f(x),x=(x1,...,xdim)f(x),x=(x1,...,xdim)
(2) 给每个蝴蝶生成n nn个初始解 x i = ( i = 1 , 2 , . . . , n ) x_i=(i=1,2,...,n)xi​=(i=1,2,...,n)
(3) 声明变量 c , α , g ∗ , p c,\alpha,g^*,pc,α,g∗,p
(4) while未到终止条件do
(5) for每一个蝴蝶do
(6) 采用式(1)计算其香味函数f ff
(7) end for
(8) 找出最优的香味函数f ff,并赋值给g ∗ g^*g∗
(9) for 每一个蝴蝶do
(10) 采用式(4)计算概率 r
(11) if r

(12) 采用式(2)进行全局搜索
(13) else
(14) 采用式(3)进行局部随机搜索
(15) end if
(16) end for
(17) end while
(18) 输出最优解 .

3.参考文献:

[1] Arora S, Singh S. Butterfly optimization algorithm: a novel approach for global optimization[C]. soft computing, 2019, 23(3): 715-734.

[2]李田来,刘方爱.带混沌映射的WSN蝴蝶优化定位算法[J].计算机工程与设计,2019,40(06):1729-1733.

[3]刘云涛.基于蝴蝶优化的粒子滤波算法[J].信息技术与网络安全,2018,37(07):37-41.

4.MATLAB代码



% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %%
%% Notes:
% Different run may generate different solutions, this is determined by
% the the nature of metaheuristic algorithms.
%%

function [MinCost] = MBO(ProblemFunction, DisplayFlag, RandSeed)

% Monarch Butterfly Optimization (MBO) software for minimizing a general function
% The fixed generation is considered as termination condition.

% INPUTS: ProblemFunction is the handle of the function that returns
%         the handles of the initialization, cost, and feasibility functions.
%         DisplayFlag = true or false, whether or not to display and plot results.
%         ProbFlag = true or false, whether or not to use probabilities to update emigration rates.
%         RandSeed = random number seed
% OUTPUTS: MinCost = array of best solution, one element for each generation
%          Hamming = final Hamming distance between solutions
% CAVEAT: The "ClearDups" function that is called below replaces duplicates with randomly-generated
%         individuals, but it does not then recalculate the cost of the replaced individuals.

tic

if ~exist('ProblemFunction', 'var')
    ProblemFunction = @Ackley;
end
if ~exist('DisplayFlag', 'var')
    DisplayFlag = true;
end
if ~exist('RandSeed', 'var')
    RandSeed = round(sum(100*clock));
end

[OPTIONS, MinCost, AvgCost, InitFunction, CostFunction, FeasibleFunction, ...
    MaxParValue, MinParValue, Population] = Init(DisplayFlag, ProblemFunction, RandSeed);

% % % % % % % % % % % %             Initial parameter setting          % % % % % % % % % % % %%%%
%% Initial parameter setting
Keep = 2; % elitism parameter: how many of the best habitats to keep from one generation to the next
maxStepSize = 1.0;        %Max Step size
partition = OPTIONS.partition;
numButterfly1 = ceil(partition*OPTIONS.popsize);  % NP1 in paper
numButterfly2 = OPTIONS.popsize - numButterfly1; % NP2 in paper
period = 1.2; % 12 months in a year
Land1 = zeros(numButterfly1, OPTIONS.numVar);
Land2 = zeros(numButterfly2, OPTIONS.numVar);
BAR = partition; % you can change the BAR value in order to get much better performance
% % % % % % % % % % % %       End of Initial parameter setting       % % % % % % % % % % % %%
%%

% % % % % % % % % % % %             Begin the optimization loop        % % % % % % % % % %%%%
% Begin the optimization loop
for GenIndex = 1 : OPTIONS.Maxgen
    
    % % % % % % % % % % % %            Elitism Strategy           % % % % % % % % % % % %%%%%
    %% Save the best monarch butterflis in a temporary array.
    for j = 1 : Keep
        chromKeep(j,:) = Population(j).chrom;
        costKeep(j) = Population(j).cost;
    end
    % % % % % % % % % % % %       End of  Elitism Strategy      % % % % % % % % % % % %%%%
    %%
    
    % % % % % % % % % % % %    Divide the whole population into two subpopulations % % % %%%
    %% Divide the whole population into Population1 (Land1) and Population2 (Land2)
    % according to their fitness.
    % The monarch butterflies in Population1 are better than or equal to Population2.
    % Of course, we can randomly divide the whole population into Population1 and Population2.
    % We do not test the different performance between two ways.
    for popindex = 1 : OPTIONS.popsize
        if popindex <= numButterfly1
            Population1(popindex).chrom = Population(popindex).chrom;
        else
            Population2(popindex-numButterfly1).chrom = Population(popindex).chrom;
        end
    end
    % % % % % % % % % % %    End of Divide the whole population into two subpopulations  % % %%%
    %%
    
    % % % % % % % % % % % %%            Migration operator          % % % % % % % % % % % %%%%
    %% Migration operator
    for k1 = 1 : numButterfly1
        for parnum1 = 1 : OPTIONS.numVar
            r1 = rand*period;
            if r1 <= partition
                r2 = round(numButterfly1 * rand + 0.5);
                Land1(k1,parnum1) = Population1(r2).chrom(parnum1);
            else
                r3 = round(numButterfly2 * rand + 0.5);
                Land1(k1,parnum1) = Population2(r3).chrom(parnum1);
            end
        end %% for parnum1
        NewPopulation1(k1).chrom =  Land1(k1,:);
    end  %% for k1
    % % % % % % % % % % % %%%       End of Migration operator      % % % % % % % % % % % %%%
    %%
    
    % % % % % % % % % % % %             Evaluate NewPopulation1          % % % % % % % % % % % %%
    %% Evaluate NewPopulation1
    SavePopSize = OPTIONS.popsize;
    OPTIONS.popsize = numButterfly1;
    % Make sure each individual is legal.
    NewPopulation1 = FeasibleFunction(OPTIONS, NewPopulation1);
    % Calculate cost
    NewPopulation1 = CostFunction(OPTIONS, NewPopulation1);
    OPTIONS.popsize = SavePopSize;
    % % % % % % % % % % % %       End of Evaluate NewPopulation1      % % % % % % % % % % % %%
    %%
    
    % % % % % % % % % % % %             Butterfly adjusting operator          % % % % % % % % % % % %%
    %% Butterfly adjusting operator
    for k2 = 1 : numButterfly2
        scale = maxStepSize/(GenIndex^2); %Smaller step for local walk
        StepSzie = ceil(exprnd(2*OPTIONS.Maxgen,1,1));
        delataX = LevyFlight(StepSzie,OPTIONS.numVar);
        for parnum2 = 1:OPTIONS.numVar,
            if (rand >= partition)
                Land2(k2,parnum2) = Population(1).chrom(parnum2);
            else
                r4 = round(numButterfly2*rand + 0.5);
                Land2(k2,parnum2) =  Population2(r4).chrom(1);
                if (rand > BAR) % Butterfly-Adjusting rate
                    Land2(k2,parnum2) =  Land2(k2,parnum2) +  scale*(delataX(parnum2)-0.5);
                end
            end
        end  %% for parnum2
        NewPopulation2(k2).chrom =  Land2(k2,:);
    end %% for k2
    % % % % % % % % % % % %       End of Butterfly adjusting operator      % % % % % % % % % % % %
    %%
    
    % % % % % % % % % % % %             Evaluate NewPopulation2          % % % % % % % % % % % %%
    %% Evaluate NewPopulation2
    SavePopSize = OPTIONS.popsize;
    OPTIONS.popsize = numButterfly2;
    % Make sure each individual is legal.
    NewPopulation2 = FeasibleFunction(OPTIONS, NewPopulation2);
    % Calculate cost
    NewPopulation2 = CostFunction(OPTIONS, NewPopulation2);
    OPTIONS.popsize = SavePopSize;
    % % % % % % % % % % % %       End of Evaluate NewPopulation2      % % % % % % % % % % % %%
    %%
    
    % % % % % % %  Combine two subpopulations into one and rank monarch butterflis       % % % % % %
    %% Combine Population1 with Population2 to generate a new Population
    Population = CombinePopulation(OPTIONS, NewPopulation1, NewPopulation2);
    % Sort from best to worst
    Population = PopSort(Population);
    % % % % % %     End of Combine two subpopulations into one and rank monarch butterflis  % %% % %
    %%
    
    % % % % % % % % % % % %            Elitism Strategy          % % % % % % % % % % % %%% %% %
    %% Replace the worst with the previous generation's elites.
    n = length(Population);
    for k3 = 1 : Keep
        Population(n-k3+1).chrom = chromKeep(k3,:);
        Population(n-k3+1).cost = costKeep(k3);
    end % end for k3
    % % % % % % % % % % % %     End of  Elitism Strategy      % % % % % % % % % % % %%% %% %
    %%
    
    % % % % % % % % % %           Precess and output the results          % % % % % % % % % % % %%%
    % Sort from best to worst
    Population = PopSort(Population);
    % Compute the average cost
    [AverageCost, nLegal] = ComputeAveCost(Population);
    % Display info to screen
    MinCost = [MinCost Population(1).cost];
    AvgCost = [AvgCost AverageCost];
    if DisplayFlag
        disp(['The best and mean of Generation # ', num2str(GenIndex), ' are ',...
            num2str(MinCost(end)), ' and ', num2str(AvgCost(end))]);
    end
    % % % % % % % % % % %    End of Precess and output the results     %%%%%%%%%% %% %
    %%
    
end % end for GenIndex
Conclude1(DisplayFlag, OPTIONS, Population, nLegal, MinCost, AvgCost);

toc

% % % % % % % % % %     End of Monarch Butterfly Optimization implementation     %%%% %% %
%%


function [delataX] = LevyFlight(StepSize, Dim)

%Allocate matrix for solutions
delataX = zeros(1,Dim);

%Loop over each dimension
for i=1:Dim
    % Cauchy distribution
    fx = tan(pi * rand(1,StepSize));
    delataX(i) = sum(fx);
end

【优化求解】蝴蝶算法MBO matlab源码_第2张图片

完整代码或者代写添加QQ1575304183

往期回顾>>>>>>

【优化求解】混沌粒子群matlab源码

【优化求解】土狼算法matlab源码

【优化求解】基于混沌反向学习改进灰狼算法matlab源码

【优化求解】粒子群优化灰狼算法matlab源码

【优化求解】改进灰狼算法求解重油热解模型matlab源码

【多目标优化求解】多目标灰狼优化算法MOGWOmatlab源码

【多目标优化求解】基于金鹰算法(MOGEO)的多目标优化求解matlab源码

【优化求解】蜉蝣算法matlab源码

【优化求解】平衡优化器算法matlab源码

【优化求解】麻雀算法matlab源码

【优化求解】探路者优化算法matlab源码

【优化求解】改进的萤火虫算法matlab源码

【优化求解】磷虾群算法(Krill Herd Algorithm,KHA)

【优化求解】差分进化算法(Differential Evolution)matlab源码

【优化求解】冠状病毒群体免疫优化算法(CHIO)matlab源码

【优化求解】金鹰优化求解算法(GEO)matlab源码

【优化求解】基于黄金正弦算法GoldSA智能优化算法matlab源码

【优化求解】遗传优化隶属度函数matlab源码

【优化求解】多目标智能算法优化求解matlab工具箱

你可能感兴趣的:(matlab,优化求解)