# Optim.jl + adding constraints

**URL:** https://discourse.julialang.org/t/optim-jl-adding-constraints/51861
**Category:** Optimization (Mathematical)
**Created:** [December 15, 2020, 11:27am UTC](https://discourse.julialang.org/t/optim-jl-adding-constraints/51861 "2020-12-15T11:27:24Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)
#### Post date: [December 15, 2020, 11:27am UTC](https://discourse.julialang.org/t/optim-jl-adding-constraints/51861/1 "2020-12-15T11:27:24Z")

</div>

I want to add equality constraints to Optim.jl (not just a box-constrained optimization). My understanding is that there were plans to add this feature. Does anybody know if this stalled? [This package](https://github.com/JuliaNLSolvers/ConstrainedOptim.jl) I see was intended to be merged with Optim.jl but I cannot presently find this feature in Optim.

If the feature is not yet added to Optim, does anyone know of any package that could give this “optimization with functional constraints” functionality? I am looking for something that does more or less what [scipy.optimize in python](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html) does.

PS. I can easily add the constraint within JuMP but this is not an option right now.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 15, 2020, 12:29pm UTC](https://discourse.julialang.org/t/optim-jl-adding-constraints/51861/2 "2020-12-15T12:29:16Z")

</div>

A lot depends on what your constraints look like (eg linear, nonlinear), so an MWE for at least the objective and the constraints could help.

---

<div class="post-metadata">

### Author: ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)
#### Post date: [December 15, 2020, 12:43pm UTC](https://discourse.julialang.org/t/optim-jl-adding-constraints/51861/3 "2020-12-15T12:43:47Z")

</div>

Hi @Tamas_Papp. Thanks. It’s a relatively large decentralized dynamic growth model–I know you are an economist too–so it will take sometime to curve out a MWE. But will post it here when ready.

---

<div class="post-metadata">

### Author: ![jovansam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jovansam/32/15023_2.png) [@jovansam](https://discourse.julialang.org/u/jovansam)
#### Post date: [December 18, 2020, 12:45pm UTC](https://discourse.julialang.org/t/optim-jl-adding-constraints/51861/4 "2020-12-18T12:45:34Z")

</div>

I decided to use scipy.optimize through [PyCall](https://github.com/JuliaPy/PyCall.jl). If any body is interested in the details. I can devote time to making a MWE and post the code here. It took some hours of googling and stackoverflow to figure out how I should parse arguments. Would not want others to go through the same pain.

FYI @Tamas_Papp, the model features non-linear constraints.

---

<div class="post-metadata">

### Author: ![Paul\_Soderlind](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_soderlind/32/1753_2.png) [@Paul\_Soderlind](https://discourse.julialang.org/u/Paul_Soderlind)
#### Post date: [December 18, 2020, 2:59pm UTC](https://discourse.julialang.org/t/optim-jl-adding-constraints/51861/5 "2020-12-18T14:59:16Z")

</div>

Maybe give [GitHub - JuliaOpt/NLopt.jl: Package to call the NLopt nonlinear-optimization library from the Julia language](https://github.com/JuliaOpt/NLopt.jl) a try?

---

<div class="post-metadata">

### Author: ![danspielman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danspielman/32/4708_2.png) [@danspielman](https://discourse.julialang.org/u/danspielman)
#### Post date: [December 19, 2020, 1:36am UTC](https://discourse.julialang.org/t/optim-jl-adding-constraints/51861/6 "2020-12-19T01:36:54Z")

</div>

Some of the optimizers in Optim support optimization on manifolds. All you need to do is implement a function that maps any point in Euclidean space to a nearby one on the manifold. Of course, it should be the identity on the manifold.

I extended Nelder-Mead to do this, too (it’s very simple). So, if you want to use that optimizer, look at [https://github.com/danspielman/Optim.jl](https://github.com/danspielman/Optim.jl). It will probably be included in the main Optim.jl soon.

For the gradient based algorithms, you also need to code the gradient of the map.

---

<div class="post-metadata">

### Author: ![csev](https://avatars.discourse-cdn.com/v4/letter/c/bbe5ce/32.png) [@csev](https://discourse.julialang.org/u/csev)
#### Post date: [October 21, 2023, 6:52pm UTC](https://discourse.julialang.org/t/optim-jl-adding-constraints/51861/7 "2023-10-21T18:52:35Z")

</div>

Hi @jovansam – curious if you ever did this or found another MWE for scipy optimization from julia base code?

---

<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 21, 2023, 6:54pm UTC](https://discourse.julialang.org/t/optim-jl-adding-constraints/51861/8 "2023-10-21T18:54:28Z")

</div>

Check out this tutorial for using Optimization.jl with equality and inequality constraints:

[https://docs.sciml.ai/Optimization/stable/tutorials/constraints/](https://docs.sciml.ai/Optimization/stable/tutorials/constraints/)
