Skip to content

bug: move_one_step() moves agents in wrong direction on real OrthogonalMooreGrid #197

@abhinavk0220

Description

@abhinavk0220

Describe the bug

move_one_step() in inbuilt_tools.py incorrectly uses the connections
dict on real OrthogonalMooreGrid cells to determine the target cell.

The problem is that real Mesa 4.x grid cells have a connections dict
populated with (row, col) delta keys internally, while the grid's _cells
dict uses (x, y) coordinates. When move_one_step() looks up
connections[(-1, 0)] for North, it gets the cell at (row-1, col) instead
of (x, y+1) — causing the agent to move in the wrong direction entirely.

To Reproduce

from mesa.model import Model
from mesa.agent import Agent
from mesa.discrete_space import OrthogonalMooreGrid
from mesa_llm.tools.inbuilt_tools import move_one_step

class DummyModel(Model):
    def __init__(self):
        super().__init__()

class DummyAgent(Agent):
    def step(self): pass

model = DummyModel()
model.grid = OrthogonalMooreGrid((5, 5), torus=False)

agent = DummyAgent(model=model)
cell = model.grid._cells.get((2, 2))
cell.add_agent(agent)
agent.cell = cell
agent.pos = (2, 2)

move_one_step(agent, "North")
print(agent.pos)  # prints (1, 2) — WRONG, expected (2, 3)

Expected behavior

Agent at (2, 2) moving North should end up at (2, 3) — i.e. y + 1 in
(x, y) convention which is what OrthogonalMooreGrid._cells uses.

Actual behavior

Agent ends up at (1, 2) — the connections dict uses (row, col)
convention internally, so connections[(-1, 0)] (North) returns the cell
at row-1 instead of y+1.

Root cause

OrthogonalMooreGrid cells have a connections dict populated with
(row, col) delta keys. The previous code checked connections for all
cell types without distinguishing between real Mesa cells and
SimpleNamespace dummy cells used in tests. Both have a connections
attribute but with incompatible coordinate conventions.

Fix

Check isinstance(current_cell, SimpleNamespace) to distinguish cell types:

  • SimpleNamespace dummy cells → use connections with (row, col) deltas
  • Real Mesa Cell objects → skip connections, use direction_map_xy
    with _cells lookup directly

This fix is included in PR #195.

Additional context

This bug affects all 8 cardinal and diagonal directions on real grids.
Torus wrapping is also broken as a consequence since the wrong starting
cell is used for the modulo calculation.

Also affects:

  • Boundary detection (agent appears to hit wrong boundary)
  • Occupied cell detection (wrong target cell is checked for capacity)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions