-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_control.py
More file actions
227 lines (173 loc) · 9.4 KB
/
model_control.py
File metadata and controls
227 lines (173 loc) · 9.4 KB
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
import pickle
import random
from knapsack import Knapsack
from social_network import SocialNetwork
class Random_Cont(object):
def __init__(self, b, MU, responsive_users = None):
"""the constructor defines the base intensity to be controlled and the amount of user incintivization budget"""
self.b = b
self.MU = MU
self.resposnive_users = responsive_users
def control(self):
"""loop till the budget is consumed, the consumption is random per user"""
controlled_MU = self.MU
user_indices = [i for i in range(len(self.MU))]
budget_counter = 0
if self.resposnive_users:
user_indices = self.resposnive_users
while True:
for id, _ in enumerate(self.MU):
if id in user_indices:
incent_amount = random.uniform(0, self.b)
budget_counter += incent_amount
if budget_counter <= self.b:
controlled_MU[id] += incent_amount
else:
return controlled_MU
class Uniform_Cont(object):
def __init__(self, b, MU, responsive_users=None):
"""the constructor defines the base intensity to be controlled and the amount of user incintivization budget"""
self.b = b
self.MU = MU
self.resposnive_users = responsive_users
def control(self):
"""loop till the budget is consumed, the consumption is uniformly assigned per user"""
controlled_MU = self.MU.copy()
if self.resposnive_users:
incent_amount = self.b/len(self.resposnive_users)
for id in self.resposnive_users:
controlled_MU[id] += incent_amount
else:
incent_amount = self.b / len(self.MU)
for i in range(len(self.MU)):
controlled_MU[i] += incent_amount
return controlled_MU
class Weak_Cont(object):
def __init__(self, b, MU, influence_relations, responsive_users= None):
"""the constructor defines the base intensity to be controlled and the amount of user incintivization budget"""
self.b = b
self.MU = MU
self.influence_relations = influence_relations
self.responsive_users = responsive_users
def control(self):
"""loop till the budget is consumed, the consumption is assigned to top explicit influence
users in the network. Influence info is extracted from top users that their posted content was
shared more by others in the past before the moment of control. We call the method weak because these top
influencers can be main spreader of misinformation and no learning was applied to learn their authenticity"""
controlled_MU = self.MU.copy()
if self.responsive_users:
influencers_weights = []
for influencer_id in self.influence_relations.keys():
if influencer_id in self.responsive_users:
influence_weight = len(self.influence_relations[influencer_id])
influencers_weights.append((influencer_id, influence_weight))
else:
influencers_weights = []
for influencer_id in self.influence_relations.keys():
influence_weight = len(self.influence_relations[influencer_id])
influencers_weights.append((influencer_id, influence_weight))
if not self.responsive_users:
for id, weight in influencers_weights:
# to assign incentivization to each user based on how much the user is an influencer
controlled_MU[id] += self.b/len(influencers_weights) #* weight
else:
for id, weight in influencers_weights:
if id in self.responsive_users:
# to assign incentivization to each user based on how much the user is an influencer
controlled_MU[id] += self.b/len(influencers_weights) #* weight
return controlled_MU
class AI_Cont(object):
def __init__(self, pred_mis_timestamps, pred_norm_timestamps, adjaceny_matrix,
realization_id, config, norm_MU, norm_A, normal_timestamps_before_simu,
responsive_users = None):
self.config = config
self.MU = norm_MU
self.A = norm_A
self.pred_mis_stamps = pred_mis_timestamps
self.pred_norm_stamps = pred_norm_timestamps
self.adjacency_matrix = adjaceny_matrix
self.r_id = realization_id
self.bounds = config.realizations_bounds
self.responsive_users = responsive_users
self.parallel = config.parallel
self.normal_timestamps_before_simu = normal_timestamps_before_simu
self.verbose = config.verbose
def control(self):
"""controlling the network with learned hidden influence users. Modeling a knapsack problem"""
sn = SocialNetwork(self.pred_mis_stamps, self.pred_norm_stamps, self.adjacency_matrix, self.r_id,
self.bounds, self.config, self.MU, self.A, self.normal_timestamps_before_simu)
ks = Knapsack(range(len(self.MU)), self.config, sn)
las, consumed_budget = ks.random_walk(self.verbose, self.parallel)
del sn, ks
controlled_MU = self.MU.copy()
for user_id, _ in enumerate(self.MU):
controlled_MU[user_id] += las[user_id].current_state
return controlled_MU, consumed_budget
class AI_Cont_Pegypt(object):
def __init__(self, sample_code, pred_propaganda_timestamps, pred_non_propaganda_timestamps,
adjacency_matrix, MU, A, non_propaganda_timestamps_before_simu, realization_id,
config):
self.pred_propaganda_timestamps = pred_propaganda_timestamps
self.non_propaganda_timestamps = pred_non_propaganda_timestamps
self.non_propaganda_timestamps_before_simu = non_propaganda_timestamps_before_simu
self.adj_mtrx = adjacency_matrix
self.MU = MU
self.A = A
self.sample_code = sample_code
self.r_id = realization_id
self.bounds = config.realizations_bounds_A
self.config = config
self.verbose = config.verbose
def control(self):
"""controlling the network with learned hidden influence users. Modeling a knapsack problem"""
sn = SocialNetwork(self.pred_propaganda_timestamps, self.non_propaganda_timestamps, self.adj_mtrx, self.r_id,
self.bounds, self.config, self.MU, self.A, self.non_propaganda_timestamps_before_simu)
ks = Knapsack(range(len(self.MU)), self.config, sn)
las, consumed_budget = ks.random_walk(self.sample_code, loss_function = 1)
f = open("loss_1_probs/" + str(random.random()) + "_temporal_bias_probs.pkl", "wb")
pickle.dump(sn.probs_bias_temporal, f)
f.close()
f = open("loss_1_probs/" + str(random.random()) + "_temporal_circles_probs.pkl", "wb")
pickle.dump(sn.probs_circles_temporal, f)
f.close()
del sn, ks
controlled_MU = self.MU.copy()
state_probs = []
for user_id, _ in enumerate(self.MU):
controlled_MU[user_id] += las[user_id].current_state
return controlled_MU, consumed_budget
class AI_Cont_Pegypt_circles(object):
def __init__(self, sample_code, pred_propaganda_timestamps, pred_non_propaganda_timestamps,
adjacency_matrix, MU, A, non_propaganda_timestamps_before_simu, realization_id,
config):
self.pred_propaganda_timestamps = pred_propaganda_timestamps
self.non_propaganda_timestamps = pred_non_propaganda_timestamps
self.non_propaganda_timestamps_before_simu = non_propaganda_timestamps_before_simu
self.sample_code = sample_code
self.adj_mtrx = adjacency_matrix
self.MU = MU
self.A = A
self.r_id = realization_id
self.bounds = config.realizations_bounds_A
self.config = config
self.verbose = config.verbose
def control(self, mapping_info, probs_tracker, bias_against_MU, bias_against_A, hawkes_bias_against,
B_MU, B_A, hawkes_B):
"""controlling the network with learned hidden influence users. Modeling a knapsack problem"""
sn = SocialNetwork(self.pred_propaganda_timestamps, self.non_propaganda_timestamps, self.adj_mtrx, self.r_id,
self.bounds, self.config, self.MU, self.A, self.non_propaganda_timestamps_before_simu,
mapping_info, probs_tracker, bias_against_MU, bias_against_A, hawkes_bias_against, B_MU, B_A, hawkes_B)
ks = Knapsack(range(len(self.MU)), self.config, sn)
las, consumed_budget = ks.random_walk(self.sample_code, loss_function = 2)
f = open("loss_2_probs/" + str(random.random())+"_temporal_bias_probs.pkl", "wb")
pickle.dump(sn.probs_bias_temporal, f)
f.close()
f = open("loss_2_probs/" + str(random.random()) + "_temporal_circles_probs.pkl", "wb")
pickle.dump(sn.probs_circles_temporal, f)
f.close()
del sn, ks
controlled_MU = self.MU.copy()
state_probs = []
for user_id, _ in enumerate(self.MU):
controlled_MU[user_id] += las[user_id].current_state
return controlled_MU, consumed_budget