Esempio n. 1
0
def mgbde_run(benchmark, bounds, pop_size, maxFE, F):

    #--- INITIALIZE A POPULATION (step #1) ----------------+

    population = init.uniform_random(pop_size, bounds)
    cr = init_cr(pop_size)
    mutation_strategy = init_mutation_strategies(pop_size)

    #--- SOLVE --------------------------------------------+

    # cycle through each generation (step #2)
    while benchmark.numFE < maxFE:
        best = init_eval(population, benchmark.eval)
        # cycle through each individual in the population
        for j in range(pop_size):
            x_t = population.get(j).vector

            #--- MUTATION (step #3.A) ---------------------+
            if mutation_strategy[j] == 0:
                v_donor = gaussian_mutation(best, population.get(j), bounds)
            else:
                v_donor = de_best_1_mutation(best, population, j, bounds, F)

            #--- RECOMBINATION (step #3.B) ----------------+
            v_trial = crossover(v_donor, x_t, cr[j])

            #--- GREEDY SELECTION (step #3.C) -------------+
            score_trial, score_target = selection(population, j,
                                                  benchmark.eval,
                                                  Individual(v_trial))

            # --- UPDATE CR (step #3.C) -------------+
            cr[j] = update_cr(score_trial, score_target, cr[j])

        #--- SCORE KEEPING --------------------------------+

        # gen_avg = sum(gen_scores) / pop_size                         # current generation avg. fitness
        # gen_best = min(gen_scores)                                  # fitness of best individual
        # gen_sol = population.get(gen_scores.index(min(gen_scores)))     # solution of best individual
        best = population.get_best()
        if benchmark.numFE % 10000 == 0:
            print('numFE:', benchmark.numFE)
            # print('      > GENERATION AVERAGE:', gen_avg)
            # print('      > GENERATION BEST:', gen_best)
            # print('         > BEST SOLUTION:', gen_sol)
            print('----------------------------------------------')
            print('      > GENERATION BEST:', best.score)
            print('      > GENERATION BEST:', best.vector)
            print('----------------------------------------------')
        if benchmark.is_solution(best):
            print('Find a solution! numFE:{0}'.format(benchmark.numFE))
            break
    return population
Esempio n. 2
0
def run(benchmark, bounds, pop_size, F, CR, maxFE):

    #--- INITIALIZE A POPULATION (step #1) ----------------+

    population = init.uniform_random(pop_size, bounds)

    #--- SOLVE --------------------------------------------+

    # cycle through each generation (step #2)
    while benchmark.numFE < maxFE:
        # cycle through each individual in the population
        for j in range(pop_size):

            #--- MUTATION (step #3.A) ---------------------+

            v_donor, x_t = mutation(population, j, bounds, F)

            #--- RECOMBINATION (step #3.B) ----------------+
            v_trial = crossover(v_donor, x_t, CR)

            #--- GREEDY SELECTION (step #3.C) -------------+
            selection(population, j, benchmark.eval, Individual(v_trial))

        #--- SCORE KEEPING --------------------------------+

        # gen_avg = sum(gen_scores) / pop_size                         # current generation avg. fitness
        # gen_best = min(gen_scores)                                  # fitness of best individual
        # gen_sol = population.get(gen_scores.index(min(gen_scores)))     # solution of best individual
        gen_best = population.get_best()
        if benchmark.numFE % 10000 == 0:
            print('numFE:', benchmark.numFE)
            # print('      > GENERATION AVERAGE:', gen_avg)
            # print('      > GENERATION BEST:', gen_best)
            # print('         > BEST SOLUTION:', gen_sol)
            print('----------------------------------------------')
            print('      > GENERATION BEST:', gen_best.score)
            print('----------------------------------------------')
        if benchmark.is_solution(gen_best):
            print('Find a solution! numFE:{0}'.format(benchmark.numFE))
            break
    return population
Esempio n. 3
0
def bnde_run(benchmark, bounds, pop_size, neighborhoodSize, maxFE, d0=1.0E-16):

    #--- INITIALIZE A POPULATION (step #1) ----------------+
    archive = []  # a archive to store all the local best

    population = init.uniform_random(pop_size, bounds)
    print(str(population))
    best = init_eval(population, benchmark.eval)
    print(str(population))
    neighbors = MultiPopulationsWithSameSize(pop_size, neighborhoodSize,
                                             population)
    print(str(neighbors))

    # cr = init_cr(pop_size)
    # mutation_strategy = init_mutation_strategies(pop_size)

    mu_pe = 0.5
    mu_cr = 0.5

    #--- SOLVE --------------------------------------------+

    # cycle through each generation (step #2)
    while benchmark.numFE < maxFE:
        # best = init_eval(population, benchmark.eval)
        pe, mu_pe = update_pe(neighbors.size, neighborhoodSize, mu_pe, q=0.1)
        cr, mu_cr = update_cr_bnde(neighbors.size,
                                   neighborhoodSize,
                                   mu_cr,
                                   q=0.1)

        diversity_preserving(neighbors, archive, bounds, benchmark.eval, d0=d0)
        chi = np.exp(-4.0 * (benchmark.numFE / maxFE + 0.4))
        # cycle through each individual in the population
        best_scores = []
        for i in range(neighbors.size):
            sub_pop_i = neighbors.get_subpopulation(i)
            best_i, worst_i = sub_pop_i.get_best_worst(redo=True)
            best_scores.append(benchmark.error(best_i))
            for j in range(sub_pop_i.size):
                x_t = sub_pop_i.get(j).vector

                #--- MUTATION (step #3.A) ---------------------+

                v_donor = gaussian_mutation_bnde(sub_pop_i.best_index, j,
                                                 sub_pop_i, bounds, pe[i][j],
                                                 chi)
                # print("v_donor", v_donor)
                # if mutation_strategy[j] == 0:
                #     v_donor = gaussian_mutation(best, population.get(j), bounds)
                # else:
                #     v_donor = de_best_1_mutation(best, population, j, bounds, F)

                #--- RECOMBINATION (step #3.B) ----------------+
                v_trial = crossover(v_donor, x_t, cr[i][j])
                # print("v_trial", v_trial)

                #--- GREEDY SELECTION (step #3.C) -------------+
                score_trial, score_target = selection(sub_pop_i, j,
                                                      benchmark.eval,
                                                      Individual(v_trial))
                # print("numFE:", benchmark.numFE)
        #--- SCORE KEEPING --------------------------------+

        # gen_avg = sum(gen_scores) / pop_size                         # current generation avg. fitness
        # gen_best = min(gen_scores)                                  # fitness of best individual
        # gen_sol = population.get(gen_scores.index(min(gen_scores)))     # solution of best individual

        if benchmark.numFE % 1000 == 0:
            print('numFE:', benchmark.numFE)
            # print('      > GENERATION AVERAGE:', gen_avg)
            # print('      > GENERATION BEST:', gen_best)
            # print('         > BEST SOLUTION:', gen_sol)
            print('----------------------------------------------')
            print('      > GENERATION AVG BEST:', np.mean(best_scores))
            print('      > GENERATION STD BEST:', np.std(best_scores))
            print('      > GENERATION BEST of BEST:', np.min(best_scores))
            print('      > GENERATION WORST of BEST:', np.max(best_scores))
            print('----------------------------------------------')
    for solution in archive:
        print(solution)
    return population
Esempio n. 4
0
def reinitialize_population(bounds, cost_fn, sub_pop):
    new_pop = init.uniform_random(sub_pop.size, bounds)
    init_eval(new_pop, cost_fn)
    return new_pop