Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 29, 2024
commit 0585b999c4f9f2dee861b9fe2fbadb3773ec3491
20 changes: 14 additions & 6 deletions examples/color_patches/color_patches/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ColorCell(mesa.Agent):
"""
Represents a cell's opinion (visualized by a color)
"""

OPINIONS = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

def __init__(self, pos, unique_id, model, initial_state):
Expand Down Expand Up @@ -91,16 +92,16 @@ def __init__(self, width=20, height=20):
# for (contents, col, row) in self._grid.coord_iter():
# replaced content with _ to appease linter
for _, (row, col) in self._grid.coord_iter():

cell = ColorCell(
(row, col), row+col*row, self, ColorCell.OPINIONS[self.random.randrange(0, 16)]
(row, col),
row + col * row,
self,
ColorCell.OPINIONS[self.random.randrange(0, 16)],
)
self._grid.place_agent(cell, (row, col))

self.running = True



def step(self):
"""
Perform the model step in three stages:
Expand Down Expand Up @@ -130,8 +131,15 @@ def change_opinion(self, radius=2):
- Randomly choose an opinion from `ColorCell.OPINIONS`.
- Set each agent's `next_state` within this neighborhood to the selected opinion.
"""
x, y = self.random.randrange(self.grid.width), self.random.randrange(self.grid.height)
agents = list(self.grid.iter_neighbors((x, y), moore=True, include_center=True, radius=radius))
x, y = (
self.random.randrange(self.grid.width),
self.random.randrange(self.grid.height),
)
agents = list(
self.grid.iter_neighbors(
(x, y), moore=True, include_center=True, radius=radius
)
)
opinion = ColorCell.OPINIONS[self.random.randrange(0, 16)]

for agent in agents:
Expand Down
3 changes: 2 additions & 1 deletion examples/color_patches/color_patches/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
handles the definition of the canvas parameters and
the drawing of the model representation on the canvas
"""

import webbrowser

import mesa
Expand Down Expand Up @@ -64,4 +65,4 @@ def color_patch_draw(cell):
{"width": grid_rows, "height": grid_cols},
)

webbrowser.open('http://127.0.0.1:8521')
webbrowser.open("http://127.0.0.1:8521")
Loading