下面的程序是在论文里找到的用粒子群算法优化LQR的matlab程序,在运行的时候出现错误,希望各位大神能帮助解决一下,十分感谢!
%%%%%
函数名:
initialize_variables() %%%%%
function f = initialize_variables(N)
min = 0.1;
max = 400;
Vmax=1;
Vmin=-1;
%come to here
M=7;
K=8;
for i = 1 : N
% Initialize the decision variables
for j = 1 : M
f(i,j) = min+ (max - min)*rand(1); % rand means random
end
% Evaluate the objective function
f(i,8) = evaluate_objective(f(i,:));
for j=K+1:K+7
f(i,j)=Vmin + (Vmax - Vmin)*rand(1);
end
end
%%%%%
%%%%%
函数名:
initialize_variables()%%%%%
min = 0.1;
max = 400;
Vmax=1;
Vmin=-1;
%come to here
M=7;
K=8;
for i = 1 : N
% Initialize the decision variables
for j = 1 : M
f(i,j) = min+ (max - min)*rand(1); % rand means random
end
% Evaluate the objective function
f(i,8) = evaluate_objective(f(i,:));
for j=K+1:K+7
f(i,j)=Vmin + (Vmax - Vmin)*rand(1);
end
end
function f = check_band(x)
[M,N]=size(x);
for i=1:7
l_limit(i) = 0.1;
u_limit(i) = 400;
end
for i=1:M
for j=1:N
if x(i,j)>u_limit(j)
x(i,j)=u_limit(j);
elseif x(i,j)
x(i,j)=l_limit(j);
end
end
end
f=x;
function f = check_band_v(x)
[M,N]=size(x);
u_limit=1;
l_limit=-1;
for i=1:M
for j=1:N
if x(i,j)>u_limit
x(i,j)=u_limit;
elseif x(i,j)
x(i,j)=l_limit;
end
end
end
f=x;
function f = evaluate_objective(x)
f = [];
A=[0,0,0,1,0,0;0,0,0,0,1,0;0,0,0,0,0,1;0,0,0,0,0,0;0,86.69,-21.62,0,0,0;0,-40.31,39.45
,0,0,0];
B=[0,0,0,1,6.64,-0.088]';
C=[1,0,0,0,0,0;0,1,0,0,0,0;0,0,1,0,0,0];
D=0;
H=ss(A,B,C,D);
q=[x(1),0,0,0,0,0;0,x(2),0,0,0,0;0,0,x(3),0,0,0;0,0,0,x(4),0,0;0,0,0,0,x(5),0;0,0,
0,0,0,x(6)];
r=x(7);
k=lqr(H,q,r);
a=A-B*k;
h=ss(a,B,C,D);
T=0:0.1:7;
[output,time,stat_vect]=step(h,T);
%output
%stat_vect
sum_1=0;
for i=1:1:71
sum_1=sum_1+stat_vect(i,:)*q*stat_vect(i,:)'+k*stat_vect(i,:)'*r*k*stat_vect(i,:)';
end
f=sum_1;
附录
C
粒子群算法程序:
%%%%%
被调函数
1
:
initialize_variables()
种群初始化函数
%%%%%
%%%%%
被调函数
2
:
evaluate_objective()
计算个体适应度的目标函数
%%%%%
clear all
clc
pop = 30;%30
gen = 50;%50
M = 1; %the number of object 目标函数的位置
V = 7; %the number of chromosome 染色体的条数
wmax=0.9;
wmin=0.35;
c1=1.3;
c2=1.3;
for iter=1:gen
W(iter)=wmax-((wmax-wmin)/gen)*iter;
end %计算惯性权重 inertia weight
chromosome = initialize_variables(pop); %变量chromosome是一个30*15矩阵,里面包含30个个体的信息
for i=1:pop
pbest(i,:)=chromosome(i,1:8); %初始化个体极值pbest pbest为pop*8的一个矩阵
end
%初始化gbest
[temp,index] = sort(chromosome(:,M+V)); %以低M+V列为升序把数组chromosome排序
gbest=chromosome(index(1),:) %index也是一个数组,index(x)表示temp数组中第x行的元素在原来数组中的位置
for i = 1 : gen
x_temp=chromosome(:,1:7);
v_temp=chromosome(:,9:15);
pbest_temp=(:,1:7);
for j=1:pop
gbest_temp(j,:)=(1:7);
end
new=W(i)*v_temp+c1*rand*(pbest_temp-x_temp)+c2*rand*(gbest_temp-x_temp);
x_new=x_temp+v_new;
x_new=check_band(x_new);
for j=1:pop
f_object(j,:)=evaluate_objective(x_new(j,:)); %f_object为一个pop*1的矩阵
end
x_new(:,8)=f_object;
chromosome = x_new;
chromosome(1:pop,9:15)=v_new;
chromosome(pop+1:2*pop,1:7)=pbest;
for j=1:pop
if chromosome(j,8)
pbest(j,:)=chromosome(j,1:8);
end
end
chromosome=chromosome(1:pop,:);
chromosome(pop+1,1:7)=gbest(1:8);
[temp,index] = sort(chromosome(:,M+V));
gbest=chromosome(index(1),:);
chromosome=chromosome(1:pop,:);
solution(i)=gbest(8);
generation(i)=i;
if ~mod(i,10)
fprintf('%d\n',i);
end
end
figure(1)
ABC_object(chromosome(1,1:7));
figure (2)
plot(generation, solution );
title('QR op using pso');
xlabel('generation');
ylabel('solution');
2017-5-8 09:18 上传
点击文件名下载附件
4.1 KB, 下载次数: 23