-
Notifications
You must be signed in to change notification settings - Fork 4
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
examples: Adding Sugarscape IG - polars with numba vectorized loop #91
base: main
Are you sure you want to change the base?
examples: Adding Sugarscape IG - polars with numba vectorized loop #91
Conversation
for more information, see https://pre-commit.ci
Codecov ReportAll modified and coverable lines are covered by tests ✅ |
…oves aren't duplicated
…o 67-sugarscape-instantaneous-growback-polars-with-numba
…hborhood. For both numba and completely vectorized it's easier to reason this way then update the current sugar and "best moves" ranking when agents move
…ght make the same move and haven't found the optimal move yet). This avoids race conditions.
…ssary since we prepare the neighborhood looking at potential/max sugar anyway)
…ing in the future)
This is ready. |
|
||
# Filter impossible moves | ||
# Filter only possible moves (agent is in his cell, blocking agent has moved before him or there is no blocking agent) |
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.
Why is the previous one, although more concise, not considered favorable?
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.
Do you mean the previous implementation?
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.
Yes.
# Agents can make the move if: | ||
# - There is no blocking agent | ||
# - The agent is in its own cell | ||
# - The blocking agent has moved before him | ||
condition = pl.col("agent_id").is_null() | ( | ||
pl.col("agent_id") == pl.col("agent_id_center") | ||
# - There isn't a higher priority agent that might make the same move |
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.
How did you find out about the necessity of adding this condition? Useful for best practice / how-to guide on how this was overlooked previously.
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.
I found that out when testing with many potential collisions.
The problem was the following:
- Consider two agents targeting the same cell:
- A mid-priority agent (higher in the agent order)
- A low-priority agent (lower in the agent order)
- The mid-priority agent has low preference for the cell
- The low-priority agent has high preference for the cell
- Without accounting for priority:
- The mid-priority agent's best moves kept getting "stolen" by higher priority agents
- This forced it to resort to lower preference target cells
- However, these lower preference cells were often already taken by lower priority agents in previous iterations
The best-practice to prevent these race conditions, is to implement a "priority" count to ensure that each action is "legal".
The downside is that the "priority" needs to be recomputed at each iteration and I think that's why is slower than Numba now. After the Ibis refactoring we can check if lazy evaluation can help mitigate the issue.
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.
Sounds messy. I hope it is possible to abstract out the move process, so that the user only has to provide the function on how the agent chooses the optimal location to move to.
Approved because my comments are questions instead of blocking requests. |
This PR introduces a Numba implementation for the loop in SugarscapePolars to optimize agent move calculations. Key points:
Implementation: Added a Numba-accelerated function to determine the best moves for agents, replacing the more complex DataFrame-based logic.
Validation: Implemented equality checks across different Polars implementations to ensure consistent results.
Performance Comparison:
Numba Considerations:
Performance Bottlenecks:
Future Optimizations: