Skip to content

Instantly share code, notes, and snippets.

@petr-kalinin
Created November 26, 2016 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petr-kalinin/deee9f8a933a2723a36b685d58da0dbe to your computer and use it in GitHub Desktop.
Save petr-kalinin/deee9f8a933a2723a36b685d58da0dbe to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import random
import math
from itertools import zip_longest
from pprint import pprint
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
class OptionsHolder:
def __init__(self):
self.options = []
self.weights = []
def add_option(self, option):
weight = 1
for pair in option:
r1 = pair[0][1]
r2 = pair[1][1]
addWeight = math.exp(-((r1 + r2)/2 - averageRating) ** 2 / D)
#addWeight *= math.exp(-(r1 - r2) ** 2 / 2 / D)
weight *= addWeight
self.options.append(option)
self.weights.append(weight)
def select_option(self):
#print(self.options)
#print(self.weights)
sumWeights = sum(self.weights)
rnd = random.uniform(0, sumWeights)
cur = 0
for i in range(len(self.options)):
cur += self.weights[i]
#print(options[i], cur, weights[i])
if cur > rnd:
print("returning ", self.weights[i])
return self.options[i]
#random.seed(124234)
data = open("users.txt").readlines()
data = [x.split() for x in data]
for x in data:
x[1] = int(x[1])
averageRating = sum([x[1] for x in data]) / len(data)
D = sum(x[1]**2 for x in data)/len(data) - averageRating**2
print("average = ", averageRating, " D = ", D, " ", math.sqrt(D))
D0 = D
D *= 0.5
first = OptionsHolder()
for i in range(100):
random.shuffle(data)
option = list(grouper(data, 2, ["_", averageRating - math.sqrt(D0)]))
first.add_option(option)
option = first.select_option()
with open("first.txt", "w") as f:
for pair in option:
f.write(pair[0][0] + " " + str(pair[0][1]) + " " + pair[1][0] + " " + str(pair[1][1]) + "\n")
firsts = []
seconds = []
for pair in option:
firsts.append(pair[0])
seconds.append(pair[1])
second = OptionsHolder()
for i in range(100):
random.shuffle(seconds)
option = list(zip(seconds, firsts))
second.add_option(option)
option = second.select_option()
with open("second.txt", "w") as f:
for pair in option:
f.write(pair[0][0] + " " + str(pair[0][1]) + " " + pair[1][0] + " " + str(pair[1][1]) + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment