コード例 #1
0
ファイル: sdd_infer.py プロジェクト: danhlephuoc/lpmln
    def sddConstructorFromLPMLN(self,content,saveSDD = False, savedSDDName=None):
        self.content = content
        self.bytesContent = str.encode(self.content)
        self.saveSDD = saveSDD
        self.saveSDDName = savedSDDName
        fn_gringo = os.path.join(os.path.dirname(__file__), '../binSupport/gringo')
        p = subprocess.Popen([fn_gringo], shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
        p.stdin.write(self.bytesContent)
        gringo_out = p.communicate()[0]
        p.stdin.close()
        p.stdout.close()
        if self.args.verbosity > 4:
            print("Grounding Done! ")
        fn_cmodels = os.path.join(os.path.dirname(__file__), '../binSupport/cmodels')
        p = subprocess.Popen([fn_cmodels+' -cdimacs'], shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
        p.stdin.write(gringo_out)
        cmodels_out_Name = p.communicate()[0]
        p.stdin.close()
        p.stdout.close()
        #print("Completion_1 Done! ")



        p = subprocess.Popen([fn_cmodels+' -dimacs'], shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
        p.stdin.write(gringo_out)
        cmodels_out_no_name = p.communicate()[0]
        p.stdin.close()
        p.stdout.close()

        if self.args.verbosity > 4:
            print("Completion Done! ")

        gp = groundComp.groundComp(cmodels_out_Name.decode(), cmodels_out_no_name.decode())
        self.nameMappingDic = gp.atom2idx_gen()
        if self.args.verbosity > 4:
            print("Name Mapping Done!")

        start = time.time()
        self.sdd, self.formula = SddManager.from_cnf_string(cmodels_out_no_name.decode())

        if self.args.verbosity > 4:
            print("SDD Done! Time For Construct SDD: ", str(time.time() - start))

        if self.saveSDD:
            return self.save()
コード例 #2
0
add_rule1("smokes(b)", "stress(b)", "friends(b,a)", "aux(b,a)", "friends(b,c)", "aux(b,c)")
add_rule1("smokes(c)", "stress(c)", "friends(c,a)", "aux(c,a)", "friends(c,b)", "aux(c,b)")
add_rule2("aux(a,b)", "stress(b)", "friends(b,c)", "stress(c)")
add_rule2("aux(a,c)", "stress(c)", "friends(c,b)", "stress(b)")
add_rule2("aux(b,a)", "stress(a)", "friends(a,c)", "stress(c)")
add_rule2("aux(b,c)", "stress(c)", "friends(c,a)", "stress(a)")
add_rule2("aux(c,a)", "stress(a)", "friends(a,b)", "stress(b)")
add_rule2("aux(c,b)", "stress(b)", "friends(b,a)", "stress(a)")
# clauses.append([7]) # This adds stress(c) as evidence
cnf = "p cnf " + str(len(vars)) + " " + str(len(clauses)) + "\n" \
    + "c weights " + " ".join([str(weight) for weight in weights]) + "\n" \
    + "\n".join(list(map(tostr, clauses)))
# print(cnf)

# Test : generate SDD and do weighted model counting
(manager, sdd) = SddManager.from_cnf_string(cnf)
# manager.auto_gc_and_minimize_off()
wmc = sdd.wmc(log_mode=False)
wmc.set_literal_weights_from_array(weights_array)
w = wmc.propagate()
# print("Weighted model count: " + str(w))

# Get interpretations
interpretations = list(examples(DATA_FILE, cap=NB_EXAMPLES))

# Generate SDD for each example
counters = []
header_cnf = "p cnf " + str(len(vars)) + " " + str(len(clauses) + 6) + "\n" \
    + "\n".join(list(map(tostr, clauses)))
for interpretation in interpretations:
    (_, sdd) = SddManager.from_cnf_string(header_cnf + "\n" + "\n".join([str(p) + " 0" for p in interpretation]))