Skip to content

Commit 3e7b352

Browse files
nandikaKjoernhees
authored andcommitted
WIP: added backtracking to deep narrow path mutation
1 parent 72fc46a commit 3e7b352

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

config/defaults.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@
8585
MUTPB_DN_MAX_NODE_COUNT = 10 # edge fixations may have <= nodes
8686
MUTPB_DN_MIN_EDGE_COUNT = 2 # edges need to be valid for >= GTPs
8787
MUTPB_DN_QUERY_LIMIT = 32 # SPARQL query limit for top edge fixations
88+
MUTPB_DN_LOOK_AHEAD_LIMIT = 2
89+
MUTPB_DN_RECURSION_LIMIT = 4
8890

8991
# for import in helpers and __init__
9092
__all__ = [_v for _v in globals().keys() if _v.isupper()]

gp_learner.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,35 +723,61 @@ def _mutate_deep_narrow_path_helper(
723723
children = [
724724
c if fit_to_live(c) else orig_child
725725
for c in children
726-
]
726+
]
727727
if children:
728728
child = random.choice(list(children))
729729
return child, fixed
730730

731731

732732
def mutate_deep_narrow_path(
733733
child, sparql, timeout, gtp_scores,
734+
_rec_depth=0,
735+
start_node=None,
734736
min_len=config.MUTPB_DN_MIN_LEN,
735737
max_len=config.MUTPB_DN_MAX_LEN,
736738
term_pb=config.MUTPB_DN_TERM_PB,
739+
recursion_look_ahead=config.MUTPB_DN_LOOK_AHEAD_LIMIT,
740+
rec_limit=config.MUTPB_DN_RECURSION_LIMIT,
737741
):
738742
assert isinstance(child, GraphPattern)
739743
nodes = list(child.nodes)
740-
start_node = random.choice(nodes)
741-
# target_nodes = set(nodes) - {start_node}
744+
if start_node is None:
745+
start_node = random.choice(nodes)
746+
fixed_for_start_node = start_node
747+
fixed_gp = child
742748
gp = child
743749
hop = 0
750+
false_fixed_count = 0
744751
while True:
745752
if hop >= min_len and random.random() < term_pb:
746753
break
747754
if hop >= max_len:
748755
break
749756
hop += 1
750757
new_triple, var_node, var_edge = _mutate_expand_node_helper(start_node)
758+
orig_gp = gp
751759
gp += [new_triple]
752760
gp, fixed = _mutate_deep_narrow_path_helper(
753761
sparql, timeout, gtp_scores, gp, var_edge, var_node)
754-
start_node = var_node
762+
if fixed:
763+
fixed_for_start_node = start_node
764+
fixed_gp = orig_gp
765+
false_fixed_count = 0
766+
start_node = var_node
767+
if not fixed:
768+
false_fixed_count += 1
769+
if false_fixed_count > recursion_look_ahead:
770+
_rec_depth += 1
771+
if _rec_depth > rec_limit:
772+
return gp
773+
start_node = fixed_for_start_node
774+
gp = mutate_deep_narrow_path(
775+
fixed_gp, sparql, timeout, gtp_scores,
776+
_rec_depth,
777+
start_node=start_node
778+
)
779+
return gp
780+
start_node = var_node
755781
return gp
756782

757783

0 commit comments

Comments
 (0)