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

Mesa implementation of Ising Model #138

Open
wants to merge 83 commits into
base: main
Choose a base branch
from

Conversation

vitorfrois
Copy link

Ising Model (https://ccl.northwestern.edu/netlogo/models/Ising, https://en.wikipedia.org/wiki/Ising_model) implementation using new Visualization of Ising Model as issued at #136.
image

agents_list = list(self.agents)
self._steps += 1000
for i in range(1000):
random_spin = self.random.choice(agents_list)
Copy link
Contributor

Choose a reason for hiding this comment

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

Having to create a new list of agents from scratch and selecting one is not ideal. I know this is a limitation of AgentSet. @EwoutH any idea to do this in a performant way via AgentSet? It seems to be a tower of abstractions, that it has slowed down the benchmark for mesa-frames as well: see projectmesa/mesa-frames#25.

Copy link
Author

Choose a reason for hiding this comment

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

thanks for the comment. it does not feel right at all haha.

Copy link
Member

Choose a reason for hiding this comment

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

Currently I don’t think there is. @vitorfrois could you open a new issue on the main Mesa repo describing this lack of functionality and linking to this PR as example?

Copy link
Member

Choose a reason for hiding this comment

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

I was going to suggest you can just do:

self.agents.select(n=1000)

But looking at our current implementation (here) that isn’t actually random.

What you can do though is:

self.agents.shuffle().select(n=1000)


What happens when the temperature slider is set very low? (This is called the "ferromagnetic" state.) Again, try this with different **Spin Up Probability** values.

Between these two very different behaviors is a transition point. On an infinite grid, the transition point can be proved to be $2 / ln (1 + sqrt 2)$, which is about 2.27. On a large enough finite toroidal grid, the transition point is near this number.
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't have means of aggregating various param sweep into a single plot of magnetization vs temperature. This is a homework for us developers to allow visualizing batch_run result easily.

Copy link
Member

Choose a reason for hiding this comment

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

@rht
Copy link
Contributor

rht commented Jul 14, 2024

This model is very helpful in demonstrating phase transition. We have Epstein civil violence, but the Ising model is the bread and butter toy model for phase transition in statistical physics. We should aim towards being able to specify this model in code concisely.

examples/ising/ising/model.py Outdated Show resolved Hide resolved
@rht
Copy link
Contributor

rht commented Jul 21, 2024

Other than the 2 comments, this PR LGTM. Once you fix them, I will merge.

EwoutH and others added 18 commits July 22, 2024 09:58
Added a main() function to bank_reserves `batch_run.py` and sugarscape_g1mt `run.py` scripts to facilitate testing and script execution.

The `main()` function encapsulates the primary script logic, allowing for easier modular testing and execution. By defining script operations within `main()`, we can directly invoke this function in testing environments without relying on command-line execution. This practice enhances code readability, maintainability, and testability, providing a clear entry point for the script's functionality.
Stop testing stable mesa builds, which can be enabled again when Mesa 3.0 is released. This allows us to start updating Mesa example to break with 2.x conventions, towards development of Mesa 3.0.

Mesa 2.x examples can be found on the mesa-2.x branch.
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.5.0 → v0.5.6](astral-sh/ruff-pre-commit@v0.5.0...v0.5.6)
- [github.com/asottile/pyupgrade: v3.16.0 → v3.17.0](asottile/pyupgrade@v3.16.0...v3.17.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This allows seeing which tests are collected, passed, etc.

See https://docs.pytest.org/en/7.1.x/how-to/output.html#verbosity
This updates all the examples to use the renamed and stabilized SolaraViz.
Resolves 4 warnings by using the AgentSet select(), shuffle() and do() functionality.

Also removes the now unused scheduler.
Don't pass the position parameter, but just use place_agent. Resolves the duplicate position warning.
Don't pass the position parameter, but just use place_agent. Resolves the duplicate position warning.
This resolves all warnings outputted by this model.

For the model step, the behavior of the old RandomActivationByType scheduler when using step(shuffle_types=True, shuffle_agents=True) is replicated. Conceptually, it can be argued that this should be modelled differently.

The verbose prints are also removed.
Don't pass the position parameter, but just use place_agent. Resolves the duplicate position warning.
Uses the  -Werror option of pytest to treat warnings as errors.

See https://docs.python.org/3/using/cmdline.html#cmdoption-W
Use model.get_agents_of_type instead of model.agents.select() to select all agents of a certain type. It should be faster, since it's a direct dictionary call.
Replace all usages of model.schedule.agents with the use of the model.agents AgentSet in all examples.
EwoutH and others added 20 commits August 30, 2024 16:05
…onality

This reinstates PR projectmesa#161 after the previous revert.
Confirm that model.steps is correctly increased to  10, and not some other value
Use pass the new rio_opener argument with gzip.open in RasterLayer.from_file(). This prevents having to do the weird /vsigzip/ stuff.

Note that urban_growth uses the rasterio opener directly, while the other two examples pass it via the mesa_geo.raster_layers.RasterLayer.open_file() method.
Fixes the population and rainfall models by passing model to RasterLayer.from_file().

> The RasterLayer creates Cells, Cells inherit from Agents, Agents need model input, but RasterLayer doesn't have access to model.
updates:
- [github.com/astral-sh/ruff-pre-commit: v0.5.6 → v0.6.3](astral-sh/ruff-pre-commit@v0.5.6...v0.6.3)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
[pre-commit.ci] auto fixes from pre-commit.com hooks

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

update
…rojectmesa#190)

The Model method `get_agents_of_type()` is replaced by the `agents_by_type` property, which directly returns the dict.

Instead of using:
```Python
model.get_agents_of_type(Wolf)
```
You should now use:
```Python
model.agents_by_type[Wolf]
```
Note that examples on the main branch work on the latest Mesa-Geo development branch, and examples Mesa-Geo 0.8.x are available on the mesa-2.x branch.
Add seeding RL examples folder (projectmesa#178)

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Also use the new, standardized order of count-do-collect.
All examples can be updated to no longer pass a unique id, nor use model.next_id.

This only fixes the examples, not the gis-examples or rl examples.

Co-authored-by: Ewout ter Hoeven <[email protected]>
Replace shuffle().do() in 18 models with the performance optimized shuffle_do() method.
…mesa#202)

This PR completes the migration from schedulers to AgentSet functionality across the mesa-examples repository for all regular (non-`gis`/-`rl`) examples. Key changes include:

- Replaced `RandomActivation`, `SimultaneousActivation`, and `RandomActivationByType` schedulers with appropriate AgentSet methods
- Updated `Model.step()` implementations to use AgentSet activation
- Removed references to `schedule.steps`, `schedule.agents`, and `schedule.agents_by_type`
- Updated agent addition/removal logic to work with AgentSets
- Adjusted data collection and visualization code to use `Model.steps` and `Model.agents`

For more details on migrating from schedulers to AgentSets, see the migration guide: https://mesa.readthedocs.io/en/latest/migration_guide.html#time-and-schedulers
@EwoutH
Copy link
Member

EwoutH commented Sep 23, 2024

@vitorfrois I still think this is awesome example. Can I help you finish the last parts of this PR?

@EwoutH
Copy link
Member

EwoutH commented Sep 23, 2024

We made some breaking changes in the latest Mesa 3.0 pre-release, but our Migration guide is now fully up to date, which might help: https://mesa.readthedocs.io/en/latest/migration_guide.html

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.

8 participants