This package is an interface to CasADi, a powerful symbolic framework for automatic differentiation and optimal control. More information are available on the official website. Although Julia has excellent libraries for optimization, they have not reached the maturity of CasADi for nonlinear optimization and optimal control yet. This library aims to give easy access to its powerful capabilities.
Please note:
- This repo is unofficial, not maintained by the original CasADi authors, and not affiliated with the CasADi project.
- There is no plan to include interfaces to all of CasADi capabilities. It has grown out of my own research needs and I am sharing it in case other people find it useful. Since PyCall.jl is used, any aspect of CasADi not implemented in this interface can be easily accessed directly via PyCall.
- I am more than happy to accept contributions and discuss potential changes that could improve this package.
This is not a registered package and there is no plan to register it soon.
There are several steps for installing this package.
- You first need to have
PyCall.jl
installed (which in turns requiresConda.jl
). - In the same environment that
Conda.jl
uses (thusPyCall.jl
uses), install CasADi. Although this package can install CasADi itself usingConda.jl
from conda-forge, I find that the conda-forge version of CasADi often causes problems within Conda / Python. Therefore I would suggest CasADi be installed manually using the official channel, in particular withpip install casadi
. I find that this method is more reliable. Furthermore, I find that using thebase
environment in Conda instead of a dedicated virtual environment forConda.jl
works better. You may want to start Python and test CasADi to make sure that it works well. - Finally, install this package directly from github by activating the package manager
]
and running
add https://github.com/nxtruong/CasADi.jl.git
We will use CasADi to find the minimum of the Rosenbrock function. This can be done as follows
using CasADi
x = SX("x")
y = SX("y")
α = 1
b = 100
f = (α - x)^2 + b*(y - x^2)^2
nlp = Dict("x" => vcat([x ; y]), "f" => f);
S = casadi.nlpsol("S", "ipopt", nlp);
sol = S(x0 = [0, 0]);
println("Optimal solution: x = ", sol["x"].toarray()[1], ", y = ", sol["x"].toarray()[2])
We will use Opti stack to solve the example problem in CasADi's documentation
using CasADi
opti = casadi.Opti();
x = opti._variable()
y = opti._variable()
opti.minimize( (y - x^2)^2 )
opti._subject_to(x^2 + y^2 == 1)
opti._subject_to(x + y >= 1)
opti.solver("ipopt");
sol = opti.solve();
println( "Optimal solution: x = ", sol.value(x), ", y = ", sol.value(y) )