Repository navigation
Handle directed graphs in Network cell and connection removal - #4090
Merged
quaquel merged 1 commit intoNov 14, 2026
Merged
Conversation
Network connects each cell to G.neighbors(), which for a DiGraph are the successors only, but remove_cell, add_connection and remove_connection assumed symmetric connections. remove_cell on a directed graph could raise halfway through and leave the space half-updated, or leave predecessors connected to the removed cell. For directed graphs, remove_cell now also disconnects every predecessor, and add_connection/remove_connection only touch the cell1 -> cell2 direction, matching the single edge in G. Undirected graphs are unchanged. Fixes mesa#4085
|
Performance benchmarks:
|
quaquel
approved these changes
Nov 14, 2026
Contributor
Author
|
@quaquel thank you |
EwoutH
pushed a commit
that referenced
this pull request
Nov 22, 2026
Adapted for 3.5.x: 3.5 has no mesa.exceptions, so removing a missing cell from a directed Network raises KeyError, like DiscreteSpace.remove_cell on 3.5. (cherry picked from commit f36fcfd) Assisted-by: Claude Opus 5.5
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.
Note
Use this template for bug fixes only. For enhancements/new features, use the feature template and get maintainer approval in an issue/discussion before opening a PR.
Pre-PR Checklist
Summary
Networkbuilt from a directed graph (nx.DiGraph) could break when cells or connections were removed:remove_cellcould raise halfway and leave the space half-updated, or leave other cells connected to the removed cell. This PR makesremove_cell,add_connectionandremove_connectionfollow the direction of the graph. Undirected networks are unchanged.Bug / Issue
Fixes #4085
Networkconnects each cell toG.neighbors(node), which for a directed graph returns only successors, so connections are one-way. ButDiscreteSpace.remove_cellassumes connections are symmetric:neighbor.disconnect(cell)raisesConnectionMissingException. By then the cell is already gone from_cells, but it is still inGand other cells still point to it.add_connectionhad the same mismatch: it connected both cells to each other but added only one directed edge toG.Implementation
All changes are in
mesa/discrete_space/network.pyand only apply whenG.is_directed(); the undirected path still calls the existingDiscreteSpacemethods.remove_cell: removes the cell from the space, disconnects every predecessor from it (usingG.predecessors()), and clears the removed cell's own connections. Then it removes the node fromGas before.add_connection: connects onlycell1 -> cell2, matching the edge added toG.remove_connection: removes onlycell1 -> cell2.add_connectionon a directed graph used to connect both ways, so it now behaves differently. I think the one-way behavior is what a directed graph means and matches how the network is built fromG, but I wanted to point it out.Testing
test_network_directed_remove_cell: directed cycle, a cell with only incoming edges, a self-loop, and removing a cell that isn't in the space.test_network_directed_connections:add_connection/remove_connectionare one-way and stay in sync withG.test_network_undirected_remove_cell_unchanged: undirected networks still disconnect both ways.mainand pass with this change; the undirected test passes on both.ruff check/ruff formatare clean and the pre-commit hooks pass.pytest tests/ -Werror -Wdefault::PendingDeprecationWarning): alltests/discrete_spacetests pass. Locally, the only failures are the same ones as on unchangedmain(Playwright browsers not installed, a stale localmesa/experimental/devsfolder, and the order-dependent logging test tracked in Importing the Boltzmann example app disables MESA log propagation, breaking caplog in later tests #3935).Additional Notes
An alternative would be to reject directed graphs in
Network.__init__. I didn't go that way because models that use aDiGraphtoday (without removing cells) would start failing, and it would need a deprecation cycle. Happy to switch if maintainers prefer that.As noted in the
DiscreteSpacedocstrings, only the caches of directly affected cells are cleared, so cached neighborhoods withradius > 1can still be stale after runtime changes. That is unchanged by this PR.I used an AI assistant to help me verify my fix is correct