Mesa 3.0.0a4__py3-none-any.whl → 3.0.0a5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of Mesa might be problematic. Click here for more details.
- mesa/__init__.py +2 -3
- mesa/agent.py +106 -61
- mesa/batchrunner.py +15 -23
- mesa/cookiecutter-mesa/hooks/post_gen_project.py +2 -0
- mesa/cookiecutter-mesa/{{cookiecutter.snake}}/{{cookiecutter.snake}}/__init__.py +1 -0
- mesa/datacollection.py +138 -27
- mesa/experimental/UserParam.py +17 -6
- mesa/experimental/__init__.py +2 -0
- mesa/experimental/cell_space/__init__.py +7 -0
- mesa/experimental/cell_space/cell.py +61 -20
- mesa/experimental/cell_space/cell_agent.py +10 -5
- mesa/experimental/cell_space/cell_collection.py +54 -17
- mesa/experimental/cell_space/discrete_space.py +16 -5
- mesa/experimental/cell_space/grid.py +19 -8
- mesa/experimental/cell_space/network.py +9 -7
- mesa/experimental/cell_space/voronoi.py +26 -33
- mesa/experimental/components/altair.py +10 -0
- mesa/experimental/components/matplotlib.py +18 -0
- mesa/experimental/devs/__init__.py +2 -0
- mesa/experimental/devs/eventlist.py +36 -15
- mesa/experimental/devs/examples/epstein_civil_violence.py +65 -29
- mesa/experimental/devs/examples/wolf_sheep.py +38 -34
- mesa/experimental/devs/simulator.py +55 -15
- mesa/experimental/solara_viz.py +10 -19
- mesa/main.py +6 -4
- mesa/model.py +43 -45
- mesa/space.py +145 -120
- mesa/time.py +57 -67
- mesa/visualization/UserParam.py +19 -6
- mesa/visualization/__init__.py +3 -2
- mesa/visualization/components/altair.py +4 -2
- mesa/visualization/components/matplotlib.py +6 -4
- mesa/visualization/solara_viz.py +157 -83
- mesa/visualization/utils.py +3 -1
- {mesa-3.0.0a4.dist-info → mesa-3.0.0a5.dist-info}/METADATA +1 -1
- mesa-3.0.0a5.dist-info/RECORD +44 -0
- mesa-3.0.0a4.dist-info/RECORD +0 -44
- {mesa-3.0.0a4.dist-info → mesa-3.0.0a5.dist-info}/WHEEL +0 -0
- {mesa-3.0.0a4.dist-info → mesa-3.0.0a5.dist-info}/entry_points.txt +0 -0
- {mesa-3.0.0a4.dist-info → mesa-3.0.0a5.dist-info}/licenses/LICENSE +0 -0
mesa/main.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
"""main module for running mesa models with a server."""
|
|
2
|
+
|
|
1
3
|
import os
|
|
2
4
|
import sys
|
|
3
5
|
from pathlib import Path
|
|
@@ -19,13 +21,13 @@ CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]}
|
|
|
19
21
|
|
|
20
22
|
@click.group(context_settings=CONTEXT_SETTINGS)
|
|
21
23
|
def cli():
|
|
22
|
-
"Manage Mesa projects"
|
|
24
|
+
"""Manage Mesa projects."""
|
|
23
25
|
|
|
24
26
|
|
|
25
27
|
@cli.command()
|
|
26
28
|
@click.argument("project", type=PROJECT_PATH, default=".")
|
|
27
29
|
def runserver(project):
|
|
28
|
-
"""Run mesa project PROJECT
|
|
30
|
+
"""Run mesa project PROJECT.
|
|
29
31
|
|
|
30
32
|
PROJECT is the path to the directory containing `run.py`, or the current
|
|
31
33
|
directory if not specified.
|
|
@@ -45,7 +47,7 @@ def runserver(project):
|
|
|
45
47
|
"--no-input", is_flag=True, help="Do not prompt user for custom mesa model input."
|
|
46
48
|
)
|
|
47
49
|
def startproject(no_input):
|
|
48
|
-
"""Create a new mesa project"""
|
|
50
|
+
"""Create a new mesa project."""
|
|
49
51
|
args = ["cookiecutter", COOKIECUTTER_PATH]
|
|
50
52
|
if no_input:
|
|
51
53
|
args.append("--no-input")
|
|
@@ -54,7 +56,7 @@ def startproject(no_input):
|
|
|
54
56
|
|
|
55
57
|
@click.command()
|
|
56
58
|
def version():
|
|
57
|
-
"""Show the version of mesa"""
|
|
59
|
+
"""Show the version of mesa."""
|
|
58
60
|
print(f"mesa {__version__}")
|
|
59
61
|
|
|
60
62
|
|
mesa/model.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
"""
|
|
2
|
-
The model class for Mesa framework.
|
|
1
|
+
"""The model class for Mesa framework.
|
|
3
2
|
|
|
4
3
|
Core Objects: Model
|
|
5
4
|
"""
|
|
@@ -28,24 +27,8 @@ class Model:
|
|
|
28
27
|
Attributes:
|
|
29
28
|
running: A boolean indicating if the model should continue running.
|
|
30
29
|
schedule: An object to manage the order and execution of agent steps.
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
agents: An AgentSet containing all agents in the model
|
|
34
|
-
agent_types: A list of different agent types present in the model.
|
|
35
|
-
agents_by_type: A dictionary where the keys are agent types and the values are the corresponding AgentSets.
|
|
36
|
-
steps: An integer representing the number of steps the model has taken.
|
|
37
|
-
It increases automatically at the start of each step() call.
|
|
38
|
-
|
|
39
|
-
Methods:
|
|
40
|
-
get_agents_of_type: Returns an AgentSet of agents of the specified type.
|
|
41
|
-
Deprecated: Use agents_by_type[agenttype] instead.
|
|
42
|
-
run_model: Runs the model's simulation until a defined end condition is reached.
|
|
43
|
-
step: Executes a single step of the model's simulation process.
|
|
44
|
-
next_id: Generates and returns the next unique identifier for an agent.
|
|
45
|
-
reset_randomizer: Resets the model's random number generator with a new or existing seed.
|
|
46
|
-
initialize_data_collector: Sets up the data collector for the model, requiring an initialized scheduler and agents.
|
|
47
|
-
register_agent : register an agent with the model
|
|
48
|
-
deregister_agent : remove an agent from the model
|
|
30
|
+
steps: the number of times `model.step()` has been called.
|
|
31
|
+
random: a seeded random number generator.
|
|
49
32
|
|
|
50
33
|
Notes:
|
|
51
34
|
Model.agents returns the AgentSet containing all agents registered with the model. Changing
|
|
@@ -54,29 +37,30 @@ class Model:
|
|
|
54
37
|
|
|
55
38
|
"""
|
|
56
39
|
|
|
57
|
-
def
|
|
58
|
-
"""Create a new model
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
# We explicitly specify the seed here so that we know its value in
|
|
63
|
-
# advance.
|
|
64
|
-
obj._seed = random.random()
|
|
65
|
-
obj.random = random.Random(obj._seed)
|
|
66
|
-
return obj
|
|
67
|
-
|
|
68
|
-
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
|
69
|
-
"""Create a new model. Overload this method with the actual code to
|
|
70
|
-
start the model. Always start with super().__init__() to initialize the
|
|
71
|
-
model object properly.
|
|
72
|
-
"""
|
|
40
|
+
def __init__(self, *args: Any, seed: float | None = None, **kwargs: Any) -> None:
|
|
41
|
+
"""Create a new model.
|
|
42
|
+
|
|
43
|
+
Overload this method with the actual code to initialize the model. Always start with super().__init__()
|
|
44
|
+
to initialize the model object properly.
|
|
73
45
|
|
|
46
|
+
Args:
|
|
47
|
+
args: arguments to pass onto super
|
|
48
|
+
seed: the seed for the random number generator
|
|
49
|
+
kwargs: keyword arguments to pass onto super
|
|
50
|
+
"""
|
|
74
51
|
self.running = True
|
|
75
52
|
self.schedule = None
|
|
76
53
|
self.steps: int = 0
|
|
77
54
|
|
|
78
55
|
self._setup_agent_registration()
|
|
79
56
|
|
|
57
|
+
self._seed = seed
|
|
58
|
+
if self._seed is None:
|
|
59
|
+
# We explicitly specify the seed here so that we know its value in
|
|
60
|
+
# advance.
|
|
61
|
+
self._seed = random.random()
|
|
62
|
+
self.random = random.Random(self._seed)
|
|
63
|
+
|
|
80
64
|
# Wrap the user-defined step method
|
|
81
65
|
self._user_step = self.step
|
|
82
66
|
self.step = self._wrapped_step
|
|
@@ -88,7 +72,7 @@ class Model:
|
|
|
88
72
|
# Call the original user-defined step method
|
|
89
73
|
self._user_step(*args, **kwargs)
|
|
90
74
|
|
|
91
|
-
def next_id(self) -> int:
|
|
75
|
+
def next_id(self) -> int: # noqa: D102
|
|
92
76
|
warnings.warn(
|
|
93
77
|
"using model.next_id() is deprecated. Agents track their unique ID automatically",
|
|
94
78
|
DeprecationWarning,
|
|
@@ -130,7 +114,7 @@ class Model:
|
|
|
130
114
|
return self.agents_by_type[agenttype]
|
|
131
115
|
|
|
132
116
|
def _setup_agent_registration(self):
|
|
133
|
-
"""
|
|
117
|
+
"""Helper method to initialize the agent registration datastructures."""
|
|
134
118
|
self._agents = {} # the hard references to all agents in the model
|
|
135
119
|
self._agents_by_type: dict[
|
|
136
120
|
type[Agent], AgentSet
|
|
@@ -138,7 +122,7 @@ class Model:
|
|
|
138
122
|
self._all_agents = AgentSet([], self) # an agenset with all agents
|
|
139
123
|
|
|
140
124
|
def register_agent(self, agent):
|
|
141
|
-
"""Register the agent with the model
|
|
125
|
+
"""Register the agent with the model.
|
|
142
126
|
|
|
143
127
|
Args:
|
|
144
128
|
agent: The agent to register.
|
|
@@ -175,10 +159,13 @@ class Model:
|
|
|
175
159
|
self._all_agents.add(agent)
|
|
176
160
|
|
|
177
161
|
def deregister_agent(self, agent):
|
|
178
|
-
"""Deregister the agent with the model
|
|
162
|
+
"""Deregister the agent with the model.
|
|
179
163
|
|
|
180
|
-
|
|
181
|
-
|
|
164
|
+
Args:
|
|
165
|
+
agent: The agent to deregister.
|
|
166
|
+
|
|
167
|
+
Notes:
|
|
168
|
+
This method is called automatically by ``Agent.remove``
|
|
182
169
|
|
|
183
170
|
"""
|
|
184
171
|
del self._agents[agent]
|
|
@@ -186,8 +173,9 @@ class Model:
|
|
|
186
173
|
self._all_agents.remove(agent)
|
|
187
174
|
|
|
188
175
|
def run_model(self) -> None:
|
|
189
|
-
"""Run the model until the end condition is reached.
|
|
190
|
-
|
|
176
|
+
"""Run the model until the end condition is reached.
|
|
177
|
+
|
|
178
|
+
Overload as needed.
|
|
191
179
|
"""
|
|
192
180
|
while self.running:
|
|
193
181
|
self.step()
|
|
@@ -201,7 +189,6 @@ class Model:
|
|
|
201
189
|
Args:
|
|
202
190
|
seed: A new seed for the RNG; if None, reset using the current seed
|
|
203
191
|
"""
|
|
204
|
-
|
|
205
192
|
if seed is None:
|
|
206
193
|
seed = self._seed
|
|
207
194
|
self.random.seed(seed)
|
|
@@ -211,8 +198,18 @@ class Model:
|
|
|
211
198
|
self,
|
|
212
199
|
model_reporters=None,
|
|
213
200
|
agent_reporters=None,
|
|
201
|
+
agenttype_reporters=None,
|
|
214
202
|
tables=None,
|
|
215
203
|
) -> None:
|
|
204
|
+
"""Initialize the data collector for the model.
|
|
205
|
+
|
|
206
|
+
Args:
|
|
207
|
+
model_reporters: model reporters to collect
|
|
208
|
+
agent_reporters: agent reporters to collect
|
|
209
|
+
agenttype_reporters: agent type reporters to collect
|
|
210
|
+
tables: tables to collect
|
|
211
|
+
|
|
212
|
+
"""
|
|
216
213
|
if not hasattr(self, "schedule") or self.schedule is None:
|
|
217
214
|
raise RuntimeError(
|
|
218
215
|
"You must initialize the scheduler (self.schedule) before initializing the data collector."
|
|
@@ -224,6 +221,7 @@ class Model:
|
|
|
224
221
|
self.datacollector = DataCollector(
|
|
225
222
|
model_reporters=model_reporters,
|
|
226
223
|
agent_reporters=agent_reporters,
|
|
224
|
+
agenttype_reporters=agenttype_reporters,
|
|
227
225
|
tables=tables,
|
|
228
226
|
)
|
|
229
227
|
# Collect data for the first time during initialization.
|