def receive(self): try: received, address = self.socketConn.recvfrom(2048) logMessage = "Received message from " + address Logger.Log(0, logMessage) except Exception as ex: Logger.Log(1, "Failed to receive message: " + str(ex)) return ex
def bind(self): try: Logger.Log(0, "Binding Socket") self.socketConn.bind((self.hostIP, self.hostPort)) Logger.Log(0, "Sucessfuly bind") except Exception as ex: Logger.Log(1, "Failed to bind:" + str(ex)) return ex
def send(self, message, destination): try: logMessage = "Sending Message to " + str(destination) Logger.Log(0,logMessage) data = message self.socketConn.sendto(data.encode('utf-8'),destination) Logger.Log(0,"Message Sent") except Exception as ex: Logger.Log(1, "Failed to send message: " + str(ex)) return ex
def regulate(self, iterations, output=False, log=False): self.normalize() for itter in range(iterations): for i, gene in enumerate(self.genes): # Save the old output values # We don't want to regulate the input genes if gene.type == "I": continue enh = 0. inh = 0. for j, g_j in enumerate(self.genes): # We don't want to regulate with the output genes if g_j.type == "O": continue enh += gene.enhImpacts[j] * g_j.concentration inh += gene.inhImpacts[j] * g_j.concentration enh /= len(self.genes) inh /= len(self.genes) # print("Gene: {} enh: {} inh: {} impact: {}".format(i, enh, inh, enh-inh)) # update the concentrations impact = gene.concentration * self.delta * (enh - inh) # print("old: {} impact: {} new: {}".format(gene.concentration, impact, gene.concentration + impact)) gene.concentration += impact #ToDo:: I hate this if gene.concentration < 0: gene.concentration = 0 self.normalize() self.mapValues() # ToDo:: Probably need to change this format for logging and outputting for simplifying the network if output: print("Iteration {}".format(itter)) for i, gene in enumerate(self.genes): print("Gene: {} Type: {} Concentration: {}".format(i, gene.type, gene.concentration)) log = True if log: if itter == 0: header = ['time'] for i, gene in enumerate(self.genes): header.append("{}.{}".format(gene.type, i)) LOG = L.Logger("Task", "regulation.csv", header, True) LOG2 = L.Logger("Task", "outputs.csv", header, True) data = "{},".format(itter) data2 = "{},".format(itter) for gene in self.genes: data += "{},".format(gene.concentration) try: data2 += "{},".format(gene.extra[0]) except: raise Exception("can't find extra information about the gene") LOG.logln(data) LOG2.logln(data2)
def regulate(self, iterations, output=False, log=False): self.outputs = [] self.normalize() # The time loop for itter in range(iterations): for i, gene in enumerate(self.genes): # We don't want to regulate the input genes if gene.type == "I": continue enh = 0. inh = 0. for j, g_j in enumerate(self.genes): # We don't want to regulate with the output genes if g_j.type == "O": continue enh += gene.enhImpacts[j] * g_j.concentration inh += gene.inhImpacts[j] * g_j.concentration enh /= (len(self.genes) - self.outputCount) inh /= (len(self.genes) - self.outputCount) # update the concentrations impact = gene.concentration * self.delta * (enh - inh) # print("old: {} impact: {} new: {}".format(gene.concentration, impact, gene.concentration + impact)) gene.concentration += impact if gene.concentration < 0: gene.concentration = 0 self.normalize() # ToDo:: Probably need to change this format for logging and outputting for simplifying the network if output: print("Iteration {}".format(itter)) for i, gene in enumerate(self.genes): print("Gene: {} Type: {} Concentration: {}".format( i, gene.type, gene.concentration)) if log: if itter == 0: header = ['time'] for i, gene in enumerate(self.genes): header.append("{}.{}".format(gene.type, i)) LOG = L.Logger("Task", "regulation.csv", header, True) data = "{},".format(itter) for gene in self.genes: data += "{},".format(gene.concentration) LOG.logln(data)
def regulate(self, iterations, output=False, log=False): self.outputs = [] oldOutputs = [] # ToDo:: Computational cost can be reduced if we save a list of inputs/outputs if self.mode == "binaryIncremental": for gene in self.genes: if gene.type == "O": oldOutputs.append(gene.concentration) elif self.mode == "continuous": pass else: print("ERROR! Mode does not exist!") exit(1) for itter in range(iterations): self.normalize() for i, gene in enumerate(self.genes): # Save the old output values # We don't want to regulate the input genes if gene.type == "I": continue enh = 0. inh = 0. for j, g_j in enumerate(self.genes): # We don't want to regulate with the output genes if g_j.type == "O": continue enh += gene.enhImpacts[j] * g_j.concentration inh += gene.inhImpacts[j] * g_j.concentration enh /= len(self.genes) inh /= len(self.genes) # print("Gene: {} enh: {} inh: {} impact: {}".format(i, enh, inh, enh-inh)) # update the concentrations impact = gene.concentration * self.delta * (enh - inh) # print("old: {} impact: {} new: {}".format(gene.concentration, impact, gene.concentration + impact)) gene.concentration += impact if gene.concentration < 0: gene.concentration = 0 self.normalize() # ToDo:: Probably need to change this format for logging and outputting for simplifying the network if output: print("Iteration {}".format(itter)) for i, gene in enumerate(self.genes): print("Gene: {} Type: {} Concentration: {}".format( i, gene.type, gene.concentration)) if log: if itter == 0: header = ['time'] for i, gene in enumerate(self.genes): header.append("{}.{}".format(gene.type, i)) LOG = L.Logger("Task", "regulation.csv", header, True) data = "{},".format(itter) for gene in self.genes: data += "{},".format(gene.concentration) LOG.logln(data) # ToDo:: To reduce the computational cost, maybe add this to the normalize function? Or even saving a list of outputs might help. if self.mode == "binaryIncremental": outputIndex = -1 for gene in self.genes: if gene.type == "O": outputIndex += 1 # ExpValue: The equal part for not changing is an experimental value. But this makes sense cause it's the same as rounding up maybe? I don't know this at this point. # if doesn't change or increases if gene.concentration >= oldOutputs[outputIndex]: self.outputs.append(1) else: # if becomes less self.outputs.append(0) else: print("ERROR! Mode not coded -.- :( Exiting!") exit(1) pass
def __init__(self, hostIP, hostPort): logMessage = "Initiating socket at " + hostIP +":" + str(hostPort) Logger.Log(0, logMessage) self.socketConn = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.hostIP = hostIP self.hostPort = hostPort