Skip to content

Commit

Permalink
now random walking is based on edge weights
Browse files Browse the repository at this point in the history
  • Loading branch information
e-yi committed Oct 21, 2019
1 parent 56d7432 commit bb67cad
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class HIN:
"""

def __init__(self, window=None):
print("Model initialization started.")
self.graph = nx.DiGraph()
self.node_size = 0
self._path_size = 0
Expand Down Expand Up @@ -69,7 +68,13 @@ def small_walk(self, start_node, length):
for i in range(1, length):
if next(nx.neighbors(self.graph, walk[-1]), None) is None:
break
walk += random.sample(list(nx.neighbors(self.graph, walk[-1])), 1) # todo 添加按权重游走的采样方式
cur_node = walk[-1]
nodes = list(nx.neighbors(self.graph, cur_node))
weights = [self.graph[cur_node][i]['weight'] for i in nodes] # 有向图可能不能这么做
s = sum(weights)
weights = [i/s for i in weights]
walk += random.choices(nodes, weights, k=1)
# walk += random.sample(list(nx.neighbors(self.graph, cur_node)), 1) # todo 添加按权重游走的采样方式
return walk

def do_walks(self, length):
Expand Down

0 comments on commit bb67cad

Please sign in to comment.