Exemplo n.º 1
0
def neat_GP_LS(population,
               toolbox,
               cxpb,
               mutpb,
               ngen,
               neat_alg,
               neat_cx,
               neat_h,
               neat_pelit,
               LS_flag,
               LS_select,
               cont_evalf,
               num_salto,
               SaveMatrix,
               GenMatrix,
               pset,
               n_corr,
               num_p,
               params,
               direccion,
               problem,
               testing,
               version,
               benchmark_flag,
               beta,
               random_speciation,
               config,
               stats=None,
               halloffame=None,
               verbose=__debug__):
    """This algorithm reproduce the simplest evolutionary algorithm as
    presented in chapter 7 of [Back2000]_.

    :param population: A list of individuals.
    :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution
                    operators.
    :param cxpb: The probability of mating two individuals.
    :param mutpb: The probability of mutating an individual.
    :param ngen: The number of generation.
    :param neat_alg: wheter or not to use species stuff.
    :param neat_cx: wheter or not to use neatGP cx
    :param neat_h: indicate the distance allowed between each specie
    :param neat_pelit: probability of being elitist, it's used in the neat cx and mutation
    :param LS_flag: wheter or not to use LocalSearchGP
    :param LS_select: indicate the kind of selection to use the LSGP on the population.
    :param cont_evalf: contador maximo del numero de evaluaciones
    :param n_corr: run number just to wirte the txt file
    :param p: problem number just to wirte the txt file
    :param params:indicate the params for the fitness sharing, the diffetent
                    options are:
                    -DontPenalize(str): 'best_specie' or 'best_of_each_specie'
                    -Penalization_method(int):
                        1.without penalization
                        2.penalization fitness sharing
                        3.new penalization
                    -ShareFitness(str): 'yes' or 'no'
    :param stats: A :class:`~deap.tools.Statistics` object that is updated
                  inplace, optional.
    :param halloffame: A :class:`~deap.tools.HallOfFame` object that will
                       contain the best individuals, optional.
    :param verbose: Whether or not to log the statistics.
    :returns: The final population.

    It uses :math:`\lambda = \kappa = \mu` and goes as follow.
    It first initializes the population (:math:`P(0)`) by evaluating
    every individual presenting an invalid fitness. Then, it enters the
    evolution loop that begins by the selection of the :math:`P(g+1)`
    population. Then the crossover operator is applied on a proportion of
    :math:`P(g+1)` according to the *cxpb* probability, the resulting and the
    untouched individuals are placed in :math:`P'(g+1)`. Thereafter, a
    proportion of :math:`P'(g+1)`, determined by *mutpb*, is
    mutated and placed in :math:`P''(g+1)`, the untouched individuals are
    transferred :math:`P''(g+1)`. Finally, those new individuals are evaluated
    and the evolution loop continues until *ngen* generations are completed.
    Briefly, the operators are applied in the following order ::

        evaluate(population)
        for i in range(ngen):
            offspring = select(population)
            offspring = mate(offspring)
            offspring = mutate(offspring)
            evaluate(offspring)
            population = offspring

    This function expects :meth:`toolbox.mate`, :meth:`toolbox.mutate`,
    :meth:`toolbox.select` and :meth:`toolbox.evaluate` aliases to be
    registered in the toolbox.

    .. [Back2000] Back, Fogel and Michalewicz, "Evolutionary Computation 1 :
       Basic Algorithms and Operators", 2000.
    """

    logbook = tools.Logbook()
    logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])

    #creating files to save data.
    d = './Results/%s/pop_file_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    pop_file = open(d, 'a')

    d = './Results/%s/bestind_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    best = open(d, 'w')  # save data

    d = './Results/%s/bestind_str_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    out = open(d, 'w')

    d = './Timing/%s/pop_file_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    time_file = open(d, 'w')

    d = './Timing/%s/specie_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    time_specie = open(d, 'w')

    d = './Timing/%s/cruce_s_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    time_cx = open(d, 'w')

    d = './Specie/%s/specieind_%d_%d.csv' % (problem, num_p, n_corr)
    ensure_dir(d)
    specie_file = open(d, 'w')

    d = './Specie/%s/speciepop_%d_%d.csv' % (problem, num_p, n_corr)
    ensure_dir(d)
    specie_file_2 = open(d, 'w')

    d = './Specie/%s/specist_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    specie_statis = open(d, 'w')

    d = './Results/%s/bestind_LStr_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    bestind = open(d, 'w')

    d = './Matrix/%s/' % (problem)
    ensure_dir(d)

    begin = time.time()

    if SaveMatrix:  # Saving data in matrix
        num_r = 11
        num_r_sp = 2
        if GenMatrix:
            num_salto = 1
            num_c = ngen + 1
            Matrix = np.empty((
                num_c,
                num_r,
            ))
            Matrix_specie = np.empty((
                num_c,
                num_r_sp,
            ), dtype=object)
            vector = np.arange(0, num_c, num_salto)
        else:
            num_c = (cont_evalf / num_salto) + 1
            num_c_sp = (cont_evalf / num_salto) + 1
            Matrix = np.empty((
                num_c,
                num_r,
            ))
            Matrix_specie = np.empty((
                num_c_sp,
                num_r_sp,
            ), dtype=object)
            vector = np.arange(1, cont_evalf + num_salto, num_salto)
        for i in range(len(vector)):
            Matrix[i, 0] = vector[i]
            Matrix_specie[i, 0] = vector[i]
        Matrix[:, 6] = 0.

    if neat_alg:
        begin_sp = time.time()
        if version == 1:
            for ind in population:
                bit = p_bin(ind)
                ind.binary_rep_set(bit)
        elif version != 1:
            for ind in population:
                level_info = level_node(ind)
                ind.nodefeat_set(level_info)

        if random_speciation:
            species_random(population, neat_h, version, beta)
        else:
            species(population, neat_h, version, beta)

        end_sp = time.time()
        time_specie.write(
            '\n%s;%s;%s;%s' %
            (0, begin_sp, end_sp, str(round(end_sp - begin_sp, 2))))

        for ind in population:
            specie_file_2.write('\n%s,%s,%s' %
                                (0, ind.get_specie(), ind.get_intracluster()))

        list_s = list_species(population)
        for specie in list_s:
            list_ind = get_ind_specie(specie, population)
            specie_file.write('\n%s,%s,%s' %
                              (0, specie, list_ind[0].get_intracluster()))

    if funcEval.LS_flag:
        for ind in population:
            sizep = len(ind) + 2
            param_ones = np.ones(sizep)
            param_ones[0] = 0
            ind.params_set(param_ones)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in population if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        funcEval.cont_evalp += 1
        ind.fitness.values = fit

    best_ind = best_pop(population)  # best individual of the population
    if testing:
        fitnesst_best = toolbox.map(toolbox.evaluate_test, [best_ind])
        best_ind.fitness_test.values = fitnesst_best[0]
    if testing:
        best.write(
            '\n%s;%s;%s;%s;%s;%s' %
            (0, funcEval.cont_evalp, best_ind.fitness_test.values[0],
             best_ind.fitness.values[0], len(best_ind), avg_nodes(population)))
    else:
        best.write('\n%s;%s;%s;%s;%s;%s' %
                   (0, funcEval.cont_evalp, None, best_ind.fitness.values[0],
                    len(best_ind), avg_nodes(population)))

    data_pop = avg_nodes(population)

    if SaveMatrix:
        idx = 0
        Matrix[idx, 1] = best_ind.fitness.values[0]
        if testing:
            Matrix[idx, 2] = best_ind.fitness_test.values[0]
        else:
            Matrix[idx, 2] = None
        Matrix[idx, 3] = len(best_ind)
        Matrix[idx, 4] = data_pop[0]
        Matrix[idx, 5] = 0.
        Matrix[idx, 6] = 1  # just an id to know if the current row is full
        Matrix[idx, 7] = data_pop[1]  # max size
        Matrix[idx, 8] = data_pop[2]  # min size
        Matrix[idx, 9] = ind.get_specie(
        )  # max number of species in the current population
        Matrix[idx, 10] = max(ind_specie(population), key=lambda x: x[0])[
            0]  # max number of species in the current population

        np.savetxt('./Matrix/%s/idx_%d_%d.txt' % (problem, num_p, n_corr),
                   Matrix,
                   delimiter=",",
                   fmt="%s")

    if neat_alg:
        SpeciesPunishment(population, params, neat_h)

    if funcEval.LS_flag == 1:
        strg = best_ind.__str__()
        l_strg = add_subt_cf(strg, args=[])
        c = tree2f()
        cd = c.convert(l_strg)
        out.write('\n%s;%s;%s;%s;%s;%s' %
                  (0, len(best_ind), best_ind.LS_applied_get(),
                   best_ind.get_params(), cd, best_ind))
    else:
        out.write('\n%s;%s;%s' % (0, len(best_ind), best_ind))

    for ind in population:
        pop_file.write('\n%s;%s;%s' % (0, ind.fitness.values[0], ind))

    print '---- Generation %d -----' % (0)
    print 'Problem: ', problem
    print 'Problem No.: ', num_p
    print 'Run No.: ', n_corr
    print 'neat-GP:', neat_alg
    print 'neat-cx:', neat_cx
    print 'Local Search:', funcEval.LS_flag
    if funcEval.LS_flag:
        ls_type = ''
        if LS_select == 1:
            ls_type = 'LSHS'
        elif LS_select == 2:
            ls_type = 'Best-Sp'
        elif LS_select == 3:
            ls_type = 'LSHS-Sp'
        elif LS_select == 4:
            ls_type = 'Best-Pop'
        elif LS_select == 5:
            ls_type = 'All-Pop'
        elif LS_select == 6:
            ls_type = 'LSHS-test'
        elif LS_select == 7:
            ls_type = 'Best set'
        elif LS_select == 8:
            ls_type = 'Random set'
        elif LS_select == 9:
            ls_type = "Best-Random set"
        print 'Local Search Heuristic: %s (%s)' % (LS_select, ls_type)
    print 'Best Ind.:', best_ind
    print 'Best Fitness:', best_ind.fitness.values[0]
    if testing:
        print 'Test fitness:', best_ind.fitness_test.values[0]
    print 'Avg Nodes:', avg_nodes(population)
    print 'Evaluations: ', funcEval.cont_evalp
    end_t = time.time()

    if SaveMatrix:
        idx = 0
        Matrix_specie[idx, 1] = ind_specie(population)
        np.savetxt('./Specie/%s/specist_%d_%d.txt' % (problem, num_p, n_corr),
                   Matrix_specie,
                   delimiter=";",
                   fmt="%s")
    if testing:
        time_file.write(
            '\n%s;%s;%s;%s;%s;%s' %
            (0, begin, end_t, str(round(end_t - begin, 2)),
             best_ind.fitness.values[0], best_ind.fitness_test.values[0]))
    else:
        time_file.write('\n%s;%s;%s;%s;%s' %
                        (0, begin, end_t, str(round(
                            end_t - begin, 2)), best_ind.fitness.values[0]))

    # Begin the generational process
    for gen in range(1, ngen + 1):
        print "hola"
        if not GenMatrix:
            if funcEval.cont_evalp > cont_evalf:
                break

        begin = time.time()
        print '---- Generation %d -----' % (gen)
        print 'Problem: ', problem
        print 'Problem No.: ', num_p
        print 'Run No.: ', n_corr
        print 'neat-GP:', neat_alg
        print 'neat-cx:', neat_cx
        print 'Local Search:', funcEval.LS_flag
        if funcEval.LS_flag:
            print 'Local Search Heuristic: %s (%s)' % (LS_select, ls_type)

        best_ind = copy.deepcopy(best_pop(population))
        if neat_alg:
            # survival_p = p_worst
            parents = p_selection(population, survival_p=0.5)
        else:
            parents = toolbox.select(population, len(population))

        begin_cx = time.time()
        if neat_alg:  # neat-Crossover neat_cx and
            n = len(parents)
            mut = 1
            cx = 1
            offspring = neatGP(toolbox, parents, cxpb, mutpb, n, mut, cx,
                               neat_pelit, neat_cx)
        else:
            offspring = varOr(parents, toolbox, cxpb, mutpb)
        end_cx = time.time()
        time_specie.write(
            '\n%s;%s;%s;%s' %
            (0, begin_cx, end_cx, str(round(end_cx - begin_cx, 2))))

        if neat_alg:  # Speciation of the descendants
            begin_sp = time.time()

            if version == 1:
                for ind in offspring:
                    if ind.binary_rep_get() is None:
                        bit = p_bin(ind)
                        ind.binary_rep_set(bit)

            elif version == 2:
                for ind in offspring:
                    if ind.nodefeat_get() is None:
                        level_info = level_node(ind)
                        ind.nodefeat_set(level_info)

            if random_speciation:
                specie_offspring_random(parents, offspring, neat_h, version,
                                        beta)
            else:
                specie_parents_child(parents, offspring, neat_h, version, beta)

            #calc_intracluster(population)
            offspring[:] = parents + offspring
            calc_intracluster(offspring)

            for ind in offspring:
                specie_file_2.write(
                    '\n%s,%s,%s' %
                    (gen, ind.get_specie(), ind.get_intracluster()))

            list_s = list_species(offspring)
            for specie in list_s:
                list_ind = get_ind_specie(specie, offspring)
                specie_file.write(
                    '\n%s,%s,%s' %
                    (gen, specie, list_ind[0].get_intracluster()))

            invalid_ind = [ind for ind in offspring]
            if funcEval.LS_flag:
                new_invalid_ind = []
                for ind in invalid_ind:
                    strg = ind.__str__()
                    l_strg = add_subt(strg, ind)
                    c = tree2f()
                    cd = c.convert(l_strg)
                    new_invalid_ind.append(cd)
                fitness_ls = toolbox.map(toolbox.evaluate, new_invalid_ind)
                for ind, ls_fit in zip(invalid_ind, fitness_ls):
                    funcEval.cont_evalp += 1
                    ind.fitness.values = ls_fit
            else:
                fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
                for ind, fit in zip(invalid_ind, fitnesses):
                    funcEval.cont_evalp += 1
                    ind.fitness.values = fit

            end_sp = time.time()
            time_specie.write(
                '\n%s;%s;%s;%s' %
                (gen, begin_sp, end_sp, str(round(end_sp - begin_sp, 2))))
            print "neat-alg"
        else:
            invalid_ind = [ind for ind in offspring]
            if funcEval.LS_flag:
                new_invalid_ind = []
                for ind in invalid_ind:
                    strg = ind.__str__()
                    l_strg = add_subt(strg, ind)
                    c = tree2f()
                    cd = c.convert(l_strg)
                    new_invalid_ind.append(cd)
                fitness_ls = toolbox.map(toolbox.evaluate, new_invalid_ind)
                for ind, ls_fit in zip(invalid_ind, fitness_ls):
                    funcEval.cont_evalp += 1
                    ind.fitness.values = ls_fit
            else:
                fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
                for ind, fit in zip(invalid_ind, fitnesses):
                    funcEval.cont_evalp += 1
                    ind.fitness.values = fit

            orderbyfit = sorted(offspring, key=lambda ind: ind.fitness.values)

            if best_ind.fitness.values[0] <= orderbyfit[0].fitness.values[0]:
                offspring[:] = [best_ind] + orderbyfit[:len(population) - 1]

        if neat_alg:
            SpeciesPunishment(offspring, params, neat_h)

        if SaveMatrix:
            if GenMatrix:
                idx_aux = np.searchsorted(Matrix[:, 0], gen)
                Matrix_specie[idx_aux, 1] = ind_specie(offspring)
            else:
                idx_aux = np.searchsorted(Matrix_specie[:, 0],
                                          funcEval.cont_evalp)
                try:
                    Matrix_specie[idx_aux, 1] = ind_specie(offspring)
                except IndexError:
                    Matrix_specie[-1, 1] = ind_specie(offspring)
            np.savetxt('./Specie/%s/specist_%d_%d.txt' %
                       (problem, num_p, n_corr),
                       Matrix_specie,
                       delimiter=";",
                       fmt="%s")

        population[:] = offspring  # population update

        cond_ind = 0
        cont_better = 0
        if funcEval.LS_flag:
            for ind in population:
                ind.LS_applied_set(0)

            if LS_select == 1:
                trees_h(population, num_p, n_corr, pset, direccion, problem,
                        benchmark_flag)
            elif LS_select == 2:
                best_specie(population, num_p, n_corr, pset, direccion,
                            problem, benchmark_flag)
            elif LS_select == 3:
                specie_h(population, num_p, n_corr, pset, direccion, problem,
                         benchmark_flag)
            elif LS_select == 4:
                best_pop_ls(population, num_p, n_corr, pset, direccion,
                            problem, benchmark_flag)
            elif LS_select == 5:
                all_pop(population, num_p, n_corr, pset, direccion, problem,
                        benchmark_flag)
            elif LS_select == 6:
                trees_h_wo(population, num_p, n_corr, pset, direccion, problem,
                           benchmark_flag)
            elif LS_select == 7:
                ls_bestset(population, num_p, n_corr, pset, direccion, problem,
                           benchmark_flag)
            elif LS_select == 8:
                ls_random(population, num_p, n_corr, pset, direccion, problem,
                          benchmark_flag)
            elif LS_select == 9:
                ls_randbestset(population, num_p, n_corr, pset, direccion,
                               problem, benchmark_flag)
            #
            invalid_ind = [ind for ind in population]
            new_invalid_ind = []
            for ind in population:
                strg = ind.__str__()
                l_strg = add_subt(strg, ind)
                c = tree2f()
                cd = c.convert(l_strg)
                new_invalid_ind.append(cd)
            fitness_ls = toolbox.map(toolbox.evaluate, new_invalid_ind)
            print 'Fitness comp.:',
            for ind, ls_fit in zip(invalid_ind, fitness_ls):
                if ind.LS_applied_get() == 1:
                    cond_ind += 1
                    if ind.fitness.values[0] < ls_fit:
                        print '-',
                    elif ind.fitness.values[0] > ls_fit:
                        cont_better += 1
                        print '+',
                    elif ind.fitness.values[0] == ls_fit:
                        print '=',
                funcEval.cont_evalp += 1
                pop_file.write(
                    '\n%s;%s;%s;%s;%s;%s;%s;%s' %
                    (gen, ind.fitness.values[0], ls_fit, ind.LS_applied_get(),
                     ind, [x for x in ind.get_parent()] if ind.get_parent()
                     is not None else [], [x for x in ind.get_params()
                                           ], ind.get_id()))
                ind.fitness.values = ls_fit
            print ''

        #for ind in population:

        else:

            for ind in population:
                pop_file.write('\n%s;%s;%s;%s;%s;%s;%s' %
                               (gen, ind.fitness.values[0],
                                ind.LS_applied_get(), ind.LS_story_get(),
                                ind.off_cx_get(), ind.off_mut_get(), ind))

        best_ind = best_pop(population)
        if funcEval.LS_flag:
            strg = best_ind.__str__()
            l_strg = add_subt(strg, best_ind)
            c = tree2f()
            cd = c.convert(l_strg)
            new_invalid_ind.append(cd)
            bestind.write('\n%s;%s;%s' % (gen, best_ind.fitness.values[0], cd))
            toolbox.map(toolbox.evaluate_best_t, [cd])
            if testing:
                fit_best = toolbox.map(toolbox.evaluate_best, [cd])
                best_ind.fitness_test.values = fit_best[0]

                best.write(
                    '\n%s;%s;%s;%s;%s;%s;%s' %
                    (gen, funcEval.cont_evalp, best_ind.fitness.values[0],
                     best_ind.LS_fitness_get(),
                     best_ind.fitness_test.values[0], len(best_ind),
                     avg_nodes(population)))
            else:
                best.write(
                    '\n%s;%s;%s;%s;%s;%s;%s' %
                    (gen, funcEval.cont_evalp, best_ind.fitness.values[0],
                     best_ind.LS_fitness_get(), None, len(best_ind),
                     avg_nodes(population)))
            out.write('\n%s;%s;%s;%s;%s;%s' %
                      (gen, len(best_ind), best_ind.LS_applied_get(),
                       best_ind.get_params(), cd, best_ind))
        else:
            toolbox.map(toolbox.evaluate_best_t, [best_ind])
            if testing:
                fitnesses_test = toolbox.map(toolbox.evaluate_best, [best_ind])
                best_ind.fitness_test.values = fitnesses_test[0]
                best.write(
                    '\n%s;%s;%s;%s;%s;%s' %
                    (gen, funcEval.cont_evalp, best_ind.fitness_test.values[0],
                     best_ind.fitness.values[0], len(best_ind),
                     avg_nodes(population)))
            else:
                best.write('\n%s;%s;%s;%s;%s;%s' %
                           (gen, funcEval.cont_evalp, None,
                            best_ind.fitness.values[0], len(best_ind),
                            avg_nodes(population)))
            bestind.write('\n%s;%s;%s' %
                          (gen, best_ind.fitness.values[0], best_ind))
            out.write('\n%s;%s;%s' % (gen, len(best_ind), best_ind))

        if funcEval.LS_flag:
            print 'Num. LS:', cond_ind
            print 'Ind. Improvement:', cont_better
            print 'Best Ind. LS:', best_ind.LS_applied_get()

        print 'Best Ind.:', best_ind
        print 'Best Fitness:', best_ind.fitness.values[0]
        if testing:
            print 'Test fitness:', best_ind.fitness_test.values[0]
        print 'Avg Nodes:', avg_nodes(population)
        print 'Evaluations: ', funcEval.cont_evalp

        if SaveMatrix:
            data_pop = avg_nodes(population)
            if GenMatrix:
                idx_aux = np.searchsorted(Matrix[:, 0], gen)
                Matrix[idx_aux, 1] = best_ind.fitness.values[0]
                if testing:
                    Matrix[idx_aux, 2] = best_ind.fitness_test.values[0]
                else:
                    Matrix[idx_aux, 2] = None
                Matrix[idx_aux, 3] = len(best_ind)
                Matrix[idx_aux, 4] = data_pop[0]
                Matrix[idx_aux, 5] = gen
                Matrix[idx_aux, 6] = 1
                Matrix[idx_aux, 7] = data_pop[1]  # max nodes
                Matrix[idx_aux, 8] = data_pop[2]  # min nodes
                if neat_alg:
                    Matrix[idx_aux, 9] = best_ind.get_specie()
                    Matrix[idx_aux, 10] = max(ind_specie(population),
                                              key=lambda x: x[0])[0]
            else:
                if funcEval.cont_evalp >= cont_evalf:
                    num_c -= 1
                    idx_aux = num_c
                    Matrix[num_c, 1] = best_ind.fitness.values[0]
                    if testing:
                        Matrix[num_c, 2] = best_ind.fitness_test.values[0]
                    else:
                        Matrix[num_c, 2] = None
                    Matrix[num_c, 3] = len(best_ind)
                    Matrix[num_c, 4] = data_pop[0]
                    Matrix[num_c, 5] = gen
                    Matrix[num_c, 6] = 1
                    Matrix[num_c, 7] = data_pop[1]  #max_nodes
                    Matrix[num_c, 8] = data_pop[2]  #min nodes
                    if neat_alg:
                        Matrix[num_c, 9] = max(ind_specie(population),
                                               key=lambda x: x[0])[0]

                else:
                    idx_aux = np.searchsorted(Matrix[:, 0],
                                              funcEval.cont_evalp)
                    Matrix[idx_aux, 1] = best_ind.fitness.values[0]
                    if testing:
                        Matrix[idx_aux, 2] = best_ind.fitness_test.values[0]
                    else:
                        Matrix[idx_aux, 2] = None
                    Matrix[idx_aux, 3] = len(best_ind)
                    Matrix[idx_aux, 4] = data_pop[0]
                    Matrix[idx_aux, 5] = gen
                    Matrix[idx_aux, 6] = 1
                    Matrix[idx_aux, 7] = data_pop[1]  #max nodes
                    Matrix[idx_aux, 8] = data_pop[2]  #min nodes
                    if neat_alg:
                        Matrix[idx_aux, 9] = max(ind_specie(population),
                                                 key=lambda x: x[0])[0]

                id_it = idx_aux - 1
                id_beg = 0
                flag = True
                flag2 = False
                while flag:
                    if Matrix[id_it, 6] == 0:
                        id_it -= 1
                        flag2 = True
                    else:
                        id_beg = id_it
                        flag = False
                if flag2:
                    x = Matrix[id_beg, 1:8]
                    Matrix[id_beg:idx_aux, 1:] = Matrix[id_beg, 1:]

            np.savetxt('./Matrix/%s/idx_%d_%d.txt' % (problem, num_p, n_corr),
                       Matrix,
                       delimiter=",",
                       fmt="%s")

        end_t = time.time()
        if testing:
            time_file.write(
                '\n%s;%s;%s;%s;%s;%s' %
                (gen, begin, end_t, str(round(end_t - begin, 2)),
                 best_ind.fitness.values[0], best_ind.fitness_test.values[0]))
        else:
            time_file.write('\n%s;%s;%s;%s;%s' %
                            (gen, begin, end_t, str(round(end_t - begin, 2)),
                             best_ind.fitness.values[0]))


#        import convert_order
#        convert_order.convert_(population)
    return population, logbook, best_ind.fitness.values[
        0], best_ind.fitness_test.values[0]
Exemplo n.º 2
0
def neat_GP( population, toolbox, cxpb, mutpb, ngen, neat_alg, neat_cx, neat_h, neat_pelit, n_corr, num_p, params, problem, stats=None,
             halloffame=None, verbose=__debug__):
    """This algorithm reproduce the simplest evolutionary algorithm as
    presented in chapter 7 of [Back2000]_.

    :param population: A list of individuals.
    :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution
                    operators.
    :param cxpb: The probability of mating two individuals.
    :param mutpb: The probability of mutating an individual.
    :param ngen: The number of generation.
    :param neat_alg: wheter or not to use species stuff.
    :param neat_cx: wheter or not to use neatGP cx
    :param neat_h: indicate the distance allowed between each specie
    :param neat_pelit: probability of being elitist, it's used in the neat cx and mutation
    :param n_corr: run number just to wirte the txt file
    :param num_p: problem number just to wirte the txt file
    :param params:indicate the params for the fitness sharing, the diffetent
                    options are:
                    -DontPenalize(str): 'best_specie' or 'best_of_each_specie'
                    -Penalization_method(int):
                        1.without penalization
                        2.penalization fitness sharing
                        3.new penalization
                    -ShareFitness(str): 'yes' or 'no'
    :param problem: (str) name of the problem.
    :param stats: A :class:`~deap.tools.Statistics` object that is updated
                  inplace, optional.
    :param halloffame: A :class:`~deap.tools.HallOfFame` object that will
                       contain the best individuals, optional.
    :param verbose: Whether or not to log the statistics.
    :returns: The final population.

    It uses :math:`\lambda = \kappa = \mu` and goes as follow.
    It first initializes the population (:math:`P(0)`) by evaluating
    every individual presenting an invalid fitness. Then, it enters the
    evolution loop that begins by the selection of the :math:`P(g+1)`
    population. Then the crossover operator is applied on a proportion of
    :math:`P(g+1)` according to the *cxpb* probability, the resulting and the
    untouched individuals are placed in :math:`P'(g+1)`. Thereafter, a
    proportion of :math:`P'(g+1)`, determined by *mutpb*, is
    mutated and placed in :math:`P''(g+1)`, the untouched individuals are
    transferred :math:`P''(g+1)`. Finally, those new individuals are evaluated
    and the evolution loop continues until *ngen* generations are completed.
    Briefly, the operators are applied in the following order ::

        evaluate(population)
        for i in range(ngen):
            offspring = select(population)
            offspring = mate(offspring)
            offspring = mutate(offspring)
            evaluate(offspring)
            population = offspring

    This function expects :meth:`toolbox.mate`, :meth:`toolbox.mutate`,
    :meth:`toolbox.select` and :meth:`toolbox.evaluate` aliases to be
    registered in the toolbox.

    .. [Back2000] Back, Fogel and Michalewicz, "Evolutionary Computation 1 :
       Basic Algorithms and Operators", 2000.
    """

    logbook = tools.Logbook()
    logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])

    if neat_alg:  # assign specie to each individual on the population
        species(population,neat_h)
        ind_specie(population)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in population if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    # save data for the best individual
    best = open('./Results/%s/bestind_%d_%d.txt'%(problem, num_p, n_corr), 'a')
    best_st = open('./Results/%s/bestind_string_%d_%d.txt' % (problem, num_p, n_corr), 'a')

    #  take the best on the population
    best_ind = best_pop(population)
    fitnesst_best = toolbox.map(toolbox.evaluate_test, [best_ind])
    best_ind.fitness_test.values=fitnesst_best[0]
    best.write('\n%s;%s;%s;%s;%s' % (0, best_ind.fitness_test.values[0], best_ind.fitness.values[0], len(best_ind), avg_nodes(population)))
    best_st.write('\n%s;%s' % (0, best_ind))

    if neat_alg:  # applying fitness sharing
        SpeciesPunishment(population,params,neat_h)

    if halloffame is not None:
        halloffame.update(population)

    record = stats.compile(population) if stats else {}
    logbook.record(gen=0, nevals=len(invalid_ind), **record)
    if verbose:
        print logbook.stream

    # Begin the generational process
    for gen in range(1, ngen+1):

        best_ind = copy.deepcopy(best_pop(population))
        if neat_alg:  # select set of parents
            parents = p_selection(population, gen)
        else:
            parents = toolbox.select(population, len(population))

        if neat_cx:  # applying neat-crossover
            n = len(parents)
            mut = 1
            cx = 1
            offspring = neatGP(toolbox, parents, cxpb, mutpb, n, mut, cx, neat_pelit)
        else:
            offspring = varOr(parents, toolbox, cxpb, mutpb)

        if neat_alg:  # Assign species
            specie_parents_child(parents, offspring, neat_h)
            offspring[:] = parents+offspring
            ind_specie(offspring)

            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring]
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit
        else:
            invalid_ind = [ind for ind in offspring]
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit

            orderbyfit = sorted(offspring, key=lambda ind: ind.fitness.values)

            if best_ind.fitness.values[0] <= orderbyfit[0].fitness.values[0]:
                offspring[:] = [best_ind] + orderbyfit[:len(population) - 1]

        if neat_alg:
            SpeciesPunishment(offspring, params, neat_h)

        # Update the hall of fame with the generated individuals
        if halloffame is not None:
            halloffame.update(offspring)

        # Replace the current population by the offspring
        population[:] = offspring

        # Append the current generation statistics to the logbook
        record = stats.compile(population) if stats else {}
        logbook.record(gen=gen, nevals=len(population), **record)
        if verbose:
            print logbook.stream

        best_ind = best_pop(population)
        fitnesses_test = toolbox.map(toolbox.evaluate_test, [best_ind])
        best_ind.fitness_test.values = fitnesses_test[0]
        best.write('\n%s;%s;%s;%s;%s'%(gen, best_ind.fitness_test.values[0], best_ind.fitness.values[0], len(best_ind), avg_nodes(population)))
        best_st.write('\n%s;%s' % (gen, best_ind))

    return population, logbook
Exemplo n.º 3
0
def evo_neat_GP_LS(population, toolbox, cxpb, mutpb, ngen, neat_alg, neat_cx, neat_h,neat_pelit, LS_flag, LS_select, cont_evalf, num_salto, SaveMatrix, GenMatrix, pset,n_corr, num_p, params, direccion, problem,stats=None,
             halloffame=None, verbose=__debug__):
    """This algorithm reproduce the simplest evolutionary algorithm as
    presented in chapter 7 of [Back2000]_.

    :param population: A list of individuals.
    :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution
                    operators.
    :param cxpb: The probability of mating two individuals.
    :param mutpb: The probability of mutating an individual.
    :param ngen: The number of generation.
    :param neat_alg: wheter or not to use species stuff.
    :param neat_cx: wheter or not to use neatGP cx
    :param neat_h: indicate the distance allowed between each specie
    :param neat_pelit: probability of being elitist, it's used in the neat cx and mutation
    :param LS_flag: wheter or not to use LocalSearchGP
    :param LS_select: indicate the kind of selection to use the LSGP on the population.
    :param cont_evalf: contador maximo del numero de evaluaciones
    :param n_corr: run number just to wirte the txt file
    :param p: problem number just to wirte the txt file
    :param params:indicate the params for the fitness sharing, the diffetent
                    options are:
                    -DontPenalize(str): 'best_specie' or 'best_of_each_specie'
                    -Penalization_method(int):
                        1.without penalization
                        2.penalization fitness sharing
                        3.new penalization
                    -ShareFitness(str): 'yes' or 'no'
    :param stats: A :class:`~deap.tools.Statistics` object that is updated
                  inplace, optional.
    :param halloffame: A :class:`~deap.tools.HallOfFame` object that will
                       contain the best individuals, optional.
    :param verbose: Whether or not to log the statistics.
    :returns: The final population.

    It uses :math:`\lambda = \kappa = \mu` and goes as follow.
    It first initializes the population (:math:`P(0)`) by evaluating
    every individual presenting an invalid fitness. Then, it enters the
    evolution loop that begins by the selection of the :math:`P(g+1)`
    population. Then the crossover operator is applied on a proportion of
    :math:`P(g+1)` according to the *cxpb* probability, the resulting and the
    untouched individuals are placed in :math:`P'(g+1)`. Thereafter, a
    proportion of :math:`P'(g+1)`, determined by *mutpb*, is
    mutated and placed in :math:`P''(g+1)`, the untouched individuals are
    transferred :math:`P''(g+1)`. Finally, those new individuals are evaluated
    and the evolution loop continues until *ngen* generations are completed.
    Briefly, the operators are applied in the following order ::

        evaluate(population)
        for i in range(ngen):
            offspring = select(population)
            offspring = mate(offspring)
            offspring = mutate(offspring)
            evaluate(offspring)
            population = offspring

    This function expects :meth:`toolbox.mate`, :meth:`toolbox.mutate`,
    :meth:`toolbox.select` and :meth:`toolbox.evaluate` aliases to be
    registered in the toolbox.

    .. [Back2000] Back, Fogel and Michalewicz, "Evolutionary Computation 1 :
       Basic Algorithms and Operators", 2000.
    """

    logbook = tools.Logbook()
    logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])
    pop_file = open('./Results/%s/pop_file_%d_%d.txt' % (problem, num_p, n_corr), 'a')

    if SaveMatrix:  # Saving data in matrix
        num_r = 9
        if GenMatrix:
            num_salto=1
            num_c=ngen+1
            Matrix= np.empty((num_c, num_r,))
            vector = np.arange(0, num_c, num_salto)
        else:
            num_c = (cont_evalf/num_salto) + 1
            Matrix = np.empty((num_c, num_r,))
            vector = np.arange(1, cont_evalf+num_salto, num_salto)
        for i in range(len(vector)):
            Matrix[i, 0] = vector[i]
            #num_r-1
        Matrix[:, 6] = 0.

    #Creation of the species
    # if neat_alg:
    #     species(population,neat_h)
    #     ind_specie(population)

    if funcEval.LS_flag:
        for ind in population:
            sizep = len(ind)+2
            param_ones = np.ones(sizep)
            param_ones[0] = 0
            ind.params_set(param_ones)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in population if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        funcEval.cont_evalp += 1
        ind.fitness.values = fit

    best = open('./Results/%s/bestind_%d_%d.txt' % (problem, num_p, n_corr), 'a')  # save data

    best_ind = best_pop(population)  # best individual of the population
    fitnesst_best = toolbox.map(toolbox.evaluate_test, [best_ind])
    best_ind.fitness_test.values = fitnesst_best[0]
    best.write('\n%s;%s;%s;%s;%s;%s' % (0, funcEval.cont_evalp, best_ind.fitness_test.values[0], best_ind.fitness.values[0], len(best_ind), avg_nodes(population)))
    data_pop=avg_nodes(population)
    if SaveMatrix:
        idx = 0
        Matrix[idx, 1] = best_ind.fitness.values[0]
        Matrix[idx, 2] = best_ind.fitness_test.values[0]
        Matrix[idx, 3] = len(best_ind)
        Matrix[idx, 4] = data_pop[0]
        Matrix[idx, 5] = 0.
        Matrix[idx, 6] = 1  # just an id to know if the current row is full
        Matrix[idx, 7] = data_pop[1]  # max size
        Matrix[idx, 8] = data_pop[2]  # min size

        np.savetxt('./Matrix/%s/idx_%d_%d.txt' % (problem,num_p, n_corr), Matrix, delimiter=",", fmt="%s")

    if neat_alg:
        SpeciesPunishment(population,params,neat_h)

    out = open('./Results/%s/bestind_str_%d_%d.txt' % (problem, num_p, n_corr), 'a')

    if funcEval.LS_flag == 1:
        strg = best_ind.__str__()
        l_strg = add_subt_cf(strg, args=[])
        c = tree2f()
        cd = c.convert(l_strg)
        out.write('\n%s;%s;%s;%s;%s;%s' % (0, len(best_ind), best_ind.LS_applied_get(), best_ind.get_params(), cd, best_ind))
    else:
        out.write('\n%s;%s;%s' % (0, len(best_ind), best_ind))

    for ind in population:
        pop_file.write('\n%s;%s'%(ind.fitness.values[0], ind))

    ls_type = ''
    if LS_select == 1:
        ls_type = 'LSHS'
    elif LS_select == 2:
        ls_type = 'Best-Sp'
    elif LS_select == 3:
        ls_type = 'LSHS-Sp'
    elif LS_select == 4:
        ls_type = 'Best-Pop'
    elif LS_select == 5:
        ls_type = 'All-Pop'
    elif LS_select == 6:
        ls_type = 'LSHS-test'
    elif LS_select == 7:
        ls_type = 'Best set'
    elif LS_select == 8:
        ls_type = 'Random set'
    elif LS_select == 9:
        ls_type = "Best-Random set"

    print '---- Generation %d -----' % (0)
    print 'Problem: ', problem
    print 'Problem No.: ', num_p
    print 'Run No.: ', n_corr
    print 'neat-GP:', neat_alg
    print 'neat-cx:', neat_cx
    print 'Local Search:', funcEval.LS_flag
    if funcEval.LS_flag:
        print 'Local Search Heuristic: %s (%s)' % (LS_select,ls_type)
    print 'Best Ind.:', best_ind
    print 'Best Fitness:', best_ind.fitness.values[0]
    print 'Test fitness:',best_ind.fitness_test.values[0]
    print 'Avg Nodes:', avg_nodes(population)
    print 'Evaluations: ', funcEval.cont_evalp

    # Begin the generational process
    for gen in range(1, ngen+1):

        if funcEval.cont_evalp > cont_evalf:
            break

        print '---- Generation %d -----' % (gen)
        print 'Problem: ', problem
        print 'Problem No.: ', num_p
        print 'Run No.: ', n_corr
        print 'neat-GP:', neat_alg
        print 'neat-cx:', neat_cx
        print 'Local Search:', funcEval.LS_flag
        if funcEval.LS_flag:
            print 'Local Search Heuristic: %s (%s)' % (LS_select, ls_type)

        best_ind = copy.deepcopy(best_pop(population))
        if neat_alg:
            parents = p_selection(population, gen)
        else:
            parents = toolbox.select(population, len(population))

        if neat_cx:
            n = len(parents)
            mut = 1
            cx = 1
            offspring = neatGP(toolbox, parents, cxpb, mutpb, n, mut, cx, neat_pelit)
        else:
            offspring = varOr(parents, toolbox, cxpb, mutpb)

        if neat_alg:
            specie_parents_child(parents,offspring, neat_h)
            offspring[:] = parents+offspring
            ind_specie(offspring)
            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
            for ind, fit in zip(invalid_ind, fitnesses):
                funcEval.cont_evalp += 1
                ind.fitness.values = fit
        else:
            invalid_ind = [ind for ind in offspring]
            if funcEval.LS_flag:
                new_invalid_ind = []
                for ind in invalid_ind:
                    strg = ind.__str__()
                    l_strg = add_subt(strg, ind)
                    c = tree2f()
                    cd = c.convert(l_strg)
                    new_invalid_ind.append(cd)
                fitness_ls = toolbox.map(toolbox.evaluate, new_invalid_ind)
                for ind, ls_fit in zip(invalid_ind, fitness_ls):
                    funcEval.cont_evalp += 1
                    ind.fitness.values = ls_fit
            else:
                fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
                for ind, fit in zip(invalid_ind, fitnesses):
                    funcEval.cont_evalp += 1
                    ind.fitness.values = fit

            orderbyfit = sorted(offspring, key=lambda ind:ind.fitness.values)
            print len(orderbyfit),len(best_ind)

            if best_ind.fitness.values[0] <= orderbyfit[0].fitness.values[0]:
                offspring[:] = [best_ind]+orderbyfit[:len(population)-1]

        if neat_alg:
            SpeciesPunishment(offspring, params, neat_h)

        population[:] = offspring  # population update

        cond_ind = 0
        cont_better=0
        if funcEval.LS_flag:
            for ind in population:
                ind.LS_applied_set(0)

            if   LS_select == 1:
                trees_h(population, num_p, n_corr,  pset, direccion)
            elif LS_select == 2:
                best_specie(population, num_p, n_corr, pset, direccion)
            elif LS_select == 3:
                specie_h(population, num_p, n_corr, pset, direccion)
            elif LS_select == 4:
                best_pop_ls(population, num_p, n_corr, pset, direccion)
            elif LS_select == 5:
                all_pop(population, num_p, n_corr, pset, direccion)
            elif LS_select == 6:
                trees_h_wo(population, num_p, n_corr, pset, direccion)
            elif LS_select == 7:
                ls_bestset(population, num_p, n_corr, pset, direccion)
            elif LS_select == 8:
                ls_random(population, num_p, n_corr, pset, direccion)
            elif LS_select == 9:
                ls_randbestset(population, num_p, n_corr, pset, direccion)
            #
            invalid_ind = [ind for ind in population]
            new_invalid_ind = []
            for ind in population:
                strg = ind.__str__()
                l_strg = add_subt(strg, ind)
                c = tree2f()
                cd = c.convert(l_strg)
                new_invalid_ind.append(cd)
            fitness_ls = toolbox.map(toolbox.evaluate, new_invalid_ind)
            print 'Fitness comp.:',
            for ind, ls_fit in zip(invalid_ind, fitness_ls):
                if ind.LS_applied_get() == 1:
                    cond_ind += 1
                    if ind.fitness.values[0] < ls_fit:
                        print '-',
                    elif ind.fitness.values[0] > ls_fit:
                        cont_better += 1
                        print '+',
                    elif ind.fitness.values[0] == ls_fit:
                        print '=',
                funcEval.cont_evalp += 1
                ind.fitness.values = ls_fit
            print ''

            pop_file.write('\n----------------------------------------%s'%(gen))
            for ind in population:
                pop_file.write('\n%s;%s;%s;%s'%(ind.LS_applied_get(),ind.fitness.values[0], ind, [x for x in ind.get_params()]))
        else:
            pop_file.write('\n----------------------------------------')
            for ind in population:
                pop_file.write('\n%s;%s;%s;%s;%s;%s'%(ind.LS_applied_get(),ind.LS_story_get(),ind.off_cx_get(),ind.off_mut_get(),ind.fitness.values[0], ind))

        best_ind = best_pop(population)
        if funcEval.LS_flag:
            strg = best_ind.__str__()
            l_strg = add_subt(strg, best_ind)
            c = tree2f()
            cd = c.convert(l_strg)
            new_invalid_ind.append(cd)
            fit_best = toolbox.map(toolbox.evaluate_test, [cd])
            best_ind.fitness_test.values = fit_best[0]
            best.write('\n%s;%s;%s;%s;%s;%s;%s' % (gen, funcEval.cont_evalp,  best_ind.fitness.values[0], best_ind.LS_fitness_get(), best_ind.fitness_test.values[0], len(best_ind), avg_nodes(population)))
            out.write('\n%s;%s;%s;%s;%s;%s' % (gen, len(best_ind), best_ind.LS_applied_get(), best_ind.get_params(), cd, best_ind))
        else:
            fitnesses_test = toolbox.map(toolbox.evaluate_test, [best_ind])
            best_ind.fitness_test.values = fitnesses_test[0]
            best.write('\n%s;%s;%s;%s;%s;%s' % (gen, funcEval.cont_evalp, best_ind.fitness_test.values[0], best_ind.fitness.values[0], len(best_ind), avg_nodes(population)))
            out.write('\n%s;%s;%s' % (gen, len(best_ind), best_ind))

        if funcEval.LS_flag:
            print 'Num. LS:', cond_ind
            print 'Ind. Improvement:', cont_better
            print 'Best Ind. LS:', best_ind.LS_applied_get()

        print 'Best Ind.:', best_ind
        print 'Best Fitness:', best_ind.fitness.values[0]
        print 'Test fitness:',best_ind.fitness_test.values[0]
        print 'Avg Nodes:', avg_nodes(population)
        print 'Evaluations: ', funcEval.cont_evalp

        if SaveMatrix:
            data_pop=avg_nodes(population)
            if GenMatrix:
                idx_aux = np.searchsorted(Matrix[:, 0], gen)
                Matrix[idx_aux, 1] = best_ind.fitness.values[0]
                Matrix[idx_aux, 2] = best_ind.fitness_test.values[0]
                Matrix[idx_aux, 3] = len(best_ind)
                Matrix[idx_aux, 4] = data_pop[0]
                Matrix[idx_aux, 5] = gen
                Matrix[idx_aux, 6] = 1
                Matrix[idx_aux, 7] = data_pop[1]  # max nodes
                Matrix[idx_aux, 8] = data_pop[2]  # min nodes
            else:
                if funcEval.cont_evalp >= cont_evalf:
                    num_c -= 1
                    idx_aux=num_c
                    Matrix[num_c, 1] = best_ind.fitness.values[0]
                    Matrix[num_c, 2] = best_ind.fitness_test.values[0]
                    Matrix[num_c, 3] = len(best_ind)
                    Matrix[num_c, 4] = data_pop[0]
                    Matrix[num_c, 5] = gen
                    Matrix[num_c, 6] = 1
                    Matrix[num_c, 7] = data_pop[1]  #max_nodes
                    Matrix[num_c, 8] = data_pop[2]  #min nodes
                else:
                    idx_aux = np.searchsorted(Matrix[:, 0], funcEval.cont_evalp)
                    Matrix[idx_aux, 1] = best_ind.fitness.values[0]
                    Matrix[idx_aux, 2] = best_ind.fitness_test.values[0]
                    Matrix[idx_aux, 3] = len(best_ind)
                    Matrix[idx_aux, 4] = data_pop[0]
                    Matrix[idx_aux, 5] = gen
                    Matrix[idx_aux, 6] = 1
                    Matrix[idx_aux, 7] = data_pop[1]  #max nodes
                    Matrix[idx_aux, 8] = data_pop[2]  #min nodes

                id_it = idx_aux-1
                id_beg = 0
                flag = True
                flag2 = False
                while flag:
                    if Matrix[id_it, 6] == 0:
                        id_it -= 1
                        flag2 = True
                    else:
                        id_beg = id_it
                        flag = False
                if flag2:
                    x = Matrix[id_beg, 1:8]
                    Matrix[id_beg:idx_aux, 1:] = Matrix[id_beg, 1:]

            np.savetxt('./Matrix/%s/idx_%d_%d.txt' % (problem, num_p, n_corr), Matrix, delimiter=",", fmt="%s")

    return population, logbook
Exemplo n.º 4
0
def neat_GP_LS(population, toolbox, cxpb, mutpb, ngen, neat_alg, neat_cx, neat_h,neat_pelit, LS_flag, LS_select, cont_evalf,
               num_salto, SaveMatrix, GenMatrix, pset,n_corr, num_p, params, direccion, problem, testing, version,
               stats=None, halloffame=None, verbose=__debug__):
    """This algorithm reproduce the simplest evolutionary algorithm as
    presented in chapter 7 of [Back2000]_.

    :param population: A list of individuals.
    :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution
                    operators.
    :param cxpb: The probability of mating two individuals.
    :param mutpb: The probability of mutating an individual.
    :param ngen: The number of generation.
    :param neat_alg: wheter or not to use species stuff.
    :param neat_cx: wheter or not to use neat-crossover
    :param neat_h: indicate the distance allowed between each specie
    :param neat_pelit: probability of being elitist, it's used in the neat cx and mutation
    :param LS_flag: wheter or not to use GP-LS
    :param LS_select: indicate the heuristic to select individuals for the LS method.
            LS_select == 1:'LSHS'            -- Heurisitic proposed by Z-Flores (2015)
            LS_select == 2:'Best-Sp'         -- Best individual of the specie
            LS_select == 3:'LSHS-Sp'         -- Heuristic 1 in each specie
            LS_select == 4:'Best-Pop'        -- Best ind of the population
            LS_select == 5:'All-Pop'         -- All population
            LS_select == 6:'LSHS-test'       -- Heuristic 1 only in the test data
            LS_select == 7:'Best set'        -- Best individuals set
            LS_select == 8:'Random set'      -- Random individuals set
            LS_select == 9:'Best-Random set' -- Best individual set and then random selection of this individuals
    :param cont_evalf: Counter of the function evaluations
    :param num_salto: This is a parameter to save data in the Matrix, indicates the range for each row
    :param SaveMatrix: wheter or not to save data on a Matrix
    :param GenMatrix: wheter or not to save data in the Matrix on a generations form or function evals form
    :param pset: this is the primitive set
    :param n_corr: run number just to write the txt file
    :param num_p: problem number just to write the txt file
    :param params:indicate the params for the fitness sharing, the different
                    options are:
                    -DontPenalize(str): 'best_specie' or 'best_of_each_specie'
                    -Penalization_method(int):
                        1.without penalization
                        2.penalization fitness sharing
                        3.new penalization
                    -ShareFitness(str): 'yes' or 'no'
    :param stats: A :class:`~deap.tools.Statistics` object that is updated
                  inplace, optional.
    :param direccion: Path to find the training data. This parameter works in the g_address.py file.
    :param problem: Name of the problem. Save and Get data.
    :param testing: Wheter or not to use testing data.
    :param version: Number of the version to make the speciation.
    :param halloffame: A :class:`~deap.tools.HallOfFame` object that will
                       contain the best individuals, optional.
    :param verbose: Whether or not to log the statistics.
    :returns: The final population.

    It uses :math:`\lambda = \kappa = \mu` and goes as follow.
    It first initializes the population (:math:`P(0)`) by evaluating
    every individual presenting an invalid fitness. Then, it enters the
    evolution loop that begins by the selection of the :math:`P(g+1)`
    population. Then the crossover operator is applied on a proportion of
    :math:`P(g+1)` according to the *cxpb* probability, the resulting and the
    untouched individuals are placed in :math:`P'(g+1)`. Thereafter, a
    proportion of :math:`P'(g+1)`, determined by *mutpb*, is
    mutated and placed in :math:`P''(g+1)`, the untouched individuals are
    transferred :math:`P''(g+1)`. Finally, those new individuals are evaluated
    and the evolution loop continues until *ngen* generations are completed.
    Briefly, the operators are applied in the following order ::

        evaluate(population)
        for i in range(ngen):
            offspring = select(population)
            offspring = mate(offspring)
            offspring = mutate(offspring)
            evaluate(offspring)
            population = offspring

    This function expects :meth:`toolbox.mate`, :meth:`toolbox.mutate`,
    :meth:`toolbox.select` and :meth:`toolbox.evaluate` aliases to be
    registered in the toolbox.

    .. [Back2000] Back, Fogel and Michalewicz, "Evolutionary Computation 1 :
       Basic Algorithms and Operators", 2000.
    """

    logbook = tools.Logbook()
    logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])

    # creating files to save data.
    d = './Results/%s/pop_file_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    pop_file = open(d, 'a')

    d = './Results/%s/bestind_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    best = open(d, 'w')

    d = './Results/%s/bestind_str_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    out = open(d, 'w')

    d='./Timing/%s/pop_file_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    time_file = open(d, 'w')

    d = './Timing/%s/specie_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    time_specie = open(d, 'w')

    d = './Timing/%s/cruce_s_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    time_cx = open(d, 'w')

    d = './Specie/%s/specieind_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    specie_file = open(d, 'w')

    d = './Specie/%s/specist_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    specie_statis = open(d, 'w')

    d = './Results/%s/bestind_LStr_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    bestind = open(d, 'w')

    begin = time.time()

    if SaveMatrix:  # Saving data in matrix
        num_r = 11
        if GenMatrix:  # To save the data in matrix by generations
            num_salto = 1
            num_c = ngen+1
            Matrix = np.empty((num_c, num_r,))
            vector = np.arange(0, num_c, num_salto)
        else:  # To save the data in matrix by function evaluations
            num_c = (cont_evalf/num_salto) + 1
            Matrix = np.empty((num_c, num_r,))
            vector = np.arange(1, cont_evalf+num_salto, num_salto)
        for i in range(len(vector)):
            Matrix[i, 0] = vector[i]
        Matrix[:, 6] = 0.

    begin_sp = time.time()  # Saving data: speciaton init timing
    if neat_alg:  # before version 3
        for ind in population:
            level_info = level_node(ind)
            ind.nodefeat_set(level_info)

        species(population, neat_h, version)

    end_sp = time.time() # Saving data: speciaton end timing
    time_specie.write('\n%s;%s;%s;%s' % (0, begin_sp, end_sp, str(round(end_sp - begin_sp, 2))))  # Saving data in file

    specie_file.write('\n-------')
    for ind in population:
        specie_file.write('\n%s;%s;%s' % (ind.get_specie(),ind,version))

    # Adding parameters for the LS algorithms
    if funcEval.LS_flag:
        for ind in population:
            sizep = len(ind)+2
            param_ones = np.ones(sizep)
            param_ones[0] = 0
            ind.params_set(param_ones)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in population if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        funcEval.cont_evalp += 1
        ind.fitness.values = fit

    best_ind = best_pop(population)  # best individual of the population

    if testing:
        fitnesst_best = toolbox.map(toolbox.evaluate_test, [best_ind])
        best_ind.fitness_test.values = fitnesst_best[0]

        best.write('\n%s;%s;%s;%s;%s;%s' % (0, funcEval.cont_evalp, best_ind.fitness_test.values[0], best_ind.fitness.values[0], len(best_ind), avg_nodes(population)))
    else:
        best.write('\n%s;%s;%s;%s;%s;%s' % (
        0, funcEval.cont_evalp, None, best_ind.fitness.values[0], len(best_ind),
        avg_nodes(population)))

    data_pop = avg_nodes(population)

    if SaveMatrix:
        idx = 0
        Matrix[idx, 1] = best_ind.fitness.values[0]                           # best ind train data
        if testing:
            Matrix[idx, 2] = best_ind.fitness_test.values[0]                  # best ind test data
        else:
            Matrix[idx, 2] = None
        Matrix[idx, 3] = len(best_ind)                                        # best individual size (number of nodes)
        Matrix[idx, 4] = data_pop[0]                                          # avg size of the population
        Matrix[idx, 5] = 0.
        Matrix[idx, 6] = 1                                                    # id to know if the current row is full
        Matrix[idx, 7] = data_pop[1]                                          # max size of the population
        Matrix[idx, 8] = data_pop[2]                                          # min size of the population
        Matrix[idx, 9] = best_ind.get_specie()                                # specie of the best individual
        Matrix[idx, 10] = max(ind_specie(population), key=lambda x: x[0])[0]  # max number of species on current pop

        np.savetxt('./Matrix/%s/idx_%d_%d.txt' % (problem,num_p, n_corr), Matrix, delimiter=",", fmt="%s")

    if neat_alg:
        SpeciesPunishment(population,params,neat_h)

    if funcEval.LS_flag == 1:
        strg = best_ind.__str__()
        l_strg = add_subt_cf(strg, args=[])
        c = tree2f()
        cd = c.convert(l_strg)
        out.write('\n%s;%s;%s;%s;%s;%s' % (0, len(best_ind), best_ind.LS_applied_get(), best_ind.get_params(), cd, best_ind))
    else:
        out.write('\n%s;%s;%s' % (0, len(best_ind), best_ind))

    for ind in population:
        pop_file.write('\n%s;%s'%(ind.fitness.values[0], ind))

    ls_type = ''
    if LS_select == 1:
        ls_type = 'LSHS'
    elif LS_select == 2:
        ls_type = 'Best-Sp'
    elif LS_select == 3:
        ls_type = 'LSHS-Sp'
    elif LS_select == 4:
        ls_type = 'Best-Pop'
    elif LS_select == 5:
        ls_type = 'All-Pop'
    elif LS_select == 6:
        ls_type = 'LSHS-test'
    elif LS_select == 7:
        ls_type = 'Best set'
    elif LS_select == 8:
        ls_type = 'Random set'
    elif LS_select == 9:
        ls_type = 'Best-Random set'

    ################################
    # Printing data
    ################################
    print '---- Generation %d -----' % (0)
    print 'Problem: ', problem
    print 'Problem No.: ', num_p
    print 'Run No.: ', n_corr
    print 'neat-GP:', neat_alg
    print 'neat-cx:', neat_cx
    print 'Local Search:', funcEval.LS_flag
    if funcEval.LS_flag:
        print 'Local Search Heuristic: %s (%s)' % (LS_select,ls_type)
    print 'Best Ind.:', best_ind
    print 'Best Fitness:', best_ind.fitness.values[0]
    if testing:
        print 'Test fitness:', best_ind.fitness_test.values[0]
    print 'Avg Nodes:', avg_nodes(population)
    print 'Evaluations: ', funcEval.cont_evalp
    ##################################

    end_t = time.time()

    specie_statis.write('\n%s;%s' % (0, count_species(population)))

    if testing:
        time_file.write('\n%s;%s;%s;%s;%s;%s' % (0, begin,end_t, str(round(end_t - begin, 2)), best_ind.fitness.values[0], best_ind.fitness_test.values[0]))
    else:
        time_file.write('\n%s;%s;%s;%s;%s' % (
        0, begin, end_t, str(round(end_t - begin, 2)), best_ind.fitness.values[0]))

    # Begin the generational process
    for gen in range(1, ngen+1):

        if not GenMatrix:
            if funcEval.cont_evalp > cont_evalf:
                break

        begin = time.time()
        print '---- Generation %d -----' % (gen)
        print 'Problem: ', problem
        print 'Problem No.: ', num_p
        print 'Run No.: ', n_corr
        print 'neat-GP:', neat_alg
        print 'neat-cx:', neat_cx
        print 'Local Search:', funcEval.LS_flag
        if funcEval.LS_flag:
            print 'Local Search Heuristic: %s (%s)' % (LS_select, ls_type)

        best_ind = copy.deepcopy(best_pop(population))

        if neat_alg:
            parents = p_selection(population, neat_pelit)
        else:
            parents = toolbox.select(population, len(population))

        begin_cx = time.time()
        if neat_cx and neat_alg:  # neat-Crossover
            n = len(parents)
            mut = 1
            cx = 1
            offspring = neatGP(toolbox, parents, cxpb, mutpb, n, mut, cx, neat_pelit)
        else:
            offspring = varOr(parents, toolbox, cxpb, mutpb)
        end_cx = time.time()
        time_specie.write('\n%s;%s;%s;%s' % (0, begin_cx, end_cx, str(round(end_sp - begin_sp, 2))))

        if neat_alg:  # Speciation of the descendants and fitness evaluation
            begin_sp = time.time()
            specie_parents_child(parents,offspring, neat_h, version)
            offspring[:] = parents+offspring

            invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)

            for ind, fit in zip(invalid_ind, fitnesses):
                funcEval.cont_evalp += 1
                ind.fitness.values = fit

            end_sp = time.time()
            time_specie.write('\n%s;%s;%s;%s' % (gen, begin_sp, end_sp, str(round(end_sp - begin_sp, 2))))

        else:  # evaluation of descendants
            invalid_ind = [ind for ind in offspring]
            if funcEval.LS_flag:  # evaluating with the parameters set
                new_invalid_ind = []
                for ind in invalid_ind:
                    strg = ind.__str__()
                    l_strg = add_subt(strg, ind)
                    c = tree2f()
                    cd = c.convert(l_strg)
                    new_invalid_ind.append(cd)
                fitness_ls = toolbox.map(toolbox.evaluate, new_invalid_ind)
                for ind, ls_fit in zip(invalid_ind, fitness_ls):
                    funcEval.cont_evalp += 1
                    ind.fitness.values = ls_fit
            else:  # normal evaluation
                fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
                for ind, fit in zip(invalid_ind, fitnesses):
                    funcEval.cont_evalp += 1
                    ind.fitness.values = fit

            orderbyfit = sorted(offspring, key=lambda ind:ind.fitness.values)
            #print len(orderbyfit),len(best_ind)

            # Applying elitism: Best individual survives
            if best_ind.fitness.values[0] <= orderbyfit[0].fitness.values[0]:
                offspring[:] = [best_ind]+orderbyfit[:len(population)-1]

        if neat_alg:
            SpeciesPunishment(offspring, params, neat_h)

        specie_statis.write('\n%s;%s' % (gen, ind_specie(offspring)))
        population[:] = offspring  # population update

        specie_file.write('\n%s--------------------------------' % gen)
        if neat_alg:
            for ind in population:
                specie_file.write('\n%s;%s;%s' % (ind.get_specie(), ind, version))

        cond_ind = 0
        cont_better = 0

        if funcEval.LS_flag:
            for ind in population:
                ind.LS_applied_set(0)

            if   LS_select == 1:
                trees_h(population, num_p, n_corr,  pset, direccion, problem)
            elif LS_select == 2:
                best_specie(population, num_p, n_corr, pset, direccion, problem)
            elif LS_select == 3:
                specie_h(population, num_p, n_corr, pset, direccion, problem)
            elif LS_select == 4:
                best_pop_ls(population, num_p, n_corr, pset, direccion, problem)
            elif LS_select == 5:
                all_pop(population, num_p, n_corr, pset, direccion, problem)
            elif LS_select == 6:
                trees_h_wo(population, num_p, n_corr, pset, direccion, problem)
            elif LS_select == 7:
                ls_bestset(population, num_p, n_corr, pset, direccion, problem)
            elif LS_select == 8:
                ls_random(population, num_p, n_corr, pset, direccion, problem)
            elif LS_select == 9:
                ls_randbestset(population, num_p, n_corr, pset, direccion, problem)


            invalid_ind = [ind for ind in population]
            new_invalid_ind = []
            for ind in population:
                strg = ind.__str__()
                l_strg = add_subt(strg, ind)
                c = tree2f()
                cd = c.convert(l_strg)
                new_invalid_ind.append(cd)
            fitness_ls = toolbox.map(toolbox.evaluate, new_invalid_ind)
            print 'Fitness comp.:',

            # Information about how goes the optimization
            for ind, ls_fit in zip(invalid_ind, fitness_ls):
                if ind.LS_applied_get() == 1:
                    cond_ind += 1
                    if ind.fitness.values[0] < ls_fit:
                        print '-',
                    elif ind.fitness.values[0] > ls_fit:
                        cont_better += 1
                        print '+',
                    elif ind.fitness.values[0] == ls_fit:
                        print '=',
                funcEval.cont_evalp += 1
                ind.fitness.values = ls_fit
            print ''

            pop_file.write('\n----------------------------------------%s'%(gen))
            for ind in population:
                pop_file.write('\n%s;%s;%s;%s'%(ind.LS_applied_get(),ind.fitness.values[0], ind, [x for x in ind.get_params()]))
        else:
            pop_file.write('\n----------------------------------------')
            for ind in population:
                pop_file.write('\n%s;%s;%s;%s;%s;%s'%(ind.LS_applied_get(),ind.LS_story_get(),ind.off_cx_get(),ind.off_mut_get(),ind.fitness.values[0], ind))

        best_ind = best_pop(population)
        if funcEval.LS_flag:
            strg = best_ind.__str__()
            l_strg = add_subt(strg, best_ind)
            c = tree2f()
            cd = c.convert(l_strg)
            new_invalid_ind.append(cd)
            bestind.write('\n%s;%s;%s' % (gen, best_ind.fitness.values[0],cd))
            if testing:
                fit_best = toolbox.map(toolbox.evaluate_test, [cd])
                best_ind.fitness_test.values = fit_best[0]

                best.write('\n%s;%s;%s;%s;%s;%s;%s' % (gen, funcEval.cont_evalp,  best_ind.fitness.values[0], best_ind.LS_fitness_get(), best_ind.fitness_test.values[0], len(best_ind), avg_nodes(population)))
            else:
                best.write('\n%s;%s;%s;%s;%s;%s;%s' % (
                gen, funcEval.cont_evalp, best_ind.fitness.values[0], best_ind.LS_fitness_get(),
                None, len(best_ind), avg_nodes(population)))
            out.write('\n%s;%s;%s;%s;%s;%s' % (gen, len(best_ind), best_ind.LS_applied_get(), best_ind.get_params(), cd, best_ind))

        else:
            if testing:
                fitnesses_test = toolbox.map(toolbox.evaluate_test, [best_ind])
                best_ind.fitness_test.values = fitnesses_test[0]
                best.write('\n%s;%s;%s;%s;%s;%s' % (gen, funcEval.cont_evalp, best_ind.fitness_test.values[0], best_ind.fitness.values[0], len(best_ind), avg_nodes(population)))
            else:
                best.write('\n%s;%s;%s;%s;%s;%s' % (
                gen, funcEval.cont_evalp, None, best_ind.fitness.values[0], len(best_ind),
                avg_nodes(population)))

            bestind.write('\n%s;%s;%s' % (gen, best_ind.fitness.values[0], best_ind))
            out.write('\n%s;%s;%s' % (gen, len(best_ind), best_ind))

        if funcEval.LS_flag:
            print 'Num. LS:', cond_ind
            print 'Ind. Improvement:', cont_better
            print 'Best Ind. LS:', best_ind.LS_applied_get()

        print 'Best Ind.:', best_ind
        print 'Best Fitness:', best_ind.fitness.values[0]
        if testing:
            print 'Test fitness:',best_ind.fitness_test.values[0]
        print 'Avg Nodes:', avg_nodes(population)
        print 'Evaluations: ', funcEval.cont_evalp

        if SaveMatrix:
            data_pop=avg_nodes(population)
            if GenMatrix:
                idx_aux = np.searchsorted(Matrix[:, 0], gen)
                Matrix[idx_aux, 1] = best_ind.fitness.values[0]
                if testing:
                    Matrix[idx_aux, 2] = best_ind.fitness_test.values[0]
                else:
                    Matrix[idx_aux, 2] = None
                Matrix[idx_aux, 3] = len(best_ind)
                Matrix[idx_aux, 4] = data_pop[0]
                Matrix[idx_aux, 5] = gen
                Matrix[idx_aux, 6] = 1
                Matrix[idx_aux, 7] = data_pop[1]  # max nodes
                Matrix[idx_aux, 8] = data_pop[2]  # min nodes
                if neat_alg:
                    Matrix[idx_aux, 9] = best_ind.get_specie()
                    Matrix[idx_aux, 10] = max(ind_specie(population), key=lambda x: x[0])[0]
            else:
                if funcEval.cont_evalp >= cont_evalf:
                    num_c -= 1
                    idx_aux = num_c
                    Matrix[num_c, 1] = best_ind.fitness.values[0]
                    if testing:
                        Matrix[num_c, 2] = best_ind.fitness_test.values[0]
                    else:
                        Matrix[num_c, 2] = None
                    Matrix[num_c, 3] = len(best_ind)
                    Matrix[num_c, 4] = data_pop[0]
                    Matrix[num_c, 5] = gen
                    Matrix[num_c, 6] = 1
                    Matrix[num_c, 7] = data_pop[1]  # max_nodes
                    Matrix[num_c, 8] = data_pop[2]  # min nodes
                    if neat_alg:
                        Matrix[num_c, 9] = max(ind_specie(population), key=lambda x: x[0])[0]

                else:
                    idx_aux = np.searchsorted(Matrix[:, 0], funcEval.cont_evalp)
                    Matrix[idx_aux, 1] = best_ind.fitness.values[0]
                    if testing:
                        Matrix[idx_aux, 2] = best_ind.fitness_test.values[0]
                    else:
                        Matrix[idx_aux, 2] = None
                    Matrix[idx_aux, 3] = len(best_ind)
                    Matrix[idx_aux, 4] = data_pop[0]
                    Matrix[idx_aux, 5] = gen
                    Matrix[idx_aux, 6] = 1
                    Matrix[idx_aux, 7] = data_pop[1]  # max nodes
                    Matrix[idx_aux, 8] = data_pop[2]  # min nodes
                    if neat_alg:
                        Matrix[idx_aux, 9] = max(ind_specie(population), key=lambda x: x[0])[0]

                id_it = idx_aux-1
                id_beg = 0
                flag = True
                flag2 = False
                while flag:
                    if Matrix[id_it, 6] == 0:
                        id_it -= 1
                        flag2 = True
                    else:
                        id_beg = id_it
                        flag = False
                if flag2:
                    x = Matrix[id_beg, 1:8]
                    Matrix[id_beg:idx_aux, 1:] = Matrix[id_beg, 1:]

            np.savetxt('./Matrix/%s/idx_%d_%d.txt' % (problem, num_p, n_corr), Matrix, delimiter=",", fmt="%s")

        end_t = time.time()
        if testing:
            time_file.write('\n%s;%s;%s;%s;%s;%s' % (gen, begin, end_t, str(round(end_t - begin, 2)), best_ind.fitness.values[0], best_ind.fitness_test.values[0]))
        else:
            time_file.write('\n%s;%s;%s;%s;%s' % (
            gen, begin, end_t, str(round(end_t - begin, 2)), best_ind.fitness.values[0]))
    return population, logbook
Exemplo n.º 5
0
def neat_GP(population,
            toolbox,
            cxpb,
            mutpb,
            ngen,
            neat_alg,
            neat_cx,
            neat_h,
            neat_pelit,
            n_corr,
            num_p,
            params,
            problem,
            beta,
            stats=None,
            halloffame=None,
            verbose=__debug__):
    """This algorithm reproduce the simplest evolutionary algorithm as
    presented in chapter 7 of [Back2000]_.

    :param population: A list of individuals.
    :param toolbox: A :class:`~deap.base.Toolbox` that contains the evolution
                    operators.
    :param cxpb: The probability of mating two individuals.
    :param mutpb: The probability of mutating an individual.
    :param ngen: The number of generation.
    :param neat_alg: wheter or not to use species stuff.
    :param neat_cx: wheter or not to use neatGP cx
    :param neat_h: indicate the distance allowed between each specie
    :param neat_pelit: probability of being elitist, it's used in the neat cx and mutation
    :param n_corr: run number just to wirte the txt file
    :param num_p: problem number just to wirte the txt file
    :param params:indicate the params for the fitness sharing, the diffetent
                    options are:
                    -DontPenalize(str): 'best_specie' or 'best_of_each_specie'
                    -Penalization_method(int):
                        1.without penalization
                        2.penalization fitness sharing
                        3.new penalization
                    -ShareFitness(str): 'yes' or 'no'
    :param problem: (str) name of the problem.
    :param stats: A :class:`~deap.tools.Statistics` object that is updated
                  inplace, optional.
    :param halloffame: A :class:`~deap.tools.HallOfFame` object that will
                       contain the best individuals, optional.
    :param verbose: Whether or not to log the statistics.
    :returns: The final population.

    It uses :math:`\lambda = \kappa = \mu` and goes as follow.
    It first initializes the population (:math:`P(0)`) by evaluating
    every individual presenting an invalid fitness. Then, it enters the
    evolution loop that begins by the selection of the :math:`P(g+1)`
    population. Then the crossover operator is applied on a proportion of
    :math:`P(g+1)` according to the *cxpb* probability, the resulting and the
    untouched individuals are placed in :math:`P'(g+1)`. Thereafter, a
    proportion of :math:`P'(g+1)`, determined by *mutpb*, is
    mutated and placed in :math:`P''(g+1)`, the untouched individuals are
    transferred :math:`P''(g+1)`. Finally, those new individuals are evaluated
    and the evolution loop continues until *ngen* generations are completed.
    Briefly, the operators are applied in the following order ::

        evaluate(population)
        for i in range(ngen):
            offspring = select(population)
            offspring = mate(offspring)
            offspring = mutate(offspring)
            evaluate(offspring)
            population = offspring

    This function expects :meth:`toolbox.mate`, :meth:`toolbox.mutate`,
    :meth:`toolbox.select` and :meth:`toolbox.evaluate` aliases to be
    registered in the toolbox.

    .. [Back2000] Back, Fogel and Michalewicz, "Evolutionary Computation 1 :
       Basic Algorithms and Operators", 2000.
    """
    d = './Results/%s/bestind_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    best = open(d, 'w')  # save data

    d = './Results/%s/bestind_string_%d_%d.txt' % (problem, num_p, n_corr)
    ensure_dir(d)
    best_st = open(d, 'w')

    logbook = tools.Logbook()
    logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])

    if neat_alg:  # assign specie to each individual on the population
        species(population, neat_h, beta)
        ind_specie(population)

    # Evaluate the individuals with an invalid fitness
    invalid_ind = [ind for ind in population if not ind.fitness.valid]
    fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
    for ind, fit in zip(invalid_ind, fitnesses):
        ind.fitness.values = fit

    #  take the best on the population
    best_ind = best_pop(population)
    fitnesst_best = toolbox.map(toolbox.evaluate_test, [best_ind])
    best_ind.fitness_test.values = fitnesst_best[0]
    best.write('\n%s;%s;%s;%s;%s' %
               (0, best_ind.fitness_test.values[0], best_ind.fitness.values[0],
                len(best_ind), avg_nodes(population)))
    best_st.write('\n%s;%s' % (0, best_ind))

    if neat_alg:  # applying fitness sharing
        SpeciesPunishment(population, params, neat_h)

    if halloffame is not None:
        halloffame.update(population)

    record = stats.compile(population) if stats else {}
    logbook.record(gen=0, nevals=len(invalid_ind), **record)
    if verbose:
        print(logbook.stream)

    # Begin the generational process
    for gen in range(1, ngen + 1):

        best_ind = copy.deepcopy(best_pop(population))
        if neat_alg:  # select set of parents
            parents = p_selection(population, gen)
        else:
            parents = toolbox.select(population, len(population))

        if neat_cx:  # applying neat-crossover
            n = len(parents)
            mut = 1
            cx = 1
            offspring = neatGP(toolbox, parents, cxpb, mutpb, n, mut, cx,
                               neat_pelit)
        else:
            offspring = varOr(parents, toolbox, cxpb, mutpb)

        if neat_alg:  # Assign species
            specie_parents_child(parents, offspring, neat_h, beta)
            offspring[:] = parents + offspring
            ind_specie(offspring)

            # Evaluate the individuals with an invalid fitness
            invalid_ind = [ind for ind in offspring]
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit
        else:
            invalid_ind = [ind for ind in offspring]
            fitnesses = toolbox.map(toolbox.evaluate, invalid_ind)
            for ind, fit in zip(invalid_ind, fitnesses):
                ind.fitness.values = fit

            orderbyfit = sorted(offspring, key=lambda ind: ind.fitness.values)

            if best_ind.fitness.values[0] <= orderbyfit[0].fitness.values[0]:
                offspring[:] = [best_ind] + orderbyfit[:len(population) - 1]

        if neat_alg:
            SpeciesPunishment(offspring, params, neat_h)

        # Update the hall of fame with the generated individuals
        if halloffame is not None:
            halloffame.update(offspring)

        # Replace the current population by the offspring
        population[:] = offspring

        # Append the current generation statistics to the logbook
        record = stats.compile(population) if stats else {}
        logbook.record(gen=gen, nevals=len(population), **record)
        if verbose:
            print(logbook.stream)

        best_ind = best_pop(population)
        fitnesses_test = toolbox.map(toolbox.evaluate_test, [best_ind])
        best_ind.fitness_test.values = fitnesses_test[0]
        best.write(
            '\n%s;%s;%s;%s;%s' %
            (gen, best_ind.fitness_test.values[0], best_ind.fitness.values[0],
             len(best_ind), avg_nodes(population)))
        best_st.write('\n%s;%s' % (gen, best_ind))

    return population, logbook