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 3, 2024
1 parent 315129c commit 47e72ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
19 changes: 14 additions & 5 deletions examples/color_patches/color_patches/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class ColorCell(mesa.Agent):
"""
Represents a cell's opinion (visualized by a color)
"""
#test

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

def __init__(self, unique_id, model, initial_state, mutation_prob=0.1):
Expand Down Expand Up @@ -36,12 +37,18 @@ def determine_opinion(self):
self.next_state = self.random.choice(ColorCell.OPINIONS)
else:
# Стандартний процес визначення "думки" на основі сусідів
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()

# Збираємо всі зв'язані думки (якщо є кілька з однаковою частотою)
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 @@ -68,8 +75,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, mutation_prob=0.01)
self.grid.place_agent(agent, (x, y))
self.schedule.add(agent)
Expand Down
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 47e72ad

Please sign in to comment.