def play_pimc(state, game, num_samples, num_simulations=None, total_simulation_seconds=1): played_tiles = {frozenset(s.action.tile) for s in game[1:]} my_tiles = { frozenset(tile) for tile in state._tiles_by_player[state._current_player] } num_tiles_by_player = [ len(tiles) for tiles in rotate(state._tiles_by_player, state._current_player) ] tile, suit_played = pimc_decision( state._suits_at_ends, my_tiles, played_tiles, num_tiles_by_player, num_samples, mcts_simulations=num_simulations, total_simulation_seconds=total_simulation_seconds) return state.next_state_from_action( DominoAction(state._current_player, tile, suit_played))
def test_pass_action_is_legal(self): state = DominoState( 1,{ 'tiles_by_player':[ [{0}, {1, 2}, {1, 5}, {5, 6}, {4, 6}, {2,3}], [{5}, {2, 5}, {0, 5}, {1, 3}, {2, 4}, {2}, {3, 5}], [{4}, {3}, {4, 5}, {0, 1}, {0, 6}, {1, 6}, {0, 2}], [{3, 4}, {0, 3}, {2, 6}, {1, 4}, {0, 4}, {3, 6}, {1}] ], 'suits_at_ends':{6} }) action = DominoAction(1, {-1}, None) self.assertTrue(state._is_action_legal(action))
def test_action_is_ilegal_not_current_player(self): state = DominoState( 1,{ 'tiles_by_player':[ [{0}, {1, 2}, {1, 5}, {5, 6}, {4, 6}, {6}], [{5}, {2, 5}, {0, 5}, {1, 3}, {2, 4}, {2}, {3, 5}], [{4}, {3}, {4, 5}, {0, 1}, {0, 6}, {1, 6}, {0, 2}], [{3, 4}, {0, 3}, {2, 6}, {1, 4}, {0, 4}, {3, 6}, {1}] ], 'suits_at_ends':{2,3} }) action = DominoAction(0, {1,2},2) self.assertFalse(state._is_action_legal(action))
def play_mcts(state, num_simulations=None, total_simulation_seconds=1): current_player = state._current_player tiles_by_player = state._tiles_by_player aux_state = DominoState( 0, { 'tiles_by_player': rotate(tiles_by_player, current_player), 'suits_at_ends': state._suits_at_ends }) root = TwoPlayersGameMonteCarloTreeSearchNode( state=DominoGameState(aux_state)) mcts = MonteCarloTreeSearch(root) best_action = mcts.best_action( simulations_number=num_simulations, total_simulation_seconds=total_simulation_seconds).state._state.action return state.next_state_from_action( DominoAction(current_player, best_action.tile, best_action.suit_played))
def test_next_state_from_action_creates_deep_copy(self): tiles_by_player = [ [{0}, {1, 2}, {1, 5}, {6}, {5, 6}, {4, 6}], [{5}, {2, 5}, {0, 5}, {1, 3}, {2, 4}, {2}, {3, 5}], [{4}, {3}, {4, 5}, {0, 1}, {0, 6}, {1, 6}, {0, 2}], [{3, 4}, {0, 3}, {2, 6}, {1, 4}, {0, 4}, {3, 6}, {1}] ] state = DominoState( 1,{ 'tiles_by_player':copy.deepcopy(tiles_by_player), 'suits_at_ends':{2,3} }) next_state = state.next_state_from_action(DominoAction(1, {2,5},2)) next_state._tiles_by_player[0].remove({0}) self.assertEqual(tiles_by_player, state._tiles_by_player) self.assertIn({0}, state._tiles_by_player[0]) self.assertNotIn({0}, next_state._tiles_by_player[0]) self.assertIn({2,5}, state._tiles_by_player[1]) self.assertNotIn({2,5}, next_state._tiles_by_player[1])
def test_next_state_from_action(self): state = DominoState( 1,{ 'tiles_by_player':[ [{0}, {1, 2}, {1, 5}, {6}, {5, 6}, {4, 6}], [{5}, {2, 5}, {0, 5}, {1, 3}, {2, 4}, {2}, {3, 5}], [{4}, {3}, {4, 5}, {0, 1}, {0, 6}, {1, 6}, {0, 2}], [{3, 4}, {0, 3}, {2, 6}, {1, 4}, {0, 4}, {3, 6}, {1}] ], 'suits_at_ends':{2,3} }) next_state = state.next_state_from_action(DominoAction(1, {2,5}, 2)) self.assertEqual(next_state._current_player, 2) self.assertEqual(next_state._suits_at_ends, {5,3}) self.assertEqual(next_state._tiles_by_player, [ [{0}, {1, 2}, {1, 5}, {6}, {5, 6}, {4, 6}], [{5},{0, 5}, {1, 3}, {2, 4}, {2}, {3, 5}], [{4}, {3}, {4, 5}, {0, 1}, {0, 6}, {1, 6}, {0, 2}], [{3, 4}, {0, 3}, {2, 6}, {1, 4}, {0, 4}, {3, 6}, {1}] ])