Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Optimization using fmincon and saving at iteration evaluation but not every functional evaluation

1,413 views
Skip to first unread message

Jesse Sipple

unread,
Jul 21, 2011, 2:58:09 PM7/21/11
to
I'm using fmincon in MATLAB to run an optimization routine and I was wondering if anyone knows how to save trigger MATLAB to save only the information I want to save from the functions inside my optimization for every iteration and NOT every functional evaluation. I have it so I store the values in my function within the optimization but it does it for every functional evaluation. The total number of iterations it takes my optimization is 12 but obviously there is about 50 functional evaluations. I want to save the data from only those 12 "iterations" and I'm not sure how to a) go back and find which functional evaluations turned into an iteration or b) just save it when the function evaluation turns into an iteration. Thanks!

Matt J

unread,
Jul 21, 2011, 3:45:12 PM7/21/11
to
Maybe using the OutputFcn option.

Jesse Sipple

unread,
Jul 21, 2011, 4:29:09 PM7/21/11
to
"Matt J" wrote in message <j09vk8$f75$1...@newscl01ah.mathworks.com>...

> Maybe using the OutputFcn option.

I tried to use that but it seems that you can only use that for things output by the actual optimization routine itself, such as step size, iteration number, etc.

Matt J

unread,
Jul 21, 2011, 4:41:09 PM7/21/11
to
"Jesse Sipple" <jesse....@tufts.edu> wrote in message <j0a26l$mt7$1...@newscl01ah.mathworks.com>...

> "Matt J" wrote in message <j09vk8$f75$1...@newscl01ah.mathworks.com>...
> > Maybe using the OutputFcn option.
>
> I tried to use that but it seems that you can only use that for things output by the actual optimization routine itself, such as step size, iteration number, etc.
====================

No, that's not true. If you study this example,

http://www.mathworks.com/help/toolbox/optim/ug/brhkghv-56.html#brjhnpu

you will see that it uses nested functions to share variables between
the objective function and the OutputFcn. The shared variables can be any quantity that you want.

Rakesh Kumar

unread,
Jul 22, 2011, 9:46:08 AM7/22/11
to
"Matt J" wrote in message <j0a2t5$p0i$1...@newscl01ah.mathworks.com>...

I think the example does not save 'all' the function calls. Notice the solver performs 8 iterations (9 if you include the 0th iteration) so, the history contains only 9 data points. The total number of function call made in this example is more than 9.

The output function is not going to work in this case. However, you can use the example as a starting point for this case as well.

You should use a nested objective function and store the values of 'x' and 'fval' every time the objective and/or constraint is called. I have an example for you:

function [objhistory,constrhistory] = saveSolverHistory

% Set up shared variables with OUTFUN
objhistory.x = [];
objhistory.fval = [];

constrhistory.x = [];
constrhistory.c = [];
constrhistory.ceq = [];

% call optimization
x0 = [-1 1];
options = optimset('display','iter',...
'Algorithm','active-set');
xsol = fmincon(@objfun,x0,[],[],[],[],[],[],@confun,options);

function f = objfun(x)
f = exp(x(1))*(4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) +...
2*x(2) + 1);
objhistory.fval = [objhistory.fval f];
objhistory.x = [objhistory.x x(:)];
end

function [c, ceq] = confun(x)
% Nonlinear inequality constraints
c = [1.5 + x(1)*x(2) - x(1) - x(2);
-x(1)*x(2) - 10];
% Nonlinear equality constraints
ceq = [];
% Keep the points in the history
constrhistory.x = [constrhistory.x x(:)];
constrhistory.c = [constrhistory.c c];
constrhistory.ceq = [constrhistory.ceq ceq];
end
end

Mark T

unread,
May 6, 2014, 1:31:10 PM5/6/14
to
Hi,
looks like I have the same issue with fminsearch. In my case, the objective function uses some additional parameters that it updates internally during the fitting (i.e. passing values from one call to the next using globals...). But it should only update these additional parameters if the call corresponds to the end of an iteration.

I checked whether the very last function evaluation preceding the OutputFcn call corresponds to the end of an iteration, but this is seemingly not the case in general.

Unfortunately, my objective function calls are relatively expensive, otherwise I would just make a redundant call of the objective function again from inside the OutputFcn using the 'x' values it receives (and add an input flag parameter so my objective function knows it is being called end-of-iteration).

So, I guess the only solution I can think of is to maintain the recent sets of fit parameters in a global 2D array (as well as my auxiliary parameters in a second array), and match up with the parameters sent to my OutputFcn each time it is called. Doesn't feel very elegant.

Any other suggestions would be greatly appreciated.
0 new messages