Network with a directed graph: remove_cell raises or leaves dangling connections
Describe the bug
Network builds cell connections from G.neighbors(). For an nx.DiGraph that returns only successors, so connections are one-way. DiscreteSpace.remove_cell assumes connections are symmetric, which breaks in two ways:
- It raises halfway through. If the removed cell points to a neighbor that doesn't point back,
neighbor.disconnect(cell)raisesConnectionMissingException. By then the cell has already been popped from_cells, so the space is left half-updated: the node is still inG, and other cells still point to the removed cell. - It fails silently. Cells that point to the removed cell (its predecessors) are never disconnected, because
remove_cellonly looks at the removed cell's own outgoing neighbors. They keep a connection to a cell that's no longer in the space, and agents can move into it.
Network.add_connection has the same mismatch in the other direction: it connects both cells to each other but adds only one directed edge to G.
Expected behavior
After remove_cell, no cell in the space should still be connected to the removed cell, and the cell should be gone from both _cells and G. If directed graphs aren't meant to be supported, Network should reject them with a clear error instead.
To Reproduce
import random
import networkx as nx
from mesa import Model
from mesa.discrete_space import Cell, CellAgent, Network
# Case 1: directed cycle, remove_cell raises and leaves the space half-updated
net = Network(nx.DiGraph([(0, 1), (1, 2), (2, 0)]), random=random.Random(42))
try:
net.remove_cell(net._cells[1])
except Exception as e:
print(f"{type(e).__name__}: {e}")
print(1 in net._cells, 1 in net.G) # False True
print([c.coordinate for c in net._cells[0].connections.values()]) # [1]
# Case 2: node with only incoming edges, removal succeeds but leaves a dangling connection
model = Model(rng=42)
net = Network(nx.DiGraph([(0, 1)]), random=model.random)
net.remove_cell(net._cells[1])
print([c.coordinate for c in net._cells[0].connections.values()]) # [1]
agent = CellAgent(model)
agent.cell = net._cells[0]
agent.cell = agent.cell.neighborhood.select_random_cell()
print(agent.cell.coordinate, agent.cell.coordinate in net._cells) # 1 False
# Case 3: add_connection is symmetric on cells but adds one directed edge to G
net = Network(nx.DiGraph(), random=random.Random(42))
a, b = Cell((0,), random=net.random), Cell((1,), random=net.random)
net.add_cell(a); net.add_cell(b)
net.add_connection(a, b)
print(list(net.G.edges), a in b.connections.values()) # [((0,), (1,))] True
Output:
ConnectionMissingException: Connection between 2 and 1 does not exist.
False True
[1]
[1]
1 False
[((0,), (1,))] True
Additional context Directed networks are common in agent-based models (who follows whom, food webs, supply chains, information flow), and removing nodes at runtime is part of the dynamic-space support added in #2755. Any model that removes nodes from a directed network will hit this.
Related: #3885 fixed the same half-updated state for self-loops, but directed graphs weren't covered.
I see two possible directions and would like maintainer input before opening a PR:
- Support directed graphs: override
remove_cellinNetworkto also disconnect predecessors viaG.predecessors(), and makeadd_connection/remove_connectionrespectG.is_directed(). - Don't support them: raise a clear error in
Network.__init__whenG.is_directed()is true, and document it.
I'm happy to open a PR for whichever direction you prefer.
- Mesa: 4.0.0a0 (current
main) - Python: 3.13
according to me option 1 is better so I am raising pr which has solution implementing option1