Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Simplify Schelling code #222

Merged
merged 4 commits into from
Oct 14, 2024
Merged
Changes from all commits
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
24 changes: 8 additions & 16 deletions examples/schelling/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,21 @@ class SchellingAgent(mesa.Agent):
Schelling segregation agent
"""

def __init__(self, model, agent_type):
def __init__(self, model: mesa.Model, agent_type: int) -> None:
"""
Create a new Schelling agent.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This docstring is self explanatory for anyone knowing what __init__ does, and should be considered a superfluous documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have read several articles about superfluous documentation, but couldn't find a good reference that explains it. Maybe this SO thread does it.


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.model.grid.iter_neighbors(
def step(self) -> None:
neighbors = self.model.grid.iter_neighbors(
self.pos, moore=True, radius=self.model.radius
):
if neighbor.type == self.type:
similar += 1
)
similar = sum(1 for neighbor in neighbors if neighbor.type == self.type)

# If unhappy, move:
if similar < self.model.homophily:
Expand Down Expand Up @@ -60,10 +57,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 @@ -79,8 +72,8 @@ def __init__(
# the coordinates of a cell as well as
# its contents. (coord_iter)
for _, pos in self.grid.coord_iter():
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)
self.grid.place_agent(agent, pos)

Expand All @@ -95,5 +88,4 @@ def step(self):

self.datacollector.collect(self)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is fully correct, I find it less readable. Maybe this could also be resolved with a comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My experience is the other way around. I found this very clear.

Loading