def test_simpleAgent(agentType, expected_monitor_results): #start agent network server agentNetwork = AgentNetwork(dashboard_modules=False) #init agents by adding into the agent network simple_agent = agentNetwork.add_agent(agentType=agentType) monitor_agent_1 = agentNetwork.add_agent(agentType=MonitorAgent) #shorten n wait loop time simple_agent.init_agent_loop(0.01) #connect agents agentNetwork.bind_agents(simple_agent, monitor_agent_1) # set all agents states to "Running" agentNetwork.set_running_state() time.sleep(test_timeout) # test to see if monitor agents have received the correct data assert str( monitor_agent_1.get_attr('memory')) == str(expected_monitor_results) # shutdown agent network agentNetwork.shutdown() time.sleep(3)
def test_send_plot(): # start agent network server agentNetwork = AgentNetwork(dashboard_modules=False) # init agents gen_agent = agentNetwork.add_agent(agentType=GeneratorAgent) monitor_agent = agentNetwork.add_agent(agentType=MonitorAgent) agentNetwork.bind_agents(gen_agent, monitor_agent) gen_agent.dummy_send_graph() time.sleep(3) assert monitor_agent.get_attr('plots')['GeneratorAgent_1'] agentNetwork.shutdown()
num_accurate = [1 if y == True else 0 for y in res] accuracy = np.sum(num_accurate) / len(num_accurate) * 100 return accuracy def on_received_message(self, message): if message['senderType'] == "Predictor": data = message['data'] y_pred = data['y_pred'] y_true = data['y_true'] res = self.compute_accuracy(y_pred=y_pred, y_true=y_true) self.send_output(res) if __name__ == '__main__': # start agent network server agentNetwork = AgentNetwork() # init agents gen_agent = agentNetwork.add_agent(agentType=DataStreamAgent) trainer_agent = agentNetwork.add_agent(agentType=Trainer) predictor_agent = agentNetwork.add_agent(agentType=Predictor) evaluator_agent = agentNetwork.add_agent(agentType=Evaluator) monitor_agent_1 = agentNetwork.add_agent(agentType=MonitorAgent) monitor_agent_2 = agentNetwork.add_agent(agentType=MonitorAgent) gen_agent.init_parameters(stream=SineGenerator(), pretrain_size=1000, batch_size=1) trainer_agent.init_parameters(ml_model=HoeffdingTree()) # connect agents : We can connect multiple agents to any particular agent # However the agent needs to implement handling multiple input types
self.results.append(np.mean(res_temp)) self.send_output(self.results[-1]) # classifier accuracy - user defined def compute_accuracy(self, y_pred, y_true): res = y_pred == y_true num_accurate = [1 if y == True else 0 for y in res] accuracy = np.sum(num_accurate) / len(num_accurate) * 100 return accuracy if __name__ == '__main__': #start agent network agentNetwork = AgentNetwork() #add agents data_stream_agent_1 = agentNetwork.add_agent(agentType=DataStreamAgent) ml_agent_hoeffdingTree = agentNetwork.add_agent(agentType=ML_Model) ml_agent_neuralNets = agentNetwork.add_agent(agentType=ML_Model) monitor_agent_1 = agentNetwork.add_agent(agentType=MonitorAgent) #init parameters data_stream_agent_1.init_parameters(stream=WaveformGenerator(), pretrain_size=1000, batch_size=100) ml_agent_hoeffdingTree.init_parameters(ml_model=HoeffdingTree()) ml_agent_neuralNets.init_parameters(ml_model=NaiveBayes()) #connect agents
plus_data = plus(message['data'], self.plus_param) self.send_output({'minus':minus_data,'plus':plus_data}) class SineGeneratorAgent(AgentMET4FOF): def init_parameters(self): self.stream = SineGenerator() def agent_loop(self): if self.current_state == "Running": sine_data = self.stream.next_sample() #dictionary self.send_output(sine_data['x']) if __name__ == '__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)
raise Exception class SineGeneratorAgent(AgentMET4FOF): def init_parameters(self): self.stream = SineGenerator() def agent_loop(self): if self.current_state == "Running": sine_data = self.stream.next_sample() #dictionary self.send_output(sine_data['x']) if __name__ == '__main__': #start agent network server agentNetwork = AgentNetwork(dashboard_modules=False) #init agents by adding into the agent network gen_agent = agentNetwork.add_agent(agentType=SineGeneratorAgent) _logger_agent = agentNetwork.add_agent(agentType=_Logger) monitor_agent = agentNetwork.add_agent(agentType=MonitorAgent) gen_agent.set_logger(_logger_agent) #connect agents by either way: # 1) by agent network.bind_agents(source,target) agentNetwork.bind_agents(gen_agent, monitor_agent) # 2) by the agent.bind_output() gen_agent.bind_output(monitor_agent)
#Each agent has internal current_state which can be used as a switch by the AgentNetwork class SineGeneratorAgent(AgentMET4FOF): def init_parameters(self): self.stream = SineGenerator() def agent_loop(self): if self.current_state == "Running": sine_data = self.stream.next_sample() #dictionary self.send_output(sine_data['x']) if __name__ == '__main__': #start agent network server agentNetwork = AgentNetwork() #init agents by adding into the agent network gen_agent = agentNetwork.add_agent(agentType=SineGeneratorAgent) monitor_agent = agentNetwork.add_agent(agentType=MonitorAgent) #connect agents by either way: # 1) by agent network.bind_agents(source,target) agentNetwork.bind_agents(gen_agent, monitor_agent) # 2) by the agent.bind_output() gen_agent.bind_output(monitor_agent) # set all agents states to "Running" agentNetwork.set_running_state()
from AgentMET4FOF import AgentNetwork #Agent modules import examples.ZEMA_EMC.zema_agents as zema_agents import examples.ZEMA_EMC.zema_datastream as zema_datastream dashboard_modules = [zema_agents, zema_datastream] if __name__ == '__main__': #start agent network server agentNetwork = AgentNetwork(dashboard_modules=dashboard_modules)
def test_zema_emc_lda_agents(): np.random.seed(100) #start agent network server agentNetwork = AgentNetwork(dashboard_modules=False) #init agents by adding into the agent network datastream_agent = agentNetwork.add_agent(agentType=DataStreamAgent) train_test_split_agent = agentNetwork.add_agent( agentType=TrainTestSplitAgent) fft_bfc_agent = agentNetwork.add_agent(agentType=FFT_BFCAgent) pearson_fs_agent = agentNetwork.add_agent( agentType=Pearson_FeatureSelectionAgent) lda_agent = agentNetwork.add_agent(agentType=LDA_Agent) evaluator_agent = agentNetwork.add_agent(agentType=EvaluatorAgent) monitor_agent = agentNetwork.add_agent(agentType=MonitorAgent) #init parameters datastream_agent.init_parameters(ZEMA_DataStream()) train_test_split_agent.init_parameters(train_ratio=0.8) #bind agents agentNetwork.bind_agents(datastream_agent, train_test_split_agent) agentNetwork.bind_agents(train_test_split_agent, fft_bfc_agent) agentNetwork.bind_agents(fft_bfc_agent, pearson_fs_agent) agentNetwork.bind_agents(pearson_fs_agent, lda_agent) agentNetwork.bind_agents(lda_agent, evaluator_agent) #bind to monitor agents agentNetwork.bind_agents(fft_bfc_agent, monitor_agent) agentNetwork.bind_agents(pearson_fs_agent, monitor_agent) agentNetwork.bind_agents(lda_agent, monitor_agent) agentNetwork.bind_agents(evaluator_agent, monitor_agent) #trigger datastream to send all at once datastream_agent.send_all_sample() time.sleep(timeout_wait) assert lda_agent.get_attr('lda_test_score') == 0.8204924543288324 agentNetwork.shutdown()
def test_remove_agent(): #start agent network server agentNetwork = AgentNetwork(dashboard_modules=False) #init agents by adding into the agent network dummy_agent1 = agentNetwork.add_agent(agentType=AgentMET4FOF) dummy_agent2 = agentNetwork.add_agent(agentType=AgentMET4FOF) dummy_agent3 = agentNetwork.add_agent(agentType=AgentMET4FOF) dummy_agent4 = agentNetwork.add_agent(agentType=AgentMET4FOF) agentNetwork.bind_agents(dummy_agent1, dummy_agent2) agentNetwork.bind_agents(dummy_agent1, dummy_agent3) agentNetwork.bind_agents(dummy_agent1, dummy_agent4) agentNetwork.bind_agents(dummy_agent4, dummy_agent2) agentNetwork.bind_agents(dummy_agent3, dummy_agent4) agentNetwork.bind_agents(dummy_agent2, dummy_agent4) agentNetwork.remove_agent(dummy_agent1) agentNetwork.remove_agent(dummy_agent2) agentNetwork.remove_agent(dummy_agent3) agentNetwork.remove_agent(dummy_agent4) time.sleep(2) assert len(agentNetwork.agents()) == 0 agentNetwork.shutdown()
from AgentMET4FOF import AgentNetwork, MonitorAgent, DataStreamAgent from examples.ZEMA_EMC.zema_datastream import ZEMA_DataStream from examples.ZEMA_EMC.zema_agents import TrainTestSplitAgent, FFT_BFCAgent, Pearson_FeatureSelectionAgent, LDA_Agent, Regression_Agent, EvaluatorAgent import examples.ZEMA_EMC.zema_agents import numpy as np np.random.seed(100) if __name__ == '__main__': #start agent network server agentNetwork = AgentNetwork(dashboard_modules=[ examples.ZEMA_EMC.zema_datastream, examples.ZEMA_EMC.zema_agents ], log_filename=False) #init agents by adding into the agent network datastream_agent = agentNetwork.add_agent(agentType=DataStreamAgent) train_test_split_agent = agentNetwork.add_agent( agentType=TrainTestSplitAgent) fft_bfc_agent = agentNetwork.add_agent(agentType=FFT_BFCAgent) pearson_fs_agent = agentNetwork.add_agent( agentType=Pearson_FeatureSelectionAgent) lda_agent = agentNetwork.add_agent(agentType=LDA_Agent) bayesianRidge_agent = agentNetwork.add_agent(agentType=Regression_Agent, name="BayesianRidge_Agent") randomForest_agent = agentNetwork.add_agent(agentType=Regression_Agent, name="RandomForest_Agent") evaluator_agent = agentNetwork.add_agent(agentType=EvaluatorAgent)