Ejemplo n.º 1
0
    def __init__(self, killer=False):
        if killer:
            game_info = lookup.by_name("draughts_killer_10x10")
        else:
            game_info = lookup.by_name("draughts_10x10")

        super().__init__(game_info)

        self.board_desc = desc.BoardDesc(10)
Ejemplo n.º 2
0
def test_depth_charges():
    board_desc = desc.BoardDesc(10)
    for game in ("draughts_10x10", "draughts_killer_10x10"):

        info = lookup.by_name(game)

        # will dupe / and reset
        sm = info.get_sm()

        joint_move = sm.get_joint_move()
        base_state = sm.new_base_state()
        role_count = len(sm.get_roles())

        all_scores = [[] for i in range(role_count)]

        s = time.time()
        ITERATIONS = 100
        total_depth = 0
        for ii in range(ITERATIONS):
            sm.reset()

            board_desc.print_board_sm(sm)
            while not sm.is_terminal():
                for role_index, role in enumerate(sm.get_roles()):
                    ls = sm.get_legal_state(role_index)
                    choice = ls.get_legal(random.randrange(0, ls.get_count()))
                    joint_move.set(role_index, choice)

                # play move, the base_state will be new state
                sm.next_state(joint_move, base_state)

                # update the state machine to new state
                sm.update_bases(base_state)
                board_desc.print_board_sm(sm)
                total_depth += 1

            for ri in range(role_count):
                all_scores[ri].append(sm.get_goal_value(ri))

        total_time = time.time() - s

        print all_scores
        print "average depth", total_depth / float(ITERATIONS)
        print(total_time / float(ITERATIONS)) * 1000

        print "running %s for 2 seconds in C" % game
        from ggplib.interface import depth_charge

        msecs_taken, rollouts, num_state_changes = depth_charge(sm, 2)

        print "===================================================="
        print "ran for %.3f seconds, state changes %s, rollouts %s" % (
            (msecs_taken / 1000.0), num_state_changes, rollouts)
        print "rollouts per second: %s" % (rollouts / (msecs_taken / 1000.0))
Ejemplo n.º 3
0
    def __init__(self, doc, fen, expect_legals):
        self.board_desc = desc.BoardDesc(10)
        self.fen = fen
        self.expect_legals = expect_legals

        print
        print(len(doc) + 1) * "="
        print doc
        print(len(doc) + 1) * "="
        print

        self.sm = desc.create_sm(self.board_desc, self.fen)
Ejemplo n.º 4
0
def do_draw_rule(fen,
                 expect_score0,
                 expect_score1,
                 min_game_depth,
                 verbose=False):
    board_desc = desc.BoardDesc(10)

    # keep playing games randomly until we trigger expect_score0

    while True:
        last_step = 1
        sm = desc.create_sm(board_desc, fen)

        base_state = sm.new_base_state()
        base_state.assign(sm.get_current_state())

        depth = 0
        while True:
            if verbose:
                board_desc.print_board(base_state)
                dump_legals(sm)

            if sm.is_terminal():
                break

            # random move
            random_move(sm, verbose=verbose)
            depth += 1

            base_state.assign(sm.get_current_state())

            # the step should be +1 last_step, or reset
            step = board_desc.step_counter(base_state)

            # step is only reset at the end of captures
            if board_desc.check_interim_status(base_state):
                assert step == last_step
                continue

            assert step == 1 or step == last_step + 1
            last_step = step

        if verbose:
            print "depth:", depth
            print "Scores: [%d %d]" % (sm.get_goal_value(0),
                                       sm.get_goal_value(1))

        if depth >= min_game_depth and sm.get_goal_value(0) == expect_score0:
            assert sm.get_goal_value(1) == expect_score1
            return board_desc.step_counter(base_state)
Ejemplo n.º 5
0
def install_draughts(add_game):
    ' load custom c++ statemachine for draughts '
    from ggplib import interface
    from ggplib.non_gdl_games.draughts import desc, model

    desc10 = desc.BoardDesc(10)

    cpp_statemachines = interface.CppStateMachines()
    model = model.create_sm_model(desc10)

    for game_variant in ["draughts_10x10",
                         "draughts_killer_10x10",
                         "draughts_bt_10x10"]:
        sm_create_meth = getattr(cpp_statemachines, game_variant)
        add_game(game_variant, sm_create_meth(), model)
Ejemplo n.º 6
0
def test_create_id_10():
    board_desc = desc.BoardDesc(10)

    info = lookup.by_name("draughts_bt_10x10")

    # will dupe / and reset
    sm = info.get_sm()

    joint_move = sm.get_joint_move()
    base_state = sm.new_base_state()

    base_state.assign(sm.get_current_state())

    board_desc.print_board(base_state)
    pprint(info.model.basestate_to_str(base_state))

    while not sm.is_terminal():
        print "==============="
        print "Dumping legals:"
        print "==============="

        for role_index, role in enumerate(sm.get_roles()):
            ls = sm.get_legal_state(role_index)
            print role, [
                sm.legal_to_move(role_index, ls.get_legal(ii))
                for ii in range(ls.get_count())
            ]

        print
        print "** Choose a random move for each role:"
        for role_index, role in enumerate(sm.get_roles()):
            ls = sm.get_legal_state(role_index)
            choice = ls.get_legal(random.randrange(0, ls.get_count()))
            joint_move.set(role_index, choice)
            print "    %s :" % role, sm.legal_to_move(role_index, choice)
        print

        # play move, the base_state will be new state
        sm.next_state(joint_move, base_state)

        # update the state machine to new state
        sm.update_bases(base_state)

        board_desc.print_board(base_state)
Ejemplo n.º 7
0
def test_captures_king2():
    fen = "W:WK48:B31,42,21,22,19,10,39,29"

    board_desc = desc.BoardDesc(10)
    sm = desc.create_sm(board_desc, fen)

    base_state = sm.new_base_state()

    base_state.assign(sm.get_current_state())

    for i in range(7):
        board_desc.print_board(base_state)
        dump_legals(sm)

        if sm.is_terminal():
            print "AND DONE"
            break

        # random move
        random_move(sm)
        base_state.assign(sm.get_current_state())
Ejemplo n.º 8
0
def test_model():
    m = model.create_sm_model(desc.BoardDesc(10))
    pprint.pprint(m.roles)
    pprint.pprint(m.bases)
    pprint.pprint(m.actions)
Ejemplo n.º 9
0
    def __init__(self, fen, killer_mode):
        self.board_desc = desc.BoardDesc(10)

        self.sm = desc.create_sm(self.board_desc, fen, killer_mode)
        self.start_state = self.sm.get_current_state()
        self.next_basestate = self.sm.new_base_state()