@@ -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
732732def 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