# Performance of log in nonlinear optimization

**URL:** https://discourse.julialang.org/t/performance-of-log-in-nonlinear-optimization/18450
**Category:** Optimization (Mathematical)
**Created:** [December 8, 2018, 2:28am UTC](https://discourse.julialang.org/t/performance-of-log-in-nonlinear-optimization/18450 "2018-12-08T02:28:35Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![danielcas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielcas/32/7515_2.png) [@danielcas](https://discourse.julialang.org/u/danielcas)
#### Post date: [December 8, 2018, 2:28am UTC](https://discourse.julialang.org/t/performance-of-log-in-nonlinear-optimization/18450/1 "2018-12-08T02:28:35Z")

</div>

I’m having some trouble with the function “log” in an optimization problem.

For instance:  
I create a variable, and then the following constraint:

@NLconstraint(ModelName, log(x) \<=100), I use Ipopt to solve it (a feasible problema) and it gives me x=0 (it is infeasible I think), but then I put this constraint: @NL(ModelName, x \<= exp(100)) and now it returns a feasible solution. This is a simpler version of my issue…

The thing is…I strongly need to use “Log” in some constraints and the objective function of the optimization problem.How can I solve this?

---

<div class="post-metadata">

### Author: ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)
#### Post date: [December 8, 2018, 3:02am UTC](https://discourse.julialang.org/t/performance-of-log-in-nonlinear-optimization/18450/2 "2018-12-08T03:02:50Z")

</div>

Does it also need to constrain x to greater than 0? Otherwise it might look at eg x=-0.1, which is fine for exp() but not so good for log()

Added: if negative x is allowable then maybe log(abs(x)) or something along those lines

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [December 8, 2018, 3:12am UTC](https://discourse.julialang.org/t/performance-of-log-in-nonlinear-optimization/18450/3 "2018-12-08T03:12:04Z")

</div>

Can you provide a minimum working example?

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

### Author: ![danielcas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielcas/32/7515_2.png) [@danielcas](https://discourse.julialang.org/u/danielcas)
#### Post date: [December 8, 2018, 4:52am UTC](https://discourse.julialang.org/t/performance-of-log-in-nonlinear-optimization/18450/4 "2018-12-08T04:52:30Z")

</div>

Yes of course.

m=Model(solver=IpoptSolver())

@variable(m, x[i=1:n] )

for i in [1:n;]  
@NLconstraint(m, x[i]\<=1 )  
@NLconstraint(m, x[i] \>= exp(-gamma[i]) )  
end  
@NLconstraint(m,-sum{log(x[i])/gamma[i], i=1:n}\>=-mm)

the last constraint presents the infeasibility I wrote above. I’m using the change of variables xi=exp(-yi\*gamma[i]), because with this the problem transforms into a convex one.

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [December 8, 2018, 6:38pm UTC](https://discourse.julialang.org/t/performance-of-log-in-nonlinear-optimization/18450/5 "2018-12-08T18:38:28Z")

</div>

The last constraint is probably an issue when `x[i]=0`. Try adding bounds on `x` rather than via `NLconstraint`. You probably want to set the `start` keyword as well since it defaults to `0.0`.

```julia
model = Model(solver = IpoptSolver())
@variable(model, 0.01 <= x[i = 1:n] <= 1, start = 1)
@NLconstraint(model, sum(log(x[i]) for i in 1:n) >= -1)

```

P.s. for future reference, take a look at the post I linked to. It demonstrates how to format code nicely. I can’t copy and paste your example because `gamma`, `n`, and `mm` aren’t defined.

---

<div class="post-metadata">

### Author: ![dpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpo/32/3335_2.png) [@dpo](https://discourse.julialang.org/u/dpo)
#### Post date: [December 8, 2018, 7:34pm UTC](https://discourse.julialang.org/t/performance-of-log-in-nonlinear-optimization/18450/6 "2018-12-08T19:34:20Z")

</div>

IPOPT is not a feasible method. The first thing it does is add slack variables to turn your constraint into an equality: log(x) + s = 100 with s ≥ 0. IPOPT only ensures that s \> 0 during the iterations, but the equality constraint is only guaranteed to be satisfied in the limit. The problem you have is that your constraint function isn’t defined everywhere, and IPOPT is likely to venture outside of its domain. If you have access to KNITRO, it has a “feasible” option to ensure that nonlinear inequality constraints remain strictly satisfied. If you don’t have access to KNITRO, your best bet is to rework your model.

---

<div class="post-metadata">

### Author: ![danielcas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielcas/32/7515_2.png) [@danielcas](https://discourse.julialang.org/u/danielcas)
#### Post date: [December 10, 2018, 9:30pm UTC](https://discourse.julialang.org/t/performance-of-log-in-nonlinear-optimization/18450/7 "2018-12-10T21:30:06Z")

</div>

Thanks a lot odow, putting start=1 solved the problem. I didn’t know that much about Ipopt solver.

And I will take a look at that post so in the future I improve the format code.

Status: Problem solved
