# Optimization.jl constraints violated...why?

**URL:** https://discourse.julialang.org/t/optimization-jl-constraints-violated-why/88269
**Category:** Optimization (Mathematical)
**Created:** [October 5, 2022, 3:59am UTC](https://discourse.julialang.org/t/optimization-jl-constraints-violated-why/88269 "2022-10-05T03:59:50Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![kjohnsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kjohnsen/32/27419_2.png) [@kjohnsen](https://discourse.julialang.org/u/kjohnsen)
#### Post date: [October 5, 2022, 3:59am UTC](https://discourse.julialang.org/t/optimization-jl-constraints-violated-why/88269/1 "2022-10-05T03:59:51Z")

</div>

I am trying to implement model predictive control using Optimization.jl, with the Optim backend. It seems like it should be straightforward: a quadratic program, with some linear constraints. But the constraints just aren’t enforced–it happens with Newton() and BFGS() and L-BFGS() solvers. I don’t know why. The output says the solution was successful.

My `cons(u, p)` function returns a vector with 0s, I pass in `lcons=0` and `ucons=0` to the `OptimizationProblem`, and after solving, `cons(sol.u, p)` does not have 0s like it’s supposed to.

I know many people will recommend using JuMP instead. I’m open to that. I’d still like to know why Optimization.jl is failing me, though.

Here is code that gets at the gist of what I’m saying, but [here](https://github.com/kjohnsen/mpc_in_julia/blob/kyle/kyle_try.jl) is the full code.

```julia
optf = OptimizationFunction(J, Optimization.AutoForwardDiff(), cons=cons)
...
lcons = # all zeros
ucons = # all zeros except for upper bounds on a couple variables
prob = OptimizationProblem(optf, u0, p, lcons=lcons, ucons=ucons)
sol = solve(prob, Newton())
cons(sol.u, p) # DOESN'T SATISFY CONSTRAINTS

```

---

<div class="post-metadata">

### Author: ![Vaibhavdixit02](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vaibhavdixit02/32/2916_2.png) [@Vaibhavdixit02](https://discourse.julialang.org/u/Vaibhavdixit02)
#### Post date: [October 5, 2022, 5:11am UTC](https://discourse.julialang.org/t/optimization-jl-constraints-violated-why/88269/2 "2022-10-05T05:11:40Z")

</div>

Hey, all the optimizers that you mentioned are unconstrained optimisers, you’d have to use `IPNewton` from Optim or one of the constrained optimizers that can be used with MOI for the constraints to be followed. We’ll soon have better checks for things like this so that an error is thrown when you attempt this!

---

<div class="post-metadata">

### Author: ![kjohnsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kjohnsen/32/27419_2.png) [@kjohnsen](https://discourse.julialang.org/u/kjohnsen)
#### Post date: [October 5, 2022, 2:05pm UTC](https://discourse.julialang.org/t/optimization-jl-constraints-violated-why/88269/3 "2022-10-05T14:05:49Z")

</div>

Awesome! The consequences of trying to do optimization without a good formal foundation…

---

<div class="post-metadata">

### Author: ![kjohnsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kjohnsen/32/27419_2.png) [@kjohnsen](https://discourse.julialang.org/u/kjohnsen)
#### Post date: [October 5, 2022, 7:43pm UTC](https://discourse.julialang.org/t/optimization-jl-constraints-violated-why/88269/4 "2022-10-05T19:43:30Z")

</div>

Hmm, I’m still stuck…it appears a vector of constraints isn’t working as expected? In my example, I keep getting a warning that my initial guess isn’t an interior point, though I double-checked and as far as I can tell it is.

So I went to the example from the docs and I’m having the opposite problem. In the `cons` function I add an unmet constraint: the second element of the vector, 1847, which clearly is not 0 as requested in `lcons`/`ucons`. Yet I’m not getting any warning that the initial guess isn’t feasible and the solver works anyway. What am I missing?

```julia
rosenbrock(x, p) = (p[1] - x[1])^2 + p[2] * (x[2] - x[1]^2)^2
function cons(res, x, p)
    res .= [x[1]^2 + x[2]^2, 1847]
end
x0 = zeros(2)
p = [1.0,100.0]
optf = OptimizationFunction(rosenbrock, Optimization.AutoForwardDiff(), cons=cons)
lcons = [-5., 0, 1]; ucons = [10.0, 0, 1]
prob = Optimization.OptimizationProblem(optf, x0, p, lcons = [-5.0, 0], ucons = [10.0, 0])
sol = solve(prob, IPNewton())

```

After some messing with `lcons` and `ucons` it looks like it _does_ see the problem and complain as long as I make it an inequality constraint, e.g., 0 \< cons[2] \< 1. Does this mean equality constraints are just totally ignored?

As far as I can tell, it fails when there’s a _mix_ of equality and inequality constraints.

---

<div class="post-metadata">

### Author: ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)
#### Post date: [October 5, 2022, 8:57pm UTC](https://discourse.julialang.org/t/optimization-jl-constraints-violated-why/88269/5 "2022-10-05T20:57:26Z")

</div>

@kjohnsen from a quick glance it looks like the problem would would like to solve would be a good fit for the JuMP framework. Have you given that a try?

---

<div class="post-metadata">

### Author: ![kjohnsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kjohnsen/32/27419_2.png) [@kjohnsen](https://discourse.julialang.org/u/kjohnsen)
#### Post date: [October 5, 2022, 9:16pm UTC](https://discourse.julialang.org/t/optimization-jl-constraints-violated-why/88269/6 "2022-10-05T21:16:58Z")

</div>

Not yet…I realize it’s more mature and better documented, so I bet it would work well. I was hoping Optimization/Optim would do the trick, since the pure-Julia approach is attractive

---

<div class="post-metadata">

### Author: ![ccoffrin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccoffrin/32/400_2.png) [@ccoffrin](https://discourse.julialang.org/u/ccoffrin)
#### Post date: [October 5, 2022, 9:56pm UTC](https://discourse.julialang.org/t/optimization-jl-constraints-violated-why/88269/7 "2022-10-05T21:56:19Z")

</div>

In my experience Optim can be suitable for small problems but I have had difficulty getting it to work on large constrained optimization problems. There is some discussion of my experience here, [AC Optimal Power Flow in Various Nonlinear Optimization Frameworks](https://discourse.julialang.org/t/ac-optimal-power-flow-in-various-nonlinear-optimization-frameworks/78486)

There are a few pure-Julia MOI compatible solvers out there but there is a good reason that JuMP primarily calls a wide variety of non-Julia solvers. These solvers distill decades of R&D in optimization algorithms, and it is not easy to get similar performance rolling your own.

---

<div class="post-metadata">

### Author: ![Vaibhavdixit02](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vaibhavdixit02/32/2916_2.png) [@Vaibhavdixit02](https://discourse.julialang.org/u/Vaibhavdixit02)
#### Post date: [October 14, 2022, 10:01am UTC](https://discourse.julialang.org/t/optimization-jl-constraints-violated-why/88269/8 "2022-10-14T10:01:55Z")

</div>

Yeah, looks like the interior check is limited to inequality constraints so that’s why this doesn’t error.

> As far as I can tell, it fails when there’s a _mix_ of equality and inequality constraints.

That’s partially true, if you set a realistic equality constraint it will follow it but looks like something like this is not enforced. Like in the examples here [http://optimization.sciml.ai/stable/tutorials/constraints/](http://optimization.sciml.ai/stable/tutorials/constraints/)

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [October 14, 2022, 10:38am UTC](https://discourse.julialang.org/t/optimization-jl-constraints-violated-why/88269/9 "2022-10-14T10:38:34Z")

</div>

> [@ccoffrin](#):
>
> There are a few pure-Julia MOI compatible solvers out there but there is a good reason that JuMP primarily calls a wide variety of non-Julia solvers. These solvers distill decades of R&D in optimization algorithms, and it is not easy to get similar performance rolling your own.

Yes, Optim.jl seems to have some issues. I plan to move more of the Optimization.jl tutorials away from Optim.jl whenever I have time, for this reason. NLopt, IPOPT, etc. just are generally more stable, and we should be pushing people in that direction instead.
