GSA(引力搜索)优化算法MATLAB源码详细中文注解

以优化SVM算法的参数c和g为例,对GSA(引力搜索)算法MATLAB源码进行了详细中文注解。
完整程序和示例文件地址:http://download.csdn.net/detail/u013337691/9645312

tic % 计时器
%% 清空环境变量
close all
clear
clc
format compact
%% 数据提取
% 载入测试数据wine,其中包含的数据为classnumber = 3,wine:178*13的矩阵,wine_labes:178*1的列向量
load wine.mat
% 选定训练集和测试集
% 将第一类的1-30,第二类的60-95,第三类的131-153做为训练集
train_wine = [wine(1:30,:);wine(60:95,:);wine(131:153,:)];
% 相应的训练集的标签也要分离出来
train_wine_labels = [wine_labels(1:30);wine_labels(60:95);wine_labels(131:153)];
% 将第一类的31-59,第二类的96-130,第三类的154-178做为测试集
test_wine = [wine(31:59,:);wine(96:130,:);wine(154:178,:)];
% 相应的测试集的标签也要分离出来
test_wine_labels = [wine_labels(31:59);wine_labels(96:130);wine_labels(154:178)];
%% 数据预处理
% 数据预处理,将训练集和测试集归一化到[0,1]区间
[mtrain,ntrain] = size(train_wine);
[mtest,ntest] = size(test_wine);

dataset = [train_wine;test_wine];
% mapminmax为MATLAB自带的归一化函数
[dataset_scale,ps] = mapminmax(dataset',0,1);
dataset_scale = dataset_scale';

train_wine = dataset_scale(1:mtrain,:);
test_wine = dataset_scale( (mtrain+1):(mtrain+mtest),: );
%% GSA优化参数 
N=20; % 群体规模 Number of agents.
max_it=30; % 最大迭代次数 Maximum number of iterations (T).
ElitistCheck=1; % 如果ElitistCheck=1,则使用文献中的公式21;如果ElitistCheck=0,则用文献中的公式9.
Rpower=1;% 文献中公式7中的R的幂次数 power of 'R' in eq.7.
min_flag=1; % 取1求解极小值问题,取0求解极大值问题 1: minimization, 0: maximization.
objfun=@objfun_svm; % 目标函数
[Fbest,Lbest,BestChart,MeanChart]=GSA_svm(objfun,N,max_it,ElitistCheck,min_flag,Rpower,...
    train_wine_labels,train_wine,test_wine_labels,test_wine);
% Fbest: 最优目标值 Best result. 
% Lbest: 最优解 Best solution. The location of Fbest in search space.
% BestChart: 最优解变化趋势 The best so far Chart over iterations. 
% MeanChart: 平均适应度函数值变化趋势 The average fitnesses Chart over iterations.
%% 打印参数选择结果
bestc=Lbest(1);
bestg=Lbest(2);
disp('打印选择结果');
str=sprintf('Best c = %g,Best g = %g',bestc,bestg);
disp(str)
%% 利用最佳的参数进行SVM网络训练
cmd_gwosvm = ['-c ',num2str(bestc),' -g ',num2str(bestg)];
model_gwosvm = svmtrain(train_wine_labels,train_wine,cmd_gwosvm);
%% SVM网络预测
[predict_label,accuracy] = svmpredict(test_wine_labels,test_wine,model_gwosvm);
% 打印测试集分类准确率
total = length(test_wine_labels);
right = sum(predict_label == test_wine_labels);
disp('打印测试集分类准确率');
str = sprintf( 'Accuracy = %g%% (%d/%d)',accuracy(1),right,total);
disp(str);
%% 结果分析
% 测试集的实际分类和预测分类图
figure;
hold on;
plot(test_wine_labels,'o');
plot(predict_label,'r*');
xlabel('测试集样本','FontSize',12);
ylabel('类别标签','FontSize',12);
legend('实际测试集分类','预测测试集分类');
title('测试集的实际分类和预测分类图','FontSize',12);
grid on
%% 最优适应度变化曲线
figure('Name','最优适应度变化曲线')
plot(BestChart,'--k');
title('最优适应度变化曲线');
xlabel('\fontsize{12}\bf Iteration');ylabel('\fontsize{12}\bf Best-so-far');
legend('\fontsize{10}\bf GSA',1);
%% 显示程序运行时间
toc
function [Fbest,Lbest,BestChart,MeanChart]=GSA_svm(objfun,N,max_it,ElitistCheck,min_flag,Rpower,...
    train_wine_labels,train_wine,test_wine_labels,test_wine)
% 说明
% Main function for Gravitational Search Algorithm.
% V: 速度 Velocity.
% a: 加速度 Acceleration.
% M: 惯性质量 Mass. Ma=Mp=Mi=M;
% dim: 自变量维度 Dimension of the test function.
% N: 种群规模 Number of agents.
% X: 个体位置集,一个N*dim矩阵 Position of agents. dim-by-N matrix.
% R: 个体距离 Distance between agents in search space.
% [low-up]: 参数范围 Allowable range for search space.
% Rnorm: 范数,参考文献公式8 Norm in eq.8.
% Rpower: 参考文献公式7 Power of R in eq.7.

Rnorm=2; % 使用二阶范数 
% 获取目标函数参数界限、维数 get allowable range and dimension of the test function.
low=0.01;
up=100;
dim=2;
% 随机初始化种群 random initialization for agents.
X=initialization(dim,N,up,low); 
% 用于保存当前最优值和平均适应度值变化情况 create the best so far chart and average fitnesses chart.
BestChart=zeros(1,max_it);
MeanChart=zeros(1,max_it);
% 初始化个体解
V=zeros(N,dim);
for iteration=1:max_it
    % 检查是否越界 Checking allowable range. 
    X=space_bound(X,up,low);
    % 计算个体适应度函数值 Evaluation of agents.
    fitness=zeros(1,N);
    for agent=1:N
        fitness(1,agent)=objfun(X(agent,:),train_wine_labels,train_wine,test_wine_labels,test_wine);
    end
    % 寻找当前迭代最优个体
    if min_flag==1
        [best,best_X]=min(fitness); % 最小化情况 minimization.
    else
        [best,best_X]=max(fitness); % 最大化情况 maximization.
    end
    if iteration==1
       Fbest=best;
       Lbest=X(best_X,:);
    end
    % 更新目前为止最优个体
    if min_flag==1
        if best% 最小化情况 minimization.
            Fbest=best;
            Lbest=X(best_X,:);
        end
    else
        if best>Fbest % 最大化情况 maximization
            Fbest=best;
            Lbest=X(best_X,:);
        end
    end
    BestChart(iteration)=Fbest;
    MeanChart(iteration)=mean(fitness);
    % 计算惯性质量M(文献公式14—20) Calculation of M. eq.14-20
    M=massCalculation(fitness,min_flag);
    % 计算引力常亮(文献公式13) Calculation of Gravitational constant. eq.13.
    G=Gconstant(iteration,max_it);
    % 计算加速度 Calculation of accelaration in gravitational field. eq.7-10,21.
    a=Gfield(M,X,G,Rnorm,Rpower,ElitistCheck,iteration,max_it);
    % 个体移动 Agent movement. eq.11-12
    [X,V]=move(X,a,V);
end
% This function initializes the position of the agents in the search space, randomly.
function X=initialization(dim,N,up,down)
if size(up,2)==1
    X=rand(N,dim).*(up-down)+down;
end
if size(up,2)>1
    for i=1:dim
        high=up(i);
        low=down(i);
        X(:,i)=rand(N,1).*(high-low)+low;
    end
end
% This function calculates the mass of each agent. eq.14-20
function M =massCalculation(fit,min_flag)
% Here, make your own function of 'mass calculation'
Fmax=max(fit);
Fmin=min(fit);
[~,N]=size(fit);
if Fmax==Fmin
    M=ones(N,1);
else
    if min_flag==1 % for minimization
        best=Fmin;
        worst=Fmax; % eq.17-18.
    else % for maximization
        best=Fmax;
        worst=Fmin; % eq.19-20.
    end    
    M=(fit-worst)./(best-worst); % eq.15.
end
M=M./sum(M); % eq.16.
% This function calculates Gravitational constant. eq.13.
function G=Gconstant(iteration,max_it)
% here, make your own function of 'G'.
alfa=20;
G0=100;
G=G0*exp(-alfa*iteration/max_it); % eq.28.
% This function calculates the accelaration of each agent in gravitational field. eq.7-10,21.
function a=Gfield(M,X,G,Rnorm,Rpower,ElitistCheck,iteration,max_it)
[N,dim]=size(X);
% In the last iteration, only 2 percent of agents apply force to the others.
% 在最后一次迭代中,只有百分之二的个体对其它个体有引力???
final_per=2; 
% 计算总引力 total force calculation
if ElitistCheck==1
    kbest=final_per+(1-iteration/max_it)*(100-final_per); % 参考文献公式21中kbest的计算 kbest in eq.21.
    kbest=round(N*kbest/100);
else
    kbest=N; % eq.9.
end
[~,ds]=sort(M,'descend');
E=zeros(N,dim);
for i=1:N % 遍历种群
    E(i,:)=zeros(1,dim);
    for ii=1:kbest
        j=ds(ii);
        if j~=i
            R=norm(X(i,:)-X(j,:),Rnorm); % 欧氏距离 Euclidian distanse.
            for k=1:dim
                E(i,k)=E(i,k)+rand*(M(j))*((X(j,k)-X(i,k))/(R^Rpower+eps));
                % note that Mp(i)/Mi(i)=1
            end
        end
    end
end
% 加速度 acceleration
a=E.*G; % note that Mp(i)/Mi(i)=1
% This function updates the velocity and position of agents.
function [X,V]=move(X,a,V)
% movement.
[N,dim]=size(X);
V=rand(N,dim).*V+a; % eq.11.
X=X+V; % eq.12.
% This function checks the search space boundaries for agents.
function X=space_bound(X,up,low)
[N,dim]=size(X);
for i=1:N
    % 对越界值进行重新初始化 Agents that go out of the search space, are reinitialized randomly.
    Tp=X(i,:)>up;
    Tm=X(i,:)i,:)=(X(i,:).*(~(Tp+Tm)))+((rand(1,dim).*(up-low)+low).*logical((Tp+Tm)));
    % 将越界值重置为边界值 Agents that go out of the search space, are returned to the boundaries.
    % Tp=X(i,:)>up;
    % Tm=X(i,:)
    % X(i,:)=(X(i,:).*(~(Tp+Tm)))+up.*Tp+low.*Tm;
end
%% SVM_Objective Function
function f=objfun_svm(cv,train_wine_labels,train_wine,test_wine_labels,test_wine)
% cv为长度为2的横向量,即SVM中参数c和v的值

cmd = [' -c ',num2str(cv(1)),' -g ',num2str(cv(2))];
model=svmtrain(train_wine_labels,train_wine,cmd); % SVM模型训练
[~,fitness]=svmpredict(test_wine_labels,test_wine,model); % SVM模型预测及其精度
f=1-fitness(1)/100; % 以分类预测错误率作为优化的目标函数值

参考文献:E. Rashedi, H. Nezamabadi-pour and S. Saryazdi,
GSA: A Gravitational Search Algorithm,Information sciences, vol. 179,no. 13, pp. 2232-2248, 2009.

(广告)欢迎扫描关注微信公众号:Genlovhyy的数据小站(Gnelovy212)

GSA(引力搜索)优化算法MATLAB源码详细中文注解_第1张图片

你可能感兴趣的:(机器学习,MATLAB,优化算法)