@@ -33,11 +33,13 @@ class Model:
3333 Properties:
3434 agents: An AgentSet containing all agents in the model
3535 agent_types: A list of different agent types present in the model.
36+ agents_by_type: A dictionary where the keys are agent types and the values are the corresponding AgentSets.
3637 steps: An integer representing the number of steps the model has taken.
3738 It increases automatically at the start of each step() call.
3839
3940 Methods:
4041 get_agents_of_type: Returns an AgentSet of agents of the specified type.
42+ Deprecated: Use agents_by_type[agenttype] instead.
4143 run_model: Runs the model's simulation until a defined end condition is reached.
4244 step: Executes a single step of the model's simulation process.
4345 next_id: Generates and returns the next unique identifier for an agent.
@@ -106,24 +108,26 @@ def agent_types(self) -> list[type]:
106108 """Return a list of all unique agent types registered with the model."""
107109 return list (self ._agents_by_type .keys ())
108110
109- def get_agents_of_type (self , agenttype : type [Agent ]) -> AgentSet :
110- """Retrieves an AgentSet containing all agents of the specified type.
111-
112- Args:
113- agenttype: The type of agent to retrieve.
114-
115- Raises:
116- KeyError: If agenttype does not exist
117-
111+ @property
112+ def agents_by_type (self ) -> dict [type [Agent ], AgentSet ]:
113+ """A dictionary where the keys are agent types and the values are the corresponding AgentSets."""
114+ return self ._agents_by_type
118115
119- """
120- return self ._agents_by_type [agenttype ]
116+ def get_agents_of_type (self , agenttype : type [Agent ]) -> AgentSet :
117+ """Deprecated: Retrieves an AgentSet containing all agents of the specified type."""
118+ warnings .warn (
119+ f"Model.get_agents_of_type() is deprecated, please replace get_agents_of_type({ agenttype } )"
120+ f"with the property agents_by_type[{ agenttype } ]." ,
121+ DeprecationWarning ,
122+ stacklevel = 2 ,
123+ )
124+ return self .agents_by_type [agenttype ]
121125
122126 def _setup_agent_registration (self ):
123127 """helper method to initialize the agent registration datastructures"""
124128 self ._agents = {} # the hard references to all agents in the model
125129 self ._agents_by_type : dict [
126- type , AgentSet
130+ type [ Agent ] , AgentSet
127131 ] = {} # a dict with an agentset for each class of agents
128132 self ._all_agents = AgentSet ([], self ) # an agenset with all agents
129133
0 commit comments