fmincon does not call output function
Show older comments
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
Torsten
on 19 Dec 2017
...,@(X) Constraints(C),...
looks strange in your call to fmincon.
Best wishes
Torsten.
GForce
on 19 Dec 2017
Torsten
on 19 Dec 2017
So C is some function of X which already includes the constraints ?
GForce
on 19 Dec 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
on 19 Dec 2017
Matt J
on 19 Dec 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.
Answers (1)
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()
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!