예제 #1
0
    def test_model_evaluation(self):
        tree = self.tree
        board = self.board
        size = conf['SIZE']

        test_board1, player = game_init()
        make_play(0, 0, test_board1)

        test_board2, player = game_init()
        make_play(1, 0, test_board2)

        class DummyModel(object):
            def predict_on_batch(_, X):
                size = conf['SIZE']
                board1 = X[0].reshape(1, size, size, 17)
                board2 = X[1].reshape(1, size, size, 17)
                self.assertTrue(np.array_equal(board1, test_board1))
                self.assertTrue(np.array_equal(board2, test_board2))
                batch_size = X.shape[0]
                policy = np.zeros((batch_size, size * size + 1),
                                  dtype=np.float32)
                policy[:, 0] = 1

                value = np.zeros((batch_size, 1), dtype=np.float32)
                value[:] = 1
                return policy, value

        model = DummyModel()

        simulate(tree, board, model, mcts_batch_size=2, original_player=1)
예제 #2
0
파일: tests.py 프로젝트: drsagitn/sejonggo
    def test_nested_selected(self):
        model = self.model
        board = self.board

        tree = {
            'count': 0,
            'mean_value': 0,
            'value': 0,
            'parent': None,
            'subtree': {
                0: {
                    'count': 0,
                    'p': 1,
                    'value': 0,
                    'mean_value': 0,
                    'subtree': {
                        1: {
                            'count': 0,
                            'p': 0,
                            'mean_value': 0,
                            'value': 0,
                            'subtree': {},
                        },
                        2: {
                            'count': 0,
                            'p': 1,
                            'mean_value': 0,
                            'value': 0,
                            'subtree': {},
                        }
                    }
                },
                1: {
                    'count': 0,
                    'p': 0,
                    'mean_value': 0,
                    'value': 0,
                    'subtree': {},
                }
            }
        }
        tree['subtree'][0]['parent'] = tree
        tree['subtree'][0]['subtree'][1]['parent'] = tree['subtree'][0]
        tree['subtree'][0]['subtree'][2]['parent'] = tree['subtree'][0]
        tree['subtree'][1]['parent'] = tree

        d = tree_depth(tree)
        assert d == 3

        simulate(tree, board, model, mcts_batch_size=2, original_player=1)
        self.assertEqual(tree['subtree'][0]['count'], 2)
        self.assertEqual(tree['subtree'][0]['subtree'][1]['count'], 1)
        self.assertEqual(tree['subtree'][0]['subtree'][2]['count'], 1)
        self.assertEqual(tree['subtree'][1]['count'], 0)
        self.assertEqual(tree['subtree'][0]['value'], 2)
        self.assertEqual(tree['subtree'][0]['mean_value'], 1)
        self.assertEqual(tree['subtree'][1]['value'], 0)
예제 #3
0
    def test_leaf(self):
        tree = self.tree
        board = self.board
        model = self.model

        simulate(tree, board, model, mcts_batch_size=2, original_player=1)
        self.assertEqual(tree['subtree'][0]['count'], 1)
        self.assertEqual(tree['subtree'][1]['count'], 1)
        self.assertEqual(tree['subtree'][0]['value'], -1)
        self.assertEqual(tree['subtree'][1]['value'], -1)
        self.assertEqual(tree['count'], 2)
        self.assertEqual(tree['value'], -2)
        self.assertEqual(tree['mean_value'], -1)
예제 #4
0
    def test_small_batch_size(self):
        tree = self.tree
        model = self.model
        board = self.board

        simulate(tree, board, model, mcts_batch_size=1, original_player=1)
        self.assertEqual(tree['subtree'][0]['count'], 1)
        self.assertEqual(tree['subtree'][0]['value'], -1)
        self.assertNotEqual(tree['subtree'][0]['subtree'], {})

        self.assertEqual(tree['subtree'][1]['count'], 0)
        self.assertEqual(tree['subtree'][1]['value'], 0)
        self.assertEqual(tree['subtree'][1]['subtree'], {})
예제 #5
0
    def test_model_evaluation_other_nested(self):
        tree = {
            'count': 0,
            'mean_value': 0,
            'value': 0,
            'parent': None,
            'subtree': {
                0: {
                    'count': 0,
                    'p': 1,
                    'value': 0,
                    'mean_value': 0,
                    'subtree': {},
                },
                1: {
                    'count': 0,
                    'p': 0,
                    'mean_value': 0,
                    'value': 0,
                    'subtree': {
                        0: {
                            'count': 0,
                            'p': 0,
                            'mean_value': 0,
                            'value': 0,
                            'subtree': {},
                        },
                        2: {
                            'count': 0,
                            'p': 1,
                            'mean_value': 0,
                            'value': 0,
                            'subtree': {},
                        }
                    }
                }
            }
        }
        tree['subtree'][0]['parent'] = tree
        tree['subtree'][1]['parent'] = tree
        tree['subtree'][1]['subtree'][0]['parent'] = tree['subtree'][1]
        tree['subtree'][1]['subtree'][2]['parent'] = tree['subtree'][1]

        board = self.board
        size = conf['SIZE']

        test_board1, player = game_init()
        make_play(0, 0, test_board1)

        test_board2, player = game_init()
        make_play(1, 0, test_board2)
        make_play(2, 0, test_board2)

        class DummyModel(object):
            def predict_on_batch(_, X):
                size = conf['SIZE']
                board1 = X[0].reshape(1, size, size, 17)
                board2 = X[1].reshape(1, size, size, 17)
                self.assertTrue(np.array_equal(board1, test_board1))
                self.assertTrue(np.array_equal(board2, test_board2))
                batch_size = X.shape[0]
                policy = np.zeros((batch_size, size * size + 1),
                                  dtype=np.float32)
                policy[:, 0] = 1

                value = np.zeros((batch_size, 1), dtype=np.float32)
                value[:] = 1
                return policy, value

        model = DummyModel()

        simulate(tree, board, model, mcts_batch_size=2, original_player=1)
예제 #6
0
    def test_nested_other_leaves(self):
        model = self.model
        board = self.board

        tree = {
            'count': 0,
            'mean_value': 0,
            'value': 0,
            'parent': None,
            'subtree': {
                0: {
                    'count': 0,
                    'p': .75,
                    'value': 0,
                    'mean_value': 0,
                    'subtree': {}
                },
                1: {
                    'count': 0,
                    'p': .25,
                    'mean_value': 0,
                    'value': 0,
                    'subtree': {
                        0: {
                            'count': 0,
                            'p': 1,
                            'mean_value': 0,
                            'value': 0,
                            'subtree': {},
                        },
                        2: {
                            'count': 0,
                            'p': 0,
                            'mean_value': 0,
                            'value': 0,
                            'subtree': {},
                        }
                    }
                },
                2: {
                    'count': 0,
                    'p': 0,
                    'value': 0,
                    'mean_value': 0,
                    'subtree': {}
                },
            }
        }
        tree['subtree'][0]['parent'] = tree
        tree['subtree'][1]['parent'] = tree
        tree['subtree'][1]['subtree'][0]['parent'] = tree['subtree'][1]
        tree['subtree'][1]['subtree'][2]['parent'] = tree['subtree'][1]

        simulate(tree, board, model, mcts_batch_size=2, original_player=1)
        self.assertEqual(tree['subtree'][0]['count'], 1)
        self.assertEqual(tree['subtree'][0]['value'], -1)
        self.assertEqual(tree['subtree'][1]['value'], 1)
        self.assertEqual(tree['subtree'][1]['count'], 1)
        self.assertEqual(tree['subtree'][1]['subtree'][0]['count'], 1)
        self.assertEqual(tree['subtree'][1]['subtree'][0]['value'], 1)
        self.assertEqual(tree['subtree'][1]['subtree'][2]['count'], 0)
        self.assertEqual(tree['count'], 2)
        self.assertEqual(tree['mean_value'], 0)
        self.assertEqual(tree['subtree'][2]['count'], 0)
        self.assertEqual(tree['subtree'][2]['subtree'], {})