Skip to content

Commit

Permalink
mutations added, grid sizes changed
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysSvintsylo committed Nov 3, 2024
1 parent 1fdc310 commit 315129c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
28 changes: 20 additions & 8 deletions examples/color_patches/color_patches/model.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from collections import Counter
from random import random

import mesa


Expand All @@ -9,13 +11,14 @@ class ColorCell(mesa.Agent):
#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):
def __init__(self, unique_id, model, initial_state, mutation_prob=0.1):
"""
Create a cell with the given opinion state.
"""
super().__init__(unique_id, model)
self.state = initial_state
self.next_state = None
self.mutation_prob = mutation_prob

def determine_opinion(self):
"""
Expand All @@ -24,13 +27,22 @@ 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_opinion = Counter(neighbor.state for neighbor in neighbors)
polled_opinions = neighbors_opinion.most_common()
if random() < 0.5:
self.next_state = self.state
return

if random() < self.mutation_prob:
# Зміна кольору на випадковий незалежно від сусідів
self.next_state = self.random.choice(ColorCell.OPINIONS)
else:
# Стандартний процес визначення "думки" на основі сусідів
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]]
self.next_state = self.random.choice(tied_opinions)
# Збираємо всі зв'язані думки (якщо є кілька з однаковою частотою)
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 Down Expand Up @@ -58,7 +70,7 @@ def __init__(self, width=20, height=20):
# Create agents
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)
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: 2 additions & 2 deletions examples/color_patches/color_patches/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
]


grid_rows = 50
grid_cols = 25
grid_rows = 100
grid_cols = 100
cell_size = 10
canvas_width = grid_rows * cell_size
canvas_height = grid_cols * cell_size
Expand Down

0 comments on commit 315129c

Please sign in to comment.