看了程序 我都看糊涂了 我不知道哪里是数据的输入
求大牛指导一下啊
PSO优化的
%用粒子群算法优化RBF网络权值
clear all
close all
G =250; %迭代次数
n = 12; %粒子维数
m = 20; %种群规模
w = 0.1; %算法参数
c1 = 2; %算法参数
c2 = 2; %算法参数
%取粒子的取值范围
for i = 1:3
MinX(i) = 0.1*ones(1);
MaxX(i) = 3*ones(1);
end
for i = 4:1:9
MinX(i) = -3*ones(1);
MaxX(i) = 3*ones(1);
end
for i = 10:1:12
MinX(i) = -ones(1);
MaxX(i) = ones(1);
end
%初始化种群pop
pop = rands(m,n);
for i = 1:m
for j = 1:3
if pop(i,j) < MinX(j)
pop(i,j) = MinX(j);
end
if pop(i,j) > MaxX(j)
pop(i,j) = MaxX(j);
end
end
for j = 4:9
if pop(i,j) < MinX(j)
pop(i,j) = MinX(j);
end
if pop(i,j) > MaxX(j)
pop(i,j) = MaxX(j);
end
end
for j = 10:12
if pop(i,j) < MinX(j)
pop(i,j) = MinX(j);
end
if pop(i,j) > MaxX(j)
pop(i,j) = MaxX(j);
end
end
end
%初始化粒子速度
V = 0.1*rands(m,n);
BsJ = 0;
%根据初始化的种群计算个体好坏,找出群体最优和个体最优
for s = 1:m
indivi = pop(s,:); %抽出个体
[indivi,BsJ] = fitness(indivi,BsJ); %求出每个粒子对应的误差
Error(s) = BsJ;
end
[OderEr,IndexEr] = sort(Error); %对误差进行排序
Error;
Errorleast = OderEr(1); %求出最小误差
for i = 1:m
if Errorleast == Error(i)
gbest = pop(i,:); %找出最小误差对应的个体极值gbest
break;
end
end
ibest = pop; %把初始化的种群作为群体极值
%循环开始
for kg = 1:G
kg
for s = 1:m;
%个体有4%的变异概率
for j = 1:n
for i = 1:m
if rand(1)<0.04
pop(i,j) = rands(1); %对个体pop(i,j)进行变异
end
end
end
%r1,r2为粒子群算法参数
r1 = rand(1);
r2 = rand(1);
% 速度更新
V(s,:) = w*V(s,:) + c1*r1*(ibest(s,:)-pop(s,:)) + c2*r2*(gbest-pop(s,:));
%个体更新
pop(s,:) = pop(s,:) + 0.3*V(s,:);
for j = 1:3
if pop(s,j) < MinX(j)
pop(s,j) = MinX(j);
end
if pop(s,j) > MaxX(j)
pop(s,j) = MaxX(j);
end
end
for j = 4:9
if pop(s,j) < MinX(j)
pop(s,j) = MinX(j);
end
if pop(s,j) > MaxX(j)
pop(s,j) = MaxX(j);
end
end
for j = 10:12
if pop(s,j) < MinX(j)
pop(s,j) = MinX(j);
end
if pop(s,j) > MaxX(j)
pop(s,j) = MaxX(j);
end
end
%求更新后的每个个体误差,可看成适应度值
[pop(s,:),BsJ] = fitness(pop(s,:),BsJ);
error(s) = BsJ;
%根据适应度值对个体最优和群体最优进行更新
if error(s)
ibest(s,:) = pop(s,:);
Error(s) = error(s);
end
if error(s)
gbest = pop(s,:);
Errorleast = error(s);
end
end
Best(kg) = Errorleast;
end
plot(Best);
title('遗传算法优化RBF网络权值中最小误差进化过程')
xlabel('进化次数');
ylabel('最小误差');
save pfile1 gbest;
GA优化的
clear all
close all
%遗传算法优化来训练RBF网络权值
%G为进化代数,Size为种群规模,CodeL为参数的二进制编码长度
G = 250;
Size = 30;
CodeL = 10;
%确定每个参数的最大最小值
for i = 1:3
MinX(i) = 0.1*ones(1);
MaxX(i) = 3*ones(1);
end
for i = 4:1:9
MinX(i) = -3*ones(1);
MaxX(i) = 3*ones(1);
end
for i = 10:1:12
MinX(i) = -ones(1);
MaxX(i) = ones(1);
end
%初始化种群
E = round(rand(Size,12*CodeL));
BsJ = 0;
%进化开始
for kg = 1:1:G
time(kg) = kg
for s = 1:1:Size
m = E(s,:); %取出其中个体
%把二进制表示的参数转化为实数
for j = 1:1:12
y(j) = 0;
mj = m((j-1)*CodeL + 1:1:j*CodeL);
for i = 1:1:CodeL
y(j) = y(j) + mj(i)*2^(i - 1);
end
f(s,j) = (MaxX(j) - MinX(j))*y(j)/1023 + MinX(j);
end
p = f(s,:);
[p,BsJ] = fitness(p,BsJ);
BsJi(s) = BsJ; %记录每个个体的总误差
end
%对误差排序,求出最好误差
[OderJi,IndexJi] = sort(BsJi);
BestJ(kg) = OderJi(1);
BJ = BestJ(kg);
Ji = BsJi + 1e-10;
%对误差取倒数,求出适应度值
fi = 1./Ji; %适应度值
[Oderfi,Indexfi] = sort(fi);
Bestfi = Oderfi(Size); %最佳适应度值
BestS = E(Indexfi(Size),:); %最佳个体
kg %进化次数
p %最佳个体
BJ %最佳个体的误差
%**************Step 2:选择操作**********************%
fi_sum = sum(fi);
fi_Size = (Oderfi/fi_sum)*Size;
fi_S = floor(fi_Size);
kk = 1;
for i = 1:1:Size
for j = 1:1:fi_S(i)
TempE(kk,:) = E(Indexfi(i),:);
kk = kk + 1;
end
end
%***************Step 3:交叉操作***********************************%
pc = 0.60;
n = ceil(20*rand);
for i = 1:2:(Size-1)
temp = rand;
if pc>temp
for j = n:1:20
TempE(i,j) = E(i+1,j);
TempE(i+1,j) = E(i,j);
end
end
end
TempE(Size,:) = BestS;
E = TempE;
%***************Step 4:变异操作**********************************%
pm = 0.001 - [1:1:Size]*(0.001)/Size;
for i = 1:1:Size
for j = 1:1:12*CodeL
temp = rand;
if pm>temp
if TempE(i,j) == 0
TempE(i,j) = 1;
else
TempE(i,j) = 0;
end
end
end
end
%把最佳个体赋于种群中
TempE(Size,:) = BestS;
E = TempE;
end
Bestfi
BestS
fi
Best_J = BestJ(G)
figure(1)
plot(time,BestJ);
title('遗传算法优化RBF网络权值中最小误差进化过程')
xlabel('进化次数');
ylabel('最小误差');
save pfile p;
测试的程序
clear all
close all
%分别使用粒子群算法,遗传算法和未经过优化权值的RBF网络做预测
%
load pfile1 gbest; %粒子群算法优化得到权值
load pfile p; %遗传算法优化得到权值
%学习系数
alfa = 0.05;
xite = 0.85;
x = [0,0]';
for M=1:3
if M==1 %取粒子群算法进化的权值
b=[gbest(1);gbest(2);gbest(3)];
c=[gbest(4) gbest(5) gbest(6);
gbest(7) gbest(8) gbest(9)];
w=[gbest(10);gbest(11);gbest(12)];
elseif M==2 %取遗传算法进化的权值
b=[p(1);p(2);p(3)];
c=[p(4) p(5) p(6);
p(7) p(8) p(9)];
w=[p(10);p(11);p(12)];
elseif M==3 %权值重新初始化
b=3*rand(3,1);
c=3*rands(2,3);
w=rands(3,1);
end
w_1 = w;w_2 = w_1;
c_1 = c;c_2 = c_1;
b_1 = b;b_2 = b_1;
y_1 = 0;
ts = 0.001;
for k = 1:1:1500
time(k) = k*ts;
%RBF网络的输入,控制量和系统上一个输入量
u(k) = sin(5*2*pi*k*ts);
y(k) = u(k)^3 + y_1/(1 + y_1^2);
x(1) = u(k);
x(2) = y(k);
%网络预测的输入
for j = 1:1:3
h(j) = exp(-norm(x - c(:,j))^2/(2*b(j)*b(j)));
end
ym(M,k) = w_1'*h';
%预测输出和实际输出的误差
e(M,k) = y(k) - ym(M,k);
%调整权值
d_w = 0*w;d_b = 0*b;d_c = 0*c;
for j = 1:1:3
d_w(j) = xite*e(M,k)*h(j);
d_b(j) = xite*e(M,k)*w(j)*h(j)*(b(j)^-3)*norm(x-c(:,j))^2;
for i = 1:1:2
d_c(i,j) = xite*e(M,k)*w(j)*h(j)*(x(i) - c(i,j))*(b(j)^-2);
end
end
w = w_1 + d_w + alfa*(w_1 - w_2);
b = b_1 + d_b + alfa*(b_1 - b_2);
c = c_1 + d_c + alfa*(c_1 - c_2);
y_1 = y(k);
w_2 = w_1;
w_1 = w;
c_2 = c_1;
c_1 = c;
b_2 = b_1;
b_1 = b;
end
end
figure(1)
plot(e(1,:));
hold on
plot(e(2,:),'r');
hold on
plot(e(3,:),'g');
title('各种算法对应的预测误差')
legend('PSO_RBF优化误差','GA_RBF优化误差','RBF优化误差')
xlabel('进化次数');
ylabel('预测误差');
figure(2)
plot(y,'y');
hold on
plot(ym(1,:),'b');
hold on
plot(ym(2,:),'r');
hold on
plot(ym(3,:),'g');
title('各种算法对应的系统预测输出')
legend('实际输出','PSO_RBF预测输出','GA_RBF预测输出','RBF预测输出')
xlabel('进化次数');
ylabel('预测误差');
2012-9-5 22:50 上传
点击文件名下载附件
6.42 KB, 下载次数: 54133
程序