def test_expand_edge(self): """ Tests if expand child adds a story node to children """ root = State(ACTORS, PLACES, ITEMS) node = TreeNode(root, possible_methods=True) expand_edge(node) assert len(node.edges) == 1
def test_most_visited_child(self): """ Test the Most Visited Child function """ root_state = State(ACTORS, PLACES, ITEMS) root_node = TreeNode(root_state, parent_edge=None, possible_methods=True) edge1 = expand_edge(root_node) edge2 = expand_edge(root_node) edge3 = expand_edge(root_node) edge4 = expand_edge(root_node) edge1.next_node.visits = 3 edge2.next_node.visits = 4 edge3.next_node.visits = 5 edge4.next_node.visits = 4 assert most_visited_child(root_node) == edge3.next_node
def test_best_child_1(self): """ Test for the best_child function """ root_state = State(ACTORS, PLACES, ITEMS) root_node = TreeNode(root_state, parent_edge=None, possible_methods=True) root_node.visits = exp(3) edge1 = expand_edge(root_node) edge1.next_node.visits = 6 edge1.next_node.value = 1 edge2 = expand_edge(root_node) edge2.next_node.visits = 6 edge2.next_node.value = 3 edge3 = expand_edge(root_node) edge3.next_node.visits = 1.5 edge3.next_node.value = 1 assert best_child(root_node, 1) == edge2.next_node
def pickle_test_1(): root_state = State(ACTORS, PLACES, ITEMS) root_node = TreeNode(root_state, None, True) child_node = expand_edge(root_node).next_node with open("tree-root.p", "wb") as rootfile: pickle.dump(root_node, rootfile, protocol=pickle.HIGHEST_PROTOCOL) with open("tree-root.p", "rb") as rootfile: pickled_root_node = pickle.load(rootfile) os.remove("tree-root.p") print(root_node.edges[0].method.sentence) print(pickled_root_node.edges[0].method.sentence)
def selection(node, C, thres): """ Uses selection function to select most "promising" node on story tree """ # Expand edge only if these conditions are satisfied while node.believability > 0: # If successful, returns expanded edge, if not, return False new_edge = expand_edge(node) if new_edge: # If new edge exists, return child node return new_edge.next_node else: # If all children are expanded, return best child node = best_child(node, C) return node
def test_backpropogate(self): """ Test Backpropogation """ root_state = State(ACTORS, PLACES, ITEMS) root_node = TreeNode(root_state, parent_edge=None, possible_methods=True) edge1 = expand_edge(root_node) edge2 = expand_edge(edge1.next_node) edge3 = expand_edge(edge2.next_node) edge4 = expand_edge(edge2.next_node) backpropogate(edge3.next_node, 4) assert edge3.next_node.value == 4 assert edge2.next_node.value == 4 assert edge1.next_node.value == 4 assert root_node.value == 4 assert edge4.next_node.value == 0 backpropogate(edge4.next_node, 8) assert edge3.next_node.value == 4 assert edge2.next_node.value == 6 assert edge1.next_node.value == 6 assert root_node.value == 6 assert edge4.next_node.value == 8
def test_uct_func_1(self): """ Test for the uct_func function """ root_state = State(ACTORS, PLACES, ITEMS) root_node = TreeNode(root_state, parent_edge=None, possible_methods=True) root_node.visits = exp(3) test_edge = expand_edge(root_node) print(test_edge) test_node = test_edge.next_node test_node.visits = 6 test_node.value = 0 for C in range(0, 10): assert uct_func(test_node, C) == C test_node.visits = 0 assert uct_func(test_node, 1) == float("inf") root_node.visits = 0 assert uct_func(test_node, 1) == 0