Skip to content

Add element-wise assignment to AgentSet.set (derived from value) - #3817

Merged
quaquel merged 4 commits into
mesa:mainfrom
AshNicolus:agentset-set-per-agent
Aug 28, 2026
Merged

quaquel merged 4 commits into
mesa:mainfrom
AshNicolus:agentset-set-per-agent

Conversation

@AshNicolus

@AshNicolus AshNicolus commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Implements element-wise assignment on AgentSet.set, closing #3816. Behavior is derived from the value (no flag), following the value-based rule pandas uses:

  • A list / tuple / numpy.ndarray / pandas.Series is assigned element-wise and must have the same length as the AgentSet; a length mismatch raises ValueError.
  • Any other value (a scalar, a string, or a non-sequence) is broadcast to every agent.

The value-resolution logic is extracted into a shared private helper (_resolve_per_agent_values) with a strict flag. set uses strict=True (raise on mismatch). create_agents is refactored onto the same helper with strict=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) where len(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 kept create_agents's current broadcast behavior here (via strict=False) and left making it raise as a follow-up. The helper's strict flag 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 ValueError for set. Verified locally: full suite passes (761), ruff check and format clean.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🟢 -5.1% [-5.7%, -4.6%] 🔵 -0.5% [-0.6%, -0.3%]
BoltzmannWealth large 🔵 -3.3% [-4.1%, -2.5%] 🟢 -6.5% [-8.9%, -4.5%]
Schelling small 🔵 -1.5% [-2.2%, -0.8%] 🔵 +0.7% [-0.2%, +1.6%]
Schelling large 🔵 -0.5% [-1.7%, +0.9%] 🔵 -0.3% [-5.1%, +4.3%]
WolfSheep small 🔵 +0.6% [-0.1%, +1.4%] 🔵 -3.7% [-7.3%, +0.1%]
WolfSheep large 🔵 -1.0% [-2.0%, -0.0%] 🔵 -3.9% [-5.0%, -2.8%]
SugarscapeG1mt small 🟢 -6.3% [-7.3%, -5.3%] 🔵 -3.8% [-5.3%, -2.3%]
SugarscapeG1mt large 🟢 -10.0% [-11.0%, -9.0%] 🟢 -5.3% [-7.0%, -3.7%]
BoidFlockers small 🟢 -4.3% [-4.6%, -4.0%] 🔵 -2.2% [-2.6%, -1.8%]
BoidFlockers large 🔵 -3.0% [-3.5%, -2.5%] 🔵 -2.2% [-2.5%, -2.0%]

@AshNicolus
AshNicolus force-pushed the agentset-set-per-agent branch from 8180ba3 to 2494ced Compare August 26, 2026 10:23
@AshNicolus AshNicolus changed the title Add per_agent option to AgentSet.set for element-wise assignment Add element-wise assignment to AgentSet.set (derived from value) Aug 26, 2026
@AshNicolus
AshNicolus marked this pull request as ready for review August 26, 2026 14:30
@AshNicolus

Copy link
Copy Markdown
Contributor Author

Quick heads-up on a change from what we discussed: the examples CI caught that flipping create_agents to raise on a length mismatch breaks the VirusAntibody example in mesa-examples, which passes a length-3 vector to broadcast across 20 agents. So I kept create_agents broadcasting for now (it's refactored onto the shared helper with strict=False, behavior unchanged) and scoped the raise change to set only, which is new and has no downstream users.

Making create_agents also raise is a one-line flip on the helper (strict=True), but it needs a matching mesa-examples update to land without breaking CI, so I'd do that as a separate follow-up. CI is green now.

@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -2.6% [-3.8%, -1.3%] 🔵 +1.6% [+1.4%, +1.9%]
BoltzmannWealth large 🔵 -0.8% [-1.8%, +0.1%] 🔵 -1.5% [-5.3%, +3.3%]
Schelling small 🔵 +0.9% [-0.2%, +2.0%] 🔵 +0.8% [-0.1%, +1.7%]
Schelling large 🔵 -0.8% [-1.7%, -0.0%] 🟢 -7.6% [-10.7%, -4.2%]
WolfSheep small 🔵 -2.2% [-3.0%, -1.3%] 🔵 -3.1% [-7.4%, +1.2%]
WolfSheep large 🔵 -2.2% [-4.5%, +0.1%] 🔵 -5.1% [-10.2%, +1.5%]
SugarscapeG1mt small 🔴 +4.4% [+3.2%, +5.4%] 🔵 +3.7% [+2.0%, +5.6%]
SugarscapeG1mt large 🔵 +3.9% [+0.2%, +7.7%] 🔵 +0.1% [-1.8%, +1.8%]
BoidFlockers small 🔵 +3.4% [+2.4%, +4.2%] 🔵 +2.4% [+2.1%, +2.8%]
BoidFlockers large 🔴 +4.1% [+3.4%, +4.6%] 🔵 +1.4% [+1.1%, +1.7%]

@quaquel

quaquel commented Aug 27, 2026

Copy link
Copy Markdown
Member

Making create_agents also raise is a one-line flip on the helper (strict=True), but it needs a matching mesa-examples update to land without breaking CI, so I'd do that as a separate follow-up. CI is green now.

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.

@AshNicolus

Copy link
Copy Markdown
Contributor Author

Thanks. Here's the plan I'd suggest:

  1. This PR: set raises on a length mismatch, create_agents unchanged (still broadcasts, via strict=False on the shared helper). Green and ready to merge as-is, and it closes AgentSet.set should support assigning per-agent values, mirroring get #3816.

  2. Follow-up in core: flip create_agents to strict=True so it also raises on a mismatch and the two line up. One-line change now that the helper is in place.

  3. Paired mesa-examples fix: update VirusAntibody to broadcast its length-3 vector explicitly (e.g. [vec] * n) so it works under the stricter create_agents.

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.

@quaquel

quaquel commented Aug 28, 2026

Copy link
Copy Markdown
Member

I'll merge this and you can cary on with 2 and 3.

@github-actions github-actions Bot added the testing Release notes label label Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants