def init_parameters(self, ip_adress="", port=7654, agent_network_ip="127.0.0.1", agent_network_port=3333): self.agentNetwork = AgentNetwork(ip_addr=agent_network_ip, port=agent_network_port, connect=True, dashboard_modules=False) self.loop_wait = 0.0 #socket.recvfrom() is blocking so no delay needed self.flags = {"Networtinited": False} self.params = { "IP": ip_adress, "Port": port, "PacketrateUpdateCount": 10000 } self.socket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM # Internet ) # UDP # Try to open the UDP connection try: self.socket.bind((ip_adress, port)) except OSError as err: print("OS error: {0}".format(err)) if err.errno == 99: print( "most likely no network card of the system has the ip address" + str(ip_adress) + " check this with >>> ifconfig on linux or with >>> ipconfig on Windows" ) if err.errno == 98: print( "an other task is blocking the connection on linux use >>> sudo netstat -ltnp | grep -w ':" + str(port) + "' on windows use in PowerShell >>> Get-Process -Id (Get-NetTCPConnection -LocalPort " + str(port) + ").OwningProcess") raise (err) # we need to raise an exception to prevent __init__ from returning # otherwise a broken class instance will be created except: print("Unexpected error:", sys.exc_info()[0]) raise ("Unexpected error:", sys.exc_info()[0]) self.flags["Networtinited"] = True self.packestlosforsensor = {} self.AllSensors = {} self.msgcount = 0 self.lastTimestamp = 0 self.Datarate = 0 print("Data receiver now running wating for Packates")
def main(): # start agent network server agentNetwork = AgentNetwork() # init agents by adding into the agent network gen_agent = agentNetwork.add_agent(agentType=ConfusionMatrixAgent) 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() # allow for shutting down the network after execution return agentNetwork
for i in range(len(current_sensor_data)) } self.send_output(current_sensor_data) def readData(message, Description): time.sleep(5) print("MSG:" + str(message)) print(type(message)) print("DATA1:" + str(message.Data_01)) current_sensor_data = unpack_sensor_data(message) print(current_sensor_data) print("DESCRIPT:" + str(Description)) if __name__ == "__main__": #start agent network server agentNetwork = AgentNetwork() #init agents by adding into the agent network gen_agent = agentNetwork.add_agent(agentType=WP1_SensorAgent, log_mode=False) monitor_agent = agentNetwork.add_agent(agentType=MonitorAgent, log_mode=False) #connect agents agentNetwork.bind_agents(gen_agent, monitor_agent) # set all agents states to "Running" agentNetwork.set_running_state()
def main(): agentNetwork = AgentNetwork(dashboard_modules=[ml_agents], dashboard_extensions=Dashboard_ML_Experiment, backend="mesa")
class Met4FOFSSUDataReceiverAgent(AgentMET4FOF): def init_parameters(self, ip_adress="", port=7654, agent_network_ip="127.0.0.1", agent_network_port=3333): self.agentNetwork = AgentNetwork(ip_addr=agent_network_ip, port=agent_network_port, connect=True, dashboard_modules=False) self.loop_wait = 0.0 #socket.recvfrom() is blocking so no delay needed self.flags = {"Networtinited": False} self.params = { "IP": ip_adress, "Port": port, "PacketrateUpdateCount": 10000 } self.socket = socket.socket( socket.AF_INET, socket.SOCK_DGRAM # Internet ) # UDP # Try to open the UDP connection try: self.socket.bind((ip_adress, port)) except OSError as err: print("OS error: {0}".format(err)) if err.errno == 99: print( "most likely no network card of the system has the ip address" + str(ip_adress) + " check this with >>> ifconfig on linux or with >>> ipconfig on Windows" ) if err.errno == 98: print( "an other task is blocking the connection on linux use >>> sudo netstat -ltnp | grep -w ':" + str(port) + "' on windows use in PowerShell >>> Get-Process -Id (Get-NetTCPConnection -LocalPort " + str(port) + ").OwningProcess") raise (err) # we need to raise an exception to prevent __init__ from returning # otherwise a broken class instance will be created except: print("Unexpected error:", sys.exc_info()[0]) raise ("Unexpected error:", sys.exc_info()[0]) self.flags["Networtinited"] = True self.packestlosforsensor = {} self.AllSensors = {} self.msgcount = 0 self.lastTimestamp = 0 self.Datarate = 0 print("Data receiver now running wating for Packates") def agent_loop(self): data, addr = self.socket.recvfrom(1500) # buffer size is 1024 bytes wasValidData = False wasValidDescription = False ProtoData = messages_pb2.DataMessage() ProtoDescription = messages_pb2.DescriptionMessage() SensorID = 0 BytesProcessed = 4 # we need an offset of 4 sice if data[:4] == b"DATA": while BytesProcessed < len(data): msg_len, new_pos = _DecodeVarint32(data, BytesProcessed) BytesProcessed = new_pos try: msg_buf = data[new_pos:new_pos + msg_len] ProtoData.ParseFromString(msg_buf) wasValidData = True SensorID = ProtoData.id message = {"ProtMsg": msg_buf, "Type": "Data"} BytesProcessed += msg_len except: pass # ? no exception for wrong data type !! if not (wasValidData or wasValidDescription): print("INVALID PROTODATA") pass # invalid data leave parsing routine if SensorID in self.AllSensors: try: self.AllSensors[SensorID].send_output(message) except: tmp = self.packestlosforsensor[SensorID] = ( self.packestlosforsensor[SensorID] + 1) if tmp == 1: print("!!!! FATAL PERFORMANCE PROBLEMS !!!!") print("FIRSTTIME packet lost for sensor ID:" + str(SensorID)) print( "DROP MESSAGES ARE ONLY PRINTETD EVERY 1000 DROPS FROM NOW ON !!!!!!!! " ) if tmp % 1000 == 0: print("oh no lost an other thousand packets :(") else: self.AllSensors[SensorID] = self.agentNetwork.add_agent( agentType=SensorAgent, log_mode=False, ID=SensorID) self.agentNetwork.add_coalition( "Sensor_Group_1", [self] + list(self.AllSensors.values())) print("FOUND NEW SENSOR WITH ID=hex" + hex(SensorID) + "==>dec:" + str(SensorID)) self.packestlosforsensor[ SensorID] = 0 # initing lost packet counter self.msgcount = self.msgcount + 1 if self.msgcount % self.params["PacketrateUpdateCount"] == 0: print("received " + str(self.params["PacketrateUpdateCount"]) + " packets") if self.lastTimestamp != 0: timeDIFF = datetime.now() - self.lastTimestamp timeDIFF = timeDIFF.seconds + timeDIFF.microseconds * 1e-6 self.Datarate = (self.params["PacketrateUpdateCount"] / timeDIFF) print("Update rate is " + str(self.Datarate) + " Hz") self.lastTimestamp = datetime.now() else: self.lastTimestamp = datetime.now() elif data[:4] == b"DSCP": while BytesProcessed < len(data): msg_len, new_pos = _DecodeVarint32(data, BytesProcessed) BytesProcessed = new_pos try: msg_buf = data[new_pos:new_pos + msg_len] ProtoDescription.ParseFromString(msg_buf) # print(msg_buf) wasValidData = True SensorID = ProtoDescription.id message = {"ProtMsg": msg_buf, "Type": "Description"} BytesProcessed += msg_len except: pass # ? no exception for wrong data type !! if not (wasValidData or wasValidDescription): print("INVALID PROTODATA") pass # invalid data leave parsing routine if SensorID in self.AllSensors: try: self.AllSensors[SensorID].buffer.put_nowait(message) except: print("packet lost for sensor ID:" + hex(SensorID)) else: self.AllSensors[SensorID] = self.agentNetwork.add_agent( agentType=SensorAgent, log_mode=False, ID=SensorID) self.agentNetwork.add_coalition( "Sensor_Group_1", [self] + list(self.AllSensors.values())) print("FOUND NEW SENSOR WITH ID=hex" + hex(SensorID) + " dec==>:" + str(SensorID)) self.msgcount = self.msgcount + 1 if self.msgcount % self.params["PacketrateUpdateCount"] == 0: print("received " + str(self.params["PacketrateUpdateCount"]) + " packets") if self.lastTimestamp != 0: timeDIFF = datetime.now() - self.lastTimestamp timeDIFF = timeDIFF.seconds + timeDIFF.microseconds * 1e-6 self.Datarate = (self.params["PacketrateUpdateCount"] / timeDIFF) print("Update rate is " + str(self.Datarate) + " Hz") self.lastTimestamp = datetime.now() else: self.lastTimestamp = datetime.now() else: print("unrecognized packed preamble" + str(data[:5])) def __repr__(self): """ Prints IP and Port as well as list of all sensors (self.AllSensors). Returns ------- None. """ return ("Datareceiver liestening at ip " + str(self.params["IP"]) + " Port " + str(self.params["Port"]) + "\n Active Snesors are:" + str(self.AllSensors))
---------- message : protobuff message Data to be dumped. Returns ------- None. """ size = message.ByteSize() self.DumpfileProto.write(_VarintBytes(size)) self.DumpfileProto.write(message.SerializeToString()) if __name__ == "__main__": #start agent network server agentNetwork = AgentNetwork() #init agents by adding into the agent network gen_agent = agentNetwork.add_agent(agentType=Met4FOFSSUDataReceiverAgent, log_mode=False) monitor_agent = agentNetwork.add_agent(agentType=MonitorAgent, log_mode=False) #connect agents agentNetwork.bind_agents(gen_agent, monitor_agent) # set all agents states to "Running" agentNetwork.set_running_state()
def main(): # if use cuda if USE_CUDA: mp.set_start_method("spawn", force=True) # start agent network server agentNetwork = AgentNetwork() # init agents by adding into the agent network datastream_agent = agentNetwork.add_agent(agentType=DataStreamAgent) train_test_split_agent = agentNetwork.add_agent( agentType=TrainTestSplitAgent) stats_features_agent = agentNetwork.add_agent(agentType=StatsFeaturesAgent) bnn_agents = [ agentNetwork.add_agent(agentType=BNN_Agent) for target in output_sizes ] # evaluator_agent = agentNetwork.add_agent(agentType=EvaluatorAgent) evaluator_agents = [ agentNetwork.add_agent(agentType=EvaluatorAgent) for target in output_sizes ] monitor_agent_1 = agentNetwork.add_agent(agentType=MonitorAgent) monitor_agent_2 = agentNetwork.add_agent(agentType=MonitorAgent) # init parameters datastream_agent.init_parameters(stream=ZEMA_Hyd_DataStream(), pretrain_size=-1, randomize=True) # train_test_split_agent.init_parameters(kfold=5) train_test_split_agent.init_parameters(train_ratio=0.8) for index, val in enumerate(output_sizes): bnn_agents[index].init_parameters( model=BNN_Model, output_size=val, selectY_col=index, architecture=architecture, ) # bnn_agent.init_parameters(model=BNN_Model, output_size=4, selectY_col = 3) # connect agents by either way: agentNetwork.bind_agents(datastream_agent, train_test_split_agent) agentNetwork.bind_agents(train_test_split_agent, stats_features_agent) # agentNetwork.bind_agents(train_test_split_agent,bnn_agent) for index, bnn_agent in enumerate(bnn_agents): agentNetwork.bind_agents(stats_features_agent, bnn_agent) agentNetwork.bind_agents(bnn_agent, monitor_agent_1) agentNetwork.bind_agents(bnn_agent, evaluator_agents[index]) agentNetwork.bind_agents(evaluator_agents[index], monitor_agent_2) # # set all agents states to "Running" agentNetwork.set_running_state()