def get_best_gain(mapping, candidate_mappings, weight_dict, instance_len, cur_match_num):
largest_gain = <NUM_LIT:0><EOL>use_swap = True<EOL>node1 = None<EOL>node2 = None<EOL>unmatched = set(range(instance_len))<EOL>for nid in mapping:<EOL><INDENT>if nid in unmatched:<EOL><INDENT>unmatched.remove(nid)<EOL><DEDENT><DEDENT>for i, nid in enumerate(mapping):<EOL><INDENT>for nm in unmatched:<EOL><INDENT>if nm in candidate_mappings[i]:<EOL><INDENT>if veryVerbose:<EOL><INDENT>print("<STR_LIT>", i, "<STR_LIT>", nid, "<STR_LIT:to>", nm, file=DEBUG_LOG)<EOL><DEDENT>mv_gain = move_gain(mapping, i, nid, nm, weight_dict, cur_match_num)<EOL>if veryVerbose:<EOL><INDENT>print("<STR_LIT>", mv_gain, file=DEBUG_LOG)<EOL>new_mapping = mapping[:]<EOL>new_mapping[i] = nm<EOL>new_match_num = compute_match(new_mapping, weight_dict)<EOL>if new_match_num != cur_match_num + mv_gain:<EOL><INDENT>print(mapping, new_mapping, file=ERROR_LOG)<EOL>print("<STR_LIT>", cur_match_num, mv_gain, new_match_num,<EOL>file=ERROR_LOG)<EOL><DEDENT><DEDENT>if mv_gain > largest_gain:<EOL><INDENT>largest_gain = mv_gain<EOL>node1 = i<EOL>node2 = nm<EOL>use_swap = False<EOL><DEDENT><DEDENT><DEDENT><DEDENT>for i, m in enumerate(mapping):<EOL><INDENT>for j in range(i + <NUM_LIT:1>, len(mapping)):<EOL><INDENT>m2 = mapping[j]<EOL>if veryVerbose:<EOL><INDENT>print("<STR_LIT>", i, "<STR_LIT>", j, file=DEBUG_LOG)<EOL>print("<STR_LIT>", i, "<STR_LIT:->", m, "<STR_LIT:U+002C>", j, "<STR_LIT:->", m2, file=DEBUG_LOG)<EOL>print(mapping, file=DEBUG_LOG)<EOL>print("<STR_LIT>", i, "<STR_LIT:->", m2, "<STR_LIT:U+002C>", j, "<STR_LIT:->", m, file=DEBUG_LOG)<EOL><DEDENT>sw_gain = swap_gain(mapping, i, m, j, m2, weight_dict, cur_match_num)<EOL>if veryVerbose:<EOL><INDENT>print("<STR_LIT>", sw_gain, file=DEBUG_LOG)<EOL>new_mapping = mapping[:]<EOL>new_mapping[i] = m2<EOL>new_mapping[j] = m<EOL>print(new_mapping, file=DEBUG_LOG)<EOL>new_match_num = compute_match(new_mapping, weight_dict)<EOL>if new_match_num != cur_match_num + sw_gain:<EOL><INDENT>print(mapping, new_mapping, file=ERROR_LOG)<EOL>print("<STR_LIT>", cur_match_num, sw_gain, new_match_num,<EOL>file=ERROR_LOG)<EOL><DEDENT><DEDENT>if sw_gain > largest_gain:<EOL><INDENT>largest_gain = sw_gain<EOL>node1 = i<EOL>node2 = j<EOL>use_swap = True<EOL><DEDENT><DEDENT><DEDENT>cur_mapping = mapping[:]<EOL>if node1 is not None:<EOL><INDENT>if use_swap:<EOL><INDENT>if veryVerbose:<EOL><INDENT>print("<STR_LIT>", file=DEBUG_LOG)<EOL><DEDENT>temp = cur_mapping[node1]<EOL>cur_mapping[node1] = cur_mapping[node2]<EOL>cur_mapping[node2] = temp<EOL><DEDENT>else:<EOL><INDENT>if veryVerbose:<EOL><INDENT>print("<STR_LIT>", file=DEBUG_LOG)<EOL><DEDENT>cur_mapping[node1] = node2<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if veryVerbose:<EOL><INDENT>print("<STR_LIT>", file=DEBUG_LOG)<EOL><DEDENT><DEDENT>if veryVerbose:<EOL><INDENT>print("<STR_LIT>", mapping, file=DEBUG_LOG)<EOL>print("<STR_LIT>", cur_mapping, file=DEBUG_LOG)<EOL><DEDENT>return largest_gain, cur_mapping<EOL>
Hill-climbing method to return the best gain swap/move can get
Arguments:
mapping: current node mapping
candidate_mappings: the candidates mapping list
weight_dict: the weight dictionary
instance_len: the number of the nodes in AMR 2
cur_match_num: current triple match number
Returns:
the best gain we can get via swap/move operation