Exemplo n.º 1
0
def test_long_simulation():
    start_state = TakeOneTwiceGame(1500)
    playout = Playout()

    value = playout.simulate(start_state)

    assert value == 1
Exemplo n.º 2
0
def test_playout():
    """ Just checking that playouts don't raise an exception. """
    game = OthelloGame()
    board = game.create_board()
    playout = Playout(game)

    playout.simulate(board)
Exemplo n.º 3
0
def test_simulate_finished_game_for_o_player():
    start_board = TicTacToeState("""\
XX.
OOO
.X.
""")
    expected_value = -1
    playout = Playout()

    value = playout.simulate(start_board)

    assert value == expected_value
Exemplo n.º 4
0
def test_two_moves_per_turn():
    start_board = TakeOneTwiceGame(2)
    playout = Playout()

    iteration_count = 10
    expected_value_total = 10
    value_total = 0
    for _ in range(iteration_count):
        value = playout.simulate(start_board)
        value_total += value

    assert value_total == expected_value_total
Exemplo n.º 5
0
def test_analyse_finished_game():
    board = TicTacToeState("""\
OXO
XXO
XOX
""")
    heuristic = Playout()
    expected_value = 0  # A tie
    expected_policy = [1 / 9] * 9

    value, policy = heuristic.analyse(board)

    assert expected_value == value
    assert expected_policy == policy.tolist()
Exemplo n.º 6
0
def test_analyse_finished_game():
    game = TicTacToeGame()
    board = game.create_board("""\
OXO
XXO
XOX
""")
    heuristic = Playout(game)
    expected_value = 0  # A tie
    expected_policy = [1/9] * 9

    value, policy = heuristic.analyse(board)

    assert expected_value == value
    assert expected_policy == policy.tolist()
Exemplo n.º 7
0
def test_simulate_wins():
    np.random.seed(0)
    start_board = TicTacToeState("""\
XOX
XO.
O..
""")
    iteration_count = 100
    expected_value_total = -iteration_count / 3
    expected_low = expected_value_total * 1.1
    expected_high = expected_value_total * 0.9
    playout = Playout()

    value_total = 0
    for _ in range(iteration_count):
        value = playout.simulate(start_board)
        value_total += value

    assert expected_low < value_total < expected_high
Exemplo n.º 8
0
def test_search_manager_reuses_node():
    start_state = TicTacToeState()
    manager = SearchManager(start_state, Playout())
    manager.search(start_state, iterations=10)
    move = manager.get_best_move()
    state2 = start_state.make_move(move)
    node = manager.current_node

    first_value_count = node.value_count
    manager.search(state2, iterations=10)
    second_value_count = node.value_count

    assert first_value_count > 0
    assert first_value_count + 10 == second_value_count
Exemplo n.º 9
0
def test_search_manager_with_opponent():
    """ Like when opponent is not sharing the SearchManager. """
    start_state = TicTacToeState()
    manager = SearchManager(start_state, Playout())
    manager.search(start_state, iterations=10)
    node = manager.current_node.children[0]  # Didn't call get_best_move().
    move = 0
    state2 = start_state.make_move(move)

    first_value_count = node.value_count
    manager.search(state2, iterations=10)
    second_value_count = node.value_count

    assert first_value_count > 0
    assert first_value_count + 10 == second_value_count
Exemplo n.º 10
0
 def load_heuristics():
     heuristics = [('Computer', Playout())]
     # entry: EntryPoint
     # for entry in iter_entry_points('zero_play.heuristic'):
     #     try:
     #         heuristic_class = entry.load()
     #     except ImportError as ex:
     #         library_path = os.environ.get('LD_LIBRARY_PATH')
     #         if library_path is not None:
     #             raise
     #         message = (f'Unable to load entry {entry.name}. Do you need to'
     #                    f' set LD_LIBRARY_PATH?')
     #         raise ImportError(message) from ex
     #     try:
     #         heuristic: Heuristic = heuristic_class(start_state)
     #     except ValueError:
     #         continue
     #     heuristics.append((entry.name, heuristic))
     return heuristics
Exemplo n.º 11
0
def test_playout():
    """ Just checking that playouts don't raise an exception. """
    board = OthelloState()
    playout = Playout()

    playout.simulate(board)