def model_config(self,predicate,formula,database,mln_path,db_path):
        """
        Returns the database and mln objects in MLN format
        --Inputs--
        predicate: predicate object with parsed predicates
        formula: formula object with parsed predicates
        database:.txt file containing the database(s)
        mln_path: .mln file name to save the learned weights per formula
        db_path: .db file to save the progress of the database learning
        """
        base_path = os.getcwd()
        mln = MLN(grammar='PRACGrammar',logic='FirstOrderLogic') #Parsing with PRACGrammar since we are using clusters
        for i in predicate:
            mln << i
            print('input predicate successful:'+i)
        for i in formula:
            mln << i
            print('input formula successful :'+i)

        mln.write()
        mln.tofile(base_path + '/'+ mln_path)

        db = Database.load(mln,database)
        #db.write()
        #db.tofile(base_path + '/'+ db_path)
        return (db,mln)
Пример #2
0
    def model_config(predicate, formula, database, mln_path,
                     db_path):  # mln_path,db_path 為string
        base_path = os.getcwd()
        mln = MLN(grammar='StandardGrammar', logic='FirstOrderLogic')
        for i in predicate:
            mln << i
            print('input predicate successful:' + i)
        for i in formula:
            mln << i
            print('input formula successful :' + i)
        mln.write()
        mln.tofile(base_path + '\\' + mln_path)  #把谓语数据储存成 mln_path.mln 档案
        db = Database(mln)
        try:
            for i in enumerate(database):
                db << i[1][1]
                print('input database successful : ' + i[1][0] + ' : ' +
                      i[1][1])
        except:
            for j in database[i[0]::]:
                db << j[1]

        db.write()
        db.tofile(base_path + '\\' + db_path)  #把证据数据储存成 db_path.db 档案
        return (db, mln)
Пример #3
0
    formulas = get_formulas(args.formula_file)
    domains = get_domains(roles, instances)
    # generates the markov logic network
    mln = MLN(logic="FirstOrderLogic", grammar="PRACGrammar")
    for domain, values in domains.items():  # domains
        for value in values:
            if len(value) > 1:
                const = ''.join(value)
            elif len(value) > 0:
                const = value[0]
            else:
                const = "None"
            mln.update_domain({domain: [const]})
    for role in roles.keys():  # predicates
        mln.predicate(Predicate(
            role, [role + "_d!"]))  # hard-functional constraints only
    for formula in formulas:  # formulas
        formula_str = "0.0 "
        for idx in range(len(formula)):
            role = formula[idx]
            if idx + 1 < len(formula):
                formula_str += role + "(+?" + role[:2] + ")" + " ^ "
            else:
                formula_str += role + "(+?" + role[:2] + ")"
        mln << formula_str
    mln.write()
    mln.tofile(args.output_mln)
    print("The initial MLN has been written to '" + args.output_mln + "'.")
    print("Verify the MLN using the print out above.")
    print("NOTE: Weights of '" + args.output_mln + "' have NOT been learned.")