csu-hmc / opty

A library for using direct collocation in the optimization of dynamic systems.
http://opty.readthedocs.io
Other
86 stars 20 forks source link

Question concerning sm.Piecewise(...) #140

Closed Peter230655 closed 3 months ago

Peter230655 commented 3 months ago

I am trying to model a cart with a fixed rear axle perpendicular to the cart, and a steerable front axle. Speed constraints are that motion is not possible perpendicular to the wheels (just like in skateboard-timo) Let qf be the angle of the front axle relative to the cart, and qf = 0 means, that front and rear acles are parallel (and perpendicular to the cart). Im my EOMs I get a term $\dfrac{\text{some expression}}{\sin(q_f)}$ again very much like skateboard-timo. I guess mechanically clear since when $q_f = 0$ the cart looses a degree of freedom - but it causes nan / inf errors in opty. So, I modified this entry of my EOMs like this:

auxiliary = sm.Piecewise((EOM[offending term], qf != 0), (0., True)) EOM[offending term] = auxiliary

Now my question: Now I do not get errors, it does not stop running. While a very similar simulation would take maybe 90 sec, this one has taken 3 hours so far. Can Piecewise slow down opty so much?

NB: sm.Piecewise(...) is the only way I know to mimick "if.....else" in sympy. Is there something better?

Thanks for any hints!

moorepants commented 3 months ago

You are introducing a discontinuity with that piecewise term and the solutions to these optimal control problem rely on continuous formulations.

Best to try to avoid the 1/sin(q) in the first place if possible.

Peter230655 commented 3 months ago

Thanks! So, similar to my 'Heaviside' problem, I had earlier, except that Heaviside was caught and gave an error?