csu-hmc / opty

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

Reduce time it takes to run the examples #204

Open moorepants opened 2 months ago

moorepants commented 2 months ago

Here is the current timing from a clean build:

computation time summary:
    - ../examples-gallery/plot_pendulum_swing_up_variable_duration.py:   110.06 sec   0.0 MB
    - ../examples-gallery/plot_drone.py:                                  75.58 sec   0.0 MB
    - ../examples-gallery/plot_two_link_pendulum_on_a_cart.py:            75.11 sec   0.0 MB
    - ../examples-gallery/plot_sliding_block.py:                          59.82 sec   0.0 MB
    - ../examples-gallery/plot_parallel_park.py:                          58.78 sec   0.0 MB
    - ../examples-gallery/plot_pendulum_swing_up_fixed_duration.py:       56.85 sec   0.0 MB
    - ../examples-gallery/plot_one_legged_time_trial.py:                  44.69 sec   0.0 MB
    - ../examples-gallery/plot_vyasarayani.py:                            11.22 sec   0.0 MB
    - ../examples-gallery/plot_betts2003.py:                              10.89 sec   0.0 MB

Reducing the time it takes to create animations is an easy adjusted, for example. We can safely skip generating a frame for every time instance by skipping frames with a minor adjustment to the code. Here is an example:

https://github.com/csu-hmc/opty/blob/master/examples-gallery/plot_drone.py#L299-L300

moorepants commented 2 months ago

Another way to reduce time is to give better initial guesses. We can even store a solution and give that as a guess.

Peter230655 commented 2 months ago

Another way to reduce time is to give better initial guesses. We can even store a solution and give that as a guess.

How can I store a solution, so it does not get lost when the program is closed? I thought about this often, but never came up with an idea... :-( (I mean short of printing the solution and then typüing it into the program...)

moorepants commented 2 months ago

You can save any value to a txt file with many different methods. NumPy has builtin ways to do this: https://numpy.org/doc/stable/reference/routines.io.html

Peter230655 commented 2 months ago

You can save any value to a txt file with many different methods. NumPy has builtin ways to do this: https://numpy.org/doc/stable/reference/routines.io.html

Amazing how this works - and it is two lines of code initially, no additional line in the final simulation. (After your hint, chatGPT told me the lines) I once modeled a drone with propellers, the one that wiggled and it cut down the simulation time from over one minute to 16 sec. - still wiggles, though :-)

moorepants commented 1 month ago

Here is the output from a recent build on the readthedocs system, which is slower than running it locally:

computation time summary:
    - ../examples-gallery/plot_parallel_park.py:                             128.56 sec   0.0 MB
    - ../examples-gallery/plot_pendulum_swing_up_variable_duration.py:       127.54 sec   0.0 MB
    - ../examples-gallery/plot_pendulum_swing_up_fixed_duration.py:          124.13 sec   0.0 MB
    - ../examples-gallery/plot_sliding_block.py:                             105.60 sec   0.0 MB
    - ../examples-gallery/plot_ball_rolling_on_spinning_disc.py:              95.81 sec   0.0 MB
    - ../examples-gallery/plot_drone.py:                                      64.82 sec   0.0 MB
    - ../examples-gallery/plot_one_legged_time_trial.py:                      63.60 sec   0.0 MB
    - ../examples-gallery/plot_two_link_pendulum_on_a_cart.py:                49.41 sec   0.0 MB
    - ../examples-gallery/plot_crane_moving_a_load.py:                        33.36 sec   0.0 MB
    - ../examples-gallery/plot_non_contiguous_parameter_identification.py:    12.81 sec   0.0 MB
    - ../examples-gallery/plot_vyasarayani.py:                                12.40 sec   0.0 MB
    - ../examples-gallery/plot_betts2003.py:                                  11.66 sec   0.0 MB
Peter230655 commented 1 month ago

If I understand correctly, this is what I tried with the examples that have animations. Should be in the PRs.

moorepants commented 1 month ago

Yes, you are. I'm just logging information here on the broader issue. I will get to all your PRs, but it just takes time.

Peter230655 commented 1 month ago

Yes, you are. I'm just logging information here on the broader issue. I will get to all your PRs, but it just takes time.

I am in NO rush, I have time! Just wanted to make sure I understood right - which you confirmed!

NB: now that I know examples-gallery a bit better, I do not think, that my sailboat and my Mississippi steamer contribute much. The Kane part may be interesting - but the opty is standard. My double body skateboard at least shows, that iterating from a simple problem to a harder one can help opty to find a solution - but otherwise it is very contrived. Anyway, you be the judge.

moorepants commented 1 month ago

We can add them all as long as we keep the time down and can always delete them later if we every have to replace one.

Peter230655 commented 1 month ago

We can add them all as long as we keep the time down and can always delete them later if we every have to replace one.

makes emminent sense!