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 #131

Closed Peter230655 closed 3 months ago

Peter230655 commented 3 months ago

I set up a2D closed link n body pendulum (actually just to play around with the holonomic constraint option). What I want to do is to move the first pendulum to an upright position, all others may do what they can.

1. I allow the pendulums to collide, so I need the Heaviside function in setting up Kane's equations to detect whether they have collided or not. I get this error: PrintMethodNotImplementedError: Unsupported by <class 'sympy.printing.c.C99CodePrinter'>: DiracDelta Set the printer option 'strict' to False in order to generate partially printed code.

When I remove the force term with the Heaviside function, this error disappears. Does opty not 'like' step functions?

2. So, I removed the Heaviside. Now I get this error: message from optimizer: b'Algorithm received an invalid number (such as NaN or Inf) from the NLP; see also option check_derivatives_fornaninf.'

What could this be related to?

Thanks for any help!

moorepants commented 3 months ago

opty relies on taking the derivative of the equatiosn of motion with respect to the states and inputs. You have to ensure 1. your EoMs are continuously differentiable*, 2. SymPy can deliver those derivatives, and 3. SymPy can code print them to a form in the C programming language. The error you see shows that the C code printer does not know what to do with a diracdelta.

*This is the ideal situation but you can get away with some computational leniency there if you employ various tricks.

The second error means that the equations probably have a divide-by-zero or some other numerical issue when the EoMs are evaluated.

These aren't so much issues with opty, but issues with SymPy and the user definition of the problems given SymPy's limitations.

Peter230655 commented 3 months ago

Thanks!! 1. So instead of using the Heaviside function I just use a 'self made one' that looks very similar but is differentiable, correct? This seems simple to do. 2. Probably errors in my Kane's equations. This I can check.

moorepants commented 3 months ago

One trick is to make discontinuous functions continuous, for example use the logistic function instead of Heaviside.

Peter230655 commented 3 months ago

Thanks! I will try it. Amazing sideline: chatGPT also suggested the logistics function as a infinitely differential function to minick the step function.

Peter230655 commented 3 months ago

The logistics function worked, thanks! ( the other error still remains, I am checking my equations)

moorepants commented 3 months ago

We tried to use the logistic function in this thesis: https://repository.tudelft.nl/islandora/object/uuid:61f4e969-8bd1-4687-9942-b70024b216dc?collection=education but it ended up not being a good idea and impacts did not simulate well (I forget the details as to why). But we ended up doing multi-phase solutions (not possible with opty unfortunately). Still the constraints we used to manage some discontinuities may be of interest. Here is the recently published paper about it too (shorter): https://doi.org/10.1007/s12283-023-00448-y

We used to have success with using a differentiable Hunt-Crossley based on: https://www.sciencedirect.com/science/article/abs/pii/S0021929009007210