-
Notifications
You must be signed in to change notification settings - Fork 0
/
comparison.py
76 lines (68 loc) · 1.86 KB
/
comparison.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
from util import *
from metrics import *
from learncps import *
from inference import *
from collections import Counter
SUSHI = {
0: 'Shrimp',
1: 'Sea Eel',
2: 'Tuna',
3: 'Squid',
4: 'Sea Urchin',
5: 'Salmon Roe',
6: 'Egg',
7: 'Fatty Tuna',
8: 'Tuna Roll',
9: 'Cucumber',
}
def convert_to_sushi(rank):
'''
function convert_to_sushi
params: rank, a list (permutation) of numbers 1-9
returns: a list of the corresponding sushi names in each rank
'''
if type(rank) != list:
rank = list(rank)
if type(rank[0]) == set:
rank = [v.pop() for v in rank]
return [SUSHI[v] for v in rank]
def time_function(f, votes, candidates):
'''
fuction time_function
params: f, a function that takes one argument
arg, the argument that function f takes
returns: a tuple of the result of f(arg), and the time it took
to apply f(arg)
'''
start = time.time()
val = f(votes, candidates)
end = time.time() - start
return val, end
def convert_votes(sushi_votes):
'''
function convert_votes
params: sushi_votes, list of lists. each list a ranking over
objects 0 - (len(lst) - 1)
returns: votes, candidates as specified format by metrics.py
'''
candidates = list(xrange(len(sushi_votes[0])))
sushi_votes = Counter([tuple(vote) for vote in sushi_votes])
sushi_votes = list(sushi_votes.iteritems())
return sushi_votes, candidates
if __name__ == '__main__':
votes, ids = read_sushi_votes()
mvotes, cands = convert_votes(votes)
print "Read in Votes, calculating Borda Count"
result, tme = time_function(borda, mvotes, cands)
print convert_to_sushi(result)
print tme
print "Simpson Method"
result, tme = time_function(minimax, mvotes, cands)
print convert_to_sushi(result)
print tme
print "Instant Runoff"
result, tme = time_function(instantrunoff, mvotes, cands)
print convert_to_sushi(result)
print tme
print('\n\n\n')
check_inference()