def k_tarjan(k, length, cid): global g if k == 1: return graph.Connect(g, length, cid) elif k == 2: init() return graph.Connect(g, length, cid) and tarjan(cid) else: l = list(range(length - 1)) lst = itertools.combinations(l, k - 2) g_ = copy.deepcopy(g) num = 0 for x in lst: num += 1 # print("The count of enum is: ", num) gg = copy.deepcopy(g_) combin = list(x) # print("Delete Point:", combin) gg = np.delete(g_, combin, 0) # 删除行 gg = np.delete(gg, combin, 1) # 删除列 g = gg init() if not graph.Connect(g, length - (k - 2), cid) or not tarjan(): # print("----------------------No k-connect!------------------------") return 0 else: # print("--------------------------Yes k-connect!-----------------------") return 1
def mlpt_utility(cid, nodes, G_mlpt, M): # cid:current id '''return No.i node optimal power and the index''' nei = nodes[cid].neighbor n = [x[0] for x in nei] # Neighbor Node id d = [x[1] for x in nei] # the distance of i and j E = [x[2] for x in nei] # energy by link utility_max = 0 # index = 0 for i in range(len(E)): MG = copy.deepcopy(G_mlpt) if i != 0: MG[cid][n[i - 1]] = 0 MG[n[i - 1]][cid] = 0 e = 0 for j in range(len(E)): if MG[cid][n[j]] == 1: e += E[j] k = graph.Connect(MG, Node.n, cid) u = M * k - e if k > 0 and u > utility_max: utility_max = u G_mlpt = MG # index = i # maybe disconnect, so cannot use index ################################################## # Determine power ################################################## # power = d[index] power = 0 # print("++++++++++++++++++++++++++++++++++++++++++++") for i in range(len(d)): if G_mlpt[cid][n[i]] == 1 and d[i] > power: power = d[i] return power, G_mlpt
def mia_utility(cid, nodes, G_Mia, M): # cid:current id '''return No.i node optimal power''' MG = copy.deepcopy(G_Mia) nei = nodes[cid].double_neighbor n = [x[0] for x in nei] # Neighbor Node id d = [x[1] for x in nei] # the distance of i and j index = 0 # index of id of node that is NE utility_max = 0 for i in range(len(n)): p = d[i] # power for j in range(i): if d[j] > p: # if d[j] > tp close the connect MG[ cid ][n[j]] = 0 MG[n[j]][ cid ] = 0 k = graph.Connect(MG, Node.n, cid) # judge the connect after close # k = graph.go_to_sink(MG, Node.n, cid) # judge the connect after close u = M * k * Node.n - Simulator.dis_to_power(d[i]) # utility funciton if k > 0 and u > utility_max: utility_max = u index = i return d[index]
def judge_connect(): Node.init_nodes() Node.find_neighbors() Node.find_interconnect() if graph.Connect(Node.interconnect_matrix, Node.n): print("connect") else: print("No connect")
def init(): connect = False cnt = 0 while not connect: Node.init_nodes() Node.find_neighbors() #Node.find_interconnect() connect = graph.Connect(Node.interconnect_matrix, Node.n) cnt += 1 print("Initialization times:", cnt)
def deba_utility(nodes, cid, G_deba, cnt): # cid:current id '''return No.i node optimal power and the index''' nei = nodes[cid].neighbor n = [x[0] for x in nei] # Neighbor Node id d = [x[1] for x in nei] # the distance of i and j #d.append(0.001) ######################################################## a = 1 b = 1 ######################################################## kc = graph.Connect(G_deba, Node.n, cid) power = nodes[cid].power p = Simulator.dis_to_power(power) pmax = Simulator.dis_to_power(nodes[cid].c_d_max) avg_e = average_energy(cid, nodes, G_deba, Node.n) ei_er = nodes[cid].energy_init / nodes[cid].energy_residual umax = kc * (a * pmax * ei_er + b * avg_e) - a * p * ei_er MG = copy.deepcopy(G_deba) if cnt < len(d): for j in range(len(d)): if d[j] > d[cnt]: MG[cid][n[j]] = 0 MG[n[j]][cid] = 0 ######################################################## kc = graph.Connect(MG, Node.n, cid) p = Simulator.dis_to_power(d[cnt]) pmax = Simulator.dis_to_power(nodes[cid].c_d_max) avg_e = average_energy(cid, nodes, MG, Node.n) ei_er = nodes[cid].energy_init / nodes[cid].energy_residual u = kc * (a * pmax * ei_er + b * avg_e) - a * p * ei_er ######################################################### if (kc) == 1 and u > umax: power = d[cnt] return power
def dia_utility(cid, nodes, G_dia, M, cnt): # cid:current id '''return No.i node optimal power and the index''' MG = copy.deepcopy(G_dia) nei = nodes[cid].double_neighbor n = [x[0] for x in nei] # Neighbor Node id d = [x[1] for x in nei] # the distance of i and j pw_list = nodes[cid].power_list power = nodes[cid].power # The line where the neighbor ID currently needs to be disconnected # It is initially 0. index = nodes[cid].power_select # next selection of power k = graph.Connect(MG, Node.n) # judge the connect after close umax = M * k - Simulator.dis_to_power(power) flag = False # Whether the topology changes when power is reduced count_power = pw_list[cnt] if index < len(nei): # Index points to the last link if temp > length of d if count_power < d[index]: MG[cid][(n[index])] = 0 MG[(n[index])][cid] = 0 flag = True k = graph.Connect(MG, Node.n, cid) # judge the connect after close u = M * k - Simulator.dis_to_power(count_power) # utility funciton if k > 0 and u > umax: power = count_power if flag: index += 1 return power, index