clear
clc
close all
% psoSVMcgForClass
%
% by faruto
%Email:[email protected] QQ:516667408 http://blog.sina.com.cn/faruto
%last modified 2011.06.08
% 若转载请注明:
% faruto and liyang , LIBSVM-farutoUltimateVersion
% a toolbox with implements for support vector machines based on libsvm, 2011.
% Software available at http://www.matlabsky.com
%
% Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for
% support vector machines, 2001. Software available at
% http://www.csie.ntu.edu.tw/~cjlin/libsvm
%% 参数初始化
pso_option = struct('c1',2.05,'c2',2.05,'maxgen',2500,'sizepop',100, ...
'k',0.6,'wV',1.1,'wP',1.1,'v',5, ...
'popcmax',30,'popcmin',-30,'popgmax',30,'popgmin',-30);
% pso_option = struct('c1',1.5,'c2',1.7,'maxgen',200,'sizepop',20, ...
% 'k',0.6,'wV',1,'wP',1,'v',5, ...
% 'popcmax',100,'popcmin',0.1,'popgmax',10^3,'popgmin',10^(-2));
% c1:初始为1.5,pso参数局部搜索能力
% c2:初始为1.7,pso参数全局搜索能力
% maxgen:初始为200,最大进化数量
% sizepop:初始为20,种群最大数量
% k:初始为0.6(k belongs to [0.1,1.0]),速率和x的关系(V = kX)
% wV:初始为1(wV best belongs to [0.8,1.2]),速率更新公式中速度前面的弹性系数
% wP:初始为1,种群更新公式中速度前面的弹性系数
% v:初始为5,SVM Cross Validation参数
% popcmax:初始为100,SVM 参数c的变化的最大值.
% popcmin:初始为0.1,SVM 参数c的变化的最小值.
% popgmax:初始为1000,SVM 参数g的变化的最大值.
% popgmin:初始为0.01,SVM 参数c的变化的最小值.
Vcmax = pso_option.k*pso_option.popcmax;
Vcmin = -Vcmax ;
Vgmax = pso_option.k*pso_option.popgmax;
Vgmin = -Vgmax ;
eps = 10^(-5);
% train_1 = train(1:100,1);
% train_label1 = train_label(1:100);
% train_2 = train(31:40,1:9);
% train_label2 = train_label(31:40);
%% 产生初始粒子和速度
for i=1:pso_option.sizepop
% 随机产生种群和速度
pop(i,1) = (pso_option.popcmax-pso_option.popcmin)*rand+pso_option.popcmin;
pop(i,2) = (pso_option.popgmax-pso_option.popgmin)*rand+pso_option.popgmin;
V(i,1)=Vcmax*rands(1,1);
V(i,2)=Vgmax*rands(1,1);
% 计算初始适应度
fitness(i)=myfunc_fit1(pop(i,:));
% [traini1,a1,b1]=svmpredict(train_label1,train_1,model);
% [traini2,a2,b2]=svmpredict(train_label2,train_2,model);
% fitness(i)= 0.25*mse(traini1-train_label1) + 0.75*mse(traini2-train_label2);
end
% 找极值和极值点
[global_fitness bestindex]=min(fitness); % 全局极值
local_fitness=fitness; % 个体极值初始化
global_x=pop(bestindex,:); % 全局极值点
local_x=pop; % 个体极值点初始化
% 每一代种群的平均适应度
avgfitness_gen = zeros(1,pso_option.maxgen);
%% 迭代寻优
for i=1:pso_option.maxgen
for j=1:pso_option.sizepop
%速度更新
V(j,:) = pso_option.wV*V(j,:) + pso_option.c1*rand*(local_x(j,:) - pop(j,:)) + pso_option.c2*rand*(global_x - pop(j,:));
if V(j,1) > Vcmax
V(j,1) = Vcmax;
end
if V(j,1) < Vcmin
V(j,1) = Vcmin;
end
if V(j,2) > Vgmax
V(j,2) = Vgmax;
end
if V(j,2) < Vgmin
V(j,2) = Vgmin;
end
%种群更新
pop(j,:)=pop(j,:) + pso_option.wP*V(j,:);
if pop(j,1) > pso_option.popcmax
pop(j,1) = pso_option.popcmax;
end
if pop(j,1) < pso_option.popcmin
pop(j,1) = pso_option.popcmin;
end
if pop(j,2) > pso_option.popgmax
pop(j,2) = pso_option.popgmax;
end
if pop(j,2) < pso_option.popgmin
pop(j,2) = pso_option.popgmin;
end
% 自适应粒子变异
if rand>0.5
k=ceil(2*rand);
if k == 1
pop(j,k) = (pso_option.popcmax-pso_option.popcmin)*rand + pso_option.popcmin;
end
if k == 2
pop(j,k) = (pso_option.popgmax-pso_option.popgmin)*rand + pso_option.popgmin;
end
end
%适应度值
fitness(j)=myfunc_fit1(pop(j,:));
% [trainj1,a1,b1]=svmpredict(train_label1,train_1,model);
% [trainj2,a2,b2]=svmpredict(train_label2,train_2,model);
% fitness(j)= 0.2*mse(trainj1-train_label1) + 0.8*mse(trainj2-train_label2);
%个体最优更新
if fitness(j) < local_fitness(j)
local_x(j,:) = pop(j,:);
local_fitness(j) = fitness(j);
end
if fitness(j) == local_fitness(j) && pop(j,1) < local_x(j,1)
local_x(j,:) = pop(j,:);
local_fitness(j) = fitness(j);
end
%群体最优更新
if fitness(j) < global_fitness
global_x = pop(j,:);
global_fitness = fitness(j);
end
if abs( fitness(j)-global_fitness )<=eps && pop(j,1) < global_x(1)
global_x = pop(j,:);
global_fitness = fitness(j);
end
end
fit_gen(i)=global_fitness;
avgfitness_gen(i) = sum(fitness)/pso_option.sizepop;
end
xlswrite('fit_gen.xlsx',fit_gen);
xlswrite('avgfitness_gen.xlsx',avgfitness_gen);
%% 结果分析
figure;
hold on;
plot(fit_gen,'r-','LineWidth',1.5);
% plot(avgfitness_gen,'o-','LineWidth',1.5);
legend('最佳适应度','平均适应度');
xlabel('进化代数','FontSize',12);
ylabel('适应度','FontSize',12);
grid on;
bestc = global_x(1);
bestg = global_x(2);
bestCVmse = fit_gen(pso_option.maxgen);
line1 = '适应度曲线MSE[PSOmethod]';
line2 = ['(参数c1=',num2str(pso_option.c1), ...
',c2=',num2str(pso_option.c2),',终止代数=', ...
num2str(pso_option.maxgen),',种群数量pop=', ...
num2str(pso_option.sizepop),')'];
line3 = ['Best c=',num2str(bestc),' g=',num2str(bestg), ...
' CVmse=',num2str(bestCVmse),];
title({line1;line2;line3},'FontSize',12);
7利用智能算法对微网中的分布式电源进行最优调度实现配电网稳定运行.zip
版权申诉
13 浏览量
2023-05-24
22:11:30
上传
评论
收藏 1.83MB ZIP 举报
JGiser
- 粉丝: 7519
- 资源: 5112
最新资源
- n.cpp
- jdk-8u411-windows-x64下载安装可用
- vgg模型-图像分类算法对水果识别-不含数据集图片-含逐行注释和说明文档.zip
- KMP算法(Knuth-Morris-Pratt算法
- vgg模型-python语言pytorch框架训练识别化妆品分类-不含数据集图片-含逐行注释和说明文档.zip
- KMP算法(Knuth-Morris-Pratt算法
- shufflenet模型-基于人工智能的卷积网络训练识别狗的表情-不含数据集图片-含逐行注释和说明文档.zip
- shufflenet模型-python语言pytorch框架训练识别张嘴闭嘴-不含数据集图片-含逐行注释和说明文档.zip
- resnet模型-基于人工智能的卷积网络训练识别面部表情识别-不含数据集图片-含逐行注释和说明文档
- resnet模型-python语言pytorch框架训练识别香蕉品质-不含数据集图片-含逐行注释和说明文档.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈