def test_recall_subtree_found_tree(recall_tree): plant_state = mcts.State() plant_state.ally_starting = True plant_state.ally_team = [1] plant_state.enemy_team = [2] node = mcts.recall_subtree(plant_state, recall_tree(plant_state), {1}) assert 5 == node.value
def test_recall_subtree_not_found(recall_tree, some_state3): plant_state = mcts.State() plant_state.ally_starting = True plant_state.ally_team = [1] plant_state.enemy_team = [2] node = mcts.recall_subtree(some_state3, recall_tree(plant_state), {1}) assert 0 == node.value
def evaluate_MCTS_against_real_matches(data): ally_wins = 0 enemy_wins = 0 total_win_pct = 0 enemy_team = data[0][0] ally_starting = data[1] banned_champs = data[0][1] exploration_term = data[2] state = MCTS.State() state.ally_starting = ally_starting tree = None while len(state.enemy_team) < 5 or len(state.ally_team) < 5: if not ally_starting: pick_for_enemy_team(enemy_team, state, ally_starting) tree = MCTS.recall_subtree(state, tree, set(banned_champs)) allowed_champions = list.copy(tree.possible_actions) suggestions, tree = MCTS.run_mcts(10, tree, True, allowed_champions, exploration_term=exploration_term) pick_for_ally_team(suggestions, enemy_team, state) if ally_starting: pick_for_enemy_team(enemy_team, state, ally_starting) input_vector = list.copy(state.ally_team) input_vector.extend(list.copy(state.enemy_team)) result = NN.predictTeamComp(input_vector) total_win_pct += result if result > 0.5: ally_wins += 1 else: enemy_wins += 1 return ally_wins, enemy_wins, result
def test_get_suggestions2(tree): simple_state = mcts.State() simple_state.ally_team = [] simple_state.enemy_team = [1] simple_state.ally_starting = False test_tree = tree(simple_state, 5) suggestion = mcts.get_suggestions(test_tree, True, 1) assert suggestion[0].score == 5
def node(): simple_state = mcts.State() simple_state.ally_team = [] simple_state.enemy_team = [1] simple_state.ally_starting = False a_node = mcts.Node([2, 3], simple_state) a_node.tree_path = {1, 4} return a_node
def evaluate_MCTS_against_winpct(data): ally_wins = 0 enemy_wins = 0 total_win_pct = 0 ally_starting = data[0] exploration_term = data[1] state = MCTS.State() state.ally_starting = ally_starting tree = None banned_champs = set(random.sample(range(0, 141), 10)) allowed_champions = MCTS.get_allowed_champions(banned_champs) while len(state.enemy_team) < 5 or len(state.ally_team) < 5: if ally_starting is not True: pick_champ_enemy_team_winpct(allowed_champions, state) tree = MCTS.recall_subtree(state, tree, set(banned_champs)) allowed_champions = list.copy(tree.possible_actions) suggestions, tree = MCTS.run_mcts(10, tree, True, allowed_champions, exploration_term=exploration_term) if suggestions[0].champ2 is None: state.ally_team.append(suggestions[0].champ) allowed_champions.remove(suggestions[0].champ) else: state.ally_team.append(suggestions[0].champ) allowed_champions.remove(suggestions[0].champ) state.ally_team.append(suggestions[0].champ2) allowed_champions.remove(suggestions[0].champ2) if ally_starting is True: pick_champ_enemy_team_winpct(allowed_champions, state) input_vector = list.copy(state.ally_team) input_vector.extend(list.copy(state.enemy_team)) result = NN.predictTeamComp(input_vector) total_win_pct += result if result > 0.5: ally_wins += 1 elif result < 0.5: enemy_wins += 1 return ally_wins, enemy_wins, result
def _tree(target_state): simple_state = mcts.State() simple_state.ally_team = [] simple_state.enemy_team = [1] simple_state.ally_starting = False root = mcts.Node([], simple_state) child1 = mcts.Node([], simple_state, root) child11 = mcts.Node([], simple_state, child1) child12 = mcts.Node([], simple_state, child1) child2 = mcts.Node([], simple_state, root) child21 = mcts.Node([], simple_state, child2) child22 = mcts.Node([], target_state, child2) root.children.append(child1) root.children.append(child2) child1.children.append(child11) child1.children.append(child12) child2.children.append(child21) child2.children.append(child22) child1.value = 0 child1.visited = 2 child2.value = 0 child2.visited = 2 child2.champ = 1 child11.value = 1 child11.visited = 1 child12.value = 2 child12.visited = 1 child21.value = 3 child21.visited = 1 child22.value = 5 child22.visited = 1 child22.champ = 2 root.depth = 0 return root
def _tree(simple_state, higher_than_three): root = mcts.Node([], simple_state) child1 = mcts.Node([], simple_state, root) child11 = mcts.Node([], simple_state, child1) child12 = mcts.Node([], simple_state, child1) child2 = mcts.Node([], simple_state, root) child21 = mcts.Node([], simple_state, child2) child22 = mcts.Node([], simple_state, child2) root.children.append(child1) root.children.append(child2) child1.children.append(child11) child1.children.append(child12) child2.children.append(child21) child2.children.append(child22) child1.value = 0 child1.visited = 2 child2.value = 0 child2.visited = 2 child11.value = 1 child11.visited = 1 child12.value = 2 child12.visited = 1 child21.value = 3 child21.visited = 1 child22.value = higher_than_three child22.visited = 1 return root
def test_suggestions_to_json2(list_of_fixtures): json_obj = mcts.suggestions_to_json(list_of_fixtures) json_data = json.loads(json_obj) assert json_data[1] == [4, 6, 5]
def test_is_dual_return3(some_state3): assert mcts.is_dual_return(some_state3)
def test_simulate2(some_state): possible = [0, 2, 3, 4, 5, 6, 7, 8, 9] not_possible = range(10, 142) node = mcts.Node(possible, some_state) output = mcts.simulate(node) assert not any(elem in output for elem in not_possible)
def test_run_mcts_suggest(some_state): node = mcts.Node(range(10, 80), some_state) suggestions, reduced_root = mcts.run_mcts(1, node, True, range(10, 80)) assert 10 == len(suggestions)
def some_state(): test_state = mcts.State() test_state.ally_team = [] test_state.enemy_team = [1] test_state.ally_starting = False return test_state
def test_find_state_at_turn2(node_with_champ, node): node.children.append(node_with_champ(1)) node.children.append(node_with_champ(2)) node.children.append(node_with_champ(3)) node.children.append(node_with_champ(4)) assert mcts.find_state_at_turn(node, 2).champ == 2
def test_game_state_from_json_bans(json_state): state, bans = mcts.game_state_from_json(json_state) assert bans == {6, 7, 8, 9, 0}
def test_backprop_root_visited(tree: mcts.Node): state = mcts.State() root = tree(state, 5) mcts.backprop(5, root.children[0].children[0]) assert root.visited == 1
def _node_with_champ(champ): node = mcts.Node([], mcts.State()) node.champ = champ return node
def some_state3(): test_state = mcts.State() test_state.ally_team = [4, 5, 6] test_state.enemy_team = [1, 2, 3, 7] test_state.ally_starting = True return test_state
def test_backprop_node1_value(tree: mcts.Node): state = mcts.State() root = tree(state, 5) mcts.backprop(5, root.children[0].children[0]) assert root.children[0].value == 5
def test_simulate3(some_state): possible = [0, 2, 3, 4, 5, 6, 7, 8, 9] node = mcts.Node(possible, some_state) output = mcts.simulate(node)[5:] assert output.__contains__(1)
def test_expand_children_champ(node): fresh_node = mcts.expand(node, 130, {1, 2}) assert fresh_node.champ == 130
def test_find_new_state(node): new_state = mcts.find_new_state(130, node) assert new_state.ally_team == [130]
def test_expand_parent(node): fresh_node = mcts.expand(node, 130, {1, 2}) assert fresh_node.parent == node
def test_game_state_from_json_enemy_team(json_state): state, bans = mcts.game_state_from_json(json_state) assert state.enemy_team == [4, 5]
def test_run_mcts_root(some_state2): node = mcts.Node(range(20, 90), some_state2) suggestions, reduced_root = mcts.run_mcts(1, node, True, range(20, 90)) assert isinstance(reduced_root, mcts.Node)
def test_game_state_from_json_ally_starting(json_state): state, bans = mcts.game_state_from_json(json_state) assert state.ally_starting
def test_is_dual_return2(some_state2): assert not mcts.is_dual_return(some_state2)
def list_of_fixtures(): list = [] list.append(mcts.Suggestion(1, 2, 3)) list.append(mcts.Suggestion(4, 5, 6)) return list
def test_game_state_from_json_ally_team(json_state): state, bans = mcts.game_state_from_json(json_state) assert state.ally_team == [1, 2, 3]
def test_simulate(some_state): possible = [0, 2, 3, 4, 5, 6, 7, 8, 9] node = mcts.Node(possible, some_state) output = mcts.simulate(node) assert all(elem in output for elem in possible)