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

Update tutorials to use create_agents and rng.integers #2541

Merged

Conversation

DarshPareek
Copy link
Contributor

@DarshPareek DarshPareek commented Dec 9, 2024

O### Summary
Updates the tutorials to align with Mesa 3.1's recommended practices for agent creation and random number generation.

Motive

The changes make the tutorials consistent with Mesa 3.1 by:

  • Using the create_agents class method instead of individual agent creation in loops
  • Replacing random.randrange with rng.integers for position generation
  • Improving performance by generating all agent positions at once instead of one at a time

Implementation

Made consistent updates across intro_tutorial.ipynb and MoneyModel.py:

  1. Agent Creation:
    Old approach:
for _ in range(self.num_agents):
    a = MoneyAgent(self)

New approach:

agents = MoneyAgent.create_agents(model=self, n=n)
  1. Position Generation:
    Old approach:
x = self.random.randrange(self.grid.width)
y = self.random.randrange(self.grid.height)

New approach:

x = self.rng.integers(0, self.grid.width, size=(n,))
y = self.rng.integers(0, self.grid.height, size=(n,))

Usage Examples

Example of the updated model initialization:

class MoneyModel(mesa.Model):
    def __init__(self, n, width, height, seed=None):
        super().__init__(seed=seed)
        self.num_agents = n
        self.grid = mesa.space.MultiGrid(width, height, True)
        
        # Create agents all at once
        agents = MoneyAgent.create_agents(model=self, n=n)
        
        # Generate all positions at once
        x = self.rng.integers(0, self.grid.width, size=(n,))
        y = self.rng.integers(0, self.grid.height, size=(n,))
        
        # Place agents efficiently using zip
        for a, i, j in zip(agents, x, y):
            self.grid.place_agent(a, (i, j))

Closes #2537.

Copy link

github-actions bot commented Dec 9, 2024

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.8% [+0.2%, +1.3%] 🔵 +0.2% [+0.0%, +0.3%]
BoltzmannWealth large 🔵 +1.2% [+0.8%, +1.5%] 🔵 +0.3% [-0.3%, +0.9%]
Schelling small 🔵 +0.8% [+0.5%, +1.1%] 🔵 +0.3% [+0.1%, +0.4%]
Schelling large 🔵 +0.9% [+0.5%, +1.3%] 🔵 +3.6% [+2.5%, +4.8%]
WolfSheep small 🔵 +3.2% [+2.8%, +3.5%] 🔵 +0.9% [+0.7%, +1.0%]
WolfSheep large 🔴 +3.7% [+3.5%, +4.0%] 🔴 +7.0% [+5.5%, +8.5%]
BoidFlockers small 🔴 +8.0% [+7.5%, +8.6%] 🔵 +0.6% [-0.3%, +1.4%]
BoidFlockers large 🔴 +7.4% [+6.7%, +8.2%] 🔵 +0.6% [-0.0%, +1.2%]

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 for the PR!

docs/tutorials/intro_tutorial.ipynb Outdated Show resolved Hide resolved
@EwoutH
Copy link
Member

EwoutH commented Dec 9, 2024

I did assign @ashish-nagmoti to issue #2537 by the way. Next time please do check if an issue is assigned, and if it is, ask first.

@ashish-nagmoti would you like to review this PR instead?

@EwoutH EwoutH added the docs Release notes label label Dec 9, 2024
@DarshPareek
Copy link
Contributor Author

In the latest update I have changed the arguments to be more explicit in create_agents function and added

x = self.rng.integers(0, self.grid.width, size=(n,)) 
y = self.rng.integers(0, self.grid.height, size=(n,))

to make positioning of agents better

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.

I’m good, but let’s see if Jan also is.

@quaquel
Copy link
Member

quaquel commented Dec 10, 2024

I am fine with this, although I personally would prefer to move self.grid.place_agent(a, (i, j)) into the MoneyAgent.

@EwoutH
Copy link
Member

EwoutH commented Dec 10, 2024

Personally I won't bother with updating the old spaces, we will replace them soon anyways.

@DarshPareek Could you make your PR title and description a bit more clear/descriptive?

@DarshPareek DarshPareek changed the title changed create agents in tutorial Enhance Tutorial by Updating Agent Creation Logic Dec 11, 2024
@DarshPareek
Copy link
Contributor Author

Personally I won't bother with updating the old spaces, we will replace them soon anyways.

@DarshPareek Could you make your PR title and description a bit more clear/descriptive?

Thank you for the suggestion! I'll revise the PR title and description to better reflect the changes made. Please let me know if there’s anything specific you'd like included.

@@ -32,3 +32,7 @@ repos:
"--ignore-words",
".codespellignore",
]
exclude: >
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I totally missed this in earlier reviews. Why are we excluding all notebooks from Codespell?

If there’s a certain word you would like to use, you can add it to .codespellignore

Copy link
Contributor Author

Choose a reason for hiding this comment

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

okay i will change that

Copy link
Member

Choose a reason for hiding this comment

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

Could you please remove all changes to .codespellignore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If i don't add words to .codespellignore the pre-commit check fails on the branch, If I remove them is there any workaround the check?

Copy link
Member

Choose a reason for hiding this comment

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

This still needs to be cleaned up

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 cleaned it up.

Copy link
Member

Choose a reason for hiding this comment

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

Final things done in f4b7eb6.

.codespellignore Outdated Show resolved Hide resolved
@DarshPareek DarshPareek force-pushed the Update_create_agents_in_tutorials branch from 7063422 to af14f2f Compare December 14, 2024 14:27
@DarshPareek
Copy link
Contributor Author

I have cleared the outputs and removed the words from .codespellignore, let me know if any further changes are needed.

@EwoutH
Copy link
Member

EwoutH commented Dec 14, 2024

Cleaned up the final things, merging.

Thanks @DarshPareek!

@EwoutH EwoutH changed the title Enhance Tutorial by Updating Agent Creation Logic Update tutorials to use create_agents and rng.integers Dec 14, 2024
@DarshPareek
Copy link
Contributor Author

Cleanup the final things, merging.

Thanks @DarshPareek!

Thanks! I would love to contribute in future.

@EwoutH EwoutH merged commit 366b482 into projectmesa:main Dec 14, 2024
11 checks passed
@EwoutH EwoutH linked an issue Dec 18, 2024 that may be closed by this pull request
@DarshPareek DarshPareek deleted the Update_create_agents_in_tutorials branch January 13, 2025 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Release notes label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update create agents in tutorials
3 participants