一、重力引力搜索算法

 1.1粒子群算法

粒子群算法( Particle Swarm Optimization, PSO)最早是由Eberhart和Kennedy于1995年提出,它的基本概念源于对鸟群觅食行为的研究。设想这样一个场景:一群鸟在随机搜寻食物,在这个区域里只有一块食物,所有的鸟都不知道食物在哪里,但是它们知道当前的位置离食物还有多远。最简单有效的策略?寻找鸟群中离食物最近的个体来进行搜素。PSO算法就从这种生物种群行为特性中得到启发并用于求解优化问题。

用一种粒子来模拟上述的鸟类个体,每个粒子可视为N维搜索空间中的一个搜索个体,粒子的当前位置即为对应优化问题的一个候选解,粒子的飞行过程即为该个体的搜索过程.粒子的飞行速度可根据粒子历史最优位置和种群历史最优位置进行动态调整.粒子仅具有两个属性:速度和位置,速度代表移动的快慢,位置代表移动的方向。每个粒子单独搜寻的最优解叫做个体极值,粒子群中最优的个体极值作为当前全局最优解。不断迭代,更新速度和位置。最终得到满足终止条件的最优解。

算法流程如下:

 1、初始化

首先,我们设置最大迭代次数,目标函数的自变量个数,粒子的最大速度,位置信息为整个搜索空间,我们在速度区间和搜索空间上随机初始化速度和位置,设置粒子群规模为M,每个粒子随机初始化一个飞翔速度。

2、 个体极值与全局最优解

定义适应度函数,个体极值为每个粒子找到的最优解,从这些最优解找到一个全局值,叫做本次全局最优解。与历史全局最优比较,进行更新。

3、 更新速度和位置的公式

【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab
【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_02

【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_03
【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_02

4、 终止条件

(1)达到设定迭代次数;(2)代数之间的差值满足最小界限

【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_05
【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_02

以上就是最基本的一个标准PSO算法流程。和其它群智能算法一样,PSO算法在优化过程中,种群的多样性和算法的收敛速度之间始终存在着矛盾.对标准PSO算法的改进,无论是参数的选取、小生境技术的采用或是其他技术与PSO的融合,其目的都是希望在加强算法局部搜索能力的同时,保持种群的多样性,防止算法在快速收敛的同时出现早熟收敛。

1.2重力搜索算法

引力搜索算法(Gravitational Search Algorithm,GSA)是Esmat Rashedi等人在2009年提出的一种随机性启发式搜索算法,这种算法的灵感来自于牛顿的万有引力定律与运动定律:1.任意两个质点有通过连心线方向上的力相互吸引,该引力大小与它们质量的乘积成正比与它们距离的平方成反比。2.力使物体获得加速度。

在GSA中,质点被抽象成解空间的一个解,解之间存在一个相互吸引力,这个吸引力由解的质量与两个解之间的距离确定,质点的质量被抽象成解的评估函数值。在解空间中,每个解由其他解对其的吸引力获得加速度,质量更大(评估函数值更优)所提供的加速度更大,从而使解向更优解的方向移动。

看到这里,有些小伙伴会以为GSA是个与PSO差不多的算法,是的,GSA与PSO的外层框架是一致的,但是,它们最关键的粒子移动策略却是不同的:

1.在PSO中,粒子的移动只使用两个最佳位置pbest和gbest。但是在GSA中,智能体的方向是根据所有其他智能体或部分较优的智能体获得的总力来计算的。
2.PSO使用一种内存来更新速度(由于pbest和gbest)。然而,GSA是无内存的,只有智能体的当前位置在更新过程中起作用。
3.在PSO中,更新不考虑解之间的距离,而在GSA中,力与解之间的距离成反比。
4.PSO模拟了鸟类的社会行为,而GSA的灵感来自于物理现象。

GSA的主要过程如下:

1. 确定搜索空间。
2. 随机初始化个体种群。
3. 对种群进行适应度评价。
4. 更新引力常量G,更新种群中最好的个体 best与最差的个体worst,更新个体的质量。
5. 计算每个个体在不同方向上的总引力。
6. 计算个体的加速度,基于此更新个体的速度。
7. 根据速度更新个体在解空间中的位置。
8. 重复步骤3-7直到达到停止标准。

算法流程:

【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_07
【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_02

二、部分代码

%%%%%  Multi-layer Perceptron (MLP) Training using CPSOGSA %%%%%
%
% Citation
% Rather, S.A. and Bala, P.S. (2020), "A hybrid constriction coefficient-based particle swarm optimization and gravitational search algorithm for training multi-layer perceptron", 
% International Journal of Intelligent Computing and Cybernetics, Vol. 13 No. 2, pp. 129-165. https://doi.org/10.1108/IJICC-09-2019-0105  
%
%  Developed in MATLAB R2013b                                       %
                                                                 %


clear all 
close all
clc

Q=1;            % ACO Parameter
tau0=10;        % Initial Phromone             (ACO)
alpha=0.3;      % Phromone Exponential Weight  (ACO)
rho=0.1;        % Evaporation Rate             (ACO)
beta_min=0.2;   % Lower Bound of Scaling Factor (DE)
beta_max=0.8;   % Upper Bound of Scaling Factor (DE)
pCR=0.2;        % Crossover Probability         (DE)
Runno=10;

SearchAgents_no=20; % Number of search agents

% classification datasets

  Function_name='F1'; %MLP_XOR dataset
%  Function_name='F2'; %MLP_Baloon dataset
%  Function_name='F3'; %MLP_Iris dataset
% Function_name='F4'; %MLP_Cancer dataset
% Function_name='F5'; %MLP_Heart dataset

% Function approximation datasets

% Function_name='F6'; %MLP_Sigmoid dataset
% Function_name='F7'; %MLP_Cosine dataset
% Function_name='F8'; %MLP_Sine dataset
% Function_name='F9'; %MLP_Sphere dataset

% Load details of the selected data set
[lb,ub,dim,fobj]=Get_Functions_details(Function_name);

ElitistCheck=1;
min_flag=1;
Rpower=1;
Max_iteration=500; % Maximum numbef of iterations

% 
if Function_name=='F1' 
input=  [0 0 0 0 1 1 1 1;0 0 1 1 0 0 1 1;0 1 0 1 0 1 0 1];
target3=[0 1 1 0 1 0 0 1];
 Hno=7;
dim = 5*7+1;                      % Dimension of the problem
 
    for i=1:1:Runno
        
        Rrate=0;
        [Fbest,Lbest,BestChart]=GSA(SearchAgents_no,Max_iteration,ElitistCheck,min_flag,Rpower,lb,ub,dim,fobj);
        BestSolutions1(i) = Fbest;
        [gBestScore,gBest,GlobalBestCost]= CPSOGSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
        BestSolutions4(i) = gBestScore;
        [BestSolACO,BestAnt,BestCostACO] = ACO(SearchAgents_no, Max_iteration,Q,tau0,alpha,rho,lb,ub,dim,fobj);
        BestSolutions5(i) = BestSolACO.Cost;
        [BestCost,Best_Hab,BestSol] = bbo( SearchAgents_no, Max_iteration,lb,ub,dim,fobj);
        BestSolutions6(i) = BestSol.Cost;
        [BestSolDE,DBestSol,BestCostDE] = DE(SearchAgents_no, Max_iteration,beta_min,beta_max,pCR,lb,ub,dim,fobj);
        BestSolutions7(i) = BestSolDE.Cost ;

W=Lbest(1:4*Hno);
B=Lbest(4*Hno+1:dim);
W=gBest(1:4*Hno);
B=gBest(4*Hno+1:dim);
W=BestAnt(1:4*Hno);
B=BestAnt(4*Hno+1:dim);
W=Best_Hab(1:4*Hno);
B=Best_Hab(4*Hno+1:dim);
W=DBestSol(1:4*Hno);
B=DBestSol(4*Hno+1:dim);

        for pp=1:8
            actualvalue=my_simulate(3,Hno,1,W,B,input(:,pp)');
            if(target3(pp)==1)
                if (actualvalue>=0.95)
                    Rrate=Rrate+1;
                end
            end
            if(target3(pp)==0)
                if (actualvalue(1)<0.05)
                    Rrate=Rrate+1;
                end  
            end
        end
         
        Classification_rate(i)=(Rrate/8)*100;
        
  disp(['GSA is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(Fbest),' Mean_Classification rate = ' , num2str(Classification_rate(i)),')'])   
% disp(['CPSOGSA is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(gBestScore),' Mean_Classification rate = ' , num2str(Classification_rate(i)),')'])
% disp(['ACO is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSolACO.Cost),' Mean_Classification rate =' , num2str(Classification_rate(i)),')'])
% disp(['BBO is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSol.Cost),' Mean_Classification rate = ' , num2str(Classification_rate(i)),')'])
% disp(['DE is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSolDE.Cost),' Mean_Classification rate = ' , num2str(Classification_rate(i)),')']) 
     end

    A_Classification_rate=mean(Classification_rate);
    Average= mean(BestSolutions1);
    StandDP=std(BestSolutions1);
    Med = median(BestSolutions1); 
    [BestValueP I] = min(BestSolutions1);
    [WorstValueP IM]=max(BestSolutions1);
 end
    
%     
% if Function_name=='F2'
% 
% load baloon.txt
%  x=sortrows(baloon,2);
%  %I2=x(1:150,1:4);
%  I2(:,1)=x(1:20,1);
%  I2(:,2)=x(1:20,2);
%  I2(:,3)=x(1:20,3);
%  I2(:,4)=x(1:20,4);
%  T=x(1:20,5);
%  
% 
% Hno=9;
% dim = 6*9+1;
%  
%    for i=1:1:Runno
%         [Fbest,Lbest,BestChart]=GSA(SearchAgents_no,Max_iteration,ElitistCheck,min_flag,Rpower,lb,ub,dim,fobj);
%         BestSolutions1(i) = Fbest;
%          [gBestScore,gBest,GlobalBestCost]= CPSOGSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
%          BestSolutions4(i) = gBestScore;
%         [BestSolACO,BestAnt,BestCostACO] = ACO(SearchAgents_no, Max_iteration,Q,tau0,alpha,rho,lb,ub,dim,fobj);
%         BestSolutions5(i) = BestSolACO.Cost;
%         [BestCost,Best_Hab,BestSol] = bbo( SearchAgents_no, Max_iteration,lb,ub,dim,fobj);
%         BestSolutions6(i) = BestSol.Cost;
%         [BestSolDE,DBestSol,BestCostDE] = DE(SearchAgents_no, Max_iteration,beta_min,beta_max,pCR,lb,ub,dim,fobj);
%         BestSolutions7(i) = BestSolDE.Cost ;
%         Rrate=0;

%W=Lbest(1:45);
%B=Lbest(46:55);
%W=gBest(1:45);
%B=gBest(46:55);
%W=BestAnt(1:45);
% B=BestAnt(46:55);
% W=Best_Hab(1:45);
% B=Best_Hab(46:55);
% W=DBestSol(1:45);
% B=DBestSol(46:55);

%         for pp=1:20
%             actualvalue=my_simulate(4,9,1,W,B,I2(pp,:));
%             if(T(pp)==1)
%                 if (actualvalue>=0.95)
%                     Rrate=Rrate+1;
%                 end
%             end
%             if(T(pp)==0)
%                 if (actualvalue(1)<0.05)
%                     Rrate=Rrate+1;
%                 end  
%             end
% 
%         end
% end 
% end
% % % 
%  if Function_name=='F3' 
%     
%     load iris.txt;
%  x=sortrows(iris,2);
%  I2=x(1:150,1:4);
%  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);
%  I=(I2-0.1)./(7.9-0.1);
%  H2=H2';
%  [xf,PS] = mapminmax(H2);
%  I2(:,1)=xf;
%  
%  H3=H3';
%  [xf,PS2] = mapminmax(H3);
%  I2(:,2)=xf;
%  
%  H4=H4';
%  [xf,PS3] = mapminmax(H4);
%  I2(:,3)=xf;
%  
%  H5=H5';
%  [xf,PS4] = mapminmax(H5);
%  I2(:,4)=xf;
%  Thelp=T;
%  T=T';
%  [yf,PS5]= mapminmax(T);
%  T=yf;
%  T=T';
%  
%     for i=1:1:Runno
%         [Fbest,Lbest,BestChart]=GSA(SearchAgents_no,Max_iteration,ElitistCheck,min_flag,Rpower,lb,ub,dim,fobj);
%         BestSolutions1(i) = Fbest;
%          [gBestScore,gBest,GlobalBestCost]= CPSOGSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
%          BestSolutions4(i) = gBestScore;
%         [BestSolACO,BestAnt,BestCostACO] = ACO(SearchAgents_no, Max_iteration,Q,tau0,alpha,rho,lb,ub,dim,fobj);
%         BestSolutions5(i) = BestSolACO.Cost;
%         [BestCost,Best_Hab,BestSol] = bbo( SearchAgents_no, Max_iteration,lb,ub,dim,fobj);
%         BestSolutions6(i) = BestSol.Cost;
%         [BestSolDE,DBestSol,BestCostDE] = DE(SearchAgents_no, Max_iteration,beta_min,beta_max,pCR,lb,ub,dim,fobj);
%         BestSolutions7(i) = BestSolDE.Cost ;

%         Rrate=0;
%             W=Lbest(1:63);
%             B=Lbest(64:75);
%             W=gBest(1:63);
%             B=gBest(64:75);
%             W=BestAnt(1:63);
%             B=BestAnt(64:75);
%             W=Best_Hab(1:63);
%             B=Best_Hab(64:75);
%             W=DBestSol(1:63);
%             B=DBestSol(64:75);

%         for pp=1:150
%             actualvalue=my_simulate(4,9,3,W,B,I2(pp,:));
%             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
% end
% end
% 
% if Function_name=='F4'
%     
%     load Cancer.txt
%  x=Cancer;
%  %I2=x(1:150,1:4);
%  H2=x(1:699,2:11);
%  for iii=1:699
%      for jjj=1:10
%          H2(iii,jjj)=((H2(iii,jjj)-1)/9);
%      end
%  end
%  I2=H2(1:699,1:9);
%  
%  T=H2(1:699,10);
%  Hno=19;
%  dim=11*19;
%  
%     for i=1:1:Runno
%         Rrate=0;
%         [Fbest,Lbest,BestChart]=GSA(SearchAgents_no,Max_iteration,ElitistCheck,min_flag,Rpower,lb,ub,dim,fobj);
%         BestSolutions1(i) = Fbest;
%          [gBestScore,gBest,GlobalBestCost]= CPSOGSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
%          BestSolutions4(i) = gBestScore;
%         [BestSolACO,BestAnt,BestCostACO] = ACO(SearchAgents_no, Max_iteration,Q,tau0,alpha,rho,lb,ub,dim,fobj);
%         BestSolutions5(i) = BestSolACO.Cost;
%         [BestCost,Best_Hab,BestSol] = bbo( SearchAgents_no, Max_iteration,lb,ub,dim,fobj);
%         BestSolutions6(i) = BestSol.Cost;
%         [BestSolDE,DBestSol,BestCostDE] = DE(SearchAgents_no, Max_iteration,beta_min,beta_max,pCR,lb,ub,dim,fobj);
%         BestSolutions7(i) = BestSolDE.Cost ;

% W=Lbest(1:10*Hno);
% B=Lbest(10*Hno+1:dim);
% W=gBest(1:10*Hno);
% B=gBest(10*Hno+1:dim);
% W=BestAnt(1:10*Hno);
% B=BestAnt(10*Hno+1:dim);
% W=Best_Hab(1:10*Hno);
% B=Best_Hab(10*Hno+1:dim);
% W=DBestSol(1:10*Hno);
% B=DBestSol(10*Hno+1:dim);

%         for pp=600:699
%             actualvalue=my_simulate(9,Hno,1,W,B,I2(pp,:) );
%             if(T(pp)>=0.3 && T(pp)<0.4)
%                 if (abs(actualvalue-0.333333333333333)<0.1)
%                     Rrate=Rrate+1;
%                 end
%             end
%             if(T(pp)>=0.1 && T(pp)<0.2)
%                 if (abs(actualvalue-0.111111111111111)<0.1)
%                     Rrate=Rrate+1;
%                 end  
%             end
% 
%         end
%   end
% end

    
% if Function_name=='F5'
% 
% load Heart.txt
%  x=Heart;
% % I2=x(1:150,1:4);
%  I2(:,1)=x(1:80,2);
%  I2(:,2)=x(1:80,3);
%  I2(:,3)=x(1:80,4);
%  I2(:,4)=x(1:80,5);
%  I2(:,5)=x(1:80,6);
%  I2(:,6)=x(1:80,7);
%  I2(:,7)=x(1:80,8);
%  I2(:,8)=x(1:80,9);
%  I2(:,9)=x(1:80,10);
%  I2(:,10)=x(1:80,11);
%  I2(:,11)=x(1:80,12);
%  I2(:,12)=x(1:80,13);
%  I2(:,13)=x(1:80,14);
%  I2(:,14)=x(1:80,15);
%  I2(:,15)=x(1:80,16);
%  I2(:,16)=x(1:80,17);
%  I2(:,17)=x(1:80,18);
%  I2(:,18)=x(1:80,19);
%  I2(:,19)=x(1:80,20);
%  I2(:,20)=x(1:80,21);
%  I2(:,21)=x(1:80,22); 
%  I2(:,22)=x(1:80,23);  
%  T=x(1:80,1);
% 
%  Hno=45;
% dim = 24*45+1;    
%  
%     for i=1:1:Runno
% 
%         Rrate=0;
%         [Fbest,Lbest,BestChart]=GSA(SearchAgents_no,Max_iteration,ElitistCheck,min_flag,Rpower,lb,ub,dim,fobj);
%         BestSolutions1(i) = Fbest;
%         [gBestScore,gBest,GlobalBestCost]= CPSOGSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
%         BestSolutions4(i) = gBestScore;
%         [BestSolACO,BestAnt,BestCostACO] = ACO(SearchAgents_no, Max_iteration,Q,tau0,alpha,rho,lb,ub,dim,fobj);
%         BestSolutions5(i) = BestSolACO.Cost;
%         [BestCost,Best_Hab,BestSol] = bbo( SearchAgents_no, Max_iteration,lb,ub,dim,fobj);
%         BestSolutions6(i) = BestSol.Cost;
%         [BestSolDE,DBestSol,BestCostDE] = DE(SearchAgents_no, Max_iteration,beta_min,beta_max,pCR,lb,ub,dim,fobj);
%         BestSolutions7(i) = BestSolDE.Cost ;

% W=Lbest(1:23*Hno);
% B=Lbest(23*Hno+1:dim);
% W=gBest(1:23*Hno);
% B=gBest(23*Hno+1:dim);
% W=BestAnt(1:23*Hno);
% B=BestAnt(23*Hno+1:dim);
% W=Best_Hab(1:23*Hno);
% B=Best_Hab(23*Hno+1:dim);
% W=DBestSol(1:23*Hno);
% B=DBestSol(23*Hno+1:dim);

%         for pp=1:80
%             actualvalue=my_simulate(22,Hno,1,W,B,I2(pp,:) );
%             if(T(pp)==1)
%                 if (actualvalue>=0.95)
%                     Rrate=Rrate+1;
%                 end
%             end
%             if(T(pp)==0)
%                 if (actualvalue(1)<0.05)
%                     Rrate=Rrate+1;
%                 end  
%             end
% 
%         end
%      end
% end


% if Function_name=='F6'  %% Sigmoid
%     
%     Hnode=15;
% dim = 3*Hnode+1;
%  
% %for test 3 times more than the training samples
% %   xf1=[0:0.01:pi];
% %   yf1=sin(2.*xf1);
% %   yf1=yf1.*exp(-xf1);
%  xf1=[-3:0.05:3];
% %  yf1=sin(2.*xf1);
% %  yf1=yf1.*exp(-xf1);
%  %yf1=xf1.^2;
% %yf1=xf1.^4-6.*xf1.^2+3;
% yf1=sigmf(xf1,[1 0]);
% 
% %   xf1=[-2*pi:0.05:2*pi];
% %   yf1=sin(2.*xf1);
%   %yf1=yf1.*exp(-xf1);
%  yNN=zeros(1,10);
%  [xf,PS] = mapminmax(xf1);
%  [yf,PS2]= mapminmax(yf1);
%   [M N]=size(xf);
%   test_error=zeros(1,Runno);
%     for i=1:1:Runno
%         [Fbest,Lbest,BestChart]=GSA(SearchAgents_no,Max_iteration,ElitistCheck,min_flag,Rpower,lb,ub,dim,fobj);
%         BestSolutions1(i) = Fbest;
%          [gBestScore,gBest,GlobalBestCost]= CPSOGSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
%          BestSolutions4(i) = gBestScore;
%         [BestSolACO,BestAnt,BestCostACO] = ACO(SearchAgents_no, Max_iteration,Q,tau0,alpha,rho,lb,ub,dim,fobj);
%         BestSolutions5(i) = BestSolACO.Cost;
%         [BestCost,Best_Hab,BestSol] = bbo( SearchAgents_no, Max_iteration,lb,ub,dim,fobj);
%         BestSolutions6(i) = BestSol.Cost;
%         [BestSolDE,DBestSol,BestCostDE] = DE(SearchAgents_no, Max_iteration,beta_min,beta_max,pCR,lb,ub,dim,fobj);
%         BestSolutions7(i) = BestSolDE.Cost ;

% W=1:2*Hnode;
% B=2*Hnode+1:3*Hnode+1;
% W=gBest(1:2*Hnode);
% B=gBest(2*Hnode+1:3*Hnode+1);
% W=BestAnt(1:2*Hnode);
% B=BestAnt(2*Hnode+1:3*Hnode+1);
% W=Best_Hab(1:2*Hnode);
% B=Best_Hab(2*Hnode+1:3*Hnode+1);
% W=DBestSol(1:2*Hnode);
% B=DBestSol(2*Hnode+1:3*Hnode+1);
% 
%         for pp=1:N
%             yNN(pp)=my_simulate(1,15,1, W,B,xf(pp));
%         end       
% 
% disp(['GSA is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(Fbest),')'])  
% disp(['CPSOGSA is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(gBestScore), ')'])
% disp(['ACO is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSolACO.Cost),')'])
% disp(['BBO is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSol.Cost),')'])
% disp(['DE is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSolDE.Cost),')'])
%     end
% end
  
% if Function_name=='F7' %% Cosine
%  
% Hnode=15;
% dim = 3*Hnode+1;
%  
% %for test 3 times more than the training samples
% %   xf1=[0:0.01:pi];
% %   yf1=sin(2.*xf1);
% %   yf1=yf1.*exp(-xf1);
%  %xf1=[-3:0.05:3];
% %  yf1=sin(2.*xf1);
% %  yf1=yf1.*exp(-xf1);
%  %yf1=xf1.^2;
% %yf1=xf1.^4-6.*xf1.^2+3;
% 
%   xf1=[1.25:0.04:2.75];
%   yf1=power(cos(xf1.*pi/2),7);
%   %yf1=yf1.*exp(-xf1);
%  yNN=zeros(1,10);
%  [xf,PS] = mapminmax(xf1);
%  [yf,PS2]= mapminmax(yf1);
%   [M N]=size(xf);
%   test_error=zeros(1,Runno);
%     for i=1:1:Runno
%         [Fbest,Lbest,BestChart]=GSA(SearchAgents_no,Max_iteration,ElitistCheck,min_flag,Rpower,lb,ub,dim,fobj);
%         BestSolutions1(i) = Fbest;
%         [gBestScore,gBest,GlobalBestCost]= CPSOGSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
%         BestSolutions4(i) = gBestScore;
%         [BestSolACO,BestAnt,BestCostACO] = ACO(SearchAgents_no, Max_iteration,Q,tau0,alpha,rho,lb,ub,dim,fobj);
%         BestSolutions5(i) = BestSolACO.Cost;
%         [BestCost,Best_Hab,BestSol] = bbo( SearchAgents_no, Max_iteration,lb,ub,dim,fobj);
%         BestSolutions6(i) = BestSol.Cost;
%         [BestSolDE,DBestSol,BestCostDE] = DE(SearchAgents_no, Max_iteration,beta_min,beta_max,pCR,lb,ub,dim,fobj);
%         BestSolutions7(i) = BestSolDE.Cost ;
%         
% W= Lbest(1:2*Hnode);
% B=Lbest(2*Hnode+1:3*Hnode+1);
% W=gBest(1:2*Hnode);
% B=gBest(2*Hnode+1:3*Hnode+1);
% W=BestAnt(1:2*Hnode);
% B=BestAnt(2*Hnode+1:3*Hnode+1);
% W=Best_Hab(1:2*Hnode);
% B=Best_Hab(2*Hnode+1:3*Hnode+1);
% W=DBestSol(1:2*Hnode);
% B=DBestSol(2*Hnode+1:3*Hnode+1);

%         for pp=1:N
%             yNN(pp)=my_simulate(1,15,1, W,B,xf(pp));
%         end
% %           
% disp(['GSA is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(Fbest)  
% disp(['CPSOGSA is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(gBestScore), ')'])
% disp(['ACO is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSolACO.Cost),')'])
% disp(['BBO is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSol.Cost),')'])
% disp(['DE is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSolDE.Cost),')'])
%     end
% end


% if Function_name=='F8' %%Sine
% 
% Hnode=15;
% dim = 3*Hnode+1;
%  
% %for test 3 times more than the training samples
% %   xf1=[0:0.01:pi];
% %   yf1=sin(2.*xf1);
% %   yf1=yf1.*exp(-xf1);
%  %xf1=[-3:0.05:3];
% %  yf1=sin(2.*xf1);
% %  yf1=yf1.*exp(-xf1);
%  %yf1=xf1.^2;
% %yf1=xf1.^4-6.*xf1.^2+3;
% 
%   xf1=[-2*pi:0.05:2*pi];
%   yf1=sin(2.*xf1);
%   %yf1=yf1.*exp(-xf1);
%  yNN=zeros(1,10);
%  [xf,PS] = mapminmax(xf1);
%  [yf,PS2]= mapminmax(yf1);
%   [M N]=size(xf);
%   test_error=zeros(1,Runno);
%     for i=1:1:Runno
% %         [Fbest,Lbest,BestChart]=GSA(SearchAgents_no,Max_iteration,ElitistCheck,min_flag,Rpower,lb,ub,dim,fobj);
% %         BestSolutions1(i) = Fbest;
% %         [gBestScore,gBest,GlobalBestCost]= CPSOGSA(SearchAgents_no,Max_iteration,lb,ub,dim,fobj);
% %         BestSolutions4(i) = gBestScore;
% %         [BestSolACO,BestAnt,BestCostACO] = ACO(SearchAgents_no, Max_iteration,Q,tau0,alpha,rho,lb,ub,dim,fobj);
% %         BestSolutions5(i) = BestSolACO.Cost;
% %         [BestCost,Best_Hab,BestSol] = bbo( SearchAgents_no, Max_iteration,lb,ub,dim,fobj);
% %         BestSolutions6(i) = BestSol.Cost;
%         [BestSolDE,DBestSol,BestCostDE] = DE(SearchAgents_no, Max_iteration,beta_min,beta_max,pCR,lb,ub,dim,fobj);
%         BestSolutions(i) = BestSolDE.Cost ;
% % 
% W=Lbest(1:2*Hnode);
% B= Lbest(2*Hnode+1:3*Hnode+1);
% W=gBest(1:2*Hnode);
% B=gBest(2*Hnode+1:3*Hnode+1);
% W=BestAnt(1:2*Hnode);
% B=BestAnt(2*Hnode+1:3*Hnode+1);
% W=Best_Hab(1:2*Hnode);
% B=Best_Hab(2*Hnode+1:3*Hnode+1);
% W=DBestSol(1:2*Hnode);
% B=DBestSol(2*Hnode+1:3*Hnode+1);
%         
%         for pp=1:N
%             yNN(pp)=my_simulate(1,15,1, W,B,xf(pp));
%         end
% %           
% disp(['GSA is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(Fbest),')'])   
% disp(['CPSOGSA is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(gBestScore), ')'])
% disp(['ACO is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSolACO.Cost),')'])
% disp(['BBO is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSol.Cost),')'])
% disp(['DE is training FNN (Run # = ', num2str(i),' ,MSE = ', num2str(BestSolDE.Cost),')'])

%     end
% end

% % A_Classification_rate=mean(Classification_rate);
%     Average= mean(BestSolutions);
%     StandDP=std(BestSolutions);
%     Med = median(BestSolutions); 
%     [BestValueP I] = min(BestSolutions);
%     [WorstValueP IM]=max(BestSolutions);
% end




% figure
%         set(axes,'FontName','Times New Roman');
%         hold on
%         grid on;
%         xfDenorm = mapminmax('reverse',xf,PS); 
%         yfDenorm = mapminmax('reverse',yNN,PS2);
%         test_error(1,i)=test_error(1,i)+sum(abs( yfDenorm- yf1 )); 
%         A_Test_Error=mean(test_error);
%         plot(xf1,yf1,'DisplayName','Real curve','Color','b');
%         plot(xfDenorm,yfDenorm,'DisplayName','Approximated curve','Marker','.','LineStyle','-','Color','r');
%         %eqtext = '$$sin(2x)e^{-x}$$'; 
%         
%         name='GSA'
% 
%         title([['\fontsize{12}\it ', name]],'FontName','Times New Roman');
%         xlabel('\fontsize{12}\it X');
%         ylabel('\fontsize{12}\it Y');
%         legend('toggle');
%         set(legend,'FontAngle','italic','FontName','Times New Roman') 


        
disp(['Best=',num2str( BestValueP)])
disp(['Worst=',num2str(WorstValueP)])
disp(['Average=',num2str( Average)])
disp(['Standard_Deviation=',num2str( StandDP)])
disp(['Median=',num2str(Med)])
% % disp(['Mean_Test_Error = ' , num2str(A_Test_Error)])


  figure
   semilogy(1:Max_iteration,BestChart,'DisplayName','GSA','Color','g','Marker','o','LineStyle','-','LineWidth',2,...
        'MarkerEdgeColor','g','MarkerFaceColor',[.49 1 .63],'MarkerSize',5);
   hold on

   semilogy(1:Max_iteration,GlobalBestCost,'DisplayName','CPSOGSA', 'Color', 'r','Marker','diamond','LineStyle','-','LineWidth',2,...
       'MarkerEdgeColor','r','MarkerFaceColor',[.49 1 .63],'MarkerSize',5);
   semilogy(1:Max_iteration,BestCostACO,'DisplayName','ACO','Color','c','Marker','square','LineStyle','-','LineWidth',2,...
    'MarkerEdgeColor','c','MarkerFaceColor',[.49 1 .63],'MarkerSize',5);
   semilogy(1:Max_iteration,BestCost,'DisplayName','BBO','Color','b','Marker','*','LineStyle','-','LineWidth',2,...
       'MarkerEdgeColor','b','MarkerFaceColor',[.49 1 .63],'MarkerSize',5);
   semilogy(1:Max_iteration,BestCostDE,'DisplayName','DE','Color','y','Marker','+','LineStyle','-','LineWidth',2,...
       'MarkerEdgeColor','y','MarkerFaceColor',[.49 1 .63],'MarkerSize',5);
    
    title ('\fontsize{12}\bf XOR Dataset');
 % title ('\fontsize{12}\bf Baloon Dataset');
 % title ('\fontsize{12}\bf Iris Dataset');
 % title ('\fontsize{12}\bf Cancer Dataset');
 % title ('\fontsize{12}\bf Heart Dataset');
 % title ('\fontsize{12}\bf Sigmoid Dataset');
 % title ('\fontsize{12}\bf Cosine Dataset');
 % title ('\fontsize{12}\bf Sine Dataset');
 
xlabel('\fontsize{12}\bf Iteration');
ylabel('\fontsize{12}\bf log(MSE)');

legend('\fontsize{10}\bf GSA','\fontsize{10}\bf CPSOGSA','\fontsize{10}\bf ACO','\fontsize{10}\bf BBO','\fontsize{10}\bf DE',1);

axis tight
box on
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
  • 278.
  • 279.
  • 280.
  • 281.
  • 282.
  • 283.
  • 284.
  • 285.
  • 286.
  • 287.
  • 288.
  • 289.
  • 290.
  • 291.
  • 292.
  • 293.
  • 294.
  • 295.
  • 296.
  • 297.
  • 298.
  • 299.
  • 300.
  • 301.
  • 302.
  • 303.
  • 304.
  • 305.
  • 306.
  • 307.
  • 308.
  • 309.
  • 310.
  • 311.
  • 312.
  • 313.
  • 314.
  • 315.
  • 316.
  • 317.
  • 318.
  • 319.
  • 320.
  • 321.
  • 322.
  • 323.
  • 324.
  • 325.
  • 326.
  • 327.
  • 328.
  • 329.
  • 330.
  • 331.
  • 332.
  • 333.
  • 334.
  • 335.
  • 336.
  • 337.
  • 338.
  • 339.
  • 340.
  • 341.
  • 342.
  • 343.
  • 344.
  • 345.
  • 346.
  • 347.
  • 348.
  • 349.
  • 350.
  • 351.
  • 352.
  • 353.
  • 354.
  • 355.
  • 356.
  • 357.
  • 358.
  • 359.
  • 360.
  • 361.
  • 362.
  • 363.
  • 364.
  • 365.
  • 366.
  • 367.
  • 368.
  • 369.
  • 370.
  • 371.
  • 372.
  • 373.
  • 374.
  • 375.
  • 376.
  • 377.
  • 378.
  • 379.
  • 380.
  • 381.
  • 382.
  • 383.
  • 384.
  • 385.
  • 386.
  • 387.
  • 388.
  • 389.
  • 390.
  • 391.
  • 392.
  • 393.
  • 394.
  • 395.
  • 396.
  • 397.
  • 398.
  • 399.
  • 400.
  • 401.
  • 402.
  • 403.
  • 404.
  • 405.
  • 406.
  • 407.
  • 408.
  • 409.
  • 410.
  • 411.
  • 412.
  • 413.
  • 414.
  • 415.
  • 416.
  • 417.
  • 418.
  • 419.
  • 420.
  • 421.
  • 422.
  • 423.
  • 424.
  • 425.
  • 426.
  • 427.
  • 428.
  • 429.
  • 430.
  • 431.
  • 432.
  • 433.
  • 434.
  • 435.
  • 436.
  • 437.
  • 438.
  • 439.
  • 440.
  • 441.
  • 442.
  • 443.
  • 444.
  • 445.
  • 446.
  • 447.
  • 448.
  • 449.
  • 450.
  • 451.
  • 452.
  • 453.
  • 454.
  • 455.
  • 456.
  • 457.
  • 458.
  • 459.
  • 460.
  • 461.
  • 462.
  • 463.
  • 464.
  • 465.
  • 466.
  • 467.
  • 468.
  • 469.
  • 470.
  • 471.
  • 472.
  • 473.
  • 474.
  • 475.
  • 476.
  • 477.
  • 478.
  • 479.
  • 480.
  • 481.
  • 482.
  • 483.
  • 484.
  • 485.
  • 486.
  • 487.
  • 488.
  • 489.
  • 490.
  • 491.
  • 492.
  • 493.
  • 494.
  • 495.
  • 496.
  • 497.
  • 498.
  • 499.
  • 500.
  • 501.
  • 502.
  • 503.
  • 504.
  • 505.
  • 506.
  • 507.
  • 508.
  • 509.
  • 510.
  • 511.
  • 512.
  • 513.
  • 514.
  • 515.
  • 516.
  • 517.
  • 518.
  • 519.
  • 520.
  • 521.
  • 522.
  • 523.
  • 524.
  • 525.
  • 526.
  • 527.
  • 528.
  • 529.
  • 530.
  • 531.
  • 532.
  • 533.
  • 534.
  • 535.
  • 536.
  • 537.
  • 538.
  • 539.
  • 540.
  • 541.
  • 542.
  • 543.
  • 544.
  • 545.
  • 546.
  • 547.
  • 548.
  • 549.
  • 550.
  • 551.
  • 552.
  • 553.
  • 554.
  • 555.
  • 556.
  • 557.
  • 558.
  • 559.
  • 560.
  • 561.
  • 562.
  • 563.
  • 564.
  • 565.
  • 566.
  • 567.
  • 568.
  • 569.
  • 570.
  • 571.
  • 572.
  • 573.
  • 574.
  • 575.
  • 576.
  • 577.
  • 578.
  • 579.
  • 580.
  • 581.
  • 582.
  • 583.
  • 584.
  • 585.
  • 586.
  • 587.
  • 588.
  • 589.
  • 590.
  • 591.
  • 592.
  • 593.
  • 594.
  • 595.
  • 596.
  • 597.
  • 598.
  • 599.
  • 600.
  • 601.
  • 602.
  • 603.
  • 604.
  • 605.
  • 606.
  • 607.
  • 608.
  • 609.
  • 610.
  • 611.
  • 612.
  • 613.
  • 614.
  • 615.
  • 616.
  • 617.
  • 618.
  • 619.
  • 620.
  • 621.
  • 622.
  • 623.
  • 624.
  • 625.
  • 626.
  • 627.
  • 628.
  • 629.
【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_02

三、仿真结果

【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_10
【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_02

四、参考文献

 Rather, S.A. and Bala, P.S. (2020), "A hybrid constriction coefficient-based particle swarm optimization and gravitational search algorithm for training multi-layer perceptron", International Journal of Intelligent Computing and Cybernetics, Vol. 13 No. 2, pp. 129-165. https://doi.org/10.1108/IJICC-09-2019-0105  

【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_12
【优化求解】粒子群优化和重力搜索算法求解MLP问题matlab源码_matlab_02