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)
Exemple #2
0
def test_not_in_db():
    some_simple_game = """
  (role white)
  (role black)

  (init o1)

  (legal white a)
  (legal white b)
  (legal black a)

  (<= (next o2) (does white a) (true o1))
  (<= (next o3) (does white b) (true o1))

  (<= (goal white 0) (true o1))
  (<= (goal white 10) (true o2))
  (<= (goal white 90) (true o3))

  (<= (goal black 0) (true o1))
  (<= (goal black 90) (true o2))
  (<= (goal black 10) (true o3))

  (<= terminal (true o2))
  (<= terminal (true o3))
    """

    gm = GameMaster(some_simple_game)

    # add two python players
    gm.add_player(get.get_player("pyrandom"), "black")
    gm.add_player(get.get_player("ggtest1"), "white")

    gm.start(meta_time=10, move_time=5)
    gm.play_to_end()
Exemple #3
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
Exemple #4
0
def test_tictactoe_take_win():
    gm = GameMaster(get_gdl_for_game("ticTacToe"))

    # add two c++ players
    gm.add_player(get.get_player("ggtest1"), "xplayer")
    gm.add_player(get.get_player("ggtest1"), "oplayer")

    str_state = '''
    (true (control xplayer))
    (true (cell 2 2 x))
    (true (cell 3 2 o))
    (true (cell 3 3 x))
    (true (cell 2 3 o))
    (true (cell 3 1 b))
    (true (cell 2 1 b))
    (true (cell 1 3 b))
    (true (cell 1 2 b))
    (true (cell 1 1 b)) '''

    gm.start(meta_time=30, move_time=2,
             initial_basestate=gm.convert_to_base_state(str_state))

    # play a single move - should take win
    move = gm.play_single_move()
    assert str(move[0]) == "(mark 1 1)"
    assert str(move[1]) == "noop"

    gm.play_to_end(last_move=move)

    # check scores/depth make some sense
    assert gm.scores['xplayer'] == 100
    assert gm.scores['oplayer'] == 0
    assert gm.get_game_depth() == 1
def test_tictactoe_play_verbose():
    gm = GameMaster(lookup.by_name("ticTacToe"), verbose=True)

    # add two python players
    gm.add_player(get.get_player("pyrandom"), "xplayer")
    gm.add_player(get.get_player("pylegal"), "oplayer")

    gm.start(meta_time=10, move_time=5)
    gm.play_to_end()

    # check scores/depth make some sense
    assert sum(gm.scores.values()) == 100
    assert 5 <= gm.get_game_depth() <= 9
Exemple #6
0
def test_tictactoe_cpp_play():
    gm = GameMaster(get_gdl_for_game("ticTacToe"))

    # add two c++ players
    gm.add_player(get.get_player("random"), "xplayer")
    gm.add_player(get.get_player("legal"), "oplayer")

    gm.start(meta_time=10, move_time=5)
    gm.play_to_end()

    # check scores/depth make some sense
    assert sum(gm.scores.values()) == 100
    assert 5 <= gm.get_game_depth() <= 9
Exemple #7
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)
Exemple #8
0
def test_tictactoe_play_test_db_lookup():
    game_gdl_str = get_gdl_for_game("ticTacToe", dict(mark="kram",
                                                      noop="notamove",
                                                      cell="bell",
                                                      oplayer="doobie"))

    gm = GameMaster(game_gdl_str)

    # add two python players
    gm.add_player(get.get_player("pyrandom"), "xplayer")
    gm.add_player(get.get_player("pylegal"), "oplayer")

    gm.start(meta_time=10, move_time=5)
    gm.play_to_end()

    # check scores/depth make some sense
    assert sum(gm.scores.values()) == 100
    assert 5 <= gm.get_game_depth() <= 9
Exemple #9
0
def test_play():
    if skip_slow:
        py.test.skip("too slow")

    from ggplib.player.gamemaster import GameMaster

    game_info = lookup.by_name("draughts_killer_10x10")
    gm = GameMaster(game_info, verbose=False)

    # add two python players
    gm.add_player(create_player(), "white")
    gm.add_player(create_player(), "black")

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

    # check scores/depth make some sense
    print gm.scores
Exemple #10
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")
Exemple #11
0
def test_speed():
    gm = GameMaster(get_gdl_for_game("reversi"))

    # add two python players
    a = get.get_player("simplemcts")
    a.skip_single_moves = True
    a.max_tree_playout_iterations = 200

    b = get.get_player("simplemcts")
    b.skip_single_moves = True
    b.max_tree_playout_iterations = 200

    gm.add_player(a, "red")
    gm.add_player(b, "black")

    import time
    gm.start(meta_time=10, move_time=5)
    s = time.time()
    gm.play_to_end()
    print "DONE", time.time() - s
def test_breakthrough():
    gm = GameMaster(get_gdl_for_game("breakthrough"))

    # add two players
    pymcs = get.get_player("pymcs")
    pymcs.max_run_time = 0.25

    gurgeh = GurgehPlayer("Gurgeh")
    gurgeh.max_tree_search_time = 1
    gurgeh.max_tree_search_time = 1
    gurgeh.thread_workers = 2

    gm.add_player(pymcs, "white")
    gm.add_player(gurgeh, "black")

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

    print "white_score", pymcs.name, gm.get_score("white")
    print "black_score", gurgeh.name, gm.get_score("black")
Exemple #13
0
def play_game(generation_name, num_previous_states):
    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 = templates.puct_config_template(generation_name, "compete")

    gm = GameMaster(get_gdl_for_game(game))

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

    gm.start(meta_time=30, move_time=15)
    gm.play_to_end()
Exemple #14
0
def test_fast_plays():
    ''' very fast rollouts, basically this config of puct player is a policy player '''
    gm = GameMaster(get_gdl_for_game("breakthrough"))

    import attr
    conf = confs.PUCTPlayerConfig(**attr.asdict(default_puct_config))
    conf.verbose = False

    # just checking that we haven't modified default
    assert not conf.verbose and default_puct_config.verbose

    conf.playouts_per_iteration = 1
    conf.playouts_per_iteration_noop = 0
    conf.dirichlet_noise_alpha = -1
    print conf

    # add two players
    white = PUCTPlayer(conf=conf)
    black = PUCTPlayer(conf=conf)

    gm.add_player(white, "white")
    gm.add_player(black, "black")

    acc_black_score = 0
    acc_red_score = 0
    s = time.time()
    for _ in range(ITERATIONS):
        gm.start(meta_time=30, move_time=15)
        gm.play_to_end()

        acc_black_score += gm.scores["black"]
        acc_red_score += gm.scores["white"]

        print gm.get_game_depth()

    print "time taken", time.time() - s
    print "white_score", gm.players_map["white"].name, acc_red_score
    print "black_score", gm.players_map["black"].name, acc_black_score
Exemple #15
0
def test_puct_tournament():
    gm = GameMaster(get_gdl_for_game("breakthrough"))

    # add two players
    pymcs = get.get_player("pymcs")
    pymcs.max_run_time = 0.1

    black = PUCTPlayer(conf=default_puct_config)

    gm.add_player(pymcs, "white")
    gm.add_player(black, "black")

    acc_black_score = 0
    acc_red_score = 0
    for _ in range(ITERATIONS):
        gm.start(meta_time=30, move_time=15)
        gm.play_to_end()

        acc_black_score += gm.scores["black"]
        acc_red_score += gm.scores["white"]

    print "white_score", gm.players_map["white"].name, acc_red_score
    print "black_score", gm.players_map["black"].name, acc_black_score
Exemple #16
0
def test_not_taking_win():
    gm = GameMaster(get_gdl_for_game("breakthrough"))

    # add two players
    gm.add_player(PUCTPlayer(default_puct_config), "white")
    gm.add_player(PUCTPlayer(default_puct_config), "black")

    str_state = """ (true (control black))
    (true (cellHolds 8 8 black)) (true (cellHolds 8 7 black)) (true (cellHolds 8 2 white))
    (true (cellHolds 8 1 white)) (true (cellHolds 7 8 black)) (true (cellHolds 6 7 white))
    (true (cellHolds 6 2 white)) (true (cellHolds 6 1 white)) (true (cellHolds 5 4 black))
    (true (cellHolds 5 3 black)) (true (cellHolds 5 2 white)) (true (cellHolds 5 1 white))
    (true (cellHolds 4 8 black)) (true (cellHolds 4 7 black)) (true (cellHolds 4 1 white))
    (true (cellHolds 3 8 black)) (true (cellHolds 3 6 black)) (true (cellHolds 3 2 white))
    (true (cellHolds 2 8 black)) (true (cellHolds 2 7 black)) (true (cellHolds 2 2 white))
    (true (cellHolds 2 1 white)) (true (cellHolds 1 8 black)) (true (cellHolds 1 7 black))
    (true (cellHolds 1 2 white)) (true (cellHolds 1 1 white)) """

    gm.start(meta_time=30, move_time=5,
             initial_basestate=gm.convert_to_base_state(str_state))

    last_move = gm.play_single_move(last_move=None)
    assert last_move[1] == "(move 7 8 6 7)"
Exemple #17
0
def test_breakthrough():
    ' mcs player vs ggtest1 '
    gm = GameMaster(get_gdl_for_game("breakthrough"))

    # add two players
    white = get.get_player("pymcs")
    white.max_run_time = 0.25

    black = get.get_player("simplemcts")
    black.skip_single_moves = True

    gm.add_player(white, "white")
    gm.add_player(black, "black")

    gm.start(meta_time=30, move_time=2.0)
    gm.play_to_end()

    # hopefully simplemcts wins!  Not a great test.
    assert gm.scores["white"] == 0
    assert gm.scores["black"] == 100

    # check scores/depth make some sense
    assert sum(gm.scores.values()) == 100
    assert gm.get_game_depth() >= 10
Exemple #18
0
    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
Exemple #19
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()
Exemple #20
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