Skip to content

Commit

Permalink
Add unrestricted mutators to the list of available options in `Genera…
Browse files Browse the repository at this point in the history
…torTool.__init__()` (#269)

It is more efficient to do this only once, not at every call to
create() or mutate().
  • Loading branch information
akosthekiss authored Jan 26, 2025
1 parent b30d9f9 commit 8c81d4f
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions grammarinator/tool/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ def __init__(self, generator_factory, out_format, lock=None, rule=None, limit=No
self._enable_generation = generate
self._enable_mutation = mutate
self._enable_recombination = recombine
self._enable_unrestricted_creators = unrestricted
self._keep_trees = keep_trees
self._cleanup = cleanup
self._encoding = encoding
Expand All @@ -204,14 +203,15 @@ def __init__(self, generator_factory, out_format, lock=None, rule=None, limit=No
self.swap_local_nodes,
self.insert_local_node,
]
self._unrestricted_mutators = [
self.unrestricted_delete,
self.unrestricted_hoist_rule,
]
self._recombiners = [
self.replace_node,
self.insert_quantified,
]
if unrestricted:
self._mutators += [
self.unrestricted_delete,
self.unrestricted_hoist_rule,
]

def __enter__(self):
return self
Expand Down Expand Up @@ -284,8 +284,6 @@ def create(self):
if self._population:
if self._enable_mutation:
creators.extend(self._mutators)
if self._enable_unrestricted_creators:
creators.extend(self._unrestricted_mutators)
if self._enable_recombination:
creators.extend(self._recombiners)
return self._create_tree(creators, individual1, individual2)
Expand All @@ -312,10 +310,7 @@ def mutate(self, individual=None):
# If you call this explicitly, then so be it, even if mutation is disabled.
# If individual is None, population MUST exist.
individual = self._ensure_individual(individual)
mutators = self._mutators
if self._enable_unrestricted_creators:
mutators.extend(self._unrestricted_mutators)
return self._create_tree(mutators, individual, None)
return self._create_tree(self._mutators, individual, None)

def recombine(self, individual1=None, individual2=None):
"""
Expand Down

0 comments on commit 8c81d4f

Please sign in to comment.