-
大小: 1KB文件类型: .rar金币: 1下载: 0 次发布日期: 2021-05-19
- 语言: Matlab
- 标签: psoSVM.rar
资源简介
用粒子群算法优化支持向量机的matlab程序,简单易学,适合matlab初学者
代码片段和文件信息
%% 清空环境
clc
clear
load wine;
train = [wine(1:30:);wine(60:95:);wine(131:153:)];
train_label = [wine_labels(1:30);wine_labels(60:95);wine_labels(131:153)];
test = [wine(31:59:);wine(96:130:);wine(154:178:)];
test_label = [wine_labels(31:59);wine_labels(96:130);wine_labels(154:178)];
[trainpstrain] = mapminmax(train‘);
pstrain.ymin = 0;
pstrain.ymax = 1;
[trainpstrain] = mapminmax(trainpstrain);
[testpstest] = mapminmax(test‘);
pstest.ymin = 0;
pstest.ymax = 1;
[testpstest] = mapminmax(testpstest);
train = train‘;
test = test‘;
%% 参数初始化
%粒子群算法中的两个参数
c1 = 1.6; % c1 belongs to [02]
c2 = 1.5; % c2 belongs to [02]
maxgen=300; % 进化次数
sizepop=30; % 种群规模
popcmax=10^(2);
popcmin=10^(-1);
popgmax=10^(3);
popgmin=10^(-2);
k = 0.6; % k belongs to [0.11.0];
Vcmax = k*popcmax;
Vcmin = -Vcmax ;
Vgmax = k*popgmax;
Vgmin = -Vgmax ;
% SVM参数初始化
v = 3;
%% 产生初始粒子和速度
for i=1:sizepop
% 随机产生种群
pop(i1) = (popcmax-popcmin)*rand+popcmin; % 初始种群
pop(i2) = (popgmax-popgmin)*rand+popgmin;
V(i1)=Vcmax*rands(1); % 初始化速度
V(i2)=Vgmax*rands(1);
% 计算初始适应度
cmd = [‘-v ‘num2str(v)‘ -c ‘num2str( pop(i1) )‘ -g ‘num2str( pop(i2) )];
fitness(i) = svmtrain(train_label train cmd);
fitness(i) = -fitness(i);
end
% 找极值和极值点
[global_fitness bestindex]=min(fitness); % 全局极值
local_fitness=fitness; % 个体极值初始化
global_x=pop(bestindex:); % 全局极值点
local_x=pop; % 个体极值点初始化
tic
%% 迭代寻优
for i=1:maxgen
for j=1:sizepop
%速度更新
wV = 0.9; % wV best belongs to [0.81.2]
V(j:) = wV*V(j:) + c1*rand*(local_x(j:) - pop(j:)) + c2*rand*(global_x - pop(j:));
if V(j1) > Vcmax
V(j1) = Vcmax;
end
if V(j1) < Vcmin
V(j1) = Vcmin;
end
if V(j2) > Vgmax
V(j2) = Vgmax;
end
if V(j2) < Vgmin
V(j2) = Vgmin;
end
%种群更新
wP = 0.6;
pop(j:)=pop(j:)+wP*V(j:);
if pop(j1) > popcmax
pop(j1) = popcmax;
end
if pop(j1) < popcmin
pop(j1) = popcmin;
end
if pop(j2) > popgmax
pop(j2) = popgmax;
end
if pop(j2) < popgmin
pop(j2) = popgmin;
end
% 自适应粒子变异
if rand>0.5
k=ceil(2*rand);
if k == 1
pop(jk) = (20-1)*rand+1;
end
if k == 2
pop(jk) = (popgmax-popgmin)*rand+popgmin;
end
end
%适应度值
cmd = [‘-v ‘num2str(v)‘ -c ‘num2str( pop(j1) )‘ -g ‘num2str( pop(j2) )];
fitness(j) = svmtrain(train_label train cmd);
fitness(j) = -fitness(j);
end
%个体最优更新
if fitness(j) < local_fitness(j)
local_x(j:) = pop(j:);
local_fitness(j
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3862 2010-04-07 21:22 psoSVM.m
----------- --------- ---------- ----- ----
3862 1
- 上一篇:光镊计算工具箱
- 下一篇:粒子群算法优化BP神经网络权值的程序
评论
共有 条评论