Opti Stack Inverted Pendulum

533 views
Skip to first unread message

John Biscanti

unread,
Aug 2, 2021, 4:54:18 PM8/2/21
to CasADi
Hello,

I am working towards solving an inverted pendulum problem using opti stack. I currently am using trapezoidal collocation. After reading through some examples, I came up with my current code. However, I am having issues understanding the casadi syntax in my trapezoidal collocation. I already have this running using scipy and am trying to follow the race car example.

 I really would appreciate any guidance or advice on this issue. This example could be used as an example for others hoping to learn opti! I hope to get it to work and it could surely be posted somewhere for others. Code is attached

Thank you,
John

casadicartpole.py

John Biscanti

unread,
Aug 2, 2021, 4:55:33 PM8/2/21
to CasADi
for reference. The problem is called cartpole in this document...https://epubs.siam.org/doi/pdf/10.1137/16M1062569

Joris Gillis

unread,
Aug 3, 2021, 5:26:24 PM8/3/21
to CasADi
Dear John,

Your file had quite a rich variety in small mistakes..

Best regards,
  Joris

John Biscanti

unread,
Aug 3, 2021, 5:55:59 PM8/3/21
to CasADi
Wow, thank you, Joris. I now see how many things I was missing. This is pretty neat stuff. Thanks a bunch.

Best,
John

Wesley Roozing

unread,
Jan 14, 2022, 11:44:06 AM1/14/22
to CasADi
Joris, thanks for providing a fixed example to this question. Allow me to hook on to this thread - I've been working on some OCP and have implemented multiple-shooting, trapezoidal collocation, and Hermite-Simpson collocation. The latter two suffer from an issue in the first and last state and input values. (To be exact; the inputs jump to half the value next to it, and the state follows suit given those inputs).

I had implemented the dynamics constraints akin to:
    f1 = f(x[:,k+1],u[:,k+1])
    f0 = f(x[:,k],u[:,k])
    opti.subject_to(h/2*(f1+f0)==x[:,k+1]-x[:,k])
Notice the k+1 on the input for f1, which is k in your fixed example.
This implementation, in my view, was consistent with piecewise linear (as opposed to piecewise constant) inputs, and what I understand from Eq. (4.56) of Betts' book. Am I wrong here?

Wesley Roozing

unread,
Feb 7, 2022, 8:50:18 AM2/7/22
to CasADi
Anyone have thoughts about this?

Wesley Roozing

unread,
Jun 16, 2022, 1:44:16 PM6/16/22
to CasADi
For future reference for anyone reading this thread; my comment above appears to be correct.

In my case, the problem was actually how I transcribed my cost function, which included the integral of a squared variable. This transcription also needs to be done using the trapezoid rule, which I hadn't.
See e.g. 2.8 of Matthew Kelly's SIAM review paper (DOI 10.1137/16M1062569):
Screenshot from 2022-06-16 19-42-16.png
I had simply written h*casadi.dot(u,u), which is only valid for piecewise constant (ZOH inputs).

Bruno M L

unread,
Jul 15, 2022, 2:39:33 PM7/15/22
to CasADi
Hi!

To implement integrals in the objective, you can try the quadrature state on the integration. Take a look on direct_multiple_shooting.py example and Casadi Docs.

Good luck!
Reply all
Reply to author
Forward
0 new messages