- Pattern search: https://www.mathworks.com/help/gads/patternsearch.html?s_tid=doc_ta#buxdit7-options
- Genetic algorithm:https://www.mathworks.com/help/gads/genetic-algorithm-options.html#f17843
- Simulated annealing:https://www.mathworks.com/help/gads/simulated-annealing-options.html#bq26j8s-8
- Surrogate optimization: https://www.mathworks.com/help/gads/surrogate-optimization-options.html#mw_26e25441-83d3-4b43-a67a-089e5cc92634
Optimization algorithms output function
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am testing global optimizers (patternsearch, ga, particleswarm, surrogateopt and simulannealbnd). I need to extract X,Y and angle data from every iteration. There are some answers on the forum and I have managed to get data for pso and patternsearch but not for others because every output function must be different based on how the solver outputs data.
Is it possible to implement this functionality into every solver? It seems odd that is possible to show real time plots of the best function value and iteration data but there is no command to store values in a beginner friendly way. Or is there one output function which can be used for all the solvers with minimal changes? For example function @pswoutfun works for pso, but in every other solver it states: Too many input (or output) arguments. What I need to be careful about when changing the function @pswoutfun so it will work for other solvers?
%% PARTICLESWARM OPTIMIZATION
clear;clc;close all
rng default % Data repeatability
lb = [-0.34,-0.85, 0]; %Lower bounds 0.25 is good
ub = [0.34,-0.25, 360]; %Upper bounds
nvars=3; %Number of variables
%Function Tolerance (default is 1e-6), we use 1e-2
tol=1e-2;
%PSO Options
options=optimoptions('particleswarm','Display','iter','PlotFcn','pswplotbestf','FunctionTolerance',tol,'OutputFcn',@pswoutfun);
%PSO function
[xopt,fval,exitflag,output]=particleswarm(@ManipulabilityFun,nvars,lb,ub,options);
x_mm=xopt(1)*1000
y_mm=xopt(2)*1000
z_mm=-95.845;
angle=xopt(3)
rot_matrix=rotz(angle,'deg'); %Rotation matrix
pos_vec=[x_mm,y_mm,z_mm]'; %Position vector
Part_transf=rt2tr(rot_matrix,pos_vec); %Homogeneus transformation matrix
%% DIRECT SEARCH (patternsearch)
clear;clc;
rng default
lb = [-0.34,-0.85,0];
ub = [0.34,-0.25,360];
x0=[0,0,0];
options = optimoptions('patternsearch','Display','iter','PlotFcn','psplotbestf','OutputFcn',@pswoutfun);
[xopt,fval,exitflag,output] = patternsearch(@ManipulabilityFun,x0,[],[],[],[],lb,ub,[],options);
%% GENETIC ALGORITHM (GA)
clear;clc;
rng default
lb = [-0.34,-0.85,0];
ub = [0.34,-0.25,360];
nvars = 3;
options = optimoptions('ga','PlotFcn', @gaplotbestf,'OutputFcn',@pswoutfun);
[xopt,fval,exitflag,output]=ga(@ManipulabilityFun,nvars,[],[],[],[],lb,ub,[],options);
%% SIMULATED ANNEALING
clear;clc;
rng default
lb = [-0.34,-0.85,0];
ub = [0.34,-0.25,360];
x0=[0,0,0];
options = optimoptions('simulannealbnd','Display','iter','PlotFcns',{@saplotbestx,@saplotbestf,@saplotx,@saplotf},'OutputFcn',@pswoutfun,MaxIterations=20);
[xopt,fval,exitflag,output] = simulannealbnd(@ManipulabilityFun,x0,lb,ub,options);
%% SURROGATE OPTIMIZATION
clear;clc;
rng default
lb = [-0.34,-0.85,0];
ub = [0.34,-0.25,360];
options = optimoptions('surrogateopt','PlotFcn','surrogateoptplot','OutputFcn',@pswoutfun);
[xopt,fval,exitflag,output] = surrogateopt(@ManipulabilityFun,lb,ub,[],[],[],[],[],options);
function stop = pswoutfun(optimValues,state)
persistent hist
stop = false;
switch state
case 'init'
hist = [0,optimValues.bestfval,optimValues.bestx];
case 'iter'
nextline = [optimValues.iteration,optimValues.bestx,optimValues.bestfval];
hist = [hist;nextline];
case 'done'
assignin('base','hist',hist);
end
0 commentaires
Réponses (1)
Avadhoot
le 15 Jan 2024
I understand that you are trying to define a custom output function for your experiment with different global optimizers. You are correct in passing the output function handle as a parameter for the “OutputFcn” property in the “optimoptions” function. The error you are getting is because there are different formats for defining the output function for different global optimization algorithms.
The structure of the output function that you are following is correct for particle swarm optimization method. For all the other algorithms, the structure of the output function is as follows:
1. Pattern search:
stop = outfun(x,optimValues,state)
Here “x” is the best point computed by the algorithm for the current iteration, “optimValues” contains a structure containing data for current iteration and “state” is the current state of the algorithm.
2. Genetic algorithm:
[state,options,optchanged] = myfun(options,state,flag)
Do not change the values of “options” and “state” parameters, just calculate the desired output in “optchanged”.
3. Simulated Annealing:
[stop,options,optchanged] = myfun(options,optimvalues,flag)
Here “options” parameter is defined by using the “optimoptions” function, the “optimvalues” is a structure containing information about current iteration and “flag” can take 3 values, i.e “init”, “iter” and ”done”.
4. Surrogate Optimization:
stop = outfun(x,optimValues,state)
This structure is like the structure for pattern search.
You need to format your output function appropriately for each algorithm. For more information about the structure of output functions please refer to the following documentation:
Hope it helps.
0 commentaires
Voir également
Catégories
En savoir plus sur Direct Search dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!