Output function for particleswarm command
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Ahmed Hussein
am 15 Jun. 2017
Bearbeitet: Ahmed Hussein
am 15 Jun. 2017
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.
Antworten (1)
Alan Weiss
am 16 Jun. 2017
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 Kommentar
Farshina Nazrul Shimim
am 12 Aug. 2020
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
Siehe auch
Kategorien
Mehr zu Particle Swarm finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!