Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
    def __call__(self):
        """
        Generate a new solution.
        :return: solution object. solution
        """
        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

        root = new_node(None, 0)

        return solution.Solution(root)
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    def __call__(self):
        """
        Generate all solutions which have an only terminal node.
        :return: list of solution object. list of all terminal solutions
        """
        def make_node(func_id):
            new_node = node.Node()
            node.set_id(new_node, func_id)
            return new_node

        population = [
            solution.Solution(make_node(func_id))
            for func_id in self.terminal_list
        ]
        return population
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    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))
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
 def setUp(self):
     ExampleSolution.__init__(self)
     self.problem = EmptyProblem()
     self.pop = [solution.Solution(node.Node()) for _ in range(100)]
Ejemplo n.º 10
0
 def test_fitness(self):
     s = solution.Solution(self.n1)
     print(self.even_parity.fitness(s))
Ejemplo n.º 11
0
 def test_fitness(self):
     s = solution.Solution(self.n1)
     print(self.cos2X.fitness(s))
Ejemplo n.º 12
0
 def setUp(self):
     self.pop = [solution.Solution(node.Node()) for _ in range(100)]