Repository navigation
Add element-wise assignment to AgentSet.set (derived from value) - #3817
Conversation
|
Performance benchmarks:
|
8180ba3 to
2494ced
Compare
|
Quick heads-up on a change from what we discussed: the examples CI caught that flipping Making |
|
Performance benchmarks:
|
Breaking mesa-examples is fine in principle. The typical work-flow is to break it here, fix it next there. I am also open to merging this, but would love to have clear plan for next steps. |
|
Thanks. Here's the plan I'd suggest:
If you'd rather not do two rounds, I can fold step 2 into this PR now and open the mesa-examples fix right after, so the break-here-fix-there happens back to back. Either sequencing works for me, just say which you prefer. |
|
I'll merge this and you can cary on with 2 and 3. |
Implements element-wise assignment on
AgentSet.set, closing #3816. Behavior is derived from the value (no flag), following the value-based rule pandas uses:list/tuple/numpy.ndarray/pandas.Seriesis assigned element-wise and must have the same length as the AgentSet; a length mismatch raisesValueError.The value-resolution logic is extracted into a shared private helper (
_resolve_per_agent_values) with astrictflag.setusesstrict=True(raise on mismatch).create_agentsis refactored onto the same helper withstrict=False, so its behavior is unchanged (a mismatched-length sequence is still broadcast).Behavior change (documented in the docstring, release-note-worthy):
set("attr", seq)wherelen(seq) == len(agentset)now assigns element-wise instead of broadcasting the sequence to every agent.On
create_agents: I first switched it to also raise on a length mismatch (per the discussion on #3816), but that breaks the VirusAntibody example in mesa-examples, which passes a length-3 vector to broadcast across 20 agents. Since aligning it needs a coordinated mesa-examples update, I keptcreate_agents's current broadcast behavior here (viastrict=False) and left making it raise as a follow-up. The helper'sstrictflag makes that a one-line change once mesa-examples is updated.Tests cover element-wise assignment (list/tuple/ndarray), the get/set round-trip, scalar and string broadcast, and the mismatch
ValueErrorforset. Verified locally: full suite passes (761), ruff check and format clean.