Skip to content

Commit

Permalink
Track parent idx in offsprings, i.e., all cloned individuals
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobj committed Jul 3, 2020
1 parent a842483 commit fb6da50
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cgp/individual.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def __init__(self, fitness: Union[float, None]) -> None:
"""
self.fitness: Union[float, None] = fitness
self.idx: int
self.parent_idx: int

def clone(self):
raise NotImplementedError()
Expand Down Expand Up @@ -105,7 +106,9 @@ def __repr__(self):
return f"Individual(idx={self.idx}, fitness={self.fitness}, genome={self.genome}))"

def clone(self) -> "IndividualSingleGenome":
return IndividualSingleGenome(self.fitness, self.genome.clone())
ind = IndividualSingleGenome(self.fitness, self.genome.clone())
ind.parent_idx = self.idx
return ind

def mutate(self, mutation_rate: float, rng: np.random.RandomState) -> None:
only_silent_mutations = self._mutate_genome(self.genome, mutation_rate, rng)
Expand Down Expand Up @@ -149,7 +152,9 @@ def __init__(self, fitness: Union[float, None], genome: List[Genome]) -> None:
self.genome: List[Genome] = genome

def clone(self) -> "IndividualMultiGenome":
return IndividualMultiGenome(self.fitness, [g.clone() for g in self.genome])
ind = IndividualMultiGenome(self.fitness, [g.clone() for g in self.genome])
ind.parent_idx = self.idx
return ind

def mutate(self, mutation_rate: float, rng: np.random.RandomState) -> None:
for g in self.genome:
Expand Down
19 changes: 19 additions & 0 deletions test/test_ea_mu_plus_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ def objective(ind):
assert ind.idx == len(pop.parents) + idx


def test_offspring_individuals_are_assigned_correct_parent_indices(
population_params, genome_params
):
def objective(ind):
ind.fitness = 0.0
return ind

population_params["n_parents"] = 1
pop = cgp.Population(**population_params, genome_params=genome_params)

ea = cgp.ea.MuPlusLambda(10, 10, 1)
ea.initialize_fitness_parents(pop, objective)

offsprings = ea._create_new_offspring_generation(pop)

for ind in offsprings:
assert ind.parent_idx == 0


def test_local_search_is_only_applied_to_best_k_individuals(
population_params, local_search_params
):
Expand Down

0 comments on commit fb6da50

Please sign in to comment.