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".
Best Answer