clc;
clear;
close all;
warning off;
addpath 'func\'
addpath 'GA_toolbox\'
addpath 'func_SVM_toolbox\'
addpath 'func_SVM_toolbox\java\'
addpath 'func_SVM_toolbox\java\libsvm\'
addpath 'func_SVM_toolbox\matlab\'
addpath 'func_SVM_toolbox\matlab-implement[by faruto]\'
addpath 'func_SVM_toolbox\python\'
addpath 'func_SVM_toolbox\svm-toy\'
addpath 'func_SVM_toolbox\tools\'
addpath 'func_SVM_toolbox\windows\'
%作为对比,直接通过SVM算法,没有通过优化算法直接进行算法的仿真;
rng(1);
%先进行优化,设置1,然后设置2,调用优化值进行测试
SEL = 2;
%导入数据
load 训练\X_train.mat;
load 测试\X_test.mat;
X_train0 = X_train;
X_test0 = X_test;
figure;
plot(X_test0);
xlabel('样本点n');
ylabel('幅值');
[y1,input1ps] = mapminmax(X_train0');
[y2,input2ps] = mapminmax(X_test0');
X_train = y1';
X_test = y2';
if SEL == 1
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%通过GA-PSO搜索最优的四个参数
%tao的范围
min1 = 1;
max1 = 10;
%m的范围
min2 = 1;
max2 = 10;
%C的范围
min3 = 1;
max3 = 1000;
%gamma的范围
min4 = 0;
max4 = 5;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
wmax = 0.9;
wmin = 0.1;
c1 = 2.5;
c2 = 2.5;
%速度的范围
vmin =-7;
vmax = 7;
MAXGEN = 50;
NIND = 10;
Chrom = crtbp(NIND,4*10);
%变量的区间
Areas = [min1,min2,min3,min4;
max1,max2,max3,max4];
FieldD = [rep([10],[1,4]);Areas;rep([0;0;0;0],[1,4])];
Data1 = zeros(NIND,4);
gen = 0;
for a=1:1:NIND
%保证每个数值不一样,
Data1(a,:) = [1,1,1000,0.3];
tao = Data1(a,1);
m = Data1(a,2);
C = Data1(a,3);
gamma = Data1(a,4);
%计算对应的目标值
[epls,tao,m,C,gamma] = func_fitness(X_train,X_test,tao,m,C,gamma);
E = epls;
J(a,1) = E;
va(a) =(vmax-vmin)*rand(1)+vmin;
vb(a) =(vmax-vmin)*rand(1)+vmin;
vc(a) =(vmax-vmin)*rand(1)+vmin;
vd(a) =(vmax-vmin)*rand(1)+vmin;
end
[V,I] = min(J);
JI = I;
tmpps = Data1(JI,:);
taos = round(tmpps(1));
ms = round(tmpps(2));
Cs = tmpps(3);
gammas = tmpps(4);
Objv = (J+eps);
gen = 0;
while gen < MAXGEN;
gen
w = wmax-gen*(wmax-wmin)/MAXGEN;
FitnV = ranking(Objv);
Selch = select('sus',Chrom,FitnV);
Selch = recombin('xovsp',Selch,0.9);
Selch = mut(Selch,0.1);
phen1 = bs2rv(Selch,FieldD);
%基于粒子群的速度更新
for i=1:1:NIND
if gen > 1
va(i) = w*va(i) + c1*rand(1)*(phen1(i,1)-taos2) + c2*rand(1)*(taos-taos2);
vb(i) = w*vb(i) + c1*rand(1)*(phen1(i,2)-ms2) + c2*rand(1)*(ms-ms2);
vc(i) = w*vc(i) + c1*rand(1)*(phen1(i,3)-Cs2) + c2*rand(1)*(Cs-Cs2);
vd(i) = w*vd(i) + c1*rand(1)*(phen1(i,4)-gammas2) + c2*rand(1)*(gammas-gammas2);
else
va(i) = 0;
vb(i) = 0;
vc(i) = 0;
vd(i) = 0;
end
end
for a=1:1:NIND
Data1(a,:) = phen1(a,:);
tao = round(Data1(a,1) + 0.15*va(i));%遗传+PSO
m = round(Data1(a,2) + 0.15*vb(i));
C = Data1(a,3) + 0.15*vc(i);
gamma = Data1(a,4) + 0.15*vd(i);
if tao >= max1
tao = max1;
end
if tao <= min1
tao = min1;
end
if m >= max2
m = max2;
end
if m <= min2
m = min2;
end
if C >= max3
C = max3;
end
if C <= min3
C = min3;
end
if gamma >= max4
gamma = max4;
end
if gamma <= min4
gamma = min4;
end
%计算对应的目标值
[epls,tao,m,C,gamma] = func_fitness(X_train,X_test,tao,m,C,gamma);
E = epls;
JJ(a,1) = E;
end
Objvsel=(JJ);
[Chrom,Objv]=reins(Chrom,Selch,1,1,Objv,Objvsel);
gen=gen+1;
%保存参数收敛过程和误差收敛过程以及函数值拟合结论
Error(gen) = mean(JJ);
pause(0.2);
[V,I] = min(Objvsel);
JI = I;
tmpps = Data1(JI,:);
taos2 = round(tmpps(1));
ms2 = round(tmpps(2));
Cs2 = tmpps(3);
gammas2 = tmpps(4);
end
[V,I] = min(Objvsel);
JI = I;
tmpps = Data1(JI,:);
tao0 = round(tmpps(1));
m0 = round(tmpps(2));
C0 = tmpps(3);
gamma0 = tmpps(4);
save GAPSO.mat tao0 m0 C0 gamma0
end
if SEL == 2
load GAPSO.mat
%调用四个最优的参数
tao = tao0;
m = m0;
C = C0;
gamma = gamma0;
%先进行相空间重构
[Xn ,dn ] = func_CC(X_train,tao,m);
[Xn1,dn1] = func_CC(X_test,tao,m);
t = 1/1:1/1:length(dn1)/1;
f = 0.05;
sn = 0.0002*sin(2*pi*f*t);
%叠加
dn1 = dn1 + sn';
%SVM训练%做单步预测
cmd = ['-s 3',' -t 2',[' -c ', num2str(C)],[' -g ',num2str(gamma)],' -p 0.000001'];
model = svmtrain(dn,Xn,cmd);
%SVM预测
[Predict1,error1] = svmpredict(dn1,Xn1,model);
RMSE = sqrt(sum((dn1-Predict1).^2)/length(Predict1));
Err = dn1-Predict1;
%误差获取
clc;
RMSE
figure;
plot(Err,'b');
title('混沌背景信号的预测误差');
xlabel('样本点n');
ylabel('误差幅值');
Fs = 1;
y = fftshift(abs(fft(Err)));
N = length(y)
fc = [-N/2+1:N/2]/N*Fs;
figure;
plot(fc(N/2+2:N),y(N/2+2:N));
xlabel('归一化频率');
ylabel('频谱');
text(0.06,0.07,'f=0.05Hz');
end