agentMET4FOF agents¶
-
class
agentMET4FOF.agents.
AgentBuffer
(buffer_size=1000)[source]¶ Buffer class which is instantiated in every agent to store data incrementally. This buffer is necessary to handle multiple inputs coming from agents. The buffer can be a dict of iterables, or a dict of dict of iterables for nested named data. The keys are the names of agents.
We can access the buffer like a dict with exposed functions such as .values(), .keys() and .items(), The actual dict object is stored in the variable self.buffer
-
buffer_filled
(agent_from=None)[source]¶ Checks whether buffer is filled, by comparing against the buffer_size.
Parameters: agent_from (str) – Name of input agent in the buffer dict to be looked up for. If agent_from is not provided, we check for all iterables in the buffer. For nested dict, this returns true for any iterable which is beyond the buffer_size.
-
check_supported_datatype
(value)[source]¶ Checks whether value is one of the supported data types.
Parameters: value (iterable) – Value to be checked. Returns: result Return type: boolean
-
clear
(agent_from=None)[source]¶ Clears the data in the buffer. if agent_from is not given, the entire buffer is removed.
- agent_from : str
- Name of agent
-
store
(agent_from, data=None, concat_axis=0)[source]¶ Stores data into self.buffer with the received message
Checks if sender agent has sent any message before If it did, then append, otherwise create new entry for it
Parameters: - agent_from (dict | str) – if type is dict, we expect it to be the agentMET4FOF dict message to be compliant with older code otherwise, we expect it to be name of agent sender and data will need to be passed as parameter
- data – optional if agent_from is a dict. Otherwise this parameter is compulsory. Any supported data which can be stored in dict as buffering.
- concat_axis (int) – optional axis to concatenate on with the buffering for numpy arrays. Default is 0.
-
-
class
agentMET4FOF.agents.
AgentMET4FOF
(name='', host=None, serializer=None, transport=None, attributes=None, backend='osbrain', mesa_model=None)[source]¶ Base class for all agents with specific functions to be overridden/supplied by user.
Behavioral functions for users to provide are init_parameters, agent_loop and on_received_message. Communicative functions are bind_output, unbind_output and send_output.
-
agent_loop
()[source]¶ User defined method for the agent to execute for loop_wait seconds specified either in self.loop_wait or explicitly via`init_agent_loop(loop_wait)`
To start a new loop, call init_agent_loop(loop_wait) on the agent Example of usage is to check the current_state of the agent and send data periodically
-
bind_output
(output_agent)[source]¶ Forms Output connection with another agent. Any call on send_output will reach this newly binded agent
Adds the agent to its list of Outputs.
Parameters: output_agent (AgentMET4FOF or list) – Agent(s) to be binded to this agent’s output channel
-
buffer_clear
(agent_name=None)[source]¶ Empties buffer which is a dict indexed by the agent_name.
Parameters: agent_name (str) – Key of the memory dict, which can be the name of input agent, or self.name. If one is not supplied, we assume to clear the entire memory.
-
buffer_filled
(agent_name=None)[source]¶ Checks whether the internal buffer has been filled to the maximum allowed specified by self.buffer_size
Parameters: agent_name (str) – Index of the buffer which is the name of input agent. Returns: status of buffer filled Return type: boolean
-
buffer_store
(agent_from: str, data=None, concat_axis=0)[source]¶ Updates data stored in self.buffer with the received message
Checks if sender agent has sent any message before If it did,then append, otherwise create new entry for it
Parameters: - agent_from (str) – Name of agent sender
- data – Any supported data which can be stored in dict as buffer. See AgentBuffer for more information.
-
get_attr
(attr)[source]¶ Return the specified attribute of the agent.
Parameters: name – Name of the attribute to be retrieved.
-
handle_process_data
(message)[source]¶ Internal method to handle incoming message before calling user-defined on_received_message method.
If current_state is either Stop or Reset, it will terminate early before entering on_received_message
-
init_agent
(buffer_size=1000, log_mode=True)[source]¶ Internal initialization to setup the agent: mainly on setting the dictionary of Inputs, Outputs, PubAddr.
Calls user-defined init_parameters() upon finishing.
-
Inputs
¶ Dictionary of Agents connected to its input channels. Messages will arrive from agents in this dictionary. Automatically updated when bind_output() function is called
Type: dict
-
Outputs
¶ Dictionary of Agents connected to its output channels. Messages will be sent to agents in this dictionary. Automatically updated when bind_output() function is called
Type: dict
-
PubAddr_alias
¶ Name of Publish address socket
Type: str
-
PubAddr
¶ Publish address socket handle
Type: str
-
AgentType
¶ Name of class
Type: str
-
current_state
¶ Current state of agent. Can 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
Type: str
-
loop_wait
¶ The interval to wait between loop. Call init_agent_loop to restart the timer or set the value of loop_wait in init_parameters when necessary.
Type: int
-
buffer_size
¶ The total number of elements to be stored in the agent buffer When total elements exceeds this number, the latest elements will be replaced with the incoming data elements
Type: int
-
-
init_agent_loop
(loop_wait: Optional[int] = None)[source]¶ Initiates the agent loop, which iterates every loop_wait seconds
Stops every timers and initiate a new loop.
Parameters: loop_wait (int, optional) – The wait between each iteration of the loop
-
log_info
(message)[source]¶ Prints logs to be saved into logfile with Logger Agent
Parameters: message (str) – Message to be logged to the internal Logger Agent
-
on_received_message
(message)[source]¶ User-defined method and is triggered to handle the message passed by Input.
Parameters: message (Dictionary) – The message received is in form {‘from’:agent_name, ‘data’: data, ‘senderType’: agent_class, ‘channel’:channel_name} agent_name is the name of the Input agent which sent the message data is the actual content of the message
-
pack_data
(data, channel='default')[source]¶ Internal method to pack the data content into a dictionary before sending out.
Special case : if the data is already a message, then the from and senderType will be altered to this agent, without altering the data and channel within the message this is used for more succinct data processing and passing.
Parameters: - data (argument) – Data content to be packed before sending out to agents.
- channel (str) – Key of dictionary which stores data
Returns: Packed message data
Return type: dict of the form {‘from’:agent_name, ‘data’: data, ‘senderType’: agent_class, ‘channel’:channel_name}.
-
reset
()[source]¶ This method will be called on all agents when the global reset_agents is called by the AgentNetwork and when the Reset button is clicked on the dashboard.
Method to reset the agent’s states and parameters. User can override this method to reset the specific parameters.
-
send_output
(data, channel='default')[source]¶ Sends message data to all connected agents in self.Outputs.
Output connection can first be formed by calling bind_output. By default calls pack_data(data) before sending out. Can specify specific channel as opposed to ‘default’ channel.
Parameters: - data (argument) – Data content to be sent out
- channel (str) – Key of message dictionary which stores data
Returns: message
Return type: dict of the form {‘from’:agent_name, ‘data’: data, ‘senderType’: agent_class, ‘channel’:channel_name}.
-
send_plot
(fig: Union[matplotlib.figure.Figure, Dict[str, matplotlib.figure.Figure]], mode: str = 'image')[source]¶ Sends plot to agents connected to this agent’s Output channel.
This method is different from send_output which will be sent to through the ‘plot’ channel to be handled.
Tradeoffs between “image” and “plotly” modes are that “image” are more stable and “plotly” are interactive. Note not all (complicated) matplotlib figures can be converted into a plotly figure.
Parameters: - fig (matplotlib.figure.Figure or dict of matplotlib.figure.Figure) – Alternatively, multiple figures can be nested in a dict (with any preferred keys) e.g {“Temperature”:matplotlib.Figure, “Acceleration”:matplotlib.Figure}
- mode (str) – “image” - converts into image via encoding at base64 string. “plotly” - converts into plotly figure using mpl_to_plotly Default: “image”
Returns: graph
Return type: str or plotly figure or dict of one of those converted figure(s)
-
set_attr
(**kwargs)[source]¶ Set object attributes.
Parameters: kwargs ([name, value]) – Keyword arguments will be used to set the object attributes.
-
shutdown
()[source]¶ Cleanly stop and shut down the agent assuming the agent is running.
Will let the main thread do the tear down.
-
stop_agent_loop
()[source]¶ Stops agent_loop from running. Note that the agent will still be responding to messages
-
unbind_output
(output_agent)[source]¶ Remove existing output connection with another agent. This reverses the bind_output method
Parameters: output_agent (AgentMET4FOF) – Agent binded to this agent’s output channel
-
-
class
agentMET4FOF.agents.
AgentNetwork
(ip_addr='127.0.0.1', 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='osbrain', mesa_update_interval=0.1)[source]¶ Object for starting a new Agent Network or connect to an existing Agent Network 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
-
add_agent
(name=' ', agentType=<class 'agentMET4FOF.agents.AgentMET4FOF'>, log_mode=True, buffer_size=1000, ip_addr=None, loop_wait=None, **kwargs)[source]¶ Instantiates a new agent in the network.
Parameters: - str (name) – with the same name. Defaults to the agent’s class name.
- AgentMET4FOF (agentType) – network. Defaults to
AgentMET4FOF
- bool (log_mode) – Logger Agent. Defaults to True.
Returns: AgentMET4FOF
Return type: Newly instantiated agent
-
agents
(filter_agent=None)[source]¶ Returns all agent names connected to Agent Network.
Returns: list Return type: names of all agents
-
bind_agents
(source, target)[source]¶ Binds two agents communication channel in a unidirectional manner from source Agent to target Agent
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='127.0.0.1', port=3333, verbose=True)[source]¶ Only for osbrain backend. Connects to an existing AgentNetwork.
Parameters: - ip_addr (str) – IP Address of server to connect to
- port (int) – Port of server to connect to
-
get_agent
(agent_name)[source]¶ Returns a particular agent connected to Agent Network.
Parameters: agent_name (str) – Name of agent to search for in the network
-
set_agents_state
(filter_agent=None, state='Idle')[source]¶ Blanket operation on all agents to set their current_state attribute to given state
Can 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
Parameters: - filter_agent (str) – (Optional) Filter name of agents to set the states
- state (str) – State of agents to set
-
set_running_state
(filter_agent=None)[source]¶ Blanket operation on all agents to set their current_state attribute to “Running”
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
-
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_mesa
()[source]¶ Handles the initialisation for backend == “mesa”. Involves spawning two nested objects : MesaModel and AgentController
-
start_server_osbrain
(ip_addr='127.0.0.1', port=3333)[source]¶ Only for osbrain backend. Starts a new AgentNetwork.
Parameters: - ip_addr (str) – IP Address of server to start
- port (int) – Port of server to start
-
unbind_agents
(source, target)[source]¶ Unbinds two agents communication channel in a unidirectional manner from source Agent to target Agent
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
-
-
class
agentMET4FOF.agents.
DataStreamAgent
(name='', host=None, serializer=None, transport=None, attributes=None, backend='osbrain', mesa_model=None)[source]¶ Able to simulate generation of datastream by loading a given DataStreamMET4FOF object.
Can be used in incremental training or batch training mode. To simulate batch training mode, set pretrain_size=-1 , otherwise, set pretrain_size and batch_size for the respective See DataStreamMET4FOF on loading your own data set as a data stream.
-
agent_loop
()[source]¶ User defined method for the agent to execute for loop_wait seconds specified either in self.loop_wait or explicitly via`init_agent_loop(loop_wait)`
To start a new loop, call init_agent_loop(loop_wait) on the agent Example of usage is to check the current_state of the agent and send data periodically
-
init_parameters
(stream=<agentMET4FOF.streams.DataStreamMET4FOF object>, pretrain_size=None, batch_size=1, loop_wait=1, randomize=False)[source]¶ Parameters: - stream (DataStreamMET4FOF) – A DataStreamMET4FOF object which provides the sample data
- pretrain_size (int) – The number of sample data to send through in the first loop cycle, and subsequently, the batch_size will be used
- batch_size (int) – The number of sample data to send in every loop cycle
- loop_wait (int) – The duration to wait (seconds) at the end of each loop cycle before going into the next cycle
- randomize (bool) – Determines if the dataset should be shuffled before streaming
-
-
class
agentMET4FOF.agents.
MonitorAgent
(name='', host=None, serializer=None, transport=None, attributes=None, backend='osbrain', mesa_model=None)[source]¶ Unique Agent for storing plots and data from messages received from input agents.
The dashboard searches for Monitor Agents’ memory and plots to draw the graphs “plot” channel is used to receive base64 images from agents to plot on dashboard
-
memory
¶ Dictionary of format {agent1_name : agent1_data, agent2_name : agent2_data}
Type: dict
-
plots
¶ Dictionary of format {agent1_name : agent1_plot, agent2_name : agent2_plot}
Type: dict
-
plot_filter
¶ List of keys to filter the ‘data’ upon receiving message to be saved into memory Used to specifically select only a few keys to be plotted
Type: list of str
-
init_parameters
(plot_filter=[], custom_plot_function=-1, *args, **kwargs)[source]¶ User provided function to initialize parameters of choice.
-
on_received_message
(message)[source]¶ Handles incoming data from ‘default’ and ‘plot’ channels.
Stores ‘default’ data into self.memory and ‘plot’ data into self.plots
Parameters: message (dict) – Acceptable channel values are ‘default’ or ‘plot’
-
reset
()[source]¶ This method will be called on all agents when the global reset_agents is called by the AgentNetwork and when the Reset button is clicked on the dashboard.
Method to reset the agent’s states and parameters. User can override this method to reset the specific parameters.
-
update_plot_memory
(message)[source]¶ Updates plot figures stored in self.plots with the received message
Parameters: message (dict) – Standard message format specified by AgentMET4FOF class Message[‘data’] needs to be base64 image string and can be nested in dictionary for multiple plots Only the latest plot will be shown kept and does not keep a history of the plots.
-