def __init__(self, number_state_vectors, number_hidden_nodes_per_layer, exploration_type, epsilon, topo): Routing.__init__(self, topo) self.topo = topo self.QLearner = LearnSDN( self.topo.numOfCores, self.topo.numOfToRs, self.topo.numOfToRs, number_state_vectors * 2 * self.topo.numOfToRs * self.topo.numOfCores, number_hidden_nodes_per_layer, activation_function_type, exploration_type, epsilon, alpha, gamma, beta, learning_rate, learning_method)
class Allocator(object): def __init__(self): self.topo.numOfCores = 2 self.topo.numOfToRs = 4 self.topo.numOfServers = 4 self.QLearner = LearnSDN(self.topo.numOfCores, self.topo.numOfToRs, self.topo.numOfToRs, 2 * self.topo.numOfToRs * self.topo.numOfCores, number_hidden_nodes_per_layer, activation_function_type, exploration_type, epsilon, alpha, gamma, beta, learning_rate, learning_method) def select_action(self, state, source_ip, destination_ip): return self.QLearner.select_action(state, source_ip, destination_ip) def update(self, previous_state, source_ip, destination_ip, previous_action, current_state, reward): return self.QLearner.update(previous_state, source_ip, destination_ip, previous_action, current_state, reward) def placeFlow(self, fid, paths, flowdb): action = self.findPath(fid, paths, flowdb) path = self.convert_to_core(action) flowdb.setPath(fid, path) return flowdb def findPath(self, paths, flowdb): return self.select_action() def placeAllCoflows(self, paths, flowdb): cids = flowdb.getAllCoflow() for c in cids: coflow = flowdb.getAllInCoflow(c) pathCount = dict(zip(paths, len(paths) * [0])) for f in coflow: path = self.findPath(paths, flowdb) pathCount[path] += 1 mostChosenPath = max(pathCount, key=pathCount.get) for f in coflow: return self.placeFlow(f['id'], mostChosenPath, flowdb) def genCandidatePaths(self, paths, flowdb): # use q learning to generate candidate paths # interface with RL module # TODO: add RL support. NOTE: there is also a pathing function in Controller.py return paths def convert_to_core(self, action): if action == 0: return "192.168.1.208" if action == 1: return "192.168.1.209" print "error"
def __init__(self, number_state_vectors, number_hidden_nodes_per_layer, exploration_type, epsilon, topo): Routing.__init__(self, topo) self.topo = topo self.QLearner = LearnSDN(self.topo.numOfCores, self.topo.numOfToRs, self.topo.numOfToRs, number_state_vectors * 2 * self.topo.numOfToRs * self.topo.numOfCores, number_hidden_nodes_per_layer, activation_function_type, exploration_type, epsilon, alpha, gamma, beta, learning_rate, learning_method)
def __init__(self): self.topo.numOfCores = 2 self.topo.numOfToRs = 4 self.topo.numOfServers = 4 self.QLearner = LearnSDN(self.topo.numOfCores, self.topo.numOfToRs, self.topo.numOfToRs, 2 * self.topo.numOfToRs * self.topo.numOfCores, number_hidden_nodes_per_layer, activation_function_type, exploration_type, epsilon, alpha, gamma, beta, learning_rate, learning_method)
class Qlearning(Routing): """ This routing approach is specific for spine-leaf topology """ def __init__(self, number_state_vectors, number_hidden_nodes_per_layer, exploration_type, epsilon, topo): Routing.__init__(self, topo) self.topo = topo self.QLearner = LearnSDN( self.topo.numOfCores, self.topo.numOfToRs, self.topo.numOfToRs, number_state_vectors * 2 * self.topo.numOfToRs * self.topo.numOfCores, number_hidden_nodes_per_layer, activation_function_type, exploration_type, epsilon, alpha, gamma, beta, learning_rate, learning_method) def BuildAllPath(self): self.CalculateAllPath() def BuildPath(self, srcId, dstId, flow, state): self.CalculatePath(srcId, dstId, flow, state) def CalculateAllPath(self): """ This function calculate path between each pair of servers with ECMP For spine-leaf, choosing a path is essentially choosing a spine to traverse """ for srcId in range(self.numOfServers): # gc.collect() for dstId in range(self.topo.numOfServers): self.CalculatePath(srcId=srcId, dstId=dstId) def select_action(self, state, source_id, destination_id): # randomly choose a core switch. Delete this after implementing Qlearning algorithm source = source_id - self.topo.numOfServers destination = destination_id - self.topo.numOfServers action = self.QLearner.select_action(state, source, destination) action_id = self.topo.numOfServers + self.topo.numOfToRs + action return action_id def update(self, previous_state, source_id, destination_id, previous_action_id, current_state, reward): source = source_id - self.topo.numOfServers destination = destination_id - self.topo.numOfServers previous_action = previous_action_id - self.topo.numOfServers - self.topo.numOfToRs return self.QLearner.update(previous_state, source, destination, previous_action, current_state, reward) def CalculatePath(self, srcId, dstId, flow, state): # only self id is contained, if destination is self if srcId == dstId: self.pathList[srcId, dstId] = [srcId] return srcToRId = self.topo.numOfServers + srcId / self.topo.serverPerRack dstToRId = self.topo.numOfServers + dstId / self.topo.serverPerRack # if src and dst are in the same tor switch if srcToRId == dstToRId: self.pathList[srcId, dstId] = [srcId, srcToRId, dstId] return # src-dst must traverse core else: self.pathList[srcId, dstId] = [ srcId, srcToRId, self.select_action(state, srcToRId, dstToRId), dstToRId, dstId ] def __del__(self): pass
class Qlearning(Routing): """ This routing approach is specific for spine-leaf topology """ def __init__(self, number_state_vectors, number_hidden_nodes_per_layer, exploration_type, epsilon, topo): Routing.__init__(self, topo) self.topo = topo self.QLearner = LearnSDN(self.topo.numOfCores, self.topo.numOfToRs, self.topo.numOfToRs, number_state_vectors * 2 * self.topo.numOfToRs * self.topo.numOfCores, number_hidden_nodes_per_layer, activation_function_type, exploration_type, epsilon, alpha, gamma, beta, learning_rate, learning_method) def BuildAllPath(self): self.CalculateAllPath() def BuildPath(self, srcId, dstId, flow, state): self.CalculatePath(srcId, dstId, flow, state) def CalculateAllPath(self): """ This function calculate path between each pair of servers with ECMP For spine-leaf, choosing a path is essentially choosing a spine to traverse """ for srcId in range(self.numOfServers): # gc.collect() for dstId in range(self.topo.numOfServers): self.CalculatePath(srcId=srcId, dstId=dstId) def select_action(self, state, source_id, destination_id): # randomly choose a core switch. Delete this after implementing Qlearning algorithm source = source_id - self.topo.numOfServers destination = destination_id - self.topo.numOfServers action = self.QLearner.select_action(state, source, destination) action_id = self.topo.numOfServers + self.topo.numOfToRs + action return action_id def update(self, previous_state, source_id, destination_id, previous_action_id, current_state, reward): source = source_id - self.topo.numOfServers destination = destination_id - self.topo.numOfServers previous_action = previous_action_id - self.topo.numOfServers - self.topo.numOfToRs return self.QLearner.update(previous_state, source, destination, previous_action, current_state, reward) def CalculatePath(self, srcId, dstId, flow, state): # only self id is contained, if destination is self if srcId == dstId: self.pathList[srcId, dstId] = [srcId] return srcToRId = self.topo.numOfServers + srcId / self.topo.serverPerRack dstToRId = self.topo.numOfServers + dstId / self.topo.serverPerRack # if src and dst are in the same tor switch if srcToRId == dstToRId: self.pathList[srcId, dstId] = [srcId, srcToRId, dstId] return # src-dst must traverse core else: self.pathList[srcId, dstId] = [srcId, srcToRId, self.select_action(state, srcToRId, dstToRId), dstToRId, dstId] def __del__(self): pass