Repository navigation
Make FixedAgent handle being unplaced as agreed in #3748 - #3986
Merged
Merged
Conversation
A FixedAgent can be assigned a cell once, after it is created, so being unplaced is a valid state until then. Two cases didn't handle it: - agent.cell = None while unplaced raised AttributeError. It is now a no-op that leaves the agent unplaced. - remove() on an unplaced agent deregistered it from the model first and then raised AttributeError, leaving it half removed. It now raises a clear ValueError before touching anything. Assigning None or another cell after placement still raises, as before. Fixes mesa#3748
quaquel
approved these changes
Oct 26, 2026
This was referenced Oct 27, 2026
Closed
EwoutH
pushed a commit
that referenced
this pull request
Oct 28, 2026
A FixedAgent can be assigned a cell once, after it is created, so being unplaced is a valid state until then. Two cases didn't handle it: - agent.cell = None while unplaced raised AttributeError. It is now a no-op that leaves the agent unplaced. - remove() on an unplaced agent deregistered it from the model first and then raised AttributeError, leaving it half removed. It now raises a clear ValueError before touching anything. Assigning None or another cell after placement still raises, as before. Fixes #3748 (cherry picked from commit 7f69078)
1 task done
53 of 65 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pre-PR Checklist
Summary
A
FixedAgentthat hasn't been placed in a cell yet crashed with an unclearAttributeErrorin two places. This makes it follow the rules agreed with @quaquel in #3748: assigningNonebefore placement is fine, and removing an unplaced agent is an error, raised clearly and before anything is changed.Bug / Issue
Fixes #3748. The behaviour below is from the discussion there (1, 2).
A fixed agent gets its cell once, after it's created, so it starts out unplaced:
stateDiagram-v2 direction LR [*] --> Unplaced: FixedAgent(model) Unplaced --> Unplaced: cell = None (no-op) Unplaced --> Placed: cell = some_cell Placed --> Removed: remove() Unplaced --> Unplaced: remove() raises ValueError, agent untouched Placed --> Placed: cell = None or another cell raises ValueErrorWhat changes:
agent.cell = NoneAttributeErrorremove()AttributeErrorValueErrorwith a clear message, agent untouchedcell = Noneor another cellValueErrorremove()The second row was the worst one: the agent was already gone from
model.agentsby the time the error came up, so it ended up half removed.Implementation
Two guard clauses in
mesa/discrete_space/cell_agent.py, so the normal path is unchanged:FixedCell.cellsetter: return early when assigningNoneto an unplaced agent.FixedAgent.remove(): check for a cell first and raiseValueErrorbefore callingsuper().remove().The new error reads:
I went with
ValueErrorto match the existing "Cannot move agent in FixedCell" error. Happy to switch both to aSpaceExceptionif you'd prefer that.As agreed,
model.remove_all_agents()still raises when the model has an unplaced fixed agent, now with this message instead of theAttributeError.Testing
Four tests in
tests/discrete_space/test_discrete_space.py:Nonewhile unplaced is a no-op, and the agent can still be placed afterwardsremove()on an unplaced agent raises and the agent is still inmodel.agentsremove()after a successful one raisesNoneafter placement still raises (unchanged behaviour, kept as a guard)The first three fail on
mainand pass here. The full suite passes withpytest tests/ -W error(937 passed), andpre-commitis clean. I also ran themesa-examplessuite against this branch: 5 example files useFixedAgentand all 25 tests pass, so nothing there relies on removing an unplaced fixed agent.