Particle swarm optimization implementation error

@all please I need your suggestion and help on this code using pso
for num_acur= 1:1:num_aopt
options=optimset('MaxIter',400);
a_init=1*rand(4,1);
[a_opt, Fval, exitFlag]=fminunc(@W,a_init,options);
a_result(num_acur,:)=[a_opt',Fval,a_init',exitFlag];
end
Rewriting this Using Pso
for num_acur = 1:1:num_aopt
fun = @W;
lb = [-30,-30,-30,-30];
ub =[100,10,10,10];
% options = optimoptions('particleswarm','SwarmSize',100);
%options = optimoptions('particleswarm','SwarmSize',50,'HybridFcn',@fmincon);
nvars = 4;
[a_opt, Fval, exitFlag] = particleswarm(fun,nvars,lb,ub);%,options);
a_result(num_acur,:)=[a_opt',Fval,lb',exitFlag];
end
Error
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in code ()
a_result(num_acur,:)=[a_opt',Fval,lb',exitFlag];

Answers (1)

[a_opt, Fval, exitFlag] = particleswarm(fun,nvars,lb,ub);%,options);
a_result(num_acur,:)=[a_opt',Fval,lb',exitFlag];
The first output from particleswarm is a row vector with length equal to the number of variables -- so 1 x 4 in your case. The second output is the scalar function result.
You use conjugate transpose on the 1 x 4, which will give you a 4 x 1. You use it with [] with the scalar Fval, so you are trying to horizontally concatenate a vector with 4 rows and a vector with 1 row. That is not a permitted operation.
You need to either not transpose a_opt and lb, or else you need to use ; instead of , in the [] so that you construct a column.

8 Comments

Thanks for your response,this really work but it doesnt solve my problem yet. the output seems to converge easilly I dont know why... and while using fminuc it responds to nesseary input.
any suggestion please??
There are relatively few kinds of functions for which PSO is known to converge. It is not necessarily all that good for other kinds of functions.
It would help to have the code for W to test with.
x1=[0.89 0.7 0.6 0.4 0.5 0.2];
x2=[0.02 0.05 -0.02 0.01 -0.3 -0.03];
y=[0.7;1.6;0.9;1;0.2;0.4];
x=[x1;x2];
function A=W(k)
t=size(x,2);
Kern_Cov=ones(t,t);
for i=1:1:t
for j=1:1:t
Kern_Cov(i,j)=k(1).^2*exp((-1/2)*((x(:,i)-x(:,j))'*inv(diag([k(2).^2,k(3).^2]))*(x(:,i)-x(:,j))))+k(4).^2*uita(x(:,i),x(:,j));
end
end
A=-(-1/2.*(y'*inv(Kern_Cov)*y)-1/2*log(det(Kern_Cov))-t/2*log(2*pi));
end
function guita=uita(x1,x2)
if(x1==x2)
guita=1;
else
guita=0;
end
end
That code looks like it will give an error as x x1 and x2 are not defined inside the functions. You are using some of the variables as if they are shared variables, but I do not not see any enclosing function.
exactly but each time i compute the next point of x1 and x2 using this code below, it converge
for num_acur = 1:1:num_aopt
fun = @W;
lb = [-30,-30,-30,-30];
ub =[100,10,10,10];
% options = optimoptions('particleswarm','SwarmSize',100);
%options = optimoptions('particleswarm','SwarmSize',50,'HybridFcn',@fmincon);
nvars = 4;
[a_opt, Fval, exitFlag] = particleswarm(fun,nvars,lb,ub);%,options);
a_result(num_acur,:)=[a_opt',Fval,lb',exitFlag];
end
Are you defining W as a nested function with x being a shared variable used in W ? If so then we need your actual code.
No, their is another function for the nested x
Tota_number =find(a_result(:,5)==min(a_result(:,5)));
a_prama_tra=a_result(Tota_number,1:4);
a_prama = a_prama_tra
%[opt_x_current, fun_negEI, exitFlag]=fmincon(@nested,opt_x_init,[-1,1.6;1,-1.6],[-0.2;1],[],[],[G_low,E_low],[G_high,E_high],[]);
[opt_x_current, fun_negE, exitFlag]=particleswarm(@nested,2,lb,ub);
opt_x_nested=opt_x_current;
Bys_flag=0;
x=[x,opt_x_nested];
function Kern_Cov_xixj=Aug(xi,xj)
global a_prama
Kern_Cov_xixj=(a_prama(1)).^2*exp(-(1/2)*((xi-xj)'*inv(diag([(a_prama(2)).^2,( a_prama(3)).^2]))*(xi-xj)))+a_prama(4).^2*uita(xi,xj);
end
function E_ne_o=nested(X_opt)
global x y E_m E_v
y_n=size(y,1);
K=ones(y_n,y_n);
for i=1:1:y_n
for j=1:1:y_n
K(i,j)=Aug(x(:,i),x(:,j));
end
end
k=ones(y_n,1);
for i=1:1:y_n
k(i,1)=Aug(X_opt,x(:,i));
end
E_m=k'*inv(K)*y;
E_v=Aug(X_opt,X_opt)-k'*inv(K)*k;
if(E_v>0)
f_best=min(y);
u_star=(f_best-E_m)/sqrt(E_v);
E_o=(f_best-E_m).*normcdf(u_star)+sqrt(E_v).*normpdf(u_star);
else
E_o=0;
end
E_ne_o=-E_o;
end
There would need to be a function header at the top and an end at the bottom of that in order for that to make use of nested functions and shared variables.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!