Skip to content

Commit

Permalink
Extend testing of MuPlusLambda class
Browse files Browse the repository at this point in the history
  • Loading branch information
mschmidt87 committed Jul 4, 2020
1 parent 415481d commit e6a9688
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions test/test_ea_mu_plus_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def objective(ind):
return ind

population_params["n_parents"] = 1

pop = cgp.Population(**population_params, genome_params=genome_params)

ea = cgp.ea.MuPlusLambda(10, 10, 1)
Expand Down Expand Up @@ -125,3 +126,96 @@ def objective(ind):

for idx in range(k_local_search, population_params["n_parents"]):
assert pop[idx].genome._parameter_names_to_values["<p1>"] == pytest.approx(1.0)

def test_n_offspring_less_than_n_breeding():
n_offsprings = 10
n_breedings = 5
with pytest.raises(ValueError):
_ = cgp.ea.MuPlusLambda(n_offsprings, n_breedings, 1)


def test_fitness_wrong_return_type(population_params, genome_params):
def objective(individual):
individual.fitness = 5
return individual

pop = cgp.Population(**population_params, genome_params=genome_params)
ea = cgp.ea.MuPlusLambda(10, 10, 1)
ea.initialize_fitness_parents(pop, objective)

with pytest.raises(ValueError):
ea.step(pop, objective)


def test_fitness_initialization(population_params, genome_params):
def objective(individual):
individual.fitness = -1.0
return individual

pop = cgp.Population(**population_params, genome_params=genome_params)

ea = cgp.ea.MuPlusLambda(10, 10, 1)
ea.initialize_fitness_parents(pop, objective)
assert all([ind.fitness is not None for ind in pop.parents])


def test_one_step(population_params, genome_params):
def objective(individual):
individual.fitness = float(individual.idx)
return individual

pop = cgp.Population(**population_params, genome_params=genome_params)
n_offsprings = 10
ea = cgp.ea.MuPlusLambda(n_offsprings, 10, 1)
ea.initialize_fitness_parents(pop, objective)
old_parent_ids = sorted([ind.idx for ind in pop.parents])
ea.step(pop, objective)
new_parent_ids = sorted([ind.idx for ind in pop.parents])
# After one step, the new parent population should have IDs that
# are offset from the old parent ids by n_offsprings
# This is by construction in this test because the fitness is equal to the id
assert all(
[new_id == old_id + n_offsprings for new_id, old_id in zip(new_parent_ids, old_parent_ids)]
)


def test_fitness_sort(population_params, genome_params):
def objective(individual):
individual.fitness = float(individual.idx)
return individual

pop = cgp.Population(**population_params, genome_params=genome_params)
n_offsprings = 10
ea = cgp.ea.MuPlusLambda(n_offsprings, 10, 1)
ea.initialize_fitness_parents(pop, objective)
sorted_parents = ea._sort(pop.parents)
# Assert that the sorting inverted the list of parents (because the fitness is equal to the id)
assert sorted_parents == pop.parents[::-1]


def test_create_new_populations(population_params, genome_params):
def objective(individual):
individual.fitness = float(individual.idx)
return individual

pop = cgp.Population(**population_params, genome_params=genome_params)
n_offsprings = 10
ea = cgp.ea.MuPlusLambda(n_offsprings, 10, 1)

ea.initialize_fitness_parents(pop, objective)

offsprings = ea._create_new_offspring_generation(pop)
assert len(offsprings) == n_offsprings
assert all([ind.idx >= pop.n_parents for ind in offsprings])
# Assert that all offspring dna are different from all parents dna
offspring_dna = [ind.genome.dna for ind in offsprings]
parent_dna = [ind.genome.dna for ind in pop.parents]
assert all([odna != pdna for odna in offspring_dna for pdna in parent_dna])

# Create new "parent" population from the offsprings and assert
# that we picked the 5 best ones (here, by construction, the 5
# highest ids)
offsprings = ea._compute_fitness(offsprings, objective)
offsprings = ea._sort(offsprings)
new_parents = ea._create_new_parent_population(pop.n_parents, offsprings)
assert [ind.idx for ind in new_parents] == [ind.idx for ind in offsprings[: pop.n_parents]]

0 comments on commit e6a9688

Please sign in to comment.