-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspimTweek.py
More file actions
200 lines (158 loc) · 5.57 KB
/
spimTweek.py
File metadata and controls
200 lines (158 loc) · 5.57 KB
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
from subprocess import Popen;
from subprocess import PIPE;
import sys
try:
from collections import Counter
except ImportError:
try:
from recipe5766111 import Counter
except ImportError:
print "Counter not found"
sys.exit(1)
import time
import random
import signal
heatmapSupport =True
try:
import heatmap
except ImportError:
heatmapSupport = False
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
signal.signal(signal.SIGALRM, alarm_handler)
signal.alarm(20)
def graptokens(rand) :
if not type(rand) == str :
rand = "-randomseed "+str(rand(1355029990,1355039990))+" -randommap"
try :
inline= Popen("./QtSpimbot -file done.s "+rand+
" -maponly -debug -run -exit_when_done", stdout=PIPE, shell=True).stdout
string = "not"
pointList =[]
while(not (string == '')) :
string = inline.readline()
if string[:6] == "TOKEN:" :
nums=string[7:].split(" ")
pointList.append( (int(nums[0]),int(nums[1])))
return pointList
except Alarm:
killerror= Popen("killall QtSpimbot", stdout=PIPE, shell=True).stdout
print "error"
return []
def tokenheatmap(test_num,seed) :
hm = heatmap.Heatmap()
tokenList =[]
for x in xrange(0, test_num) :
signal.alarm(20)
tokenList+=graptokens(seed)
hm.heatmap(tokenList, 50, 120,(2048, 2048), 'classic', ((0,0),(300,300))).save("token_heat_map.png")
def runGame( seed_list, rand) :
if not type(rand) == str :
rand = "-randomseed "+str(rand(1355029990,1355039990))+" -randommap"
print rand
try :
inline= Popen("./QtSpimbot -file spimbot.s -run "+rand+ " -exit_when_done -maponly -quiet ", stdout=PIPE, shell=True).stdout
string = "not"
while(not (string == '')) :
string = inline.readline()
if string[:7] == "cycles:" :
return [string[7:-1]]
return ["error, What? This should not be so?"]
except Alarm:
print "your bot is too slow"
killerror= Popen("killall QtSpimbot", stdout=PIPE, shell=True).stdout
print killerror.read()
time.sleep(1)
seed_list.append(rand[12:-11])
return ["fail"]
def runTwoPlayers( seed_list, rand) :
if not type(rand) == str :
rand = "-randomseed "+str(rand(1355029990,1355039990))+" -randommap"
print rand
try :
inline= Popen("./QtSpimbot -file spimbot.s -file2 spimbot2.s -run "+rand+ " -exit_when_done -maponly -quiet ", stdout=PIPE, shell=True).stdout
string = "not"
out_come =[]
while(not (string == '')) :
string = inline.readline()
if string[:7] == "cycles:" :
out_come.append(string[7:-1])
if string[:7] == "winner:" :
out_come.append(string)
return out_come
return ["error, What? This should not be so?"]
except Alarm:
print "your bot is too slow"
killerror= Popen("killall QtSpimbot", stdout=PIPE, shell=True).stdout
print killerror.read()
time.sleep(1)
seed_list.append(rand[12:-11])
return ["fail"]
def runtests(run, test_num,seed_list,seed ="-randommap"):
for x in xrange(0, test_num) :
signal.alarm(30)
for y in run(seed_list,seed) :
count[y] +=1
print "histogram :"
print count.most_common()
min_time = None
for x in count.elements() :
if not x[:7] == "winner:" :
l=[min_time ,int(x)]
min_time= min(i for i in l if i is not None)
print "minimum time: ", min_time
max_time = None
for x in count.elements() :
if not x[:7] == "winner:" :
max_time= max(max_time, int(x))
print "max time: ", max_time
avg =0;
for x in count.elements() :
if x is not 'fail' :
if not x[0:7] == "winner:" :
avg+=int(x)
avg/=test_num
print "avg runtime: ", avg
if __name__ == "__main__" :
if (len(sys.argv) < 3) or str(sys.argv[0]) == "--help" :
print """
SpimBot testing tool by Silverdev
usage spimTweek <scan type> <number of times> <seed type> <seed>
--help: prints this
values
<scan type> : single, double, token
<number of times> : int
<seed type> : default, random, static <seed>, setOrder <seed>
"""
sys.exit(1)
test_num =int(sys.argv[2])
print str(sys.argv)
setting ={"single":0,"double":1,"token":3}
if setting[sys.argv[1]] is 3 :
if heatmapSupport :
tokenheatmap(test_num,"-randommap")
print "done"
sys.exit(0)
else :
print "\n \n Error: heatmap.py missing or misconfigured."
sys.exit(1)
gamerunner=lambda x,y,z: runtests((runGame,runTwoPlayers) \
[setting[str(sys.argv[1])]],x,y,z)
count = Counter()
r=random
if str(sys.argv[3]) == "default" :
r.seed("good luck")
if str(sys.argv[3]) == "setOrder" :
r.seed(sys.argv[4])
failed_seed_list = []
if str(sys.argv[3]) == "random" :
gamerunner(test_num,failed_seed_list,"-randommap")
elif str(sys.argv[3]) == "static" :
gamerunner(test_num,failed_seed_list,"-randomseed "+str(sys.argv[4]) \
+" -randommap")
else :
gamerunner(test_num, failed_seed_list, r.randint)
if not failed_seed_list ==[] :
print "the following seeds failed", failed_seed_list