Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

two link pendulum to be uprighted using opty with variable h #178

Merged
merged 5 commits into from
Jun 30, 2024

Conversation

Peter230655
Copy link
Contributor

A two linked pendulum is attached to a cart which can move in the horizontal (X axis) direction). A force may be applied to the cart. The goal is to get the pendulum into an upright position as fast as possible, by moving the cart left / right. The absolute value of the force is limited.

@moorepants
Copy link
Member

Congrats on making the pull request! This looks very good but will need some tweaks before we merge it.

I see that you edited the master branch of your clone. It is best to make a new branch (from master) with a unique name and then make the PR from that branch. This is important, for example, when I clone your PR branch so it does not conflict with my local master branch.

@moorepants
Copy link
Member

The thumbnail doesn't seem to be selected with the right number:
image

See https://sphinx-gallery.github.io/stable/auto_examples/plot_4_choose_thumbnail.html for more info.

@moorepants
Copy link
Member

No plots or animations seem to be displaying:
image

@moorepants
Copy link
Member

The file name needs to be named in this fashion:

plot_two_link_pendulum_on_a_cart.py

i.e. start with plot_ and use underscores.

@moorepants
Copy link
Member

When I ran this the first solve gives:

1161  1,6179697e-02 4,19e-08 2,52e-11 -11,0 2,88e-02    -  1,00e+00 1,00e+00h  1

Number of Iterations....: 1161

                                   (scaled)                 (unscaled)
Objective...............:   1,6179697365982335e-02    1,6179697365982335e-02
Dual infeasibility......:   2,5208033631335926e-11    2,5208033631335926e-11
Constraint violation....:   4,9616580890162378e-09    4,1858328359012369e-08
Variable bound violation:   1,1037084135523401e-08    1,1037084135523401e-08
Complementarity.........:   1,0000000004925789e-11    1,0000000004925789e-11
Overall NLP error.......:   4,9616580890162378e-09    4,1858328359012369e-08


Number of objective function evaluations             = 1273
Number of objective gradient evaluations             = 1162
Number of equality constraint evaluations            = 1273
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 1162
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 0
Total seconds in IPOPT                               = 22,866

EXIT: Optimal Solution Found.

So an optimal solution is found in the first call to solve. That is all you need for this example, otherwise it takes too long. It took 22 seconds for 1161 iterations, which is ok time wise.

@Peter230655
Copy link
Contributor Author

When I ran this the first solve gives:

1161  1,6179697e-02 4,19e-08 2,52e-11 -11,0 2,88e-02    -  1,00e+00 1,00e+00h  1

Number of Iterations....: 1161

                                   (scaled)                 (unscaled)
Objective...............:   1,6179697365982335e-02    1,6179697365982335e-02
Dual infeasibility......:   2,5208033631335926e-11    2,5208033631335926e-11
Constraint violation....:   4,9616580890162378e-09    4,1858328359012369e-08
Variable bound violation:   1,1037084135523401e-08    1,1037084135523401e-08
Complementarity.........:   1,0000000004925789e-11    1,0000000004925789e-11
Overall NLP error.......:   4,9616580890162378e-09    4,1858328359012369e-08


Number of objective function evaluations             = 1273
Number of objective gradient evaluations             = 1162
Number of equality constraint evaluations            = 1273
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 1162
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 0
Total seconds in IPOPT                               = 22,866

EXIT: Optimal Solution Found.

So an optimal solution is found in the first call to solve. That is all you need for this example, otherwise it takes too long. It took 22 seconds for 1161 iterations, which is ok time wise.

Should I remove the loop, or just run through it once?
(This looping has helped me with some examples, so maybe useful if users see it?)

@moorepants
Copy link
Member

Should I remove the loop, or just run through it once?

Remove the loop. For these "simple" examples, we should keep them short and simple (both in code length and in run time). If an optimal solution is found with one call to .solve() that is sufficient.

@Peter230655
Copy link
Contributor Author

No plots or animations seem to be displaying: image

In my ipynb files, if I add the line
display(HTML(anim.to_jshtml()))

it does give the animation.
I must admit, that I have never written a program in 'real' python, always Jupyter notebooks, so I do not know how to make anaanimation in an python program. :-(

@moorepants
Copy link
Member

display(HTML(anim.to_jshtml())) is not needed here, you have to follow the instruction in the sphinx-gallery documentation and/or copy exactly how I do the other examples files that start with plot_.

@Peter230655
Copy link
Contributor Author

Congrats on making the pull request! This looks very good but will need some tweaks before we merge it.

I see that you edited the master branch of your clone. It is best to make a new branch (from master) with a unique name and then make the PR from that branch. This is important, for example, when I clone your PR branch so it does not conflict with my local master branch.

Do I understand this correctly:

  • I make a new fork
  • I clone it to my PC
  • I use this to make changes
    It is such things I have trouble with! :-((

@moorepants
Copy link
Member

It works for me now! Great. Here are some more issues to fix:

The thumbnail still does not display (the plot also doesn't display in the notebook):
image

Separate the two plot calls so the plots are large"
image

It took 40 seconds in IPOPT, would be nice if this was < 20:

Number of Iterations....: 1161

                                   (scaled)                 (unscaled)
Objective...............:   1,6179697365982335e-02    1,6179697365982335e-02
Dual infeasibility......:   2,5208033631335926e-11    2,5208033631335926e-11
Constraint violation....:   4,9616580890162378e-09    4,1858328359012369e-08
Variable bound violation:   1,1037084135523401e-08    1,1037084135523401e-08
Complementarity.........:   1,0000000004925789e-11    1,0000000004925789e-11
Overall NLP error.......:   4,9616580890162378e-09    4,1858328359012369e-08


Number of objective function evaluations             = 1273
Number of objective gradient evaluations             = 1162
Number of equality constraint evaluations            = 1273
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 1162
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 0
Total seconds in IPOPT                               = 43,845

If you can provide a better initial guess we could reduce this time.

@moorepants
Copy link
Member

Do I understand this correctly:

I make a new fork
I clone it to my PC
I use this to make changes
It is such things I have trouble with! :-((

You make a single fork on github of the opty repository: makes a copy of this repo under your github account name

You then clone your fork to your PC using git.

You then create a new branch git checkout -b my-new-example.

Make edits to the code or add new files using git add and git commit.

When happy with changes push your new branch to your fork on github: git push origin ny-new-example.

Github will then say "would you like to make a PR?" (if it is the first push). Make the PR (which is what we we now have here).

Then we review your code and you have to make changes. Locally your edit the file(s) in the same branch "my-new-example". Then when happy you git commit the changes and push them to your fork git push. This will update this PR with new changes and we continue reviewing. We repeat this cycle until the example is up to snuff and then I will merge it.

@Peter230655
Copy link
Contributor Author

Do I understand this correctly:

I make a new fork
I clone it to my PC
I use this to make changes
It is such things I have trouble with! :-((

You make a single fork on github of the opty repository: makes a copy of this repo under your github account name

You then clone your fork to your PC using git.

You then create a new branch git checkout -b my-new-example.

Make edits to the code or add new files using git add and git commit.

When happy with changes push your new branch to your fork on github: git push origin ny-new-example.

Github will then say "would you like to make a PR?" (if it is the first push). Make the PR (which is what we we now have here).

Then we review your code and you have to make changes. Locally your edit the file(s) in the same branch "my-new-example". Then when happy you git commit the changes and push them to your fork git push. This will update this PR with new changes and we continue reviewing. We repeat this cycle until the example is up to snuff and then I will merge it.

The fork and the clone on my PC I already have , otherwise I could not have done, what I have been doing so far. Correct?
git checkout -b my-new-example. Where do I do this? in Git Bash?
Sorry about these absolutely stupid questions!!

@moorepants
Copy link
Member

This should be a well behaved problem. It isn't so complex. I think choosing the right # of nodes and an good initial guess is needed. For example I set num_nodes=400 and initial guess:

initial_guess = np.zeros(prob.num_free)
initial_guess[-1] = 0.01

and I got:

Number of Iterations....: 394

                                   (scaled)                 (unscaled)
Objective...............:   7.1091941500737625e-03    7.1091941500737625e-03
Dual infeasibility......:   4.5693976317924821e-09    4.5693976317924821e-09
Constraint violation....:   5.6629071802187058e-10    1.6988721540656115e-09
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   7.9277052543380919e-09    7.9277052543380919e-09
Overall NLP error.......:   7.9277052543380919e-09    7.9277052543380919e-09


Number of objective function evaluations             = 428
Number of objective gradient evaluations             = 395
Number of equality constraint evaluations            = 428
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 397
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 0
Total seconds in IPOPT                               = 32.685

EXIT: Optimal Solution Found.

This gives a 2.84 second swing up and very smooth bang bang input.

@Peter230655
Copy link
Contributor Author

This should be a well behaved problem. It isn't so complex. I think choosing the right # of nodes and an good initial guess is needed. For example I set num_nodes=400 and initial guess:

initial_guess = np.zeros(prob.num_free)
initial_guess[-1] = 0.01

and I got:

Number of Iterations....: 394

                                   (scaled)                 (unscaled)
Objective...............:   7.1091941500737625e-03    7.1091941500737625e-03
Dual infeasibility......:   4.5693976317924821e-09    4.5693976317924821e-09
Constraint violation....:   5.6629071802187058e-10    1.6988721540656115e-09
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   7.9277052543380919e-09    7.9277052543380919e-09
Overall NLP error.......:   7.9277052543380919e-09    7.9277052543380919e-09


Number of objective function evaluations             = 428
Number of objective gradient evaluations             = 395
Number of equality constraint evaluations            = 428
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 397
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 0
Total seconds in IPOPT                               = 32.685

EXIT: Optimal Solution Found.

This gives a 2.84 second swing up and very smooth bang bang input.

I guess because I am retired and I have time, I never put much emphasis on the speed. :-)
I will do so in future - after all life left with me is less! :-) :-)

@moorepants
Copy link
Member

I guess because I am retired and I have time, I never put much emphasis on the speed. :-)

The speed matters for these examples because we run the automated building on Github's computers which they let use use for free up to limits. One of the limits is the length of computation time. It's best if we keep the examples simple and lean so we can add lots of them.

@Peter230655
Copy link
Contributor Author

This should be a well behaved problem. It isn't so complex. I think choosing the right # of nodes and an good initial guess is needed. For example I set num_nodes=400 and initial guess:

initial_guess = np.zeros(prob.num_free)
initial_guess[-1] = 0.01

and I got:

Number of Iterations....: 394

                                   (scaled)                 (unscaled)
Objective...............:   7.1091941500737625e-03    7.1091941500737625e-03
Dual infeasibility......:   4.5693976317924821e-09    4.5693976317924821e-09
Constraint violation....:   5.6629071802187058e-10    1.6988721540656115e-09
Variable bound violation:   0.0000000000000000e+00    0.0000000000000000e+00
Complementarity.........:   7.9277052543380919e-09    7.9277052543380919e-09
Overall NLP error.......:   7.9277052543380919e-09    7.9277052543380919e-09


Number of objective function evaluations             = 428
Number of objective gradient evaluations             = 395
Number of equality constraint evaluations            = 428
Number of inequality constraint evaluations          = 0
Number of equality constraint Jacobian evaluations   = 397
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations             = 0
Total seconds in IPOPT                               = 32.685

EXIT: Optimal Solution Found.

This gives a 2.84 second swing up and very smooth bang bang input.

with these initial conditions I got thes results:

message from optimizer: b'Algorithm terminated successfully at a locally optimal point, satisfying the convergence tolerances (can be specified by options).'
Iterations needed 511
objective value 7.109e-03
h value: 0.007109257724212399

similar to yours.

@moorepants
Copy link
Member

Let's stick with the ~2.84 second colution and if it takes 30 seconds to find we can merge it for now.

It think a better initial guess would be zeros for everything except: q2 & q3 as a linear function from initial value to final value, h as the expected ~0.01 and that might make it a bit faster to find it.

@Peter230655
Copy link
Contributor Author

I guess because I am retired and I have time, I never put much emphasis on the speed. :-)

The speed matters for these examples because we run the automated building on Github's computers which they let use use for free up to limits. One of the limits is the length of computation time. It's best if we keep the examples simple and lean so we can add lots of them.

Understood. I did not know this.

@Peter230655
Copy link
Contributor Author

Let's stick with the ~2.84 second colution and if it takes 30 seconds to find we can merge it for now.

It think a better initial guess would be zeros for everything except: q2 & q3 as a linear function from initial value to final value, h as the expected ~0.01 and that might make it a bit faster to find it.

I will try this tomorrow morning.
Dumb question: Does the animation work with you?

@moorepants
Copy link
Member

All of the figures and animation work expect for the last frame, which I explained why above.

@moorepants moorepants merged commit 2413380 into csu-hmc:master Jun 30, 2024
21 checks passed
@moorepants
Copy link
Member

Hi Peter, I added the linear initial guess and it solves very quickly. Merging! Congrats on the PR!

You can see the result here: https://opty.readthedocs.io/latest/examples/plot_two_link_pendulum_on_a_cart.html

If you want to tweak it more, open a new PR with edits to the example.

@moorepants
Copy link
Member

Also see that you are now a contributor: https://github.com/csu-hmc/opty/graphs/contributors

@Peter230655
Copy link
Contributor Author

In settings, I would go there and click delete this repository?
I would also delete it on my PC?
for my next attempt at contributing a simulation, I would create a fork again, but then go by the instructions with git (I hope I manage!) you gave me?
image

@Peter230655
Copy link
Contributor Author

Also see that you are now a contributor: https://github.com/csu-hmc/opty/graphs/contributors

I must admit, that I am a bit proud of this! :-)
I was not 100% sure I'd manage.

@moorepants
Copy link
Member

Don't delete anything. You only need to make a fork once (copy of this repo to your github account) and keep that forever. Also you only need to clone once to your PC.

@Peter230655
Copy link
Contributor Author

Peter230655 commented Jul 1, 2024

Pull request closed

If you wish, you can delete this fork of csu-hmc/opty in the settings.

I got this message:
image

@moorepants
Copy link
Member

I have never seen that delivered as a message, but it is a screen in the settings of a repository. Do not delete anything.

@Peter230655
Copy link
Contributor Author

I have never seen that delivered as a message, but it is a screen in the settings of a repository. Do not delete anything.

Clear, thanks! Sorry about the confusion!

@Peter230655
Copy link
Contributor Author

Peter230655 commented Jul 3, 2024

git checkout -b my-new-example. Where do I do this? in Git Bash?

Yes, but not for this example, as you have already made the PR. Next time make sure to create a custom branch.

Thanks! I just turned 69, when I will be 75 I hope I will know all of it! :-)

I have prepared my new optimization to the requirements of example-gallery to the best of my ability. I want to call it plot_sliding_block

  1. where do I save it? On my local clone of my opty fork in the folder examples-gallery? (This is where I saved my first example)
  2. After having saved it (wherever you tell me), next I got to Git Bash and type: git checkout -b plot_sliding-block ?
    Sorry about all these questions, but I want to learn it before I will turn 75!

@moorepants
Copy link
Member

On my local clone of my opty fork in the folder examples-gallery?

Yes.

git checkout -b plot_sliding-block

yes, but it also depends on the state of your git repo. I would do this:

git branch

to check what branch i'm on. If on master, then you need to pull in the changes from the main repository:

git pull upstream master

but 'upstream should be set up to point to the main repo before that command will work:

git remote add upstream git@github.com:csu-hmc/opty.git

Now:

git pull upstream master

This updates your local master branch with changes in the main repo's master branch.

Now you create a new branch for you new example:

git checkout -b my_new_example

Add any files with git add <path_to_file> and then git commit <path_to_file> -m "<commit message>".

When ready you push the branch to your fork:

git push origin my_new_example

and you can open a PR.

@moorepants
Copy link
Member

This is a full lesson explaining the basics of git commands: https://swcarpentry.github.io/git-novice/03-create.html

@Peter230655
Copy link
Contributor Author

On my local clone of my opty fork in the folder examples-gallery?

Yes.

git checkout -b plot_sliding-block

yes, but it also depends on the state of your git repo. I would do this:

git branch

to check what branch i'm on. If on master, then you need to pull in the changes from the main repository:

git pull upstream master

but 'upstream should be set up to point to the main repo before that command will work:

git remote add upstream git@github.com:csu-hmc/opty.git

Now:

git pull upstream master

This updates your local master branch with changes in the main repo's master branch.

Now you create a new branch for you new example:

git checkout -b my_new_example

Add any files with git add <path_to_file> and then git commit <path_to_file> -m "<commit message>".

When ready you push the branch to your fork:

git push origin my_new_example

and you can open a PR.

GREAT set of instructions!!
I do this on Git Bash, right?

@moorepants
Copy link
Member

Yes.

@Peter230655
Copy link
Contributor Author

Peter230655 commented Jul 4, 2024

Already stuck! :-(
I saved my program as plot_sliding_block on my PC in the clone of opty, in examples-gallery.
I went into Git Hub to make sure my fork was synced with master.
I opened Git Bash - and I got this:

image

my clone is called opty and has this path
C:\Users\Peter\github_repos\opty\examples-gallery
My optimization is in examples-gallery

@moorepants
Copy link
Member

Google the error message.

@Peter230655
Copy link
Contributor Author

Google the error message.

I need git init first, but still not working right.
As I understood, I cannot destroy anything by palying around. :-)

@moorepants
Copy link
Member

You are not in the correct directory in BASH.

I recommend doing these two lessons (in full and in order):

https://swcarpentry.github.io/shell-novice/

https://swcarpentry.github.io/git-novice/

There are even videos online:

https://www.youtube.com/watch?v=U3iNcBtycaQ&list=PLUQy4zfrctjH-FsjpIZDZ0NBznvF4FdNP

https://www.youtube.com/watch?v=evaOmk10wrw

@Peter230655
Copy link
Contributor Author

I watched the first video once and started with the second. Frustrating how little I know!
I hope after the videos I will understand the tow lessons easier.
Thanks a lot for thes pointers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants