Conversation
|
Performance benchmarks:
|
|
@codebyNJ Thanks for the PR. After reviewing the problem again, it appears we have two distinct issues: the Node ID mapping error (which you identified) and the layout synchronization race condition (identified by @codebreaker32). While this PR fixes the mapping logic, we also need the graceful failure handling from @codebreaker32's approach to prevent crashes during frame delays. I’d like you two to collaborate on a hybrid solution in this PR. Ideally, we should combine the accuracy of dictionary lookups with |
79e3d2b to
2cee4db
Compare
d88afd3 to
23adae6
Compare
|
Hybrid fix combining approaches from #3045 and #3065 This PR addresses two distinct issues: Non-contiguous node ID mapping - Using dictionary lookup instead of array indexing |
|
@Sahil-Chhoker I think the hybrid suggestion and collaboration is genius! I will throw out a thought that anyone can feel free to dismiss, but in the hybrid vein, instead of a |
|
@tpike3 I tried out your idea locally and also benchmarked by implementing vectorization using If you want me to make the change I am ready to make it. Vectorization ResultsThe vectorized approach (using
|
|
@codebyNJ thanks for benchmarking both approaches, can you implement the vectorized approach if it doesn't introduce the discrete |
|
@Sahil-Chhoker I have updated with vectorization technique. It does not introduce discrete |
| # Warn if significant nodes missing (likely layout issue, not race condition) | ||
| if missing_nodes and len(missing_nodes) > len(pos_dict) / 10: | ||
| sample = missing_nodes[: min(5, len(missing_nodes))] | ||
| warnings.warn( | ||
| f"Many nodes {sample}{'...' if len(missing_nodes) > 5 else ''} not found " | ||
| f"in position layout ({len(missing_nodes)}/{len(node_ids)} agents). " | ||
| f"This may indicate the network layout needs to be updated or regenerated.", | ||
| UserWarning, | ||
| stacklevel=2, | ||
| ) |
There was a problem hiding this comment.
Did you run into such a problem while testing?
There was a problem hiding this comment.
I didn't encounter this during testing of Issue #3023 specifically, but I added it as defensive handling since we're now using dictionary lookup. Without the warning, missing nodes would silently become NaN (invisible) with no indication to the user.
If you think this warning is unnecessary, I can remove it and just let missing nodes silently become NaN. Let me know your preference!


Fixes #3023. @Sahil-Chhoker This is a new PR I have raised a cleaner version.
Problem: Code used node IDs as array indices instead of dictionary keys, causing IndexError that was silently suppressed.
Solution: Replace array indexing with dictionary lookup with proper error handling.
Before: Nodes [0,1,5,10,15] → 0/5 agents visible (all lost)
After: Nodes [0,1,5,10,15] → 5/5 agents visible
Tests: 6 comprehensive unit tests added covering:
Files changed:
mesa/visualization/space_renderer.py- Fix implementationtests/visualization/test_space_renderer.py- Unit testsTo Reproduce
Explaination
Networks with non-contiguous node IDs (like
[0, 1, 5, 10, 15]) caused the old code to silently fail when using array indexing. By replacing array indexing with dictionary lookup, ensures all agents are properly visualized regardless of node ID values.