# Wrong answer with NonLinear Optimization in JuMP

**URL:** <https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, optimization\
**Created:** [January 9, 2022, 8:34pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304 "2022-01-09T20:34:35Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [January 9, 2022, 8:34pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/1 "2022-01-09T20:34:35Z")

</div>

Hi, I am trying to maximize a very simple function in JuMP which I can solve analytically. I am getting an optimal solution which is different from what I should get. I was hoping someone could point me to my error.

Here you can find a MWE

```julia
using JuMP, Ipopt, Optim, LinearAlgebra
C1 = 50
K1 = 1
K2 = 0
K3 = 0
σ = 5
problem_test = Model(Ipopt.Optimizer)
#set_silent(primal_capacity)
@variable(problem_test, Q[1:3] >= 0)
@NLobjective(problem_test, Max, Q[1]^((σ-1)/σ)*K1 + Q[2]^((σ-1)/σ)*K2 + Q[3]^((σ-1)/σ)*K3 - 1.0*Q[1] - 1.1*Q[2] - 1.1*Q[3] )
@constraint(problem_test, c1, 1.0*Q[1] + 1.1*Q[2] + 1.1*Q[3]<= C1)
optimize!(problem_test)
objective_value(problem_test)
sol = value.(Q)
sum(sol)
dual(c1)

soltrue1 = ((σ-1)/σ)^σ*(K1/1.0)^σ
soltrue2 = ((σ-1)/σ)^σ*(K2/1.1)^σ
soltrue3 = ((σ-1)/σ)^σ*(K3/1.1)^σ

```

The variables soltrue correspond to the optimizer computed by paper and pencil.

---

<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:** [January 9, 2022, 10:00pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/2 "2022-01-09T22:00:12Z")

</div>

```nohighlight
julia> sol = value.(Q)
3-element Vector{Float64}:
 0.327742785832787
 1.1393908780473464e-5
 1.1393908780473464e-5

julia> soltrue1 = ((σ-1)/σ)^σ*(K1/1.0)^σ
0.3276800000000001

julia> soltrue2 = ((σ-1)/σ)^σ*(K2/1.1)^σ
0.0

julia> soltrue3 = ((σ-1)/σ)^σ*(K3/1.1)^σ
0.0

```

They seems pretty similar to me? Ipopt uses tolerances, so you should expect some (small) differences between the analytic solution and the computed optimum.

You can get a better solution by turning off scaling:

```julia
set_optimizer_attribute(problem_test, "nlp_scaling_method", "none")

```

But the underlying problem is that `x^0.8` is only defined for `x>=0` and this can cause problems when Ipopt attempts to evaluate a solution that is slightly negative (e.g., `Q[1] = -1e-8`).

---

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [January 10, 2022, 3:10pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/3 "2022-01-10T15:10:55Z")

</div>

Thanks so much for the reply! Now, I am even more worried!

See my results. which I should have included in the original post

```julia
julia> sol = value.(Q)
3-element Vector{Float64}:
 1.38696926506704
 0.20409119357963545
 0.204091193579635

julia> 

julia> soltrue1 = ((σ-1)/σ)^σ*(K1/1.0)^σ
0.3276800000000001

julia> soltrue2 = ((σ-1)/σ)^σ*(K2/1.1)^σ
0.0

julia> soltrue3 = ((σ-1)/σ)^σ*(K3/1.1)^σ
0.0

```

In case it is helpful, I am using

```julia
VERSION
v"1.6.1"

```

and

```julia
(@v1.6) pkg> status JuMP
      Status `C:\Users\juan\.julia\environments\v1.6\Project.toml`
  [4076af6c] JuMP v0.21.10

(@v1.6) pkg> status Ipopt
      Status `C:\Users\juan\.julia\environments\v1.6\Project.toml`
  [b6b21f68] Ipopt v0.7.0

```

Actually, I have a much larger problem, and I do not know if this is a symptom. The solution in this optimization routine is interior (in terms that the constraint c1 is not binding at the optimum), however when I change the value of the constant C1 the value of the optimizer changes.

---

<div class="post-metadata">

**Author:** ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)\
**Post date:** [January 10, 2022, 3:27pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/4 "2022-01-10T15:27:32Z")

</div>

Edit: your analytical solution is correct, **provided it satisfies the constraint** (which of course should be checked).  
Are you sure Ipopt converged properly? When I ran your example, it failed with “EXIT: Restoration Failed”.

---

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [January 10, 2022, 3:35pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/5 "2022-01-10T15:35:04Z")

</div>

Hi! Thanks for the answer.

Edit: Sorry I had replied to the previous post where I said that since this function is globally concave, there should be no local optima.

The exit from Ipopt says EXIT: Optimal Solution Found.

---

<div class="post-metadata">

**Author:** ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)\
**Post date:** [January 10, 2022, 3:36pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/6 "2022-01-10T15:36:26Z")

</div>

Did you mean to set K2 and K3 to 0?

---

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [January 10, 2022, 3:38pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/7 "2022-01-10T15:38:44Z")

</div>

I just set K2 and K3 to 0 to make the error more apparent, but if I set them to something else. Say 1, I still get the error.

```julia
using JuMP, Ipopt, Optim, LinearAlgebra
C1 = 50
K1 = 1
K2 = 1
K3 = 1
σ = 5
problem_test = Model(Ipopt.Optimizer)
@variable(problem_test, Q[1:3] >= 0)
@NLobjective(problem_test, Max, Q[1]^((σ-1)/σ)*K1 + Q[2]^((σ-1)/σ)*K2 + Q[3]^((σ-1)/σ)*K3 - 1.0*Q[1] - 1.1*Q[2] - 1.1*Q[3] )
@constraint(problem_test, c1, 1.0*Q[1] + 1.1*Q[2] + 1.1*Q[3]<= C1)
optimize!(problem_test)
objective_value(problem_test)
sol = value.(Q)
sum(sol)
dual(c1)

soltrue1 = ((σ-1)/σ)^σ*(K1/1.0)^σ
soltrue2 = ((σ-1)/σ)^σ*(K2/1.1)^σ
soltrue3 = ((σ-1)/σ)^σ*(K3/1.1)^σ

julia> value.(Q)
3-element Vector{Float64}:
 0.9511299412613234
 0.7518509419158073
 0.7518509419158073

julia> soltrue1 = ((σ-1)/σ)^σ*(K1/1.0)^σ
0.3276800000000001

julia> soltrue2 = ((σ-1)/σ)^σ*(K2/1.1)^σ
0.20346349914002398

julia> soltrue3 = ((σ-1)/σ)^σ*(K3/1.1)^σ
0.20346349914002398

```

---

<div class="post-metadata">

**Author:** ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)\
**Post date:** [January 10, 2022, 3:54pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/8 "2022-01-10T15:54:12Z")

</div>

Weird. Can you try minimizing -f(x)?  
(edit: the nonlinear solver I’m developing indeed returned 0.32768 0.203464 0.203464)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 10, 2022, 3:54pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/9 "2022-01-10T15:54:33Z")

</div>

This is unlikely to be relevant here, but you probably shouldn’t use 1.6.1 - the current stable release is 1.7.1, and even if for some reason you need to use 1.6, the latest patch release is 1.6.5

---

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [January 10, 2022, 4:00pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/10 "2022-01-10T16:00:11Z")

</div>

The minimization does work! I find it very very puzzling though.

---

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [January 10, 2022, 4:00pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/11 "2022-01-10T16:00:23Z")

</div>

Thanks. I will update it.

---

<div class="post-metadata">

**Author:** ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)\
**Post date:** [January 10, 2022, 4:01pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/12 "2022-01-10T16:01:42Z")

</div>

Awesome. There has to be some kind of bug in Jump/Ipopt then 🙂

---

<div class="post-metadata">

**Author:** ![jmcastro2109](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmcastro2109/32/38427_2.png) [@jmcastro2109](https://discourse.julialang.org/u/jmcastro2109)\
**Post date:** [January 10, 2022, 4:07pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/13 "2022-01-10T16:07:25Z")

</div>

Installing Julia 1.7.1 or minimizing -f seem to have done the trick

Thanks so much for your time @cvanaret , @odow, @nilshg

---

<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:** [January 10, 2022, 6:19pm UTC](https://discourse.julialang.org/t/wrong-answer-with-nonlinear-optimization-in-jump/74304/14 "2022-01-10T18:19:05Z")

</div>

Please update your packages. I think ipopt v0.7 had a bug. The latest release is v0.9.1
