def test_calc_node_depth(self): node.set_id(self.n1, 0) node.set_id(self.n2, 1) node.set_children(self.n1, [self.n2, self.n3]) node.set_children(self.n2, [self.n4, self.n5, self.n6]) self.assertEqual(node.calc_node_depth(self.n1), 2)
def test_get_all_node(self): node.set_id(self.n1, 0) node.set_id(self.n2, 1) node.set_children(self.n1, [self.n2, self.n3]) node.set_children(self.n2, [self.n4, self.n5, self.n6]) all_nodes = [self.n1, self.n2, self.n4, self.n5, self.n6, self.n3] self.assertEqual(node.get_all_node(self.n1), all_nodes)
def create_tree(): n1 = node.Node(0) n2 = node.Node(1) n3 = node.Node(0) n4 = node.Node(1) n5 = node.Node(0) node.set_children(n1, [n2, n3]) node.set_children(n2, [n4, n5]) return solution.Solution(n1)
def create_tree_type3(): n1 = node.Node(0) n2 = node.Node(1) n3 = node.Node(0) n4 = node.Node(1) n5 = node.Node(0) node.set_children(n1, [n2, n3, n4]) node.set_children(n2, [n5]) return Solution(n1)
def setUp(self): self.data = 5 self.cos2X = arithmetic.Cos2XProblem(self.data) self.n1 = node.Node(0) self.n2 = node.Node(1) self.n3 = node.Node(4) self.n4 = node.Node(4) self.n5 = node.Node(4) node.set_children(self.n1, [self.n2, self.n3]) node.set_children(self.n2, [self.n4, self.n5])
def create_tree(): # odd numbers are ids of terminal nodes, the others are non-terminal nodes. n1 = node.Node(0) n2 = node.Node(2) n3 = node.Node(1) n4 = node.Node(3) n5 = node.Node(5) node.set_children(n1, [n2, n3]) node.set_children(n2, [n4, n5]) return solution.Solution(n1)
def test_get_parent_node(self): node.set_id(self.n1, 0) node.set_id(self.n2, 1) node.set_children(self.n1, [self.n2, self.n3]) node.set_children(self.n2, [self.n4, self.n5, self.n6]) self.assertEqual(node.get_parent_node(self.n1, self.n4), (0, self.n2)) msg = 'There is no parent of root.' with self.assertRaises(ValueError, msg=msg): node.get_parent_node(self.n1, self.n1) msg = 'Invalid arguments: cannot find parent.' with self.assertRaises(ValueError, msg=msg): node.get_parent_node(node.Node(), node.Node())
def setUp(self): self.dim = 2 self.even_parity = boolean.EvenParity(self.dim) self.x = np.array( [[False, False], [True, False], [False, True], [True, True]], dtype=bool) self.y = np.array([False, True, True, False], dtype=bool) self.n1 = node.Node(0) self.n2 = node.Node(1) self.n3 = node.Node(4) self.n4 = node.Node(4) self.n5 = node.Node(4) node.set_children(self.n1, [self.n2, self.n3]) node.set_children(self.n2, [self.n4, self.n5])
def test_node_equal(self): node.set_id(self.n1, 0) node.set_id(self.n2, 0) node.set_id(self.n3, 0) node.set_id(self.n4, 0) node.set_id(self.n5, 0) node.set_id(self.n6, 0) node.set_children(self.n1, [self.n2, self.n3]) node.set_children(self.n4, [self.n5, self.n6]) self.assertTrue(node.node_equal(self.n1, self.n4)) self.assertTrue(node.node_equal(self.n1, self.n4, as_tree=True)) node.set_id(self.n2, 1) self.assertFalse(node.node_equal(self.n1, self.n3)) self.assertFalse(node.node_equal(self.n2, self.n3)) self.assertFalse(node.node_equal(self.n1, self.n4, as_tree=True))
def create_tree(root_id): # odd numbers are ids of terminal nodes, the others are non-terminal nodes. n1 = node.Node(root_id) n2 = node.Node(2) n3 = node.Node(1) n4 = node.Node(3) n5 = node.Node(5) node.set_children(n1, [n2, n3]) node.set_children(n2, [n4, n5]) s = solution.Solution(n1) solution.set_previous_fitness(s, root_id) return s
def setUp(self): self.n1 = node.Node(0) self.n2 = node.Node(1) self.n3 = node.Node(0) self.n4 = node.Node(1) self.n5 = node.Node(0) self.n6 = node.Node(1) self.f1 = node.Function(2) self.f2 = node.Function(3) # ``function_dict'' is not separated based on kinds of nodes # such as non-terminal nodes or terminal nodes here. self.function_dict = {0: self.f1, 1: self.f2} node.set_children(self.n1, [self.n2, self.n3]) node.set_children(self.n2, [self.n4, self.n5, self.n6]) self.s1 = solution.Solution(self.n1) self.depth = 2 self.n_nodes = 6
def test_destructive_crossover_core(self): s1_nodes = node.get_all_node(self.parents[0].root) s2_nodes = node.get_all_node(self.parents[1].root) points = [s1_nodes[0], s2_nodes[2]] expected_nodes1 = [node.Node(1)] expected_nodes2 = [node.Node(0), node.Node(1), node.Node(0), node.Node(1), node.Node(1), node.Node(0), node.Node(0), node.Node(0), node.Node(0)] node.set_children(expected_nodes2[0], [expected_nodes2[1], expected_nodes2[8]]) node.set_children(expected_nodes2[1], [expected_nodes2[2], expected_nodes2[7]]) node.set_children(expected_nodes2[2], [expected_nodes2[3], expected_nodes2[6]]) node.set_children(expected_nodes2[3], [expected_nodes2[4], expected_nodes2[5]]) new_s1, new_s2 = co.destructive_crossover(self.parents, points) self.assertTrue(node.node_equal(expected_nodes1[0], new_s1.root, as_tree=True)) self.assertTrue(node.node_equal(expected_nodes2[0], new_s2.root, as_tree=True)) self.assertTrue(self.parents[0] is new_s1) self.assertTrue(self.parents[1] is new_s2)
def test_get_nonterminal_nodes(self): node.set_id(self.n1, 0) node.set_id(self.n2, 1) node.set_children(self.n1, [self.n2, self.n3]) node.set_children(self.n2, [self.n4, self.n5]) node.set_children(self.n3, [self.n6]) nonterminal_nodes = node.get_all_nonterminal_nodes(self.n1) expected_nonterminal_nodes = [self.n1, self.n2, self.n3] for n in nonterminal_nodes: self.assertTrue(n in expected_nonterminal_nodes)
def test__copy_nodes_along_graph(self): node.set_id(self.n1, 0) node.set_id(self.n2, 1) node.set_children(self.n1, [self.n2, self.n3]) node.set_children(self.n2, [self.n4, self.n5]) node.set_children(self.n3, [self.n6]) graph = [(0, self.n1), (1, self.n2)] pos, parent, root = node.copy_nodes_along_graph(graph) self.assertEqual(pos, 1) self.assertTrue(node.node_equal(parent, self.n2)) self.assertTrue(node.node_equal(root, self.n1, as_tree=True)) self.assertFalse(parent is self.n2) self.assertFalse(root is self.n1)
def test_solution_equal(self): na1 = node.Node(0) na2 = node.Node(1) na3 = node.Node(0) na4 = node.Node(1) na5 = node.Node(0) na6 = node.Node(1) node.set_id(na1, 0) node.set_id(na2, 1) node.set_children(na1, [na2, na3]) node.set_children(na2, [na4, na5, na6]) sa = solution.Solution(na1) self.assertTrue(solution.solution_equal(self.s1, sa, True)) self.assertFalse(solution.solution_equal(self.s1, sa, False)) na4 = node.Node(0) node.set_children(na2, [na4, na5, na6]) self.assertFalse(solution.solution_equal(self.s1, sa, True))
def test_destructive_replace_node(self): na1 = node.Node(1) na2 = node.Node(0) node.set_id(na1, 0) node.set_id(na2, 1) node.set_children(na1, [na2]) original_s1 = copy.deepcopy(self.s1) new_s1 = solution.replace_node(self.s1, self.n2, na1, destructive=True) expected_nodes = [node.Node(0), node.Node(0), node.Node(1), node.Node(0)] node.set_children(expected_nodes[0], [expected_nodes[1], expected_nodes[3]]) node.set_children(expected_nodes[1], [expected_nodes[2]]) self.assertTrue(node.node_equal(new_s1.root, expected_nodes[0], as_tree=True)) self.assertEqual(new_s1.n_nodes, len(expected_nodes)) self.assertEqual(new_s1.depth, 2) # Check whether the original solution is NOT protected. self.assertFalse(solution.solution_equal(original_s1, new_s1)) self.assertTrue(self.s1 is new_s1)
def test_set_children(self): children = [self.n2, self.n3] node.set_children(self.n1, children) self.assertEqual(self.n1.children, children)