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

reduced frames in animation, better initial guess #244

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added examples-gallery/parallel_park_solution.npy
Binary file not shown.
23 changes: 19 additions & 4 deletions examples-gallery/plot_parallel_park.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# %%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this is added as the first line, should be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this is added as the first line, should be removed.

I have to add this in VSC so I can run the simulation from the begining.
I will remove before I push

"""
Parallel Park a Car
===================
Expand Down Expand Up @@ -185,10 +186,14 @@

# %%
# Find the optimal solution.
initial_guess = np.load('parallel_park_solution.npy')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add these to examples, we need to explain to the reader why we are doing so.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add these to examples, we need to explain to the reader why we are doing so.

I will correct. Just slipped.

solution, info = prob.solve(initial_guess)
print(info['status_msg'])
print(info['obj_val'])

# %%
#Improved initial_guess is stored like this
# ```np.save('parallel_park_solution.npy', solution)```
# %%
# Plot the optimal state and input trajectories.
prob.plot_trajectories(solution)
Expand Down Expand Up @@ -223,8 +228,18 @@
coords = []
for xi, ui in zip(xs.T, us.T):
coords.append(eval_point_coords(xi, ui, list(par_map.values())))
coords = np.array(coords) # shape(600, 3, 8)

coords = np.array(coords) # shape(501, 3, 5)

coords1 = []
time1 = []
for i in range(coords.shape[0]):
if i % 3 == 0:
coords1.append(coords[i, :, :])
time1.append(time[i])
coords1.append(coords[-1, :, :])
time1.append(time[-1])
time = np.array(time1)
coords = np.array(coords1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an overly complicated way to skip frames (if that is what it does). It isn't so favorable for such things to be in examples for beginners.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use slicing in numpy that would be a need approach. Though an easier approach in this case would be providing a range like range(0, len(time), 3) to FuncAnimation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an overly complicated way to skip frames (if that is what it does). It isn't so favorable for such things to be in examples for beginners.

Yes, I wanted to skip frames, and I could not think of a simpler way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought one of the examples already has exactly what Timo suggests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use slicing in numpy that would be a need approach. Though an easier approach in this case would be providing a range like range(0, len(time), 3) to FuncAnimation.

Not sure, I understand what slicing means - my Python skill are limited. Could you explain, please.
This "range thing" I understand, I will try!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought one of the examples already has exactly what Timo suggests.
I will look at the examples for this feature, I did not look before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Internet search of "numpy slicing" points to: https://numpy.org/doc/stable/user/basics.indexing.html#slicing-and-striding

Now clear! This I even know, just did not think about it :-(

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the PRs I made today have equally clumsy ways of reducing the frames.
I will improve tomorrow.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Peter230655 in this case it is even easier to use ani = animation.FuncAnimation(fig, animate, range(0, len(time), 3), than numpy slicing. Though learning more about numpy slicing is definitely useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Peter230655 in this case it is even easier to use ani = animation.FuncAnimation(fig, animate, range(0, len(time), 3), than numpy slicing. Though learning more about numpy slicing is definitely useful.

I will try this first thing tomorrow morning!! :-)


def frame(i):

Expand Down Expand Up @@ -261,12 +276,12 @@ def animate(i):


ani = animation.FuncAnimation(fig, animate, len(time),
interval=int(interval_value*1000))
interval=int(interval_value*1000*1))

# %%
# A frame from the animation.

# sphinx_gallery_thumbnail_number = 7
frame(450)
frame(140)

plt.show()
Loading