-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob.py
160 lines (121 loc) · 4.63 KB
/
prob.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""This module is used in the RRWG simulation to calculate the
probability of a walk w located in a vertex u to go to a neighbor v of
u, taking into account the number of visits of the other walks in the
vertex v. The walk w tends to go to the vertex less visited by the
other walks.
"""
import math
import sys
from log import write as logwrite
from walk import Walk
def vertex_count_visits(walks: list[Walk], vert: int) -> int:
"""Count the number of visits of all walks in the specified vertex.
walks list[Walks]: list of Walk objects
vert (int): vertex to count the visits
"""
acc = 0
for i, walk in enumerate(walks):
nvis = walk.nvisits(vert)
acc += nvis
logwrite('\t\tvisits(w{}, v{})={}'.format(i, vert, nvis))
return acc
def sum_norm_vertex_visits_from_other_walks(walks: list[Walk], \
cur_walk: Walk,
vert: int) -> float:
"""Sum of the normalized number of visits from all walks
excepting the current walk to the specified vertex.
walks list[Walks]: list of Walk objects
cur_walk (Walk): index of the current walk
vert (int): vertex eligible to be visited
"""
# Accumulator of the normalized number of visits from the
# walks, excepting the current walk.
acc = 0.0
# Total number of visits in the vertex vert
total_nvis = vertex_count_visits(walks, vert)
for i, walk in enumerate(walks):
if walk == cur_walk: # ignore current walk
continue
norm_nvis = \
float(walk.nvisits(vert)) / total_nvis
logwrite('\t\tother_ws_norm_visits(w{}, v{})={:.2f}'
.format(i, vert, norm_nvis))
acc += norm_nvis
return acc, total_nvis
class Probability():
"""Transition probability class.
"""
def __init__(self, function='EXP'):
"""Initialize probability object with the reinforced factor alpha and
the function chosen to be used in the calculation.
"""
self._funcname = function.upper()
if self._funcname == 'EXP':
self.__func = self.__exp
elif self._funcname == 'POW':
self.__func = self.__pow
else:
sys.exit('panic: unknown function \"{}\"'.format(function))
# Set some default values for alpha and epsilon
self._alpha = 1.0
self._epsilon = 0.0
def function_name(self):
"""Get the name of the function used in the transition probability
calculation.
"""
return self._funcname
@property
def alpha(self):
"""Get the value of alpha.
"""
return self._alpha
@alpha.setter
def alpha(self, value):
"""Set the value for alpha.
"""
self._alpha = value
@property
def epsilon(self):
"""Get the value of epsilon.
"""
return self._epsilon
@epsilon.setter
def epsilon(self, value):
"""Set the value for epsilon.
"""
self._epsilon = value
def __exp(self, walks: list[Walk], cur_walk: Walk, vert: int):
"""Apply the exponential function to the number of visits and the
reinforcing factor alpha.
"""
acc, _ = \
sum_norm_vertex_visits_from_other_walks(walks, cur_walk,
vert)
prob = math.exp(-self._alpha * acc)
return prob
def __pow(self, walks: list[Walk], cur_walk: Walk, vert: int):
"""Apply a factor to a power function of the number of visits and the
reinforcing factor alpha.
"""
acc, total_nvis = \
sum_norm_vertex_visits_from_other_walks(walks, cur_walk,
vert)
# Normalized number of visits of the current walk.
nvw = cur_walk.nvisits(vert) / total_nvis
logwrite('\t Pr=pow({} - {}*{:.2f} - {:.2f}, {})'
.format(len(walks), self._epsilon, nvw,
acc, self._alpha))
prob = nvw * pow(len(walks) - self._epsilon*nvw - acc,
self._alpha)
return prob
def calculate(self, walks: list[Walk], cur_walk: Walk, \
v_dest: int) -> float:
"""Calculate the transition probability of the current walk that are
located at source vertex to go to destination vertex.
walks (Walks): Walks object to access data
from each walk
cur_walk (Walk): current walk
v_src (int): source vertex
v_dest (int): destination vertex
"""
return self.__func(walks, cur_walk, v_dest)