示例#1
0
def main():
	for q in xrange(0, 1):
		# an array of posts and users
		content = [0] * num_posts
		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
			elif (user_bias_prob < user_no_group_prob + user_first_group_prob):
				user_bias = first_group
			else:
				user_bias = second_group
			# create a new user
			users.append(User(user_bias = user_bias, id = user_id, num_posts = num_posts))
			user_id += 1
			
		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
		blah = 0
		for i in xrange(iterate):
			# create three new posts every minute
			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)
				if (post_bias_prob < no_group_prob):
					post_bias = no_group
				elif (post_bias_prob < no_group_prob + first_group_prob):
					post_bias = first_group
				else:
					post_bias = second_group
				# create a new post
				content[content_id] = (Post(post_bias = post_bias, id = content_id, time = i))
				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])
					else:
						for k in xrange(last_hour):
							w = content_id - k - 1
							users[j].vote(post = content[w])
		
			# call page rank algorithm for reddit
			if (i % 30 == 0 and not i == 0):
				posts_ranking = []
				for j in xrange(content_id - 1):	
					post_data = [reddit.hot(content[j].ups, content[j].downs, content[j].date), content[j].id]
					posts_ranking.append(post_data)
					
				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
				
				posts_ranking = []
				for j in xrange(content_id - 1):	
					post_data = [sampling.hot(content[j].ups, content[j].downs, content[j].date), content[j].id]
					posts_ranking.append(post_data)
					
				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_sum2 += no_bias_number
				first_bias_sum2 += first_bias_number
				second_bias_sum2 += second_bias_number
				
				posts_ranking = []
				for j in xrange(content_id - 1):	
					post_data = [noisy.hot(content[j].ups, content[j].downs, content[j].date), content[j].id]
					posts_ranking.append(post_data)
					
				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_sum3 += no_bias_number
				first_bias_sum3 += first_bias_number
				second_bias_sum3 += second_bias_number
				
		total = no_bias_sum + first_bias_sum + second_bias_sum
		print '-----Reddit-----'
		print float(no_bias_sum) / total
		print float(first_bias_sum) / total
		print float(second_bias_sum) / total
		print '----Sampling----'
		print float(no_bias_sum2) / total
		print float(first_bias_sum2) / total
		print float(second_bias_sum2) / total
		print '-----Noisy------'
		print float(no_bias_sum3) / total
		print float(first_bias_sum3) / total
		print float(second_bias_sum3) / total
		print no_bias_sum + first_bias_sum + second_bias_sum
示例#2
0
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