Skip to content

Refactor hex_snowflake example to store environment state in a NumPy array#382

Open
ShreyasN707 wants to merge 3 commits intomesa:mainfrom
ShreyasN707:hex-snowflake-array-refactor
Open

Refactor hex_snowflake example to store environment state in a NumPy array#382
ShreyasN707 wants to merge 3 commits intomesa:mainfrom
ShreyasN707:hex-snowflake-array-refactor

Conversation

@ShreyasN707
Copy link
Contributor

@ShreyasN707 ShreyasN707 commented Mar 13, 2026

This PR refactors the hex_snowflake example so that environment state is stored in a NumPy array on the model rather than inside individual patch agents.

Previously each grid position created a Cell agent that stored simulation state (state, is_considered, _next_state). These values represent environment state, not independent agent behavior.

Following the approach discussed in Issue #366, this state is now stored centrally on the model while Cell agents are retained as lightweight wrappers for visualization.

Key Changes

  1. Environment state moved to model-level arrays
self.state_grid = np.zeros((width, height), dtype=np.int8)
self.is_considered = np.zeros((width, height), dtype=bool)

state_grid[x,y] → frozen (1) or empty (0)

is_considered[x,y] → frontier cells that should be evaluated
  1. Cell agents simplified

Cell agents no longer store simulation state.

class Cell(FixedAgent):
    def __init__(self, cell, model):
        super().__init__(model)
        self.cell = cell

They now exist only to provide coordinates for visualization.

  1. Simulation logic reads from the NumPy grid
for agent in self.agents:
    x, y = agent.cell.coordinate

    if self.state_grid[x, y] == 1:
        continue

    if not self.is_considered[x, y]:
        continue

    live_neighbors = sum(
        self.state_grid[n.coordinate]
        for n in agent.cell.neighborhood
    )

    if live_neighbors == 1:
        new_state[x, y] = 1
  1. Visualization reads from model state
agent = cell.agents[0]
x, y = cell.coordinate
state = agent.model.state_grid[x, y]

return {
    "Shape": "hex",
    "Color": "black" if state else "white",
}

Outcome

The simulation behavior is unchanged, but:

  • environment state is centralized

  • patch agents no longer hold simulation state

  • the example follows the same pattern introduced in the forest_fire refactor

Screenshot from 2026-03-16 08-24-04 Screenshot from 2026-03-16 08-24-15

Does it belong?

  • No significant overlap with an existing example, or the refactor is justified (aligns with newer patterns like forest_fire)
  • The model remains well-scoped and focused on demonstrating the snowflake growth logic
  • Showcases updated Mesa patterns (environment state on model instead of agents)
  • Still demonstrates interesting ABM dynamics (pattern formation)

Is it correct and current?

  • Uses current Mesa APIs and recommended design patterns
  • Refactor preserves original behavior (no logic regression)
  • Runs and visualizes correctly
  • Deterministic with a fixed rng seed

Is it clean?

  • Removes unnecessary state from agents (cleaner separation of concerns)
  • Clear structure: environment state centralized in NumPy arrays
  • Logic is readable and better aligned with modern examples
  • No unnecessary complexity introduced
  • PR is focused (refactor only, no unrelated changes)

@EwoutH
Copy link
Member

EwoutH commented Mar 15, 2026

Thanks for the PR, looks like a cool model.

Could you:

  • Checkout the test failure
  • Add a screenshot or gif of the visualisation to the PR description
  • Request a peer-review (and maybe do one or two yourself)

@ShreyasN707
Copy link
Contributor Author

The failing tests come from the hex_ant and termites examples, both related to PropertyLayer API usage. It says
PropertyLayer no longer supports direct item assignment (layer[pos] = value), and the add_property_layer() method signature has changed.

These files appear unrelated to this PR. Should they be updated here to make the tests pass, or handled in a separate PR?

Screenshot from 2026-03-16 08-07-17 Screenshot from 2026-03-16 08-07-52

@EwoutH
Copy link
Member

EwoutH commented Mar 16, 2026

I think if you git rebase the PR the tests should pass

@ShreyasN707 ShreyasN707 force-pushed the hex-snowflake-array-refactor branch from 78a9d7d to 7c65ef3 Compare March 16, 2026 03:20
@ShreyasN707
Copy link
Contributor Author

@EwoutH Thanks for the guidance! After rebasing the PR, all the tests are now passing successfully.

Copy link

@himanksingh2024-svg himanksingh2024-svg left a comment

Choose a reason for hiding this comment

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

Peer review of PR #382:

✅ Verified the problem — current hex_snowflake fails with AttributeError: module 'mesa' has no attribute 'visualization' due to deprecated APIs
✅ Shreyas's refactor moves state from agents to NumPy array on model — aligns with Issue #366
✅ Cell class correctly simplified to lightweight visualization wrapper
✅ Code is clean and readable
✅ This is a necessary refactor to modernize the example

Good work Shreyas! The current example is broken so this fix is needed.

@EwoutH
Copy link
Member

EwoutH commented Mar 16, 2026

@himanksingh2024-svg thanks for the review. Have you actually ran the visualisation code?

@himanksingh2024-svg
Copy link

@EwoutH Honest answer — I ran the original run.py which failed with the deprecated API error, which confirmed the problem exists. I didn't checkout Shreyas's branch to test the new visualization. Let me do that properly now.

@himanksingh2024-svg
Copy link

Checked out the branch and ran solara run run.py locally. It fails with AttributeError: module 'mesa' has no attribute 'visualization' — server.py still uses mesa.visualization.CanvasHexGrid which was removed in Mesa 4.0. The array refactor in the model looks good, but the visualization needs updating before this can run. Happy to help if needed!

@ShreyasN707
Copy link
Contributor Author

Checked out the branch and ran solara run run.py locally. It fails with AttributeError: module 'mesa' has no attribute 'visualization' — server.py still uses mesa.visualization.CanvasHexGrid which was removed in Mesa 4.0. The array refactor in the model looks good, but the visualization needs updating before this can run. Happy to help if needed!

Thanks for checking !
You're right — the example still uses the old CanvasHexGrid visualization . My PR focused on the array refactor, but I agree it should run end-to-end.

@EwoutH should I update this PR to add a Solara-based app.py, or handle the visualization in a separate PR?

@EwoutH
Copy link
Member

EwoutH commented Mar 17, 2026

Both are okay, what you prefer

@ShreyasN707
Copy link
Contributor Author

I would prefer a separate PR, as this pr is to address array refactor of the example. That will also keep the PRs clean.

@EwoutH
Copy link
Member

EwoutH commented Mar 17, 2026

@himanksingh2024-svg A proper review needs to be a bit more in-depth. See for some pointers:

Copy link

@himanksingh2024-svg himanksingh2024-svg left a comment

Choose a reason for hiding this comment

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

Reviewed against #390 checklist. Array refactor in model.py is clean — state_grid and is_considered correctly moved to NumPy arrays on the model, removing per-agent state storage. portrayal.py correctly reads from model.state_grid. Ran locally: visualization fails with AttributeError: mesa.visualization — server.py still uses deprecated CanvasHexGrid. Model logic is sound; visualization fix being tracked in a separate PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants