mesa icon indicating copy to clipboard operation
mesa copied to clipboard

What are the functions available for OrthogonalMooreGrid?

Open Ravencar11 opened this issue 9 months ago • 2 comments

I have been trying to find documentation on the website on which functions are available for cells and agents when on an orthogonal moore grid, but there isn't any I can see, and I keep running into the issue that it doesn't support certain functions available on MultiGrid and SingleGrid.

I could really use some help with this asap (sorry haha my project is due tomorrow and I am running into this issue now). An example is that OrthogonalMooreGrid doesn't have a move_to_empty() option.

Thank you!

Ravencar11 avatar Apr 27 '25 22:04 Ravencar11

Hey @Ravencar11, thanks for reaching out.

The grids in Mesa are cell-focused, meaning agents interact based on their current cell. You can check out our examples in the main repository for more clarity.

An example is that OrthogonalMooreGrid doesn't have a move_to_empty() option.

In this case, you need to associate a cell with your agent and subclass CellAgent. Here's an example:

class CustomAgent(CellAgent):
    def __init__(self, cell, model, ...):
        super().__init__(model)
        self.cell = cell
      
        # ... (rest of your logic)

    def step(self):
        self.cell = self.model.grid.select_random_empty_cell()  # Move to a random empty cell

Then, you can assign the cells to agents. For example:

class CustomModel(Model):
    def __init__(self, seed, ...):
        super().__init__(seed=seed)
        self.grid = OrthogonalMooreGrid((width, height), random=self.random)

        # a very basic example of assigning cells to agents
        for cell in self.grid.all_cells: # to access all the cells
            CustomAgent(
                self, cell, ...(other properties to pass)
            )
        # ... (rest of your logic)

    def step(self):
        self.agents.shuffle_do("step")  # Randomly call the agents' step methods

Sahil-Chhoker avatar Apr 28 '25 04:04 Sahil-Chhoker

And the docs can be found here

quaquel avatar Apr 28 '25 06:04 quaquel