def swap_gain(mapping, node_id1, mapping_id1, node_id2, mapping_id2, weight_dict, match_num):
new_mapping_list = mapping[:]<EOL>new_mapping_list[node_id1] = mapping_id2<EOL>new_mapping_list[node_id2] = mapping_id1<EOL>if tuple(new_mapping_list) in match_triple_dict:<EOL><INDENT>return match_triple_dict[tuple(new_mapping_list)] - match_num<EOL><DEDENT>gain = <NUM_LIT:0><EOL>new_mapping1 = (node_id1, mapping_id2)<EOL>new_mapping2 = (node_id2, mapping_id1)<EOL>old_mapping1 = (node_id1, mapping_id1)<EOL>old_mapping2 = (node_id2, mapping_id2)<EOL>if node_id1 > node_id2:<EOL><INDENT>new_mapping2 = (node_id1, mapping_id2)<EOL>new_mapping1 = (node_id2, mapping_id1)<EOL>old_mapping1 = (node_id2, mapping_id2)<EOL>old_mapping2 = (node_id1, mapping_id1)<EOL><DEDENT>if new_mapping1 in weight_dict:<EOL><INDENT>for key in weight_dict[new_mapping1]:<EOL><INDENT>if key == -<NUM_LIT:1>:<EOL><INDENT>gain += weight_dict[new_mapping1][-<NUM_LIT:1>]<EOL><DEDENT>elif new_mapping_list[key[<NUM_LIT:0>]] == key[<NUM_LIT:1>]:<EOL><INDENT>gain += weight_dict[new_mapping1][key]<EOL><DEDENT><DEDENT><DEDENT>if new_mapping2 in weight_dict:<EOL><INDENT>for key in weight_dict[new_mapping2]:<EOL><INDENT>if key == -<NUM_LIT:1>:<EOL><INDENT>gain += weight_dict[new_mapping2][-<NUM_LIT:1>]<EOL><DEDENT>elif key[<NUM_LIT:0>] == node_id1:<EOL><INDENT>continue<EOL><DEDENT>elif new_mapping_list[key[<NUM_LIT:0>]] == key[<NUM_LIT:1>]:<EOL><INDENT>gain += weight_dict[new_mapping2][key]<EOL><DEDENT><DEDENT><DEDENT>if old_mapping1 in weight_dict:<EOL><INDENT>for key in weight_dict[old_mapping1]:<EOL><INDENT>if key == -<NUM_LIT:1>:<EOL><INDENT>gain -= weight_dict[old_mapping1][-<NUM_LIT:1>]<EOL><DEDENT>elif mapping[key[<NUM_LIT:0>]] == key[<NUM_LIT:1>]:<EOL><INDENT>gain -= weight_dict[old_mapping1][key]<EOL><DEDENT><DEDENT><DEDENT>if old_mapping2 in weight_dict:<EOL><INDENT>for key in weight_dict[old_mapping2]:<EOL><INDENT>if key == -<NUM_LIT:1>:<EOL><INDENT>gain -= weight_dict[old_mapping2][-<NUM_LIT:1>]<EOL><DEDENT>elif key[<NUM_LIT:0>] == node_id1:<EOL><INDENT>continue<EOL><DEDENT>elif mapping[key[<NUM_LIT:0>]] == key[<NUM_LIT:1>]:<EOL><INDENT>gain -= weight_dict[old_mapping2][key]<EOL><DEDENT><DEDENT><DEDENT>match_triple_dict[tuple(new_mapping_list)] = match_num + gain<EOL>return gain<EOL>
Compute the triple match number gain from the swapping
Arguments:
mapping: current node mapping list
node_id1: node 1 index in AMR 1
mapping_id1: the node index in AMR 2 node 1 maps to (in the current mapping)
node_id2: node 2 index in AMR 1
mapping_id2: the node index in AMR 2 node 2 maps to (in the current mapping)
weight_dict: weight dictionary
match_num: the original matching triple number
Returns:
the gain number (might be negative)