예제 #1
0
    def test_cycle_goodcode(self):
        N = 20
        program = self.program_v1[:]

        for i in range(0, N):
            seed = str(random.random())[2:]
            random.seed(seed)
            random.shuffle(program)
            txt = "\n".join(program)
            f = DefaultEngine(label_all=True).ground_all(PrologString(txt))
            paths = list(list_paths(f))

            edges = set()
            for p in paths:
                for i in range(0, len(p) - 1):
                    edges.add((int(p[i]), int(p[i + 1])))
            edges = list(sorted(edges))

            # if (edges != self.edges) :
            #     with open('cycle_error.pl', 'w') as f :
            #         print(txt, file=f)
            #     with open('cycle_error.dot', 'w') as f :
            #         print('digraph CycleError {', file=f)
            #         for edge in edges :
            #             print('%s -> %s;' % edge, file=f)
            #         print('}', file=f)

            self.assertCollectionEqual(self.edges,
                                       edges,
                                       msg="Test failed for random seed %s" %
                                       seed)
예제 #2
0
def run_tests_with_static_methods():
    from problog.program import PrologString
    from problog.engine import DefaultEngine
    from problog.logic import Term, Var
    p = PrologString("""
    coin(c1). coin(c2).
    0.4::heads(C); 0.6::tails(C) :- coin(C).
    win :- heads(C).
    """)

    qs, evs = ([Term("win")], [(Term("heads", Term("c1")), False)])  # For now

    engine = DefaultEngine()
    db = engine.prepare(p)
    labels = (LogicFormula.LABEL_QUERY, LogicFormula.LABEL_EVIDENCE_POS,
              LogicFormula.LABEL_EVIDENCE_NEG)
    lf = LogicFormula()
    lf = AMCQuery.ground_query_evidence(engine, db, qs, evs, lf, labels)

    circuit = AMCQuery.compile_to_circuit(lf, "ddnnf")
    prob_sr = SemiringProbability()
    results, ground_evidence = AMCQuery.evaluate_circuit(
        circuit, labels, prob_sr)
    print("evidence: ", ground_evidence)
    for r in results:
        print(r)
    print("---")
예제 #3
0
 def __init__(self,
              pos_examples,
              neg_examples,
              extra_terms=[],
              target_name='target'):
     # Define the language of terms
     self.target = Term(target_name)
     self.equal = Term('equal')
     self.pos_examples = pos_examples
     self.neg_examples = neg_examples
     self.examples = pos_examples + neg_examples
     self.extra_terms = extra_terms
     #TODO: check extra terms arity, if greater than target arity, create more variables
     n_target_variables = len(self.examples[0])
     target_variables_names = [
         'X' + str(i) for i in range(1, n_target_variables + 1)
     ]
     self.X = list(map(Var, target_variables_names))
     constants = set()
     for example in self.examples:
         constants.update(example)
     self.c = list(map(Term, [str(constant) for constant in constants]))
     # Initialize the logic program
     self.pl = SimpleProgram()
     self.pl += self.equal(self.X[0], self.X[0])
     self.pl += self.target(*tuple(self.X))
     for extra_term in self.extra_terms:
         self.pl += PrologString(extra_term)
     self.predicates = [self.equal]  # + list(extra_terms.keys())
     self.engine = DefaultEngine()
     self.db = self.engine.prepare(self.pl)
     self.original_rule = list(self.pl)[1]
     self.new_body_literals = []
     print(list(self.pl))
예제 #4
0
def main_cross_validation(fname_examples: str, fname_settings: str, fname_background: str,
                          dir_fold_files: str, fname_prefix_fold: str, fold_start_index: int, nb_folds: int,
                          fold_suffix: str, dir_output_files: str,
                          filter_out_unlabeled_examples=False,
                          debug_printing_example_parsing=False,
                          debug_printing_tree_building=False,
                          debug_printing_tree_pruning=False,
                          debug_printing_program_conversion=False,
                          debug_printing_get_classifier=False,
                          debug_printing_classification=False):
    engine = DefaultEngine()
    engine.unknown = 1

    fd = FoldData.build_fold_data(fname_examples, fname_settings, fname_background,
                                  dir_fold_files, fname_prefix_fold, fold_start_index, nb_folds, fold_suffix,
                                  dir_output_files,
                                  filter_out_unlabeled_examples,
                                  debug_printing_example_parsing,
                                  debug_printing_tree_building,
                                  debug_printing_tree_pruning,
                                  debug_printing_program_conversion,
                                  debug_printing_get_classifier,
                                  debug_printing_classification,
                                  engine=engine
                                  )

    # take one key set as test, the others as training
    for fold_index, test_key_set in enumerate(fd.all_key_sets):
        do_one_fold(fold_index, test_key_set, fd)

    do_all_examples(fd)
예제 #5
0
 def __init__(self, engine: GenericEngine = None):
     if engine is None:
         self.engine = DefaultEngine()
         self.engine.unknown = 1
     else:
         self.engine = engine
     self.to_query = Term('to_query')
def load_data(filename, engine=None):
    if engine is None:
        engine = DefaultEngine()
        engine.prepare(PrologString(':- unknown(fail).'))

    data = read_data(filename)

    background_pl = list(PrologString('\n'.join(data.get('BACKGROUND', []))))

    language = CModeLanguage.load(data)

    background_pl += language.background

    examples = data.get('', [])
    examples_db = [
        engine.prepare(background_pl + list(PrologString(example_pl)))
        for example_pl in examples
    ]
    instances = Interpretations(
        [Instance(example_db) for example_db in examples_db], background_pl)

    neg_examples = data.get('!', [])
    #print("the negative examples are {}".format("".join(neg_examples)))
    neg_examples_db = [
        engine.prepare(background_pl + list(PrologString(neg_example_pl)))
        for neg_example_pl in neg_examples
    ]
    neg_instances = Interpretations(
        [Instance(neg_example_db) for neg_example_db in neg_examples_db],
        background_pl)

    return language, instances, neg_instances, engine
def _run_tests():
    from problog.program import PrologString
    from problog.engine import DefaultEngine
    p = PrologString("""
    coin(c1). coin(c2).
    0.4::heads(C); 0.6::tails(C) :- coin(C).
    win :- heads(C).
    """)

    engine = DefaultEngine()
    db = engine.prepare(p)

    # tasks = [
    #     ([Term("win")], []),
    #     ([Term("win")], [(Term("heads", Term("c1")), True)]),
    #     ([Term("win")], [(Term("heads", Term("c1")), False)]),
    # ]
    # for q,e in tasks:
    #     qs.prepare_query(q, e)
    # print(qs.evaluate_queries())

    qs = QuerySession(engine, db)
    inline_queries = [" win | heads(c1).", "win | \+heads(c1).", "win."]
    for iq in inline_queries:
        q, e = qs.transform_inline_query(PrologString(iq)[0])
        qs.prepare_query(q, e)

    result = qs.evaluate_queries()
    print(result)
예제 #8
0
def run_eval_neg(filename, **other):
    from .data import read_data, concat, Interpretations, Instance
    from problog.program import PrologString
    # from problog.logic import AnnotatedDisjunction, Clause
    print("starting eval")
    data = read_data(filename)

    rules = concat(data['RULES'])

    engine = DefaultEngine()
    engine.prepare(PrologString(':- unknown(fail).'))

    background_pl = concat(data.get('BACKGROUND', []))

    examples = data.get('!', [])
    examples_db = [
        engine.prepare(PrologString(background_pl + example_pl))
        for example_pl in examples
    ]
    instances = Interpretations(
        [Instance(example_db) for example_db in examples_db],
        PrologString(background_pl))

    for rule in PrologString(rules):
        clause = Clause.from_logic(rule)
        print('Evaluation of rule:', clause)
        if not clause.validate(instances, engine):
            print('\tRule is invalid')
            #for ex, success in enumerate(clause.successes):
            #    if not success:
            #        print('\t\tExample %s:' % (ex + 1), success)

        else:
            print('\tRule is valid.')
예제 #9
0
def run_test_with_query_instance():

    from problog.program import PrologString
    from problog.engine import DefaultEngine
    from problog.logic import Term, Var
    p = PrologString("""
    coin(c1). coin(c2).
    0.4::heads(C); 0.6::tails(C) :- coin(C).
    win :- heads(C).
    """)
    from .formula_wrapper import FormulaWrapper

    s_qs, s_evs = ([Term("win")], [(Term("heads",
                                         Term("c1")), False)])  # For now

    engine = DefaultEngine()
    probsr = SemiringProbability()
    fw = FormulaWrapper(engine.prepare(p))
    qobj = AMCQuery(s_qs, s_evs, fw, target_class=DDNNF, semiring=probsr)

    qobj.ground(engine)
    result, ground_evidence = qobj.evaluate(engine)
    print("evidence: ", ground_evidence)
    for r in result:
        print(r)
    print("---")
예제 #10
0
def init_engine(**kwdargs):
    engine = DefaultEngine(**kwdargs)
    engine.add_builtin("sample", 2, builtin_sample)
    engine.add_builtin("value", 2, builtin_sample)
    engine.add_builtin("previous", 2, builtin_previous)
    engine.previous_result = None
    return engine
예제 #11
0
 def __init__(self, *sources):
     self._database = DefaultEngine().prepare(sources[0])
     # pdb.set_trace()
     for source in sources[1:]:
         for clause in source:
             # pdb.set_trace()
             self._database += clause
예제 #12
0
def main():
    p = PrologString("""
    mother_child(trude, sally).
    
    father_child(tom, sally).
    father_child(tom, erica).
    father_child(mike, tom).
    
    sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
    
    parent_child(X, Y) :- father_child(X, Y).
    parent_child(X, Y) :- mother_child(X, Y).
    """)

    sibling = Term('sibling')
    query_term = sibling(None, None)
    engine = DefaultEngine()

    # prepare the model for querying
    model_db = engine.prepare(p)  # This compiles the Prolog model into an internal format.
    # This step is optional, but it might be worthwhile if you
    #  want to query the same model multiple times.

    times_query = test_query_method1(engine, model_db, query_term)
    times_query_extended = test_query_method2(engine, model_db, query_term)

    print("average duration query:", statistics.mean(times_query), "seconds")
    print("average duration query:", statistics.mean(times_query_extended), "seconds")
예제 #13
0
    def __init__(self,
                 fname_prefix_fold,
                 nb_folds,
                 dir_output_files,
                 debug_printing_example_parsing=False,
                 debug_printing_tree_building=False,
                 debug_printing_tree_pruning=False,
                 debug_printing_program_conversion=False,
                 debug_printing_get_classifier=False,
                 debug_printing_classification=False,
                 engine: GenericEngine = None):

        self.fname_prefix_fold = fname_prefix_fold
        self.nb_folds = nb_folds
        self.dir_output_files = dir_output_files
        self.dir_output_files = dir_output_files

        self.debug_printing_example_parsing = debug_printing_example_parsing
        self.debug_printing_tree_building = debug_printing_tree_building
        self.debug_printing_tree_pruning = debug_printing_tree_pruning
        self.debug_printing_program_conversion = debug_printing_program_conversion
        self.debug_printing_get_classifier = debug_printing_get_classifier
        self.debug_printing_classification = debug_printing_classification

        if engine is None:
            self.engine = DefaultEngine()
            self.engine.unknown = 1
        else:
            self.engine = engine
예제 #14
0
파일: sample.py 프로젝트: rasata/ProbLog
def init_engine(**kwdargs):
    engine = DefaultEngine(**kwdargs)
    engine.add_builtin('sample', 2, builtin_sample)
    engine.add_builtin('value', 2, builtin_sample)
    engine.add_builtin('previous', 2, builtin_previous)
    engine.previous_result = None
    return engine
예제 #15
0
    def test_anonymous_variable(self):
        """Anonymous variables are distinct"""

        program = """
            p(_,X,_) :- X = 3.

            q(1,2,3).
            q(1,2,4).
            q(2,3,5).
            r(Y) :- q(_,Y,_).

        """

        engine = DefaultEngine()
        db = engine.prepare(PrologString(program))
        self.assertEqual(
            list(
                map(
                    list,
                    engine.query(
                        db, Term("p", Constant(1), Constant(3), Constant(2))),
                )),
            [[Constant(1), Constant(3), Constant(2)]],
        )

        self.assertEqual(list(map(list, engine.query(db, Term("r", None)))),
                         [[2], [3]])
예제 #16
0
    def test_functors(self) :
        """Calls with functors"""

        program = """
            p(_,f(A,B),C) :- A=y, B=g(C).
            a(X,Y,Z) :- p(X,f(Y,Z),c).
        """
        pl = PrologString(program)

        r1 = DefaultEngine().query(pl, Term('a',Term('x'),None,Term('g',Term('c'))))
        r1 = [ list(map(str,sol)) for sol in r1  ]
        self.assertCollectionEqual( r1, [['x', 'y', 'g(c)']])

        r2 = DefaultEngine().query(pl, Term('a',Term('x'),None,Term('h',Term('c'))))
        self.assertCollectionEqual( r2, [])

        r3 = DefaultEngine().query(pl, Term('a',Term('x'),None,Term('g',Term('z'))))
        self.assertCollectionEqual( r3, [])
예제 #17
0
def sample( filename, N=1, with_facts=False, oneline=False ) :
    pl = PrologFile(filename)
    
    engine = DefaultEngine()
    db = engine.prepare(pl)
    
    for i in range(0, N) :
        result = engine.ground_all(db, target=SampledFormula())
        print ('====================')
        print (result.toString(db, with_facts, oneline))
예제 #18
0
    def test_compare(self) :
        """Comparison operator"""

        program = """
            morning(Hour) :- Hour >= 6, Hour =< 10.
        """

        engine = DefaultEngine()
        db = engine.prepare( PrologString(program) )

        self.assertEqual( list(map(list,engine.query(db, Term('morning', Constant(8)) ))), [[8]])
예제 #19
0
def do_inference(a, b, terms, program):
    engine = DefaultEngine()
    xs = engine.prepare(program)

    print("[%s]是[%s]的 --> " % (b, a), end="")
    for key in terms:
        query_term = terms[key](a, b)
        res = engine.query(xs, query_term)
        if bool(res):
            print(terms[key], end=" ")
    print("")
예제 #20
0
 def __init__(self, program, fformat):
     #GlobalEngine
     self._engine = DefaultEngine()
     gdl_parser = GDLIIIParser()
     self._model = gdl_parser.output_model(program, fformat)
     self._baseModelFile = self._model.as_problog()
     self._playerList = []
     self._randomIdentifier = Constant(0) #Hardcoded to give the random player a specific constant id as we apply some special rules to the random player
     worlds = self._initialiseKB()
     self._cur_node = GDLNode(worlds, GameData(self._playerList, self._randomIdentifier))
     self._moveList = dict([(i,None) for i in self._playerList])
     self.terminal = False
예제 #21
0
    def test_functors(self):
        """Calls with functors"""

        program = """
            p(_,f(A,B),C) :- A=y, B=g(C).
            a(X,Y,Z) :- p(X,f(Y,Z),c).
        """
        pl = PrologString(program)

        r1 = DefaultEngine().query(
            pl, Term("a", Term("x"), None, Term("g", Term("c"))))
        r1 = [list(map(str, sol)) for sol in r1]
        self.assertCollectionEqual(r1, [["x", "y", "g(c)"]])

        r2 = DefaultEngine().query(
            pl, Term("a", Term("x"), None, Term("h", Term("c"))))
        self.assertCollectionEqual(r2, [])

        r3 = DefaultEngine().query(
            pl, Term("a", Term("x"), None, Term("g", Term("z"))))
        self.assertCollectionEqual(r3, [])
예제 #22
0
    def __init__(self,
                 query_result_label_extractor: QueryResultLabelExtractor,
                 debug_printing: Optional[bool] = None,
                 engine: GenericEngine = None):
        if engine is None:
            self.engine = DefaultEngine()
            self.engine.unknown = 1
        else:
            self.engine = engine
        self.debug_printing = debug_printing

        self.query_result_label_extractor = query_result_label_extractor
예제 #23
0
파일: lfi.py 프로젝트: miguelvidex/ProbLog
def extract_evidence(pl):
    engine = DefaultEngine()
    atoms = engine.query(pl, Term('evidence', None, None))
    atoms1 = engine.query(pl, Term('evidence', None))
    atoms2 = engine.query(pl, Term('observe', None))
    for atom in atoms1 + atoms2:
        atom = atom[0]
        if atom.is_negated():
            atoms.append((-atom, Term('false')))
        else:
            atoms.append((atom, Term('true')))
    return [(at, str2bool(vl)) for at, vl in atoms]
예제 #24
0
def preprocessing_examples_keys(
        fname_examples: str, settings: FileSettings, internal_ex_format: InternalExampleFormat,
        fname_background_knowledge: Optional[str] = None,
        debug_printing_example_parsing=False,
        filter_out_unlabeled_examples = False, fold_data: Optional['FoldData'] = None) \
        -> Tuple[ExampleCollection, Term, int, List[Label], BackgroundKnowledgeWrapper]:
    prediction_goal_handler = settings.get_prediction_goal_handler(
    )  # type: KeysPredictionGoalHandler
    prediction_goal = prediction_goal_handler.get_prediction_goal(
    )  # type: Term

    background_knowledge_wrapper \
        = parse_background_knowledge_keys(fname_background_knowledge,
                                          prediction_goal)  # type: BackgroundKnowledgeWrapper

    full_background_knowledge_sp \
        = background_knowledge_wrapper.get_full_background_knowledge_simple_program()  # type: Optional[SimpleProgram]

    # EXAMPLES
    example_builder = KeysExampleBuilder(prediction_goal,
                                         debug_printing_example_parsing)
    training_examples_collection = example_builder.parse(
        internal_ex_format, fname_examples,
        full_background_knowledge_sp)  # type: ExampleCollection

    # ENGINE
    engine = DefaultEngine()
    #engine=GenericEngine()

    # LABELS
    index_of_label_var = prediction_goal_handler.get_predicate_goal_index_of_label_var(
    )  # type: int
    label_collector = LabelCollectorMapper.get_label_collector(
        internal_ex_format, prediction_goal, index_of_label_var, engine=engine)

    keys_of_unlabeled_examples = label_collector.extract_labels(
        training_examples_collection)

    nb_of_unlabeled_examples = len(keys_of_unlabeled_examples)
    # TODO: change this back if necessary
    if filter_out_unlabeled_examples and nb_of_unlabeled_examples > 0:
        if fold_data is not None:
            fold_data.total_nb_of_examples = len(
                training_examples_collection.example_wrappers_sp)
        training_examples_collection = training_examples_collection.filter_examples_not_in_key_set(
            keys_of_unlabeled_examples)
        print("DANGEROUS: FILTERED OUT UNLABELED EXAMPLES")

    possible_labels = label_collector.get_labels()  # type: Set[Label]
    possible_labels = list(possible_labels)  # type: List[Label]

    return training_examples_collection, prediction_goal, index_of_label_var, possible_labels, background_knowledge_wrapper
예제 #25
0
    def learn(self):
        '''Construisce gli oggetti problog per la valutazione dell'inferenza'''
        try:
            knowledge_str = ''
            for predicate in self.conoscenza_prob:
                knowledge_str += predicate + '\n'

            knowledge_str = PrologString(knowledge_str)
            self.problog = DefaultEngine()
            self.knowledge_database = self.problog.prepare(knowledge_str)

        except Exception as e:
            print(e)
예제 #26
0
파일: lfi.py 프로젝트: miguelvidex/ProbLog
    def _compile_examples(self):
        """Compile examples.
    
        :param examples: Output of ::func::`process_examples`.
        """
        logger = logging.getLogger('problog_lfi')

        baseprogram = DefaultEngine(**self.extra).prepare(self)
        examples = self._process_examples()
        result = []
        for example in examples:
            example.compile(self, baseprogram)
        self._compiled_examples = examples
예제 #27
0
def get_labels_single_example_models(example: SimpleProgram,
                                     rules: SimpleProgram,
                                     possible_labels: Iterable[str],
                                     background_knowledge=None,
                                     debug_printing=False) -> List[str]:
    """


    Classifies a single example and returns a list of its labels
    :param example:
    :param rules:
    :param possible_labels:
    :return:
    """
    eng = DefaultEngine()
    eng.unknown = 1

    if background_knowledge is not None:
        db = eng.prepare(background_knowledge)
        for statement in example:
            db += statement
        for rule in rules:
            db += rule
    else:
        db = eng.prepare(rules)
        for statement in example:
            db += statement

    if debug_printing:
        print('\nQueried database:')
        for statement in db:
            print('\t' + str(statement))
            # print('\n')

    result_list = []
    for label in possible_labels:
        db_to_query = db.extend()
        db_to_query += Term('query')(label)
        start_time = time.time()
        result = problog.get_evaluatable().create_from(db_to_query,
                                                       engine=eng).evaluate()
        end_time = time.time()
        print("call time:", end_time - start_time)

        if result[label] > 0.5:
            result_list.append(label)

    return result_list
예제 #28
0
    def test_nonground_query_ad(self):
        """Non-ground call to annotated disjunction"""

        program = """
            0.1::p(a); 0.2::p(b).
            query(p(_)).
        """

        engine = DefaultEngine()
        db = engine.prepare(PrologString(program))

        result = None
        for query in engine.query(db, Term("query", None)):
            result = engine.ground(db, query[0], result, label="query")

        found = [str(x) for x, y in result.queries()]

        self.assertCollectionEqual(found, ["p(a)", "p(b)"])
예제 #29
0
    def get_full_background_knowledge_clausedb(self, engine=None) -> ClauseDB:
        if self.full_background_knowledge_clausedb is not None:
            return self.full_background_knowledge_clausedb
        else:
            if engine is None:
                engine = DefaultEngine()
                engine.unknown = 1

            full_bg_kw = self.get_full_background_knowledge_simple_program()

            if full_bg_kw is not None:
                self.full_background_knowledge_clausedb = engine.prepare(
                    full_bg_kw)  # ClauseDB
                return self.full_background_knowledge_clausedb
            else:
                raise Exception(
                    "No sense in making an empty ClauseDB for an empty background knowledge"
                )
예제 #30
0
    def _run_sl_operators_on_semiring(self, givensemiring, program=None):
        engine = DefaultEngine()
        if program == None:
            program = self._slproblog_program

        db = engine.prepare(PrologString(program))
        semiring = givensemiring
        knowledge = get_evaluatable(None, semiring=semiring)
        formula = knowledge.create_from(db, engine=engine, database=db)
        res = formula.evaluate(semiring=semiring)

        ret = {}
        for k, v in res.items():
            if isinstance(semiring, BetaSemiring):
                ret[k] = moment_matching(semiring.parse(v))
            else:
                ret[k] = semiring.parse(v)

        return self._order_dicts(ret)