Skip to content

Commit

Permalink
Improve the replicate_quantified
Browse files Browse the repository at this point in the history
The mutator is improved by ensuring to replicate non-empty subtrees
only and instead of inserting a single copy, a random number of
replicant will be added that still obeys the quantifier restrictions.
  • Loading branch information
renatahodovan committed Dec 31, 2024
1 parent cb6d120 commit 706c82c
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions grammarinator/tool/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,12 @@ def replicate_quantified(self, individual=None, _=None):
root_options = [node for node in annot.quants if node.stop > len(node.children)]
recipient_root_token_counts = annot.token_counts[root]
node_options = [child for root in root_options for child in root.children if
recipient_root_token_counts + annot.token_counts[child] <= self._limit.tokens]
recipient_root_token_counts < recipient_root_token_counts + annot.token_counts[child] <= self._limit.tokens]
if node_options:
node_to_repeat = random.choice(node_options)
node_to_repeat.parent.insert_child(idx=random.randint(0, len(node_to_repeat.parent.children)), node=deepcopy(node_to_repeat))
max_repeat = (self._limit.tokens - recipient_root_token_counts) // annot.token_counts[node_to_repeat]
for _ in range(random.randint(1, max_repeat)):
node_to_repeat.parent.insert_child(idx=random.randint(0, len(node_to_repeat.parent.children)), node=deepcopy(node_to_repeat))

# Return with the original root, whether the replication was successful or not.
return root
Expand Down

0 comments on commit 706c82c

Please sign in to comment.