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

2024-09-04 18:32

本文主要是介绍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:Nfitness(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.endif iteration==1Fbest=best;Lbest=X(best_X,:);end% 更新目前为止最优个体if min_flag==1if best<Fbest % 最小化情况 minimization.Fbest=best;Lbest=X(best_X,:);endelseif best>Fbest % 最大化情况 maximizationFbest=best;Lbest=X(best_X,:);endendBestChart(iteration)=Fbest;MeanChart(iteration)=mean(fitness);% 计算惯性质量M(文献公式14—20) Calculation of M. eq.14-20M=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)==1X=rand(N,dim).*(up-down)+down;
end
if size(up,2)>1for i=1:dimhigh=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==FminM=ones(N,1);
elseif min_flag==1 % for minimizationbest=Fmin;worst=Fmax; % eq.17-18.else % for maximizationbest=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==1kbest=final_per+(1-iteration/max_it)*(100-final_per); % 参考文献公式21中kbest的计算 kbest in eq.21.kbest=round(N*kbest/100);
elsekbest=N; % eq.9.
end
[~,ds]=sort(M,'descend');
E=zeros(N,dim);
for i=1:N % 遍历种群E(i,:)=zeros(1,dim);for ii=1:kbestj=ds(ii);if j~=iR=norm(X(i,:)-X(j,:),Rnorm); % 欧氏距离 Euclidian distanse.for k=1:dimE(i,k)=E(i,k)+rand*(M(j))*((X(j,k)-X(i,k))/(R^Rpower+eps));% note that Mp(i)/Mi(i)=1endendend
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,:)<low;X(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,:)<low;% 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源码详细中文注解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1136696

相关文章

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

SQL Server数据库死锁处理超详细攻略

《SQLServer数据库死锁处理超详细攻略》SQLServer作为主流数据库管理系统,在高并发场景下可能面临死锁问题,影响系统性能和稳定性,这篇文章主要给大家介绍了关于SQLServer数据库死... 目录一、引言二、查询 Sqlserver 中造成死锁的 SPID三、用内置函数查询执行信息1. sp_w

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

mapstruct中的@Mapper注解的基本用法

《mapstruct中的@Mapper注解的基本用法》在MapStruct中,@Mapper注解是核心注解之一,用于标记一个接口或抽象类为MapStruct的映射器(Mapper),本文给大家介绍ma... 目录1. 基本用法2. 常用属性3. 高级用法4. 注意事项5. 总结6. 编译异常处理在MapSt

Python包管理工具核心指令uvx举例详细解析

《Python包管理工具核心指令uvx举例详细解析》:本文主要介绍Python包管理工具核心指令uvx的相关资料,uvx是uv工具链中用于临时运行Python命令行工具的高效执行器,依托Rust实... 目录一、uvx 的定位与核心功能二、uvx 的典型应用场景三、uvx 与传统工具对比四、uvx 的技术实

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte