Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max_genome_size parameter #160

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pyshgp/gp/estimators.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __init__(self,
population_size: int = 300,
max_generations: int = 100,
initial_genome_size: Tuple[int, int] = (20, 100),
max_genome_size: int = 100,
simplification_steps: int = 2000,
last_str_from_stdout: bool = False,
interpreter: PushInterpreter = "default",
Expand All @@ -86,6 +87,7 @@ def __init__(self,
self.population_size = population_size
self.max_generations = max_generations
self.initial_genome_size = initial_genome_size
self.max_genome_size = max_genome_size
self.simplification_steps = simplification_steps
self.last_str_from_stdout = last_str_from_stdout
self.parallelism = parallelism
Expand Down Expand Up @@ -126,6 +128,7 @@ def _build_search_algo(self):
population_size=self.population_size,
max_generations=self.max_generations,
initial_genome_size=self.initial_genome_size,
max_genome_size=self.max_genome_size,
simplification_steps=self.simplification_steps,
parallelism=self.parallelism,
push_config=self.push_config
Expand Down
9 changes: 6 additions & 3 deletions pyshgp/gp/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ def __init__(self,
max_generations: int = 100,
error_threshold: float = 0.0,
initial_genome_size: Tuple[int, int] = (10, 50),
max_genome_size: int = 100,
simplification_steps: int = 2000,
parallelism: Union[int, bool] = True,
**kwargs):
Expand All @@ -105,6 +106,7 @@ def __init__(self,
self.max_generations = max_generations
self.error_threshold = error_threshold
self.initial_genome_size = initial_genome_size
self.max_genome_size = max_genome_size
self.simplification_steps = simplification_steps
self.ext = kwargs

Expand Down Expand Up @@ -265,7 +267,7 @@ def _make_child(self) -> Individual:
op = self.config.get_variation_op()
selector = self.config.get_selector()
parent_genomes = [p.genome for p in selector.select(self.population, n=op.num_parents)]
child_genome = op.produce(parent_genomes, self.config.spawner)
child_genome = op.produce_and_fix(parent_genomes, self.config.spawner, self.config.max_genome_size)
return Individual(child_genome, self.config.signature)

@tap
Expand Down Expand Up @@ -329,9 +331,10 @@ def step(self):
return

candidate = Individual(
self.config.get_variation_op().produce(
self.config.get_variation_op().produce_and_fix(
[self.population.best().genome],
self.config.spawner
self.config.spawner,
self.config.max_genome_length
),
self.config.signature
)
Expand Down
8 changes: 8 additions & 0 deletions pyshgp/gp/variation.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ def produce(self, parents: Sequence[Genome], spawner: GeneSpawner) -> Genome:
"""
pass

def fix(self, child: Genome, max_genome_size: int) -> Genome:
if len(child) > max_genome_size:
child = child[:max_genome_size]
return child

def produce_and_fix(self, parents: Sequence[Genome], spawner: GeneSpawner, max_genome_size: int) -> Genome:
return self.fix(self.produce(parents, spawner), max_genome_size)


class VariationStrategy(DiscreteProbDistrib):
"""A collection of VariationOperator and how frequently to use them."""
Expand Down