Esempio n. 1
0
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
Esempio n. 2
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()