Skip to content

Commit

Permalink
Apply PR-FB
Browse files Browse the repository at this point in the history
Apply PR-FB
  • Loading branch information
HenrikMettler committed Nov 4, 2020
1 parent 48d7d09 commit 178afb4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
34 changes: 16 additions & 18 deletions test/test_ea_mu_plus_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,39 +228,37 @@ def objective(individual):
individual.fitness = float(individual.idx)
return individual

# create a population and an instance of the evolutionary algorithm and
# check that number of objective calls is initially zero
n_objective_calls_expected = 0
pop = cgp.Population(**population_params, genome_params=genome_params)
ea = cgp.ea.MuPlusLambda(**ea_params)
assert ea.n_objective_calls == 0
assert ea.n_objective_calls == n_objective_calls_expected

# after initialization of parents number of objective calls must be equal
# to the number of parents
ea.initialize_fitness_parents(pop, objective)
assert ea.n_objective_calls == population_params["n_parents"]
n_objective_calls_expected = population_params["n_parents"]
assert ea.n_objective_calls == n_objective_calls_expected

# count the number of objective calls by adding the number of offspring with fitness = None
# at each generation
n_objective_calls = ea.n_objective_calls
n_objective_calls_expected = ea.n_objective_calls
n_generations = 100
for _ in range(n_generations):
offsprings = ea._create_new_offspring_generation(pop)
combined = offsprings + pop.parents
n_objective_calls += sum([1 for ind in combined if ind.fitness is None])
n_objective_calls_expected += sum([1 for ind in combined if ind.fitness is None])
combined = ea._compute_fitness(combined, objective)
assert n_objective_calls == ea.n_objective_calls
assert n_objective_calls_expected == ea.n_objective_calls


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

# create a population with mutation rate one and check that the number
# of objective calls is always equal to the number of parents
# plus the current generation times the number of offspring
population_params["mutation_rate"] = 1.0
pop = cgp.Population(**population_params, genome_params=genome_params)
ea = cgp.ea.MuPlusLambda(**ea_params)
ea.initialize_fitness_parents(pop, objective)
n_objective_calls_expected = population_params["n_parents"]
n_step_calls = 100
for idx_current_step in range(n_step_calls):
ea.step(pop, objective)
assert (
ea.n_objective_calls
== population_params["n_parents"] + (idx_current_step + 1) * ea_params["n_offsprings"]
)
n_objective_calls_expected += ea_params["n_offsprings"]
assert ea.n_objective_calls == n_objective_calls_expected
6 changes: 4 additions & 2 deletions test/test_hl_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ def f1(x):
assert abs(pop.champion.fitness) == pytest.approx(0.0)


def test_finite_evolve(population_params, genome_params, ea_params):
def test_finite_max_generations_or_max_objective_calls(
population_params, genome_params, ea_params
):
def objective(individual):
individual.fitness = float(individual.idx)
return individual
Expand All @@ -144,7 +146,7 @@ def objective(individual):
ea = cgp.ea.MuPlusLambda(**ea_params)
evolve_params = {
"max_generations": np.inf,
"min_fitness": -1e-12,
"min_fitness": 0,
"max_objective_calls": np.inf,
}
with pytest.raises(ValueError):
Expand Down

0 comments on commit 178afb4

Please sign in to comment.