def remove_vars(literal): """ This removes all variables by putting XXX at the front of the string, so it cannot be unified anymore. """ return tuple('XXX' + ele if is_variable(ele) else remove_vars(ele) if isinstance(ele, tuple) else ele for ele in literal)
def generalize_literal(literal, gensym): """ This takes a literal and returns the most general version of it possible. i.e., a version that has all the values replaced with new veriables. """ return (literal[0], ) + tuple(ele if is_variable(ele) else # '?gen%s' % hash(ele) gensym() for ele in literal[1:])
def successors(self, node): h = node.state (constraints, c_length, p_uncovered, n_uncovered, pset, nset, gensym) = node.extra # remove literals for literal in h: removable = True for ele in literal[1:]: if not is_variable(ele): removable = False break if (count_occurances(ele, h) > 1): removable = False break if removable: new_h = frozenset(x for x in h if x != literal) new_pset, new_nset = test_coverage(new_h, constraints, p_uncovered, n_uncovered) new_p_uncovered = [p for p in p_uncovered if p not in new_pset] new_n_uncovered = [n for n in n_uncovered if n not in new_nset] new_c_length = c_length - 1 # new_c_length = clause_length(new_h) score = self.score( len(pset) - len(new_p_uncovered), len(nset) - len(new_n_uncovered), len(pset), len(nset), new_c_length) yield Node(new_h, node, ('remove literal', literal), -1 * score, extra=(constraints, new_c_length, new_p_uncovered, new_n_uncovered, pset, nset, gensym)) # replace constants with variables. for literal in h: for new_l in get_variablizations(literal, gensym): new_h = frozenset([x if x != literal else new_l for x in h]) new_pset, new_nset = test_coverage(new_h, constraints, p_uncovered, n_uncovered) new_p_uncovered = [p for p in p_uncovered if p not in new_pset] new_n_uncovered = [n for n in n_uncovered if n not in new_nset] new_c_length = c_length - 1 # new_c_length = clause_length(new_h) score = self.score( len(pset) - len(new_p_uncovered), len(nset) - len(new_n_uncovered), len(pset), len(nset), new_c_length) yield Node(new_h, node, ('variablize', literal, new_l), -1 * score, extra=(constraints, new_c_length, new_p_uncovered, new_n_uncovered, pset, nset, gensym))
def count_elements(x, var_counts): """ Counts the number of constants and keeps track of variable occurnaces. """ c = 0 if isinstance(x, tuple): c = sum([count_elements(ele, var_counts) for ele in x]) elif is_variable(x): if x not in var_counts: var_counts[x] = 0 var_counts[x] += 1 else: c = 1 return c
def get_variablizations(literal): """ Takes a literal and returns all possible variablizations of it. Currently, this replaces constants only. Also, it replaces them with a variable that is generated based on the hash of the constant, so that similar constants map to the same variable. """ if isinstance(literal, tuple): head = literal[0] possible_bodies = [[e] + list(get_variablizations(e)) for e in literal[1:]] for body in product(*possible_bodies): yield (head, ) + tuple(body) elif not is_variable(literal): yield '?gen%i' % hash(literal)
def gen_generalizations(self, node): h = node.state (constraints, c_length, p_covered, p_uncovered, n_covered, n_uncovered, gensym) = node.extra # remove literals for literal in h: removable = True for ele in literal[1:]: if not is_variable(ele): removable = False break if (count_occurances(ele, h) > 1): removable = False break if removable: new_h = frozenset(x for x in h if x != literal) new_pc_subset, new_nc_subset = test_coverage( new_h, constraints, p_uncovered, n_uncovered) new_p_covered = p_covered + new_pc_subset new_n_covered = n_covered + new_nc_subset new_p_uncovered = [ p for p in p_uncovered if p not in new_pc_subset ] new_n_uncovered = [ n for n in n_uncovered if n not in new_nc_subset ] new_c_length = c_length - 1 score = self.score(len(new_p_covered), len(new_p_uncovered), len(new_n_covered), len(new_n_uncovered), new_c_length) yield Node(new_h, None, None, -1 * score, extra=(constraints, new_c_length, new_p_covered, new_p_uncovered, new_n_covered, new_n_uncovered, gensym)) # replace constants with variables. for literal in h: for new_l in get_variablizations(literal, gensym): new_h = frozenset([x if x != literal else new_l for x in h]) new_pc_subset, new_nc_subset = test_coverage( new_h, constraints, p_uncovered, n_uncovered) new_p_covered = p_covered + new_pc_subset new_n_covered = n_covered + new_nc_subset new_p_uncovered = [ p for p in p_uncovered if p not in new_pc_subset ] new_n_uncovered = [ n for n in n_uncovered if n not in new_nc_subset ] new_c_length = c_length - 1 score = self.score(len(new_p_covered), len(new_p_uncovered), len(new_n_covered), len(new_n_uncovered), new_c_length) yield Node(new_h, None, None, -1 * score, extra=(constraints, new_c_length, new_p_covered, new_p_uncovered, new_n_covered, new_n_uncovered, gensym))