Skip to content

Commit

Permalink
Model: Natively support multiple agent types
Browse files Browse the repository at this point in the history
This commit introduces native support for multiple agent types to the model class. The key change is the introduction of a new method, `create_agent`, and an attribute, `agents_by_type`, which together streamline the process of creating and categorizing different types of agents.

Changes:
1. Added `agents_by_type` Dictionary:
   - This attribute categorizes agents by their class type, enabling efficient filtering and retrieval of agents based on their specific type.
   - It uses the class of the agent as the key, providing a more robust and type-safe approach to agent categorization compared to using string identifiers.

2. New Method `create_agent`:
   - This method simplifies the creation of agents. It initializes an agent with a unique ID and adds it to the `agents_by_type` dictionary, handling agent categorization seamlessly.
   - The method accepts the agent's class type and any additional arguments required for initialization, offering flexibility in agent creation.

Advantages:
1. Enhanced Flexibility and Scalability:
   - This construct allows for the easy addition of new agent types and modifications to existing ones without the need to overhaul the agent management system.
   - By decoupling agent creation from their assignment to the schedule and grid, it offers more control over how and when agents are integrated into different model components.

2. Improved Type Safety and Clarity:
   - Using class types as identifiers enhances type safety and clarity. It avoids potential errors or confusion that may arise from using string-based identifiers, especially in large and complex models.

3. Streamlined Agent Management:
   - The `create_agent` method encapsulates the logic of agent initialization and categorization in one place, reducing redundancy and potential for errors in agent setup.
   - The `agents_by_type` dictionary simplifies the process of working with different agent types, be it for data collection, scheduling, or implementing specific behaviors based on the agent type.

4. Facilitates Complex Simulations:
   - These enhancements are particularly beneficial for simulations that require a diverse range of agent types with distinct behaviors. The new structure supports the development of more sophisticated and nuanced agent-based models.
  • Loading branch information
EwoutH committed Nov 30, 2023
1 parent 0593c25 commit 1531ea7
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions mesa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,29 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.running = True
self.schedule = None
self.current_id = 0
self.agents_by_type = {}

def create_agent(self, agent_type, *args, **kwargs):
"""
Creates an agent of the given type with a unique ID and adds it to the agents_by_type dictionary.
Parameters:
agent_type (class): The class of the agent to be created.
*args, **kwargs: Additional arguments passed to the agent's constructor.
Returns:
An instance of the specified agent_type.
"""
# Create a new agent instance with a unique ID
agent = agent_type(self.next_id(), self, *args, **kwargs)

Check warning on line 58 in mesa/model.py

View check run for this annotation

Codecov / codecov/patch

mesa/model.py#L58

Added line #L58 was not covered by tests

# Add the agent to the agents_by_type dictionary
agent_class = agent.__class__

Check warning on line 61 in mesa/model.py

View check run for this annotation

Codecov / codecov/patch

mesa/model.py#L61

Added line #L61 was not covered by tests
if agent_class not in self.agents_by_type:
self.agents_by_type[agent_class] = []
self.agents_by_type[agent_class].append(agent)

Check warning on line 64 in mesa/model.py

View check run for this annotation

Codecov / codecov/patch

mesa/model.py#L63-L64

Added lines #L63 - L64 were not covered by tests

return agent

Check warning on line 66 in mesa/model.py

View check run for this annotation

Codecov / codecov/patch

mesa/model.py#L66

Added line #L66 was not covered by tests

def run_model(self) -> None:
"""Run the model until the end condition is reached. Overload as
Expand Down

0 comments on commit 1531ea7

Please sign in to comment.