fmincon user-supplied hessian inconsistency
Show older comments
I'm using fmincon to optimize a scalar function with this abridged set of options:
options = optimset('Display', 'iter-detailed','MaxFunEvals',10000,...
'Maxiter',1000,'TolX',1e-18,'TolFun',1e-18, 'GradObj', 'on',...
'Algorithm', 'trust-region-reflective', 'Hessian',...
'user-supplied');
The following is the call to fmincon, objective function, and constraint functi n). Note that all are nested functions allowing the passing of extra parameters and unnecessary details have been left out.
[X,fval, exitflag, output, lambda, grad, hessian] = fmincon(@nestedfcn, min, [], [], [], [], lb, [], @cfcn, options);
function [c, ceq] = cfcn(min)
for i = 1:length(min)
if i ~= 9
c(i) = -25000 + abs(min(i));
else
c(i) = -75000 + abs(min(i));
end
end
ceq = [];
end
function [y, grady, H] = nestedfcn(min)
a = F ./ min;
[u,sig,vol] = truss2d(nnod,nel,e,a,conn,x,bc,f);
y = vol;
for i = 1:length(min)
grady(i) = -F(i)*L / (sig(i)^2);
for j=1:length(min)
if i==j
H(i,j) = F(i)*L / (sig(i)^3);
else
H(i,j) = 0;
end
end
end
end
The answer does not come out as it is supposed to and the Hessian is, at every point, a diagonal matrix, while fmincon outputs a non-diagonal matrix. For brevity, I've left out the specific matrices, but if it helps to arrive at a solution, I can post those as well (I think the particular values are irrelevant at the moment as I think it is an implementation issue).
Answers (1)
Well, I for one, can't see why FMINCON would output a diagonal Hessian from what you've posted. The reason for that would have to be in details that you've omitted. Note, however, that the matrix returned by FMINCON will not be the Hessian purely of your objective function. As explained in the documentation, it will be the Hessian of the entire Lagrangian (i.e., it includes the Hessian of your constraints). So, if there are any other constraints you've omitted in an effort to simplify presentation, that could be an explanation.
Other than that, I see one red flag issue. The use of expressions abs(min(i)) in cfcn makes your constraints non-differentiable, violating smoothness assumptions of FMINCON. If you weren't aware of differentiability requirements, I assume you could have violated them in truss2d() as well. This could explain why you're not converging to the required point, especially (but not necessarily) if some of the desired min(i) lie near zero.
If the lb(i) you haven't shown are all >=0 then you can replace abs(min(i)) with just min(i) and that would solve the issue. A better solution though would be to rewrite the constraints you've shown as ub,lb bounds. For example
c(i) = -25000 + abs(min(i));
is equivalent to -25000<=min(i)<=25000 and you can use ub(i),lb(i) to express this constraint instead, merging with any lb that you already have.
4 Comments
Phil
on 13 Jul 2013
Matt J
on 13 Jul 2013
Well, there are a few tests I can recommend. You have two candidate solutions, one of which you think is the correct one and one of which fmincon thinks is the correct one.
minPhil = [25000 25000 -25000 -25000 -0.007 25000 25000 -25000 37500 -25000]
minFmincon = [25000 25000 -25000 -25000 25000 25000 25000 -25000 75000 -25000]
You should evaluate the objective function at both candidates (and make sure they both satisfy constraints to within TolCon). If minFmincon has the lower value, you will know that fmincon is correct, and you have an error either in your expectations or in the code you have provided to it. Otherwise, it means that minFmincon is a sub-optimal local minimum, and you simply chose a bad initial guess.
The second test you should do is to run fmincon again, this time with minPhil as the initial guess. If fmincon terminates without changing minPhil (much), you will verify that minPhil is at least a local minimum. Otherwise, your code or expectations are again in doubt.
Phil
on 17 Jul 2013
but my final outputted gradient is not equal to zero.
I'm not sure whether the gradient output is the gradient of the objective function or of the Lagrangian. You can experiment with a few simple problems to check. Either way, even at an unconstrained minimum, the gradient wouldn't be exactly zero. There are other stopping criteria besides the first order optimality measure that FMINCON uses to decide whether to stop iterating. There's TolX, TolFun, etc... It certainly won't wait until you land smack dab on top of an optimal solution.
Categories
Find more on Solver Outputs and Iterative Display 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!