def test_mcb_search(self): board_size = 3 start_color_code = HexBoard1.BLUE_CODE winner_color_code = HexBoard1.BLUE_CODE hex_game: HexBoard1 = HexBoard1(board_size, start_color_code) parent_hex_node = None hex_game_status = hex_game placed_coord_tuple = None root_tree_node = MCTSNode(parent_hex_node, hex_game_status, placed_coord_tuple) c_exploration_para = 2 to_expand_tree_node: MCTSNode = MCTSRule.search( root_tree_node, c_exploration_para) print("to_explore_tree_node: ", to_expand_tree_node.__str__()) expanded_child_tree_node: MCTSNode = MCTSRule.expand( to_expand_tree_node) print("expanded_child_tree_node: ", expanded_child_tree_node.__str__()) winner_color_name: str = hex_game.obtain_color_name(winner_color_code) value: int = MCTSRule.simluate(expanded_child_tree_node, winner_color_name) print("value: ", value) # rule = MCTSRule() MCTSRule.back_propagate(expanded_child_tree_node, value) print("root_tree_node: ", root_tree_node) print("expanded_child_tree_node: ", expanded_child_tree_node)
def test_mcb_search1(self): board_size = 3 start_color_code = HexBoard1.BLUE_CODE winner_color_code = HexBoard1.BLUE_CODE hex_game: HexBoard1 = HexBoard1(board_size, start_color_code) parent_hex_node = None hex_game_status = hex_game placed_coord_tuple = None root_tree_node = MCTSNode(parent_hex_node, hex_game_status, placed_coord_tuple) c_exploration_para = 2 to_add_tuple_list = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] for to_add_tuple in to_add_tuple_list: hex_child: HexBoard1 = HexBoard1(board_size, start_color_code) hex_child.place_with_color(to_add_tuple, hex_game.turn_of_color_code) hex_tree_node: MCTSNode = MCTSNode(hex_game, hex_child, to_add_tuple) hex_tree_node.num_of_win = 1 hex_tree_node.num_of_sample = 1 root_tree_node.child_hex_node_list.append(hex_tree_node) root_tree_node.num_of_sample = 9 to_expand_node: MCTSNode = MCTSRule.search(root_tree_node, c_exploration_para) print("to_expand_node ", to_expand_node)
def back_propagate_reference(): hexTreeNode1: MCTSNode = MCTSNode(None, None, None) hexTreeNode2: MCTSNode = MCTSNode(hexTreeNode1, None, None) # hexTreeNode1.num_of_win = 1 # hexTreeNode2.num_of_win = 2 # list1 = [hexTreeNode1] # list1.append(hexTreeNode2) # for i in list1: # print(i) MCTSRule.back_propagate(hexTreeNode2, 1) hexTreeNode1.child_hex_node_list.append(hexTreeNode2) hexTreeNode2.num_of_sample += 1 print("hexTreeNode2: ", hexTreeNode2) print("hexTreeNode1: ", hexTreeNode1)
def test_search_when_one_move_left(self): hex_board: HexBoard1 = HexBoard1(3, HexBoard1.BLUE_CODE) hex_board.place_with_color((0, 0), HexBoard1.BLUE_CODE) hex_board.place_with_color((1, 1), HexBoard1.BLUE_CODE) hex_board.place_with_color((2, 0), HexBoard1.BLUE_CODE) hex_board.place_with_color((2, 1), HexBoard1.BLUE_CODE) hex_board.place_with_color((1, 0), HexBoard1.RED_CODE) hex_board.place_with_color((2, 2), HexBoard1.RED_CODE) hex_board.place_with_color((1, 2), HexBoard1.RED_CODE) hex_board.place_with_color((0, 1), HexBoard1.RED_CODE) hex_board.print_board_ui() print("hex_board.hex_board.is_game_over: ", hex_board.is_game_over) tree_node: MCTSNode = MCTSNode(None, hex_board, (0, 0)) to_expand_tree_node = MCTSRule.search(tree_node, 2) print("to_expand_tree_node: ", to_expand_tree_node)
hex_tree_root_node: MCTSNode = MCTSNode( parent_hex_node, root_hex_game.copy_hex_board(), placed_color_tuple) hex_tree_root_node.num_of_possible_child = len( root_hex_game.empty_coord_tuple_list) wasted_search_count: int = 0 for simulation in range(0, num_of_simulations_per_turn): is_root_node_exhausted: bool = ( hex_tree_root_node.num_of_exhausted_child == hex_tree_root_node.num_of_possible_child) if is_root_node_exhausted: print("root_node_exhausted") break best_tree_node_to_expand: MCTSNode = MCTSRule.search( hex_tree_root_node, c_exploration_para) expanded_tree_node: MCTSNode = MCTSRule.expand( best_tree_node_to_expand) is_tree_exhausted = ( expanded_tree_node == best_tree_node_to_expand) if is_tree_exhausted: wasted_search_count += 1 continue back_propagate_value: int = MCTSRule.simluate( expanded_tree_node, winner_color_name) MCTSRule.back_propagate(expanded_tree_node, back_propagate_value)