def test_game_end(self): game = ZertzGame(1) self.assertEqual(game.get_game_ended(), 0) game.get_next_state((2, 0, 1), 'PUT') self.assertEqual(game.get_game_ended(), 1) game = ZertzGame(7) self.assertEqual(game.get_game_ended(), 0) game.board.state[11] += 4 game.board.state[5] -= 4 self.assertEqual(game.get_game_ended(), -1)
def test_take_422(self): game = ZertzGame(19) game.get_next_state((0, 11, 5), 'PUT') #print('') #print(np.sum(game.board.state[:2], axis=0)) game.get_next_state((0, 8, 24), 'PUT') #print(np.sum(game.board.state[:2], axis=0)) game.get_next_state((0, 12, 22), 'PUT') #print(np.sum(game.board.state[:2], axis=0)) game.get_next_state((4, 2, 1), 'CAP')
def test_init(self): game = ZertzGame(19) self.assertEqual(game.initial_rings, 19) self.assertEqual(np.sum(game.board.state), 619) self.assertEqual(game.board.get_cur_player(), 0) game = ZertzGame(19, {'w': 2, 'g': 2, 'b': 2}) self.assertEqual(game.initial_rings, 19) self.assertEqual(game.board.state[5, 0, 0], 2) self.assertEqual(game.board.state[6, 0, 0], 2) self.assertEqual(game.board.state[7, 0, 0], 2) self.assertEqual(np.sum(game.board.state), 169)
def test_str_to_action(self): game = ZertzGame(19) action_strs = [ 'PUT w A3 C5', 'PUT g A2 B4', 'PUT b A1 A3', 'PUT g C3', 'PUT g E1 D1' ] actions = [('PUT', (0, 0, 2)), ('PUT', (1, 5, 1)), ('PUT', (2, 10, 0)), ('PUT', (1, 12, 25)), ('PUT', (1, 24, 23))] for action, action_str in zip(actions, action_strs): self.assertEqual(action, game.str_to_action(action_str)) action_strs = [ 'CAP b C4 g C2', 'CAP w C1 b C3', 'CAP g C1 b A1', 'CAP b D4 w B3', 'CAP w A3 w C3', 'CAP b C2 w E2' ] actions = [('CAP', (0, 1, 2)), ('CAP', (3, 4, 2)), ('CAP', (2, 4, 2)), ('CAP', (1, 1, 3)), ('CAP', (5, 0, 0)), ('CAP', (4, 3, 2))] for action, action_str in zip(actions, action_strs): self.assertEqual(action, game.str_to_action(action_str))
def test_str_to_action(self): game = ZertzGame(19) action_strs = [ 'PUT w A3 C5', 'PUT g A2 B4', 'PUT b A1 A3', 'PUT g C3', 'PUT g E1 D1' ] actions = [('PUT', (0, 0, 2)), ('PUT', (1, 5, 1)), ('PUT', (2, 10, 0)), ('PUT', (1, 12, 25)), ('PUT', (1, 24, 23))] for action, action_str in zip(actions, action_strs): self.assertEqual(action_str, game.action_to_str('PUT', action[1])) game.get_next_state((2, 7, 25), 'PUT') game.get_next_state((1, 12, 25), 'PUT') # Only first capture action has actual marbels to check the marble types against. # The other will default to 'w' marbles because they are blank rings. action_strs = [ 'CAP b C4 g C2', 'CAP w C1 w C3', 'CAP w C1 w A1', 'CAP w D4 b B3', 'CAP w A3 w C3', 'CAP w C2 w E2' ] actions = [('CAP', (0, 1, 2)), ('CAP', (3, 4, 2)), ('CAP', (2, 4, 2)), ('CAP', (1, 1, 3)), ('CAP', (5, 0, 0)), ('CAP', (4, 3, 2))] for action, action_str in zip(actions, action_strs): self.assertEqual(action_str, game.action_to_str('CAP', action[1]))
def test_take_actions(self): game = ZertzGame(19) #(('PUT', 'w', (4, 4)), ('REM', (4, 3))) game.get_next_state((0, 24, 23), 'PUT') #(('PUT', 'b', (3, 4)), ('REM', (4, 2))) game.get_next_state((2, 19, 22), 'PUT') #(('PUT', 'g', (2, 3)), ('REM', (1, 3))) game.get_next_state((1, 13, 8), 'PUT') #(('PUT', 'b', (1, 1)), ('REM', (3, 1))) game.get_next_state((2, 6, 16), 'PUT') #(('PUT', 'b', (2, 1)), ('REM', (0, 2))) state, player_value = game.get_next_state((2, 11, 2), 'PUT') self.assertTrue(np.all(state[5:8, 0, 0] == [5, 7, 7])) self.assertEqual(np.sum(state), 519) self.assertEqual(player_value, -1) #(('CAP', 'w', (4, 4)), ('b', (2, 4)), ('g', (2, 2)), ('b', (2, 0))) state, player_value = game.get_next_state((3, 4, 4), 'CAP') self.assertEqual(player_value, -1) state, player_value = game.get_next_state((1, 2, 4), 'CAP') self.assertEqual(player_value, -1) state, player_value = game.get_next_state((1, 2, 2), 'CAP') self.assertTrue(np.all(state[11:14, 0, 0] == [0, 1, 2])) self.assertEqual(np.sum(state), 566) self.assertTrue(np.all(state[:4, 2, 0] == [1, 1, 0, 0])) self.assertTrue(np.all(state[:4, 4, 4] == [1, 0, 0, 0])) self.assertTrue(np.all(state[:4, 2, 3] == [1, 0, 0, 0])) self.assertTrue(np.all(state[:4, 1, 1] == [1, 0, 0, 1])) self.assertEqual(np.sum(state[game.board._CAPTURE_LAYER]), 0) self.assertEqual(player_value, 1)
def test_get_actions(self): game = ZertzGame(19) placement, capture = game.get_valid_actions() self.assertEqual(np.sum(placement), 648) self.assertTrue(np.all(capture == False)) game = ZertzGame(37) placement, capture = game.get_valid_actions() self.assertEqual(np.sum(placement), 1944) self.assertTrue(np.all(capture == False)) #(('PUT', 'w', (4, 4)), ('REM', (4, 3))) state = game.get_next_state((0, 32, 31), 'PUT') #(('PUT', 'b', (3, 4)), ('REM', (4, 2))) game.get_next_state((2, 25, 30), 'PUT') #(('PUT', 'g', (2, 3)), ('REM', (1, 3))) game.get_next_state((1, 17, 10), 'PUT') #(('PUT', 'b', (1, 1)), ('REM', (3, 1))) game.get_next_state((2, 8, 22), 'PUT') #(('PUT', 'b', (2, 1)), ('REM', (0, 2))) game.get_next_state((2, 15, 2), 'PUT') placement, capture = game.get_valid_actions() self.assertEqual(np.sum(capture), 5) self.assertTrue(np.all(placement == False)) game = ZertzGame(1) placement, capture = game.get_valid_actions() self.assertEqual(np.sum(placement), 3) self.assertTrue(np.all(placement[:, :, 1])) self.assertTrue(np.all(capture == False))
def test_mcts2(self): # set up rings = 19 marbles = {'w': 10, 'g': 10, 'b': 10} win_con = [{'w': 2}, {'g': 2}, {'b': 2}, {'w': 1, 'g': 1, 'b': 1}] t = 3 game = Game(rings, marbles, win_con, t) nnet = DumbNN(game) # take some actions #Human: PUT g B1 B4 game.get_next_state((1, 16, 1), 'PUT') #AI: PUT b D3 C5 game.get_next_state((2, 13, 2), 'PUT') #Human: PUT b E1 C4 game.get_next_state((2, 24, 7), 'PUT') #AI: PUT w B2 D1 game.get_next_state((0, 11, 23), 'PUT') #Human: CAP g B1 w B3 game.get_next_state((3, 3, 1), 'CAP') #AI: PUT g A3 D4 #game.get_next_state((1, 0, 8), 'PUT') #Human: CAP g A3 g C3 #game.get_next_state((5, 0, 0), 'CAP') #Human: CAP g C3 b E3 # do MCTS board_state, player_value = game.get_current_state() print(board_state[0] + board_state[1] + board_state[2]*2 + board_state[3]*3) print(board_state[-1]) ai = MCTS(game, nnet, 1, 6) ai.reset(player_value) ai.get_action_prob(board_state, temp=0)
def test_mcts(self): # set up rings = 19 marbles = {'w': 10, 'g': 10, 'b': 10} win_con = [{'w': 2}, {'w': 1, 'g': 1, 'b': 1}] t = 3 game = Game(rings, marbles, win_con, t) nnet = DumbNN(game) # take some actions #(('PUT', 'w', (4, 4)), ('REM', (4, 3))) game.get_next_state((0, 24, 23), 'PUT') #(('PUT', 'b', (3, 4)), ('REM', (4, 2))) game.get_next_state((2, 19, 22), 'PUT') #(('PUT', 'g', (2, 3)), ('REM', (1, 3))) game.get_next_state((1, 13, 8), 'PUT') #(('PUT', 'b', (1, 1)), ('REM', (3, 1))) game.get_next_state((1, 6, 16), 'PUT') #(('PUT', 'b', (2, 1)), ('REM', (0, 2))) game.get_next_state((2, 11, 2), 'PUT') #(('PUT', 'w', (3, 3)), ('REM', (0, 0))) game.get_next_state((0, 18, 0), 'PUT') # do MCTS board_state, player_value = game.get_current_state() print(board_state[0] + board_state[1] + board_state[2]*2 + board_state[3]*3) ai = MCTS(game, nnet, 1, 50) ai.reset(player_value) ai.get_action_prob(board_state, temp=0)
def __init__(self): ZertzGame.__init__(self)