def test_get_action(self): player = NNPolicy() input = np.zeros((1, fe.get_feature_dim(player.features), 2 * gm.START_HANDS)) game = gm.GameState() while(not game.is_end_of_game()): (card, move) = player.get_action(game) self.assertTrue(card.position == (-1, -1) and card.owner == game.current_player) self.assertTrue(game.board[Helper.tuple2idx(game.board_size, *move)] is None) game.play_round(card, *move)
def test_run_single_game(self): game = gm.GameState() (states, cards, moves) = su.simulate_single_game(self.target, game) self.assertTrue(np.array(states).shape == (gm.BOARD_SIZE **2, fe.get_feature_dim(), gm.START_HANDS * 2)) self.assertTrue(np.array(cards).shape == (gm.BOARD_SIZE **2, gm.START_HANDS * 2)) self.assertTrue(np.array(moves).shape == (gm.BOARD_SIZE **2, gm.BOARD_SIZE **2)) sum_cards = np.sum(np.array(cards), 0) sum_moves = np.sum(np.array(moves), 0) self.assertTrue( np.all( sum_cards <= 1) ) self.assertTrue( np.all( sum_moves == 1) )
def test_game_start(self): game = gm.GameState() self.assertTrue(fe.get_feature_dim() == 16) features = fe.state2feature(game) self.assertTrue(features.shape[0] == 1) self.assertTrue(features.shape[1] == 16) self.assertTrue(features.shape[2] == 2 * gm.START_HANDS) # At the beginning all the cards are in the hands self.assertTrue((np.sum(features[0, 4:14, :], axis=1) == [0, 0, 0, 0, 0, 0, 0, 0, 0, 10]).all()) # At the beginning all the cards are equally owner by both players self.assertTrue(np.sum(features[0, 14, :]) == gm.START_HANDS) # The current player should either be 1 for all the cards, or 0 for all the cards start_player = np.sum(features[0, 15, :]) self.assertTrue(start_player == features.shape[2] or start_player == 0)
def create_neural_network(self): """ Draft Network Architecture for testing purpose Input: 16x10 size Try with 3 hidden layers for now. Each layer has 120 units with a dropout rate at 0.25 and relu as the activation layer Use Flatten to map to a one dimension output with size 19 (first 10 maps to the cards to pick, and the last 9 map to the board to drop the card) There should be two output layers: one for the card and one for the move """ state_input = Input(shape = (fe.get_feature_dim(self.features), 2 * gm.START_HANDS)) # Testing for Conv1d layer x = SeparableConv1D(self.params["filters"], 5, padding='same', data_format='channels_first', name="Conv_kernal_5")(state_input) x = BatchNormalization()(x) x = Activation(self.params["activation"])(x) x = MaxPooling1D(padding='same')(x) for i in range(self.params["conv_layers"]): x = SeparableConv1D(self.params["filters"], 3, padding='same', data_format='channels_first', name="Conv1D_{}".format(i+1))(x) x = BatchNormalization()(x) x = Activation(self.params["activation"])(x) x = MaxPooling1D(padding='same')(x) x = Flatten()(x) for i in range(self.params["dense_layers"]): x = Dense(self.params["units"], activation=self.params["activation"], name="Dense_{}".format(i+1))(x) if self.params["dropout"] > 0: x = Dropout(self.params["dropout"])(x) x1 = Bias()(x) x2 = Bias()(x) card_output = Dense(2 * gm.START_HANDS, activation=self.params["output_activation"], name='card_output')(x1) move_output = Dense(gm.BOARD_SIZE ** 2, activation=self.params["output_activation"], name='move_output')(x2) network = Model(input=state_input, output=[card_output, move_output]) return network
def create_neural_network(self): """ Draft Network Architecture for testing purpose Input: 16x10 size Try with 3 hidden layers for now. Each layer has 120 units with a dropout rate at 0.25 and relu as the activation layer Use Flatten to map to a one dimension output with size 19 (first 10 maps to the cards to pick, and the last 9 map to the board to drop the card) There should be two output layers: one for the card and one for the move """ state_input = Input(shape=(fe.get_feature_dim(self.features), 2 * gm.START_HANDS)) # Testing for Conv1d layer x = SeparableConv1D(CONV_LAYER_PARAMETERS["filters"], CONV_LAYER_PARAMETERS["strides"], padding='same', data_format='channels_first', name="Conv_kernal_1")(state_input) x = BatchNormalization()(x) x = Activation("relu")(x) # x = MaxPooling1D(padding='same')(x) for i in range(RESIDUAL_LAYER_PARAMETERS["layer_number"]): x = self.residual_layer(x) # x = Flatten()(x) policy_output = self.policy_head(x) value_output = self.value_head(x) network = Model(input=state_input, output=[policy_output, value_output]) self.model = network
def test_generator(self): data_generator = su.state_action_generator(self.target, self.multi_meta) for _ in range(1): (states, action) = next(data_generator) self.assertTrue(states.shape == (self.multi_meta["batch_size"] * (gm.BOARD_SIZE **2), fe.get_feature_dim(), gm.START_HANDS * 2)) self.assertTrue(action["card_output"].shape == (self.multi_meta["batch_size"] * (gm.BOARD_SIZE **2), gm.START_HANDS * 2 )) self.assertTrue(action["move_output"].shape == (self.multi_meta["batch_size"] * (gm.BOARD_SIZE **2), gm.BOARD_SIZE ** 2))