def simulate(self, dt): try: engine.simulate(self, self.data, dt) self.simulate_error = None except Exception as error: if self.simulate_error != error.message: print("Unable to render, reason:") print(error) self.simulate_error = error.message
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'], {})
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)
def test_nested_other_leaves(self): model = self.model board = self.board tree = { 'count': 0, 'mean_value': 0, 'value': 0, 'parent': None, 'move': 1, 'subtree': { 0:{ 'count': 0, 'p': .75, 'value': 0, 'mean_value': 0, 'move': 2, 'subtree': {} }, 1: { 'count': 0, 'p': .25, 'mean_value': 0, 'value': 0, 'move': 2, 'subtree': { 0: { 'count': 0, 'p': 1, 'mean_value': 0, 'value': 0, 'move': 3, 'subtree': {}, }, 2: { 'count': 0, 'p': 0, 'mean_value': 0, 'value': 0, 'move': 3, 'subtree': {}, } } }, 2:{ 'count': 0, 'p': 0, 'value': 0, 'mean_value': 0, 'move': 2, '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'], {})
def test_model_evaluation_other_nested(self): tree = { 'count': 0, 'mean_value': 0, 'value': 0, 'parent': None, 'move': 1, 'subtree':{ 0:{ 'count': 0, 'p': 1, 'value': 0, 'mean_value': 0, 'move': 2, 'subtree': {}, }, 1: { 'count': 0, 'p': 0, 'mean_value': 0, 'value': 0, 'move': 2, 'subtree': { 0: { 'count': 0, 'p': 0, 'mean_value': 0, 'value': 0, 'move': 3, 'subtree': {}, }, 2: { 'count': 0, 'p': 1, 'mean_value': 0, 'value': 0, 'move': 3, '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 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)
def model(): inputs = engine.sample( 'input', dist.Normal(loc=np.array([0.]), scale=np.array([1.]))) outputs = engine.simulate('gn', inputs, obs=obs) return inputs, outputs
def model(): inputs = engine.sample( 'input', dist.Normal(loc=torch.tensor([0.]), scale=torch.tensor([1.]))) outputs = engine.simulate('gn', inputs, obs=obs) return inputs, outputs