agentMET4FOF agent network
- class agentMET4FOF.network.AgentNetwork(ip_addr='0.0.0.0', port=3333, connect=False, log_filename='log_file.csv', dashboard_modules=True, dashboard_extensions=[], dashboard_update_interval=3, dashboard_max_monitors=10, dashboard_port=8050, backend=Backend.OSBRAIN, mesa_update_interval=0.1, network_stylesheet=[{'selector': 'node', 'style': {'color': '#FFF', 'label': 'data(id)', 'shape': 'rectangle', 'text-halign': 'center', 'text-outline-color': '#000232', 'text-outline-width': 1.5, 'text-valign': 'center'}}, {'selector': 'edge', 'style': {'arrow-scale': 2, 'color': '#FFF', 'curve-style': 'unbundled-bezier', 'label': 'data(channel)', 'line-color': '#4287f5', 'mid-target-arrow-color': '#4287f5', 'mid-target-arrow-shape': 'triangle', 'text-margin-x': '10px', 'text-margin-y': '20px', 'text-outline-color': '#000232', 'text-outline-width': 1.5}}, {'selector': '.rectangle', 'style': {'shape': 'rectangle'}}, {'selector': '.triangle', 'style': {'shape': 'triangle'}}, {'selector': '.octagon', 'style': {'shape': 'octagon'}}, {'selector': '.ellipse', 'style': {'shape': 'ellipse'}}, {'selector': '.bluebackground', 'style': {'background-color': '#c4fdff'}}, {'selector': '.blue', 'style': {'background-color': '#006db5'}}, {'selector': '.coalition', 'style': {'background-color': '#c4fdff', 'text-halign': 'center', 'text-valign': 'top'}}, {'selector': '.coalition-edge', 'style': {'line-style': 'dashed'}}, {'selector': '.outline', 'style': {'color': '#fff', 'text-outline-color': '#888', 'text-outline-width': 2}}], **dashboard_kwargs)[source]
Object for starting a new Agent Network or connect to an existing Agent Network
An existing Agent Network can be specified by ip & port. Provides function to add agents, (un)bind agents, query agent network state, set global agent states Interfaces with an internal _AgentController which is hidden from user.
- class Coalition(name='Coalition', agents=[])[source]
A special class for grouping agents.
It is rendered as a parent group on the dashboard, along with its member agents.
- class _AgentController(name='', host=None, serializer=None, transport=None, attributes=None, backend=Backend.OSBRAIN, mesa_model=<class 'mesa.model.Model'>)[source]
Unique internal agent to provide control to other agents
Automatically instantiated when starting server. Provides global control to all agents in network.
- _transform_string_into_valid_name(name: str) str [source]
Ensure that name does not contain invalid characters
osBrain does not allow spaces in agents’ names, so we replace them by underscores. Mesa does not allow a single space as name, so we replace that as well by an underscore.
- add_coalition_agent(name: str, agents: List[AgentMET4FOF | Proxy])[source]
Add agents into the coalition
- agents(exclude_names: List[str] | None = None) List[str] [source]
Returns all or subset of agents’ names connected to agent network
For the osBrain backend , the mandatory agents
AgentController
,Logger
are never returned.
- get_agent(agent_name: str) AgentMET4FOF | Proxy | None [source]
Returns a particular agent connected to Agent Network
- Parameters:
agent_name (str) – Name of agent to search for in the network
- Returns:
The particular agent with the provided name or None, if no agent with the provided name can be found
- Return type:
Union[AgentMET4FOF, Proxy]
- class _Logger(name='', host=None, serializer=None, transport=None, attributes=None, backend=Backend.OSBRAIN, mesa_model=<class 'mesa.model.Model'>)[source]
An internal logger agent which is instantiated with each AgentNetwork
It collects all the logs which are sent to it, and print them and optionally save them into a csv log file. Since the user is not expected to directly access the logger agent, its initialisation option and interface are provided via the AgentNetwork object.
When log_info of any agent is called, the agent will send the data to the logger agent.
- _get_controller() _AgentController [source]
Internal method to access the AgentController relative to the nameserver
- _get_controller_mode()[source]
Internal method to get mode of agent controller
- Returns:
state – State of Agent Network
- Return type:
- _set_controller_mode(state: str)[source]
Internal method to set mode of agent controller
- Parameters:
state (str) – State of agent controller to set
- add_agent(name: str | None = None, agentType: ~typing.Type[~agentMET4FOF.agents.base_agents.AgentMET4FOF] | None = <class 'agentMET4FOF.agents.base_agents.AgentMET4FOF'>, log_mode: bool | None = True, buffer_size: int | None = 1000, ip_addr: str | None = None, loop_wait: int | None = None, **kwargs) Type[AgentMET4FOF] [source]
Instantiates a new agent in the network.
- Parameters:
name (str, optional) – Unique name of agent, defaults to the agent’s class name.
agentType (Type[AgentMET4FOF] or subclass of AgentMET4FOF, optional) – Agent class to be instantiated in the network, defaults to
AgentMET4FOF
log_mode (bool, optional) – Determines if messages will be logged to background Logger Agent, defaults to
True
buffer_size (int, optional) – The total number of elements to be stored in the agent
buffer
, defaults to 1.000ip_addr (str, optional) – IP Address of the Agent Network address to connect to. By default, it will match that of the ip_addr, assuming the Dashboard and Agent Network are run on the same machine with same IP address.
loop_wait (float, optional) – The wait between each iteration of the loop, defaults to the
AgentMET4FOF.init_agent_loop()
default
- Returns:
Newly instantiated agent
- Return type:
- agents(filter_agent: str | None = None) List[str] [source]
Returns all or subset of agents’ names connected to agent network
- agents_by_type(only_type: ~typing.Type[~agentMET4FOF.agents.base_agents.AgentMET4FOF] | None = <class 'agentMET4FOF.agents.base_agents.AgentMET4FOF'>) Set[AgentMET4FOF | Proxy | None] [source]
Returns all or a subset of agents connected to an agent network
As expected, the returned set might be empty, if there is no agent of the specified class present in the network.
- Parameters:
only_type (Type[AgentMET4FOF], optional) – if present, only those agents which are instances of the class
only_type
or a subclasses are listed- Returns:
requested agents’ objects depending on the backend either instances of subclasses of
AgentMET4FOF
or of osBrain’s``Proxy``.- Return type:
Set[AgentMET4FOF, Proxy]
- bind_agents(source, target, channel='default')[source]
Binds two agents’ communication channel in a unidirectional manner
Any subsequent calls of source.send_output() will reach target agent’s message queue.
- Parameters:
source (AgentMET4FOF) – Source agent whose Output channel will be binded to target
target (AgentMET4FOF) – Target agent whose Input channel will be binded to source
- connect(ip_addr: str | None = '127.0.0.1', port: int | None = 3333)[source]
Connects to an existing agent network’s name server for osBrain backend
- get_agent(agent_name: str) AgentMET4FOF | Proxy | None [source]
Returns a particular agent connected to Agent Network
- Parameters:
agent_name (str) – Name of agent to search for in the network
- Returns:
The particular agent with the provided name or None, if no agent with the provided name can be found
- Return type:
Union[AgentMET4FOF, Proxy]
- set_agents_state(filter_agent: str | None = None, state: str | None = 'Idle')[source]
Blanket operation on all agents to set their
current_state
to given stateCan be used to define different states of operation such as “Running”, “Idle, “Stop”, etc.. Users will need to define their own flow of handling each type of self.current_state in the agent_loop.
- set_running_state(filter_agent: str | None = None)[source]
Blanket operation on all agents to set their
current_state
to “Running”- Parameters:
filter_agent (str, optional) – Filter name of agents to set the states
- set_stop_state(filter_agent=None)[source]
Blanket operation on all agents to set their current_state attribute to “Stop”
Users will need to define their own flow of handling each type of self.current_state in the agent_loop.
- Parameters:
filter_agent (str) – (Optional) Filter name of agents to set the states
- start_server_osbrain(ip_addr: str | None = '127.0.0.1', port: int | None = 3333)[source]
Starts a new agent network’s name server for osBrain
- unbind_agents(source, target)[source]
Unbinds two agents communication channel in a unidirectional manner
This is the reverse of bind_agents()
- Parameters:
source (AgentMET4FOF) – Source agent whose Output channel will be unbinded from target
target (AgentMET4FOF) – Target agent whose Input channel will be unbinded from source