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

Discussion in 'MATLAB' started by Jesse Sipple, Jul 21, 2011.

1. ### Jesse SippleGuest

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!

Jesse Sipple, Jul 21, 2011

2. ### Matt JGuest

Maybe using the OutputFcn option.

Matt J, Jul 21, 2011

3. ### Jesse SippleGuest

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.

Jesse Sipple, Jul 21, 2011
4. ### Matt JGuest

====================

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.

Matt J, Jul 21, 2011
5. ### Rakesh KumarGuest

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

Rakesh Kumar, Jul 22, 2011