How should I modify my MATLAB code (using fmincon) in order to use YALMIP to provide automatic differentiation for my constraints?

759 views
Skip to first unread message

KiDo Tom

unread,
May 30, 2015, 8:03:55 AM5/30/15
to yal...@googlegroups.com
Dear all:

I wrote a MATLAB code using fmincon to solve a (linear objective function but nonlinear constraint function) optimization problem. The results are not robust, so I decided to try YALMIP to provide automatic differentiation for fmincon. However, I'm a very new programmer, I have no idea how should I use YALMIP to do this, could you give some guidance?

My objective function:

function [ J, grad_obj ] = obj( States)


%% objective function
J = -States(10);


%% analytic gradient 
if nargout > 1
    
    grad_obj = [0;0;0;0;0;0;0;0;0;(-1);0];

end


end


My constraint function:

function [ c, ceq ] = ctr( a_lot_of_parameters, States )

My optimset for fmincon:

optNLP = optimset( 'Algorithm','sqp', 'UseParallel','always', 'Display', 'iter', 'MaxFunEval', 20000, 'MaxIter', 1000, 'TolX', 1e-10, 'TolCon', 1e-7, 'GradObj','on' );


My initial condition:

States0 = [States1_0, States2_0, States3_0, States4_0, States5_0, States6_0, States7_0, States8_0, States9_0, States10_0, States11_0];


My linear constraints to States:

lb = [0;0;0;0;0;0;0;0;(-1);(0);(-1)];
    
ub = [1;1;1;1;1;1;1;1;(1);(1);(1)];

I want to keep the all the settings I used for fmincon in MATLAB, just to use YALMIP to provide automatic differentiation.

I will be very grateful if anyone of you give me some detailed guidance. 


Sincerely

Mark L. Stone

unread,
May 30, 2015, 11:39:35 AM5/30/15
to yal...@googlegroups.com
Johan can weigh in if he pleases, but i believe YALMIP is providing exact first derivatives to FMINCON and other nonlinear solvers which can use them, without your having to specify this as an option to the solver,.  But YALMIP does not provide 2nd derivatives to solvers (nor can you, unless you call FMINCON directly without using YALMIP).

KiDo Tom

unread,
May 30, 2015, 12:20:37 PM5/30/15
to yal...@googlegroups.com
Hi Mark, thanks for the reply. So you mean YALMIP can provide automatic differentiation to MATLAB? And could you provide a guidance on how to do it? Very grateful for the help.

Mark L. Stone

unread,
May 30, 2015, 12:52:08 PM5/30/15
to yal...@googlegroups.com
I will have to defer to Johan whether what YALMIP does would be considered to be automatic differentiation vs. symbolic differentiation. But whatver it is called, YALMIP does it automatically for you, without you having to "ask".   You don't need to specify any solver option for differentiation of nonlinear constraints or objective function. Sit back and let YALMIP do the work.

Mark L. Stone

unread,
May 30, 2015, 12:57:42 PM5/30/15
to yal...@googlegroups.com
x = sdpvar(2,1)
optimize([x(1)^2+2*x(2)^3 <= 5, x(1)*x(2)^2 <= 7],x(1) + 5*x(2)^10,sdpsettings('solver','fmincon'))

YALMIP will provide first derivatives to FMINCON.

Mark L. Stone

unread,
May 30, 2015, 1:06:06 PM5/30/15
to yal...@googlegroups.com
Bug report (YALMIP R20150204 under MATLAB R2014A WIN64):

>> x = sdpvar(2,1)
>> optimize([x(1)^2+2*x(2)^3 <= 5, x(1)*x(2)^2 <= 7],x(1) + 5*x(2)^10,sdpsettings('solver','mosek','verbose',2))
Linear matrix variable 2x1 (full, real, 2 variables)

Warning(mskgpopt): The problem is badly formulated due to:
Warning(mskgpopt): Variable 1 can be fixed to zero because A(:,1)>=0.
Warning(mskgpopt): Variable 2 can be fixed to zero because A(:,2)>=0.

MOSEK Version 7.1.0.30 (Build date: 2015-5-4 11:25:39)
Copyright (c) 1998-2015 MOSEK ApS, Denmark. WWW: http://mosek.com
Platform: Windows/64-X86

Computer
  Platform               : Windows/64-X86 
  Cores                  : 8              

Problem
  Name                   :                
  Objective sense        : min            
  Type                   : GECO (general convex optimization problem)
  Constraints            : 3              
  Cones                  : 0              
  Scalar variables       : 5              
  Matrix variables       : 0              
  Integer variables      : 0              

Optimizer started.
Interior-point optimizer started.
Presolve started.
Eliminator - tries                  : 0                 time                   : 0.00           
Eliminator - elim's                 : 0              
Lin. dep.  - tries                  : 0                 time                   : 0.00           
Lin. dep.  - number                 : 0              
Presolve terminated. Time: 0.02   
Interior-point optimizer terminated. Time: 0.08.


MOSEK PRIMAL INFEASIBILITY REPORT.

Problem status: The problem is primal infeasible


Optimizer terminated. Time: 1.14   

Interior-point solution summary
  Problem status  : PRIMAL_INFEASIBLE
  Solution status : PRIMAL_INFEASIBLE_CER
  Dual.    obj: -1.#IND000000e+000  Viol.  con: 0e+000   var: 0e+000
Optimizer summary
  Optimizer                 -                        time: 1.14   
    Interior-point          - iterations : 0         time: 0.08   
      Basis identification  -                        time: 0.00   
        Primal              - iterations : 0         time: 0.00   
        Dual                - iterations : 0         time: 0.00   
        Clean primal        - iterations : 0         time: 0.00   
        Clean dual          - iterations : 0         time: 0.00   
        Clean primal-dual   - iterations : 0         time: 0.00   
    Simplex                 -                        time: 0.00   
      Primal simplex        - iterations : 0         time: 0.00   
      Dual simplex          - iterations : 0         time: 0.00   
      Primal-dual simplex   - iterations : 0         time: 0.00   
    Mixed integer           - relaxations: 0         time: 0.00   

ans =
    yalmiptime: 0.054674975875906
    solvertime: 2.391325024124094
          info: 'Unbounded objective function (MOSEK)'
       problem: 2

Mark L. Stone

unread,
May 30, 2015, 1:08:49 PM5/30/15
to yal...@googlegroups.com
Well, o.k, I guess this is the same "bug" as from https://groups.google.com/forum/#!category-topic/yalmip/eUclkaGMd78 .

KiDo Tom

unread,
May 30, 2015, 1:24:50 PM5/30/15
to yal...@googlegroups.com
Hi, Mark, thank you for your example, in my original MATLAB code, my objective function "obj.m" and constraint function "ctr.m" are standalone .m files, in this case, how should I put everything?

Mark L. Stone

unread,
May 30, 2015, 1:39:09 PM5/30/15
to yal...@googlegroups.com
The standard approach to do it in YALMIP is to declare YALMIP variables sdpvar, intvar, binvar, then enter your code into a YALMIP objective function and constraints.

However, see https://groups.google.com/forum/#!topic/yalmip/P5Wh7yqXFEI in which I have inquired about using constraints specified in a nonlcon function in YALMIP.
Message has been deleted
Message has been deleted

Mark L. Stone

unread,
May 30, 2015, 2:05:43 PM5/30/15
to yal...@googlegroups.com
You can do something like
States = sdpvar(11)
assign(States,States0) % assigns your starting value States0 to States

Include lb <= States <= ub in your constraints, and add any linear constraints, such as A * States <= b to your constraints.

In the optimize command, use
sdpsettings('solver','fmincon','usex0',1) % invokes fmincon and provides fmincon with starting value assigned to States.
Message has been deleted

Mark L. Stone

unread,
May 30, 2015, 2:13:43 PM5/30/15
to yal...@googlegroups.com
Just take the options from optimset, except for GradObj, and put them into sdpsettings, except that you need to prepend fmincon. to option name.  For example, MaxIter becomes fmincon.MaxIter .

Johan Löfberg

unread,
May 30, 2015, 2:21:56 PM5/30/15
to yal...@googlegroups.com
Correct.

Johan Löfberg

unread,
May 30, 2015, 2:23:12 PM5/30/15
to yal...@googlegroups.com
Yes, YALMIP detects this as a GP (which it is), but it is trivial and Mosek does not like such trivial problems

Johan Löfberg

unread,
May 30, 2015, 2:25:30 PM5/30/15
to yal...@googlegroups.com
Sounds like you are complicating things by trying to think in terms of how fmincon works. YALMIP is solver agnostic, the way you write the code is the same for all nonlinear solvers, and derivatives are supplied without you having to do absolutely anything. Just define the constraints and objective using sdpvar variables (as in the wiki and Marks examples here), and call optimize

Mark L. Stone

unread,
May 30, 2015, 3:52:03 PM5/30/15
to yal...@googlegroups.com
I shall have to remember not to give MOSEK trivial problems.  I know you plan to fix it, but the gotcha here is that if no solver is specified and MOSEK is available, YALMIP chooses MOSEK and decides to add nonnegativity constraints which were not specified in the constraints in the optimize command.

BTW, in school, we were only assigned, in increasing order of difficulty, "simple", "straightforward", "easy", and at most once or twice per year-long course sequence, "not so easy" problems.  We were never assigned any 'trivial" problems.
Reply all
Reply to author
Forward
0 new messages