What is the calling order of the objective function and nonlinear constraint function for Optimization Toolbox functions?
4 views (last 30 days)
Show older comments
MathWorks Support Team
on 27 Jun 2009
Edited: MathWorks Support Team
on 21 Aug 2017
I am using functions in the Optimization Toolbox to optimize a function that has nonlinear constraints. The nonlinear constraint function is dependent on the objective function value of the current point "x". Since the objective function value is already being computed for each "x", I am trying to avoid having to recompute this within the nonlinear constraint function. However, I am uncertain whether the algorithm in the optimization functions will always be calling the objective function prior to calling the nonlinear constraint function.
As an example, consider the following code which uses the FMINCON function along with two nested functions:
function [x,fval] = runsharedvalues(a,b,c,d,lower)
objval = []; % Initialize shared variables
xcheck = [];
x0 = [-1 1]; % Initial guess
options = optimset('LargeScale','off');
[x,fval] = fmincon(@objfun,x0,[],[],[],[],[],[],@constrfun,options);
% Objective function
function f = objfun(x)
% Variable objval shared with constrfun
objval = exp(x(1))*(a*x(1)^2+b*x(2)^2+c*x(1)*x(2)+d*x(2)+1);
f = objval;
end
% Constraint function
function [c,ceq] = constrfun(x)
c(1) = -objval + lower;
c(2:3) = [1.5 + x(1)*x(2) - x(1) - x(2); -x(1)*x(2) - 10];
ceq = [];
end
end
Note that the constraint function is using the "objval" that was computed within the objective function. However, if the value of "x" has been changed within the optimization function prior to calling the constraint function, then I will be using an incorrect value for "objval".
I would like to guarantee that the "objval" being used in the constraint function is the objective function value for the current "x".
Accepted Answer
MathWorks Support Team
on 21 Aug 2017
Edited: MathWorks Support Team
on 21 Aug 2017
In general, the Optimization Toolbox functions do call the objective function prior to calling the nonlinear constraint function. However, there may be instances where the nonlinear constraint function is being passed a specific point "x" prior to the objective function being called with the same "x".
For the given example, to ensure that the value used for "objval" corresponds to the objective function value for "x", you can use the following modified code:
function [x,fval] = runsharedvalues(a,b,c,d,lower)
objval = []; % Initialize shared variables
xcheck = [];
x0 = [-1 1]; % Initial guess
options = optimset('LargeScale','off');
[x,fval] = fmincon(@objfun,x0,[],[],[],[],[],[],@constrfun,options);
% Objective function
function f = objfun(x)
% Variable objval shared with constrfun
objval = exp(x(1))*(a*x(1)^2+b*x(2)^2+c*x(1)*x(2)+d*x(2)+1);
f = objval;
xcheck = x; % xcheck share with constrfun
end
% Constraint function
function [con,ceq] = constrfun(x)
% Reuse the value of objval, unless the current x is not equal to
% the x that was used to compute objval in objfun - in which case
% objval has to be recomputed
if any(x ~= xcheck)
objval = exp(x(1))*(a*x(1)^2+b*x(2)^2+c*x(1)*x(2)+d*x(2)+1);
end
con(1) = -objval + lower;
con(2:3) = [1.5 + x(1)*x(2) - x(1) - x(2); -x(1)*x(2) - 10];
ceq = [];
end
end
Note that the value of "x" has been assigned to a temporary variable "xcheck". Then, when the nonlinear constraint function is called, we can verify that the "objval" being used corresponds to the current point "x". If it does not, then the nonlinear constraint function recomputes the objective function value.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!