Exemplo n.º 1
0
    def get_initial_weights(self, examples: Task) -> dict:
        example_weights = {}
        for examples in examples.get_examples():
            for example in examples:
                example_weights[example] = 1

        return example_weights
Exemplo n.º 2
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
Exemplo n.º 3
0
    def evaluate(
            self, examples: Task, clause: Clause,
            hypothesis_space: HypothesisSpace) -> typing.Union[int, float]:
        """
        Evaluates a clause by calling the Learner's eval_fn.
        Returns a number (the higher the better)
        """
        # add_to_cache(node,key,val)
        # retrieve_from_cache(node,key) -> val or None
        # remove_from_cache(node,key) -> None

        # Cache holds sets of examples that were covered before
        covered = hypothesis_space.retrieve_from_cache(clause, "covered")

        # We have executed this clause before
        if covered is not None:
            # Note that _eval.fn.evaluate() will ignore clauses in `covered`
            # that are not in the current Task
            result = self._eval_fn.evaluate(clause, examples, covered)
            # print("No query here.")
            return result
        else:
            covered = self._execute_program(clause)
            # if 'None', i.e. trivial hypothesis, all clauses are covered
            if covered is None:
                pos, neg = examples.get_examples()
                covered = pos.union(neg)

            result = self._eval_fn.evaluate(clause, examples, covered)
            hypothesis_space.add_to_cache(clause, "covered", covered)
            return result
Exemplo n.º 4
0
    def evaluate_distinct(self, examples: Task,
                          clause: Clause) -> typing.Tuple[int, int]:
        covered = self._execute_program(examples, clause)
        pos, neg = examples.get_examples()

        covered_pos = pos.intersection(covered)
        covered_neg = neg.intersection(covered)

        return len(covered_pos), len(covered_neg)
Exemplo n.º 5
0
    def evaluate(self, clause: Clause, examples: Task,
                 covered: Sequence[Atom]):
        self._clauses_evaluated += 1

        pos, neg = examples.get_examples()
        covered_pos = len(pos.intersection(covered))
        covered_neg = len(neg.intersection(covered))
        if self._return_upperbound:
            return (covered_pos - covered_neg), covered_pos
        return covered_pos - covered_neg
    def evaluate(self, examples: Task,
                 clause: Clause) -> typing.Union[int, float]:
        covered = self._execute_program(clause)

        pos, neg = examples.get_examples()

        covered_pos = pos.intersection(covered)
        covered_neg = neg.intersection(covered)

        if len(covered_neg) > 0:
            return 0
        else:
            return len(covered_pos)
Exemplo n.º 7
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
        """

        pos, neg = examples.get_examples()
        self._solver.assertz(clause)
        coverage = []
        for example in pos:
            if self._solver.has_solution(example):
                coverage.append(example)
        self._solver.retract(clause)
        return coverage
Exemplo n.º 8
0
    def evaluate(self, clause: Clause, examples: Task,
                 covered: Sequence[Atom]):
        self._clauses_evaluated += 1

        pos, neg = examples.get_examples()
        covered_pos = len(pos.intersection(covered))
        covered_neg = len(neg.intersection(covered))

        if covered_pos + covered_neg == 0:
            return 0 if not self._return_upperbound else 0, 0
        return (
            covered_pos / (covered_pos + covered_neg)
            if not self._return_upperbound else covered_pos /
            (covered_pos + covered_neg),
            1,
        )
Exemplo n.º 9
0
    def evaluate(self, clause: Clause, examples: Task,
                 covered: Sequence[Atom]):
        self._clauses_evaluated += 1

        pos, neg = examples.get_examples()
        covered_pos = len(pos.intersection(covered))
        covered_neg = len(neg.intersection(covered))
        if covered_pos + covered_neg == 0:
            return 0

        p = covered_pos / (covered_pos + covered_neg)

        # Perfect split, no entropy
        if p == 1 or p == 0:
            return 0
        return -(p * math.log10(p) + (1 - p) * math.log10(1 - p))
Exemplo n.º 10
0
    def get_best_primitives(
        self, examples: Task, current_cand: typing.Union[Clause, Recursion,
                                                         Body]
    ) -> typing.Sequence[typing.Union[Clause, Body, Procedure]]:
        scores = [0] * 22

        # Filter examples (e.g. only use positive/negative examples)
        # examples = self.filter_examples(examples)
        pos, neg = examples.get_examples()

        # Calculate nn output for each example
        for example in pos:
            # Update output
            nn_output = self.model.predict(
                get_nn_input_data(current_cand, example,
                                  self.current_primitives.tolist()))[0]
            nn_output = self.process_output(nn_output)

            # Update score vector
            scores = self.update_score(current_cand, example, scores,
                                       nn_output)

        for example in neg:
            # Update output
            nn_output = self.model.predict(
                get_nn_input_data(current_cand, example,
                                  self.current_primitives.tolist()))[0]
            nn_output = self.process_output(nn_output)

            # Update score vector
            scores = self.update_score(current_cand, example, scores,
                                       -0.2 * nn_output)

        # Return x best primitives
        indices = numpy.argpartition(
            scores, -self.amount_chosen_from_nn)[-self.amount_chosen_from_nn:]

        # TODO now a random order but could be replaced to the ordering via the score
        print("PRIMITIVES CHOSEN BY NN: ")
        print(self.current_primitives[indices], "\n")
        return self.current_primitives[indices]
Exemplo n.º 11
0
    def evaluate(self, examples: Task,
                 clause: Clause) -> typing.Union[int, float]:
        covered = self._execute_program(examples, clause)

        pos, neg = examples.get_examples()

        covered_pos = pos.intersection(covered)
        covered_neg = neg.intersection(covered)

        print("\t covered neg: ", len(covered_neg))
        print("\t covered pos: ", len(covered_pos))
        if (len(covered_pos) + len(covered_neg)) == 0:
            print("\t score: ", 0)
        else:
            print("\t score: ",
                  len(covered_pos) / (len(covered_pos) + len(covered_neg)))

        if len(covered_neg) > 0:
            return 0
        else:
            return len(covered_pos)
Exemplo n.º 12
0
 def evaluate(self, examples: Task, clause: Clause):
     pos, neg = examples.get_examples()
     numberofpositivecoverance = 0
     self._solver.assertz(clause)
     for example in pos:
         if self._solver.has_solution(example):
             numberofpositivecoverance += 1
     numberofnegativecoverance = 0
     for example in neg:
         if self._solver.has_solution(example):
             numberofnegativecoverance += 1
             # print(example)
     self._solver.retract(clause)
     if numberofnegativecoverance + numberofpositivecoverance == 0:
         return [0, 0]
     else:
         return [
             numberofpositivecoverance /
             (numberofpositivecoverance + numberofnegativecoverance) *
             (numberofpositivecoverance) / len(pos),
             numberofnegativecoverance
         ]
Exemplo n.º 13
0
 def filter_examples(self, examples: Task) -> Task:
     pos, _ = examples.get_examples()
     return pos