Update boid_flockers example include flight direction#2696
Conversation
|
Performance benchmarks:
|
|
No worries, it’s all voluntarily. Thanks for communicating your availability clearly, I can pick this one up. |
|
@sanika-n a simple initial test is running the old model for 1000 steps and (manually) timing how long it takes, and then doing the same for the updated model. Make sure to use the same seed in both cases. |
Just added the direction element to all the boids, and these are the timings that I got after updating the non-flocking boids Random seed 42 Altered version Original version |
|
@sanika-n thanks for the benchmarks. A 2x slowdown is more that I would like. It would be interesting to see where the performance degradation is coming from:
If it's the former, we could try to vectorize it. If it's the latter, we could add a marker cache. Something like: import numpy as np
from matplotlib.markers import MarkerStyle
# Pre-compute markers for different angles (e.g., every 10 degrees)
MARKER_CACHE = {}
for angle in range(0, 360, 10):
marker = MarkerStyle(10)
marker._transform = marker.get_transform().rotate_deg(angle)
MARKER_CACHE[angle] = marker
def boid_draw(agent):
neighbors = len(agent.neighbors)
# Calculate the angle
deg = np.degrees(np.arctan2(agent.direction[0], agent.direction[1]))
# Round to nearest 10 degrees
rounded_deg = round(deg / 10) * 10 % 360
if neighbors <= 1:
return {"color": "red", "size": 20, "marker": MARKER_CACHE[rounded_deg]}
elif neighbors >= 2:
return {"color": "green", "size": 20, "marker": MARKER_CACHE[rounded_deg]} |
|
Thank you so much! It's much faster now (5 min 43 seconds for 1000 steps) |
|
Glad to hear it helped, that’s a serious speed-up! It might be good to add a few comments what we’re doing and why. Conceptually and functionally we’re good. @quaquel, could you do a code/implementation review? |
|
@sanika-n can you update this to be in line with the base branch? My agenda is a bit of a mess, but will try to find a few hours soon to go through various open PRs. |
|
@Sahil-Chhoker, @EwoutH, @quaquel This looks like it met all the requirements, can we merge this? |
I updated boid_flockers so that the direction of movement of agents is shown in the simulation.

I did this by changing the marker to caret-down (a triangle with a larger base so that the direction of motion is not ambiguous) and then rotating the marker according to the direction of motion of the agent. Here are some images of boid_flockers after this update: