Skip to content

fix(hex_ant): fix relative imports and PropertyLayer API to make example fully runnable#470

Open
dietcoke-17 wants to merge 2 commits into
mesa:mainfrom
dietcoke-17:fix/hex-ant-property-layer
Open

fix(hex_ant): fix relative imports and PropertyLayer API to make example fully runnable#470
dietcoke-17 wants to merge 2 commits into
mesa:mainfrom
dietcoke-17:fix/hex-ant-property-layer

Conversation

@dietcoke-17

@dietcoke-17 dietcoke-17 commented Jun 16, 2026

Copy link
Copy Markdown

Summary

Closes #357 — hex_ant was completely non-functional. Running solara run app.py
crashed immediately and the model never reached the browser. This PR works through
every crash in sequence until the example fully runs.

Bug / Issue

Bug 1 — Relative imports in app.py and model.py

app.py — before
from .agent import AntState
from .model import AntForaging
removed the . before the module

why this change
Relative imports (.module) only work when Python treats the directory as a package with a known parent. When Solara executes app.py directly, __package__ is None, so there is no parent to be relative to

Result: Model loads past the import stage.

Bug 2 — Direct PropertyLayer item assignment in _init_environment
model.py — before
self.grid.pheromone_home[center] = 1.0
self.grid.home[center] = 1

model.py — after
self.grid.pheromone_home.data[center] = 1.0
self.grid.home.data[center] = 1

Why
In Mesa 3.x, PropertyLayer is a wrapper object, not a numpy array. Direct item assignment (layer[x, y] = val) is not supported. The underlying numpy array is accessed through the .data attribute, which does support index assignment.

Result: Model initialises, nest and food sources are placed,
visualization opens in browser.

Bug 3 — Wrong dynamic layer lookup in _update_pheromone_layer
model.py — before
np_layer = self.grid.property_layers[layer_name]

model.py — after
np_layer = getattr(self.grid, layer_name).data

Why:

self.grid.property_layers does not exist as an attribute. HexGrid overrides __getattr__ and routes ALL attribute access through the internal property layer dictionary — so accessing .property_layers is itself treated as a layer name lookup and fails. The correct pattern for dynamic layer access by name is getattr(self.grid, layer_name). The .data is also needed here for the same reason as Bug 2 — numpy operations like *= and boolean masking must be performed on the raw
array, not the wrapper object.

Testing

{F6415441-5EB7-41B7-B4DC-BCB88C82849E}

Additional Notes

Relation to existing PRs

Verified

Tested on Windows, Mesa 3.5.0, Python 3.14:

  • solara run app.py starts without error
  • Visualization opens at http://localhost:8765
  • Ants spawn at nest and begin foraging
  • Pheromone trails build up and evaporate each step
  • Returning ants (gold) follow home pheromone correctly
  • Food clusters deplete as ants pick up food

After Codex Suggestion
In model.py and app.py: used try/except import pattern to support both solara run app.py (direct execution) and package import via test runner and it worked

{335454A4-326F-42A9-8570-CFAF0F73B7DD}

Also CI Failing Issue
Also fixed a pre-existing lint error in rl/wolf_sheep/agents.py — two variables x, y were unpacked but never actually used anywhere. Renamed them to _x, _y to tell the linter they are intentionally unused. This was flagged by pre-commit CI and is unrelated to the hex_ant fix.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fa7a20f19e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread examples/hex_ant/model.py Outdated
@dietcoke-17 dietcoke-17 force-pushed the fix/hex-ant-property-layer branch from fa7a20f to 4ace501 Compare June 16, 2026 23:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix hex_ant: relative import error prevents example from running

1 participant