【优化求解】基于matlab生物地理算法求解MLP问题【含Matlab源码 1415期】

1,770 阅读3分钟

一、生物地理算法简介

1 基本思路 BBO 算法起源于生物地理学,它通过模拟多物种在不同栖息地的分布、迁移、突变等规律求解寻优问题,在多目标规划领域有广泛应用. 栖息地被认为是独立的区域,不同的栖息地拥有不同的适宜指数HSI(Habitat suitability index)。 HSI较高的栖息地物种丰富度较高,随着种群趋于饱和,其迁出率增高,迁入率减少,而HIS较低的栖息地与之相反,迁入率增高,迁出率减少. 当栖息地遭遇灾害或瘟疫等突发事件时,HIS将随之突变,打破动态平衡,为低HIS的栖息地添加了不可预见性,增大了搜索目标解的几率2.2 迁移和突变操作物种的迁移有其具体的物理模型,最常的有线性模型、二次模型、余弦模型等 . 以图 3线性模型为例,当某栖息地物种数目为 0 时迁入率最高,此刻 λ = I,随着迁入物种数目不断增加,受阳光、水、食物等资源限制,迁入率不断降低,迁出率不断增高 . 当栖息地物种数目为 S0时,恰好达到动态平衡,此时迁出率与迁入率相同 . 而栖息地达到饱和状态时,物种数量达到最大值Smax ,此刻不再有物种迁入,迁出率 μ = E.突变操作基于生物地理学统计公式完成: 在这里插入图片描述 在这里插入图片描述 式中:ms为栖息地发生突变的概率,mmax为最大突变率,用户可自行设定 . ps为栖息地容纳s种物种的概率, pmax代表容纳最大种群的概率。

二、部分源代码

%      Biogeography-Based Optimization (BBO) trainer for MLP        %
%                      source codes version 1                       %
%                                                                   %

clc
clear all

% For modifying initial parameters please have a look at init.m file 

display('..........................................................................................')
display('BBO is training MLP ...')
display('..........................................................................................')
[cg_curve1,Hamming,best] = BBO(@MLP_Iris, 1, 1, 300); % BBO trainer
Best_W_B(1,:)=best;

display('..........................................................................................')
display('PSO is training MLP ...')
display('..........................................................................................')
[cg_curve2,best]= PSO(@MLP_Iris, 1);  % PSO trainer
Best_W_B(2,:)=best;

display('..........................................................................................')
display('GA is training MLP ...')
display('..........................................................................................')
[cg_curve3,best]= GA(@MLP_Iris, 1); % GA trainer
Best_W_B(3,:)=best;

display('..........................................................................................')
display('ACO is training MLP ...')
display('..........................................................................................')
[cg_curve4,best]= ACO(@MLP_Iris, 1); % ACO trainer
Best_W_B(4,:)=best;

display('..........................................................................................')
display('ES is training MLP ...')
display('..........................................................................................')
[cg_curve5,best]= ES(@MLP_Iris, 1); % ES trainer
Best_W_B(5,:)=best;

display('..........................................................................................')
display('PBIL is training MLP ...')
display('..........................................................................................')
[cg_curve6,best]=PBIL(@MLP_Iris, 1); % PBIL trainer
Best_W_B(6,:)=best;

 % Calculating classification rates
  
 load iris.txt
 x=sortrows(iris,2);
 
 H2=x(1:150,1);
 H3=x(1:150,2);
 H4=x(1:150,3);
 H5=x(1:150,4);
 T=x(1:150,5);
  
 H2=H2';
 [xf,PS] = mapminmax(H2);  % Normalzation of input
 I2(:,1)=xf;
 
 H3=H3';
 [xf,PS2] = mapminmax(H3); % Normalzation of input
 I2(:,2)=xf;
 
 H4=H4';
 [xf,PS3] = mapminmax(H4); % Normalzation of input
 I2(:,3)=xf;
 
 H5=H5';
 [xf,PS4] = mapminmax(H5); % Normalzation of input
 I2(:,4)=xf;
 Thelp=T;
 T=T';
 [yf,PS5]= mapminmax(T);   % Normalzation of output
 T=yf;
 T=T';
 
    for i=1:6
        Rrate=0;
        W=Best_W_B(i,1:63);
        B=Best_W_B(i,64:75);
        for pp=1:150
            actualvalue=my_MLP(4,9,3,W,B,I2(pp,1),I2(pp,2), I2(pp,3),I2(pp,4));
            if(T(pp)==-1)
                if (actualvalue(1)>=0.95 && actualvalue(2)<0.05 && actualvalue(3)<0.05)
                    Rrate=Rrate+1;
                end
            end
            if(T(pp)==0)
                if (actualvalue(1)<0.05 && actualvalue(2)>=0.95 && actualvalue(3)<0.05)
                    Rrate=Rrate+1;
                end  
            end
            if(T(pp)==1)
                if (actualvalue(1)<0.05 && actualvalue(2)<0.05 && actualvalue(3)>=0.95)
                    Rrate=Rrate+1;
                end              
            end
        end
        
        Final_Classification_Rates(1,i)=(Rrate/150)*100;
        
    end
    
 display('--------------------------------------------------------------------------------------------')
 display('Classification rate')
 display('   BBO       PSO       GA       ACO       ES       PBIL')
 display(Final_Classification_Rates(1:6))
 display('--------------------------------------------------------------------------------------------')
 

figure('Position',[500 500 660 290])
%Draw convergence curves
subplot(1,2,1);
hold on
title('Convergence Curves')
semilogy(cg_curve1,'Color','r')
semilogy(cg_curve2,'Color','k')
semilogy(cg_curve3,'Color','b')
semilogy(cg_curve4,'Color','r')
semilogy(cg_curve5,'Color','g')
semilogy(cg_curve6,'Color','c')
xlabel('Generation');
ylabel('MSE');

axis tight
grid on
box on
legend('BBO','PSO', 'GA', 'ACO', 'ES', 'PBIL')

%Draw classification rates
subplot(1,2,2);
hold on
title('Classification Accuracies')
bar(Final_Classification_Rates)
xlabel('Algorithm');
ylabel('Classification rate (%)');

grid on
box on
set(gca,'XTickLabel',{'BBO','PSO', 'GA', 'ACO', 'ES', 'PBIL'});

三、运行结果

在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本 2014a

2 参考文献 [1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016. [2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017.