def detect(self): """detect the source with GSBA. Returns: @rtype:int the detected source """ ''' 这是什么时候调用的?这是公式需要,对别的来说,就不是这个了。所有对于贪心是有两个 先验的,一个是要谣言中心性,一个是其他。 我们要加我们的就是加一个先验的,比如覆盖的操作,模仿其谣言定位方式,自己写一个 然后作为先验,放在先验中。 ''' self.reset_centrality() self.prior_detector.set_data(self.data) self.prior_detector.detect() self.prior = nx.get_node_attributes(self.subgraph, 'centrality') # print('先验检测器是什么?') # print(self.prior) # epa带权重的东西 self.reset_centrality() epa_weight_object = epa2.EPA_center_weight() epa_weight_object.set_data(self.data) epa_weight_object.detect() epa_weight_cnetralities = nx.get_node_attributes(self.subgraph, 'centrality') # 覆盖率中心 self.reset_centrality() cc_object = cc.CoverageCenter() cc_object.set_data(self.data) cc_object.detect() coverage_centralities = nx.get_node_attributes(self.subgraph, 'centrality') self.reset_centrality() infected_nodes = set(self.subgraph.nodes()) n = len(infected_nodes) epa_coverage={} for node in infected_nodes: epa_coverage[node] = decimal.Decimal(coverage_centralities[node]*epa_weight_cnetralities[node]) nx.set_node_attributes(self.subgraph,'centrality',epa_coverage) result= self.sort_nodes_by_centrality() print('result') high_value =[x[0] for x in result] new_subgraph =nx.Graph() new_subgraph= self.data.graph.subgraph(high_value[0:40]) new_infect_nodes = new_subgraph.nodes() self.reset_centrality() posterior = {} included = set() neighbours = set() weights = self.data.weights for v in new_infect_nodes: """find the approximate upper bound by greedy searching""" included.clear() neighbours.clear() included.add(v) neighbours.add(v) likelihood = 1 w = {} # effective propagation probabilities: node->w w_key_sorted = blist() w[v] = 1 w_key_sorted.append(v) while len(included) < n and len(w_key_sorted)>1: # print('邻居用来计算所谓的neighbours') # print(neighbours) w_sum = sum([w[j] for j in neighbours]) u = w_key_sorted.pop() # pop out the last element from w_key_sorted with the largest w likelihood *= w[u] / w_sum # print('分母是?') # print(w_sum) # print('likelihood') # print(likelihood) included.add(u) neighbours.remove(u) new = nx.neighbors(new_subgraph, u) # print('new也就是在总图中的邻居') # print(new) for h in new: # print('遍历到某个邻居') # print(h) if h in included: continue neighbours.add(h) # compute w for h w_h2u = weights[self.data.node2index[u], self.data.node2index[h]] # w_h2u = weights[self.data.node2index[u]][self.data.node2index[h]] if h in w.keys(): # print('------') # print(w[h]) # print(w_h2u) w[h] = 1 - (1 - w[h]) * (1 - w_h2u) # print('w[h],,,,h在keys') # print(w[h]) else: # print('h不在keys') w[h] = w_h2u # print(w[h]) # print('w是什么') # print(w) # h_neighbor = nx.neighbors(self.data.graph, h) # w_h = 1 # for be in included.intersection(h_neighbor): # w_h *= 1 - self.data.get_weight(h, be) # w[h] = 1 - w_h """insert h into w_key_sorted, ranking by w from small to large""" if h in infected_nodes: # print('开始排序了') if h in w_key_sorted: w_key_sorted.remove(h) # remove the old w[h] k = 0 while k < len(w_key_sorted): if w[w_key_sorted[k]] > w[h]: break k += 1 # print(w_key_sorted) w_key_sorted.insert(k, h) # 安排降序加入,就是排列可能性加入,安排顺序插入进去 # print('w_key_sorted') # print(w_key_sorted) # w_key_sorted[k:k] = [h] # print('每次开始的是那个节点呢?') # print(v) # print('每一个的可能性是likehood') # print(likelihood) posterior[v] = (decimal.Decimal(self.prior[v]) * decimal.Decimal(likelihood) * self.prior[v]) nx.set_node_attributes(self.subgraph, 'centrality', posterior) return self.sort_nodes_by_centrality()
def detect(self): """detect the source with GSBA. Returns: @rtype:int the detected source """ ''' 这是什么时候调用的?这是公式需要,对别的来说,就不是这个了。所有对于贪心是有两个 先验的,一个是要谣言中心性,一个是其他。 我们要加我们的就是加一个先验的,比如覆盖的操作,模仿其谣言定位方式,自己写一个 然后作为先验,放在先验中。 ''' self.reset_centrality() self.prior_detector.set_data(self.data) self.prior_detector.detect() self.prior = nx.get_node_attributes(self.subgraph, 'centrality') # print('先验检测器是什么?') # print(self.prior) # 覆盖率中心 self.reset_centrality() cc_object = cc.CoverageCenter() cc_object.set_data(self.data) cc_object.detect() coverage_centralities = nx.get_node_attributes(self.subgraph, 'centrality') # 谣言中心 self.reset_centrality() rc = rumor_center.RumorCenter() rc.set_data(self.data) rc.detect() rumor_centralities = nx.get_node_attributes(self.subgraph, 'centrality') # #print('先验加进去,试试看') self.reset_centrality() infected_nodes = set(self.subgraph.nodes()) n = len(infected_nodes) # print(infected_nodes) # print('infected_nodes') posterior = {} included = set() neighbours = set() weights = self.data.weights for v in infected_nodes: posterior[v] = (decimal.Decimal(self.prior[v]) * coverage_centralities[v]) # print('w_key_sorted') # print(w_key_sorted) # # print('------------') # print(coverage_centralities) # print('看下这里的posterior') # print(posterior) nx.set_node_attributes(self.subgraph, 'centrality', posterior) return self.sort_nodes_by_centrality()
def detect(self): """detect the source with GSBA. Returns: @rtype:int the detected source """ ''' 这是什么时候调用的?这是公式需要,对别的来说,就不是这个了。所有对于贪心是有两个 先验的,一个是要谣言中心性,一个是其他。 我们要加我们的就是加一个先验的,比如覆盖的操作,模仿其谣言定位方式,自己写一个 然后作为先验,放在先验中。 ''' self.reset_centrality() self.prior_detector.set_data(self.data) self.prior_detector.detect() self.prior = nx.get_node_attributes(self.subgraph, 'centrality') # print('先验检测器是什么?') # print(self.prior) # epa带权重的东西 self.reset_centrality() epa_weight_object = epa2.EPA_center_weight() epa_weight_object.set_data(self.data) epa_weight_object.detect() epa_weight_cnetralities = nx.get_node_attributes( self.subgraph, 'centrality') # 覆盖率中心 self.reset_centrality() cc_object = cc.CoverageCenter() cc_object.set_data(self.data) cc_object.detect() coverage_centralities = nx.get_node_attributes(self.subgraph, 'centrality') self.reset_centrality() # 获取边界点. infected_nodes = set(self.subgraph.nodes()) n = len(infected_nodes) bound_list = [] for node in infected_nodes: neig = nx.neighbors(self.data.graph, node) infect_ne = len([x for x in neig if x in infected_nodes]) if infect_ne == 1: bound_list.append(node) print('bound_list') print(bound_list) # infected_nodes_new = set(simple_subgraph.nodes()) # n = len(infected_nodes_new) posterior = {} included = set() neighbours = set() weights = self.data.weights for v in infected_nodes: path_list = [] likepath_all = 0.000000000000000000000000000000000000000000000000001 for bound_node in bound_list: path = nx.bidirectional_shortest_path(self.subgraph, source=v, target=bound_node) path_list.extend(path) print('path') print(path) likepathhood = 1 for index in range(len(path) - 1): likepathhood *= weights[self.data.node2index[path[index]], self.data.node2index[path[index + 1]]] likepath_all += likepathhood posterior[v] = (decimal.Decimal( decimal.Decimal(likepath_all) * coverage_centralities[v] * epa_weight_cnetralities[v])) # print('w_key_sorted') # print(w_key_sorted) # # print('------------') # print(coverage_centralities) # print('看下这里的posterior') # print(posterior) nx.set_node_attributes(self.subgraph, 'centrality', posterior) return self.sort_nodes_by_centrality()
def detect(self): """detect the source with GSBA. Returns: @rtype:int the detected source """ ''' 这是什么时候调用的?这是公式需要,对别的来说,就不是这个了。所有对于贪心是有两个 先验的,一个是要谣言中心性,一个是其他。 我们要加我们的就是加一个先验的,比如覆盖的操作,模仿其谣言定位方式,自己写一个 然后作为先验,放在先验中。 ''' print('steiner tree版本检测') print('------------------这一趟结束了----------------------------------') self.reset_centrality() self.prior_detector.set_data(self.data) self.prior_detector.detect() self.prior = nx.get_node_attributes(self.subgraph, 'centrality') # print('先验检测器是什么?') # print(self.prior) # epa带权重的东西 self.reset_centrality() epa_weight_object = epa2.EPA_center_weight() epa_weight_object.set_data(self.data) epa_weight_object.detect() epa_weight_cnetralities = nx.get_node_attributes( self.subgraph, 'centrality') # 覆盖率中心 self.reset_centrality() cc_object = cc.CoverageCenter() cc_object.set_data(self.data) cc_object.detect() coverage_centralities = nx.get_node_attributes(self.subgraph, 'centrality') self.reset_centrality() # 获取边界点.不得,如果边界点为空,那么就把全体感染点作为边界点。 infected_nodes = set(self.subgraph.nodes()) n = len(infected_nodes) bound_list = [] for node in infected_nodes: neig = nx.neighbors(self.data.graph, node) infect_ne = len([x for x in neig if x in infected_nodes]) if infect_ne == 1: bound_list.append(node) print('bound_list,当前的边界点为') print(bound_list) # infected_nodes_new = set(simple_subgraph.nodes()) # n = len(infected_nodes_new) posterior = {} included = set() neighbours = set() weights = self.data.weights import copy for v in infected_nodes: if len(bound_list) > 5: bound_list.append(v) bound_list = list(set(bound_list)) else: temp = copy.deepcopy(infected_nodes) bound_list = list(set(temp)) simple_subgraph_steiner_tree = MSTbyMetric_closure( self.data.subgraph, bound_list) infected_nodes_steiner_tree = set( simple_subgraph_steiner_tree.nodes()) ''' 就是这里的问题了,这里我们限制了解集合,那么自然就需要用steiner ——tree来处理这个问题。 ''' print('看下生成的树有多少个点') print(simple_subgraph_steiner_tree.number_of_nodes()) print(simple_subgraph_steiner_tree.number_of_edges()) print('而边界点有多少个呢?和生成树应该是一样的。') print(len(bound_list)) # print('开始寻找likehood的') """find the approximate upper bound by greedy searching""" included.clear() neighbours.clear() included.add(v) neighbours.add(v) likelihood = 1 w = {} # effective propagation probabilities: node->w w_key_sorted = blist() w[v] = 1 w_key_sorted.append(v) includedlen = len(bound_list) while len(included) < includedlen and len(w_key_sorted) > 0: # print('邻居用来计算所谓的neighbours') # print(neighbours) w_sum = sum([w[j] for j in neighbours]) u = w_key_sorted.pop( ) # pop out the last element from w_key_sorted with the largest w 弹出最大可能被感染的元素 likelihood += w[u] / w_sum # print('分母是?') # print(w_sum) # print('likelihood') # print(likelihood) included.add(u) neighbours.remove(u) new = nx.neighbors(simple_subgraph_steiner_tree, u) #为什么u会不在形成的树中。 ''' 分析原因: 该steiner tree 的节点形成的边界点+斯坦纳点+源点。 而u是从那个集合取出来的? ''' # print('new也就是在总图中的邻居') # print(new) for h in new: # print('遍历到某个邻居') # print(h) if h in included: continue neighbours.add(h) # compute w for h w_h2u = weights[self.data.node2index[u], self.data.node2index[h]] # w_h2u = weights[self.data.node2index[u]][self.data.node2index[h]] if h in w.keys(): # print('------') # print(w[h]) # print(w_h2u) w[h] = 1 - (1 - w[h]) * (1 - w_h2u) # print('w[h],,,,h在keys') # print(w[h]) else: # print('h不在keys') w[h] = w_h2u # print(w[h]) # print('w是什么') # print(w) # h_neighbor = nx.neighbors(self.data.graph, h) # w_h = 1 # for be in included.intersection(h_neighbor): # w_h *= 1 - self.data.get_weight(h, be) # w[h] = 1 - w_h """insert h into w_key_sorted, ranking by w from small to large""" if h in infected_nodes_steiner_tree: # print('开始排序了') if h in w_key_sorted: w_key_sorted.remove(h) # remove the old w[h] k = 0 while k < len(w_key_sorted): if w[w_key_sorted[k]] > w[h]: break k += 1 # print(w_key_sorted) w_key_sorted.insert(k, h) # 安排降序加入,就是排列可能性加入,安排顺序插入进去 # print('w_key_sorted') # print(w_key_sorted) # w_key_sorted[k:k] = [h] # print('每次开始的是那个节点呢?') # print(v) # print('每一个的可能性是likehood') # print(likelihood) posterior[v] = (decimal.Decimal( decimal.Decimal(likelihood) * coverage_centralities[v] * epa_weight_cnetralities[v])) # print('w_key_sorted') # print(w_key_sorted) # # print('------------') # print(coverage_centralities) # print('看下这里的posterior') # print(posterior) nx.set_node_attributes(self.subgraph, 'centrality', posterior) return self.sort_nodes_by_centrality()
def detect(self): """detect the source with GSBA. Returns: @rtype:int the detected source """ ''' 首先用覆盖率和epa-weight作为先验作为pageRank迭代的起始值,然后再使用 迭代,只用传播图来迭代吧? ''' # epa带权重的东西 self.reset_centrality() epa_weight_object = epa2.EPA_center_weight() epa_weight_object.set_data(self.data) epa_weight_object.detect() epa_weight_cnetralities = nx.get_node_attributes(self.subgraph, 'centrality') # 覆盖率中心 self.reset_centrality() cc_object = cc.CoverageCenter() cc_object.set_data(self.data) cc_object.detect() coverage_centralities = nx.get_node_attributes(self.subgraph, 'centrality') self.reset_centrality() infected_nodes = set(self.subgraph.nodes()) n = len(infected_nodes) initvalue={} for node in infected_nodes: initvalue[node] =float(epa_weight_cnetralities[node]*coverage_centralities[node]) posterior=nx.pagerank(self.subgraph,alpha=0.85, personalization=None, max_iter=100, tol=1.0e-6, nstart=initvalue, weight='weight', dangling=None) print('posterior') print(posterior) nx.set_node_attributes(self.subgraph, 'centrality', posterior) return self.sort_nodes_by_centrality()