Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
fmincon troubles

Subject: fmincon troubles

From: Simon Preston

Date: 19 Jul, 2011 09:47:28

Message: 1 of 3

Hi,

I am having some difficulties with fmincon and wondered if somebody might be able to advise. I want to minimise an objective function objFunc w.r.t. variable theta, subject to the condition that some matrix function of theta, C(theta), is positive definite. I know that the feasible region contains a unique solution, and also that it includes the point theta0 = [0 0]. Sketched code for my objective function and constraint is as follows.

% ---------------------------------------------------------------------

function [out1, out2] = objFunc(theta,otherpars)

C = calc_C(theta,otherpars);
Cinv = inv(C);
out1 = calc_K(Cinv,otherpars) - sum(theta);

if nargout > 1
  out2 = calc_K1(Cinv,otherpars) - 1;
end

% ---------------------------------------------------------------------

function [c,ceq] = nonlcon(theta,otherpars)
C = calc_C(theta,otherpars);
c = -min(eig(C)); % Nonlinear inequality at theta.
ceq = []; % No nonlinear equalities at theta.

% ---------------------------------------------------------------------

For particular choices of “otherpars” I can use fminunc and the constraint doesn’t come into play, e.g. “thetaHat = fminunc(@(theta) objFunc(theta,otherpars), theta0);” returns

                               Norm of First-order
Iteration f(x) step optimality CG-iterations
    0 0 286
    1 -8.29312 0.0364222 128 1
    2 -14.0235 0.0560131 57.5 1
                        ….
    12 -25.077 1.55671e-06 1.62e-12 1
    13 -25.077 1.90914e-12 0 1

Optimization completed: The first-order optimality measure, 0.000000e+00,
is less than the selected value of options.TolFun = 1.000000e-14, and no
negative/zero curvature is detected in the trust-region model.

But when I try using fmincon instead, with the *same value of “otherpars”*, i.e. with the call “fmincon(@(theta) objFunc(theta,otherpars), theta0, [],[],[],[],[],[], @(theta) nonlcon(theta,otherpars));”, things blow up and the search seems to be exploring values of theta that don’t respect the contraint:


                              Max Line search Directional First-order
Iter F-count f(x) constraint steplength derivative optimality Procedure
    0 3 0 -0.1
    1 11 -3.70977 -23.39 0.0625 -341 0.941
    2 15 -3.87583 -22.88 1 -0.63 0.941
    3 19 -442.068 0.001148 1 -0.642 7.64e+05 Hessian modified
    4 38 -1123.75 0.0004496 3.05e-05 -7.36e+05 4.99e+06
                              …..........
  13 345 -280205 1.801e-06 7.45e-09 -1.61e+11 3.11e+11 Hessian modified
  14 372 -424098 1.19e-06 1.19e-07 -3.11e+11 7.13e+11 Hessian modified twice
  15 401 -925468 5.452e-07 2.98e-08 -7.12e+11 3.4e+12 Hessian modified twice
  16 432 -2.93929e+07 1.716e-08 7.45e-09 -3.39e+12 3.42e+15 Hessian modified
                              …......

To repeat: I am trying to use fmincon for a problem in which I know there is a unique solution in the feasible region, and in which I know the starting point lies inside the feasible region. fmincon seems to fail in circumstances where using fminunc (ignoring the constraint) finds the correct solution.

Can anybody tell me what might be going wrong?

S

Subject: fmincon troubles

From: Alan Weiss

Date: 19 Jul, 2011 12:39:09

Message: 2 of 3

On 7/19/2011 5:47 AM, Simon Preston wrote:
> Hi,
>
> I am having some difficulties with fmincon and wondered if somebody
> might be able to advise. I want to minimise an objective function
> objFunc w.r.t. variable theta, subject to the condition that some matrix
> function of theta, C(theta), is positive definite. I know that the
> feasible region contains a unique solution, and also that it includes
> the point theta0 = [0 0]. Sketched code for my objective function and
> constraint is as follows.
>
> % ---------------------------------------------------------------------
>
> function [out1, out2] = objFunc(theta,otherpars)
>
> C = calc_C(theta,otherpars);
> Cinv = inv(C);
> out1 = calc_K(Cinv,otherpars) - sum(theta);
>
> if nargout > 1
> out2 = calc_K1(Cinv,otherpars) - 1;
> end
>
> % ---------------------------------------------------------------------
>
> function [c,ceq] = nonlcon(theta,otherpars)
> C = calc_C(theta,otherpars);
> c = -min(eig(C)); % Nonlinear inequality at theta.
> ceq = []; % No nonlinear equalities at theta.
>
> % ---------------------------------------------------------------------
>
> For particular choices of “otherpars” I can use fminunc and the
> constraint doesn’t come into play, e.g. “thetaHat = fminunc(@(theta)
> objFunc(theta,otherpars), theta0);” returns
>
> Norm of First-order
> Iteration f(x) step optimality CG-iterations
> 0 0 286 1 -8.29312 0.0364222 128 1
> 2 -14.0235 0.0560131 57.5 1
> ….
> 12 -25.077 1.55671e-06 1.62e-12 1
> 13 -25.077 1.90914e-12 0 1
>
> Optimization completed: The first-order optimality measure, 0.000000e+00,
> is less than the selected value of options.TolFun = 1.000000e-14, and no
> negative/zero curvature is detected in the trust-region model.
>
> But when I try using fmincon instead, with the *same value of
> “otherpars”*, i.e. with the call “fmincon(@(theta)
> objFunc(theta,otherpars), theta0, [],[],[],[],[],[], @(theta)
> nonlcon(theta,otherpars));”, things blow up and the search seems to be
> exploring values of theta that don’t respect the contraint:
>
>
> Max Line search Directional First-order
> Iter F-count f(x) constraint steplength derivative optimality Procedure
> 0 3 0 -0.1 1 11 -3.70977 -23.39 0.0625 -341 0.941 2 15 -3.87583 -22.88 1
> -0.63 0.941 3 19 -442.068 0.001148 1 -0.642 7.64e+05 Hessian modified 4
> 38 -1123.75 0.0004496 3.05e-05 -7.36e+05 4.99e+06 …..........
> 13 345 -280205 1.801e-06 7.45e-09 -1.61e+11 3.11e+11 Hessian modified 14
> 372 -424098 1.19e-06 1.19e-07 -3.11e+11 7.13e+11 Hessian modified twice
> 15 401 -925468 5.452e-07 2.98e-08 -7.12e+11 3.4e+12 Hessian modified
> twice 16 432 -2.93929e+07 1.716e-08 7.45e-09 -3.39e+12 3.42e+15 Hessian
> modified
> …......
>
> To repeat: I am trying to use fmincon for a problem in which I know
> there is a unique solution in the feasible region, and in which I know
> the starting point lies inside the feasible region. fmincon seems to
> fail in circumstances where using fminunc (ignoring the constraint)
> finds the correct solution.
>
> Can anybody tell me what might be going wrong?
>
> S

There could be a few different things going on.
1. Your nonlinear constraint is not smooth. That can sometimes cause
fmincon to behave badly.
2. Your starting point might be touchy. Try one nearby, such as x0 +
.1*randn(size(x0))
3. Try using the fmincon interior-point algorithm, which is much more
robust than the active-set algorithm (the one you were using).

Good luck,

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: fmincon troubles

From: Steve Grikschat

Date: 25 Jul, 2011 13:57:08

Message: 3 of 3

Alan Weiss <aweiss@mathworks.com> wrote in message
> There could be a few different things going on.
> 1. Your nonlinear constraint is not smooth. That can sometimes cause
> fmincon to behave badly.
> 2. Your starting point might be touchy. Try one nearby, such as x0 +
> .1*randn(size(x0))
> 3. Try using the fmincon interior-point algorithm, which is much more
> robust than the active-set algorithm (the one you were using).
>
> Good luck,
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

In addition to Alan's point about the non-smooth nature of your constraint, you might also check to make sure that C is symmetric. Fmincon may not handle complex constraint values. This is another reason to consider the interior-point algorithm (R2009a or later) since it can deal with them in a nicer way.

+Steve

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us