Refactor forest_fire example to store environment state in a NumPy array#375
Open
ShreyasN707 wants to merge 3 commits intomesa:mainfrom
Open
Refactor forest_fire example to store environment state in a NumPy array#375ShreyasN707 wants to merge 3 commits intomesa:mainfrom
ShreyasN707 wants to merge 3 commits intomesa:mainfrom
Conversation
Contributor
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR refactors the forest_fire example to store the environment state using a NumPy array instead of representing each grid cell as a stateful patch agent.
Previously, each grid cell was implemented as a TreeCell agent that stored its condition (Fine, On Fire, Burned Out). In this refactor, the cell state is stored in a 2D NumPy array (fire_state), while TreeCell agents are kept only as lightweight wrappers for visualization and grid positioning.
The goal is to simplify the model logic and avoid creating thousands of objects when the environment state can be represented more efficiently.
Motivation
In the original implementation, each grid cell was represented by a TreeCell agent whose primary purpose was to hold the state of the environment. For large grids this means creating thousands of agents just to store simple state values.
For example, the previous implementation stored state directly on the agent:
While this works, the agents themselves do not make decisions — they only store environment data.
This PR moves the environment state into a
NumPygrid, which:avoids unnecessary agent objects
simplifies the simulation logic
makes the example clearer for new users
aligns the example with patterns where environment state is separate from agents
Implementation
The main change is the introduction of a NumPy-based state grid inside the model.
Environment state
A NumPy array now stores the fire state of each grid cell:
This array replaces the previous
TreeCell.conditionattribute.Fire propagation logic
The fire spread logic is now handled in
model.step()using the array instead of agent state:Using a copied array ensures that updates occur synchronously, preserving the behavior of the original cellular automaton.
Data collection
The
DataCollectorwas updated to read directly from theNumPygrid:Visualization
TreeCellagents are still created so the existing visualization system can iterate over cells. However, they now act only as wrappers for location, while the actual state is read from the NumPy array:Result
The simulation behavior remains identical to the original example, but the implementation becomes:
simpler
more explicit about environment state
less dependent on large numbers of state-holding agents
The example still runs using:
and the visualization, fire propagation, and density behavior remain unchanged.
Additional Notes
TreeCellagents are retained only for visualization compatibility.The refactor focuses solely on internal model structure and does not change the user-facing interface.
The example was tested manually to confirm that fire propagation and visualization behave the same as before.