Skip to content

Commit 2b5e822

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents 1ba465d + 002d3f4 commit 2b5e822

File tree

15 files changed

+545
-10
lines changed

15 files changed

+545
-10
lines changed

.github/workflows/build_lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ jobs:
7777
- name: Install uv
7878
run: pip install uv
7979
- name: Install Mesa
80-
run: uv pip install --system .[dev]
80+
run: uv pip install --system .[examples]
8181
- name: Checkout mesa-examples
8282
uses: actions/checkout@v4
8383
with:
@@ -86,4 +86,4 @@ jobs:
8686
- name: Test examples
8787
run: |
8888
cd mesa-examples
89-
pytest -rA -Werror test_examples.py
89+
pytest -rA -Werror -Wdefault::FutureWarning test_examples.py

CONTRIBUTING.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,40 @@ discuss via [Matrix] OR via [an issue].
3636
- Describe the change w/ ticket number(s) that the code fixes.
3737
- Format your commit message as per [Tim Pope's guideline].
3838

39+
## I have no idea where to start
40+
That's fine! Here's a rough outline where you could start, depending on your experience:
41+
42+
### I'm a modeller (but not an experienced developer)
43+
You already know how to build Mesa models (if not skip below), and probably have found things Mesa can't do (elegantly). You want to improve that. Awesome!
44+
45+
First step is to install some proper tools, if you haven't already.
46+
- A good IDE helps for code development, testing and formatting. [PyCharm](https://www.jetbrains.com/pycharm/) or [VSCode](https://code.visualstudio.com/) for example.
47+
- Dive into Git and GitHub. Watch some videos, this takes some time to click. [GitHub Desktop](https://desktop.github.com/) is great.
48+
- [`https://github.dev/projectmesa/mesa`](https://github.dev/projectmesa/mesa) is great for small changes (to docs).
49+
50+
Learn the tools, talk to us about what you want to change, and open a small PR. Or update an [example model](https://github.com/projectmesa/mesa-examples) (check open [issues](https://github.com/projectmesa/mesa-examples/issues))!
51+
52+
### I'm a developer (but not a modeller)
53+
Awesome! You have the basics of open-source software development (if not check above), but not much modelling experience.
54+
55+
First step is to start thinking like a modeller. To understand the fine details about our library and contribute meaningfully, get some modelling experience:
56+
- Go though our [Introductory Tutorial](https://mesa.readthedocs.io/en/latest/tutorials/intro_tutorial.html) and [Visualization Tutorial](https://mesa.readthedocs.io/en/latest/tutorials/visualization_tutorial.html). While going through them, dive into the source code to really see what everything does.
57+
- Follow an ABM course (if possible). They might be a bit outdated programming language wise, but conceptual they're sound.
58+
- This MOOC on ABM concepts: [Agent Based Modeling](https://ocw.tudelft.nl/course-lectures/agent-based-modeling/)
59+
- This MOOC on practical ABM modelling: [Agent-Based Models with Python: An Introduction to Mesa](https://www.complexityexplorer.org/courses/172-agent-based-models-with-python-an-introduction-to-mesa)
60+
- Go though multiple of our [examples](https://github.com/projectmesa/mesa-examples). Play with them, modify things and get a feel for Mesa and ABMs.
61+
- Check our open [issues](https://github.com/projectmesa/mesa-examples/issues) for the examples.
62+
- If you see anything you want to improve, feel free to open a (small) PR!
63+
- If you have a feel for Mesa, check our [discussions](https://github.com/projectmesa/mesa/discussions) and [issues](https://github.com/projectmesa/mesa/issues).
64+
- Also go thought our [release notes](https://github.com/projectmesa/mesa/releases) to see what we recently have been working on, and see some examples of successful PRs.
65+
- Once you found or thought of a nice idea, comment on the issue/discussion (or open a new one) and get to work!
66+
67+
### I'm both
68+
That's great! You can just start working on things, reach out to us. Skim to the list above if you feel you're missing anything. Start small but don't be afraid to dream big!
69+
70+
### I'm neither
71+
Start with creating your own models, for fun. Once you have some experience, move to the topics above.
72+
3973
## Testing and Code Standards
4074

4175
```{image} https://codecov.io/gh/projectmesa/mesa/branch/main/graph/badge.svg

HISTORY.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,54 @@
11
---
22
title: Release History
33
---
4+
# 3.0.0a3 (2024-08-30)
5+
## Highlights
6+
Developments toward Mesa 3.0 are steaming ahead, and our fourth alpha release is packed with features and updates - only 8 days after our third.
7+
8+
Mesa 3.0.0a3 contains one breaking change: We now automatically increase the `steps` counter by one at the beginning of each `Model.steps()` call. That means increasing `steps` by hand isn't necessary anymore.
9+
10+
The big new features is the experimental Voronoi grid that @vitorfrois implemented in #2084. It allows creating cells in a [Voronoi](https://en.wikipedia.org/wiki/Voronoi_diagram) layout as part of the experimental cell space. An example using it to model Cholera spread can be [found here](https://github.com/projectmesa/mesa-examples/pull/118).
11+
12+
The AgentSet got a lot of love with two brand new methods: `.groupby()` to split in groups (#2220) and `.set()` to easily assign variables to all agents in that set (#2254). The `select()` method is improved by allowing to select at most a fraction of the agents (#2253), and we split the `do()` method in `do()` and `map()` to make a distinction between the return types (#2237).
13+
14+
Furthermore, we improved the performance of accessing `Model.agents`, squashed a bug in SolaraViz, started testing on Python 3.13 and added a new benchmark model.
15+
16+
Our example models also got more love: We removed the `RandomActivation` scheduler in 14 models and removed SimultaneousActivation in 3 models ([examples#183](https://github.com/projectmesa/mesa-examples/pull/183)). They now use the automatic step increase and AgentSet functionality. We started testing our GIS model in CI ([examples#171](https://github.com/projectmesa/mesa-examples/pull/171)) and resolved a lot of bugs in them ([examples#172](https://github.com/projectmesa/mesa-examples/issues/172), help appreciated!).
17+
18+
Finally, we have two brand new examples: An Ant Colony Optimization model using an Ant System approach to the Traveling Salesman problem, a Mesa NetworkGrid, and a custom visualisation with SolaraViz ([examples#157](https://github.com/projectmesa/mesa-examples/pull/157) by @zjost). The first example using the `PropertyLayer` was added, a very fast implementation of Conway's Game of Life ([examples#182](https://github.com/projectmesa/mesa-examples/pull/182)).
19+
20+
To help the transition to Mesa 3.0, we started writing a [migration guide](https://mesa.readthedocs.io/en/latest/migration_guide.html). Progress is tracked in #2233, feedback and help is appreciated! Finally, we also added a new section to our [contributor guide](https://github.com/projectmesa/mesa/blob/main/CONTRIBUTING.md#i-have-no-idea-where-to-start) to get new contributors up to speed.
21+
22+
This pre-release can be installed as always with `pip install --pre mesa`
23+
24+
## What's Changed
25+
### ⚠️ Breaking changes
26+
* model: Automatically increase `steps` counter by @EwoutH in https://github.com/projectmesa/mesa/pull/2223
27+
### 🧪 Experimental features
28+
* Voronoi Tessellation based Discrete Space by @vitorfrois in https://github.com/projectmesa/mesa/pull/2084
29+
### 🎉 New features added
30+
* Add AgentSet.groupby by @quaquel in https://github.com/projectmesa/mesa/pull/2220
31+
* AgentSet: Add `set` method by @EwoutH in https://github.com/projectmesa/mesa/pull/2254
32+
### 🛠 Enhancements made
33+
* Split AgentSet into map and do to separate return types by @quaquel in https://github.com/projectmesa/mesa/pull/2237
34+
* Performance enhancements for Model.agents by @quaquel in https://github.com/projectmesa/mesa/pull/2251
35+
* AgentSet: Allow selecting a fraction of agents in the AgentSet by @EwoutH in https://github.com/projectmesa/mesa/pull/2253
36+
### 🐛 Bugs fixed
37+
* SolaraViz: Reset components when params are changed by @rht in https://github.com/projectmesa/mesa/pull/2240
38+
### 📜 Documentation improvements
39+
* Contribution: Add "I have no idea where to start" section by @EwoutH in https://github.com/projectmesa/mesa/pull/2258
40+
* Write initial Mesa Migration guide by @EwoutH in https://github.com/projectmesa/mesa/pull/2257
41+
### 🔧 Maintenance
42+
* CI: Add test job for Python 3.13 by @EwoutH in https://github.com/projectmesa/mesa/pull/2173
43+
* Add pull request templates by @EwoutH in https://github.com/projectmesa/mesa/pull/2217
44+
* benchmarks: Add BoltzmannWealth model by @EwoutH in https://github.com/projectmesa/mesa/pull/2252
45+
* CI: Add optional dependency for examples by @EwoutH in https://github.com/projectmesa/mesa/pull/2261
46+
47+
## New Contributors
48+
* @vitorfrois made their first contribution in https://github.com/projectmesa/mesa/pull/2084
49+
50+
**Full Changelog**: https://github.com/projectmesa/mesa/compare/v3.0.0a2...v3.0.0a3
51+
452
# 3.0.0a2 (2024-08-21)
553
## Highlights
654
In Mesa 3.0 alpha 2 (`v3.0.0a2`) we've done more clean-up in preparation for Mesa 3.0. We now [require](https://github.com/projectmesa/mesa/pull/2218) `super().__init__()` to run on initializing a Mesa model subclass, `Model.agents` is now fully reserved for the Model's internal AgentSet and we fixed a bug in our Solara space_drawer.

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,18 @@ Or any other (development) branch on this repo or your own fork:
5252
pip install -U -e git+https://github.com/YOUR_FORK/mesa@YOUR_BRANCH#egg=mesa
5353
```
5454

55+
## Resources
5556
For resources or help on using Mesa, check out the following:
5657

5758
- [Intro to Mesa Tutorial](http://mesa.readthedocs.org/en/stable/tutorials/intro_tutorial.html) (An introductory model, the Boltzmann
5859
Wealth Model, for beginners or those new to Mesa.)
60+
- [Visualization Tutorial](https://mesa.readthedocs.io/en/stable/tutorials/visualization_tutorial.html) (An introduction into our Solara visualization)
5961
- [Complexity Explorer Tutorial](https://www.complexityexplorer.org/courses/172-agent-based-models-with-python-an-introduction-to-mesa) (An advanced-beginner model,
6062
SugarScape with Traders, with instructional videos)
6163
- [Mesa Examples](https://github.com/projectmesa/mesa-examples/tree/main/examples) (A repository of seminal ABMs using Mesa and
6264
examples of employing specific Mesa Features)
6365
- [Docs](http://mesa.readthedocs.org/) (Mesa's documentation, API and useful snippets)
66+
- [Development version docs](https://mesa.readthedocs.io/en/latest/) (the latest version docs if you're using a pre-release Mesa version)
6467
- [Discussions](https://github.com/projectmesa/mesa/discussions) (GitHub threaded discussions about Mesa)
6568
- [Matrix Chat](https://matrix.to/#/#project-mesa:matrix.org) (Chat Forum via Matrix to talk about Mesa)
6669

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ ABM features users have shared that you may want to use in your model
8181
Mesa Overview <overview>
8282
tutorials/intro_tutorial
8383
tutorials/visualization_tutorial
84+
Migration guide <migration_guide>
8485
Best Practices <best-practices>
8586
How-to Guide <howto>
8687
API Documentation <apis/api_main>

docs/migration_guide.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Mesa Migration guide
2+
This guide contains breaking changes between major Mesa versions and how to resolve them.
3+
4+
Non-breaking changes aren't included, for those see our [Release history](https://github.com/projectmesa/mesa/releases).
5+
6+
## Mesa 3.0
7+
<!-- TODO small introduction-->
8+
9+
_This guide is a work in progress. The development of it is tracked in [Issue #2233](https://github.com/projectmesa/mesa/issues/2233)._
10+
11+
### Reserved and private variables
12+
<!-- TODO: Update this section based on https://github.com/projectmesa/mesa/discussions/2230 -->
13+
14+
#### Reserved variables
15+
Currently, we have reserved the following variables:
16+
- Model: `agents`, `current_id`, `random`, `running`, `steps`, `time`.
17+
- Agent: `unique_id`, `model`.
18+
19+
You can use (read) any reserved variable, but Mesa may update them automatically and rely on them, so modify/update at your own risk.
20+
#### Private variables
21+
Any variables starting with an underscore (`_`) are considered private and for Mesa's internal use. We might use any of those. Modifying or overwriting any private variable is at your own risk.
22+
23+
- Ref: [Discussion #2230](https://github.com/projectmesa/mesa/discussions/2230), [PR #2225](https://github.com/projectmesa/mesa/pull/2225)
24+
25+
26+
### Removal of `mesa.flat` namespace
27+
The `mesa.flat` namespace is removed. Use the full namespace for your imports.
28+
29+
- Ref: [PR #2091](https://github.com/projectmesa/mesa/pull/2091)
30+
31+
32+
### Automatic assignment of `unique_id` to Agents
33+
<!-- TODO -->
34+
35+
- Ref: [PR #2226](https://github.com/projectmesa/mesa/pull/2226)
36+
37+
38+
### AgentSet and `Model.agents`
39+
#### AgentSet
40+
<!-- TODO -->
41+
42+
#### `Model.agents`
43+
<!-- TODO -->
44+
45+
46+
### Time and schedulers
47+
<!-- TODO general explanation-->
48+
49+
#### Automatic increase of the `steps` counter
50+
The `steps` counter is now automatically increased. With each call to `Model.steps()` it's increased by 1, at the beginning of the step.
51+
52+
You can access it by `Model.steps`, and it's internally in the datacollector, batchrunner and the visualisation.
53+
54+
- Ref: [PR #2223](https://github.com/projectmesa/mesa/pull/2223), Mesa-examples [PR #161](https://github.com/projectmesa/mesa-examples/pull/161)
55+
56+
#### Removal of `Model._time` and rename `._steps`
57+
- `Model._time` is removed. You can define your own time variable if needed.
58+
- `Model._steps` steps is renamed to `Model.steps`.
59+
60+
#### Removal of `Model._advance_time()`
61+
- The `Model._advance_time()` method is removed. This now happens automatically.
62+
63+
<!-- TODO deprecate all schedulers? -->
64+
65+
66+
### Visualisation
67+
<!-- TODO -->

mesa/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
]
2525

2626
__title__ = "mesa"
27-
__version__ = "3.0.0a2"
27+
__version__ = "3.0.0a3"
2828
__license__ = "Apache 2.0"
2929
_this_year = datetime.datetime.now(tz=datetime.timezone.utc).date().year
3030
__copyright__ = f"Copyright {_this_year} Project Mesa Team"

mesa/agent.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,39 +114,58 @@ def __contains__(self, agent: Agent) -> bool:
114114
def select(
115115
self,
116116
filter_func: Callable[[Agent], bool] | None = None,
117-
n: int = 0,
117+
at_most: int | float = float("inf"),
118118
inplace: bool = False,
119119
agent_type: type[Agent] | None = None,
120+
n: int | None = None,
120121
) -> AgentSet:
121122
"""
122123
Select a subset of agents from the AgentSet based on a filter function and/or quantity limit.
123124
124125
Args:
125126
filter_func (Callable[[Agent], bool], optional): A function that takes an Agent and returns True if the
126127
agent should be included in the result. Defaults to None, meaning no filtering is applied.
127-
n (int, optional): The number of agents to select. If 0, all matching agents are selected. Defaults to 0.
128+
at_most (int | float, optional): The maximum amount of agents to select. Defaults to infinity.
129+
- If an integer, at most the first number of matching agents are selected.
130+
- If a float between 0 and 1, at most that fraction of original the agents are selected.
128131
inplace (bool, optional): If True, modifies the current AgentSet; otherwise, returns a new AgentSet. Defaults to False.
129132
agent_type (type[Agent], optional): The class type of the agents to select. Defaults to None, meaning no type filtering is applied.
130133
131134
Returns:
132135
AgentSet: A new AgentSet containing the selected agents, unless inplace is True, in which case the current AgentSet is updated.
136+
137+
Notes:
138+
- at_most just return the first n or fraction of agents. To take a random sample, shuffle() beforehand.
139+
- at_most is an upper limit. When specifying other criteria, the number of agents returned can be smaller.
133140
"""
141+
if n is not None:
142+
warnings.warn(
143+
"The parameter 'n' is deprecated. Use 'at_most' instead.",
144+
DeprecationWarning,
145+
stacklevel=2,
146+
)
147+
at_most = n
134148

135-
if filter_func is None and agent_type is None and n == 0:
149+
inf = float("inf")
150+
if filter_func is None and agent_type is None and at_most == inf:
136151
return self if inplace else copy.copy(self)
137152

138-
def agent_generator(filter_func=None, agent_type=None, n=0):
153+
# Check if at_most is of type float
154+
if at_most <= 1.0 and isinstance(at_most, float):
155+
at_most = int(len(self) * at_most) # Note that it rounds down (floor)
156+
157+
def agent_generator(filter_func, agent_type, at_most):
139158
count = 0
140159
for agent in self:
160+
if count >= at_most:
161+
break
141162
if (not filter_func or filter_func(agent)) and (
142163
not agent_type or isinstance(agent, agent_type)
143164
):
144165
yield agent
145166
count += 1
146-
if 0 < n <= count:
147-
break
148167

149-
agents = agent_generator(filter_func, agent_type, n)
168+
agents = agent_generator(filter_func, agent_type, at_most)
150169

151170
return AgentSet(agents, self.model) if not inplace else self._update(agents)
152171

mesa/experimental/cell_space/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
OrthogonalVonNeumannGrid,
1010
)
1111
from mesa.experimental.cell_space.network import Network
12+
from mesa.experimental.cell_space.voronoi import VoronoiGrid
1213

1314
__all__ = [
1415
"CellCollection",
@@ -20,4 +21,5 @@
2021
"OrthogonalMooreGrid",
2122
"OrthogonalVonNeumannGrid",
2223
"Network",
24+
"VoronoiGrid",
2325
]

0 commit comments

Comments
 (0)