How to get values for each iteration on optimization toolbox

18 visualizaciones (últimos 30 días)
Hugo
Hugo el 30 de Ag. de 2013
Respondida: Daniel Frisch el 7 de Oct. de 2020
Hello,
I am using MATLAB optimization toolbox. I want to know how can I get values on each iteration, such as objective function value, or variable values. I am using fmincon function. So long What I can get is only the final value of the variables and the final value of the objective function after some iterations. I want to get those values for each iteration, saving them anywhere, but preferably on a .txt file or displaying them on the screen.
Thanks and regards, Hugo Silva, Eng. MSc.
  1 comentario
Craig
Craig el 7 de Mzo. de 2019
Editada: Craig el 7 de Mzo. de 2019
My own understanding has led to a different approach from those answers given to this query. For more formal and appropriate programming approaches to this problem, I would recommend following the methods given in those previous answers.
However, if you are just interested in interrogating and understanding what is happening in your model, and how the optimiser is progressing, then you can as easily use a simple interrogation of global variables to help you do this. And it is simple to later remove this code without effecting the resulting code.
In your top level .m script which calls the optimiser, declare a global variable, say 'variable_tracking_array'. In your objective function (the function that the optimiser calls to evaluate its performance) redeclare the same global variable. Then assign whatever variables from that function you wish to monitor each time the function is called.
If, in addition, you want to stack them in an array so you can see the entirety of the variable values and the sequence in which they were calculated, simply define a persistent variable, say 'n_calls' in your objective function, and increment it each time the function is called, and use it as the index to specify into which row (or column) you wish to place the current set of variables into the already globally defined variable array.
If you wish, you can supplement this data with the information available from specifying the options for displaying iteration information for the optimiser, in which case you get a fairly complete picture of the progress and 'thinking' of the optimiser, as it does its job.
Happy investigations!
example.
In the main calling script where your optimiser is called from, eg. mainscript.m...
global variable_tracking_array;%<-create a global for tracking data
variable_tracking_array = zeros(1000,3);%<-in this case we're tracking 3 variables
In your objective_function.m script that the optimiser calls using @objective_function put
the following near the start of your function amongst the variable declarations...
persistent n_calls;%<-this will be incrementing each time the function is called
if isempty(n_calls)
n_calls=0;
end
global variable_tracking_array;%<-this is so you can see the global variable
And in the same objective_function.m script, near the end of the function, before it exits, put the following...
n_calls=n_calls+1;%<-increments each time the objective_function completes
variable_tracking_array(n_calls,1)=variable_1;%<-you can supply any local variable name, not just variable_1 :-)
variable_tracking_array(n_calls,2)=variable_2;
variable_tracking_array(n_calls,3)=variable_3;

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 30 de Ag. de 2013
Editada: Matt J el 30 de Ag. de 2013
Use the OutputFcn option, such as in the example here

Más respuestas (2)

Alan Weiss
Alan Weiss el 30 de Ag. de 2013
The objective function value is available simply by turning on iterative display. Set the Display option to 'iter'. This section describes how to set options.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 comentario
Hugo
Hugo el 30 de Ag. de 2013
Hi,
Thanks for your reply,
I have it turned on, but I want the fval data saved on every iteration on a text file or displayed on the screen at the end of the process.
regards, Hugo Silva

Iniciar sesión para comentar.


Daniel Frisch
Daniel Frisch el 7 de Oct. de 2020
You can use my function
outputFcn_global.m
It stores the intermediate results in a global variable so you can inspect it later.
Using this function directly as 'OutputFcn' in the solver saves you the extra trouble writing it yourself.

Categorías

Más información sobre Solver Outputs and Iterative Display 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