# Constraint nonlinear problem

**URL:** <https://discourse.julialang.org/t/constraint-nonlinear-problem/60033>\
**Category:** Optimization (Mathematical)\
**Tags:** question, optimization, nonlinear\
**Created:** [April 26, 2021, 2:10pm UTC](https://discourse.julialang.org/t/constraint-nonlinear-problem/60033 "2021-04-26T14:10:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [April 26, 2021, 2:10pm UTC](https://discourse.julialang.org/t/constraint-nonlinear-problem/60033/1 "2021-04-26T14:10:16Z")

</div>

I have tried `GalacticOptim.jl` but I did not manage to get the constraints to work.

```julia
using GalacticOptim, Optim
using Plots

rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
x0 = zeros(2)
p = [1.0, 100.0]
circle_constr(x, p) = [x[1]^2 + x[2]^2 - 1] # x_1^2 + x_^2 <= 1
x0 = zeros(2)

optprob = OptimizationFunction(rosenbrock, GalacticOptim.AutoForwardDiff(); cons=circle_constr)
prob = OptimizationProblem(optprob, x0, p, lcons = [-Inf], ucons = [Inf])
sol = solve(prob, IPNewton())

plot(ratio=:equal)
contourf!(-1:0.1:1, -1:0.1:1, (x,y)->rosenbrock([x,y], p), c=:viridis, lw=0)
contour!(-1:0.1:1, -1:0.1:1, (x,y)->circle_constr([x,y], p)[1], levels=[0], c=:blue)
scatter!([sol.u[1]], [sol.u[1]], c=:red, label="solution")

```

Although no error is thrown the result is incorrect. It is the unconstrained optimum.

 ![test](https://global.discourse-cdn.com/julialang/original/3X/c/9/c9b677713c3d2dfe4c3917c4f26a57dbd21fa516.png)  
Am I doing something wrong?

---

<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:** [April 26, 2021, 10:11pm UTC](https://discourse.julialang.org/t/constraint-nonlinear-problem/60033/2 "2021-04-26T22:11:49Z")

</div>

> [@ohmsweetohm1](#):
>
> `lcons = [-Inf], ucons = [Inf]`

I’m guessing you need to set the bounds to [-\infty, 0].

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [April 27, 2021, 4:57pm UTC](https://discourse.julialang.org/t/constraint-nonlinear-problem/60033/3 "2021-04-27T16:57:24Z")

</div>

The result is slightly more correct but still infeasible.

```julia
prob = OptimizationProblem(optprob, x0, p, lcons = [-Inf], ucons = [0])

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [April 27, 2021, 5:05pm UTC](https://discourse.julialang.org/t/constraint-nonlinear-problem/60033/4 "2021-04-27T17:05:25Z")

</div>

Try [https://github.com/mohamed82008/Nonconvex.jl](https://github.com/mohamed82008/Nonconvex.jl).

---

<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:** [April 27, 2021, 6:02pm UTC](https://discourse.julialang.org/t/constraint-nonlinear-problem/60033/5 "2021-04-27T18:02:57Z")

</div>

Can you be more specific? What point do you get from the solver?

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [April 28, 2021, 7:54am UTC](https://discourse.julialang.org/t/constraint-nonlinear-problem/60033/6 "2021-04-28T07:54:03Z")

</div>

```julia
u: 2-element Array{Float64,1}:
 0.7864151541684259
 0.6176983125233905

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [April 28, 2021, 7:57am UTC](https://discourse.julialang.org/t/constraint-nonlinear-problem/60033/7 "2021-04-28T07:57:31Z")

</div>

```julia
julia> norm([0.7864151541684259, 0.6176983125233905])
0.9999999999999967

```

---

<div class="post-metadata">

**Author:** ![ohmsweetohm1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ohmsweetohm1/32/49126_2.png) [@ohmsweetohm1](https://discourse.julialang.org/u/ohmsweetohm1)\
**Post date:** [April 28, 2021, 8:04am UTC](https://discourse.julialang.org/t/constraint-nonlinear-problem/60033/8 "2021-04-28T08:04:23Z")

</div>

I see my error now:

> [@ohmsweetohm1](#):
>
> `scatter!([sol.u[1]], [sol.u[1]], c=:red, label="solution")`

THX for the help!
