# Using casadi from Julia

**URL:** <https://discourse.julialang.org/t/using-casadi-from-julia/63988>\
**Category:** Optimization (Mathematical)\
**Tags:** pycall\
**Created:** [July 3, 2021, 1:55pm UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988 "2021-07-03T13:55:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![adityac](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@adityac](https://discourse.julialang.org/u/adityac)\
**Post date:** [July 3, 2021, 1:55pm UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/1 "2021-07-03T13:55:20Z")

</div>

I am trying to use Casadi from Julia using the opti stack. I am trying to do this using PyCall. I get an error when I use the following code:

```julia
opti = casadi.Opti();

x = opti.variable();
y = opti.variable();

opti.minimize((1-x)^2+(y-x^2)^2);

opti.solver('ipopt');
sol = opti.solve();

plot(sol.value(x),sol.value(y),'o');

```

However, if I replace it with the following code the error disappears.

```julia
opti = casadi.Opti();

x = opti._variable();
y = opti._variable();

opti.minimize((1-x)^2+(y-x^2)^2);

opti.solver('ipopt');
sol = opti.solve();

plot(sol.value(x),sol.value(y),'o');

```

How, is opti.\_variable() different from opti.variable()? I could not find any documentation of \_ usage in PyCall. Can anyone point me to it?

Note 1: I got to know about \_ usage from example on [GitHub - ichatzinikolaidis/CasADi.jl: Julia interface to CasADi via PyCall](https://github.com/ichatzinikolaidis/CasADi.jl).

Note 2: I am trying to learn numerical optimal control and hence want to implement the algorithms given in [Practical Methods for Optimal Control and Estimation Using Nonlinear ... - John T. Betts - Google Books](https://books.google.co.in/books/about/Practical_Methods_for_Optimal_Control_an.html?id=n9hLriD8Lb8C&source=kp_book_description&redir_esc=y). It seems that second order methods are better suited for this.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [July 3, 2021, 1:57pm UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/2 "2021-07-03T13:57:45Z")

</div>

Does casadi offer some functionality you require and can’t find in the native Julia offerings, such as [JuMP](https://github.com/jump-dev/JuMP.jl) ?

---

<div class="post-metadata">

**Author:** ![adityac](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@adityac](https://discourse.julialang.org/u/adityac)\
**Post date:** [July 3, 2021, 2:10pm UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/3 "2021-07-03T14:10:29Z")

</div>

I am using Casadi for numerical optimal control. Initially I was trying to use Jump. However, due to the issue mentioned here ([https://github.com/jump-dev/JuMP.jl/issues/1198](https://github.com/jump-dev/JuMP.jl/issues/1198)) JuMP is not suitable for my application.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 3, 2021, 3:25pm UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/4 "2021-07-03T15:25:22Z")

</div>

How about using some native Julia packages for numerical optimal control such as [GitHub - RoboticExplorationLab/TrajectoryOptimization.jl: A fast trajectory optimization library written in Julia](https://github.com/RoboticExplorationLab/TrajectoryOptimization.jl) or [GitHub - JuliaMPC/NLOptControl.jl: nonlinear control optimization tool](https://github.com/JuliaMPC/NLOptControl.jl) ?

---

<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:** [July 3, 2021, 3:58pm UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/5 "2021-07-03T15:58:02Z")

</div>

Second order optimization using Ipopt or the augmented Lagrangian algorithm are supported in [https://github.com/mohamed82008/Nonconvex.jl](https://github.com/mohamed82008/Nonconvex.jl). I use AD for that.

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [July 3, 2021, 4:03pm UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/6 "2021-07-03T16:03:23Z")

</div>

You may perhaps want to share the (minimal version of the) optimal control problem statement here, possibly accompanied by a casadi-based python code for its solving. It may happen that somebody here will be able to show the native Julia way to solve the problem.

---

<div class="post-metadata">

**Author:** ![adityac](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@adityac](https://discourse.julialang.org/u/adityac)\
**Post date:** [July 3, 2021, 4:55pm UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/7 "2021-07-03T16:55:01Z")

</div>

I could use package like NLOptControl directly. However, I am trying to learn numerical optimal control and hence want to implement the algorithms given in [Practical Methods for Optimal Control and Estimation Using Nonlinear … - John T. Betts - Google Books](https://books.google.co.in/books/about/Practical_Methods_for_Optimal_Control_an.html?id=n9hLriD8Lb8C&source=kp_book_description&redir_esc=y).

---

<div class="post-metadata">

**Author:** ![adityac](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@adityac](https://discourse.julialang.org/u/adityac)\
**Post date:** [July 3, 2021, 4:56pm UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/8 "2021-07-03T16:56:38Z")

</div>

Thank you for the link. Looks good. I will try using it.

---

<div class="post-metadata">

**Author:** ![IlyaOrson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ilyaorson/32/1681_2.png) [@IlyaOrson](https://discourse.julialang.org/u/IlyaOrson)\
**Post date:** [July 4, 2021, 7:21pm UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/9 "2021-07-04T19:21:58Z")

</div>

I think the closest to casadi in julia right now, as a DSL for control problems, would be a mix of [ModelingToolkit](https://mtk.sciml.ai/stable/tutorials/nonlinear_optimal_control/) and [DiffEqFlux](https://diffeqflux.sciml.ai/dev/examples/optimal_control/), being the former the “direct method” and the later the “indirect method”, following control terminology.

When I reproduce your error with my copy of casadi it points to an automatically generated file, but the distinction between `variable` and `_variable` comes from casadi’s internals. Not really sure why the `get_frame` call makes PyCall throw an error.

> **generated code from casadi**
>
> ```python
> # This file was automatically generated by SWIG (http://www.swig.org).
> # Version 3.0.11
> ...
> def _variable(self, *args):
> """
> Create a decision variable (symbol)
> 
> _variable(self, int n, int m, str attribute) -> MX
> 
> The order of creation matters. The order will be reflected in the
> optimization problem. It is not required for decision variables to actualy
> appear in the optimization problem.
> 
> Parameters:
> -----------
> 
> n: number of rows (default 1)
> 
> m: number of columnss (default 1)
> 
> attribute: 'full' (default) or 'symmetric'
> """
> return _casadi.Opti__variable(self, *args)
> ...
> def variable(self,*args):
> import sys
> import os
> frame = sys._getframe(1)
> meta = {"stacktrace": {"file":os.path.abspath(frame.f_code.co_filename),"line":frame.f_lineno,"name":frame.f_code.co_name}}
> ret = self._variable(*args)
> self.update_user_dict(ret, meta)
> return ret
> 
> ```

---

<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:** [July 4, 2021, 11:28pm UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/10 "2021-07-04T23:28:32Z")

</div>

Just to clarify, JuMP will use second derivative information if you write out the problem algebraically.  
It’s only if you write a [user-defined function](https://jump.dev/JuMP.jl/stable/manual/nlp/#User-defined-Functions) that we disable hessians.

Your first example in JuMP is:

```julia
using JuMP, Ipopt
model = Model(Ipopt.Optimizer)
@variable(model, x)
@variable(model, y)
@NLobjective(model, Min, (1 - x)^2 + (y - x^2)^2)
optimize!(model)

```

---

<div class="post-metadata">

**Author:** ![adityac](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@adityac](https://discourse.julialang.org/u/adityac)\
**Post date:** [July 5, 2021, 2:33am UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/11 "2021-07-05T02:33:56Z")

</div>

I had initially tried JuMP. In particular the example given [here](https://jump.dev/JuMP.jl/stable/tutorials/Nonlinear%20programs/rocket_control/). However, if I want to automate the procedure (the user will enter the dynamics and constraints on controls and states and the program will transcribe the problem in format suitable for JuMP) hessians of user defined functions would be required. There could be a workaround using macros but I am not proficient in Julia.

---

<div class="post-metadata">

**Author:** ![adityac](https://avatars.discourse-cdn.com/v4/letter/a/ee59a6/32.png) [@adityac](https://discourse.julialang.org/u/adityac)\
**Post date:** [July 5, 2021, 2:47am UTC](https://discourse.julialang.org/t/using-casadi-from-julia/63988/12 "2021-07-05T02:47:25Z")

</div>

The Python version of the code uses the name “variable()”. However, I am not able to use the name “variable()” using PyCall.

```julia
using PyCall

cd = pyimport("casadi")

opti = cd.Opti()

x = py"$(opti).variable()"
y = opti.variable()

```

In this code the variable x is defined without error. But defining y throws an error.
