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 Jan 3, 2025
1 parent 7c41221 commit 545297e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions grammarinator/tool/generator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017-2024 Renata Hodovan, Akos Kiss.
# Copyright (c) 2017-2025 Renata Hodovan, Akos Kiss.
#
# Licensed under the BSD 3-Clause License
# <LICENSE.rst or https://opensource.org/licenses/BSD-3-Clause>.
Expand Down 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 545297e

Please sign in to comment.