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];
回答(1 个)
Walter Roberson
2019-11-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 个评论
Odesanmi Gbenga Abiodun
2019-11-4
Walter Roberson
2019-11-4
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.
Odesanmi Gbenga Abiodun
2019-11-4
Walter Roberson
2019-11-8
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.
Odesanmi Gbenga Abiodun
2019-11-8
Walter Roberson
2019-11-8
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.
Odesanmi Gbenga Abiodun
2019-11-9
Walter Roberson
2019-11-9
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Particle Swarm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!