Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into new_spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
quaquel committed Oct 14, 2024
2 parents e5c69b6 + d63ce06 commit 95f862e
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions examples/schelling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ class SchellingAgent(CellAgent):
Schelling segregation agent
"""

def __init__(self, model, agent_type):
def __init__(self, model: mesa.Model, agent_type: int) -> None:
"""
Create a new Schelling agent.
Args:
x, y: Agent initial location.
agent_type: Indicator for the agent's type (minority=1, majority=0)
"""
super().__init__(model)
self.type = agent_type

def step(self):
similar = 0
for neighbor in self.cell.get_neighborhood(radius=self.model.radius).agents:
if neighbor.type == self.type:
similar += 1
neighbors = self.cell.get_neighborhood(radius=self.model.radius).agents
similar = len([neighbor for neighbor in neighbors if neighbor.type == self.type])

# If unhappy, move:
if similar < self.model.homophily:
Expand Down Expand Up @@ -59,10 +56,6 @@ def __init__(
"""

super().__init__(seed=seed)
self.height = height
self.width = width
self.density = density
self.minority_pc = minority_pc
self.homophily = homophily
self.radius = radius

Expand All @@ -78,8 +71,8 @@ def __init__(
# the coordinates of a cell as well as
# its contents. (coord_iter)
for cell in self.grid.all_cells:
if self.random.random() < self.density:
agent_type = 1 if self.random.random() < self.minority_pc else 0
if self.random.random() < density:
agent_type = 1 if self.random.random() < minority_pc else 0
agent = SchellingAgent(self, agent_type)
agent.cell = cell

Expand All @@ -94,5 +87,4 @@ def step(self):

self.datacollector.collect(self)

if self.happy == len(self.agents):
self.running = False
self.running = self.happy != len(self.agents)

0 comments on commit 95f862e

Please sign in to comment.