Beispiel #1
0
def test_clone_population():
    # for this test we don't require a real population of Individual objects
    inds = np.array([1, 2, 3, 4, 5])
    pop1 = pg.Population(individuals=inds)
    pop2 = pop1.clone()
    assert np.array_equal(pop1.individuals, pop2.individuals)
    assert pop1.size == pop2.size

    pop1.individuals = np.array([6, 7, 8])
    pop1.size = 3
    assert np.array_equal(pop1.individuals, pop2.individuals) is False
    assert pop2.size == 5
def test_negative_tournament_selection():
    np.random.seed(42)
    individuals = [
        pg.Individual(fitness=pg.Fitness(value=np.random.randint(10)))
        for _ in range(10)
    ]
    pop = pg.Population(individuals=np.array(individuals))
    # [6, 3, 7, 4, 6, 9, 2, 6, 7, 4]
    np.random.seed(42)
    inds1 = [pg.negative_tournament_selection(pop, size=5) for _ in range(10)]
    assert [inds1[i].fitness.value
            for i in range(10)] == [6, 7, 6, 9, 9, 9, 7, 7, 7, 7]
    np.random.seed(24)
    inds2 = [pg.negative_tournament_selection(pop, size=5) for _ in range(10)]
    assert [inds2[i].fitness.value
            for i in range(10)] == [7, 6, 7, 7, 6, 6, 3, 4, 6, 7]
def test_random_selection():
    np.random.seed(42)
    individuals = [
        pg.Individual(fitness=pg.Fitness(value=np.random.randint(10)))
        for _ in range(10)
    ]
    pop = pg.Population(individuals=np.array(individuals))
    # [6, 3, 7, 4, 6, 9, 2, 6, 7, 4]
    np.random.seed(42)
    inds1 = [pg.random_selection(pop) for _ in range(10)]
    assert [inds1[i].fitness.value
            for i in range(10)] == [2, 4, 6, 6, 2, 4, 7, 2, 6, 6]
    np.random.seed(24)
    inds2 = [pg.random_selection(pop) for _ in range(10)]
    assert [inds2[i].fitness.value
            for i in range(10)] == [7, 4, 6, 6, 3, 3, 3, 6, 6, 4]
Beispiel #4
0
def test_make_population():
    pop = pg.Population()
    assert type(pop) is pg.Population
    assert pop.individuals is None
    assert pop.size == 0