def nextNode(self, node, visited): """ Finds next node from the node in argument """ if len(visited) > self.size: # the length of the trip before the last step cannot be greater than the number of nodes ET.error('nextNode', 'Unexpected behavior') return None elif len(visited) == self.size: # the trip ends so we go to the first node return visited[0] else: attractions = [] for n in self.nodes: if n not in visited: if Gbl.Parameter('PheromoneInfluence') > 0: pheromoneInfluence = self.pheromones.getValue(node, n) ** Gbl.Parameter('PheromoneInfluence') else: pheromoneInfluence = 1 if Gbl.Parameter('DistanceInfluence') > 0: distanceInfluence = self.distances.getValue(node, n) ** Gbl.Parameter('DistanceInfluence') else: distanceInfluence = 1 attractions.append((pheromoneInfluence / distanceInfluence, n)) return max(attractions)[1]
def DNAfill(self, Nucleotides): " fills the DNA with given Nucleotides " self.__dna = Nucleotides[:] # important: make a ground copy if len(Nucleotides) > 0 and len(Nucleotides) != self.nb_nucleotides: Tools.error('DNA: initialization','Provided genome length does not match gene map') if len(Nucleotides) > 0 and not set(Nucleotides) <= set([0,1]): Tools.error('DNA: initialization','Provided genome is not binary')
def __init__(self, Size=100, nbNodes=0): #self.TestMessages = [] # Messages used to test the efficiency of the network margin = 5 if Size > 20 else 0 self.nodes = [] self.currentLength = 0 if Gbl.Parameter('RandomNetwork') and nbNodes > 1: self.nodes = [Node('N%d' % i, (random.randint(margin, Size-margin), random.randint(margin, Size-margin))) for i in range(nbNodes)] else: # loading network from file # file format: # Name1 x1 y1 # Name2 x2 y2 # ... try: for Line in open(Gbl.Parameter('NetworkFileName'), 'r', 1): # read one line at a time NodeDef = Line.split() self.nodes.append(Node(NodeDef[0], tuple(map(int, NodeDef[1:])))) except IOError: ET.error('Unable to find Network description', Gbl.Parameter('NetworkFileName')) self.size = len(self.nodes) for n in self.nodes: print "%s %d %d" % (n.name, n.getX(), n.getY()) self.distances = Distances(self.nodes) self.pheromones = Pheromones(self.nodes)
def __init__(self, Size=100, NbNodes=0, NetworkFileName=None): self.TestMessages = [ ] # Messages used to test the efficiency of the network margin = 5 if Size > 20 else 0 self.NodeNames = {} self.Nodes = [] self.Links = [] if Gbl.Parameter('RandomNetwork') and NbNodes > 1: self.Nodes = [ Node('N%d' % i, (random.randint(margin, Size - margin), random.randint(margin, Size - margin))) for i in range(NbNodes) ] if NbNodes > 1: for n in self.Nodes: OtherNodes = self.Nodes[:] OtherNodes.remove(n) # does not need to be efficient if OtherNodes: self.Links.append((n, random.choice(OtherNodes))) self.Links.append((n, random.choice(OtherNodes))) self.Links = list(set(self.Links)) # to remove duplicates self.NodeNames = dict([(n.Name, n) for n in self.Nodes]) else: # loading network from file # file format: # Name1 x1 y1 # Name2 x2 y2 # ... # Link NameA NameB C (C = capacity) # ... # ('Link' = keyword) LinkDefinitions = [] try: for Line in open(Gbl.Parameter('NetworkFileName'), 'r', 1): # read one line at a time Link = re.match('(?i)link\s+(.*)', Line) if Link is not None: LinkDefinitions.append(Link.group(1).split()) elif Line: NodeDef = Line.split() self.Nodes.append( Node(NodeDef[0], tuple(map(int, NodeDef[1:])))) self.NodeNames[NodeDef[0]] = self.Nodes[-1] except IOError: ET.error('Unable to find Network description', Gbl.Parameter('NetworkFileName')) for L in LinkDefinitions: self.Links.append(( self.NodeNames[L[0]], self.NodeNames[L[1]], )) for N in self.Nodes: print(N) self.Size = len(self.Nodes) self.TestMessages = [Message('M1', self.Nodes[0], self.Nodes[1])] # Creating routing tables for n in self.Nodes: n.CreateTable(self.Nodes, self.Neighbours(n))
def __init__(self, Size=100, NbNodes=0, NetworkFileName=None): self.TestMessages = [] # Messages used to test the efficiency of the network margin = 5 if Size > 20 else 0 self.NodeNames = {} self.Nodes = [] self.Links = [] if Gbl.Parameter('RandomNetwork') and NbNodes > 1: self.Nodes = [Node('N%d' % i, (random.randint(margin, Size-margin), random.randint(margin, Size-margin))) for i in range(NbNodes)] if NbNodes > 1: for n in self.Nodes: OtherNodes = self.Nodes[:] OtherNodes.remove(n) # does not need to be efficient if OtherNodes: self.Links.append((n, random.choice(OtherNodes))) self.Links.append((n, random.choice(OtherNodes))) self.Links = list(set(self.Links)) # to remove duplicates self.NodeNames = dict([(n.Name, n) for n in self.Nodes]) else: # loading network from file # file format: # Name1 x1 y1 # Name2 x2 y2 # ... # Link NameA NameB C (C = capacity) # ... # ('Link' = keyword) LinkDefinitions = [] try: for Line in open(Gbl.Parameter('NetworkFileName'), 'r', 1): # read one line at a time Link = re.match('(?i)link\s+(.*)', Line) if Link is not None: LinkDefinitions.append(Link.group(1).split()) elif Line: NodeDef = Line.split() self.Nodes.append(Node(NodeDef[0], tuple(map(int, NodeDef[1:])))) self.NodeNames[NodeDef[0]] = self.Nodes[-1] except IOError: ET.error('Unable to find Network description', Gbl.Parameter('NetworkFileName')) for L in LinkDefinitions: self.Links.append((self.NodeNames[L[0]],self.NodeNames[L[1]],)) for N in self.Nodes: print N self.Size = len(self.Nodes) self.TestMessages = [Message('M1', self.Nodes[0], self.Nodes[1])] # Creating routing tables for n in self.Nodes: n.CreateTable(self.Nodes, self.Neighbours(n))
def read_DNA(self, start, end, coding = None): " reads a chunk of DNA " if coding == None: coding = self.Scenario.Parameter('GeneCoding') if coding in range(-1,3): # old numeric designation of coding coding = ['NoCoding', 'Weighted', 'Unweighted', 'Gray'][coding+1] value = 0 if coding == 'NoCoding': return 0 if coding not in ['Weighted', 'Unweighted', 'Gray']: Tools.error("DNA", 'unknown binary coding mode') try: for pos in range(start,end): if coding == 'Unweighted': value += self.__dna[pos] else: # Weighted or Gray ## value += self.__dna[pos]* 2 ** (end - 1 - pos) value += (self.__dna[pos] << (end - 1 - pos)) if coding == 'Gray': value = Tools.GrayTable.Gray2Int(value) return(value) except IndexError: Tools.error("DNA", "reading outside the DNA")
def recordChanges(self, Info, Slot='Positions'): # stores current changes # Info is a couple (InfoName, Position) and Position == (x,y) or a longer tuple if Slot == 'Positions': self.Positions.append(Info) elif Slot == 'Trajectories': self.Trajectories.append(Info) else: ET.error('Antnet Observer', 'unknown slot')
def Neighbours(self, Node): if not Node in self.Nodes: ET.error('Network', 'Non existing node has no neighbours') return [n for n in self.Nodes if (Node, n) in self.Links]