Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions examples/color_patches/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,17 @@ An agent's state represents its "opinion" and is shown by the color of the cell

## How to Run

To run the model interactively, run ``mesa runserver` in this directory. e.g.
To run the model interactively, run Solara in this directory. e.g.

```
$ mesa runserver
$ solara run app.py
```

Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and press Reset, then Run.
Then open your browser to [http://localhost:8765/](http://localhost:8765/) and press Reset, then Run.

## Files

* ``color_patches/model.py``: Defines the cell and model classes. The cell class governs each cell's behavior. The model class itself controls the lattice on which the cells live and interact.
* ``color_patches/server.py``: Defines an interactive visualization.
* ``run.py``: Launches an interactive visualization
* ``app.py``: Launches an interactive SolaraViz visualization.

## Further Reading

Expand Down
32 changes: 24 additions & 8 deletions examples/color_patches/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
SolaraViz,
make_space_component,
)
from mesa.visualization.user_param import (
Slider,
)

_COLORS = [
"Aqua",
Expand Down Expand Up @@ -45,23 +48,36 @@ def color_patch_draw(cell):
:return: the portrayal dictionary.
"""
if cell is None:
raise AssertionError
portrayal = {"Shape": "rect", "w": 1, "h": 1, "Filled": "true", "Layer": 0}
portrayal["x"] = cell.get_row()
portrayal["y"] = cell.get_col()
portrayal["color"] = _COLORS[cell.state]
return portrayal
return
return {
"color": _COLORS[cell.state],
"size": 100,
}


space_component = make_space_component(
color_patch_draw,
draw_grid=False,
)
Comment on lines +52 to 61
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Why are we not using the lastest visualization API here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @Sahil-Chhoker, thanks for the review! Since this PR was originally just focused on fixing the missing height/width parameters in the interface and cleaning up the outdated comments as requested by @tpike3, I kept the existing dictionary structure to keep the scope of these changes narrow.
While looking into this, I noticed that several other examples in the repository also still need to be updated to the latest AgentPortrayalStyle visualization API.
Would it be acceptable to get these immediate fixes merged in first so this example runs correctly for users? If so, I'll gladly open a new issue to track the API migration across the whole repository, assign it to myself, and submit a separate PR to update all the examples at once. I figure this will keep the commit history clean and tackle the deprecation globally.
Let me know if this works for you!

model = ColorPatches()
model_params = {
"width": Slider(
"Grid Width",
grid_rows,
5,
100,
),
"height": Slider(
"Grid Height",
grid_cols,
5,
100,
),
}

page = SolaraViz(
model,
components=[space_component],
model_params={"width": grid_rows, "height": grid_cols},
model_params=model_params,
name="Color Patches",
)
# webbrowser.open('http://127.0.0.1:8521') # TODO: make this configurable
25 changes: 2 additions & 23 deletions examples/color_patches/color_patches/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,10 @@ def __init__(self, width=20, height=20):
The agents next state is first determined before updating the grid
"""
super().__init__()
self._grid = OrthogonalMooreGrid(
self.grid = OrthogonalMooreGrid(
(width, height), torus=False, random=self.random
)

# self._grid.coord_iter()
# --> should really not return content + col + row
# -->but only col & row
# for (contents, col, row) in self._grid.coord_iter():
# replaced content with _ to appease linter
for cell in self._grid.all_cells:
for cell in self.grid.all_cells:
agent = ColorCell(self, ColorCell.OPINIONS[self.random.randrange(0, 16)])
agent.move_to(cell)

Expand All @@ -95,18 +89,3 @@ def step(self):
"""
self.agents.do("determine_opinion")
self.agents.do("assume_opinion")

@property
def grid(self):
"""
/mesa/visualization/modules/CanvasGridVisualization.py
is directly accessing Model.grid
76 def render(self, model):
77 grid_state = defaultdict(list)
---> 78 for y in range(model.grid.height):
79 for x in range(model.grid.width):
80 cell_objects = model.grid.get_cell_list_contents([(x, y)])

AttributeError: 'ColorPatches' object has no attribute 'grid'
"""
return self._grid
Loading