def create_graph(): parser = FileReader.SemEvalParser() sent_ids = parser.get_sentences_ids_by(doc_id) root = GraphManager.Node(doc_id) graph = GraphManager.Graph(root=root) # sent_ids = sent_ids[:2] for sent_id in sent_ids: sent_node = GraphManager.Node(sent_id) graph.add_node(sent_node, root) words = parser.get_instances_by(sentence=sent_id) for node_id, word in words: word_node = GraphManager.WordNode(identifier=node_id, word=word) graph.add_word_node(word_node, sent_node) senses = utils.get_sences(word) for sense in senses: extended_def = utils.get_extended_def(sense) odour = convert_extended_def_to_odour(extended_def) nest = GraphManager.NestNode(identifier=sense, lemma=sense.lemmas()[0].key(), odour=odour) graph.add_nest(nest, word_node) global vocabulary global index del vocabulary del index return graph
def DEPARTURE(self, GM, pk, index, DDMM, packetsize): #current time now = time.time() pnow = time.ctime(now) sourceID = pk.getsourceID() destinationID = pk.getdestinationID() # print('time in departure:', time.ctime(now)) # print("Id packet leaving") pkID = pk.getpacketID() # print(pk.getpacketID()) # print("generated by node ") # print(pk.getsourceID()) #update current node pk.updateCurrentNode(pk.getnextnodeID()) # print("leaving from node ") # print(pk.getCurrentNode()) #update info about the packet in GM # GM.dellstPackets(pk.getCurrentNode(), pk) # #update size of the current node: the packet is leaving # GM.updateSizeQueue(pk.getCurrentNode(), -1) # print("queue size of ") # print(pk.getCurrentNode()) ## print(" is ") # print(GM.showSizeQueue(pk.getCurrentNode())) flowID = pk.getflowID() print("{}-{}\t {} -> {}\t [{}] \t time DEPARTURE: {}".format( pk.getflowID(), pk.getpacketID(), pk.getsourceID(), pk.getdestinationID(), pk.getpath(), pnow)) alivepath = pk.getpath() alivepk = GM.getalivepk() # print('alive packets', alivepk) GM.delalivepk(pk.getflowID(), pk.getpacketID()) alivepk = GM.getalivepk() # print('alive packets', alivepk) countpk = 0 # print('alivepath', alivepath[:-2]) for id in alivepk: if (id[0] == flowID): countpk += 1 if (countpk == 0): GM.delaliveflow(flowID) GM.delalivePaths(alivepath[:-2]) # print('countpk', countpk) DM = DDMM.updateDemandMatrix(sourceID, destinationID, packetsize, -1)
odour1 = np.array([1, 2, 3, 4, 5]) odour2 = np.array([3, 5, 7, 8, 9, 1]) print("SIZE", odour2.size) print(utils.lesk_similarity(odour1, odour2)) # syn = wn.synsets('monkey')[0] # # print(syn.lemmas()[0].key()) d = {("a", "b"): "da"} print(d[('a', 'b')]) a = GraphManager.Node() b = GraphManager.Node() l = [a, b] for node in l: if isinstance(node, GraphManager.NestNode): print(node.lemma) print(set([a, b]) == set([b, a])) print(str(None)) parser = FileReader.SemEvalParser() sent_ids = parser.get_sentences_ids_by('d001') sent = parser.get_instances_by(sentence=sent_ids[0])
def main(argv): input_graph_file = None graph_save_file = None input_params_file = None airport_q = 10 only_generate = False verbosity = 5 data = None graph = None # Parameters params = { 'graph' : graph, 'start_idx' : 1, 'end_idx' : 4, 'max_flights' : 150, 'cost_weight' : 2, 'time_weight' : 1, 'pop_size' : 20, 'generations' : 10, 'mutation_rate' : 0.2, 'tournament_size' : 2, 'elitism' : False, 'dest_min' : 2, 'dest_max' : 5 } # Parse command line options try: opts, args = getopt.getopt(argv, "hi:s:p:a:gv:", ["ifile=", "save=", "params=", "airports=", "verbose=", "start_idx=" , "end_idx=" , "max_flights=", "cost_weight=", "time_weight=", "pop_size=" , "generations=", "mutation_rate=", "tournament_size=", "elitism=", "dest_min=", "dest_max=" ]) except getopt.GetoptError: print_help() sys.exit(2) for opt, arg in opts: if opt == '-h': print_help() sys.exit() elif opt in ("-i", "--ifile"): input_graph_file = arg print('Will read data from: {}'.format(input_graph_file)) elif opt in ("-s", "--save"): graph_save_file = arg print('Will save graph to: {}'.format(graph_save_file)) elif opt in ("-p", "--params"): input_params_file = arg print('Will read params from: {}'.format(input_params_file)) elif opt in ("-a", "--airports"): airport_q = int(arg) print('Number of airports = {}'.format(airport_q)) elif opt in ("-g", "--generate"): only_generate = True print('Will just generate data') elif opt in ("-v", "--verbose"): verbosity = int(arg) print('Verbosity level = {} ({})'.format(verbosity, get_verbosity_info(verbosity))) elif opt in list("--" + x for x in params.keys()): if opt[2:] == 'mutation_rate': params[opt[2:]] = float(arg) elif opt[2:] == 'elitism': params[opt[2:]] = arg.lower() in ['true', 't', 'tak', 'yes', 'y', '1'] else: params[opt[2:]] = int(arg) print('Params are: \n{} \n'.format('\n'.join(' {:{}}: {}'.format(k, len(max(params.keys()))+1, v) for k,v in sorted(params.items())))) # DataGenerator data = DataGenerator() DataGenerator.DESTINATIONS_MIN = params['dest_min'] DataGenerator.DESTINATIONS_MAX = params['dest_max'] if input_graph_file is not None: data.load_saved_graph(input_graph_file) else: data.load_new_data(airport_q) data.create_graph() if graph_save_file is not None: data.save_graph(graph_save_file) testsuite_airports = data.get_airports() testsuite_graph = data.get_graph() graph = GraphManager(params['max_flights']) graph.set_graph(testsuite_graph, testsuite_airports) if verbosity > 1: graph.print_airports() graph.print_flights() graph.print_graph() if not only_generate: print(' === GeneticAlgorithm tests ===') params['graph'] = graph GA.run_with_params(params)
mRefreshDelay = int(mConfig.get('SETTINGS', 'refreshperiod')) mTypeGRF = int(mConfig.get('SETTINGS', 'typegrf')) mScale = int(mConfig.get('SETTINGS', 'scale')) mRecDir = mConfig.get('RECORDS', 'recordsdirectory') mRecRatio = int(mConfig.get('RECORDS', 'recordratio')) sockData = 0 sockConfig = 0 sockConfig2 = 0 mDisplayConnected = False mIpAddr = "" mScreen = Afficheur() mDataManager = DataManager() mGraphManager = GraphManager() def dessineGraph(): mDuree = mScreen.getDuree(mScreen.getScale()) mGraphManager.setDuree(mDuree) mGraphManager.fillGraph(mDataManager.getDataList(mDuree)) mScreen.majTendance(mDataManager.getTendance()) coordList = mGraphManager.getCoords() th_dessineGrf = threading.Thread(target=mScreen.traceGraphe, args=(coordList, )) th_dessineGrf.start() #mScreen.traceGraphe(coordList) mRecord = 0
def GENERATE(self, GM, newpk, DDMM, packetsize): """ event called when a new packet is generated. It triggers - a migration event if the next node is not the destination - a departure event if the next node is the destination one of this two events will occur after a specific time interval. This time interval = elaboration time + transmission time from the current node to the next node Args: GM = newpk = Returns: """ # print("generate") # if(Topology[n].getSizeQueue() == 1) serve that packet in the queue now = time.time() pnow = time.ctime(now) # print("Id packet arriving") # print(pk.getpacketID()) pkID = newpk.getpacketID() sourceID = newpk.getsourceID() destinationID = newpk.getdestinationID() path = newpk.getpath() nextnodeID = path[1] newpk.updateNextNode(pkID, nextnodeID) print("{}-{}\t {} -> {}\t [{}] -> {} \t time GENERATION: {}".format( newpk.getflowID(), pkID, sourceID, destinationID, path, newpk.getnextnodeID(), pnow)) # newpk = packet(pkID, sourceID , destinationID) GM.updatelstPackets(sourceID, newpk) index = 1 GM.updateSizeQueue(sourceID, 1) nodeA = sourceID if (nextnodeID < sourceID): nodeA = nextnodeID nodeB = sourceID else: nodeB = nextnodeID transmissionTime = random.uniform( 1, 3) #GM.getDelayEdges(nodeA, nodeB)#30#evaluateDelay() # print('size', GM.showSizeQueue(newpk.getCurrentNode())) # #when the packet arrive to the first desitation nodenode # self.scheduler.enter(transmissionTime, 1, self.ARRIVAL, (GM, newpk, destinationID)) if (GM.showSizeQueue(newpk.getCurrentNode()) >= 1): #serve that packet in the queue # print('len path', len(path)) if (index < len(path) - 3): # print("migrate from gen") serviceTime = 60 #exponential(1) tempo = transmissionTime + pkID print('migrazione dopo ', tempo) Timer(tempo, self.MIGRATE, (GM, newpk, index, DDMM, packetsize)).start() else: tempo = transmissionTime + pkID Timer(tempo, self.DEPARTURE, (GM, newpk, index, DDMM, packetsize)).start() #devo considerare ogni volta il nodo successivo #per trovare il percorso del pacchetto successivo devo considerare i percorsi dei pacchetti ancora nel sistema generationTime = 1 #exponential(1)
def MIGRATE(self, GM, newpk, index, DDMM, packetsize): #current time now = time.time() pnow = time.ctime(now) # print('time MIGRATION:', time.ctime(now)) #when the migration occurs, the packet moves from currentNode to nextNode #therefore, the queue size of currentNode decreases GM.updateSizeQueue(newpk.getCurrentNode(), -1) #update packet and oldnode information in GM GM.dellstPackets(newpk.getCurrentNode(), newpk) # print("previous node") # print(newpk.getCurrentNode()) #nextNode is the new current node currentNode = newpk.getnextnodeID() # print("current node") # print(currentNode) #the queue size of the new currentNode increase GM.updateSizeQueue(currentNode, 1) #update packet and currentnode information in GM GM.updatelstPackets(currentNode, newpk) #update new current node of the packet newpk.updateCurrentNode(newpk.getnextnodeID()) #update new nextnode path = newpk.getpath() # print("path") # print(path) # print('index', index) newindex = index + 1 newNextNode = path[newindex] newpk.updateNextNode(newpk.getpacketID(), newNextNode) # print("sourceID") # print(newpk.getsourceID()) # print("destination") # print(newpk.getdestinationID()) # print("pkID") # print(newpk.getpacketID()) # print('next node', newNextNode) # print("current node queue size", GM.showSizeQueue(currentNode)) print("{}-{}\t {} -> {}\t [{}] -> {} \t time MIGRATION: {}".format( newpk.getflowID(), newpk.getpacketID(), newpk.getsourceID(), newpk.getdestinationID(), path, newNextNode, pnow)) nodeA = currentNode if (newNextNode < currentNode): nodeA = newNextNode nodeB = currentNode else: nodeB = newNextNode transmissionTime = random.uniform( 1, 3) #GM.getDelayEdges(nodeA, nodeB)#30#evaluateDelay() # pk.removePacket(packetID) if (GM.showSizeQueue(currentNode) >= 1): #serve the next packet in the queue if (newNextNode == newpk.getdestinationID()): Timer(transmissionTime, self.DEPARTURE, (GM, newpk, newindex, DDMM, packetsize)).start() else: Timer(transmissionTime, self.MIGRATE, (GM, newpk, newindex, DDMM, packetsize)).start()
def main(): parser = argparse.ArgumentParser( description= 'Simulate run of routing strategies\n\n python RL.py <EPISODES> <GAMMA> <EPOCH> <BATCHSIZE> <BUFFER> <TOPOLOGY> <TSIM> <ROUTING> \n \n === topologies options === \n [3cycle] \n [4cycle] \n [4diag] \n [6s4hMultiSwitch] \n [Aarnet] \n [Abilene] \n \n === routing options ===\n[-rl] \t\t run reinforcement learning \n[-so] \t\t run semi-oblivious \n[-ecmp] \t run ecmp \n[-ospf] \t run ospf', formatter_class=RawTextHelpFormatter) parser.add_argument( 'episodes', type=int, help='Number of episodes to train the Reinforcement Learning algorith') parser.add_argument('gamma', type=float, help='Discount factor') parser.add_argument('epochs', type=int, help='Number of epochs') parser.add_argument('batchSize', type=int, help='batch size of the Experience Replay buffer') parser.add_argument( 'buffer', type=int, help='size of the replay buffer (for the Experience Replay)') parser.add_argument('topology', type=str, help='Simulation topology') parser.add_argument('simTime', type=int, help='Simulation Time') parser.add_argument('routing', type=str, help='Routing protocol') args = parser.parse_args() if args.gamma > 1 or args.gamma < 0: parser.error("gamma cannot be larger than 1 and smaller than 0") parameters = [ args.episodes, args.gamma, args.epochs, args.batchSize, args.buffer, args.routing, args.simTime ] print('parameters', parameters) initialnFlow = 3 #10 #maximum number of flows that can coexist in the system (for the training) # RL1 = ReinforcementLearning(initialnFlow, 0) # model, nNode, points_list, bandwidth_list, delay_list = RL1.training(parameters) dotFormat = './topologies/' + args.topology + '.dot' G = nx_agraph.from_agraph(pygraphviz.AGraph(dotFormat)) # pos = nx.spring_layout(G) # nx.draw_networkx_nodes(G,pos) # nx.draw_networkx_edges(G,pos) # nx.draw_networkx_labels(G,pos) labels = nx.get_edge_attributes(G, 'weight') #topology parameters node_name = nx.get_node_attributes(G, 'type') print('lables', node_name) nNode = nx.number_of_nodes(G) print('nnode', nNode) nHost = 0 print('nodi: ', nNode) for attr in node_name: # print('attr', attr) if (attr[0] == 'h'): nHost += 1 nSwitch = nNode - nHost # print('nhost, nswitch', nHost, nSwitch) #newgraph with nodes' name from 0 to nNode-1 myG = G mapping = {} cuonter_h = 1 cuonter_s = 1 num_h = 0 num_s = num_h + nHost index = 0 while index < nNode: for n in G.nodes(): if (n[0] == 's' and n[1:] == str(cuonter_s)): mapping[n] = num_s cuonter_s += 1 num_s += 1 index += 1 elif (n[0] == 'h' and n[1:] == str(cuonter_h)): mapping[n] = num_h cuonter_h += 1 num_h += 1 index += 1 myG = nx.relabel_nodes(G, mapping) model = None RL1 = None if args.routing == 'rl': #trainig RL!! activeFlows = initialnFlow - 1 RL1 = ReinforcementLearning(initialnFlow, myG, 0) model = RL1.training(parameters, nNode, myG, nHost, activeFlows) # nx.draw_networkx_edge_labels(G,pos,edge_labels=labels) # plt.show() #save name #save_path = 'C:/example/' #name_of_file = raw_input("What is the name of the file: ") #completeName = os.path.join(save_path, name_of_file+".txt") #file1 = open(completeName, "w") # toFile = raw_input(model) # file1.write(toFile) GM = GraphManager(nNode) #start generation packet and normal simulation packetsGen = packetsGeneration(initialnFlow) failedRL = packetsGen.generation(GM, nNode, nHost, nSwitch, myG, model, parameters, RL1, dotFormat)
def __init__(self, row, column, inputData = []): self.connector = GraphManager() self.inputData = inputData self.outputData = [] self.row = row self.col = column
class DataManager: def __init__(self, row, column, inputData = []): self.connector = GraphManager() self.inputData = inputData self.outputData = [] self.row = row self.col = column def generateZeroMatrix(self): newMatrix = [[0 for x in range(self.row*self.col)] for y in range(self.col*self.row)] return newMatrix def setup(self): adjcencyMatrix = self.generateZeroMatrix() freeNode = [] parkingNode = [] exitNode = [] newMatrixRow = -1 for i in range(0,self.row): for j in range(0,self.col): newMatrixRow+=1 #check for restricted block if(self.inputData[i][j] > 5 or self.inputData[i][j] < 0): raise InvalidDataFormat() if(self.inputData[i][j] == 2): parkingNode.append(newMatrixRow) elif(self.inputData[i][j] == 5): exitNode.append(newMatrixRow) if(self.inputData[i][j] == 0 or self.inputData[i][j] == 2 or self.inputData[i][j] == 3): #check-left if(j-1 >= 0): if(self.inputData[i][j-1] == 0 or self.inputData[i][j-1] == 2 or self.inputData[i][j-1] == 3 or self.inputData[i][j-1] == 5): adjcencyMatrix[newMatrixRow][newMatrixRow-1] = 1 #check-right if(j+1 <= col-1): if(self.inputData[i][j+1] == 0 or self.inputData[i][j+1] == 2 or self.inputData[i][j+1] == 3 or self.inputData[i][j+1] == 5): adjcencyMatrix[newMatrixRow][newMatrixRow+1] = 1 #check-up if(i-1 >= 0): if(self.inputData[i-1][j] == 0 or self.inputData[i-1][j] == 2 or self.inputData[i-1][j] == 3 or self.inputData[i-1][j] == 5): adjcencyMatrix[newMatrixRow][newMatrixRow-col] = 1 #check-down if(i+1 <= row-1): if(self.inputData[i+1][j] == 0 or self.inputData[i+1][j] == 2 or self.inputData[i+1][j] == 3 or self.inputData[i+1][j] == 5): adjcencyMatrix[newMatrixRow][newMatrixRow+col] = 1 self.connector.setGraph(adjcencyMatrix, parkingNode, exitNode) def findFastestParkingRoute(self, row, col): #print((row * self.row) + col) self.outputData = self.connector.park(((row * self.row) + col)) if self.outputData == []: return [] return self.returnTuple() def findFastestExitRoute(self, row, col): #print((row * self.row) + col) self.outputData = self.connector.autoexit(((row * self.row) + col)) if self.outputData == []: return [] return self.returnTuple() def returnTuple(self): listTuple = [] for i in self.outputData: x = i//self.row y = i%self.col listTuple.append((x,y)) return listTuple def getLenPark(self): return self.connector.getLenPark()
def main(argv): input_graph_file = None graph_save_file = None input_params_file = None airport_q = 10 only_generate = False verbosity = 5 data = None graph = None # Parameters params = { 'graph': graph, 'start_idx': 1, 'end_idx': 4, 'max_flights': 150, 'cost_weight': 2, 'time_weight': 1, 'pop_size': 20, 'generations': 10, 'mutation_rate': 0.2, 'tournament_size': 2, 'elitism': False, 'dest_min': 2, 'dest_max': 5 } # Parse command line options try: opts, args = getopt.getopt(argv, "hi:s:p:a:gv:", [ "ifile=", "save=", "params=", "airports=", "verbose=", "start_idx=", "end_idx=", "max_flights=", "cost_weight=", "time_weight=", "pop_size=", "generations=", "mutation_rate=", "tournament_size=", "elitism=", "dest_min=", "dest_max=" ]) except getopt.GetoptError: print_help() sys.exit(2) for opt, arg in opts: if opt == '-h': print_help() sys.exit() elif opt in ("-i", "--ifile"): input_graph_file = arg print('Will read data from: {}'.format(input_graph_file)) elif opt in ("-s", "--save"): graph_save_file = arg print('Will save graph to: {}'.format(graph_save_file)) elif opt in ("-p", "--params"): input_params_file = arg print('Will read params from: {}'.format(input_params_file)) elif opt in ("-a", "--airports"): airport_q = int(arg) print('Number of airports = {}'.format(airport_q)) elif opt in ("-g", "--generate"): only_generate = True print('Will just generate data') elif opt in ("-v", "--verbose"): verbosity = int(arg) print('Verbosity level = {} ({})'.format( verbosity, get_verbosity_info(verbosity))) elif opt in list("--" + x for x in params.keys()): if opt[2:] == 'mutation_rate': params[opt[2:]] = float(arg) elif opt[2:] == 'elitism': params[opt[2:]] = arg.lower() in [ 'true', 't', 'tak', 'yes', 'y', '1' ] else: params[opt[2:]] = int(arg) print('Params are: \n{} \n'.format('\n'.join( ' {:{}}: {}'.format(k, len(max(params.keys())) + 1, v) for k, v in sorted(params.items())))) # DataGenerator data = DataGenerator() DataGenerator.DESTINATIONS_MIN = params['dest_min'] DataGenerator.DESTINATIONS_MAX = params['dest_max'] if input_graph_file is not None: data.load_saved_graph(input_graph_file) else: data.load_new_data(airport_q) data.create_graph() if graph_save_file is not None: data.save_graph(graph_save_file) testsuite_airports = data.get_airports() testsuite_graph = data.get_graph() graph = GraphManager(params['max_flights']) graph.set_graph(testsuite_graph, testsuite_airports) if verbosity > 1: graph.print_airports() graph.print_flights() graph.print_graph() if not only_generate: print(' === GeneticAlgorithm tests ===') params['graph'] = graph GA.run_with_params(params)
class DataManager: def __init__(self, row, column, inputData=[]): self.connector = GraphManager() self.inputData = inputData self.outputData = [] self.row = row self.col = column def generateZeroMatrix(self): newMatrix = [[0 for x in range(self.row * self.col)] for y in range(self.col * self.row)] return newMatrix def setup(self): adjcencyMatrix = self.generateZeroMatrix() freeNode = [] exitNode = [] newMatrixRow = -1 for i in range(0, self.row): for j in range(0, self.col): newMatrixRow += 1 #check for restricted block if (self.inputData[i][j] == 2 or self.inputData[i][j] == 3): freeNode.append(newMatrixRow) ## if(self.inputData[i][j] == 2): ## freeNode.append(newMatrixRow) ## elif(self.inputData[i][j] == 3): ## exitNode.append(newMatrixRow) if (self.inputData[i][j] == 0 or self.inputData[i][j] == 2 or self.inputData[i][j] == 3): #check-left if (j - 1 >= 0): if (self.inputData[i][j - 1] == 0 or self.inputData[i][j - 1] == 2 or self.inputData[i][j - 1] == 3): adjcencyMatrix[newMatrixRow][newMatrixRow - 1] = 1 #check-right if (j + 1 <= col - 1): if (self.inputData[i][j + 1] == 0 or self.inputData[i][j + 1] == 2 or self.inputData[i][j + 1] == 3): adjcencyMatrix[newMatrixRow][newMatrixRow + 1] = 1 #check-up if (i - 1 >= 0): if (self.inputData[i - 1][j] == 0 or self.inputData[i - 1][j] == 2 or self.inputData[i - 1][j] == 3): adjcencyMatrix[newMatrixRow][newMatrixRow - col] = 1 #check-down if (i + 1 <= row - 1): if (self.inputData[i + 1][j] == 0 or self.inputData[i + 1][j] == 2 or self.inputData[i + 1][j] == 3): adjcencyMatrix[newMatrixRow][newMatrixRow + col] = 1 print(freeNode) #self.connector.setGraph(adjcencyMatrix, freeNode ,exitNode) self.connector.setGraph(adjcencyMatrix, freeNode) def findFastestRoute(self, a=601, b=623, status='enter'): self.outputData = self.connector.getPath(a, b, status) #print(self.outputData) return self.returnTuple() def returnTuple(self): listTuple = [] for i in self.outputData: x = i // self.row y = i % self.col listTuple.append((x, y)) return listTuple