saving output at every iteration in simulated annealing
9 views (last 30 days)
Show older comments
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);
0 Comments
Answers (2)
Cam Salzberger
on 17 May 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
Pragya Srivastava
on 19 May 2017
2 Comments
Alan Weiss
on 19 May 2017
Edited: Alan Weiss
on 19 May 2017
[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
on 5 Apr 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
See Also
Categories
Find more on Simulated Annealing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!