Output function for particleswarm command
조회 수: 12 (최근 30일)
이전 댓글 표시
Hi I'm trying to extract all the best values during particle swarm iterations using built in particle swarm function of matlab
if true
% code
options = optimoptions('particleswarm','SwarmSize',100,'display','iter','PlotFcn',@pswplotbestf,'OutputFcn',@outfun)
x=particleswarm(fun,nvars,lb,ub,options)
end
So I've to use an output function to do that. Unfortunately, the descriptions in Matlab help didn't help. Until now, I've been able to show them on command window through this code:
if true
% code
function stop = outfun(optimValues,state)
stop = false;
switch state
case 'iter'
fprintf('%g %0.30f\n',[optimValues.iteration;optimValues.bestfval]')
% ya(optimValues.iteration,1)=optimValues.bestfval;
% assignin('base', 'var', ya)
end
end
end
As you can see in the last two lines, I've tried to record the best values but it didn't work. It gives me a vector of zeros to the length of iterations (optimValues.iteration) and records the last best value only in the last cell as shown below:
if true
% code
var
var =
1.0e-04 *
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0.2915
end
Can anyone help me write the right code to record the best values (optimValues.bestfval) in all iterations? P.S.: I'm using Matlab R2015a
Best Regards
댓글 수: 1
Ahmed Hussein
2017년 6월 15일
편집: Ahmed Hussein
2017년 6월 15일
This could simply be done using a global variable. However, I do not think this is the optimum solution. In my opinion the particleswarm implementation is still missing a lot of functionality, compared to ga implementation. In ga the best value in all iterations is already available in the state variable along with other useful information. For reference here is the Global Variable method:
function stop = outfun(optimValues,state)
global BstFval
stop = false;
switch state
case 'iter'
BstFval(optimValues.iteration) = optimValues.bestfval;
end
The variable is then accessible from anywhere. I would also appreciate if anyone has figured a better method to do this.
답변 (1개)
Alan Weiss
2017년 6월 16일
Try this function:
function stop = pswoutfun(optimValues,state)
persistent hist
stop = false;
switch state
case 'init'
hist = [0,optimValues.bestfval];
case 'iter'
nextline = [optimValues.iteration,optimValues.bestfval];
disp(nextline)
hist = [hist;nextline];
case 'done'
assignin('base','hist',hist);
end
Do you think that this is this worth giving as an example in the documentation?
Alan Weiss
MATLAB mathematical toolbox documentation
댓글 수: 1
Farshina Nazrul Shimim
2020년 8월 12일
Hello Alan,
This is indeed very helpful and would be great if it were in the documentation. In my opinion, the documentation for PSO should have more explanations, specially with optimoptions. I keep getting errors for a lot of parameter tuning attempts.
Best,
Farshina
참고 항목
카테고리
Help Center 및 File Exchange에서 Particle Swarm에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!