Mesa 2.2.3__py3-none-any.whl → 2.2.4__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 CHANGED
@@ -25,7 +25,7 @@ __all__ = [
25
25
  ]
26
26
 
27
27
  __title__ = "mesa"
28
- __version__ = "2.2.3"
28
+ __version__ = "2.2.4"
29
29
  __license__ = "Apache 2.0"
30
30
  _this_year = datetime.datetime.now(tz=datetime.timezone.utc).date().year
31
31
  __copyright__ = f"Copyright {_this_year} Project Mesa Team"
mesa/agent.py CHANGED
@@ -54,6 +54,7 @@ class Agent:
54
54
  except AttributeError:
55
55
  # model super has not been called
56
56
  self.model.agents_ = defaultdict(dict)
57
+ self.model.agents_[type(self)][self] = None
57
58
  self.model.agentset_experimental_warning_given = False
58
59
 
59
60
  warnings.warn(
mesa/datacollection.py CHANGED
@@ -14,8 +14,7 @@ name.
14
14
 
15
15
  When the collect() method is called, each model-level function is called, with
16
16
  the model as the argument, and the results associated with the relevant
17
- variable. Then the agent-level functions are called on each agent in the model
18
- scheduler.
17
+ variable. Then the agent-level functions are called on each agent.
19
18
 
20
19
  Additionally, other objects can write directly to tables by passing in an
21
20
  appropriate dictionary object for a table row.
@@ -30,8 +29,7 @@ The DataCollector then stores the data it collects in dictionaries:
30
29
  Finally, DataCollector can create a pandas DataFrame from each collection.
31
30
 
32
31
  The default DataCollector here makes several assumptions:
33
- * The model has a schedule object called 'schedule'
34
- * The schedule has an agent list called agents
32
+ * The model has an agent list called agents
35
33
  * For collecting agent-level variables, agents must have a unique_id
36
34
  """
37
35
  import contextlib
@@ -67,7 +65,7 @@ class DataCollector:
67
65
 
68
66
  Model reporters can take four types of arguments:
69
67
  1. Lambda function:
70
- {"agent_count": lambda m: m.schedule.get_agent_count()}
68
+ {"agent_count": lambda m: len(m.agents)}
71
69
  2. Method of a class/instance:
72
70
  {"agent_count": self.get_agent_count} # self here is a class instance
73
71
  {"agent_count": Model.get_agent_count} # Model here is a class
@@ -180,11 +178,14 @@ class DataCollector:
180
178
  rep_funcs = self.agent_reporters.values()
181
179
 
182
180
  def get_reports(agent):
183
- _prefix = (agent.model.schedule.steps, agent.unique_id)
181
+ _prefix = (agent.model._steps, agent.unique_id)
184
182
  reports = tuple(rep(agent) for rep in rep_funcs)
185
183
  return _prefix + reports
186
184
 
187
- agent_records = map(get_reports, model.schedule.agents)
185
+ agent_records = map(
186
+ get_reports,
187
+ model.schedule.agents if hasattr(model, "schedule") else model.agents,
188
+ )
188
189
  return agent_records
189
190
 
190
191
  def collect(self, model):
@@ -207,7 +208,7 @@ class DataCollector:
207
208
 
208
209
  if self.agent_reporters:
209
210
  agent_records = self._record_agents(model)
210
- self._agent_records[model.schedule.steps] = list(agent_records)
211
+ self._agent_records[model._steps] = list(agent_records)
211
212
 
212
213
  def add_table_row(self, table_name, row, ignore_missing=False):
213
214
  """Add a row dictionary to a specific table.
@@ -178,7 +178,7 @@ def ModelController(model, play_interval, current_step, reset_counter):
178
178
  def do_step():
179
179
  model.step()
180
180
  previous_step.value = current_step.value
181
- current_step.value += 1
181
+ current_step.value = model._steps
182
182
 
183
183
  def do_play():
184
184
  model.running = True
mesa/model.py CHANGED
@@ -13,11 +13,13 @@ import warnings
13
13
  from collections import defaultdict
14
14
 
15
15
  # mypy
16
- from typing import Any
16
+ from typing import Any, Union
17
17
 
18
18
  from mesa.agent import Agent, AgentSet
19
19
  from mesa.datacollection import DataCollector
20
20
 
21
+ TimeT = Union[float, int]
22
+
21
23
 
22
24
  class Model:
23
25
  """Base class for models in the Mesa ABM library.
@@ -68,6 +70,9 @@ class Model:
68
70
  self.current_id = 0
69
71
  self.agents_: defaultdict[type, dict] = defaultdict(dict)
70
72
 
73
+ self._steps: int = 0
74
+ self._time: TimeT = 0 # the model's clock
75
+
71
76
  # Warning flags for current experimental features. These make sure a warning is only printed once per model.
72
77
  self.agentset_experimental_warning_given = False
73
78
 
@@ -112,6 +117,11 @@ class Model:
112
117
  def step(self) -> None:
113
118
  """A single step. Fill in here."""
114
119
 
120
+ def _advance_time(self, deltat: TimeT = 1):
121
+ """Increment the model's steps counter and clock."""
122
+ self._steps += 1
123
+ self._time += deltat
124
+
115
125
  def next_id(self) -> int:
116
126
  """Return the next unique ID for agents, increment current_id"""
117
127
  self.current_id += 1
mesa/time.py CHANGED
@@ -73,6 +73,8 @@ class BaseScheduler:
73
73
  self.model = model
74
74
  self.steps = 0
75
75
  self.time: TimeT = 0
76
+ self._original_step = self.step
77
+ self.step = self._wrapped_step
76
78
 
77
79
  if agents is None:
78
80
  agents = []
@@ -115,6 +117,11 @@ class BaseScheduler:
115
117
  self.steps += 1
116
118
  self.time += 1
117
119
 
120
+ def _wrapped_step(self):
121
+ """Wrapper for the step method to include time and step updating."""
122
+ self._original_step()
123
+ self.model._advance_time()
124
+
118
125
  def get_agent_count(self) -> int:
119
126
  """Returns the current number of agents in the queue."""
120
127
  return len(self._agents)
@@ -143,8 +150,8 @@ class BaseScheduler:
143
150
 
144
151
  def do_each(self, method, shuffle=False):
145
152
  if shuffle:
146
- self.agents.shuffle(inplace=True)
147
- self.agents.do(method)
153
+ self._agents.shuffle(inplace=True)
154
+ self._agents.do(method)
148
155
 
149
156
 
150
157
  class RandomActivation(BaseScheduler):
@@ -299,7 +306,7 @@ class RandomActivationByType(BaseScheduler):
299
306
 
300
307
  agentsbytype = defaultdict(dict)
301
308
  for k, v in self._agents_by_type.items():
302
- agentsbytype[k] = {agent: agent.unique_id for agent in v}
309
+ agentsbytype[k] = {agent.unique_id: agent for agent in v}
303
310
 
304
311
  return agentsbytype
305
312
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: Mesa
3
- Version: 2.2.3
3
+ Version: 2.2.4
4
4
  Summary: Agent-based modeling (ABM) in Python
5
5
  Project-URL: homepage, https://github.com/projectmesa/mesa
6
6
  Project-URL: repository, https://github.com/projectmesa/mesa
@@ -1,11 +1,11 @@
1
- mesa/__init__.py,sha256=Yc2zjRJrWikG5FSdvxPgv_Zw-yZarIuveZ8NUNy_N0w,679
2
- mesa/agent.py,sha256=tyRUsZTRGEiv8GLEZJ77eH75N0OYyy8DQ3eBFqgRyK8,13047
1
+ mesa/__init__.py,sha256=1rj0Zvd80Sk-TWYndb9bpGNn5ZuBQqGa2SQ46mDiw0A,679
2
+ mesa/agent.py,sha256=Dh7FikTOr36fjQJ3w59h5reEIJWevhtZvCSW-adk8qk,13103
3
3
  mesa/batchrunner.py,sha256=2A1_FbFlSCkDm8xfv1ZamFiBE4VYce8sKP5SR_CledE,6087
4
- mesa/datacollection.py,sha256=uyPLY5LYNH2lfDvPsar3-WtsgoIMYdaqB9k_889PhAQ,11169
4
+ mesa/datacollection.py,sha256=4WGJYc2jAvdiBi-e-uEPp5G100KlVambym9a4ZAxsuU,11139
5
5
  mesa/main.py,sha256=_7y918D0EEHJBLgiV7rqVNWvCyzjamHUSQ873OeEGf8,1468
6
- mesa/model.py,sha256=sH0rJEe5x0bLanbyeNCsqQDD7bU-xtxTs3B4qUqwvsg,5629
6
+ mesa/model.py,sha256=6OPZ717idd3O7AyA-Y5f0rjuwPoRzBLOqk0BCUs6sXM,5908
7
7
  mesa/space.py,sha256=9cN0fW0E850oBFp1lBVvr9_JWKN8wEbUrWuG05QHTE8,61870
8
- mesa/time.py,sha256=ldt5TbXQ3vVuwMECafcHiMDBEe2VFvYfEVsNFR9kWKk,19955
8
+ mesa/time.py,sha256=BAchKHNFh0ggQqaIMvojHhAF6I3zSQRlDXhQCyKq0NU,20208
9
9
  mesa/cookiecutter-mesa/cookiecutter.json,sha256=tBSWli39fOWUXGfiDCTKd92M7uKaBIswXbkOdbUufYY,337
10
10
  mesa/cookiecutter-mesa/hooks/post_gen_project.py,sha256=8JoXZKIioRYEWJURC0udj8WS3rg0c4So62sOZSGbrMY,294
11
11
  mesa/cookiecutter-mesa/{{cookiecutter.snake}}/README.md,sha256=Yji4lGY-NtQSnW-oBj0_Jhs-XhCfZA8R1mBBM_IllGs,80
@@ -15,7 +15,7 @@ mesa/cookiecutter-mesa/{{cookiecutter.snake}}/{{cookiecutter.snake}}/__init__.py
15
15
  mesa/cookiecutter-mesa/{{cookiecutter.snake}}/{{cookiecutter.snake}}/model.pytemplate,sha256=gTxSZ9t8My_Qiwuusqaf8DmspncrLKptxbg9cvfbloc,1842
16
16
  mesa/cookiecutter-mesa/{{cookiecutter.snake}}/{{cookiecutter.snake}}/server.pytemplate,sha256=nqi6cPjhiyrYw82_Y9hLSrfZtSVCGIhMLOXRB7kKTlQ,823
17
17
  mesa/experimental/__init__.py,sha256=lHhvI2jfsLHfICO7q-LRA6UK4Pyxr7edkBDbjTqPGVo,55
18
- mesa/experimental/jupyter_viz.py,sha256=-E8Ie4J3N0894Wr8kmsSGabELbh34GG_elOxUjEutOw,11712
18
+ mesa/experimental/jupyter_viz.py,sha256=snVzObTPw-ekZyp6wmaCQdmQ2444gmvY443asNqKM98,11722
19
19
  mesa/experimental/components/matplotlib.py,sha256=5xqlSc8wwYxVc_M0GNt0NC0BRS8cdEp6TvBqHoRMhvY,4065
20
20
  mesa/flat/__init__.py,sha256=hSqQDjkfIgDu7B3aYtjPeNEUXdlKPHQuNN8HEV0XR6s,218
21
21
  mesa/flat/visualization.py,sha256=5aCm8xDCmZij3hoJZvOVmmpzU9ACXSSSvmQr51buLVg,290
@@ -24,8 +24,8 @@ mesa/visualization/TextVisualization.py,sha256=BIP0XcmIdYhz0igqe8yRZXlXeOOqJZeu8
24
24
  mesa/visualization/UserParam.py,sha256=D3qxoX-Cpqhyn06IdIO_C5s0u8nlhv3988lVwkBlcGo,49
25
25
  mesa/visualization/__init__.py,sha256=5fwVAzgVsmxAzgoLxdC26l2ZE-m2bWj963xPNSDaQEQ,287
26
26
  mesa/visualization/modules.py,sha256=pf6K3KECX51VNNqpFCm2EE5KV0A22UYmfXzTVXPnF_o,47
27
- mesa-2.2.3.dist-info/METADATA,sha256=URL5-0xna14R6lkaZhfzf1sbYnevYauiq9R3K1rLY0E,7148
28
- mesa-2.2.3.dist-info/WHEEL,sha256=mRYSEL3Ih6g5a_CVMIcwiF__0Ae4_gLYh01YFNwiq1k,87
29
- mesa-2.2.3.dist-info/entry_points.txt,sha256=IOcQtetGF8l4wHpOs_hGb19Rz-FS__BMXOJR10IBPsA,39
30
- mesa-2.2.3.dist-info/licenses/LICENSE,sha256=OGUgret9fRrm8J3pdsPXETIjf0H8puK_Nmy970ZzT78,572
31
- mesa-2.2.3.dist-info/RECORD,,
27
+ mesa-2.2.4.dist-info/METADATA,sha256=Ay9vvBwnAMzLgDbbKho9IALEkIal7VAZopk5Ehh_MxE,7148
28
+ mesa-2.2.4.dist-info/WHEEL,sha256=TJPnKdtrSue7xZ_AVGkp9YXcvDrobsjBds1du3Nx6dc,87
29
+ mesa-2.2.4.dist-info/entry_points.txt,sha256=IOcQtetGF8l4wHpOs_hGb19Rz-FS__BMXOJR10IBPsA,39
30
+ mesa-2.2.4.dist-info/licenses/LICENSE,sha256=OGUgret9fRrm8J3pdsPXETIjf0H8puK_Nmy970ZzT78,572
31
+ mesa-2.2.4.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.21.0
2
+ Generator: hatchling 1.21.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any