fmincon does not call output function

MATLAB R2016b
I am using fmincon, and would like to run an output function to save data of each iteration.
I set the options and call fmincon as follows:
options = optimoptions('fmincon','Display','iter','Algorithm','sqp','DiffMinChange',0.01,'DiffMaxChange',0.5,'OutputFcn',@outf,'PlotFcn',@optimplotfval);
[x,fval,exitflag,output] = fmincon(@(X) Start(X,initval,C),X0,[],[],[],[],lb,ub,@(X) Constraints(C),options);
My output function is
function stop = outf(x,optimValues,state)
tenditer=toc;
stop=false;
fid = fopen('history_fuel.txt', 'at');
fprintf(fid,'%f \n',optimValues.fval);
fclose(fid);
%...
%snip
%...
tic;
end
Fmincon iterates without problems, but never calls the output function, no file written and I double-checked by adding a debug-pause in my output function which is never triggered. I just followed the documentation and now I don't see what went wrong.
My question is: Why does it not call the output function and how can I do it?
Question 2: Also the PlotFcn is supposed to do something every iteration or not? In case it is supposed to, that one does not do anything either.

7 Comments

...,@(X) Constraints(C),...
looks strange in your call to fmincon.
Best wishes
Torsten.
It used to be @(X) Constraints(X,C), but then I changed it to pass variables on through global variables to avoid making the same big calculation again. And then I did not need X anymore in Constraints and removed it.
Would that give problems? It does work when I run it, it evaluates the constraints without problems.
So C is some function of X which already includes the constraints ?
No sorry forgot to mention, C is just a vector of constants. These constants together with the global variables from the objective function are used to calculate the constraints
Ok.
What about changing
options = optimoptions('fmincon',...
to
options = optimoptions(@fmincon,...
You know this page
https://de.mathworks.com/help/optim/ug/output-functions.html#brjhnpu
?
Best wishes
Torsten.
Nope that does not help unfortunately
No sorry forgot to mention, C is just a vector of constants. These constants together with the global variables from the objective function are used to calculate the constraints.
You are making a big assumption that the objective function will always be evaluated before the constraints. It would be better if you followed the guidelines here.

Sign in to comment.

Answers (1)

Matt J
Matt J on 19 Dec 2017
Edited: Matt J on 19 Dec 2017
What happens when you insert the debug pause again and call the OutputFcn directly from the options object
options = optimoptions('fmincon','Display','iter','Algorithm','sqp',...
'DiffMinChange',0.01,'DiffMaxChange',0.5,...
'OutputFcn',@outf,'PlotFcn',@optimplotfval);
options.OutputFcn()

2 Comments

Then it does enter into the 'outf' function and pause there
Hmmm. Have you tried restarting MATLAB, rebooting the computer, etc...?

Sign in to comment.

Asked:

on 19 Dec 2017

Commented:

on 19 Dec 2017

Community Treasure Hunt

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

Start Hunting!