mesa_frames.agent
1from typing import TYPE_CHECKING, Optional 2 3import numpy as np 4import pandas as pd 5from numpy.random import randint 6 7if TYPE_CHECKING: 8 from mesa_frames.model import ModelDF 9 10 11class AgentDF: 12 """The AgentDF class is the base class for other agents. 13 It should be used as inherited class for new agents classes. 14 15 Attributes 16 ---------- 17 dtypes : dict[str, str] 18 The attributes of the Agent as a dictionary of columns and data types. It contains: 19 - id : int64 20 -- The unique id of the Agent. 21 - type : str 22 -- The type of the Agent. 23 model : Optional['ModelDF'] 24 The model of the simulation where the Agent is used. See src/model.py. Default: None 25 mask : pd.Series | None 26 The mask of the agents dataframe in the model which corresponds to the Agent class. 27 Initialized when model is created. Default: None 28 """ 29 30 dtypes: dict[str, str] = { 31 "id": "int64", 32 "type": "str", 33 } 34 model: Optional["ModelDF"] = None 35 mask: pd.Series | None = None 36 37 @classmethod 38 def __init__(cls): 39 """Initializes the Agent class. 40 Assigns a 64-bit random id to each agent in the model. 41 """ 42 if cls.mask is None or cls.model is None or cls.model.agents is None: 43 raise ValueError( 44 "The Agent classes have not been initialized. Please use the model.create_agents() method to initialize the mask." 45 ) 46 cls.model.agents.loc[cls.mask, "id"] = np.random.randint( 47 low=-9223372036854775808, 48 high=9223372036854775807, 49 size=cls.mask.sum(), 50 dtype="int64", 51 ) 52 53 @classmethod 54 def step(cls): 55 """The step method of the Agent class. 56 It should be decorated as @classmethod and should act on the mask portion of the agents dataframe of the model. 57 """ 58 pass 59 60 61class GeoAgentDF(AgentDF): 62 """The GeoAgentDF extends the AgentDF class to include a geometry attribute. 63 The agents will be stored in a GeoDataFrame. 64 65 Attributes 66 ---------- 67 dtypes : dict[str, str] 68 The attributes of the Agent as a dictionary of columns and data types. It contains: 69 - geometry : geometry 70 -- The geometry of the Agent. 71 """ 72 73 dtypes: dict[str, str] = {"geometry": "geometry"} 74 75 @classmethod 76 def step(cls): 77 """The step method of the GeoAgentDF class. 78 It should act on the mask portion of the agents dataframe of the model. 79 """ 80 pass
class
AgentDF:
12class AgentDF: 13 """The AgentDF class is the base class for other agents. 14 It should be used as inherited class for new agents classes. 15 16 Attributes 17 ---------- 18 dtypes : dict[str, str] 19 The attributes of the Agent as a dictionary of columns and data types. It contains: 20 - id : int64 21 -- The unique id of the Agent. 22 - type : str 23 -- The type of the Agent. 24 model : Optional['ModelDF'] 25 The model of the simulation where the Agent is used. See src/model.py. Default: None 26 mask : pd.Series | None 27 The mask of the agents dataframe in the model which corresponds to the Agent class. 28 Initialized when model is created. Default: None 29 """ 30 31 dtypes: dict[str, str] = { 32 "id": "int64", 33 "type": "str", 34 } 35 model: Optional["ModelDF"] = None 36 mask: pd.Series | None = None 37 38 @classmethod 39 def __init__(cls): 40 """Initializes the Agent class. 41 Assigns a 64-bit random id to each agent in the model. 42 """ 43 if cls.mask is None or cls.model is None or cls.model.agents is None: 44 raise ValueError( 45 "The Agent classes have not been initialized. Please use the model.create_agents() method to initialize the mask." 46 ) 47 cls.model.agents.loc[cls.mask, "id"] = np.random.randint( 48 low=-9223372036854775808, 49 high=9223372036854775807, 50 size=cls.mask.sum(), 51 dtype="int64", 52 ) 53 54 @classmethod 55 def step(cls): 56 """The step method of the Agent class. 57 It should be decorated as @classmethod and should act on the mask portion of the agents dataframe of the model. 58 """ 59 pass
The AgentDF class is the base class for other agents. It should be used as inherited class for new agents classes.
Attributes
- dtypes (dict[str, str]):
The attributes of the Agent as a dictionary of columns and data types. It contains:
- id : int64 -- The unique id of the Agent.
- type : str -- The type of the Agent.
- model (Optional['ModelDF']): The model of the simulation where the Agent is used. See src/model.py. Default: None
- mask (pd.Series | None): The mask of the agents dataframe in the model which corresponds to the Agent class. Initialized when model is created. Default: None
@classmethod
AgentDF()
38 @classmethod 39 def __init__(cls): 40 """Initializes the Agent class. 41 Assigns a 64-bit random id to each agent in the model. 42 """ 43 if cls.mask is None or cls.model is None or cls.model.agents is None: 44 raise ValueError( 45 "The Agent classes have not been initialized. Please use the model.create_agents() method to initialize the mask." 46 ) 47 cls.model.agents.loc[cls.mask, "id"] = np.random.randint( 48 low=-9223372036854775808, 49 high=9223372036854775807, 50 size=cls.mask.sum(), 51 dtype="int64", 52 )
Initializes the Agent class. Assigns a 64-bit random id to each agent in the model.
@classmethod
def
step(cls):
54 @classmethod 55 def step(cls): 56 """The step method of the Agent class. 57 It should be decorated as @classmethod and should act on the mask portion of the agents dataframe of the model. 58 """ 59 pass
The step method of the Agent class. It should be decorated as @classmethod and should act on the mask portion of the agents dataframe of the model.
62class GeoAgentDF(AgentDF): 63 """The GeoAgentDF extends the AgentDF class to include a geometry attribute. 64 The agents will be stored in a GeoDataFrame. 65 66 Attributes 67 ---------- 68 dtypes : dict[str, str] 69 The attributes of the Agent as a dictionary of columns and data types. It contains: 70 - geometry : geometry 71 -- The geometry of the Agent. 72 """ 73 74 dtypes: dict[str, str] = {"geometry": "geometry"} 75 76 @classmethod 77 def step(cls): 78 """The step method of the GeoAgentDF class. 79 It should act on the mask portion of the agents dataframe of the model. 80 """ 81 pass
The GeoAgentDF extends the AgentDF class to include a geometry attribute. The agents will be stored in a GeoDataFrame.
Attributes
- dtypes (dict[str, str]):
The attributes of the Agent as a dictionary of columns and data types. It contains:
- geometry : geometry -- The geometry of the Agent.
@classmethod
def
step(cls):
76 @classmethod 77 def step(cls): 78 """The step method of the GeoAgentDF class. 79 It should act on the mask portion of the agents dataframe of the model. 80 """ 81 pass
The step method of the GeoAgentDF class. It should act on the mask portion of the agents dataframe of the model.