def play_game(config_0, config_1, game_size=11, move_time=10.0): # add players gm = GameMaster(get_gdl_for_game("hexLG%s" % game_size)) gm.add_player(PUCTPlayer(config_0), "black") gm.add_player(PUCTPlayer(config_1), "white") # play move via gamemaster: gm.reset() gm.start(meta_time=15, move_time=move_time) def remove_gdl(m): return m.replace("(place ", "").replace(")", "").strip().replace(' ', '') move = None sgf_moves = [] while not gm.finished(): move = gm.play_single_move(last_move=move) if move[0] == "noop": ri, str_move = 1, remove_gdl(move[1]) else: ri, str_move = 0, remove_gdl(move[0]) sgf_moves.append((ri, str_move)) if str_move == "swap": sgf_moves[0] = (0, swapaxis(sgf_moves[0][1])) for ri, m in sgf_moves: print ri, m x = hashlib.md5(hashlib.sha1("%.5f" % time.time()).hexdigest()).hexdigest()[:6] with open("game_%s_%s_%s.sgf" % (config_0.name, config_1.name, x), "w") as f: f.write("(;FF[4]EV[null]PB[%s]PW[%s]SZ[%s]GC[game#%s];" % (config_1.name, config_0.name, game_size, x)) # piece colours are swapped for hexgui from LG for ri, m in sgf_moves: f.write("%s[%s];" % ("W" if ri == 0 else "B", m)) f.write(")\n")
def get_gm(self, players, move_time, moves): gm = GameMaster(self.game_info) sm = self.game_info.get_sm() for player, role in zip(players, sm.get_roles()): gm.add_player(player, role) if moves: _, _, initial_state, match_depth = self.make_moves(moves) else: match_depth = 0 initial_state = None gm.reset() if initial_state: gm.start(meta_time=15, initial_basestate=initial_state, game_depth=match_depth, # XXX rename to match_depth on gm move_time=move_time) else: gm.start(meta_time=15, move_time=move_time) return gm, match_depth
def play(moves, move_time): # add players gm = GameMaster(get_gdl_for_game("breakthroughSmall")) for role in gm.sm.get_roles(): gm.add_player(PUCTPlayer(conf=compete), role) sm = lookup.by_name("breakthroughSmall").get_sm() sm.reset() pretty_board(sm) # get some states joint_move = sm.get_joint_move() base_state = sm.get_initial_state() def to_cords(s): mapping_x_cord = {x0: x1 for x0, x1 in zip('abcdef', '654321')} return mapping_x_cord[s[0]], s[1] def f(ri, i): return sm.legal_to_move(ri, ls.get_legal(i)) lead_role_index = 0 gdl_moves = [] for m in moves: from_, to_ = map(to_cords, m.split("-")) gdl_move = [] for ri in range(2): if ri == lead_role_index: gdl_role_move = "(move %s %s %s %s)" % (from_[0], from_[1], to_[0], to_[1]) else: gdl_role_move = "noop" ls = sm.get_legal_state(ri) the_moves = [f(ri, ii) for ii in range(ls.get_count())] choice = the_moves.index(gdl_role_move) joint_move.set(ri, ls.get_legal(choice)) gdl_move.append(str(gdl_role_move)) # update state machine sm.next_state(joint_move, base_state) sm.update_bases(base_state) lead_role_index ^= 1 gdl_moves.append(str(gdl_move)) print "%s -> %s" % (m, gdl_move) pretty_board(sm) # play move via gamemaster: gm.reset() gm.start(meta_time=15, move_time=move_time, initial_basestate=sm.get_current_state(), game_depth=len(moves)) move = gm.play_single_move(last_move=None) player = gm.get_player(lead_role_index) move = move[lead_role_index] move = move.replace("(move", "").replace(")", "") a, b, c, d = move.split() mapping_x_cord = {x0: x1 for x0, x1 in zip('654321', 'abcdef')} next_move = "%s%s-%s%s" % (mapping_x_cord[a], b, mapping_x_cord[c], d) sm.update_bases(gm.sm.get_current_state()) pretty_board(sm) print "PLAYED", next_move, player.last_probability return next_move, player.last_probability
class GameMasterByGame(object): def __init__(self, game_config): assert isinstance(game_config, GameConfig) self.game_config = game_config game_name = self.game_config.game_name if game_name == "breakthrough2": game_name = "breakthrough" self.gm = GameMaster(get_gdl_for_game(game_name)) # add players puct_conf = self.get_puct_config() for role in self.gm.sm.get_roles(): self.gm.add_player(puctplayer.PUCTPlayer(conf=puct_conf), role) def get_puct_config(self): multiplier = self.game_config.sims_multiplier if multiplier == 0: playouts_per_iteration = 1 else: playouts_per_iteration = 100 * multiplier conf = confs.PUCTPlayerConfig( name="clx", generation=self.game_config.generation, verbose=True, playouts_per_iteration=playouts_per_iteration, playouts_per_iteration_noop=0, dirichlet_noise_alpha=-1, root_expansions_preset_visits=-1, fpu_prior_discount=0.25, puct_before_expansions=3, puct_before_root_expansions=4, puct_constant_before=3.0, puct_constant_after=0.75, choose="choose_temperature", temperature=1.5, depth_temperature_max=self.game_config.depth_temperature_max, depth_temperature_start=4, depth_temperature_increment=0.25, depth_temperature_stop=self.game_config.depth_temperature_stop, random_scale=0.9, max_dump_depth=2) return conf def wtf(self, str_state): bs = self.gm.convert_to_base_state(str_state) nn = self.gm.get_player(0).nn print nn.predict_1(bs.to_list()) def get_move(self, str_state, depth, lead_role_index): print "GameMasterByGame.get_move", str_state self.gm.reset() self.gm.start( meta_time=120, move_time=120, initial_basestate=self.gm.convert_to_base_state(str_state), game_depth=depth) move = self.gm.play_single_move(last_move=None) player = self.gm.get_player(lead_role_index) return move[ lead_role_index], player.last_probability, self.gm.finished()