Beispiel #1
0
def check_fitness(trees):
    errors = []
    for one_tree in trees:
        if len(one_tree.tree_map) == 0:
            continue
        sum_error = 0
        good_individual = True
        for i in range(0, len(VARIABLE_VALUES_SET)):
            error = Reproductor.get_error(one_tree, TARGET_VALUES[i], VARIABLE_VALUES_SET[i])
            if error > ALLOWABLE_ERROR:
                good_individual = False
            sum_error += error
        if isinf(sum_error):
            continue
        errors.append(sum_error)
        if good_individual or sum_error < TARGET_RESULT:
            print("RESULT")
            print(Tree.tree_map_to_string(one_tree.tree_map))
            print(one_tree.init_tree)
            one_tree.fitness = sum_error
            results.append(one_tree)
    print("MIN FITNESS RESULT: ", min(errors))
    print "AVERAGE FITNESS: ", sum(errors)/len(errors)
    print "length ", len(errors)
    print "results "
    for r in results:
        print "fitness ", r.fitness
        print Tree.tree_map_to_string(r.tree_map)
Beispiel #2
0
def main():

    init_population = generate_init_population()
    result = False
    counter = 0

    while counter < ITERATIONS_COUNT:
        print("************************************************************************************************************"
              "************************************************************************************************************")
        print(counter)
        best_individuals = deepcopy(reproduce(init_population))

        if len(best_individuals) == 0:
            print("Reproduction empty")
            break

        new_generation = deepcopy(create_new_generation(best_individuals))
        mutated_trees = deepcopy(mutate_trees(new_generation))
        check_fitness(mutated_trees)
        init_population = deepcopy(mutated_trees)

        if len(init_population) == 0:
            print("Empty population")
            break
        counter += 1

    print("End")
    if len(results) > 0:
        print("")
        print "MIN RESULT"
        print Tree.tree_map_to_string(min(results))
        print min(results).init_tree
        print min(results).fitness
Beispiel #3
0
    def cross(self):
        try:
            if len(self._parent1.init_tree) <= 1 or len(self._parent2.init_tree) <= 1:
                return False
            index1 = self._get_index(list(self._parent1.init_tree))
            index2 = self._get_index(list(self._parent2.init_tree))
            while self.current_recursion_depth < MAX_RECURSION:
                if (isinstance(self._parent1.tree_map[index1], TwoVariableFunction) and
                        isinstance(self._parent2.tree_map[index2], TwoVariableFunction)) or \
                        (isinstance(self._parent1.tree_map[index1], OneVariableFunction)
                         and isinstance(self._parent2.tree_map[index2], OneVariableFunction)):
                    self._parent1.index = index1
                    self._parent2.index = index2

                    self._parent1.children = Tree([], {})
                    self._parent2.children = Tree([], {})

                    self._parent1.find_children()
                    self._parent2.find_children()

                    self._parent1.delete_subtree()
                    self._parent2.delete_subtree()

                    self.new_tree1 = self._parent1.add_child_to_tree()
                    self.new_tree2 = self._parent2.add_child_to_tree()
                    self.current_recursion_depth = 0
                    return True
                else:
                    index2 = self._get_index(deepcopy(self._parent2.init_tree))
                self.current_recursion_depth += 1
                return False
        except:
            print("Cross except")
            print("parent1 ",self._parent1.init_tree)
            print(Tree.tree_map_to_string(self._parent1.tree_map))
            print("index1 ", index1)
            print("parent2 ", self._parent2.init_tree)
            print(Tree.tree_map_to_string(self._parent2.tree_map))
            print("index2 ", index2)
Beispiel #4
0
def generate_init_population():
    trees = []
    j = 0
    while j < TREE_NUMBER:
        depth = (j+1)/2 + 1 # (j+100)/100 + 1
        tree_creator = tree_creation.TreeCreator(depth)
        if j % 2 == 0:
            tree_creator.create(False)
        else:
            tree_creator.create(True)
        t = tree_creator.tree
        trees.append(Tree(t.init_tree, t.tree_map))
        print(Tree.tree_map_to_string(t.tree_map))
        print(t.init_tree)
        print("")
        j += 1
    return trees