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

Conversation

rht
Copy link
Contributor

@rht rht commented Oct 13, 2024

  1. Remove unused model attributes
  2. Make similar calculation more natural language readable

With these changes and shuffle_do, the Schelling code in https://github.com/JuliaDynamics/ABMFrameworksComparison should go down from 44 to ~33, closer to Agents.jl's 28.

@rht rht force-pushed the simplify_schelling branch from e488ab1 to fc943ed Compare October 13, 2024 08:25
rht added 3 commits October 13, 2024 04:30
1. Remove unused model attributes
2. Make `similar` calculation more natural language readable
@rht rht force-pushed the simplify_schelling branch from fc943ed to 68ff95e Compare October 13, 2024 08:30
@@ -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.

Copy link
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

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

Thanks, especially the step simplification is really nice.

One comment, otherwise good. Maybe check with @quaquel if it doesn’t conflict his work.

@@ -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.

@quaquel
Copy link
Member

quaquel commented Oct 13, 2024

It does conflict with #198 but I can easily rectify that there once this is merged.

It would be good to move these changes also to the benchmarks in the main repo (is this an argument in favor of #2349? 😉 )

@rht rht merged commit d63ce06 into projectmesa:main Oct 14, 2024
3 checks passed
@rht rht deleted the simplify_schelling branch October 14, 2024 09:59
rht added a commit to rht/mesa that referenced this pull request Oct 14, 2024
quaquel added a commit to projectmesa/mesa that referenced this pull request Oct 18, 2024
* refactor: Simplify Schelling code

Ported from projectmesa/mesa-examples#222.

* Update benchmarks/Schelling/schelling.py

Co-authored-by: Ewout ter Hoeven <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Jan Kwakkel <[email protected]>
Co-authored-by: Ewout ter Hoeven <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
quaquel added a commit to quaquel/mesa that referenced this pull request Oct 18, 2024
* refactor: Simplify Schelling code

Ported from projectmesa/mesa-examples#222.

* Update benchmarks/Schelling/schelling.py

Co-authored-by: Ewout ter Hoeven <[email protected]>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Jan Kwakkel <[email protected]>
Co-authored-by: Ewout ter Hoeven <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants