-
Notifications
You must be signed in to change notification settings - Fork 4
/
DNA.py
257 lines (213 loc) · 11.9 KB
/
DNA.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import numpy as np
import warnings
import cupy as cp
class DNA:
'''
This class is to define a preference for a node. NodeSocial has a DNA. A group of NodeSocial referring to the same DNA
tells us that all the Nodes in that group has the same preference. In such case, those bunch of nodes would have the
same label. The assumption here is, on social networks, people usually have different features but what we may want
to predict as a label is their preferences. Preferences define categories in a social network. For example, a group
of people on a social network with a certain political view have some commonalities although their features such as
age, gender might be different. That political view may, in many cases, define their interest on social media in
terms of connecting with other people. Those preferences are influence their choice of connecting other people and
how they view their features. Another example could be, there are people who only connect with other people who are
nearer to them in geolocation. As a result, those group of people would always consider the location feature on
another person before the connect and will prefer to connect with a person which has a location feature closer
to them.
For example, if features are F = [25,10,200] , DNA = [0, 0.5, 1, 0.5, 0, 0.8] The first two value of DNA,
DNA[0,1]: 0, 0.5 correspond to the first feature F[0] : 25. The first value is a binary 0/1 called the preference and second is a weighting parameter.
In this example, 0 implies that difference between the feature value 25 and the other node's feature will be multiplied by -1.
Thus higher the difference is smaller the score will be.
That means this Node for the specific feature 25 will prefer someone with a similar feature.
Whereas, for the 2nd feature F[1]:10 the preference DNA is 1. Thus it will prefer someone with a different feature.
Because if it's 1 then we do not multiply with -1. Thus higher the difference between nodes bigger the score will be.
When called from the socialiser class, scores are calculated for both Nodes.
As for the weighting factor, We multiply it with the other node's feature before calculating the difference. The intuition is, the definition of differences or similarities is different to different people with different taste or preferences. And the preference is defined by the DNA which also corresponds to the label.
'''
def __init__(self, value='random', len=5, *args, **kwargs):
'''
:param value: random','auto' or an vector, i.e. [0, 0.5, 1, 0.5, 0, 0.8]. If put auto DNA will be randomly
generated from a uniform distribution'
:param len: The length of the DNA , the vector length will be 2 * len. This is because there are two numbers
per feature
:param args:
:param kwargs:
'''
self.value = value
self.nodes = []
self.preferPopularity = None
self.preferShorterPath = None
self.preferPopularityIntensity = None
self.preferShorterPathIntensity = None
self.useGPU = None
self.createInGPUMem = None
if self.value== 'auto':
self.value = self._autoGenDNA(len=len)
elif self.value == 'autoWeightless':
self.value = self._autoGenDNA2(len=len)
def _autoGenDNA(self, len):
'''
This function is not meant to access by the user but called from the constructor if the DNA.value param is set to 'auto'
:param len: length of the DNA, vector size will be 2 * len
:return: a vector containing the DNA
'''
dnaValue = []
for i in range(1,len+1) :
rand1 = np.random.uniform(low=0.0, high=1.0, size=None)
rand2 = np.random.uniform(low=0.0, high=1.0, size=None)
if rand1 >0.5:
dnaValue.append(1)
else:
dnaValue.append(-1)
dnaValue.append(rand2)
return dnaValue
def _autoGenDNA2(self, len):
'''
This function is not meant to access by the user but called from the constructor if the DNA.value param is set to 'auto'
:param len: length of the DNA, vector size will be 2 * len
:return: a vector containing the DNA
'''
dnaValue = []
for i in range(1, len + 1):
rand1 = np.random.uniform(low=0.0, high=1.0, size=None)
rand2 = 1
if rand1 > 0.5:
dnaValue.append(1)
else:
dnaValue.append(0)
dnaValue.append(rand2)
return dnaValue
def mutateDNA(self, mutatePreference, mutatePreferenceProbability,intensity):
'''
This method can be called by the user. This is to mutate/change the DNA after instantiation if needed, mainly to
to generate dynamic networks
:param mutatePreference: whether you want to mutate/ change , True/False
:param mutatePreferenceProbability: whether you want to mutate/ change mutatePreferenceProbability, True/False
:param intensity: Intensity of mutation. If choosen high all of them will be mutated. Float, 0.00 to 1.00
:return: void
'''
l = len(self.value)
preferenceMutationCount = 0
probablityMutationCount = 0
for i in range(0,l,2):
if self.value[i]!=-1 and self.value[i]!=1:
raise ValueError('invalid dna, first index is not Preference')
else:
if mutatePreference:
if 1.0 - intensity <= np.random.uniform(low=0.0, high=1.0, size=None):
rand1 = np.random.uniform(low=0.0, high=1.0, size=None)
if rand1 > 0.5:
self.value[i] = -1
else:
self.value[i] = 1
preferenceMutationCount +=1
if mutatePreferenceProbability:
if 1.0 - intensity <= np.random.uniform(low=0.0, high=1.0, size=None):
self.value[i+1] = np.random.uniform(low=0.0, high=1.0, size=None)
probablityMutationCount +=1
i +=1
if preferenceMutationCount>0 or probablityMutationCount > 0:
warnings.warn("mutation occured in %s preference(s) and %s preference weights(s)!" % (preferenceMutationCount, probablityMutationCount))
else:
warnings.warn("mutateDNA called but no mutation detected, try increasing intensity / changing mutatePreferenceProbability and/or mutatePreference = true!")
if cp.cuda.is_available() and self.useGPU:
if self.createInGPUMem:
self.valueWeight = cp.asarray(self.value[1::2], dtype=np.float64)
self.valuePreference = cp.asarray(self.value[0::2], dtype=np.float64)
def getDnaType(self, *args, **kwargs):
'''
:param args:
:param kwargs:
:return: show DNA value
'''
if self.value == 'random':
print('Random')
return self.value
def _assignedNodes(self, nodes):
self.nodes = nodes
def _assignedNode(self, node):
self.nodes.append(node)
def __str__(self):
print(self.value)
class DNAadvanced(DNA):
'''
This class inherits DNA class. In this type of DNA popularity preference and path length preference is included
'''
def __init__(self, value='random', len=5,useGPU=True,createInGPUMem=False,preferPopularity=True,preferShorterPath=True,preferPopularityIntensity=None,preferShorterPathIntensity=None, *args, **kwargs):
'''
:param value: random','auto' or an vector, i.e. [0, 0.5, 1, 0.5, 0, 0.8]. If put auto DNA will be randomly
generated from a uniform distribution'
:param len: The length of the DNA , the vector length will be 2 * len. This is because there are two numbers
per feature
:param preferPopularity: Whether or not to prefer popularity/Degree of the nodes. True/False
:param preferShorterPath: Whether or not to prefer shorter path length nodes. True/False
:param preferPopularityIntensity: The intensity of the preference of popularity. The preferential Attachment parameter.
Numeric value.
:param preferShorterPathIntensity: The intensity of shorter path preference (not implemented)
:param args:
:param kwargs:
'''
super(DNAadvanced, self).__init__(value=value, len=len)
self.value = value
# self.nodes = []
self.preferPopularity = preferPopularity
self.preferShorterPath = preferShorterPath
self.preferPopularityIntensity = preferPopularityIntensity
self.preferShorterPathIntensity = preferShorterPathIntensity
# self.percentageOfConnectionNodes = percentageOfConnectionNodes
self.useGPU = useGPU
self.createInGPUMem = createInGPUMem
if self.value== 'auto':
self.value = self._autoGenDNA(len=len)
elif self.value == 'autoWeightless':
self.value = self._autoGenDNA2(len=len)
if cp.cuda.is_available() and self.useGPU :
if self.createInGPUMem:
self.valueWeight = cp.asarray(self.value[1::2],dtype=np.float64)
self.valuePreference = cp.asarray(self.value[0::2], dtype=np.float64)
def _autoGenDNA(self, len):
'''
same as parent class's method but extends to incorporate preferPopularityIntensity , and preferShorterPathIntensity
This method meant to be called from the instructor.
:param len: length of the DNA, vector size will be 2 * len
:return:
'''
if self.preferPopularity and self.preferPopularityIntensity is None:
self.preferPopularityIntensity = np.random.uniform(low=0.0, high=1.0, size=None)
if self.preferShorterPath and self.preferShorterPathIntensity is None:
self.preferShorterPathIntensity = [np.random.uniform(low=0.7, high=1.0, size=None),np.random.uniform(low=0.3, high=7.0, size=None),np.random.uniform(low=0.0, high=0.3, size=None)]
return super()._autoGenDNA(len=len)
def _autoGenDNA2(self, len):
'''
same as parent class's method but extends to incorporate preferPopularityIntensity , and preferShorterPathIntensity
This method meant to be called from the instructor.
:param len: length of the DNA, vector size will be 2 * len
:return:
'''
if self.preferPopularity and self.preferPopularityIntensity is None:
self.preferPopularityIntensity = np.random.uniform(low=0.0, high=1.0, size=None)
if self.preferShorterPath and self.preferShorterPathIntensity is None:
self.preferShorterPathIntensity = [np.random.uniform(low=0.7, high=1.0, size=None),np.random.uniform(low=0.3, high=7.0, size=None),np.random.uniform(low=0.0, high=0.3, size=None)]
return super()._autoGenDNA2(len=len)
def mutateDNA(self, mutatePreference, mutatePreferenceProbability,intensity):
'''
Same as parent class's method but extends to incorporate mutatePreference and mutatePreferenceProbability
:param mutatePreference: whether you want to mutate/ change , True/False
:param mutatePreferenceProbability: whether you want to mutate/ change mutatePreferenceProbability, True/False
:param intensity: Intensity of mutation. If choosen high all of them will be mutated. Float, 0.00 to 1.00
:return: void
'''
super().mutateDNA(mutatePreference=mutatePreference, mutatePreferenceProbability=mutatePreferenceProbability,intensity=intensity)
def getDnaType(self, *args, **kwargs):
'''
:param args:
:param kwargs:
:return: DNA value
'''
if self.value == 'random':
print('Random')
return self.value
def _assignedNodes(self, nodes):
self.nodes = nodes
def __str__(self):
print(self.value)