-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (43 loc) · 1.35 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2014 Joseph M. Sleiman
# This program is free software; you can redistribute it and/or
# modify it under the terms of the LGPLv2.1 or LGPLv3 license.
import random
import math
import os.path
import time
import sys
import node
import hypercube
# build instructions
# ls | xargs -I {} dot -Tpng {} -o {}.png
def main(d, largestWins):
n = pow(2, d)
nodeValues = range(n)
random.shuffle(nodeValues)
x = hypercube.HyperCube(d, nodeValues)
results = x.election(largestWins)
# details for prettifying the output
now = time.localtime()
out = "output-{0}{1}{2}-{3}{4}{5}".format(now.tm_year, now.tm_mon, now.tm_mday, now.tm_hour, now.tm_min, now.tm_sec)
zeroFillFactor = int(math.floor(math.log(len(results), 10)) + 1) + len(".dot")
if not os.path.exists(out):
os.makedirs(out)
for x in xrange(0, len(results)):
location = os.path.join(out, "{0}.dot".format(x).zfill(zeroFillFactor))
with open(location, "w+") as f:
f.write(results[x])
if __name__ == "__main__":
try:
d = int(sys.argv[1])
hl = sys.argv[2]
if(hl.lower() == "high"):
main(d, True)
elif (hl.lower() == "low"):
main(d, False)
else:
raise Exception("Election choice invalid")
except ValueError as e:
print e
print "usage: main.py [int: dimension of the hypercube] [high/low: elect highest/lowest]"