コード例 #1
0
def genetic_algorithm_binary(fitness_fn,
                             chr_size,
                             pop_size=100,
                             total_generations=20,
                             cx_rate=0.7,
                             ind_mt_rate=1.0,
                             op_mt_rate=0.01,
                             elitism=False):
    # initial population
    pop = pg.make_integer_population(pop_size, chr_size)
    pop = pg.evaluate_population(pop, fitness_fn)
    pg.evolution_progress(0, pop)

    # evolutionary loop
    for i in range(1, total_generations):
        pop = pg.select_population(pop, pg.tournament_selection)
        offsprings = pg.apply_crossover(pop, cx_rate, pg.uniform_crossover)
        offsprings = pg.apply_mutation(offsprings,
                                       ind_mt_rate,
                                       pg.flip_mutation,
                                       gene_rate=op_mt_rate)
        if elitism:
            pop = pg.elite_strategy(
                pg.generational_replacement(pop, offsprings),
                pg.best_individual(pop))
        else:
            pop = pg.generational_replacement(pop, offsprings)
        pop = pg.evaluate_population(pop, fitness_fn)
        pg.evolution_progress(i, pop)

    return pop
コード例 #2
0
def test_worst_individual():
    np.random.seed(42)
    pop = pg.make_integer_population(10, 5)
    pop = pg.evaluate_population(pop, lambda x: 1. / (1. + pg.onemax(x)))
    worst = pg.worst_individual(pop)
    assert np.array_equal(worst.genotype, np.array([0, 1, 0, 0, 0]))
    assert worst.fitness.value == 0.5
コード例 #3
0
def test_best_individual():
    np.random.seed(42)
    pop = pg.make_integer_population(10, 5)
    pop = pg.evaluate_population(pop, lambda x: 1. / (1. + pg.onemax(x)))
    best = pg.best_individual(pop)
    assert np.array_equal(best.genotype, np.array([1, 1, 1, 1, 1]))
    assert best.fitness.value == 0.16666666666666666
コード例 #4
0
def test_m_plus_lambda():
    np.random.seed(42)
    sphere_model_size = 30
    fitness_fn = lambda x: pg.sphere_model(x[:sphere_model_size])
    size = 10
    pop1 = pg.make_uniform_population(size, sphere_model_size)
    pop1 = pg.evaluate_population(pop1, fitness_fn)
    pop1_clone = pop1.clone()
    pop2 = pg.make_uniform_population(size, sphere_model_size)
    pop2 = pg.evaluate_population(pop2, fitness_fn)
    pop1 = pg.mu_plus_lambda_replacement(pop1, pop2)
    f_p1c = np.array(
        [pop1_clone.individuals[i].fitness.value for i in range(size)])
    f_p2 = np.array([pop2.individuals[i].fitness.value for i in range(size)])
    f_p1 = np.array([pop1.individuals[i].fitness.value for i in range(size)])
    res = np.array([
        7.76332642, 8.08389311, 8.42498289, 8.77341594, 8.9523664, 9.45857558,
        9.49688766, 9.52769953, 9.7264757, 9.83516821
    ])
    assert np.allclose(f_p1, res) is True
    assert np.allclose(f_p1, f_p1c) is False
    assert f_p1.any() == f_p2.any()
コード例 #5
0
def test_m_comma_lambda():
    np.random.seed(42)
    sphere_model_size = 30
    fitness_fn = lambda x: pg.sphere_model(x[:sphere_model_size])
    size = 10
    pop1 = pg.make_uniform_population(size, sphere_model_size)
    pop1 = pg.evaluate_population(pop1, fitness_fn)
    pop1_clone = pop1.clone()
    pop2 = pg.make_uniform_population(size, sphere_model_size)
    pop2 = pg.evaluate_population(pop2, fitness_fn)
    pop1 = pg.mu_comma_lambda_replacement(pop1, pop2)
    f_p1c = np.array(
        [pop1_clone.individuals[i].fitness.value for i in range(size)])
    f_p2 = np.array([pop2.individuals[i].fitness.value for i in range(size)])
    f_p1 = np.array([pop1.individuals[i].fitness.value for i in range(size)])
    res = np.array([
        7.7633264242708808, 8.4249828886076532, 9.4585755842303119,
        9.8351682100669695, 10.109666113362849, 10.120464613773503,
        11.148338435537882, 12.62638917659477, 12.68750163403401,
        13.486951247705489
    ])
    assert np.allclose(f_p1, res) is True
    assert np.allclose(f_p1, f_p1c) is False
    assert f_p1.any() == f_p2.any()
コード例 #6
0
def test_elite_strategy():
    np.random.seed(42)
    size = 10
    ind_size = 20
    pop = pg.make_integer_population(size, ind_size)
    fitness_function = lambda x: 1. / pg.onemax(x)
    pop = pg.make_integer_population(size, ind_size)
    pop = pg.evaluate_population(pop, fitness_function)
    best = pg.best_individual(pop)
    pop = pg.elite_strategy(pop, best)
    found = False
    for ind in pop.individuals:
        if ind.equal(best) is True:
            found = True
            break

    assert found is True
コード例 #7
0
def test_select_population():
    np.random.seed(42)
    pop = pg.make_integer_population(10, 5)
    pop = pg.evaluate_population(pop, lambda x: 1. / (1. + pg.onemax(x)))
    diff = 0
    for i in range(pop.size):
        if not np.array_equal(pop.individuals[i].genotype,
                              pop.individuals[i].genotype):
            diff += 1
    assert diff == 0

    original_pop = pop.clone()
    pop = pg.select_population(pop, pg.tournament_selection)
    for i in range(pop.size):
        if not np.array_equal(pop.individuals[i].genotype,
                              original_pop.individuals[i].genotype):
            diff += 1
    assert diff != 0