# [Ipopt] Optimal control problem (discretize and c++ inerface)

Pablo Perez pabloperez555 at hotmail.com
Thu Sep 15 07:53:45 EDT 2016

```Hi,
First of all, thank you very much to you all for your soon answer, they have provided me with a new focus about my problem.
For solving this problem I have been using until now a dynamic programming method, which always finds a global minimun and does not present convergency issues, but it is very slow and memory gready for allowing use it within an embeded device. So I wish like to migrate from "computer methods" to mathematical ones.
About using a third party framework, for personal reasons I would like to use as many open source software as possible, so I have discarded AMPL for being propietary code. Anyway, CasADi is an open source project with LGPL license, and the few time I have been looking it this morning makes me think it is an awesome tool, and I would like to use it for fast prototyping and test checks. Thank you Joel for your efforts developing and spreading it.
About using the Euler method (RK1 method), it is only for a first version of the program. In next development I was thinking about use higher order methods, as midpoint or RK4. About simultaneus approach, I also was thinking about using it, even I have only very limited knowledge about optimal control. I will check the Biegler's book and look to the example in "Ipopt-3.12.6\Ipopt\tutorial\CodingExercise\Cpp\3-solution" for knowing more about this subject.
About using c++, it is because is the language I better know. I would like to implement all the problem using the Ipopt c++ interface, including the hessian derivatives. In a second approach, I let Ipopt calculate them using the "hessian_approximation->limited-memory" option in code. And finally, i would like to implement the problem using Casadi an use the C source code within my own program, so I can test performance between the three options.
Please, Joel, may you tell me is Casadi can be used directly with "#include <casadi/casadi.hpp>" as in the example you sent to me for defining the problem in the same way as Ipopt does? It is because I am not proficiency in python neither in matlab...
Best regards,Pablo

From: antonello at lobianco.org
Date: Thu, 15 Sep 2016 08:10:06 +0200
Subject: Re: [Ipopt] Optimal control problem (discretize and c++ inerface)
CC: pabloperez555 at hotmail.com; ipopt at list.coin-or.org

For an other solution that uses IPOPT as the solver engine and the Pyomo library (in Python) to code the model see this thread:

On 14 September 2016 at 19:08, Joel Andersson <j.a.e.andersson at gmail.com> wrote:
Hi Pablo!
Since you're interested in optimal control, you might want to have a look at our tool CasADi (http://casadi.org), which is an optimization modeling environment with focus on optimal control.
Here is an example that shows how to do more or less exactly what you're describing:
That is an Euler method discretization for time and then solved with IPOPT via the CasADi interface (which takes care of all derivative calculations).
There are other more efficient ways of solving this problem, however. Especially if you're using IPOPT, it's better to use a "simultaneous approach" and include the entire state trajectory as decision variables instead of the "sequential approach" in the linked example. You should also consider replacing your Euler scheme with something of higher order - that will allow you to get the same accuracy with a more crude time discretization. I would recommend a collocation type integrator scheme (a type of implicit Runge-Kutta methods popular in optimal control) or if you're system is non-stiff, you can also consider the the classical (explicit) Runge-Kutta 4 scheme. Biegler's "Nonlinear Programming: Concepts, Algorithms, and Applications to Chemical Processes" is an excellent reference: http://epubs.siam.org/doi/book/10.1137/1.9780898719383. You'll find implementations of these methods in CasADi.
Also note that you don't necessarily need to do the modeling in C++ even if you're planning to do solve it online in an environment that only has C++. If you're using AMPL, you can can use the AMPL executable to simply to parse the problem and generate a ".nl" file. This file would then be read by the open-source AMPL Solver Library, which is pure C and easy to embed. If you're using CasADi as a modeling environment, you can do the modeling in Python or MATLAB and have CasADi generate C code for the function calls, including first and second order derivative information. It can then be run on an embedded system in a minimalistic CasADi-IPOPT environment.
Best regards,Joel

2016-09-14 8:35 GMT-05:00 Pablo Perez <pabloperez555 at hotmail.com>:

Dear all,
after having discovered Ipopt last month, I have thought using it in my academic research for optimizing the power use of a dynamic of a point mass train.
Due to the optimal control nature of the problem, I understand that the problem must be discretized (by an Euler method, pe). The problem has the next structure:
min J(x, u) = Integral[0, T](f(x, u)·dt);dx/dt = y(x, u);g(x, u) >= K;x_min <= x <= x_max;u_min <= u <= u_max;x(0) = x0;
I have test and understand the examples provided with Ipopt and other users, but all of them are about continuos time problems. The only discrete systems problems I have found are used throught AMPL interface mostly, but I would like using Ipopt throught the C++ interface because it should be embeded within a standalone application.
Please, does anyone knows of some link where there aer an example of how to use Ipopt with a discretized system using c++ interface? Might anybody provide me with any source code example?
Best regards,Pablo

_______________________________________________

Ipopt mailing list

Ipopt at list.coin-or.org

https://urldefense.proofpoint.com/v2/url?u=http-3A__list.coin-2Dor.org_mailman_listinfo_ipopt&d=CwICAg&c=Ngd-ta5yRYsqeUsEDgxhcqsYYY1Xs5ogLxWPA_2Wlc4&r=OtjwlDFa50ew9RXNsKUjcDYXBipGc0uQDVH_0daPLFk&m=EVZY3slpC56CA2pzk9y1rGYqC9FTWeGeR0vY6pONZAA&s=19a6i0ofgIF1oDAvpq3MaUg30PUmRSLFdgJjL7B-kEw&e=

_______________________________________________

Ipopt mailing list

Ipopt at list.coin-or.org

https://urldefense.proofpoint.com/v2/url?u=http-3A__list.coin-2Dor.org_mailman_listinfo_ipopt&d=CwICAg&c=Ngd-ta5yRYsqeUsEDgxhcqsYYY1Xs5ogLxWPA_2Wlc4&r=8oTnxj08sd2ffhbFEprcyo6DDTVDvo_DCJ2aU6CdYWE&m=RBLqMC9Av60yHWVeArfuoN15iLaMbfnkMx7CMFP_q00&s=5IB71TBmk-Eg-mNDJ6CrCVdf8do44GFQkwWwMwQLXbk&e=

--
Antonello Lobianco
AgroParisTech, Laboratoire d'Economie Forestière
14 Rue Girardet - 54000 Nancy, France
Tel: +33.383396865
Email: antonello.lobianco at nancy.inra.fr
http://antonello.lobianco.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://list.coin-or.org/pipermail/ipopt/attachments/20160915/67131b63/attachment-0001.html>
```