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 Kommentare

Torsten
Torsten am 19 Dez. 2017
...,@(X) Constraints(C),...
looks strange in your call to fmincon.
Best wishes
Torsten.
GForce
GForce am 19 Dez. 2017
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.
Torsten
Torsten am 19 Dez. 2017
So C is some function of X which already includes the constraints ?
GForce
GForce am 19 Dez. 2017
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
Torsten
Torsten am 19 Dez. 2017
Bearbeitet: Torsten am 19 Dez. 2017
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.
GForce
GForce am 19 Dez. 2017
Nope that does not help unfortunately
Matt J
Matt J am 19 Dez. 2017
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 19 Dez. 2017
Bearbeitet: Matt J am 19 Dez. 2017

0 Stimmen

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 Kommentare

GForce
GForce am 19 Dez. 2017
Then it does enter into the 'outf' function and pause there
Matt J
Matt J am 19 Dez. 2017
Hmmm. Have you tried restarting MATLAB, rebooting the computer, etc...?

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 19 Dez. 2017

Kommentiert:

am 19 Dez. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by