-
Notifications
You must be signed in to change notification settings - Fork 948
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
updated boid_flockers to include direction #2696
base: main
Are you sure you want to change the base?
Conversation
Performance benchmarks:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, this is really useful. Code itself looks good!
Why don't the non-blocking boids have an direction?
Have you any idea on the performance impact?
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. |
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: