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

一、重力引力搜索算法

 1.1粒子群算法

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

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

算法流程如下:

 1、初始化

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

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

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

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

4、 终止条件

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

以上就是最基本的一个标准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直到达到停止标准。

算法流程:
在这里插入图片描述

二、部分代码

                                    
                                
                                %%%%%  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



三、仿真结果

 

四、参考文献

 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  

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Matlab科研辅导帮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值