-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfractories.py
278 lines (220 loc) · 8.61 KB
/
fractories.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import networkx as nx
import random as r
from actors import Actor
class Graphs:
def __init__(self):
pass
@staticmethod
def create_complete(**kwargs):
"""
Creates a completely connected graph.
:param n: number of nodes.
:return: a Graph with n Nodes and all connected.
"""
return nx.generators.complete_graph(kwargs.get("n"))
@staticmethod
def create_barabasi_albert(**kwargs):
return nx.generators.barabasi_albert_graph(n=kwargs.get("n"), m=kwargs.get("m"), seed=None)
@staticmethod
def create_clustered_barabasi_albert(**kwargs):
return nx.generators.barabasi_albert_graph(kwargs.get("n"), seed=None)
@staticmethod
def create_watts_strogatz(**kwargs):
return nx.generators.connected_watts_strogatz_graph(n=kwargs.get("n"), k=kwargs.get("k"), p=kwargs.get("p"))
@staticmethod
def create_newman_watts(**kwargs):
print(kwargs)
return nx.generators.connected_watts_strogatz_graph(n=kwargs.get("n"), k=kwargs.get("k"), p=kwargs.get("p"))
@staticmethod
def create_clustered_watts_strogatz(**kwargs):
return nx.generators.barabasi_albert_graph(kwargs.get("n"), seed=None)
@staticmethod
def create_clustered_powerlaw_cluster(**kwargs):
return nx.powerlaw_cluster_graph()
@staticmethod
def create_powerlaw_cluster(**kwargs):
return nx.powerlaw_cluster_graph(n=kwargs.get("n"), m=kwargs.get("m"), p=kwargs.get("p"))
@staticmethod
def func_by_name(name):
"""
This function returns a function from a name. If you want to add another option you have to alter the
func_mapper in this function.
:param name: is the name referred to a generator function.
:return: a function to create a graph
"""
func_mapper = {
"complete": Graphs.create_complete,
"barabasi_albert": Graphs.create_barabasi_albert,
"clustered_barabasi_albert": Graphs.create_clustered_barabasi_albert,
"watts_strogatz": Graphs.create_watts_strogatz,
"clustered_watts_strogatz": Graphs.create_clustered_watts_strogatz,
"powerlaw_cluster": Graphs.create_powerlaw_cluster,
"clustered_powerlaw_cluster": Graphs.create_clustered_powerlaw_cluster,
"newman_watts": Graphs.create_newman_watts
}
return func_mapper.get(name)
class Actors:
def __init__(self):
pass
@staticmethod
def create_nihilist(**kwargs):
return Actor([0 for x in range(kwargs.get("n"))])
@staticmethod
def create_opportunist(**kwargs):
return Actor([0 for x in range(kwargs.get("n"))])
@staticmethod
def create_random(**kwargs):
return [r.choice([1.0, 0.0, -1.0]) for x in range(kwargs.get("n"))]
@staticmethod
def create_linear(**kwargs):
target_r, cur_r = r.random(), 1
cur_values = [1, 0, -1]
while target_r < cur_r:
cur_values.append(0)
cur_r -= kwargs["b"]
return [r.choice(cur_values) for x in range(kwargs.get("n"))]
@staticmethod
def create_exponential(**kwargs):
target_r, cur_r = r.random(), 1
cur_values = [1, 0, -1]
while target_r < cur_r:
cur_values.append(0)
cur_r /= kwargs["b"]
return [r.choice(cur_values) for x in range(kwargs.get("n"))]
@staticmethod
def func_by_name(name):
"""
This function returns a function from a name. If you want to add another option you have to alter the
func_mapper in this function.
:param name: is the name referred to a generator function.
:return: a function to create a actor
"""
func_mapper = {
"nihilist": Actors.create_nihilist,
"opportunist": Actors.create_opportunist,
"random": Actors.create_random,
"linear": Actors.create_linear,
"exponential": Actors.create_exponential
}
return func_mapper.get(name)
class Distributions:
def __init__(self):
pass
@staticmethod
def create_random(**kwargs):
randoms = [x for x in range(len(kwargs.get("graph").nodes))]
r.shuffle(randoms)
return randoms
@staticmethod
def create_linear(**kwargs):
randoms = [x for x in range(len(kwargs.get("graph").nodes))]
r.shuffle(randoms)
return randoms
@staticmethod
def create_exponential(**kwargs):
randoms = [x for x in range(len(kwargs.get("graph").nodes))]
r.shuffle(randoms)
return randoms
@staticmethod
def func_by_name(name):
"""
This function returns a function from a name. If you want to add another option you have to alter the
func_mapper in this function.
:param name: is the name referred to a generator function.
:return: a function to create a actor
"""
func_mapper = {
"random": Distributions.create_random,
"linear": Distributions.create_linear,
"exponential": Distributions.create_exponential
}
return func_mapper.get(name)
class Rules:
def __init__(self):
pass
# APPLY FUNCTIONS
@staticmethod
def create_actions_by_name(edge, graph, conf):
"""
"""
func_mapper = {
"association": Rules.gen_association,
"disassociation": Rules.gen_disassociation,
"dynamic_ad": Rules.gen_dynamic_ad,
"social_desirability": Rules.gen_social_desirability,
}
return func_mapper.get(conf.get("name"))(edge=edge, graph=graph, conf=conf)
# PARAM FUNCTIONS
@staticmethod
def gen_dynamic_ad(edge, graph, conf):
node_a = edge[0]
node_b = edge[1]
actor_a = graph.nodes[node_a]["actor"]
actor_b = graph.nodes[node_b]["actor"]
za = actor_a.orientation_of_action(actor_b)
zb = actor_b.orientation_of_action(actor_a)
zv = r.random() * (za + zb)
node_x, node_y = (node_a, node_b) if zv < za else (node_b, node_a)
actor_x = graph.nodes[node_x]["actor"]
actor_y = graph.nodes[node_y]["actor"]
neighbors_y = list(graph.neighbors(node_y))
node_z = r.choice(neighbors_y)
z = r.random()
if z < actor_x.orientation_of_action(actor_y):
return [{"name": "add_edge",
"node_x": node_x,
"node_z": node_z}]
else:
return [{"name": "remove_edge",
"node_x": node_x,
"node_z": node_z}]
@staticmethod
def gen_association(edge, graph, conf):
node_a = edge[0]
node_b = edge[1]
actor_a = graph.nodes[node_a]["actor"]
actor_b = graph.nodes[node_b]["actor"]
if r.random() < actor_a.orientation(actor_b):
return []
za = actor_a.orientation_of_action(actor_b)
zb = actor_b.orientation_of_action(actor_a)
zv = r.random() * (za + zb)
if zv <= za:
node_x = node_a
node_y = node_b
else:
node_x = node_b
node_y = node_a
neighbors_y = list(graph.neighbors(node_y))
node_z = r.choice(neighbors_y)
return [{"name": "add_edge",
"node_x": node_x,
"node_z": node_z}]
@staticmethod
def gen_disassociation(edge, graph, conf):
node_a = edge[0]
node_b = edge[1]
actor_a = graph.nodes[node_a]["actor"]
actor_b = graph.nodes[node_b]["actor"]
if r.random() > actor_a.orientation(actor_b):
return []
za = actor_a.orientation_of_action(actor_b)
zb = actor_b.orientation_of_action(actor_a)
zv = r.random() * (za + zb)
node_x, node_y = (node_b, node_a) if zv > za else (node_a, node_b)
neighbors_y = list(graph.neighbors(node_y))
node_z = r.choice(neighbors_y)
return [{"name": "remove_edge",
"node_x": node_x,
"node_z": node_z}]
@staticmethod
def gen_social_desirability(edge, graph, conf):
node_a = edge[0]
actor_a = graph.nodes[node_a]["actor"]
if not actor_a.obeys_social_pressure():
norm = [r.choice([1, 0,-1]) for x in range(len(actor_a.interests()))]
for node in graph.nodes:
d = conf["options"]["d"]
p = conf["options"]["d"]
graph.nodes[node]["actor"].set_social_desirability(p=p, d=d, norm=norm)
return []