-
Notifications
You must be signed in to change notification settings - Fork 165
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
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: | ||
|
@@ -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 | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My experience is the other way around. I found this very clear. |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.