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
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
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
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
def test_onemax():
    assert pg.onemax(np.array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])) == 10
    assert pg.onemax(np.array([1, 0, 1, 0, 1, 0, 1, 0, 1, 0])) == 5
    assert pg.onemax(np.array([0, 0, 1, 1, 0, 0, 1, 0, 0, 0])) == 3
    assert pg.onemax(np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])) == 0
Beispiel #6
0
def fitness_fn(x):
    return 1. / pg.onemax(x)