:'( 我用的matlab是2014a版本的,输入以下程序
clear
clc
%导入训练数据
train_input=xlsread('Sample Data2',['D2:Y115']);
train_out=xlsread('Sample Data2',['Z2:Z115']);
train_input=train_input';
train_out=train_out';
[train_data,pstrain0]=mapminmax(train_input,0,1);
[train_result,pstrain1]=mapminmax(train_out,0,1);
c1=1.5; %PSO参数局部搜索能力
c2=1.7; %PSO参数全局搜索能力
maxgen=200; %最大迭代次数Tmax
sizepop=30; %粒子数m,又称群体规模,过大的m会影响算法的运算速度和收敛性。
%%初始化惩罚因子c和核函数参数 的取值范围
popcmax=1000; %惩罚因子c变化的最大值
popcmin=0.1; %惩罚因子c变化的最小值
popgmax=100; %核函数参数 变化的最大值
popgmin=0.01; %核函数参数 变化的最小值
k=0.5; %适应阈值 =0~1
Vcmax=k*popcmax; %惩罚因子c迭代速度的最大值
Vcmin=-Vcmax; %参数c迭代速度的最小值
Vgmax=k*popgmax; %核函数参数 迭代速度的最大值
Vgmin=-Vgmax; %核函数参数 迭代速度的最小值
eps=10^(-8); %机器的浮点运算误差限,当某个数小于它时则记为0
%%定义LSSVM相关参数
type='function estimation'; %回归
%type=’c’,表示classification 分类,type=’f’,表示回归。
kernel='RBF_kernel'; %LSSVM的核函数选取为径向基内核
proprecess='proprecess';
%proprecess表明数据已归一化处理,original表明数据没有进行归一化处理
%%产生初始粒子和速度
for i=1:sizepop %种群规模
%随机产生种群
pop(i,1)=(popcmax-popcmin)*rand(1,1)+popcmin; %初始种群
pop(i,2)=(popgmax-popgmin)*rand(1,1)+popgmin;
V(i,1)=Vcmax*rands(1,1); %初始化速度
V(i,2)=Vgmax*rands(1,1); %rands(1,1)产生介于-1~1之间的1*1的权值矩阵
%计算初始适应度=以训练集的预测值计算的均方差为适应度值
gam=pop(i,1); %惩罚因子c初始化
sig2=pop(i,2); %核函数参数 初始化
model=initlssvm(train_data,train_result,type,gam,sig2,kernel,proprecess);
model=trainlssvm(model);
%求出训练集的预测值
[train_predict_y,zt,model]=simlssvm(model,train_data);
%预测数据转置并取反归一化
train_predict=mapminmax('reverse',train_predict_y',pstrain1);
%计算训练集预测值的均方差
trainmse=sum((train_predict-train_out).^2)/length(train_result);
fitness(i)=trainmse; %以训练集的预测值计算的均方差为适应度值
end
%%找极值和极值点
[global_fitness bestindex]=min(fitness); %全局极值
local_fitness=fitness; %个体极值初始化
global_x=pop(bestindex,:); %全局极值点
local_x=pop; %个体极值点初始化
%每一代种群的平均适应度
avgfitness_gen=zeros(1,maxgen)
tic %%%tic与toc是计时用的
%%迭代寻优
for i=1:maxgen %最大迭代次数
for j=1:sizepop %种群规模即粒子数
%速度更新
wmax=1.2; %初始权重w为1.2
wmin=0.8; %最终权重w为0.8
wV=wmax-i*(wmax-wmin)/maxgen; %带惯性权重的粒子群算法
V(j,:)=wV*V(j,:)+c1*rand*(local_x(j,:)-pop(j,:))+c2*rand*(global_x-pop(j,:));%速度更新
%%以下几个不等式是为了限定速度在最大最小之间
if V(j,1)>Vcmax
V(j,1)=Vcmax;
end
if V(j,1)
V(j,1)=Vcmin;
end
if V(j,2)>Vgmax
V(j,2)=Vgmax;
end
if V(j,2)
V(j,2)=Vgmin;
end
%种群位置更新(根据位置更新公式而得)
pop(j,:)=pop(j,:)+V(j,:);
%以下几个不等式是为了限定惩罚因子c在最大值最小值之间
if pop(j,1)>popcmax
pop(j,1)=popcmax;
end
if pop(j,1)
pop(j,1)=popcmin;
end
%以下几个不等式是为了限定核函数参数 在最大值最小值之间
if pop(j,2)>popgmax
pop(j,2)=popgmax;
end
if pop(j,2)
pop(j,2)=popgmin;
end
%%自适应粒子变异
if rand>0.5
k=ceil(2*rand); %ceil是向离它最近的大整数圆整
if k==1
pop(j,k)=(20-1)*rand+1;
end
if k==2
pop(j,k)=(popgmax-popgmin)*rand+popgmin;
end
%%新粒子适应度值
gam=pop(j,1); %新粒子的惩罚因子c
sig2=pop(j,2); %新粒子的核函数参数
model=initlssvm(train_data,train_result,type,gam,sig2,kernel,proprecess);
model=trainlssvm(mdoel)
%%求出训练集的预测值
[train_predict_y,zt,model]=simlssvm(model,train_data);
%预测数据转置并取反归一化
train_predict=mapminmax('reverse',train_predict_y',pstrain1);
%计算训练集预测数据的均方差
trainmse=sum((train_predict-train_out).^2)/length(train_result);
fitness(j)=trainmse;
end
%个体最优更新
if 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,:)=pop(j,:);
local_fitness(j)=fitness(j);
end
%%群体最优更新
if fitness(j)
global_x=pop(j,:);
global_fitness=fitness(j);
end
if abs(fitness(j)-global_fitness)<=eps&&pop(j,1)
global_x=pop(j,:);
global_fitness=fitness(j);
end
end
fit_gen(i)=global_fitness; %全局极值
avgfitness_gen(i)=sum(fitness)/sizepop;
%个体极值的平均值(每一次迭代的极值之和除以迭代次数)
end
toc %与前述的tic相结合用来计算优化程序运行的时间
%%结果分析
plot(fit_gen,'LineWidth',5); %全局极值随迭代次数的变化曲线
title(['适应度曲线','(参数c1=',num2str(c1),',c2=',num2str(c2),',终止迭代次数=',num2str(maxgen),')'],'FontSize',13);
xlabel('进化次数');yabel('适应度');
%%记录粒子群优化结果,最优的惩罚因子c和核函数参数
bestc=global_x(1);
bestg=global_x(2);
gam=bestc
sig2=bestg
%%%利用粒子群算法优化参数结果bestc、bestg建模
model=initlssvm(train_data,train_result,type,gam,sig2,kernel,proprecess);
model=trainlssvm(model);
%save model.mat
%%求出训练集的预测值
[train_predict_y,zt,model]=simlssvm(model,train_data);
%%预测数据转置并取反归一化
train_predict=mapminmax('reverse',train_predict_y',pstrain1);
%%计算训练集预测值均方差
trainmse=sum((train_predict-train_out).^2)/length(train_result)
%%计算训练集预测值的相对误差
trainRelativeError=sum(abs((train_out-train_predict)/train_out))/length(train_out)
%%计算训练集预测值相对误差的均方根误差
trainRMSE=sqrt(sum(((train_out-train_predict)/train_out).^2)/length(train_out))
%%用训练集模型预测测试集
%%导入测试数据
test_input=xlsread('Sample Data2',['D116:Y145']);
test_out=xlsread('Sample Data2',['Z116:Z145']);
%%测试数据取转置
test_input=test_input';
test_out=test_out';
%%使用mapminmax归一化测试数据
[test_data,pstrain0]=mapminmax(test_input,0,1);
%%利用训练集模型预测测试集
%load model.mat
[test_predict_y,zt,model]=simlssvm(model,test_data);
%预测数据转置并取反归一化
test_predict=mapminmax('reverse',test_predict_y',pstrain1)
%%计算预测集预测值的均方差
testmse=sum((test_predict-test_out).^2)/length(test_out)
%%计算预测集预测值的相对误差
testRelativeError=sum(abs((test_out-test_predict)/test_out))/length(test_out)
%%计算预测集预测值相对误差的均方根误差
testRMSE=sqrt(sum(((test_out-test_predict)/test_out).^2)/length(test_out))
%%%训练集与测试集的真值与预测值归一
result=[train_out,test_out];
predict=[train_predict,test_predict];
%%计算所有数据预测值的均方差
testmse=sum((predict-result).^2)/length(result)
%%计算所有数据预测值的相对误差
testRelativeError=sum(abs((resssult- predict)/result))/length(result)
%%计算所有数据预测值相对误差的均方根误差
testRMSE=sqrt(sum(((result-predict)/result).^2)/length(result))
%%画图
figure
plot(result,'k')
hold on
plot(predict,'r')
xlabel('样本/组')
ylabel('工信部百公里燃油消耗量L/100km')
%%%%%%%%%%%
%%%%%%%%%
%%%%%%%%%
%%%%%%%%%
%%%总是提示出错 Untitled10 (line 41)
%%%%%%model=initlssvm(train_data,train_result,type,gam,sig2,kernel,proprecess);
望大神给看看,是怎么回事,急求啊……………………:'(