Example #1
0
    def test_predict(self):
        game_root = Game()
        root = Node(game_root)
        model = MagicMock()

        prediction = [
            np.array([[0.25]]),
            np.reshape(np.arange(0.001, 0.897, step=0.001), newshape=(1, 896))
        ]
        model.predict.return_value = prediction

        action_encoder = ActionEncoder(DirectionResolver())
        mcts = MCTS(root,
                    config={
                        'ALPHA': 0.8,
                        'CPUCT': 1,
                        'EPSILON': 0.2
                    },
                    model=model,
                    state_encoder=StateEncoder(),
                    action_encoder=action_encoder)

        value, probs, allowed_actions = mcts.predict_state_value(game_root)

        self.assertEqual(value, 0.25)
        self.assertCountEqual(
            allowed_actions,
            action_encoder.convert_moves_to_action_ids(
                game_root.get_possible_moves_from_current_player_perspective())
        )
        for idx, prob in enumerate(probs):
            if idx in allowed_actions:
                self.assertTrue(prob > 0.01)
            else:
                self.assertTrue(prob < np.exp(-40))
Example #2
0
 def test_convert_moves_to_action_ids(self):
     encoder = ActionEncoder(DirectionResolver())
     values = encoder.convert_moves_to_action_ids([[10, 17], [10, 15]])
     self.assertCountEqual(values, [9, 105])