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
Esempio n. 2
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
Esempio n. 4
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
Esempio n. 5
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
Esempio n. 6
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
Esempio n. 7
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 ===")
Esempio n. 8
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)