Tutorial 2 - A simple pipeline with signal postprocessing.

Here we demonstrate how to build a MathAgent as an intermediate to process the SineGeneratorAgent’s output before plotting. Subsequently, a MultiMathAgent is built to show the ability to send a dictionary of multiple fields to the recipient. We provide a custom on_received_message() function, which is called every time a message is received from input agents.

The received message is a dictionary of the form:

{
'from':agent_name,
'data': data,
'senderType': agent_class_name,
'channel':'channel_name'
}

By default, 'channel' is set to "default", however a custom channel can be set when needed, which is demonstrated in the next tutorial.

[1]:
# %load tutorial_2_math_agent.py
from agentMET4FOF.agents import (
    AgentMET4FOF,
    AgentNetwork,
    MonitorAgent,
    SineGeneratorAgent,
)


class MathAgent(AgentMET4FOF):
    def on_received_message(self, message):
        data = self.divide_by_two(message["data"])
        self.send_output(data)

    # Define simple math functions.
    @staticmethod
    def divide_by_two(numerator: float) -> float:
        return numerator / 2


class MultiMathAgent(AgentMET4FOF):

    _minus_param: float
    _plus_param: float

    def init_parameters(self, minus_param=0.5, plus_param=0.5):
        self._minus_param = minus_param
        self._plus_param = plus_param

    def on_received_message(self, message):
        minus_data = self.minus(message["data"], self._minus_param)
        plus_data = self.plus(message["data"], self._plus_param)

        self.send_output({"minus": minus_data, "plus": plus_data})

    @staticmethod
    def minus(minuend: float, subtrahend: float) -> float:
        return minuend - subtrahend

    @staticmethod
    def plus(summand_1: float, summand_2: float) -> float:
        return summand_1 + summand_2


def main():
    # start agent network server
    agentNetwork = AgentNetwork()
    # init agents
    gen_agent = agentNetwork.add_agent(agentType=SineGeneratorAgent)
    math_agent = agentNetwork.add_agent(agentType=MathAgent)
    multi_math_agent = agentNetwork.add_agent(agentType=MultiMathAgent)
    monitor_agent = agentNetwork.add_agent(agentType=MonitorAgent)
    # connect agents : We can connect multiple agents to any particular agent
    agentNetwork.bind_agents(gen_agent, math_agent)
    agentNetwork.bind_agents(gen_agent, multi_math_agent)
    # connect
    agentNetwork.bind_agents(gen_agent, monitor_agent)
    agentNetwork.bind_agents(math_agent, monitor_agent)
    agentNetwork.bind_agents(multi_math_agent, monitor_agent)
    # set all agents states to "Running"
    agentNetwork.set_running_state()

    # allow for shutting down the network after execution
    return agentNetwork


if __name__ == "__main__":
    main()
Starting NameServer...
Broadcast server running on 0.0.0.0:9091
NS running on 127.0.0.1:3333 (127.0.0.1)
URI = PYRO:Pyro.NameServer@127.0.0.1:3333

--------------------------------------------------------------
|                                                            |
| Your agent network is starting up. Open your browser and   |
| visit the agentMET4FOF dashboard on http://127.0.0.1:8050/ |
|                                                            |
--------------------------------------------------------------

INFO [2021-02-05 19:18:07.585019] (SineGeneratorAgent_1): INITIALIZED
INFO [2021-02-05 19:18:07.619684] (MathAgent_1): INITIALIZED
INFO [2021-02-05 19:18:07.654192] (MultiMathAgent_1): INITIALIZED
INFO [2021-02-05 19:18:07.691566] (MonitorAgent_1): INITIALIZED
[2021-02-05 19:18:07.706815] (SineGeneratorAgent_1): Connected output module: MathAgent_1
[2021-02-05 19:18:07.714664] (SineGeneratorAgent_1): Connected output module: MultiMathAgent_1
[2021-02-05 19:18:07.724529] (SineGeneratorAgent_1): Connected output module: MonitorAgent_1
[2021-02-05 19:18:07.738462] (MathAgent_1): Connected output module: MonitorAgent_1
[2021-02-05 19:18:07.750583] (MultiMathAgent_1): Connected output module: MonitorAgent_1
SET STATE:   Running
[2021-02-05 19:18:08.589720] (SineGeneratorAgent_1): Pack time: 0.000765
[2021-02-05 19:18:08.592546] (MathAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:18:08.595246] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:18:08.596230] (MultiMathAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:18:08.595257] (SineGeneratorAgent_1): Sending: [0.]
[2021-02-05 19:18:08.593186] (MathAgent_1): Pack time: 0.000388
[2021-02-05 19:18:08.600139] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.])}
[2021-02-05 19:18:08.596854] (MultiMathAgent_1): Pack time: 0.000383
[2021-02-05 19:18:08.597959] (MultiMathAgent_1): Sending: {'minus': array([-0.5]), 'plus': array([0.5])}
[2021-02-05 19:18:08.593788] (MathAgent_1): Sending: [0.]
[2021-02-05 19:18:08.600521] (MonitorAgent_1): Tproc: 0.000931
[2021-02-05 19:18:08.598126] (MultiMathAgent_1): Tproc: 0.001735
[2021-02-05 19:18:08.594015] (MathAgent_1): Tproc: 0.001276
[2021-02-05 19:18:08.606396] (MonitorAgent_1): Received: {'from': 'MathAgent_1', 'data': array([0.]), 'senderType': 'MathAgent', 'channel': 'default'}
[2021-02-05 19:18:08.607428] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.]), 'MathAgent_1': array([0.])}
[2021-02-05 19:18:08.607807] (MonitorAgent_1): Tproc: 0.001189
[2021-02-05 19:18:08.613784] (MonitorAgent_1): Received: {'from': 'MultiMathAgent_1', 'data': {'minus': array([-0.5]), 'plus': array([0.5])}, 'senderType': 'MultiMathAgent', 'channel': 'default'}
[2021-02-05 19:18:08.620942] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.]), 'MathAgent_1': array([0.]), 'MultiMathAgent_1': {'minus': array([-0.5]), 'plus': array([0.5])}}
[2021-02-05 19:18:08.625194] (MonitorAgent_1): Tproc: 0.01115
[2021-02-05 19:18:09.590078] (SineGeneratorAgent_1): Pack time: 0.000561
[2021-02-05 19:18:09.592887] (SineGeneratorAgent_1): Sending: [0.06279052]
[2021-02-05 19:18:09.593616] (MathAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.06279052]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:18:09.593134] (MultiMathAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.06279052]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:18:09.598873] (MonitorAgent_1): Received: {'from': 'SineGeneratorAgent_1', 'data': array([0.06279052]), 'senderType': 'SineGeneratorAgent', 'channel': 'default'}
[2021-02-05 19:18:09.608525] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052]), 'MathAgent_1': array([0.]), 'MultiMathAgent_1': {'minus': array([-0.5]), 'plus': array([0.5])}}
[2021-02-05 19:18:09.594706] (MathAgent_1): Pack time: 0.000551
[2021-02-05 19:18:09.596935] (MultiMathAgent_1): Pack time: 0.001516
[2021-02-05 19:18:09.613332] (MonitorAgent_1): Tproc: 0.013659
[2021-02-05 19:18:09.596653] (MathAgent_1): Sending: [0.03139526]
[2021-02-05 19:18:09.613588] (MultiMathAgent_1): Sending: {'minus': array([-0.43720948]), 'plus': array([0.56279052])}
[2021-02-05 19:18:09.620995] (MonitorAgent_1): Received: {'from': 'MathAgent_1', 'data': array([0.03139526]), 'senderType': 'MathAgent', 'channel': 'default'}
[2021-02-05 19:18:09.597111] (MathAgent_1): Tproc: 0.003054
[2021-02-05 19:18:09.616401] (MultiMathAgent_1): Tproc: 0.022068
[2021-02-05 19:18:09.631221] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052]), 'MathAgent_1': array([0.        , 0.03139526]), 'MultiMathAgent_1': {'minus': array([-0.5]), 'plus': array([0.5])}}
[2021-02-05 19:18:09.632022] (MonitorAgent_1): Tproc: 0.0105
[2021-02-05 19:18:09.637249] (MonitorAgent_1): Received: {'from': 'MultiMathAgent_1', 'data': {'minus': array([-0.43720948]), 'plus': array([0.56279052])}, 'senderType': 'MultiMathAgent', 'channel': 'default'}
[2021-02-05 19:18:09.651789] (MonitorAgent_1): Buffer: {'SineGeneratorAgent_1': array([0.        , 0.06279052]), 'MathAgent_1': array([0.        , 0.03139526]), 'MultiMathAgent_1': {'minus': array([-0.5       , -0.43720948]), 'plus': array([0.5       , 0.56279052])}}
[2021-02-05 19:18:09.652908] (MonitorAgent_1): Tproc: 0.014285