Example #1
0
    def _execute_program(self, examples: Task, 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:
            self._solver.asserta(clause)
            covered_examples = []
            pos, neg = examples.get_examples()
            total_examples = pos.union(neg)
            for example in total_examples:
                if self._solver.has_solution(example):
                    covered_examples.append(example)
            self._solver.retract(clause)

            # 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 covered_examples
    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
Example #3
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
Example #4
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])