Skip to content

Commit dba564a

Browse files
committed
refactored expand node helper out of mutate_expand_node
1 parent 6a1af6f commit dba564a

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

gp_learner.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -441,24 +441,38 @@ def mutate_del_triple(child):
441441
return new_child
442442

443443

444-
def mutate_expand_node(child, pb_en_out_link):
445-
"""Expands a random node by adding a new var-only triple to it.
446-
447-
Randomly selects a node. Then (depending on the probability pb_en_out_link)
448-
adds an outgoing or incoming triple with two new vars to it.
444+
def _mutate_expand_node_helper(node, pb_en_out_link=config.MUTPB_EN_OUT_LINK):
445+
"""Adds a new var-only triple to node.
449446
450-
:arg pb_en_out_link: Probability to create an outgoing triple.
451-
:return: A child with the added outgoing/incoming triple.
447+
:param pb_en_out_link: Probability to create an outgoing triple.
448+
:return: The new triple, node and var
452449
"""
453-
# TODO: can maybe be improved by sparqling
454-
nodes = list(child.nodes)
455-
node = random.choice(nodes)
456450
var_edge = gen_random_var()
457451
var_node = gen_random_var()
458452
if random.random() < pb_en_out_link:
459453
new_triple = (node, var_edge, var_node)
460454
else:
461455
new_triple = (var_node, var_edge, node)
456+
return new_triple, var_node, var_edge
457+
458+
459+
def mutate_expand_node(
460+
child, node=None, pb_en_out_link=config.MUTPB_EN_OUT_LINK):
461+
"""Expands a random node by adding a new var-only triple to it.
462+
463+
Randomly selects a node. Then adds an outgoing or incoming triple with two
464+
new vars to it.
465+
466+
:param child: The GraphPattern to expand a node in.
467+
:param node: If given the node to expand, otherwise
468+
:param pb_en_out_link: Probability to create an outgoing triple.
469+
:return: A child with the added outgoing/incoming triple.
470+
"""
471+
# TODO: can maybe be improved by sparqling
472+
if not node:
473+
nodes = list(child.nodes)
474+
node = random.choice(nodes)
475+
new_triple, _, _ = _mutate_expand_node_helper(node, pb_en_out_link)
462476
return child + (new_triple,)
463477

464478

@@ -742,7 +756,6 @@ def mutate(
742756
pb_ae=config.MUTPB_AE,
743757
pb_dt=config.MUTPB_DT,
744758
pb_en=config.MUTPB_EN,
745-
pb_en_out_link=config.MUTPB_EN_OUT_LINK,
746759
pb_fv=config.MUTPB_FV,
747760
pb_id=config.MUTPB_ID,
748761
pb_iv=config.MUTPB_IV,
@@ -773,7 +786,7 @@ def mutate(
773786
child = mutate_del_triple(child)
774787

775788
if random.random() < pb_en:
776-
child = mutate_expand_node(child, pb_en_out_link)
789+
child = mutate_expand_node(child)
777790
if random.random() < pb_ae:
778791
child = mutate_add_edge(child)
779792

0 commit comments

Comments
 (0)