|
11 | 11 | import contextlib |
12 | 12 | import copy |
13 | 13 | import operator |
14 | | -import warnings |
15 | 14 | import weakref |
16 | | -from collections import defaultdict |
17 | 15 | from collections.abc import Callable, Iterable, Iterator, MutableSet, Sequence |
18 | 16 | from random import Random |
19 | 17 |
|
@@ -52,17 +50,11 @@ def __init__(self, unique_id: int, model: Model) -> None: |
52 | 50 | # register agent |
53 | 51 | try: |
54 | 52 | self.model.agents_[type(self)][self] = None |
55 | | - except AttributeError: |
| 53 | + except AttributeError as err: |
56 | 54 | # model super has not been called |
57 | | - self.model.agents_ = defaultdict(dict) |
58 | | - self.model.agents_[type(self)][self] = None |
59 | | - self.model.agentset_experimental_warning_given = False |
60 | | - |
61 | | - warnings.warn( |
62 | | - "The Mesa Model class was not initialized. In the future, you need to explicitly initialize the Model by calling super().__init__() on initialization.", |
63 | | - FutureWarning, |
64 | | - stacklevel=2, |
65 | | - ) |
| 55 | + raise RuntimeError( |
| 56 | + "The Mesa Model class was not initialized. You must explicitly initialize the Model by calling super().__init__() on initialization." |
| 57 | + ) from err |
66 | 58 |
|
67 | 59 | def remove(self) -> None: |
68 | 60 | """Remove and delete the agent from the model.""" |
@@ -100,8 +92,6 @@ class AgentSet(MutableSet, Sequence): |
100 | 92 | which means that agents not referenced elsewhere in the program may be automatically removed from the AgentSet. |
101 | 93 | """ |
102 | 94 |
|
103 | | - agentset_experimental_warning_given = False |
104 | | - |
105 | 95 | def __init__(self, agents: Iterable[Agent], model: Model): |
106 | 96 | """ |
107 | 97 | Initializes the AgentSet with a collection of agents and a reference to the model. |
@@ -227,26 +217,37 @@ def _update(self, agents: Iterable[Agent]): |
227 | 217 | return self |
228 | 218 |
|
229 | 219 | def do( |
230 | | - self, method_name: str, *args, return_results: bool = False, **kwargs |
| 220 | + self, method: str | Callable, *args, return_results: bool = False, **kwargs |
231 | 221 | ) -> AgentSet | list[Any]: |
232 | 222 | """ |
233 | | - Invoke a method on each agent in the AgentSet. |
| 223 | + Invoke a method or function on each agent in the AgentSet. |
234 | 224 |
|
235 | 225 | Args: |
236 | | - method_name (str): The name of the method to call on each agent. |
| 226 | + method (str, callable): the callable to do on each agents |
| 227 | +
|
| 228 | + * in case of str, the name of the method to call on each agent. |
| 229 | + * in case of callable, the function to be called with each agent as first argument |
| 230 | +
|
237 | 231 | return_results (bool, optional): If True, returns the results of the method calls; otherwise, returns the AgentSet itself. Defaults to False, so you can chain method calls. |
238 | | - *args: Variable length argument list passed to the method being called. |
239 | | - **kwargs: Arbitrary keyword arguments passed to the method being called. |
| 232 | + *args: Variable length argument list passed to the callable being called. |
| 233 | + **kwargs: Arbitrary keyword arguments passed to the callable being called. |
240 | 234 |
|
241 | 235 | Returns: |
242 | | - AgentSet | list[Any]: The results of the method calls if return_results is True, otherwise the AgentSet itself. |
| 236 | + AgentSet | list[Any]: The results of the callable calls if return_results is True, otherwise the AgentSet itself. |
243 | 237 | """ |
244 | 238 | # we iterate over the actual weakref keys and check if weakref is alive before calling the method |
245 | | - res = [ |
246 | | - getattr(agent, method_name)(*args, **kwargs) |
247 | | - for agentref in self._agents.keyrefs() |
248 | | - if (agent := agentref()) is not None |
249 | | - ] |
| 239 | + if isinstance(method, str): |
| 240 | + res = [ |
| 241 | + getattr(agent, method)(*args, **kwargs) |
| 242 | + for agentref in self._agents.keyrefs() |
| 243 | + if (agent := agentref()) is not None |
| 244 | + ] |
| 245 | + else: |
| 246 | + res = [ |
| 247 | + method(agent, *args, **kwargs) |
| 248 | + for agentref in self._agents.keyrefs() |
| 249 | + if (agent := agentref()) is not None |
| 250 | + ] |
250 | 251 |
|
251 | 252 | return res if return_results else self |
252 | 253 |
|
|
0 commit comments