Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Nov 1, 2024
1 parent 345584d commit b10d55a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
17 changes: 12 additions & 5 deletions examples/color_patches/color_patches/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,18 @@ def determine_opinion(self):
A choice is made at random in case of a tie.
The next state is stored until all cells have been polled.
"""
neighbors = self.model.grid.get_neighbors(self.pos, moore=True, include_center=False)
neighbors = self.model.grid.get_neighbors(
self.pos, moore=True, include_center=False
)
neighbors_opinion = Counter(neighbor.state for neighbor in neighbors)
polled_opinions = neighbors_opinion.most_common()

# Collect all tied opinions
tied_opinions = [opinion[0] for opinion in polled_opinions if opinion[1] == polled_opinions[0][1]]
tied_opinions = [
opinion[0]
for opinion in polled_opinions
if opinion[1] == polled_opinions[0][1]
]
self.next_state = self.random.choice(tied_opinions)

def assume_opinion(self):
Expand All @@ -56,8 +62,10 @@ def __init__(self, width=20, height=20):
self.schedule = mesa.time.RandomActivation(self)

# Create agents
for (content, x, y) in self.grid.coord_iter():
initial_state = ColorCell.OPINIONS[self.random.randrange(0, len(ColorCell.OPINIONS))]
for content, x, y in self.grid.coord_iter():
initial_state = ColorCell.OPINIONS[
self.random.randrange(0, len(ColorCell.OPINIONS))
]
agent = ColorCell(self.next_id(), self, initial_state)
self.grid.place_agent(agent, (x, y))
self.schedule.add(agent)
Expand All @@ -75,4 +83,3 @@ def step(self):
for agent in self.schedule.agents:
agent.assume_opinion()
self.schedule.step()

4 changes: 3 additions & 1 deletion examples/color_patches/color_patches/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ def color_patch_draw(cell):
portrayal = {"Shape": "rect", "w": 1, "h": 1, "Filled": "true", "Layer": 0}
# Use cell.pos to get the (x, y) coordinates
portrayal["x"], portrayal["y"] = cell.pos
portrayal["Color"] = _COLORS[cell.state] # Make sure to use cell.state for the color
portrayal["Color"] = _COLORS[
cell.state
] # Make sure to use cell.state for the color
return portrayal


Expand Down

0 comments on commit b10d55a

Please sign in to comment.