def switch_partition_and_controller_deployment(self,): assert self.pn assert self.level assert self.network assert self.network.sn assert self.network.s_wei assert self.network.path_cost #输入 sn = self.network.sn pn = self.pn s_wei = self.network.s_wei l_wei = self.network.l_wei path_cost = self.network.path_cost #划分算法开始 #1.粗化 #NULL #2.初始划分 partition = self.initial_partition(s_wei, l_wei, path_cost, 0, 1)#迭代二分当前层数0,区域编号从1开始 #3.细化 #NULL #划分算法结束 #放置算法开始 s_wei_2 = get_s_wei_2(l_wei, partition) #获取part_no part_no = {} #无法保证分区数量为pn #for i in range(sn): # part_no[partition[i]] = partition[i] #################### #修改如下 for i in xrange(pn): part_no[i+pn] = i+pn ################### ctr_place = {} part_cost = {} for pno in part_no.keys(): ctr_place[pno] = -1 part_cost[pno] = 0 for c_part_no in part_no.keys(): res = get_child_network(s_wei,s_wei_2,l_wei,path_cost,partition,c_part_no) if res == None: ctr_place[c_part_no] = -1 part_cost[c_part_no] = 0 continue c_s_wei = res[0] c_l_wei = res[1] c_path_cost = res[2] c_index = res[3] res = self.controller_deployment(c_s_wei,c_l_wei,c_path_cost) ctr_i = res[0] ctr_place[c_part_no] = c_index[ctr_i] part_cost[c_part_no] = res[1] tmp_res = tool.get_res(s_wei, l_wei, path_cost, partition, ctr_place) part_s_num = tmp_res[0] part_s_wei = tmp_res[1] inter_traffic = tmp_res[2] res = Result(self.network, self, pn, partition, ctr_place, part_cost, part_s_num, part_s_wei, inter_traffic) return res
def initial_partition(self, s_wei, l_wei, path_cost, level, part_no): #数据合法性检验 assert self.level > 0 assert level >= 0 and level <= self.level sn = len(s_wei) assert sn > 0 assert len(l_wei) == sn for i in range(sn): assert len(l_wei[i]) == sn part = [part_no]*sn #最后一层,无需再划分 if level == self.level: return part #如果只有一个交换机,无需再划分 if sn == 1: part[0] = part_no*(2**(self.level-level)) return part ############### ''' print '----------------l_wei-----------------------' for i in xrange(sn): print '%2d '%i, print '' print '--------------------------------------------' for i in xrange(sn): for j in xrange(sn): print '%2d '%l_wei[i][j], print '' print '----------------s_wei-----------------------' for i in xrange(sn): print '%2d '%i, print '' print '--------------------------------------------' for i in xrange(sn): print '%2d '%s_wei[i], print '' ''' ############### #get bipartition of graph #获取两个子分区 # 1 # / \ # / \ # 2 3 # / \ / \ # 4 5 6 7 #/ \ / \ / \ / \ #8 9 10 11 12 13 14 15 lc_part_no = part_no*2 #left child part rc_part_no = part_no*2 + 1 #right child part s_wei_2 = [0]*sn edge_cut = sys.maxint index = -1 for i in xrange(50): tmp_part = tool.randomly_get_bipartition(s_wei, l_wei, lc_part_no, rc_part_no) #part = tmp_part #break #print 'part.len=%d'%len(part) #微调左右part tmp_part = tool.kernighan_lin_algorithm(s_wei, l_wei, tmp_part, lc_part_no, rc_part_no) tmp_edge_cut = 0 tmp_s_wei_2 = get_s_wei_2(l_wei, tmp_part) for j in range(sn): tmp_edge_cut += tmp_s_wei_2[j] ''' print 'i=%d, tmp_edge_cut=%d'%(i, tmp_edge_cut) print 'tmp_part array' for i in range(sn): print '%d '%tmp_part[i], print '' ''' if tmp_edge_cut < edge_cut: edge_cut = tmp_edge_cut s_wei_2 = tmp_s_wei_2 part = tmp_part index = i #print 'index = %d'%index ''' #left part of bipart #right part of bipart #统计左右分区交换机个数 lc_s_num = 0 rc_s_num = 0 for i in range(sn): if part[i] == lc_part_no: lc_s_num +=1 else:# if part[i] == rc_part_no: rc_s_num +=1 #print 'lc_s_num=%d,rc_s_num=%d'%(lc_s_num,rc_s_num) lc_index = [0]*lc_s_num rc_index = [0]*rc_s_num lc_s_wei = [0]*lc_s_num rc_s_wei = [0]*rc_s_num lc_l_wei = [[0 for col in xrange(lc_s_num)] for row in xrange(lc_s_num)] rc_l_wei = [[0 for col in xrange(rc_s_num)] for row in xrange(rc_s_num)] i0 = 0 i1 = 0 for i in range(sn): if part[i] == lc_part_no: lc_index[i0] = i lc_s_wei[i0] = s_wei[i] + s_wei_2[i] i0 += 1 else:# if part[i] == rc_part_no: rc_index[i1] = i rc_s_wei[i1] = s_wei[i] + s_wei_2[i] i1 += 1 #复杂度:O(switch_number^2) #lc_l_wei for i in xrange(lc_s_num): ii = lc_index[i] for j in xrange(lc_s_num): jj = lc_index[j] lc_l_wei[i][j] = l_wei[ii][jj] #rc_l_wei for i in xrange(rc_s_num): ii = rc_index[i] for j in xrange(rc_s_num): jj = rc_index[j] rc_l_wei[i][j] = l_wei[ii][jj] ''' ##################### lc_net = get_child_network(s_wei, s_wei_2, l_wei, path_cost, part, lc_part_no) lc_s_wei = lc_net[0] lc_l_wei = lc_net[1] lc_path_cost = lc_net[2] lc_index = lc_net[3] rc_net = get_child_network(s_wei, s_wei_2, l_wei, path_cost, part, rc_part_no) rc_s_wei = rc_net[0] rc_l_wei = rc_net[1] rc_path_cost = rc_net[2] rc_index = rc_net[3] #################### part_0 = self.initial_partition(lc_s_wei, lc_l_wei, lc_path_cost, level+1, lc_part_no) #print 'part.len=%d,part_0.len=%d'%(len(part),len(part_0)) for i in range(len(lc_s_wei)): part[lc_index[i]] = part_0[i] #s_wei[lc_index[i]] = lc_s_wei[i] part_1 = self.initial_partition(rc_s_wei, rc_l_wei, rc_path_cost, level+1, rc_part_no) for i in range(len(rc_s_wei)): part[rc_index[i]] = part_1[i] #s_wei[rc_index[i]] = rc_s_wei[i] return part