Are piecewise functions allowed as constraints in Casadi?

1,200 views
Skip to first unread message

pablope...@gmail.com

unread,
Oct 11, 2016, 7:56:54 PM10/11/16
to CasADi
Hello,

I am researching in nlp applied to optimal control, specifically in an very simple electric point mass vehicle model, defined by the next dae:

# min tf
# dx/dt = v
# dv/dt = u - R*v^2
# x(0)=0; x(tf)=L
# v(0)=0; v(tf)=0
# -3 <= u <= 1 (engine constraint)
#
#         {v <= 7m/s ===> u < 1
# u <= {                                        (electric car constraint)
#         {v >  7m/s ===> u < 1*7/v
#
# -1.5 <= dv/dt <= 0.8 (comfort constraint -> smooth driving)
# -0.5 <= d2v/dt2 <= 0.5 (comfort constraint -> jerk)
# v <= Vmax (40 kmh[0-500m] + 25 kmh(500-1000m])


As you can see, the acceleration provided by the electric engine (the only control variable, by the way) must be modelled as a piecewise equation, because it is define by constant acceleration until reaching a specific speed, and then by a constant power hyperbola depending of the vehicle speed (a state variable). Something close happens with the Max allowed speed constraint.

I could model that easily in AMPL, but I would like to migrate to an open source library, so I first tried Pyomo, but it does not work due that this kind of constraints are not allowed by it, due that constrain are not defined as callbacks that are called by the solver in every iteration, but as a container filler called once at model creation, so I wonder if Casadi would allow this feature?

1) If so, I have not found anything even close to this in the examples repository, could someone show me how to do it using some code example?

2) Also, if Casadi does not allow this or implementation becomes hard and/or clumpsy, what about the combination of JModelica + Casadi? Should I install JModelica executable or is Casadi able of opening, parsing and using Modelica source code directly?

Thanks in advance,
Pablo


Joel Andersson

unread,
Oct 12, 2016, 12:12:19 PM10/12/16
to CasADi
Hi Pablo,

What you are describing is a state-dependent switching condition. It wouldn't be hard to formulate this in CasADi, just
introduce a path constraint using the "if_else" atomic:

pathcon = if_else(v<=7, 1,  1*7/v)-u

And then require that pathcon be nonnegative. This would need to be enforced at all time points.

Fundamentally, however, this switch destroys the smoothness properties of the NLP and you might be better off reformulating the problem. If, for example, you know that the solution will consist of two phases, one in which v <= 7m/s and another in which v >= 7m/s, then you could reformulate it as a two-stage problem. You could also replace the if_else with a smooth approximation, but this may or may not be a good idea.

As for how to do this with JModelica.org, this is a question you'd have to pose to the JModelica forum. You'd have to install JModelica.org, which includes an installation of CasADi, although not necessarily the latest version.

Best regards,
Joel

pablope...@gmail.com

unread,
Oct 12, 2016, 6:48:46 PM10/12/16
to CasADi
Thanks for your quick answer

I am still kind of new in optimal control, and don't have the deep understanding of the solving algorithms and how are they related with the problem description, so please, could you answer these questions?

1) When you talk about that using a switch statement destroys the smoothness properties of the NLP, are you talking about a loss of performance, an incompatibility with the solver, infeasibility of the problem...? Please, clarify me this issue because I may be reformulate all the problem.

2) The two stage aproximation is not applicabled in this system, or at least not in an easy and elegant way. So I will like to focus in the smooth approximation proposal. Can you please explain me a little bit more about that or suggesting me some example/book where I can keeping researching by my own?

Best regards,
Pablo

Joel Andersson

unread,
Oct 13, 2016, 10:00:37 AM10/13/16
to CasADi
Hi Pablo,
Algorithms for continuous optimization always require some smoothness properties for the underlying problem. A typical requirement is that the objective and constraint functions are twice continuously differentiable. This is required by the solver (e.g. IPOPT) and the solver may or may not converge to a solution if that isn't satisfied. CasADi will always try to calculate derivatives, but the derivatives are only correct in the "almost everywhere" sense.

I cannot say if it makes sense to do a smooth approximation of if your better off just trying to handle the non-smoothness.

A good introduction to numerical optimal control can be found in Biegler's "Nonlinear Programming": http://epubs.siam.org/doi/book/10.1137/1.9780898719383.

One more note: If your problem really only has two state and one control, you might be able to solve the problem using a "dynamic programming" approach (or, related, by applying the Hamilton-Jacobi-Bellman equation). This is a very different approach, which does not require any NLP solver - or any smoothness properties - and is guaranteed to always give the globally optimal solution. You'll find more about that approach in Bertsekas' "Dynamic Programming and Optimal Control": http://www.athenasc.com/dpbook.html.

Good luck!
Joel

pablope...@gmail.com

unread,
Oct 16, 2016, 8:16:07 PM10/16/16
to CasADi
Thanks again Joel for your attention and advices.

I have already made that path, but in the other way. I began formulating a model based in graph methods that were almost brute force and gave good results but comsumes too many time. Next spep i tried was dynamic programming, still too gready in resouces and time for being used in real time applications. Part of the focus of my research is to point that optimal control can be applied in embedded devices for reducing comsumption in electrical vehicles, so fast calculation must be a must 

I will consult Bieglers' again, and try to smooth the problem constraints as much as possible. By the way, does someone knows any good book or online web page where smoothing techniques are explained? Per example, using tah instead of heaviside funtion???

Thanks,
Pablo
Reply all
Reply to author
Forward
0 new messages