Пример #1
0
def _turn_clause_to_interim_repr(clause: Clause, suffix: str = "_x"):
    head_vars = dict([
        (v, ind) for ind, v in enumerate(clause.get_head().get_variables())
    ])

    return [
        tuple([a.get_predicate()] + [
            head_vars[t] if isinstance(t, Variable) and t in head_vars else t
            for t in a.get_terms()
        ]) for a in clause.get_literals()
    ]
Пример #2
0
def compute_bottom_clause(theory: Sequence[Clause], c: Clause) -> Clause:
    """
    Computes the bottom clause given a theory and a clause.
    Algorithm from (De Raedt,2008)
    """
    # 1. Find a skolemization substitution θ for c (w.r.t. B and c)
    _, theta = skolemize(c)

    # 2. Compute the least Herbrand model M of theory ¬body(c)θ
    body_facts = [
        Clause(l.substitute(theta), []) for l in c.get_body().get_literals()
    ]
    m = herbrand_model(theory + body_facts)

    # 3. Deskolemize the clause head(cθ) <= M and return the result.
    theta_inv = {value: key for key, value in theta.items()}
    return Clause(c.get_head(),
                  [l.get_head().substitute(theta_inv) for l in m])
    def _execute_program(self, clause: Clause) -> typing.Sequence[Atom]:
        """
        Evaluates a clause using the Prolog engine and background knowledge

        Returns a set of atoms that the clause covers
        """
        if len(clause.get_body().get_literals()) == 0:
            return []
        else:
            head_predicate = clause.get_head().get_predicate()
            head_variables = clause.get_head_variables()

            sols = self._solver.query(*clause.get_body().get_literals())

            sols = [
                head_predicate(*[s[v] for v in head_variables]) for s in sols
            ]

            return sols
Пример #4
0
 def encode2(self, clause: Clause):
     head = clause.get_head()
     if not self._problemindices.__contains__(head.get_predicate()):
         self.addproblem(head.get_predicate(), 10)
     problems = self._encodingProblems.copy()
     primitives = self._encodingprimitives.copy()
     variables = self._encodingvariables.copy()
     problems[self._problemindices[head.get_predicate()]] = 1
     variables[self._variablesindices[head.get_variables()[0]]] = 1
     cijfer = 3
     for literal in clause.get_literals():
         startindexliteral = self._primitivesindices[
             literal.get_predicate()]
         fillin = False
         while fillin == False:
             if (primitives[startindexliteral] == 0):
                 primitives[startindexliteral] = cijfer
                 fillin = True
             else:
                 startindexliteral += 1
         startindexvariable = self._variablesindices[
             literal.get_variables()[0]]
         fillin = False
         while fillin == False:
             if (variables[startindexvariable] == 0):
                 variables[startindexvariable] = cijfer
                 fillin = True
             else:
                 startindexvariable += 1
         if (len(literal.get_variables()) == 2):
             startindexvariable = self._variablesindices[
                 literal.get_variables()[1]]
             fillin = False
             while fillin == False:
                 if (variables[startindexvariable] == 0):
                     variables[startindexvariable] = cijfer + 1
                     fillin = True
                 else:
                     startindexvariable += 1
         cijfer += 2
     return problems + primitives + variables
Пример #5
0
    def _execute_program(self, clause: Clause, count_as_query: bool = True) -> typing.Sequence[Atom]:
        """
        Evaluates a clause using the Prolog engine and background knowledge

        Returns a set of atoms that the clause covers
        """
        if len(clause.get_body().get_literals()) == 0:
            # Covers all possible examples because trivial hypothesis
            return None
        else:
            head_predicate = clause.get_head().get_predicate()
            head_args = clause.get_head_arguments()
            # print("{}({})".format(head_predicate, *head_args))

            sols = self._solver.query(*clause.get_body().get_literals())
            self._prolog_queries += 1 if count_as_query else 0

            # Build a solution by substituting Variables with their found value
            # and copying constants without change
            sols = [head_predicate(*[s[v] if isinstance(v,Variable) else v for v in head_args]) for s in sols]

            return sols
Пример #6
0
    def encode(self, clause: Clause):
        encodingClause = np.zeros(1850)
        vars = []
        set = {}
        index = 0
        for lit in [clause.get_head(), *clause.get_literals()]:
            var = ''
            for variable in lit.get_variables():
                var += variable.get_name()
            if var in set:
                index = set[var]
                vars[index][1].append(lit)
            else:
                set[var] = index
                index += 1
                list = [lit]
                if len(var) == 1:
                    value = 100000 * (ord(var) - 64) + 3500 * (
                        ord(var) - 64) + 130 * (ord(var) - 64)
                else:
                    if len(var) == 2:
                        if ord(var[0]) <= ord(var[-1]):
                            value = 100000 * (ord(var[0]) - 64) + 3500 * (
                                ord(var[0]) - 64) + 130 * (ord(var[-1]) - 64)
                        else:
                            value = 100000 * (ord(var[0]) - 64) + 3500 * (ord(
                                var[0]) - 64) + 130 * (ord(var[-1]) - 64) + 1
                    else:
                        if ord(var[0]) <= ord(var[1]) <= ord(var[2]):
                            value = 100000 * (ord(var[0]) - 64) + 3500 * (
                                ord(var[1]) - 64) + 130 * (ord(var[2]) - 64)
                        else:
                            if ord(var[0]) <= ord(var[2]) <= ord(var[1]):
                                value = 100000 * (ord(var[0]) - 64) + 3500 * (
                                    ord(var[2]) - 64) + 130 * (ord(var[1]) -
                                                               64) + 1
                            else:
                                if ord(var[1]) <= ord(var[0]) <= ord(var[2]):
                                    value = 100000 * (
                                        ord(var[1]) - 64) + 3500 * (
                                            ord(var[0]) -
                                            64) + 130 * (ord(var[2]) - 64) + 2
                                else:
                                    if ord(var[1]) <= ord(var[0]) <= ord(
                                            var[2]):
                                        value = 100000 * (
                                            ord(var[1]) - 64) + 3500 * (
                                                ord(var[0]) - 64) + 130 * (
                                                    ord(var[2]) - 64) + 3
                                    else:
                                        if ord(var[2]) <= ord(var[0]) <= ord(
                                                var[1]):
                                            value = 100000 * (
                                                ord(var[2]) - 64) + 3500 * (
                                                    ord(var[0]) - 64) + 130 * (
                                                        ord(var[1]) - 64) + 4
                                        else:
                                            value = 100000 * (
                                                ord(var[2]) - 64) + 3500 * (
                                                    ord(var[1]) - 64) + 130 * (
                                                        ord(var[0]) - 64) + 5

                vars.append((value, list))
        vars.sort()
        newClause = []
        for v in vars:
            newClause = newClause + v[1]
        encoding = [
            self.variableSubstition(newClause[i:i + 2])
            for i in range(len(newClause) - 1)
        ]
        for element in encoding:
            encodingClause[self._dictionary.get(tuple(element))] += 1
        encodingClause[1849] = len(clause.get_variables())
        return encodingClause