# [Ipopt] FW: If statement in objective function

Andreas Waechter awaechter.iems at gmail.com
Wed Sep 21 17:42:07 EDT 2011

```Hi,

Yes, one way to handle such discontinuous functions is the use of
complementarity constraints, but, as Etienne pointed out, one has to be
careful with this approach.  In particular, one thing to keep in mind is
that Ipopt only looks for a local solution, so for Greg's objective, any
value for x except for and b is a local solution.

The C++ version of Ipopt has no special way of handling complementarity
constraints - since then the standard NLP constraint qualifications do
not told, Ipopt might have trouble solving the resulting NLP.  The
modeler can try to overcome this using a penalty reformulation or
another strategies (this is also described in Prof. Biegler's book,
Chapter 11.2.1).  The former Fortran version of Ipopt had some specific
solution features for complementarity constraints in the AMPL interface
- this is still available, but no longer supported.

Andreas

On 09/20/2011 09:01 AM, Ayotte-Sauvé, Étienne wrote:
> Hi Greg,
>
> Another way to model your objective function without invoking binary variables is to use complementarity constraints. However, such constraints cause non-uniqueness and unboundedness of the Lagrange multipliers (i.e. the dual optimization problem will not be well posed in classical terms). I am not an optimization specialist and I'm even less knowledgable with respect to IPOPT (that's in part why I joined this message group), but my guess is you'll be able to tag these complementarity contraints so that IPOPT treats them with adapted subroutines (e.g. relaxation strategies).
>
> To be more precise, you can reformulate your piecewise function via complementarity constraints in the following manner
>
> p = y[2];
>
> y[1] + y[2] + y[3] = 1;
>
> (x - xL)*(x - a) - mu - lambda[1] = 0;
>
> (x - a)*(x - b) - mu - lambda[2] = 0;
>
> (x - b)*(x - xU) - mu - lambda[3] = 0;
>
> y[i]*lambda[i] = 0, (i = 1, ..., 3); (*complementarity constraints*)
>
> y[i]>= 0, (i = 1, ..., 3); (*complementarity constraints*)
>
> lambda[i]>= 0, (i = 1, ..., 3), (*complementarity constraints*)
>
> where p denotes the objective function value defined in your original message; xL and xU denote lower and upper bounds (respectively) on the variable x; y[1], y[2], y[3], lambda[1], lambda[2], lambda[3] and mu are variables originating from a reformulation of your piecewise function via a convex optimization problem which in turn is reformulated via first order optimality conditions (i.e. KKT conditions) - see chapter 11 of "Nonlinear Programming: Concepts, Algorithms and Applications to Chemical Processes" by Lorenz Biegler. To help your intuition, note that the variable y[i] takes value 1 if x is in the open interval ] c[i-1], c[i] [ and it takes value 0 if x is NOT in the closed interval [ c[i-1], c[i ] ] (i = 1, 2, 3), where c[0] = xL, c[1] = a, c[2] = b, c[3] = xU.
>
> Remarks.
>
> 1. The variable mu may take negative values (it is a Lagrange multiplier corresponding to an equality constraint).
>
> 2. For x = a or x = b, the above formulation may yield values of p which are between 0 and 1; for x<  a or x>  b, this formulation yields p = 0; for a<  x<  b, it gives p = 1.
>
> 3. Please verify with the book since I wrote the above formulation from memory (and intuition).
>
> I hope this helps.
>
> Etienne Ayotte-Sauve'
> eayottes at nrcan.gc.ca
>
>
>
>
>
> -----Original Message-----
> From: ipopt-bounces at list.coin-or.org [mailto:ipopt-bounces at list.coin-or.org] On Behalf Of Hans Pirnay
> Sent: September 20, 2011 2:49 AM
> To: Gregory K.
> Cc: ipopt ipopt
> Subject: Re: [Ipopt] If statement in objective function
>
> Hi Greg,
>
> that model is not a smooth nonlinear optimization problem, and therefore not directly solvable with Ipopt.
>
> Here's one simple thing you can do if you only have one constraint of this kind: Solve the problem three times, once with the constraint
>
> x<=a,   price=0
> x>=a, x<=b   ,  price=1
> x>=b,  price = 0
>
> If you have several price variables, this approach quickly becomes infeasible, and you will need to use an MINLP solver (I don't have any suggestion which, it probably depends on what you can afford, though).
>
> good luck
>
> Hans
>
> On Mon, Sep 19, 2011 at 11:44 PM, Gregory K.<khoroshylov at yahoo.com>  wrote:
>> Thank's for your response. To be honest I had finished my CO major
>> awhile ago, and a bit rusty on theory.
>> On a very simplified level, that's what I am trying to achieve
>> maximize price:
>> if x>=a and x<=b then 1 else 0
>> But IPOPT gives me all zero solution which is obviously is not optimal.
>> What should be done to make IPOPT solve it correctly? Am I missing
>> anything (any ipopt option)?
>> Is it even possible to solve it?
>> Or maybe you can recomend any other more suitable solver.
>> Thanks a bunch,
>> Greg
>> ________________________________
>> From: Andrea Walther<andrea.walther at uni-paderborn.de>
>> To: Stefan Vigerske<stefan at math.hu-berlin.de>
>> Cc: Gregory K.<khoroshylov at yahoo.com>; ipopt at list.coin-or.org
>> Sent: Monday, September 19, 2011 2:05:14 AM
>> Subject: Re: [Ipopt] If statement in objective function
>>
>> Hi,
>>
>>> I don't know how AMPL computes derivatives for functions containing
>>> if-statements, but you should make sure that you have at least
>>> continuous first derivatives for Ipopt to work.
>> as far as I know, AMPL uses Automatic Differentiation for the
>> derivative calculation. Therefore, as long as you are not right at the
>> kink caused by the if statement you get a derivative that is valid in
>> a certain region around the current point. However, if this point is
>> close to the kink the derivative information perhaps is not that
>> useful. This might cause the problems in the optimisation process.
>>
>> Best regards,
>>
>> Andrea Walther
>>
>> --
>> Prof. Dr. Andrea Walther
>> Lehrstuhl fuer Mathematik und ihre Anwendungen Institut fuer
>> Mathematik Universitaet Paderborn Warburger Str. 100
>>
>> Phone: ++49 5251 602721
>> Fax:  ++49 5251 603728
>>
>> **********
>>
>>
>>
>>
>> _______________________________________________
>> Ipopt mailing list
>> Ipopt at list.coin-or.org
>> http://list.coin-or.org/mailman/listinfo/ipopt
>>
>>
> _______________________________________________
> Ipopt mailing list
> Ipopt at list.coin-or.org
> http://list.coin-or.org/mailman/listinfo/ipopt
>
> _______________________________________________
> Ipopt mailing list
> Ipopt at list.coin-or.org
> http://list.coin-or.org/mailman/listinfo/ipopt

```