Exemple #1
0
    def problog(self, line, cell=None):
        """problog line/cell magic"""
        args = parse_argstring(self.problog, line)
        if args.knowledge == 'nnf':
            knowledge = DDNNF
        elif args.knowledge == 'sdd':
            knowledge = SDD
        elif args.knowledge is None:
            if SDD.is_available():
                knowledge = SDD
            else:
                knowledge = DDNNF
        else:
            error("Unknown option for --knowledge: '%s'" % args.knowledge)
        if args.logspace:
            semiring = SemiringLogProbability()
        else:
            semiring = None

        s = cell
        data = runproblog(s,
                          knowledge=knowledge,
                          semiring=semiring,
                          output='html')
        if data:
            display_html(data, raw=True)
 def evaluate_base(self, rule, engine, index=None):
     query_database = self.database.extend()
     for clause in rule.asLP(with_substitutions=True):
         query_database += clause
     gp = engine.ground_all(query_database)
     result = SDD.createFrom(gp).evaluate()
     return EvaluationResult(index, result)
 def build_sdd(self, q, test):
     i = 1 if test else 0
     ground = self.engine[i].ground_all(self.problog_model[i], queries=[q])
     sdd = SDD.create_from(ground)
     # print(sdd)
     # sdd.build_dd()
     # print(sdd)
     # shape = VectorShape(self, sdd)
     # semiring = SemiringGradient(self, shape)
     # evaluator = sdd.get_evaluator(semiring=semiring)
     return sdd
Exemple #4
0
def solve(model, semiring, operator):
    ev = SDD.create_from(model, label_all=True).to_formula(
    ) if semiring.is_dsp() else LogicDAG.create_from(model, label_all=True)
    fe = FormulaEvaluatorNSP(
        ev, semiring) if semiring.is_nsp() else FormulaEvaluator(ev, semiring)
    weights = ev.extract_weights(semiring=semiring)
    fe.set_weights(weights)
    result = {}

    for n, q in ev.queries():
        p = fe.evaluate(q)
        result[n] = (p, p.integrate(operator))
    return result
def call_theorem_prover(theorem_prover,
                        instance_id,
                        question_id,
                        theory,
                        assertion,
                        gold_label,
                        print_log=True):
    """Function that takes a single theory/assertion example and runs it through the theorem prover
    to obtain a label. Returns the obtained label, elapsed time to solve it, and exception returned
    by the engine, if any.
    """
    obtained_result = False
    millisecs_elapsed = 0
    if print_log: print("=======ORIGINAL THEORY=========")
    theory_as_txt = theory.program(theorem_prover)
    if print_log: print(theory_as_txt)
    theory.preprocess(theorem_prover)
    theory_as_txt = theory.program(theorem_prover)
    if theorem_prover == "problog":
        assertion_lf = assertion.logical_form(theorem_prover, False)
        assertion_lf = f"query({assertion_lf})."
        program = f"{theory_as_txt}\n{assertion_lf}"
        if print_log:
            print("=======PROGRAM FROM PREPROCESSED THEORY=========")
            print(program)
            print("=======EXPECTED LABEL=========")
            print(f"    {gold_label}")
        start_millisecs = current_milli_time()
        try:
            lf = LogicFormula.create_from(program)  # ground the program
            dag = LogicDAG.create_from(
                lf)  # break cycles in the ground program
            sdd = SDD.create_from(dag)
            result = sdd.evaluate()
            end_millisecs = current_milli_time()
            elapsed_millisecs = end_millisecs - start_millisecs
            result_tuples = [(k, v) for k, v in result.items()]
            obtained_result = result_tuples[0][1] != float(0)
            return obtained_result, elapsed_millisecs, None
        except (NegativeCycle, NonGroundProbabilisticClause,
                UnknownClause) as e:
            end_millisecs = current_milli_time()
            elapsed_millisecs = end_millisecs - start_millisecs
            if print_log:
                print(
                    f"!!!Encountered Exception at instance id {instance_id}, question id {question_id}: {e}"
                )
            exception_name = str(type(e)).lstrip("<class '").rstrip("'>")
            return None, elapsed_millisecs, exception_name
    return obtained_result, elapsed_millisecs, None
Exemple #6
0
def run_theory_in_problog(theory, assertion):
    """Run the given theory and assertion through ProbLog engine to obtain a True/False label.
    If an exception is encountered, return None so that this example will not be part of output."""
    theorem_prover = "problog"
    try:
        program = theory.program(theorem_prover, assertion)
        lf = LogicFormula.create_from(program)  # ground the program
        dag = LogicDAG.create_from(lf)  # break cycles in the ground program
        sdd = SDD.create_from(dag)
        result = sdd.evaluate()
        result_tuples = [(k, v) for k, v in result.items()]
        if len(result_tuples) == 0:
            return False
        return result_tuples[0][1] != 0.0
    except (NegativeCycle, NonGroundProbabilisticClause, UnknownClause) as e:
        return None
    return None
Exemple #7
0
def solve(model, semiring, operator):
    t = time.time()
    if semiring.is_dsp():
        ev = SDD.create_from(model, label_all=True).to_formula()
        # for n, q in diagram.queries():
        # diagram.get_manager().write_to_dot(diagram.get_inode(q), "examples/formula" + str(q) + ".gv")
    else:
        ev = LogicDAG.create_from(model, label_all=True)
    if semiring.is_nsp():
        fe = FormulaEvaluatorNSP(ev, semiring)
    else:
        fe = FormulaEvaluator(ev, semiring)
    weights = ev.extract_weights(semiring=semiring)
    fe.set_weights(weights)
    result = {}
    print("inference time: {}".format(time.time() - t))
    for n, q in ev.queries():
        p = fe.evaluate(q)
        result[n] = (p, p.integrate(operator))

    return result
Exemple #8
0
def solve(model, semiring):
    t = time.time()
    if semiring.is_dsp():
        ev = SDD.create_from(model, label_all=True).to_formula()
        print ev

        print "SDD time: {}".format(time.time() - t)
        exit(0)
    else:
        ev = LogicDAG.create_from(model, label_all=True)
    if semiring.is_nsp():
        fe = FormulaEvaluatorNSP(ev, semiring)
    else:
        fe = FormulaEvaluator(ev, semiring)

    weights = ev.extract_weights(semiring=semiring)
    # print weights
    fe.set_weights(weights)

    result = {}
    [(n, q)] = ev.queries()
    p = fe.evaluate(q)
    result[n] = p
    return result
Exemple #9
0
    def test(self) :
        try :
            parser = DefaultPrologParser(ExtendedPrologFactory())
            sdd = SDD.createFrom(PrologFile(filename, parser=parser))

            if logspace :
                semiring = SemiringLogProbability()
            else :
                semiring = SemiringProbability()

            computed = sdd.evaluate(semiring=semiring)
            computed = { str(k) : v for k,v in computed.items() }
        except Exception as err :
            e = err
            computed = None

        if computed is None :
            self.assertEqual(correct, type(e).__name__)
        else :
            self.assertIsInstance( correct, dict )
            self.assertSequenceEqual(correct, computed)

            for query in correct :
                self.assertAlmostEqual(correct[query], computed[query], msg=query)
Exemple #10
0
def main(filename, with_dot, knowledge):

    dotprefix = None
    if with_dot:
        dotprefix = os.path.splitext(filename)[0] + "_"

    model = PrologFile(filename)

    engine = DefaultEngine(label_all=True)

    with Timer("parsing"):
        db = engine.prepare(model)

    print("\n=== Database ===")
    print(db)

    print("\n=== Queries ===")
    queries = engine.query(db, Term("query", None))
    print("Queries:", ", ".join([str(q[0]) for q in queries]))

    print("\n=== Evidence ===")
    evidence = engine.query(db, Term("evidence", None, None))
    print("Evidence:", ", ".join(["%s=%s" % ev for ev in evidence]))

    print("\n=== Ground Program ===")
    with Timer("ground"):
        gp = engine.ground_all(db)
    print(gp)

    if dotprefix != None:
        with open(dotprefix + "gp.dot", "w") as f:
            print(gp.toDot(), file=f)

    print("\n=== Acyclic Ground Program ===")
    with Timer("acyclic"):
        gp = gp.makeAcyclic()
    print(gp)

    if dotprefix != None:
        with open(dotprefix + "agp.dot", "w") as f:
            print(gp.toDot(), file=f)

    if knowledge == "sdd":
        print("\n=== SDD compilation ===")
        with Timer("compile"):
            nnf = SDD.createFrom(gp)

        if dotprefix != None:
            nnf.saveSDDToDot(dotprefix + "sdd.dot")

    else:
        print("\n=== Conversion to CNF ===")
        with Timer("convert to CNF"):
            cnf = CNF.createFrom(gp)

        print("\n=== Compile to d-DNNF ===")
        with Timer("compile"):
            nnf = DDNNF.createFrom(cnf)

    if dotprefix != None:
        with open(dotprefix + "nnf.dot", "w") as f:
            print(nnf.toDot(), file=f)

    print("\n=== Evaluation result ===")
    with Timer("evaluate"):
        result = nnf.evaluate()

    for it in result.items():
        print("%s : %s" % (it))
Exemple #11
0
def main(argv, handle_output=None):
    args = argparser().parse_args(argv)

    verbose = args.verbose
    filename = args.filename

    if verbose:
        debug("Loading...")
    problog_model = PrologFile(filename, factory=ConstraintFactory())

    engine = DefaultEngine()
    database = engine.prepare(problog_model)

    # Ground the constraints
    if verbose:
        debug("Grounding...")
    target = engine.ground(database,
                           Term("constraint", None, None),
                           label="constraint")

    queries = [q[0] for q in engine.query(database, Term("query", None))]
    for query in queries:
        target = engine.ground(database, query, label="query", target=target)

    if verbose > 1:
        print(target, file=sys.stderr)

    has_prob_constraint = False
    for name, index in target.get_names(label="constraint"):
        if index is not None:
            if name.args[1].functor == "ensure_prob":
                has_prob_constraint = True

    # Compile and turn into CP-problem
    if has_prob_constraint:
        if verbose:
            debug("Probabilistic constraints detected.")

        if verbose:
            debug("Compiling...")
        sdd = SDD.create_from(target)
        formula = sdd.to_formula()
        # Convert to flatzinc
        if verbose:
            debug("Converting...")
        fzn = formula_to_flatzinc_float(formula)
        sdd.clear_labeled("constraint")

        if verbose > 1:
            print(fzn, file=sys.stderr)

        # Solve the flatzinc model
        if verbose:
            debug("Solving...")
        sols = list(solve(fzn))

        has_solution = False
        for i, res in enumerate(sols):
            if verbose:
                debug("Evaluating solution %s/%s..." % (i + 1, len(sols)))
            # target.lookup_evidence = {}
            weights = sdd.get_weights()
            # ev_nodes = []
            for k, v in res.items():
                sddvar = formula.get_node(k).identifier
                sddatom = sdd.var2atom[sddvar]
                sddname = sdd.get_name(sddatom)
                weights[sddatom] = v
            for k, v in sdd.evaluate().items():
                if v > 0.0:
                    print(k, v)
            print("----------")
            has_solution = True
        if has_solution:
            print("==========")
        else:
            print("=== UNSATISFIABLE ===")

    else:
        formula = LogicDAG()
        break_cycles(target, formula)

        if verbose:
            debug("Converting...")
        fzn = formula_to_flatzinc_bool(formula)

        if verbose > 1:
            print(fzn, file=sys.stderr)

        if verbose:
            debug("Solving...")
        sols = list(solve(fzn))

        has_solution = False
        for i, res in enumerate(sols):
            if verbose:
                debug("Evaluating solution %s/%s..." % (i + 1, len(sols)))

            if verbose:
                debug("Compressing...")
            new_formula = compress(formula, res)

            if verbose:
                debug("Compiling...")
            sdd = SDD.create_from(new_formula)

            if verbose:
                debug("Evaluating...")
            for k, v in sdd.evaluate().items():
                if v > 0.0:
                    print(k, v)
            print("----------")
            has_solution = True
        if has_solution:
            print("==========")
        else:
            print("=== UNSATISFIABLE ===")
Exemple #12
0
    return new_weights, iters


# Read in the progam with tunable parameters
#ground_progam = file_to_string("test_learn.pl")

# Assign to each tunable parameter a random initialization value
#ground_progam_tunable = init_tunable(ground_progam)

# Write back the prolog file with random initialized tunable probabilities
#f = open('test_learn_tunable.pl', 'w')
#f.write(ground_progam_tunable)

pf = PrologFile("test_learn_tunable.pl")
formula = LogicFormula.create_from(pf)
sdd = SDD.create_from(formula)

queries = [
    Term('stress', Term('a')),
    Term('stress', Term('b')),
    Term('stress', Term('c')),
    Term('smokes', Term('a')),
    Term('smokes', Term('b')),
    Term('smokes', Term('c'))
]

# File which holds all the evidence examples
examples = file_to_string('data.pl')

# Retrieve the amount of interpretations that has been specified
interpretations_100 = get_interpretations(examples, 100)
Exemple #13
0
import sys
import os

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
sys.path.append(os.path.join(os.path.dirname(__file__), '../examples'))

from problog.logic import Term
from problog.program import PrologString, ExtendedPrologFactory, LogicProgram
from problog.evaluator import SemiringLogProbability
from problog.parser import DefaultPrologParser
from problog.ddnnf_formula import DDNNF
from problog.sdd_formula import SDD
import problog.tasks.sample as plsample

if SDD.is_available():
    knowledge_default = SDD
else:
    knowledge_default = DDNNF

from IPython.core.display import display_html
from IPython.core.magic import (Magics, magics_class, line_magic,
                                line_cell_magic)
from IPython.core.magic_arguments import (argument, magic_arguments,
                                          parse_argstring)
import IPython
if IPython.version_info[0] >= 6:
    import warnings

    def info(message):
        warnings.warn('WARNING: ' + message)