示例#1
0
def main(filename, examplefile):
    try:
        examples = list(lfi.read_examples(examplefile))
        program = PrologFile(filename)
        score, weights, names, iterations = lfi.run_lfi(program, examples)

        new_names = []
        for n in names:
            new_names.append((str(n.with_probability()), ) +
                             program.lineno(n.location)[1:])

        return True, (score, weights, new_names, iterations)
    except Exception as err:
        return False, {"err": process_error(err)}
示例#2
0
def main(file):
    pl = PrologFile(file)

    result = solve(pl, CachetSemiring())

    for k, v in result.items():
        print('(%s): %s' % (k, v))
def parse_examples_key_format_with_key(
    file_name_labeled_examples: str,
    prediction_goal: Optional[Term] = None
) -> List[SimpleProgramExampleWrapper]:

    prediction_goal_functor = prediction_goal.functor

    examples_found = {}  # type: Dict[Term, SimpleProgramExampleWrapper]

    file = PrologFile(file_name_labeled_examples)  # type: PrologFile

    for prolog_statement in file:  # type: Term
        example_key = prolog_statement.args[0]
        if example_key not in examples_found:

            new_example = SimpleProgramExampleWrapper()
            new_example.key = example_key  # type: Term

            if prolog_statement.functor.startswith(prediction_goal_functor):
                new_example.classification_term = prolog_statement
            else:
                new_example += prolog_statement
            examples_found[example_key] = new_example
        else:
            if prolog_statement.functor.startswith(prediction_goal_functor):
                examples_found[
                    example_key].classification_term = prolog_statement
            else:
                examples_found[example_key] += prolog_statement

    return list(examples_found.values())
示例#4
0
def main_mpe_semiring(args):
    inputfile = args.inputfile

    init_logger(args.verbose)

    if args.web:
        result_handler = print_result_json
    else:
        result_handler = print_result

    if args.output is not None:
        outf = open(args.output, 'w')
    else:
        outf = sys.stdout

    with Timer("Total"):
        try:
            pl = PrologFile(inputfile)

            lf = LogicFormula.create_from(model, label_all=True)

            prob, facts = mpe_semiring(lf, args.verbose)
            result_handler((True, (prob, facts)), outf)

        except Exception as err:
            trace = traceback.format_exc()
            err.trace = trace
            result_handler((False, err), outf)
示例#5
0
def parse_background_knowledge_keys(
        file_name: Optional[str] = None,
        prediction_goal: Optional[Term] = None) -> BackgroundKnowledgeWrapper:
    if file_name is None:
        return BackgroundKnowledgeWrapper()

    logic_program = PrologFile(file_name)

    if prediction_goal is not None:
        prediction_goal_functor = prediction_goal.functor  # type: str

        found_a_prediction_goal_clause = False

        prediction_goal_clauses = SimpleProgram()
        stripped_logic_program = SimpleProgram()

        for prolog_statement in logic_program:
            if str(prolog_statement).startswith(prediction_goal_functor):
                found_a_prediction_goal_clause = True
                prediction_goal_clauses += prolog_statement
            else:
                stripped_logic_program += prolog_statement

        if found_a_prediction_goal_clause:
            return BackgroundKnowledgeWrapper(
                logic_program=stripped_logic_program,
                prediction_goal_clauses=prediction_goal_clauses)
        else:
            return BackgroundKnowledgeWrapper(logic_program=logic_program)
    else:
        return BackgroundKnowledgeWrapper(logic_program=logic_program)
示例#6
0
    def evaluate_explicit_from_fsdd(self, custom_semiring=None):
        try:
            parser = DefaultPrologParser(ExtendedPrologFactory())
            lf = PrologFile(filename, parser=parser)
            kc = _ForwardSDD.create_from(lf)  # type: _ForwardSDD
            kc = kc.to_explicit_encoding()

            if custom_semiring is not None:
                semiring = custom_semiring  # forces the custom semiring code.
            elif logspace:
                semiring = SemiringLogProbability()
            else:
                semiring = SemiringProbability()

            computed = kc.evaluate(semiring=semiring)
            computed = {str(k): v for k, v in computed.items()}
        except Exception as err:
            #print("exception %s" % 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)
示例#7
0
def main_mpe_maxsat(args):
    inputfile = args.inputfile

    if args.web:
        result_handler = print_result_json
    else:
        result_handler = print_result

    if args.output is not None:
        outf = open(args.output, 'w')
    else:
        outf = sys.stdout

    with Timer("Total"):
        try:
            pl = PrologFile(inputfile)

            dag = LogicDAG.createFrom(pl,
                                      avoid_name_clash=True,
                                      label_all=True,
                                      labels=[('output', 1)])

            prob, output_facts = mpe_maxsat(dag,
                                            verbose=args.verbose,
                                            solver=args.solver)

            result_handler((True, (prob, output_facts)), outf)
        except Exception as err:
            trace = traceback.format_exc()
            err.trace = trace
            result_handler((False, err), outf)

    if args.output is not None:
        outf.close()
示例#8
0
def main(argv, result_handler=None):
    parser = argparser()
    args = parser.parse_args(argv)

    if result_handler is None:
        if args.web:
            result_handler = print_result_json
        else:
            result_handler = print_result

    knowledge = get_evaluatable(args.koption)

    if args.output is None:
        outf = sys.stdout
    else:
        outf = open(args.output, 'w')

    create_logger('problog_lfi', args.verbose)
    create_logger('problog', args.verbose - 1)

    program = PrologFile(args.model)
    examples = list(read_examples(*args.examples))
    if len(examples) == 0:
        logging.getLogger('problog_lfi').warn('no examples specified')
    else:
        logging.getLogger('problog_lfi').info('Number of examples: %s' %
                                              len(examples))
    options = vars(args)
    del options['examples']

    try:
        results = run_lfi(program, examples, knowledge=knowledge, **options)

        for n in results[2]:
            n.loc = program.lineno(n.location)
        retcode = result_handler((True, results), output=outf)
    except Exception as err:
        trace = traceback.format_exc()
        err.trace = trace
        retcode = result_handler((False, err), output=outf)

    if args.output is not None:
        outf.close()

    if retcode:
        sys.exit(retcode)
示例#9
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))
示例#10
0
def main(argv=sys.argv[1:]):
    types = {}    # dict[tuple[str,int],tuple[str]]: signature / argument types
    values = defaultdict(set)   # dict[str, set[Term]]: values in data for given type
    groundings = defaultdict(set)
    modes = []   
    # pdb.set_trace()
    args = argparser().parse_args(argv)
    # yahan load input files and create database
    data = DataFile(*(PrologFile(source) for source in args.files))
    print(data._database)
    for typeinfo in data.query('base', 1):
        typeinfo = typeinfo[0]
        argtypes = list(map(str, typeinfo.args))
        key = (typeinfo.functor, len(argtypes))
        if key in types:
            raise ValueError("A type definition already exists for '%s/%s'."
                             % (typeinfo.functor, len(argtypes)))
        else:
            types[key] = argtypes
    print(types)

    for modeinfo in data.query('mode', 1):
        modeinfo = modeinfo[0]
        modes.append((modeinfo.functor, list(map(str, modeinfo.args))))
    #     print(modes)
    # print(types.items())
    for predicate, type_el in types.items():
        arg_values = zip(*data.query(*predicate))
        for a, t in zip(arg_values, type_el):
            for value in a:
                values[predicate].add(value)
                groundings[t].add(value) 
    # print(values)
    # print(groundings) #predicate name, arity is the key and value: actual groundings
    # grounding is also a dicyionary
    # print(values)
    # print("Types:", types)

    target = data.query('learn', 1)[0]
    target_name, target_arity = target[0].args
    Literals_List = []
    for i in values.keys():
        name = i[0]
        arity = i[1]
        g = tuple(values[i])
        print(g)
        # print(name, arity, g)
        g1 = [r for r in data.query(name, arity)]
        # print(g1)
        temp = Predicate(name, arity, g1, data.evaluate(None, name, g1, None), types[i])
        Literals_List.append(temp)
    prev_list = []
    for i in Literals_List:
        if i.name == target_name:
            prev_list = i.pi_dict.values()
示例#11
0
def main(argv, result_handler=None):
    parser = argparser()
    args = parser.parse_args(argv)
    init_logger(args.verbose)

    outfile = sys.stdout
    if args.output:
        outfile = open(args.output, 'w')
    target = LogicDAG  # Break cycles
    print_result = print_result_standard

    try:
        gp = target.createFrom(
            PrologFile(args.filename,
                       parser=DefaultPrologParser(ExtendedPrologFactory())),
            label_all=True,
            avoid_name_clash=False,
            keep_order=True,  # Necessary for to prolog
            keep_all=args.keep_all,
            keep_duplicates=False,  # args.keep_duplicates,
            hide_builtins=args.hide_builtins)
        # rc = print_result((True, gp), output=outfile)
        # p = PrologFile(args.filename)
        # engine = DefaultEngine(label_all=True, avoid_name_clash=True, keep_order=True,
        #                        keep_duplicates=False, keep_all=True)
        # db = engine.prepare(p)
        # gp = engine.ground_all(db)

        bn = formula_to_bn(gp)
        if args.format == 'hugin':
            bn_str = bn.to_hugin_net()
        elif args.format == 'xdsl':
            bn_str = bn.to_xdsl()
        elif args.format == 'uai08':
            bn_str = bn.to_uai08()
        elif args.format == 'dot':
            bn_str = bn.to_graphviz()
        else:
            bn_str = str(bn)
        rc = print_result((True, bn_str), output=outfile)

    except Exception as err:
        import traceback
        err.trace = traceback.format_exc()
        rc = print_result((False, err))

    if args.output:
        outfile.close()

    if rc:
        sys.exit(rc)
示例#12
0
 def test_learning(self):
     structure_learner = StructureLearner(PrologFile('surfing.data'))
     time_total = structure_learner.learn()
     print('================= FINAL THEORY =================')
     for rule in structure_learner.get_learned_rules():
         print(rule)
     print('==================== SCORES ====================')
     print('            Accuracy:\t', structure_learner.accuracy())
     print('           Precision:\t', structure_learner.precision())
     print('              Recall:\t', structure_learner.recall())
     print('================== STATISTICS ==================')
     for name, value in structure_learner.get_statistics():
         print('%20s:\t%s' % (name, value))
     print('          Total time:\t%.4fs' % time_total)
示例#13
0
def main(inputfile, semiring, **kwdargs):

    pl = PrologFile(inputfile)

    if semiring == 'prob':
        sm = SemiringProbability()
    elif semiring == 'mpe':
        sm = SemiringMPE()
    elif semiring == 'mpe_state':
        sm = SemiringMPEState()

    result = solve(pl, semiring=sm)

    for k, v in result.items():
        print ('%s: %s' % (k, v))
示例#14
0
 def test_learning_with_logfile(self):
     structure_learner = StructureLearner(PrologFile('surfing.data'),
                                          log_file="log.txt")
     time_total = structure_learner.learn(max_rule_length=1,
                                          significance=0.5,
                                          beam_size=20)
     print('================= FINAL THEORY =================')
     for rule in structure_learner.get_learned_rules():
         print(rule)
     print('==================== SCORES ====================')
     print('            Accuracy:\t', structure_learner.accuracy())
     print('           Precision:\t', structure_learner.precision())
     print('              Recall:\t', structure_learner.recall())
     print('================== STATISTICS ==================')
     for name, value in structure_learner.get_statistics():
         print('%20s:\t%s' % (name, value))
     print('          Total time:\t%.4fs' % time_total)
示例#15
0
    def test(self):
        try:
            target = LogicDAG  # Break cycles
            gp = target.createFrom(
                PrologFile(filename, parser=DefaultPrologParser(ExtendedPrologFactory())),
                label_all=True, avoid_name_clash=False, keep_order=True,  # Necessary for to prolog
                keep_duplicates=False)
            bn = formula_to_bn(gp)
            computed = str(bn).strip()
        except Exception as err :
            e = err
            computed = None

        if computed is None :
            self.assertEqual(correct, type(e).__name__)
        else :
            self.assertIsInstance(computed, str)
            self.assertEqual(correct, computed)
示例#16
0
def main_mpe_maxsat(args):
    inputfile = args.inputfile

    if args.web:
        result_handler = print_result_json
    else:
        result_handler = print_result

    if args.output is not None:
        outf = open(args.output, "w")
    else:
        outf = sys.stdout

    with Timer("Total"):
        try:
            pl = PrologFile(inputfile)

            # filtered_pl = SimpleProgram()
            # has_queries = False
            # for statement in pl:
            #     if 'query/1' in statement.predicates:
            #         has_queries = True
            #     else:
            #         filtered_pl += statement
            # if has_queries:
            #     print('%% WARNING: ignoring queries in file', file=sys.stderr)

            dag = LogicDAG.createFrom(
                pl, avoid_name_clash=True, label_all=True, labels=[("output", 1)]
            )

            prob, output_facts = mpe_maxsat(
                dag, verbose=args.verbose, solver=args.solver, minpe=args.minpe
            )

            result_handler((True, (prob, output_facts)), outf)
        except Exception as err:
            trace = traceback.format_exc()
            err.trace = trace
            result_handler((False, err), outf)

    if args.output is not None:
        outf.close()
示例#17
0
def estimate( filename, N=1 ) :
    from collections import defaultdict
    pl = PrologFile(filename)
    
    engine = DefaultEngine()
    db = engine.prepare(pl)
    
    estimates = defaultdict(float)
    counts = 0.0
    for i in range(0, N) :
        result = engine.ground_all(db, target=SampledFormula())
        for k, v in result.queries() :
            if v == 0 :
                estimates[k] += 1.0
        counts += 1.0

    for k in estimates :
        estimates[k] = estimates[k] / counts
    return estimates
示例#18
0
def estimate(filename, N=1):
    from collections import defaultdict

    pl = PrologFile(filename)

    engine = SamplingEngine(builtins=True)
    db = engine.prepare(pl)

    estimates = defaultdict(float)
    counts = 0.0
    for i in range(0, N):
        queries, facts, prob = engine.sample(db)
        for k, v in queries:
            if v:
                estimates[k] += 1.0
        counts += 1.0

    for k in estimates:
        estimates[k] = estimates[k] / counts
    return estimates
示例#19
0
    def run_learning(self, out_file_name=None, **kargs):
        """ Runs ProbFoil and process the output

            Params:
                out_file_name: [optional] name of the output file to be generated in the experiment folder
            
            If there is no out_file_name this method is going to print the output
        """
        to_str = True if out_file_name else False
        if self._has_necessary_files():
            logger.debug('%s <- %s',
                         self.name,
                         self.get_files())

            data = DataFile(*(PrologFile(source) for source in self.get_files()))
            if kargs.get('probfoil1', False):
                learn_class = probfoil.ProbFOIL
            else:
                learn_class = probfoil.ProbFOIL2

            results = {}
            try:
                learner = learn_class(data)
                hypothesis = learner.learn()
                rules = hypothesis.to_clauses(hypothesis.target.functor)
                skiped_rules = 1 if len(rules) > 1 else 0

                results['rules'] = [str(r) for r in rules[skiped_rules:]]
                results['accuracy'] = accuracy(hypothesis)
                results['precision'] = precision(hypothesis)
                results['recall'] = recall(hypothesis)
            except Exception as ex:  # TODO use a more specific exception
                logger.exception(ex)

            if out_file_name:
                with open(path.join(self.dir, out_file_name), 'w') as out_stream:
                    dump(results, out_stream)
            else:
                print results
        else:
            logger.debug('Empty dir: %s', self.dir)
示例#20
0
def parse_background_knowledge_models(
    file_name: Optional[str] = None,
    possible_labels: Optional[Iterable[Term]] = None
) -> BackgroundKnowledgeWrapper:
    if file_name is None:
        return BackgroundKnowledgeWrapper()

    logic_program = PrologFile(file_name)

    if possible_labels is not None:
        possible_labels_str = [str(label)
                               for label in possible_labels]  # type: List[str]

        found_a_prediction_clause = False

        prediction_goal_clauses = SimpleProgram()
        stripped_logic_program = SimpleProgram()

        for prolog_statement in logic_program:
            is_prediction_clause = False
            for possible_label_str in possible_labels_str:
                if str(prolog_statement).startswith(possible_label_str):
                    is_prediction_clause = True
                    found_a_prediction_clause = True
                    break

            if is_prediction_clause:
                prediction_goal_clauses += prolog_statement
            else:
                stripped_logic_program += prolog_statement

        if found_a_prediction_clause:
            return BackgroundKnowledgeWrapper(
                logic_program=stripped_logic_program,
                prediction_goal_clauses=prediction_goal_clauses)
        else:
            return BackgroundKnowledgeWrapper(logic_program=logic_program)
    else:
        return BackgroundKnowledgeWrapper(logic_program=logic_program)
示例#21
0
def generate_interpretations(problog_filename, m):
    pl = PrologFile(problog_filename)
    interpretations = []

    for interpretation in sample(pl, n=m, as_evidence=True):
        temp = []

        # drop each observation with a probability of 30%
        for i in interpretation.splitlines():
            if random.random() <= 0.7:
                temp.append(i)

        interpretations.append("\n".join(temp))

    interpretations_file = os.path.abspath(
        os.path.join(os.path.dirname(__file__), "files",
                     "interpretations.txt"))
    with open(interpretations_file, "w") as out:
        sep = os.linesep + separator_1 + os.linesep
        out.write(sep.join(interpretations))

    return interpretations
示例#22
0
def sample(filename, N=1, with_facts=False, oneline=False):
    pl = PrologFile(filename)

    engine = SamplingEngine(builtins=True)
    db = engine.prepare(pl)

    for i in range(0, N):
        queries, facts, probability = engine.sample(db)
        lines = []
        for k, v in queries:
            if v:
                lines.append('%s.' % k)

        if with_facts:
            for f in facts:
                lines.append('%s.' % f)

        if oneline:
            print(' '.join(lines), '%% P=%.4g' % probability)
        else:
            print('====================')
            print('\n'.join(lines))
            print('%%Probability: %.4g' % probability)
示例#23
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)
示例#24
0
def main(filename, output):

    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)

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

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

    with open(output, "w") as f:
        f.write(cnf.to_dimacs(weighted=False, names=True))
示例#25
0
from problog.program import SimpleProgram, PrologString, LogicProgram, PrologFile
from problog.logic import Constant, Var, Term, AnnotatedDisjunction
from problog import get_evaluatable

true = Term('true')
ok = Term('ok')
query = Term('query')

p = PrologFile("Try.pl")
x = [p]
for i in range(0, 2):
    s = SimpleProgram()
    brand = Term('brand')
    ind = Term('ind')
    prop = Term('prop')
    query = Term('query')
    X = Var('X')
    ford = Term('ford')
    seat = Term('seat')
    suzuki = Term('suzuki')
    audi = Term('audi')
    fiat = Term('fiat')
    s = SimpleProgram()
    s += AnnotatedDisjunction([
        prop(ind, brand, ford, p=0.222222222222222),
        prop(ind, brand, seat, p=0.222222222222222),
        prop(ind, brand, suzuki, p=0.222222222222222),
        prop(ind, brand, audi, p=0.222222222222222),
        prop(ind, brand, fiat, p=0.111111111111111)
    ], true)
    if i == 1:
示例#26
0
def matchAccuracy(fold, person, ruleDir, precedence):
	
	trainDataFile = DataFile(PrologFile(mainDir + '/cv' + str(fold) + '_train_' + str(person) + '.pl'))
	#trainFile = open(mainDir + '/cv' + str(fold) + '_train_' + str(person) + '.pl', 'r')
	trainFacts = []
	trainAnswers = []
	trainSyllogisms = set()
	acCount = 0
	caCount = 0
	for line in trainDataFile._database._ClauseDB__nodes:
		if hasattr(line, 'probability') and line.probability == None and str(line.functor) not in ['base', 'mode']:
			if str(line.functor)[0] == 'r':
				trainAnswers.append((line.functor, line.args))
				if str(line.args[0])[0] == "a":
					acCount += 1
				else:
					caCount += 1
			else:
				trainSyllogisms.add(int(str(line.args[0])[1:]))
				trainFacts.append((line.functor, line.args))

	#print("acCount \t:" + str(acCount))
	#print("caCount \t:" + str(caCount))
	#Get the most frequent response of the users in train dataset
	occurence_count = Counter([functor for (functor, args) in trainAnswers])
	#print("trainAnswers \t:" + str(trainAnswers))
	#print(occurence_count)
	mostFrequentResponse = occurence_count.most_common(1)[0][0]

	#Get the most frequent response of the users in train dataset at predicate level
	#Response Dict = {id: Fact1, Fact2, Response}
	responseDict = {}
	for (functor, args) in trainAnswers:
		id = str(args[0])[1:]
		responseDict[id] = [str(functor)]

	for (functor, args) in trainFacts:
		id = str(args[0])[1:]
		responseDict[id].append(str(functor))

	#print('Train Answers \t: '  + str(trainAnswers) +'\n\n')
	#print('Train Facts \t: '  + str(trainFacts) +'\n\n')
	#print('Response Dict \t: '  + str(responseDict) +'\n\n')

	#Most Frequent Response Dict = {Predcate: (MostFrequentResponseForPredicate, NumberOfOccurances)}

	frequentResponseDict = {}
	for id, p in responseDict.items():
		
		(predicate1, predicate2, predicate3) = p

		if predicate1[0] == 'r':
			premises = [predicate2, predicate3]
			response = predicate1
		elif predicate2[0] == 'r':
			premises = [predicate1, predicate3]
			response = predicate2
		elif predicate3[0] == 'r':
			premises = [predicate1, predicate2]
			response = predicate3

		for premise in premises:
			if premise not in frequentResponseDict:
				frequentResponseDict[premise]= {}
				frequentResponseDict[premise][response] = 1
			else:
				if response not in frequentResponseDict[premise]:
					frequentResponseDict[premise][response] = 1
				else:
					frequentResponseDict[premise][response] += 1

	mostFrequentResponseDict = {}
	for premise, value in frequentResponseDict.items():
		maxFreq = 0
		for response, frequency in value.items():
			if frequency > maxFreq:
				mode = [response]
				maxFreq = frequency
			elif frequency == maxFreq:
				mode.append(response)
		mostFrequentResponseDict[premise] = (mode, maxFreq)

	learn = False		# When learn = True, run ProbFOIL to learn rules from scratch. 
						# Otherwise read learned rules from out files.
	responses = ['rall', 'rsome', 'rno', 'rsomenot', 'rnvc']
	rules = []

	if learn == True:
		def callProbfoil(response):
			print('Probfoil started for '+ response + str(person) + '/2')
			pf = ProbFOIL2(trainDataFile, target = response + str(person) + '/2')
			print('Probfoil ended for '+ response + str(person) + '/2')
			return pf.learn()

		pool = Pool(processes = 5)
		ruleList = pool.map(callProbfoil, responses)

	emptyResponses = []
	for i, response in enumerate(responses):
		if learn == True:
			hypothesis = ruleList[i]
			while str(hypothesis)[-4:] != 'fail':
				rules.append(str(hypothesis)+'.')
				hypothesis = hypothesis.previous
		else:
			pf = open(ruleDir + '/cv' + str(fold) + '_' + response + str(person) + '.out').readlines()[12:-7]
			if pf == []:
				emptyResponses.append(response)
			for rule in pf:
				rules.append(rule[:-1]+'.')
		

	testFile = DataFile(PrologFile(mainDir + "/cv" + str(fold) + "_test_" + str(person) + ".pl"))._database._ClauseDB__nodes

	testFacts = []
	testAnswers = {}
	testPremises = {}
	for line in testFile:
		if hasattr(line, 'probability') and line.probability == None and str(line.functor) != 'base':
			if str(line.functor)[0] == 'r':
				id = int(str(line.args[0])[1:])
				testAnswers[id] = str(line.functor) + '(' + str(line.args[0]) + ',' + str(line.args[1]) + ')'
			else:
				testFacts.append((line.functor, line.args))
				id = int(str(line.args[0])[1:])
				if id not in testPremises:
					testPremises[id] = set([str(line.functor)])
				else:
					testPremises[id].add(str(line.functor))


	testSyllogisms = []
	for i in range(1, 65):
		if i not in trainSyllogisms:
			testSyllogisms.append(i)

	queries = []
	for response in responses:
		for id in testSyllogisms:
			if response in emptyResponses:
				continue
			if response == 'rnvc':
				queries.append('query(rnvc' + str(person) + '(a' + str(id) + ', c' + str(id) + ')).')
			else:
				queries.append('query(' + response + str(person) + '(c' + str(id) + ', a' + str(id) + ')).')
				queries.append('query(' + response + str(person) + '(a' + str(id) + ', c' + str(id) + ')).')

	init = []
	for response in responses:
		init.append('0::' + response[1:] + str(person) + '(a,b).')

	problogQuery = '\n'.join(init)
	problogQuery += '\n'
	problogQuery += '\n'.join([str(item[0])+str(item[1])+'.' for item in testFacts])
	problogQuery += '\n'
	problogQuery += '\n'.join(rules)
	problogQuery += '\n'
	problogQuery += '\n'.join(queries)

	p = PrologString(problogQuery)
	inferences = get_evaluatable().create_from(p).evaluate()

	inferenceDict = {}
	for term, probability in inferences.items():
		inferenceDict[str(term)] = probability
		#if probability > 1 - 1e-8:
			#print('Fact inferred \t:' + str(term))
	#Get predictions from inferences by fitting in the most frequence response

	testPredictions = {}
	total = 0
	tp_person = 0.0
	tn_person = 0.0
	fp_person = 0.0
	fn_person = 0.0
	match = 0
	for id in testSyllogisms:
		premises = testPremises[id]

		possibleResponses = []
		for response in responses:
			if response == 'rnvc':
				possibleResponses.append('rnvc' + str(person) + '(a' + str(id) + ',c' + str(id) + ')')
			else:
				if caCount >= acCount:
					possibleResponses.append(response + str(person) + '(c' + str(id) + ',a' + str(id) + ')')
					possibleResponses.append(response + str(person) + '(a' + str(id) + ',c' + str(id) + ')')
				else:
					possibleResponses.append(response + str(person) + '(a' + str(id) + ',c' + str(id) + ')')
					possibleResponses.append(response + str(person) + '(c' + str(id) + ',a' + str(id) + ')')
		# Prediction starts now
			# If exactly 1 possible response has probability = 1, predict that.
			# If more than 1 possible responses has probability = 1, predict one of those through the following tie-breaker:
				# 1. Take intersection with the most frequent responses for the premises and predict that if there's only one.


		set1 = set() # Set of all possible responses with 1 probability.
		for possibleResponse in possibleResponses:
			if possibleResponse.split('(')[0][:-1*len(str(person))] in emptyResponses:
				continue
			if possibleResponse not in inferenceDict:
				print('Error:' + possibleResponse + ' not in the inference dict: ' + str(inferenceDict))
			if inferenceDict[possibleResponse] > 1 - 1e-8:
				set1.add(possibleResponse)
		
		#TO DO: Add arguments to the premises in set2
		set2 = set() # Set of most frequent response by the user for the 2 premises
		set2_withoutArgs = set()
		maxFreq = 0
		for premise in premises:
			mode, freq = mostFrequentResponseDict[premise]
			if freq > maxFreq:
				set2_withoutArgs = set(mode)
			elif freq == maxFreq:
				set2_withoutArgs = set2_withoutArgs|mode

		for predicate in set2_withoutArgs:
			if predicate[:4] == "rall":
				set2.add('rall' + str(person) + '(c' + str(id) + ',a' + str(id) + ')')
				set2.add('rall' + str(person) + '(a' + str(id) + ',c' + str(id) + ')')
			elif predicate[:3] == "rno":
				set2.add('rno' + str(person) + '(c' + str(id) + ',a' + str(id) + ')')
				set2.add('rno' + str(person) + '(a' + str(id) + ',c' + str(id) + ')')
			elif predicate[:8] == "rsomenot":
				set2.add('rsomenot' + str(person) + '(c' + str(id) + ',a' + str(id) + ')')
				set2.add('rsomenot' + str(person) + '(a' + str(id) + ',c' + str(id) + ')')
			elif predicate[:5] == "rsome":
				set2.add('rsome' + str(person) + '(c' + str(id) + ',a' + str(id) + ')')
				set2.add('rsome' + str(person) + '(a' + str(id) + ',c' + str(id) + ')')
			elif predicate[:4] == "rnvc":
				set2.add('rnvc' + str(person) + '(a' + str(id) + ',c' + str(id) + ')')
				

		set3 = set() # Set of most frequent response (overall) for the user
		if mostFrequentResponse == 'rnvc' + str(person):
			set3.add(mostFrequentResponse + '(a' + str(id) + ',c' + str(id) + ')')
		else:
			set3.add(mostFrequentResponse + '(c' + str(id) + ',a' + str(id) + ')')
			set3.add(mostFrequentResponse + '(a' + str(id) + ',c' + str(id) + ')')

		set4 = set() # Set of the most frequent response for the premise in the training data of the fold

		# Most Frequent Reponses in Training fold = [{'all':'rsome', 'no':'rnvc', 'some':'rsome', 'somenot':'rnvc'}]
		
		for premise in premises:
			if premise[:2] == "no" or premise[:7] == "somenot":
				set4.add('rnvc' + str(person) + '(a' + str(id) + ',c' + str(id) + ')')
			elif premise[:3] == "all" or premise[:4] == "some":
				set4.add('rsome' + str(person) + '(c' + str(id) + ',a' + str(id) + ')')
				set4.add('rsome' + str(person) + '(a' + str(id) + ',c' + str(id) + ')')

		set5 = set(['rnvc' + str(person) + '(a' + str(id) + ',c' + str(id) + ')']) # NVC

		#print('Set1 \t: ' + str(set1))
		#print('Set2 \t: ' + str(set2))
		#print('Set3 \t: ' + str(set3))
		#print('Set4 \t: ' + str(set4))
		#print('Set5 \t: ' + str(set5))

		responseNumList = []
		for possibleResponse in possibleResponses:
			responseNumber = 0.0
			if possibleResponse in set1:
				responseNumber += 10**(precedence[0])
			if possibleResponse in set2:
				responseNumber += 10**(precedence[1])
			if possibleResponse in set3:
				responseNumber += 10**(precedence[2])
			if possibleResponse in set4:
				responseNumber += 10**(precedence[3])
			if possibleResponse in set5:
				responseNumber += 1e0**(precedence[4])
			responseNumList.append(responseNumber)
			#print(str(id) + '\t:' + possibleResponse + '\t:' + str(responseNumber))


		tp_id = 0.0
		tn_id = 0.0
		fp_id = 0.0
		fn_id = 0.0
		for possibleResponse in possibleResponses:
			if possibleResponse in set1:
				if possibleResponse == testAnswers[id]:
					tp_id += 1
				else:
					fp_id += 1
			else:
				if possibleResponse == testAnswers[id]:
					fn_id += 1
				else:
					tn_id += 1
		
		#print(str(id) + '\t: possibleResponses = ' + str(possibleResponses))
		#print(str(id) + '\t: set1 = ' + str(set1))
		#print(str(id) + '\t: tp = ' + str(tp_id))
		#print(str(id) + '\t: tn = ' + str(tn_id))
		#print(str(id) + '\t: fp = ' + str(fp_id))
		#print(str(id) + '\t: fn = ' + str(fn_id))

		pfPrecision_id = 0.0
		if tp_id + fp_id > 0:
			pfPrecision_id = tp_id/(tp_id+fp_id)
		
		pfRecall_id = 0.0
		if tp_id + fn_id > 0:
			pfRecall_id = tp_id/(tp_id+fn_id)
		
		pfAccuracy_id = (tp_id+tn_id)/(tp_id+tn_id+fp_id+fn_id)

		#print(str(id) + '\t: pfPrecision = ' + str(pfPrecision_id))
		#print(str(id) + '\t: pfRecall = ' + str(pfRecall_id))
		#print(str(id) + '\t: pfAccuracy = ' + str(pfAccuracy_id))

		testPredictions[id] = possibleResponses[max(enumerate(responseNumList), key=lambda x: x[1])[0]]

		if testPredictions[id] == testAnswers[id]:
			match += 1
		total += 1
		tp_person += tp_id
		tn_person += tn_id
		fp_person += fp_id
		fn_person += fn_id

	pfPrecision_person = 0.0
	if tp_person + fp_person > 0:
		pfPrecision_person = tp_person/(tp_person+fp_person)
	
	pfRecall_person = 0.0
	if tp_person + fn_person > 0:
		pfRecall_person = tp_person/(tp_person+fn_person)
	
	pfAccuracy_person = (tp_person+tn_person)/(tp_person+tn_person+fp_person+fn_person)

	#print('Person ' + str(person) + ' : tp = ' + str(tp_person))
	#print('Person ' + str(person) + ' : tn = ' + str(tn_person))
	#print('Person ' + str(person) + ' : fp = ' + str(fp_person))
	#print('Person ' + str(person) + ' : fn = ' + str(fn_person))
	#print('Person ' + str(person) + ' : pfPrecision = ' + str(pfPrecision_person))
	#print('Person ' + str(person) + ' : pfRecall = ' + str(pfRecall_person))
	#print('Person ' + str(person) + ' : pfAccuracy = ' + str(pfAccuracy_person))
	#print('Learned Rule \t: ' + str(rules))
	#print('testSyllogisms \t: ' + str(testSyllogisms))
	#print('testPremises \t: ' + str(testPremises))
	#print('testFacts \t: ' + str(testFacts))
	#print('testAnswers \t: ' + str(testAnswers))
	#print('testPredictions \t: ' + str(testPredictions))
	#print(str(match) + ' matches out of ' + str(total))
	return (match, total, tp_person, tn_person, fp_person, fn_person)
示例#27
0
def main(args, result_handler=None):
    import argparse

    parser = argparse.ArgumentParser()

    parser.add_argument("filename")
    parser.add_argument(
        "-N",
        "-n",
        type=int,
        dest="n",
        default=argparse.SUPPRESS,
        help="Number of samples.",
    )
    parser.add_argument(
        "--with-facts",
        action="store_true",
        help="Also output choice facts (default: just queries).",
    )
    parser.add_argument("--with-probability",
                        action="store_true",
                        help="Show probability.")
    parser.add_argument("--as-evidence",
                        action="store_true",
                        help="Output as evidence.")
    parser.add_argument(
        "--propagate-evidence",
        dest="propagate_evidence",
        default=False,
        action="store_true",
        help="Enable evidence propagation",
    )
    parser.add_argument(
        "--dont-propagate-evidence",
        action="store_false",
        dest="propagate_evidence",
        default=False,
        help="Disable evidence propagation",
    )
    parser.add_argument("--oneline",
                        action="store_true",
                        help="Format samples on one line.")
    parser.add_argument(
        "--estimate",
        action="store_true",
        help="Estimate probability of queries from samples.",
    )
    parser.add_argument(
        "--timeout",
        "-t",
        type=int,
        default=0,
        help="Set timeout (in seconds, default=off).",
    )
    parser.add_argument("--output",
                        "-o",
                        type=str,
                        default=None,
                        help="Filename of output file.")
    parser.add_argument("--web", action="store_true", help=argparse.SUPPRESS)
    parser.add_argument("--verbose",
                        "-v",
                        action="count",
                        help="Verbose output")
    parser.add_argument("--seed",
                        "-s",
                        type=float,
                        help="Random seed",
                        default=None)
    parser.add_argument("--full-trace", action="store_true")
    parser.add_argument("--strip-tag",
                        action="store_true",
                        help="Strip outermost tag from output.")
    parser.add_argument(
        "-a",
        "--arg",
        dest="args",
        action="append",
        help="Pass additional arguments to the cmd_args builtin.",
    )
    parser.add_argument("--progress",
                        help="show progress",
                        action="store_true")

    args = parser.parse_args(args)

    init_logger(args.verbose, "problog_sample")

    if args.seed is not None:
        random.seed(args.seed)
    else:
        seed = random.random()
        logging.getLogger("problog_sample").debug("Seed: %s", seed)
        random.seed(seed)

    pl = PrologFile(args.filename)

    outf = sys.stdout
    if args.output is not None:
        outf = open(args.output, "w")

    if args.timeout:
        start_timer(args.timeout)

    # noinspection PyUnusedLocal
    def signal_term_handler(*sigargs):
        sys.exit(143)

    signal.signal(signal.SIGTERM, signal_term_handler)

    if result_handler is not None or args.web:
        outformat = "dict"
        if result_handler is None:
            result_handler = print_result_json
    else:
        outformat = "str"
        result_handler = print_result
    try:
        if args.estimate:
            results = estimate(pl, **vars(args))
            print(format_dictionary(results))
        else:
            result_handler(
                (True, sample(pl, format=outformat, **vars(args))),
                output=outf,
                oneline=args.oneline,
            )
    except Exception as err:
        trace = traceback.format_exc()
        err.trace = trace
        result_handler((False, err), output=outf)

    if args.timeout:
        stop_timer()

    if args.output is not None:
        outf.close()
示例#28
0
def main(argv, result_handler=None):
    import argparse

    parser = argparse.ArgumentParser()
    parser.add_argument("filename",
                        metavar="MODEL",
                        type=str,
                        help="input ProbLog model")
    parser.add_argument(
        "--format",
        choices=("dot", "pl", "cnf", "svg", "internal"),
        default=None,
        help="output format",
    )
    parser.add_argument("--break-cycles",
                        action="store_true",
                        help="perform cycle breaking")
    parser.add_argument("--transform-nnf",
                        action="store_true",
                        help="transform to NNF")
    parser.add_argument("--keep-all",
                        action="store_true",
                        help="also output deterministic nodes")
    parser.add_argument(
        "--keep-duplicates",
        action="store_true",
        help="don't eliminate duplicate literals",
    )
    parser.add_argument("--any-order",
                        action="store_true",
                        help="allow reordering nodes")
    parser.add_argument(
        "--hide-builtins",
        action="store_true",
        help="hide deterministic part based on builtins",
    )
    parser.add_argument("--propagate-evidence",
                        action="store_true",
                        help="propagate evidence")
    parser.add_argument("--propagate-weights",
                        action="store_true",
                        help="propagate evidence")
    parser.add_argument(
        "--compact",
        action="store_true",
        help="allow compact model (may remove some predicates)",
    )
    parser.add_argument("--noninterpretable", action="store_true")
    parser.add_argument("--web", action="store_true", help=argparse.SUPPRESS)
    parser.add_argument("--verbose",
                        "-v",
                        action="count",
                        default=0,
                        help="Verbose output")
    parser.add_argument("-o",
                        "--output",
                        type=str,
                        help="output file",
                        default=None)
    parser.add_argument(
        "-a",
        "--arg",
        dest="args",
        action="append",
        help="Pass additional arguments to the cmd_args builtin.",
    )

    args = parser.parse_args(argv)

    outformat = args.format
    outfile = sys.stdout
    if args.output:
        outfile = open(args.output, "w")
        if outformat is None:
            outformat = os.path.splitext(args.output)[1][1:]

    if outformat == "cnf" and not args.break_cycles:
        print(
            "Warning: CNF output requires cycle-breaking; cycle breaking enabled.",
            file=sys.stderr,
        )

    if args.transform_nnf:
        target = LogicNNF
    elif args.break_cycles or outformat == "cnf":
        target = LogicDAG
    else:
        target = LogicFormula

    if args.propagate_weights:
        semiring = SemiringLogProbability()
    else:
        semiring = None

    if args.web:
        print_result = print_result_json
    else:
        print_result = print_result_standard

    try:
        gp = target.createFrom(
            PrologFile(args.filename,
                       parser=DefaultPrologParser(ExtendedPrologFactory())),
            label_all=not args.noninterpretable,
            avoid_name_clash=not args.compact,
            keep_order=not args.any_order,
            keep_all=args.keep_all,
            keep_duplicates=args.keep_duplicates,
            hide_builtins=args.hide_builtins,
            propagate_evidence=args.propagate_evidence,
            propagate_weights=semiring,
            args=args.args,
        )

        if outformat == "pl":
            rc = print_result((True, gp.to_prolog()), output=outfile)
        elif outformat == "dot":
            rc = print_result((True, gp.to_dot()), output=outfile)
        elif outformat == "svg":
            dot = gp.to_dot()
            tmpfile = mktempfile(".dot")
            with open(tmpfile, "w") as f:
                print(dot, file=f)
            svg = subprocess_check_output(["dot", tmpfile, "-Tsvg"])
            rc = print_result((True, svg), output=outfile)
        elif outformat == "cnf":
            cnfnames = args.verbose > 0
            rc = print_result(
                (True, CNF.createFrom(gp).to_dimacs(names=cnfnames)),
                output=outfile)
        elif outformat == "internal":
            rc = print_result((True, str(gp)), output=outfile)
        else:
            rc = print_result((True, gp.to_prolog()), output=outfile)
    except Exception as err:
        import traceback

        err.trace = traceback.format_exc()
        rc = print_result((False, err))

    if args.output:
        outfile.close()

    if rc:
        sys.exit(rc)
示例#29
0
def wmc(file):
    pl = PrologFile(file)
    result = solve(pl, CachetSemiring())
    return result.items()[0][1]
示例#30
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))