# Does DiffEqFlux.jl supports a constrained optimal control problem, where in both control and states are constrained

**URL:** <https://discourse.julialang.org/t/does-diffeqflux-jl-supports-a-constrained-optimal-control-problem-where-in-both-control-and-states-are-constrained/44246>\
**Category:** Specific Domains\
**Tags:** question, jump, diffeq\
**Created:** [August 4, 2020, 10:53am UTC](https://discourse.julialang.org/t/does-diffeqflux-jl-supports-a-constrained-optimal-control-problem-where-in-both-control-and-states-are-constrained/44246 "2020-08-04T10:53:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![manvibharat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manvibharat/32/11635_2.png) [@manvibharat](https://discourse.julialang.org/u/manvibharat)\
**Post date:** [August 4, 2020, 10:53am UTC](https://discourse.julialang.org/t/does-diffeqflux-jl-supports-a-constrained-optimal-control-problem-where-in-both-control-and-states-are-constrained/44246/1 "2020-08-04T10:53:07Z")

</div>

I am new to Julia and I am still learning the language.  
I thought I could use Julia to solve an Optimal Control problem, which has some constraints. I looked at two examples I found at [Readme · DiffEqFlux.jl](https://juliahub.com/docs/DiffEqFlux/BdO4p/1.9.0/#example-usage) and [https://diffeqflux.sciml.ai/dev/examples/optimal\_control/](https://diffeqflux.sciml.ai/dev/examples/optimal_control/).

From sciml\_train ( [https://diffeqflux.sciml.ai/dev/Scimltrain/](https://diffeqflux.sciml.ai/dev/Scimltrain/) ) documentation I found that there is a box constraint available.

Is it possible to solve the constrained optimal control problem?

---

<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:** [August 4, 2020, 11:15am UTC](https://discourse.julialang.org/t/does-diffeqflux-jl-supports-a-constrained-optimal-control-problem-where-in-both-control-and-states-are-constrained/44246/2 "2020-08-04T11:15:42Z")

</div>

> [@manvibharat](#):
>
> Is it possible to solve the constrained optimal control problem?

Yes, do the same thing except throw the loss function into NLopt.jl with constrained optimization.

---

<div class="post-metadata">

**Author:** ![manvibharat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manvibharat/32/11635_2.png) [@manvibharat](https://discourse.julialang.org/u/manvibharat)\
**Post date:** [August 14, 2020, 1:21pm UTC](https://discourse.julialang.org/t/does-diffeqflux-jl-supports-a-constrained-optimal-control-problem-where-in-both-control-and-states-are-constrained/44246/3 "2020-08-14T13:21:32Z")

</div>

Thanks for the reply, but is there any example where can get an insight into solving the problem. I am able to solve a simple unconstrained problem however could not find the syntax for adding the constraints for optimal control problem. Any pointers would be much appreciated.

---

<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:** [August 16, 2020, 1:38am UTC](https://discourse.julialang.org/t/does-diffeqflux-jl-supports-a-constrained-optimal-control-problem-where-in-both-control-and-states-are-constrained/44246/4 "2020-08-16T01:38:14Z")

</div>

Instead of using `sciml_train` there, you can do a direct construction of NLopt.jl objective function and use `Zygote.gradient` to compute the gradient.
