Ejemplo n.º 1
0
def do_game(game, gen, players, meta_time, move_time):
    # ensure we have a network

    gm = GameMaster(get_gdl_for_game(game), verbose=False)
    info = lookup.by_name(game)

    for player, role in zip(players, gm.sm.get_roles()):
        gm.add_player(player, role)

    roles = gm.sm.get_roles()
    match_info = create_match_info(meta_time, move_time, len(roles),
                                   game, [p.get_name() for p, r in gm.players], False)

    # save match_info
    update_match_info(game, match_info)

    gm.start(meta_time=meta_time, move_time=move_time)

    the_bs = gm.sm.new_base_state()
    gm.sm.get_current_state(the_bs)
    set_initial_state(match_info, basestate_to_str(info.model, the_bs))

    last_move = None
    while not gm.finished():
        last_move = gm.play_single_move(last_move)

        gm.sm.get_current_state(the_bs)
        add_to_next(match_info, list(last_move), basestate_to_str(info.model, the_bs))

        update_match_info(game, match_info)

    gm.play_to_end(last_move)
    set_goals(match_info, [gm.get_score(r) for _, r in gm.players])

    update_match_info(game, match_info)
Ejemplo n.º 2
0
def play_game(generation_name, player_clz, num_previous_states=1):
    game = "breakthrough"

    # ensure we have a network
    man = get_manager()
    generation_descr = templates.default_generation_desc(
        game,
        multiple_policy_heads=True,
        num_previous_states=num_previous_states)

    nn = man.create_new_network(game, "tiny", generation_descr)
    man.save_network(nn, generation_name)
    nn.summary()

    conf = get_default_conf(generation_name,
                            max_dump_depth=2,
                            playouts_per_iteration=42)

    attrutil.pprint(conf)

    gm = GameMaster(lookup.by_name(game))

    gm.add_player(player_clz(conf), "white")
    gm.add_player(get.get_player("random"), "black")

    gm.start(meta_time=30, move_time=15)

    last_move = None
    while not gm.finished():
        last_move = gm.play_single_move(last_move=last_move)

    gm.finalise_match(last_move)
    return gm
Ejemplo n.º 3
0
def play(player_white, player_black, move_time=0.5):
    gm = GameMaster(lookup.by_name(GAME), verbose=True)
    gm.add_player(player_white, "white")
    gm.add_player(player_black, "black")

    gm.start(meta_time=15, move_time=move_time)

    move = None
    while not gm.finished():

        # print out the board
        pretty_board(BOARD_SIZE, gm.sm)

        move = gm.play_single_move(last_move=move)

    gm.finalise_match(move)
Ejemplo n.º 4
0
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")
Ejemplo n.º 5
0
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()