def start(args): logger = utils.default_logger_setup(logging.INFO) logger.info('Writing to ' + args.outdir) config.set_cuda(args.cuda) p1dir = os.path.join(args.outdir, 'p1') p2dir = os.path.join(args.outdir, 'p2') utils.mkdir_p(p1dir) utils.mkdir_p(p2dir) prog = os.path.join( config.get('showdown_root'), config.get('showdown_server_dir'), 'pokemon-showdown') game = Game(options = formats.get(args.format), prog = prog) policy_1 = torch_policy.load(args.p1) policy_2 = torch_policy.load(args.p2) wins = [0, 0] logger.info('starting...') # TODO: make multithreaded. Maybe integrate this with RL experiment for i in range(args.num_matches): p1 = EnginePkmnPlayer(policy_1, '%s-p1' % i, play_best_move = args.play_best_move in ['p1', 'both']) p2 = EnginePkmnPlayer(policy_2, '%s-p2' % i, play_best_move = args.play_best_move in ['p2', 'both']) game.play(p1, p2) for j in [0, 1]: player = [p1, p2][j] dirname = [p1dir, p2dir][j] bogger = battlelogs.BattleLogger(player.gid, dirname) for block in player.blocks: bogger.log(block) bogger.close() if player.result == 'winner': wins[j] += 1 else: assert player.result in ['loser', 'tie'] return wins
def main(): args = parse_args() config.set_cuda(False) from metagrok import remote_debug remote_debug.listen() p1_policy = torch_policy.load(args.policy_tag) p2_policy = p1_policy if args.p2_policy_tag: p2_policy = torch_policy.load(args.p2_policy_tag) fmt = formats.get(args.fmt) game = Game(fmt, '{}/{}/pokemon-showdown'.format( config.get('showdown_root'), config.get('showdown_server_dir'))) count = 0 while True: time.sleep(0.1) r = sys.stdin.readline().strip() if r == 'done': break battle_dir = os.path.join('/tmp', args.id, '%06d' % count) utils.mkdir_p(battle_dir) p1 = EnginePkmnPlayer(p1_policy, 'p1', epsilon = args.epsilon) p2 = EnginePkmnPlayer(p2_policy, 'p2', epsilon = args.epsilon) game.play(p1, p2) num_blocks = 0 for i, player in enumerate([p1, p2]): blogger = battlelogs.BattleLogger('p%d' % (i + 1), battle_dir) for block in player.blocks: blogger.log(block) num_blocks += 1 blogger.close() count += 1 sys.stdout.write('%s\t%d\n' % (battle_dir, num_blocks)) sys.stdout.flush()
def create_type_chart(): rv = {} type_chart_fname = '{}/BattleTypeChart.json'.format(config.get('dex_root')) for defender_type, type_data in json.load(type_chart_fname).items(): defender_type = utils.to_id(defender_type) attacker_data = {} for attacker_type, modifier in type_data['damageTaken'].items(): attacker_type = utils.to_id(attacker_type) if modifier == 0: multiplier = 1. elif modifier == 1: multiplier = 2. elif modifier == 2: multiplier = 0.5 elif modifier == 3: multiplier = 0. else: raise ValueError('Unknown modifier %s' % modifier) attacker_data[attacker_type] = multiplier rv[defender_type] = attacker_data return rv
if modifier == 0: multiplier = 1. elif modifier == 1: multiplier = 2. elif modifier == 2: multiplier = 0.5 elif modifier == 3: multiplier = 0. else: raise ValueError('Unknown modifier %s' % modifier) attacker_data[attacker_type] = multiplier rv[defender_type] = attacker_data return rv Movedex = json.load('{}/BattleMovedex.json'.format(config.get('dex_root'))) TypeChart = create_type_chart() class RandomPlayer(object): def act(self, state, candidates): probs = np.asarray([1. if c else 0. for c in candidates]) probs = probs / probs.sum() return dict(probs = probs) class MostDamageMovePlayer(object): ''' Ranks actions in the following order: - damaging moves (sort by power, then move modifier) - zmove - ultra - mega
def start(args): logger = utils.default_logger_setup(logging.INFO) logger.info('Writing to ' + args.outdir) config.set_cuda(args.cuda) p1dir = os.path.join(args.outdir, 'p1') p2dir = os.path.join(args.outdir, 'p2') utils.mkdir_p(p1dir) utils.mkdir_p(p2dir) prog = os.path.join( config.get('showdown_root'), config.get('showdown_server_dir'), 'pokemon-showdown') #game = Game(options = formats.get(args.format), prog = prog) policy_1 = torch_policy.load(args.p1) policy_2 = torch_policy.load(args.p2) wins = [0, 0] logger.info('starting...') # TODO: make multithreaded. Maybe integrate this with RL experiment p1_teams, p2_teams = tg.init_lc_thunderdome() strategy_agent = team_choice.AgentStrategyProfile(p1_teams, p2_teams) for i in range(args.num_matches): #team1_ind = team_choice.teamchoice_random(formats.ou_teams) team1_ind = strategy_agent.select_action() team2_ind = strategy_agent.select_action_p2() game = Game(options = strategy_agent.get_teams(team1_ind, team2_ind), prog = prog) p1 = EnginePkmnPlayer(policy_1, '%s-p1' % i, play_best_move = args.play_best_move in ['p1', 'both']) p2 = EnginePkmnPlayer(policy_2, '%s-p2' % i, play_best_move = args.play_best_move in ['p2', 'both']) game.play(p1, p2) for j in [0, 1]: player = [p1, p2][j] dirname = [p1dir, p2dir][j] bogger = battlelogs.BattleLogger(player.gid, dirname) for block in player.blocks: bogger.log(block) bogger.close() if j == 0: if player.result == 'winner': strategy_agent.update(team1_ind, team2_ind, p1_win=True) else: strategy_agent.update(team1_ind, team2_ind, p1_win=False) if player.result == 'winner': wins[j] += 1 else: assert player.result in ['loser', 'tie'] print(strategy_agent.get_utility_matrix()) with open("lc_thunderdome_results.txt", "w+") as wf: for ct, team in enumerate(strategy_agent.p1_teams): wf.write("{}\t{}\n".format(ct, team)) for ct, team in enumerate(strategy_agent.p2_teams): wf.write("{}\t{}\n".format(ct, team)) # wf.write(strategy_agent.get_utility_matrix()) wf.write("\n") wf.flush() wf.close() return wins
name = to_id(name) if name.startswith('hiddenpower'): match = HP_REGEX.match(name) if match: name = 'hiddenpower' + match.group(1) elif name.startswith('return') and RETURN_REGEX.match(name): name = 'return' if name not in self.name_to_index: logger.warn('Could not find %s in %s', name, self.feature_key) return 1 return self.name_to_index[name] def nidx(self, names): if not isinstance(names, (list, tuple)): names = (names,) return [self.to_index(n) for n in names] def nhot(self, names): if not isinstance(names, (list, tuple)): names = (names,) rv = np.zeros(self.size, dtype = config.nt()) for name in names: rv[self.to_index(name)] = 1 return rv DEX = Dex.create(config.get('dex_root')) if __name__ == '__main__': print(DEX.gen7rb_stats().describe())
def script(): return fileio.read('{}/engine.js'.format(config.get('metagrok_client_root')))
def load_test_data(): return jsons.load('{}/reward-shaper-tests.jsons'.format( config.get('test_data_root')))