Skip to content

Commit

Permalink
Add test for reordering parameterization consistency check
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikMettler committed Oct 30, 2020
1 parent e4ff8b9 commit 227dde8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 13 deletions.
14 changes: 6 additions & 8 deletions cgp/ea/mu_plus_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ def __init__(
k_local_search : int
Number of individuals in the whole population (parents +
offsprings) to apply local search to.
reorder_genome: bool, optional
reorder_genome: bool, optional
Whether genome reordering should be applied.
If True, reorder is applied to the parents genomes
at every generation before creating offspring.
Thereby creating a neutral drift though the search space.
Reorder randomizes the genotype of an individual without changing the phenotype.
Reorder shuffles the genotype of an individual without changing its phenotype,
thereby contributing to neutral drift through the genotypic search space.
If True, reorder is applied to each parents genome at every generation
before creating offsprings.
Defaults to True.
"""
self.n_offsprings = n_offsprings
Expand Down Expand Up @@ -80,9 +80,7 @@ def initialize_fitness_parents(
pop._parents = self._compute_fitness(pop.parents, objective)

def step(
self,
pop: Population,
objective: Callable[[IndividualBase], IndividualBase],
self, pop: Population, objective: Callable[[IndividualBase], IndividualBase],
) -> Population:
"""Perform one step in the evolution.
Expand Down
7 changes: 4 additions & 3 deletions cgp/genome.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,9 @@ def _replace_invalid_input_alleles(self, dna: List[int], rng: np.random.RandomSt
gene_value = rng.choice(permissible_values)
dna[gene_idx] = gene_value

def _get_addable_nodes(self, node_dependencies: Dict[int, Set[int]],
used_node_indices: List[Union[int, None]] = []) -> Set[int]:
def _get_addable_nodes(
self, node_dependencies: Dict[int, Set[int]], used_node_indices: List[int] = [],
) -> Set[int]:
""" Get the set of addable nodes,
nodes which have no dependencies and were not already used.
"""
Expand Down Expand Up @@ -394,7 +395,7 @@ def _determine_node_dependencies(self) -> Dict[int, Set[int]]:
input_node_idx = self._dna[operator_idx + idx_gene]
if not self._is_input_region(
input_node_idx
): # not necessary to add input regions, since they are first anyway
): # not necessary to add input regions, since their positions remain fixed
current_node_dependencies.add(input_node_idx)

dependencies[region_idx] = current_node_dependencies
Expand Down
2 changes: 0 additions & 2 deletions examples/example_reorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,10 @@ def recording_callback_with_reorder(pop):

# %%
# and finally perform the evolution of the two populations
print("Population without genome reordering:")
cgp.evolve(
pop, objective, ea, **evolve_params, print_progress=True, callback=recording_callback,
)

print("Population with genome reordering:")
cgp.evolve(
pop_with_reorder,
objective,
Expand Down
31 changes: 31 additions & 0 deletions test/test_genome.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,3 +616,34 @@ def test_genome_reordering_empirically(rng):
genome.reorder(rng)
sympy_expression_after_reorder = cgp.CartesianGraph(genome).to_sympy()
assert sympy_expression_after_reorder == sympy_expression


def test_genome_reordering_parameterization_consistency(rng):

genome_params = {
"n_inputs": 2,
"n_outputs": 1,
"n_columns": 10,
"n_rows": 2,
"levels_back": None,
"primitives": (cgp.Mul, cgp.Sub, cgp.Add, cgp.ConstantFloat),
}

genome = cgp.Genome(**genome_params)

with pytest.raises(ValueError):
genome.reorder(rng)

genome_params = {
"n_inputs": 2,
"n_outputs": 1,
"n_columns": 10,
"n_rows": 1,
"levels_back": 5,
"primitives": (cgp.Mul, cgp.Sub, cgp.Add, cgp.ConstantFloat),
}

genome = cgp.Genome(**genome_params)

with pytest.raises(ValueError):
genome.reorder(rng)

0 comments on commit 227dde8

Please sign in to comment.