Newer
Older
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
"""
pydtn community module.
Implements the following community detection schemes on epochs:
- Louvain community detection
- K-Clique community detection
"""
__all__ = [
"Community",
"KCliqueCommunity",
"LouvainCommunity",
"CommunityNode",
"BubbleNode",
"HCBFNode",
]
__version__ = '0.1'
__author__ = 'Jarrod Pas <j.pas@usask.ca>'
from collections import defaultdict
# TODO: Consider using igraph instead of networkx - jpas (2017-08-11)
import networkx as nx
from community import best_partition as louvain_partition
from pydtn import Node
class Community:
"""
Base class for community detection algorithms.
Implements partitioning on epoch turn over.
To implement your own community detection algorithm inherit from this class
and implement `partition`. You may need to extend other methods.
"""
def __init__(self, epoch):
"""Create a community detector."""
self.network = None
self.epoch = epoch
self.graph = nx.Graph()
self._prev_graph = None
self._community = {}
def start(self, network):
"""
Start community detection loop.
If it has already been started do nothing.
"""
if self.network is not None:
return
self.network = network
self.network.env.process(self.epoch_loop())
def epoch_loop(self):
"""Transition current epoch to next epoch."""
env = self.network.env
while True:
yield env.timeout(self.epoch)
new_graph = nx.Graph()
for node_a, node_b, start in self.graph.edges(data='start'):
if start is not None:
self.leave(node_a, node_b)
data = {
'start': env.now,
'duration': 0,
}
new_graph.add_edge(node_a, node_b, data)
self.graph, self._prev_graph = new_graph, self.graph
self.partition()
def partition(self):
"""Partition nodes into communities."""
self._community = {}
for node in self._prev_graph.nodes():
self._community[node] = frozenset(list(node))
def join(self, node_a, node_b):
"""
Node a and b join each other's neighbourhoods.
This is an idempotent operation.
"""
if not self.graph.has_edge(node_a, node_b):
data = {
'start': None,
'duration': 0,
}
self.graph.add_edge(node_a, node_b, data)
edge = self.graph[node_a][node_b]
if edge['start'] is None:
edge['start'] = self.network.env.now
def leave(self, node_a, node_b):
"""
Node a and b leave each other's neighbourhoods.
This is an idempotent operation.
"""
if self.graph.has_edge(node_a, node_b):
edge = self.graph[node_a][node_b]
if edge['start'] is not None:
edge['duration'] += self.network.env.now - edge['start']
edge['start'] = None
def __getitem__(self, node):
"""
Return the community of a node.
If the node has no community it is it's own all alone.
"""
if node not in self._community:
self._community[node] = frozenset(list(node))
return self._community[node]
def local_popularity(self, node):
"""Return local popularity of a node."""
graph = self._prev_graph
if node not in graph:
return 0
return sum(
edge['duration']
for other, edge in graph[node].items()
if other in node.community
)
def global_popularity(self, node):
"""Return global popularity of a node."""
graph = self._prev_graph
if node not in graph:
return 0
return sum(
edge['duration']
for other, edge in graph[node].items()
if other not in node.community
)
def unique_interactions(self, node):
graph = self._prev_graph
if node not in graph:
return 0
return len(graph[node])
def community_betweenness(self, node_a, node_b):
"""Return community betweenness for 2 nodes."""
community_a = self[node_a]
community_b = self[node_b]
if community_a == community_b:
return float('inf')
graph = self._prev_graph
return sum(
graph[node_a][node_b]['duration']
for node_a, node_b in product(community_a, community_b)
if node_a in graph and node_b in graph[node_a]
)
class KCliqueCommunity(Community):
"""K-clique community detection."""
def __init__(self, epoch, k, threshold):
"""Create a k-clique community detector."""
super().__init__(epoch)
self.k = k
self.threshold = threshold
def partition(self):
"""Partition graph by k-clique."""
graph = nx.Graph()
for edge in self._prev_graph.edges(data='duration'):
duration = edge[2]
if duration > self.threshold:
graph.add_edge(*edge)
communities = nx.k_clique_communities(graph, self.k)
for community in communities:
for node in community:
self._community[node] = community
class LouvainCommunity(Community):
"""Louviain community detection."""
def partition(self):
"""Partition graph with Louvain algorithm."""
partitions = louvain_partition(self._prev_graph, weight='duration')
communities = defaultdict(set)
for node, community in partitions.items():
communities[community].add(node)
for community in communities.values():
community = frozenset(community)
for node in community:
self._community[node] = community
class CommunityNode(Node):
"""Base node for community based forwarding heuristics."""
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
if community is None:
raise ValueError('No community set')
self._community = community
def start(self, network):
"""Start event loop for node and community detector."""
super().start(network)
self._community.start(network)
@property
def community(self):
"""Return my community."""
return self._community[self]
@property
def in_community_neighbours(self):
"""Return nodes in my community."""
return [
neighbour
for neighbour in self.neighbours
if neighbour in self.community
]
@property
def out_community_neighbours(self):
"""Return nodes not in my community."""
return [
neighbour
for neighbour in self.neighbours
if neighbour not in self.community
]
class BubbleNode(CommunityNode):
"""Node with BubbleRap forwarding."""
def forward(self, packet):
"""Forward packet with the BubbleRap heuristic."""
# direct forwarding
forward = super().forward(packet)
if forward:
return forward
if self.in_community_neighbours:
return {}
elif self.out_community_neighbour:
return {}
return {}
class HCBFNode(CommunityNode):
"""Node with Hybrid Community Based forwarding."""
def forward(self, packet):
"""Forward packet with Hybrid Community Based heuristic."""
# direct forwarding
forward = super().forward(packet)
if forward:
return forward
return {}