Skip to content

Handle directed graphs in Network cell and connection removal - #4090

Merged
quaquel merged 1 commit into
mesa:mainfrom
RITVIKKAMASETTY:fix/network-directed-remove-cell
Nov 14, 2026
Merged

quaquel merged 1 commit into
mesa:mainfrom
RITVIKKAMASETTY:fix/network-directed-remove-cell

Conversation

@RITVIKKAMASETTY

Copy link
Copy Markdown
Contributor

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

  • This PR is a bug fix, not a new feature or enhancement.

Summary

Network built from a directed graph (nx.DiGraph) could break when cells or connections were removed: remove_cell could raise halfway and leave the space half-updated, or leave other cells connected to the removed cell. This PR makes remove_cell, add_connection and remove_connection follow the direction of the graph. Undirected networks are unchanged.

Bug / Issue

Fixes #4085

Network connects each cell to G.neighbors(node), which for a directed graph returns only successors, so connections are one-way. But DiscreteSpace.remove_cell assumes connections are symmetric:

  • When the removed cell points to a neighbor that doesn't point back, neighbor.disconnect(cell) raises ConnectionMissingException. By then the cell is already gone from _cells, but it is still in G and other cells still point to it.
  • Predecessors (cells pointing to the removed cell) are never disconnected, so they keep a connection to a cell that's no longer in the space, and agents can move into it.

add_connection had the same mismatch: it connected both cells to each other but added only one directed edge to G.

Implementation

All changes are in mesa/discrete_space/network.py and only apply when G.is_directed(); the undirected path still calls the existing DiscreteSpace methods.

  • remove_cell: removes the cell from the space, disconnects every predecessor from it (using G.predecessors()), and clears the removed cell's own connections. Then it removes the node from G as before.
  • add_connection: connects only cell1 -> cell2, matching the edge added to G.
  • remove_connection: removes only cell1 -> cell2.

add_connection on 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 from G, but I wanted to point it out.

Testing

  • Added 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.
  • Added test_network_directed_connections: add_connection / remove_connection are one-way and stay in sync with G.
  • Added test_network_undirected_remove_cell_unchanged: undirected networks still disconnect both ways.
  • The two directed tests fail on main and pass with this change; the undirected test passes on both.
  • The reproducer from Network with a directed graph: remove_cell raises or leaves dangling connections #4085 no longer raises or leaves dangling connections.
  • ruff check / ruff format are clean and the pre-commit hooks pass.
  • Full suite (pytest tests/ -Werror -Wdefault::PendingDeprecationWarning): all tests/discrete_space tests pass. Locally, the only failures are the same ones as on unchanged main (Playwright browsers not installed, a stale local mesa/experimental/devs folder, 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 a DiGraph today (without removing cells) would start failing, and it would need a deprecation cycle. Happy to switch if maintainers prefer that.

As noted in the DiscreteSpace docstrings, only the caches of directly affected cells are cleared, so cached neighborhoods with radius > 1 can 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

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
@github-actions github-actions Bot added the testing Release notes label label Nov 14, 2026
@github-actions

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -1.5% [-2.0%, -0.8%] 🔵 +2.6% [+2.4%, +2.9%]
BoltzmannWealth large 🔴 +5.2% [+3.5%, +6.9%] 🔴 +5.6% [+4.1%, +7.0%]
Schelling small 🔵 +0.9% [+0.1%, +1.6%] 🔵 -0.5% [-1.3%, +0.2%]
Schelling large 🔵 +0.7% [+0.1%, +1.3%] 🔵 +2.4% [+1.4%, +3.4%]
WolfSheep small 🔵 +2.0% [+1.4%, +2.5%] 🔵 +3.6% [+0.3%, +7.2%]
WolfSheep large 🔵 +1.4% [+0.6%, +2.1%] 🔴 +5.3% [+3.6%, +7.4%]
SugarscapeG1mt small 🔵 +2.5% [+1.6%, +3.5%] 🔵 +3.5% [+1.4%, +5.5%]
SugarscapeG1mt large 🔵 +0.3% [-0.9%, +1.6%] 🔵 +0.9% [-0.5%, +2.5%]
BoidFlockers small 🔵 -0.1% [-0.5%, +0.3%] 🔵 +0.8% [+0.3%, +1.2%]
BoidFlockers large 🔵 +0.4% [-0.0%, +0.8%] 🔵 +0.5% [+0.3%, +0.7%]
MultiLevelAllianceModel small 🔵 +0.2% [-0.7%, +1.2%] 🔵 +0.6% [+0.2%, +0.9%]
MultiLevelAllianceModel large 🔵 +0.1% [-1.1%, +1.2%] 🔵 +1.6% [-0.6%, +3.7%]

@quaquel
quaquel merged commit f36fcfd into mesa:main Nov 14, 2026
17 checks passed
@quaquel quaquel added the bug Release notes label label Nov 14, 2026
@RITVIKKAMASETTY

Copy link
Copy Markdown
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Network with a directed graph: remove_cell raises or leaves dangling connections

2 participants