MATLAB Answers

How to get values for each iteration on optimization toolbox

106 views (last 30 days)
Hugo
Hugo on 30 Aug 2013
Answered: Daniel Frisch on 7 Oct 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 Comment
Craig
Craig on 7 Mar 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;

Sign in to comment.

More Answers (2)

Alan Weiss
Alan Weiss on 30 Aug 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 Comment
Hugo
Hugo on 30 Aug 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

Sign in to comment.


Daniel Frisch
Daniel Frisch on 7 Oct 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.

Community Treasure Hunt

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

Start Hunting!