def arena(mapfile, punter_executables, print_verbose=lambda x: None): num_punters = len(punter_executables) states = [0] * len(punter_executables) with open(mapfile) as f: themap = rwjson.readJson(f) serverstate = pt.map_to_nice(themap) #print 'serverstate is', serverstate all_moves = [] for punterid in range(num_punters): setup = {} setup['map'] = themap setup['punters'] = num_punters setup['punter'] = punterid ready = offline_call(['sh', '-c', punter_executables[punterid]], setup) states[punterid] = ready['state'] all_moves.append({'pass': {'punter': punterid}}) print_verbose('\nstates are {}'.format(states)) for movenum in range(len(serverstate['siteids'])): punterid = movenum % num_punters # the following passes just the most recent num_punters moves gameplay = { 'move': { 'moves': all_moves[-num_punters:] }, 'state': states[punterid], } result = offline_call(['sh', '-c', punter_executables[punterid]], gameplay) if result is None: all_moves.append({'pass': {'punter': punterid}}) print_verbose('bad result from punter') else: # print '\nresult is\n', result states[punterid] = result['state'] del result['state'] pt.update_nice(serverstate, [result]) all_moves.append(result) scores = [] for pid in range(num_punters): score = pt.score(serverstate, pid) print_verbose('score for {} ({}) is {}'.format( pid, punter_executables[pid], score)) scores.append((score, punter_executables[pid])) return list(reversed(sorted(scores)))
markersize=10) plt.legend(loc='best') if __name__ == "__main__": with open('examples/gameplay0.txt') as f: handshake = readMessage(f) messageIn = readMessage(f) nice = pt.map_to_nice(messageIn['move']['state']['map']) R = len(nice['riverdata']) plt.figure() for turn in range(0, 5): gameState = ['examples/gameplay' + str(turn) + '.txt'] # Currently the map and the gamestate are not coupled. # By that I mean that the state id's which are specified in the file gameplay # are the same as the id's for maps/sample.json only with open(gameState[0]) as f: handshake = readMessage(f) game = readMessage(f) pt.update_nice(nice, game['move']['moves']) punter = game['move']['moves'][0]['claim']['punter'] # print "punter", punter turns = 1 visualize_board(nice) plt.show()
claimx = [setup['sitemap'][source]['x'],setup['sites'][target]['x']] claimy = [setup['sites'][source]['y'],setup['sites'][target]['y']] plt.plot(claimx,claimy,farg[punter],linewidth = 2) for j in range(len(setup['siteids'])): plt.plot(setup['sites'][j]['x'],setup['sites'][j]['y'],'k.') for k in range(len(setup['mines'])): mineLoc = setup['mines'][k] plt.plot(setup['sites'][mineLoc]['x'],setup['sites'][mineLoc]['y'],'-ro') with open('examples/gameplay0.txt') as f: handshake = readMessage(f) messageIn = readMessage(f) setup = pt.map_to_nice(messageIn['move']['state']['map']) R = len(setup['riverdata']) farg = ['-y','-m','-m','-y','-w'] for turn in range(R): gameState = ['examples/gameplay'+str(turn)+'.txt'] with open(gameState[0]) as f: handshake = readMessage(f) game = readMessage(f) pt.update_nice(setup,game['move']['moves']) punter = game['move']['moves'][0]['claim']['punter'] visualize_board(setup) plt.show()
def arena(mapfile, punter_executables, print_verbose = lambda x: None, vis = False): if vis: plt.ion() num_punters = len(punter_executables) states = [0]*len(punter_executables) with open(mapfile) as f: themap = rwjson.readJson(f) serverstate = pt.map_to_nice(themap) #print 'serverstate is', serverstate all_moves = [] for punterid in range(num_punters): setup = {} setup['map'] = themap setup['punters'] = num_punters setup['punter'] = punterid ready = offline_call(['sh', '-c', punter_executables[punterid]], setup) states[punterid] = ready['state'] all_moves.append({'pass': {'punter': punterid}}) print_verbose('\nstates are {}'.format(states)) if vis: plt.clf() visualize.visualize_board(serverstate) plt.draw() plt.pause(1e-6) scores = [] for movenum in range(len(serverstate['riverdata'])): punterid = movenum % num_punters # the following passes just the most recent num_punters moves gameplay = { 'move': {'moves': all_moves[-num_punters:]}, 'state': states[punterid], } result = offline_call(['sh', '-c', punter_executables[punterid]], gameplay) if result is None: all_moves.append({'pass': {'punter': punterid}}) print_verbose('bad result from punter') else: # print '\nresult is\n', result states[punterid] = result['state'] del result['state'] pt.update_nice(serverstate, [result]) all_moves.append(result) if vis: plt.cla() legends = punter_executables if len(scores) > 0: legends = map(lambda (s,n): '{} - {}'.format(s,n), scores) visualize.visualize_board(serverstate, legends) plt.draw() plt.pause(1e-6) scores = [] for pid in range(num_punters): score = pt.score(serverstate, pid) print_verbose('[{}] score for {} ({}) is {}' .format(movenum, pid, punter_executables[pid], score)) scores.append((score, punter_executables[pid])) return list(sorted(scores))