-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.py
216 lines (177 loc) · 6.12 KB
/
main2.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
# simulates multiple days of posts and users in a social content website
import random
import detector
import reddit
import time
from user2 import User2
from post2 import Post2
# number of days in the simulation
num_days = 1
# number of users
num_users = 100
num_posts = 4320 #Length of day times 3
# possible groups a post or user can side with
no_group = 0
first_group = 1
second_group = 2
# probabilities of a post belonging to a collusive group
no_group_prob = .85
first_group_prob = .075
second_group_prob = .075
# probabilities of users belong to collusive groups
user_no_group_prob = .90
user_first_group_prob = .05
user_second_group_prob = .05
# textfile with all of the reports for each minute
textfile = file("SimulationReport.txt", "wt")
# print out a report of the last minute
#def report(minute):
#textfile.write(str(minute / 30) + '\n')
# run the simulation
def main():
# an array of posts and users
content = [0] * num_posts
users = []
nb_users = []
fg_users = []
sg_users = []
ranking = [0] * num_posts
# ids of posts and users
user_id = 0
content_id = 0
# initialize the number of users, create collusions
for i in xrange(num_users):
# determine user bias if any
user_bias = -1
user_bias_prob = random.uniform(0, 1)
if (user_bias_prob < user_no_group_prob):
user_bias = no_group
# create a new user
nb_users.append(User2(user_bias = user_bias, id = user_id, num_posts = num_posts))
user_id += 1
elif (user_bias_prob < user_no_group_prob + user_first_group_prob):
user_bias = first_group
# create a new user
fg_users.append(User2(user_bias = user_bias, id = user_id, num_posts = num_posts))
user_id += 1
else:
user_bias = second_group
# create a new user
sg_users.append(User2(user_bias = user_bias, id = user_id, num_posts = num_posts))
user_id += 1
users.append(User2(user_bias = user_bias, id = user_id, num_posts = num_posts))
no_bias_sum = 0
first_bias_sum = 0
second_bias_sum = 0
no_bias_sum2 = 0
first_bias_sum2 = 0
second_bias_sum2 = 0
no_bias_sum3 = 0
first_bias_sum3 = 0
second_bias_sum3 = 0
# run simulation by the minute
iterate = 60 * 24 * num_days
for i in xrange(iterate):
# create three new posts every minute
# raw_input("Press Enter to continue...")
number_new_posts = 3
# number of new posts a minute
for j in xrange(number_new_posts):
# determine post bias if any for new post
post_bias = -1
post_bias_prob = random.uniform(0, 1)
this_post_poster_id = -1
if (post_bias_prob < no_group_prob):
post_bias = no_group
this_post_poster_id = random.choice(nb_users).id
elif (post_bias_prob < no_group_prob + first_group_prob):
post_bias = first_group
if (not fg_users):
this_post_poster_id = 0
else:
this_post_poster_id = random.choice(fg_users).id
else:
if (not sg_users):
this_post_poster_id = 0
else:
this_post_poster_id = random.choice(sg_users).id
# create a new post
content[content_id] = (Post2(post_bias = post_bias, id = content_id, time = i, poster_id = this_post_poster_id))
content_id += 1
# go through all users and see if they will upvote/downvote posts
for j in xrange(num_users):
var = random.uniform(0, 1)
if (var > 0.0):
# look at the past hour
last_hour = 60 * 3
if (content_id < last_hour):
for k in xrange(content_id):
users[j].vote(post = content[k],poster = users[content[k].poster_id])
else:
for k in xrange(last_hour):
w = content_id - k - 1
users[j].vote(post = content[w], poster = users[content[w].poster_id])
# call page rank algorithm for reddit
if (i % 30 == 0 and not i == 0):
#Detector
posts_ranking = []
different = 0
summation = 0
for j in xrange(content_id - 1):
if (content[j].alt_ups != content[j].ups):
different += 1
summation += 1
post_data = [detector.hot(content[j].alt_ups, content[j].alt_downs, content[j].date), content[j].id]
posts_ranking.append(post_data)
# print "Percent different == %f" % (float(different)/float(summation))
sorted_by_second = sorted(posts_ranking, key=lambda tup: tup[0], reverse = True)
no_bias_number = 0
first_bias_number = 0
second_bias_number = 0
for b in xrange(0, 30):
id = sorted_by_second[b][1]
if (content[id].post_bias == no_group):
no_bias_number += 1
elif (content[id].post_bias == first_group):
first_bias_number += 1
else:
second_bias_number += 1
no_bias_sum += no_bias_number
first_bias_sum += first_bias_number
second_bias_sum += second_bias_number
#Reddit
posts_ranking2 = []
for j in xrange(content_id - 1):
post_data2 = [reddit.hot(content[j].ups, content[j].downs, content[j].date), content[j].id]
posts_ranking2.append(post_data)
sorted_by_second2 = sorted(posts_ranking2, key=lambda tup: tup[0], reverse = True)
no_bias_number2 = 0
first_bias_number2 = 0
second_bias_number2 = 0
for b in xrange(0, 30):
id = sorted_by_second[b][1]
if (content[id].post_bias == no_group):
no_bias_number2 += 1
elif (content[id].post_bias == first_group):
first_bias_number2 += 1
else:
second_bias_number2 += 1
no_bias_sum2 += no_bias_number2
first_bias_sum2 += first_bias_number2
second_bias_sum2 += second_bias_number2
total = no_bias_sum + first_bias_sum + second_bias_sum
print '-------------------------------------'
print '-----Detector------'
print float(no_bias_sum) / total
print float(first_bias_sum) / total
print float(second_bias_sum) / total
total = no_bias_sum2 + first_bias_sum2 + second_bias_sum2
print '-------------------------------------'
print '-----Reddit------'
print float(no_bias_sum2) / total
print float(first_bias_sum2) / total
print float(second_bias_sum2) / total
if __name__ == '__main__':
start_time = time.time()
main()
print time.time() - start_time