Borrar filtros
Borrar filtros

saving output at every iteration in simulated annealing

9 visualizaciones (últimos 30 días)
Pragya Srivastava
Pragya Srivastava el 17 de Mayo de 2017
Comentada: RoboTomo el 5 de Abr. de 2022
Hi, I would like to record the output of the following at every iteration step to specially see how re-annealing is working (how many iteration steps and by what amount). Could you please point me to an example of the syntax on how to do this ?
options = optimoptions(@simulannealbnd... ,'PlotFcn',{@saplotf,@saplotx,@saplotbestf,@saplottemperature}); options.Display='iter'; options.DisplayInterval=1; options.InitialTemperature=[10]; options.MaxFunctionEvaluations=12000; options.ReannealInterval=10; options.FunctionTolerance=10^(-5);
[pot_sa,fv_sa,exf_sa,output_sa] = simulannealbnd(obj_fun,x0,[0 0],[],options);

Respuestas (2)

Cam Salzberger
Cam Salzberger el 17 de Mayo de 2017
Hello Pragya,
I am not sure whether you are trying to customize the data that is printed each iteration, or save certain parts of the iteration data to a file for later examination. Either way though, I think that specifying the 'OutputFcn' in "optimoptions" to a custom function is the way to go. You can either change the display, or save the data to file, in an entirely custom manner.
-Cam
  1 comentario
Pragya Srivastava
Pragya Srivastava el 17 de Mayo de 2017
Thank you. I was looking to save the printed data at every iteration to a file. I tried looking at the OutputFcn but I did not quite understand how to use it. This is why an example of it will be highly appreciated.

Iniciar sesión para comentar.


Pragya Srivastava
Pragya Srivastava el 19 de Mayo de 2017
Hello, I am trying to write custom function for 'OutputFcn' to be used in simulated annealing.
options = saoptimset('OutputFcn',@outfun); However I am lost on how to write outfun which will allow me to save the temperature at each iteration.
I know that the syntax should probably be something like
stop=outfun(x, optimvalues,state) stop=false; mat=[]; mat=[mat state.Temperature] dlmwrite('Temp.dat','mat')
However I do not know if this is the syntax for what I want to do. I am not even sure if I need to define mat in the main file when I call the simulannealbnd routine. If yes, then what is the structure of outfun.
A help will be very much appreciated on this. Thanks.
  2 comentarios
Alan Weiss
Alan Weiss el 19 de Mayo de 2017
Editada: Alan Weiss el 19 de Mayo de 2017
The documentation is here for Simulated Annealing. The output function should have the syntax
[stop,options,optchanged] = myfun(options,optimvalues,flag)
The optimvalues argument has the following data:
  • x — Current point
  • fval — Objective function value at x
  • bestx — Best point found so far
  • bestfval — Objective function value at best point
  • temperature — Current temperature, a vector the same length as x
  • iteration — Current iteration
  • funccount — Number of function evaluations
  • t0 — Start time for algorithm
  • k — Annealing parameter, a vector the same length as x
The solver passes in options, optimvalues, and flag to your function. Your function should at least set the stop output to false so that the solver does not stop.
For an example of a custom output function for the ga solver, which has a different syntax, but still might help you, see this example.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
RoboTomo
RoboTomo el 5 de Abr. de 2022
Why is it so complicated to obtain iteration data from the objective function for optimization algoritms? Is it possible to implement additional command to global solvers in Matlab (pso,ga,sa,etc.) to make this easier?
Here I have an output function (from Matlab answers) that works with direct search but not with simulated annealing.
Error code: 'Unrecognized method, property, or field 'fval' for class 'optim.options.SimulannealbndOptions'.
I guess it has something to do with data storing, because the outputs are little different?
function history = saoutfun
% Initialize history fields to store iterative data
history.x = [];
history.fval = [];
%% Simulated annealing options
rng default
lb = [-0.34,-0.85,0];
ub = [0.34,-0.25,360];
x0 = [0 0 0];
options = optimoptions('simulannealbnd','Display','iter','PlotFcn','saplotbestf','OutputFcn',@outfun,MaxIterations=20);
[xopt,fval,exitflag,output] = simulannealbnd(@ManipulabilityFun,x0,lb,ub,options);
%% OutputFcn for patternsearch
% IMPORTANT: optimValues contains data from the current iteration.
% We will use this parameter in order to access 'x' and 'fval' information
function [stop,options,optchanged] = outfun(optimValues,options,flag)
stop = false; % Let algorithm continue to next iteration
optchanged = false; % We are not making any changes to 'options'
switch flag % Current state in which the output is called
case 'iter'
% Concatenate current point and objective function
% value with history. x must be a row vector.
history.fval = [history.fval; optimValues.fval];
history.x = [history.x; optimValues.x];
otherwise
end
end
end

Iniciar sesión para comentar.

Categorías

Más información sobre Simulated Annealing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by