-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHandleQueuedResults.py
647 lines (515 loc) · 25.2 KB
/
HandleQueuedResults.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#!/usr/bin/env python
#import cgi
import datetime
import wsgiref.handlers
try:
import json
except:
import simplejson as json
import string
import cPickle as pickle
#import pickle
from google.appengine.ext import db
#from google.appengine.api import users
from google.appengine.api import taskqueue
from google.appengine.ext import webapp
from google.appengine.api import memcache
#from operator import attrgetter
import random
import time
import zlib
import threading
import structures
import logging
from structures import global_dict
import numpy
import marshal
from collections import deque
global_sync = {}
#sync_lock = threading.Lock()
#write_lock = threading.Lock()
last_write = {}
locks = {}
def rreplace(s, old, new, occurrence):
li = s.rsplit(old, occurrence)
return new.join(li)
class HandleQueuedResults(webapp.RequestHandler):
def post(self):
global global_dict
#global_dict = {}
global global_sync
global locks
global last_write
#global sync_lock
# starttime = time.time()
results = json.loads(self.request.body)
rumble = results.get("game","ERROR")
syncname = str(bool(results.get("melee") == "YES")) + "|" + structures.sync
if syncname not in locks:
locks[syncname] = threading.Lock()
sync_lock = locks[syncname]
global_sync[syncname] = global_sync.get(syncname,{})
botsync = global_sync[syncname]
bota = results.get("fname")
botb = results.get("sname")
#bota = rreplace(bota,"_"," ",1)
#botb = rreplace(botb,"_"," ",1)
logging.debug("Bots : " + bota + " vs. " + botb)
bd = [[bota, rumble], [botb, rumble]]
botHashes = [string.join(a,"|").encode('ascii') for a in bd]
memget = [rumble]
memget.extend(botHashes) #botHashes.append(rumble)
memdict = memcache.get_multi(memget)
game = memdict.get(rumble,None)
game_future = None
if game is None:
#game = structures.Rumble.get_by_key_name(rumble)
game_future = db.get_async(db.Key.from_path('Rumble',rumble))
newBot = False
bots = [memdict.get(h,None) for h in botHashes]
pairingsarray = [[],[]]
botFutures = [None,None]
for i in [0, 1]:
if bots[i] is None or bots[i].PairingsList is None:
botFutures[i] = db.get_async(db.Key.from_path('BotEntry',botHashes[i]))
for i in [0,1]:
if botFutures[i] is not None:
modelbot = botFutures[i].get_result()
if modelbot is not None:
bots[i] = structures.CachedBotEntry(modelbot)
#logging.debug("retrieved from database")
for i in [0,1]:
if bots[i] is None:
modelbot = structures.BotEntry(key_name = botHashes[i],
Name = bd[i][0],Battles = 0, Pairings = 0, APS = 0.0,
Survival = 0.0, PL = 0, Rumble = rumble, Active = False,
PairingsList = zlib.compress(pickle.dumps([]),1))
bots[i] = structures.CachedBotEntry(modelbot)
newBot = True
if isinstance(bots[i],structures.BotEntry):
bots[i] = structures.CachedBotEntry(bots[i])
try:
pairingsarray[i] = pickle.loads(zlib.decompress(bots[i].PairingsList))
bots[i].PairingsList = None
except:
try:
pairsDicts = marshal.loads(zlib.decompress(bots[i].PairingsList))
pairingsarray[i] = [structures.ScoreSet() for _ in pairsDicts]
for s,d in zip(pairingsarray[i],pairsDicts):
s.__dict__.update(d)
bots[i].PairingsList = None
except:
pairingsarray[i] = []
if game_future is not None:
game = game_future.get_result()
if game is None:
game = structures.Rumble(key_name = rumble,
Name = rumble, Rounds = int(results["rounds"]),
Field = results["field"], Melee = bool(results["melee"] == "YES"),
Teams = bool(results["teams"] == "YES"), TotalUploads = 0,
MeleeSize = 10, ParticipantsScores = db.Blob(zlib.compress(pickle.dumps([]))))
self.response.out.write("CREATED NEW GAME TYPE " + rumble + "\n")
logging.info("Created new game type: " + rumble)
else:
field = game.Field == results["field"]
rounds = (game.Rounds == int(results["rounds"]))
teams = game.Teams == bool(results["teams"] == "YES")
melee = game.Melee == bool(results["melee"] == "YES")
allowed = field and rounds and teams and melee
if not allowed:
errstr = "OK. ERROR. Incorrect " + rumble + " config: "
errorReasons = []
if not field:
errorReasons.append("field size ")
if not rounds:
errorReasons.append("number of rounds ")
if not teams:
errorReasons.append("teams ")
if not melee:
errorReasons.append("melee ")
logging.error(errstr + string.join(errorReasons,", ") + " User: " + results["user"])
return
scores = None
try:
scores = pickle.loads(zlib.decompress(game.ParticipantsScores))
game.ParticipantsScores = None
if len(scores) == 0:
scores = {}
except:
scoresdicts = marshal.loads(zlib.decompress(game.ParticipantsScores))
game.ParticipantsScores = None
scoreslist = [structures.LiteBot(loadDict = d) for d in scoresdicts]
#for s,d in zip(scoreslist,scoresdicts):
# s.__dict__.update(d)
scores = {s.Name:s for s in scoreslist}
if len(scores) == 0:
scores = {}
#logging.debug("uncompressed scores: " + str(len(s)) + " compressed: " + str(a))
for i in [0,1]:
if not bots[i].Active or bots[i].Name not in scores:
bots[i].Active = True
scores[bots[i].Name] = structures.LiteBot(bots[i])
newBot = True
self.response.out.write("Added " + bd[i][0] + " to " + rumble + "\n")
logging.info("added new bot!")
#retrievetime = time.time() - starttime
scorea = float(results["fscore"])
scoreb = float(results["sscore"])
if scorea + scoreb > 0:
APSa = 100*scorea/(scorea+scoreb)
else:
APSa = 50#register a 0/0 as 50%
#APSb = 100 - APSa
survivala = float(results["fsurvival"])
survivalb = float(results["ssurvival"])
survivala = 100.0*survivala/game.Rounds
survivalb = 100.0*survivalb/game.Rounds
for b, pairings in zip(bots, pairingsarray):
#b.PairingsList = zlib.compress(marshal.dumps([s.__dict__ for s in pairings]),1)
if len(pairings) > 0:
removes = []
for p in pairings:
try:
p.APS = float(p.APS)
p.Survival = float(p.Survival)
p.Battles = int(p.Battles)
except:
removes.append(pairings.index(p))
continue
removes.sort(reverse=True)
for i in removes:
pairings.pop(i)
apair = None
for p in pairingsarray[0]:
if p.Name == botb:
apair = p
if apair is None:
apair = structures.ScoreSet(name = botb)
pairingsarray[0].append(apair)
bpair = None
for p in pairingsarray[1]:
if p.Name == bota:
bpair = p
if bpair is None:
bpair = structures.ScoreSet(name = bota)
pairingsarray[1].append(bpair)
#participantsSet = set(game.Participants)
for b, pairings in zip(bots, pairingsarray):
i = 0
while i < len(pairings):
if pairings[i].Name == b.Name:
pairings.pop(i)
continue
if pairings[i].Name in scores:
pairings[i].Alive = True
else:
pairings[i].Alive = False
i += 1
#b.Pairings = i
aBattles = apair.Battles
#rolling average with a half-life of 10k
maxPerPair = 10000 / len(bots)
if aBattles > maxPerPair:
aBattles = maxPerPair
#bBattles = bpair.Battles
inv_ab = 1.0/(aBattles + 1.0)
apair.APS *= float(aBattles)*inv_ab
apair.APS += APSa*inv_ab
apair.__dict__["Min_APS"] = min(APSa,apair.__dict__.get("Min_APS",100))
#bpair.APS *= float(bBattles)/(bBattles + 1)
#bpair.APS += APSb/(bBattles+1)
bpair.APS = 100 - apair.APS
bpair.__dict__["Min_APS"] = min(100-APSa,bpair.__dict__.get("Min_APS",100))
apair.Survival *= float(aBattles)*inv_ab
apair.Survival += survivala*inv_ab
bpair.Survival *= float(aBattles)*inv_ab
bpair.Survival += survivalb*inv_ab
apair.Battles += 1
bpair.Battles = apair.Battles
apair.LastUpload = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
bpair.LastUpload = apair.LastUpload
for b, pairings in zip(bots, pairingsarray):
aps = 0.0
survival = 0.0
pl = 0
battles = 0
alivePairings = 0
if len(pairings) > 0:
for p in pairings:
if p.Alive:
aps += p.APS
survival += p.Survival
if p.APS > 50:
pl += 1
else:
pl -= 1
battles += p.Battles
alivePairings += 1
aps /= alivePairings
survival /= alivePairings
b.APS = aps
b.Survival = survival
b.PL = pl
b.Battles = battles
b.PairingsList = db.Blob(zlib.compress(pickle.dumps(pairings,pickle.HIGHEST_PROTOCOL),1))
b.LastUpload = apair.LastUpload
b.Pairings = alivePairings
game.TotalUploads += 1
#self.response.out.write("<" + str(bots[0].Battles) + " " + str(bots[1].Battles) + ">")
game.LastUpload = apair.LastUpload
game.AvgBattles = game.AvgBattles * 0.99 + 0.005 * (bots[0].Battles + bots[1].Battles)
if game.Uploaders is None:
uploaders = None
else:
uploaders = pickle.loads(zlib.decompress(game.Uploaders))
if uploaders is None or len(uploaders) == 0:
uploaders = {}
uploaderName = results["user"]
try:
uploader = uploaders[uploaderName]
uploader.latest = apair.LastUpload
uploader.total += 1
except KeyError:
uploader = structures.User(name=uploaderName)
uploaders[uploaderName] = uploader
game.Uploaders = zlib.compress(pickle.dumps(uploaders,-1),1)
for b in bots:
try:
bUploaders = b.Uploaders
if uploaderName not in bUploaders:
bUploaders.append(uploaderName)
except:
b.__dict__["Uploaders"] = [uploaderName]
with sync_lock:
for b in bots:
key = None
if isinstance(b,structures.BotEntry):
key = b.key().name()
else:
key = b.key_name
botsync[key] = botsync.get(key,0) + 1
minSize = min(10,len(scores)/2)
wrote = False
logging.debug("botsync: " + str(len(botsync)))
if len(botsync) > minSize:
syncset = botsync.keys()
syncbotsDict = memcache.get_multi(syncset)
syncbots = []
for sb in syncset:
b = syncbotsDict.get(sb,None)
if b is None or b.PairingsList is None:
syncbotsDict.pop(sb,1)
else:
syncbots.append(b)
botsync.pop(sb,1)
try:
thisput = []
while len(syncbots) > 0:
b = syncbots.pop()
key = None
if isinstance(b,structures.BotEntry):
key = b.key().name()
else:
key = b.key_name
putb = structures.BotEntry(key_name = key)
putb.init_from_cache(b)
thisput.append(putb)
db.put(thisput)
logging.info("wrote " + str(len(thisput)) + " results to database")
for b in thisput:
s = b.key().name()
botsync.pop(s,1)
syncbotsDict.pop(s,1)
wrote = True
except Exception, e:
logging.error('Failed to write data: '+ str(e))
#self.response.out.write('Failed to write data: '+ str(e.__class__))
for b in bots:
if b.Name in scores:
lb = scores[b.Name]
b.ANPP = lb.ANPP
b.VoteScore = lb.VoteScore
scores.pop(b.Name,1)
scores[b.Name] = structures.LiteBot(b)
game.ParticipantsScores = db.Blob(zlib.compress(pickle.dumps(scores,pickle.HIGHEST_PROTOCOL),1))
game.Participants = []
if game.BatchScoresAccurate:
game.BatchScoresAccurate = False
if not newBot:
game.put()
wrote = True
if newBot or wrote:
game.put()
#db.put(bots)
memcache.delete("home")
botsDict = {rumble:game}
global_dict[rumble] = game
for b in bots:
if isinstance(b,structures.BotEntry):
b = structures.CachedBotEntry(b)
key = b.key_name
botsDict[key] = b
memcache.set_multi(botsDict)
# puttime = time.time() - scorestime - retrievetime - starttime
scoreVals = scores.values()
maxPairs = len(scoreVals) - 1
if game.PriorityBattles and ((not game.Melee) or
(random.random() < 0.0222
or bots[0].Pairings != maxPairs or bots[1].Pairings != maxPairs
or min([bots[0].Battles,bots[1].Battles]) == min([b.Battles for b in scoreVals])
)): # or min(bots, key = lambda b: b.Battles) < game.AvgBattles):
#do a gradient descent to the lowest battled pairings:
#1: take the bot of this pair which has less pairings/battles
#2: find an empty pairing or take a low pairing
#3: ????
#4: PROFIT!!!
priobot = None
priopairs = None
#logging.debug("Maxpairs: " + str(maxPairs))
#let's do a direct search since we already have the data
#First look for bots missing pairings
missingPairings = filter(lambda b: b.Pairings != maxPairs,scoreVals)
#logging.debug("missingPairings: " + str(len(missingPairings)))
priobot2 = None
if len(missingPairings) > 0:
total = 0
#weight bots based on how different they are from desired pairings
weighted = [(abs(maxPairs - b.Pairings),b) for b in missingPairings]
#select them for battles according to that weight
for t in weighted:
total += t[0]
running = 0
point = random.randint(0,total-1)
for t in weighted:
running += t[0]
if running > point:
priobot = t[1]
break
#check if we have the selected bot already loaded
if priobot.Name == bots[0].Name:
priobot = bots[0]
priopairs = pairingsarray[0]
elif priobot.Name == bots[1].Name:
priobot = bots[1]
priopairs = pairingsarray[1]
else:
#if not try to load them from memcache, but only if we need a special battle
if priobot.Pairings < maxPairs:
bhash = priobot.Name + "|" + rumble
fullPrioBot = memcache.get(bhash)
else:
fullPrioBot = None
if fullPrioBot:
priopairs = pickle.loads(zlib.decompress(fullPrioBot.PairingsList))
logging.info("memcache lookup shortcut to local search")
else:
#if they aren't in memcache give them a generic battle
priobot2 = random.choice(scoreVals).Name
catch = 10
while priobot2 == priobot.Name and catch > 0:
priobot2 = random.choice(scoreVals).Name
catch -= 1
if catch == 0:
priobot2 = None
logging.info("repeatedly found same bot for prio")
else:
logging.info("global min search successful for non-paired bot")
if priobot != None and priobot2 == None:
#create the first battle of a new pairing
#cache pairings into dictionary to speed lookups against entire rumble
pairsdict = {}
for b in priopairs:
pairsdict[b.Name] = b
#select all unpaired bots
possPairs = []
for p in scores:
if p not in pairsdict and p != priobot.Name:
possPairs.append(p)
if len(possPairs) > 0:
#choose a random new pairing to prevent overlap
priobot2 = random.choice(possPairs)
logging.info("successful local search for new pair")
else:
logging.info("unsuccessful local search for new pair")
else:
minBattles = 1.1*min([b.Battles for b in scoreVals if b.Active])
possBots = filter(lambda b: b.Battles <= minBattles and b.Active, scoreVals )
names = [b.Name for b in possBots]
if bots[1].Name not in names and bots[0].Name not in names:
priobot = random.choice(possBots)
bhash = priobot.Name + "|" + rumble
fullPrioBot = memcache.get(bhash)
if fullPrioBot:
priopairs = pickle.loads(zlib.decompress(fullPrioBot.PairingsList))
logging.info("memcache lookup shortcut to global search")
else:
priobot2 = random.choice(scoreVals).Name
catch = 10
while priobot2 == priobot.Name and catch > 0:
priobot2 = random.choice(scoreVals).Name
catch -= 1
if catch == 0:
priobot2 = None
logging.info("repeatedly found same bot for prio")
else:
logging.info("global min search successful for low-battled bot")
elif bots[0].Battles <= bots[1].Battles:
priobot = bots[0]
priopairs = pairingsarray[0]
else:
priobot = bots[1]
priopairs = pairingsarray[1]
if priobot2 is None and priopairs is not None:
#sort for lowest battled pairing
#priopairs = sorted(priopairs, key = lambda score: score.Battles)
minbat = min([p.Battles for p in priopairs if p.Name in scores and scores[p.Name].Active])
possPairs =filter(lambda p: p.Battles <= minbat and p.Name != priobot.Name and p.Name in scores and scores[p.Name].Active,priopairs)
if len(possPairs) < min(50,0.5*len(scores)):
possPairs = filter(lambda p: p.Battles <= minbat + 1 and p.Name != priobot.Name and p.Name in scores and scores[p.Name].Active,priopairs)
if len( possPairs) > 0:
priobot2 = random.choice(possPairs).Name
logging.info("successful local search for low-battled pair")
#choose low battles, but still random - prevents lots of duplicates
else:
logging.info("unsuccessful local search for low-battled pair")
if priobot is not None and priobot2 is not None:
priobots = [priobot.Name,priobot2]
priobots = [b.replace(' ','_') for b in priobots]
prio_string = "[" + string.join(priobots,",") + "]\n"
logging.info("adding priority battle: " + prio_string + ", " + rumble)
rq_name = rumble + "|queue"
try:
rumble_queue = global_dict[rq_name]
rumble_queue.append(prio_string)
except KeyError:
logging.info("No queue for rumble " + rumble + ", adding one!")
global_dict[rq_name] = deque()
rumble_queue = global_dict[rq_name]
rumble_queue.append(prio_string)
else:
logging.info("no suitable priority battle found in " + rumble)
if priobot is None:
logging.info("priobot is None")
else:
logging.info("priobot2 is None")
else:
logging.info("no priority battle attempted for " + rumble)
# priotime = time.time() - puttime - scorestime - retrievetime - starttime
self.response.out.write("\nOK. " + bota + " vs " + botb + " received")
#time.sleep(0.5)
#self.response.out.write(" retrieve:" + str(int(round(retrievetime*1000))) + "ms")
#self.response.out.write(" scores:" + str(int(round(scorestime*1000))) + "ms")
#self.response.out.write(" priority battles:" + str(int(round(priotime*1000))) + "ms")
#self.response.out.write(" put:" + str(int(round(puttime*1000))) + "ms")
#self.response.out.write(", wrote " + str(syncsize) + " bots")
#self.response.out.write(", " + str(len(sync)) + " bots waiting to write.")
#self.response.out.write(", newBot: " + str(newBot))
# if put_result is not None:
# put_result.get_result()
application = webapp.WSGIApplication([
('/HandleQueuedResults', HandleQueuedResults)
], debug=True)
def main():
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()