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 get_population(): s1 = solution.Solution(node.Node()) s2 = solution.Solution(node.Node()) s3 = solution.Solution(node.Node()) solution.set_previous_fitness(s1, 1) solution.set_previous_fitness(s2, 2) solution.set_previous_fitness(s3, 3) pop = [] for _ in range(2): pop.extend([s1, s2, s3]) pop = pop * 2 return pop
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 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 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 new_node(parent, depth): current_node = node.Node() if self.t_prob > random.random() or depth == self.max_depth: func_id = random.choice(self.terminal_list) else: current_node.children = [] func_id = random.choice(self.nonterminal_list) n_child = node.get_n_children( func_id, self.func_bank.get_function_list()) for _ in range(n_child): new_node(current_node, depth + 1) node.set_id(current_node, func_id) if parent is not None: parent.children.append(current_node) else: return current_node
def setUp(self): self.n1 = node.Node() self.n2 = node.Node() self.n3 = node.Node() self.n4 = node.Node() self.n5 = node.Node() self.n6 = node.Node() 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}
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 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 setUp(self): self.pop = [solution.Solution(node.Node()) for _ in range(100)]
def setUp(self): ExampleSolution.__init__(self) self.problem = EmptyProblem() self.pop = [solution.Solution(node.Node()) for _ in range(100)]
def make_node(func_id): new_node = node.Node() node.set_id(new_node, func_id) return new_node