コード例 #1
0
ファイル: grounder.py プロジェクト: orsinif/kProbLog
    def run(self):
        global DEBUG_FLAG
        data = self.preprocess_metafunctions()
        if DEBUG_FLAG:
            self.display_preprocessed_code(data)
        model = PrologString(data)
        if DEBUG_FLAG:
            print '=' * 80
            print "BEFORE ESCAPING"
            print '=' * 80
            for elem in model: print clause2str(elem)
            print '=' * 80

        model = escape_metafunctions(model)

        if DEBUG_FLAG:
            print '=' * 80
            print "AFTER ESCAPING"
            print '=' * 80
            for elem in model: print clause2str(elem)
            print '=' * 80

        engine = DefaultEngine(label_all=True)
        engine.add_builtin(METAMETAFUNCTION_FUNCTOR, 2, BooleanBuiltIn(self.builtin_metafunction))
        engine.add_builtin('declare', 2, BooleanBuiltIn(self.builtin_declare))
        engine.add_builtin('declare', 3, BooleanBuiltIn(self.builtin_declare))

        db = engine.prepare(model)
        if DEBUG_FLAG:
            print "=" * 80
            print "DATABASE"
            print "=" * 80
            for elem in db: print elem
            print "=" * 80

        gp = LogicFormula(
            keep_all=True,
            keep_order=True,
            keep_duplicates=True,
            avoid_name_clash=True
        )

        gp = engine.ground_all(db, target=gp)
        if DEBUG_FLAG:
            print "=" * 80
            print "GROUND PROGRAM (GROUNDER)"
            print "=" * 80
            for elem in gp.enum_clauses():
                print elem
            print "=" * 80
        clauses = []
        facts = []
        for clause in unescape_metafunctions(gp.enum_clauses()):
            if isinstance(clause, Clause):
                clauses.append(clause)
            else:
                facts.append(clause)

        query_atoms = gp._names['query']
        return self.builtin_declare.declarations, clauses + facts, query_atoms
コード例 #2
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))
コード例 #3
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
コード例 #4
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))
コード例 #5
0
class AIController(PlayerController):
    '''Intelligenza artificiale

    Contiene i metodi per la gestione delle decisioni prese dal maziere
    '''
    ''' conoscenza del dt_problog'''
    conoscenza = []
    ''' conoscenza del problog (calcolo inferenza) '''
    conoscenza_prob = []

    vincita_attuale = 0

    def __init__(self, nome, soldi, prolog):
        '''COSTRUTTORE
        chiama il costruttore della superclasse PlayerController ed istanzia gli oggetti per la gestione delle query problog

        :type nome: string
        :param nome: nome del giocatore
        :type soldi: float
        :param soldi: soldi del giocatore 
        :type prolog: PrologController
        :param prolog: Oggetto PrologController che estende la classe Prolog del modulo pyswip
        '''
        super(AIController, self).__init__(nome, soldi, prolog)
        self.read_file(path='controller/dt_problog.pl',
                       path1='controller/knowledge_problog.pl')
        self.learn()

    def read_file(self, path, path1):
        '''Legge i file .pl contenenti la conoscenza problog e crea una lista contenente tutti i predicati
        '''
        try:

            with open(path) as kn_file:
                self.conoscenza = kn_file.read()

            with open(path1) as kn_file:
                for row in kn_file:
                    self.conoscenza_prob.append(row.rstrip('\n'))

            self.conoscenza_prob = list(
                filter(('').__ne__, self.conoscenza_prob))

        except Exception as e:
            print(e)

    def learn(self):
        '''Construisce gli oggetti problog per la valutazione dell'inferenza'''
        try:
            knowledge_str = ''
            for predicate in self.conoscenza_prob:
                knowledge_str += predicate + '\n'

            knowledge_str = PrologString(knowledge_str)
            self.problog = DefaultEngine()
            self.knowledge_database = self.problog.prepare(knowledge_str)

        except Exception as e:
            print(e)

    def get_used_card_evidence(self, prolog):
        '''construisce la lista contenente le stringhe delle evidenze per il DTProblog'''
        ret_list = []
        for card in prolog.uscite:
            ret_list.append('evidence(not ' + str(card) + ').\n')

        return ret_list

    def get_utility(self, prolog):
        '''constuisce la lista delle utility per il DTProblog'''
        vs_score, utility = prolog.get_utility()
        ret_list = []
        for i in range(len(vs_score)):
            ret_list.append(
                f'utility(vinco({self.punteggio},{vs_score[i]}), {utility[i]}).\n'
            )
            ret_list.append(
                f'utility(perdo({self.punteggio},{vs_score[i]}), {-1*utility[i]}).\n'
            )
        ret_list.append(
            f'utility(sballo({self.punteggio}), {-1*prolog.get_winnable_bet()}).\n'
        )

        vincita, _ = prolog.get_gain()
        prob_di_migliorare = self.query('prob_di_migliorare',
                                        self.punteggio,
                                        evidence=self.get_used_card(prolog))
        util = 0
        for i in range(len(vincita)):
            util += prob_di_migliorare * vincita[i]
        ret_list.append(f'utility(miglioro({self.punteggio}), {util}).\n')
        '''
        prob_di_migliorare = self.query('prob_di_migliorare',self.punteggio,evidence = self.get_used_card(prolog))
        for i in range(len(vincita)):
            ret_list.append(f'utility(miglioro({self.punteggio}), {prob_di_migliorare*vincita[i]}).\n')
        '''
        return ret_list

    def decidi(self, prolog):
        '''prende la decisione'''
        temp = self.conoscenza
        evidence = self.get_used_card_evidence(prolog)
        for ev in evidence:
            temp += ev
        utilities = self.get_utility(prolog)
        for ut in utilities:
            temp += ut

        program = PrologString(temp)
        decisions, _, _ = dtproblog(program)

        for _, value in decisions.items():
            if value == 1:
                return True
            print('STO\n')
            return False

    def eval_query(self, term, *args, **kwargs):
        try:
            if args:
                t = '\'' + term + '\''
                termine = f'Term({t},'
                for termine_arg in args:
                    if isinstance(termine_arg, str):
                        termine += 'Term(\'' + termine_arg + '\')' + ','
                    elif isinstance(termine_arg, int) or isinstance(
                            termine_arg, float):
                        termine += 'Constant(' + str(termine_arg) + ')' + ','
                termine = termine[:-1] + ')'

                query_term = eval(termine)
            else:
                query_term = Term(term)

            evidenze = []
            if kwargs:
                for i in kwargs['evidence']:
                    # Term(Constant(1),Term('spade'))
                    i = i.replace('card(',
                                  '').replace(' ', '').replace(')',
                                                               '').split(',')
                    term_str = f'Term(\'card\',Constant({i[0]}), Term(\'{i[1]}\'))'
                    tupla = (eval(term_str), False)
                    evidenze.append(tupla)

            lf = self.problog.ground_all(self.knowledge_database,
                                         queries=[query_term],
                                         evidence=evidenze)
            return get_evaluatable().create_from(lf).evaluate()
        except Exception as e:
            print(e)
            print("QUESTA SOPRA è L'ECCEZIONE")
            return None

    def query(self, term, *args, **kwargs):
        # print(ai.query('prob_di_sballare', 7, evidence=['card(2,bastoni)']))
        query_result = self.eval_query(term, *args, **kwargs)
        if len(query_result) > 1:
            res = []
            for prob_res in query_result.values():
                res.append(prob_res)
        else:
            for prob_res in query_result.values():
                res = prob_res

        return res

    def get_used_card(self, prolog):
        '''ritorna una lista di stringhe contenenti le carte uscite'''
        uscite = []
        for card in prolog.uscite:
            uscite.append(str(card))

        return uscite
コード例 #6
0
ファイル: step-by-step.py プロジェクト: Nicolas-Boltz/problog
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))
コード例 #7
0
ファイル: engine.py プロジェクト: vishalbelsare/mdp-problog
class Engine(object):
    """
    Adapter class to Problog grounding and query engine.

    :param program: a valid MDP-ProbLog program
    :type program: str
    """
    def __init__(self, program):
        self._engine = DefaultEngine()
        self._db = self._engine.prepare(PrologString(program))
        self._gp = None
        self._knowledge = None

    def declarations(self, declaration_type):
        """
        Return a list of all terms of type `declaration_type`.

        :param declaration_type: declaration type.
        :type declaration_type: str
        :rtype: list of problog.logic.Term
        """
        return [
            t[0]
            for t in self._engine.query(self._db, Term(declaration_type, None))
        ]

    def assignments(self, assignment_type):
        """
        Return a dictionary of assignments of type `assignment_type`.

        :param assignment_type: assignment type.
        :type assignment_type: str
        :rtype: dict of (problog.logic.Term, problog.logic.Constant) items.
        """
        return dict(
            self._engine.query(self._db, Term(assignment_type, None, None)))

    def get_instructions_table(self):
        """
        Return the table of instructions separated by instruction type
        as described in problog.engine.ClauseDB.

        :rtype: dict of (str, list of (node,namedtuple))
        """
        instructions = {}
        for node, instruction in enumerate(self._db._ClauseDB__nodes):
            instruction_type = str(instruction)
            instruction_type = instruction_type[:instruction_type.find('(')]
            if instruction_type not in instructions:
                instructions[instruction_type] = []
            assert (self._db.get_node(node) == instruction)  # sanity check
            instructions[instruction_type].append((node, instruction))
        return instructions

    def add_fact(self, term, probability=None):
        """
        Add a new `term` with a given `probability` to the program database.
        Return the corresponding node number.

        :param term: a predicate
        :type term: problog.logic.Term
        :param probability: a number in [0,1]
        :type probability: float
        :rtype: int
        """
        return self._db.add_fact(term.with_probability(Constant(probability)))

    def get_fact(self, node):
        """
        Return the fact in the table of instructions corresponding to `node`.

        :param node: identifier of fact in table of instructions
        :type node: int
        :rtype: problog.engine.fact
        """
        fact = self._db.get_node(node)
        if not str(fact).startswith('fact'):
            raise IndexError('Node `%d` is not a fact.' % node)
        return fact

    def add_rule(self, head, body):
        """
        Add a new rule defined by a `head` and `body` arguments
        to the program database. Return the corresponding node number.

        :param head: a predicate
        :type head: problog.logic.Term
        :param body: a list of literals
        :type body: list of problog.logic.Term or problog.logic.Not
        :rtype: int
        """
        b = body[0]
        for term in body[1:]:
            b = b & term
        rule = head << b
        return self._db.add_clause(rule)

    def get_rule(self, node):
        """
        Return the rule in the table of instructions corresponding to `node`.

        :param node: identifier of rule in table of instructions
        :type node: int
        :rtype: problog.engine.clause
        """
        rule = self._db.get_node(node)
        if not str(rule).startswith('clause'):
            raise IndexError('Node `%d` is not a rule.' % node)
        return rule

    def add_assignment(self, term, value):
        """
        Add a new utility assignment of `value` to `term` in the program database.
        Return the corresponding node number.

        :param term: a predicate
        :type term: problog.logic.Term
        :param value: a numeric value
        :type value: float
        :rtype: int
        """
        args = (term.with_probability(None), Constant(1.0 * value))
        utility = Term('utility', *args)
        return self._db.add_fact(utility)

    def get_assignment(self, node):
        """
        Return the assignment in the table of instructions corresponding to `node`.

        :param node: identifier of assignment in table of instructions
        :type node: int
        :rtype: pair of (problog.logic.Term, problog.logic.Constant)
        """
        fact = self._db.get_node(node)
        if not (str(fact).startswith('fact') and fact.functor == 'utility'):
            raise IndexError('Node `%d` is not an assignment.' % node)
        return (fact.args[0], fact.args[1])

    def add_annotated_disjunction(self, facts, probabilities):
        """
        Add a new annotated disjunction to the program database from
        a list of `facts` and its `probabilities`.
        Return a list of choice nodes.

        :param facts: list of probabilistic facts
        :type  facts: list of problog.logic.Term
        :param probabilities: list of valid individual probabilities
                              such that the total probability is less
                              than or equal to 1.0
        :type probabilities: list of float in [0.0, 1.0]
        :rtype: list of int
        """
        disjunction = [
            f.with_probability(Constant(p))
            for f, p in zip(facts, probabilities)
        ]
        self._db += AnnotatedDisjunction(heads=disjunction,
                                         body=Constant('true'))

        choices = []
        for node, term in enumerate(self._db._ClauseDB__nodes):
            if str(term).startswith('choice'):
                choices.append((term, node))

        nodes = []
        for term in disjunction:
            term = term.with_probability(None)
            for choice, node in choices:
                if term in choice.functor.args:
                    nodes.append(node)
        return nodes

    def get_annotated_disjunction(self, nodes):
        """
        Return the list of choice nodes in the table of instructions
        corresponding to `nodes`.

        :param nodes: list of node identifiers
        :type nodes: list of int
        :rtype: list of problog.engine.choice
        """
        choices = [self._db.get_node(node) for node in nodes]
        for choice in choices:
            if not str(choice).startswith('choice'):
                raise IndexError('Node `%d` is not a choice node.' % choice)
        return choices

    def relevant_ground(self, queries):
        """
        Create ground program with respect to `queries`.

        :param queries: list of predicates
        :type queries: list of problog.logic.Term
        """
        self._gp = self._engine.ground_all(self._db, queries=queries)

    def compile(self, terms=[]):
        """
        Create compiled knowledge database from ground program.
        Return mapping of `terms` to nodes in the compiled knowledge database.

        :param terms: list of predicates
        :type terms: list of problog.logic.Term
        :rtype: dict of (problog.logic.Term, int)
        """
        self._knowledge = get_evaluatable(None).create_from(self._gp)
        term2node = {}
        for term in terms:
            term2node[term] = self._knowledge.get_node_by_name(term)
        return term2node

    def evaluate(self, queries, evidence):
        """
        Compute probabilities of `queries` given `evidence`.

        :param queries: mapping of predicates to nodes
        :type queries: dict of (problog.logic.Term, int)
        :param evidence: mapping of predicate and evidence weight
        :type evidence: dictionary of (problog.logic.Term, {0, 1})
        :rtype: list of (problog.logic.Term, [0.0, 1.0])
        """
        evaluator = self._knowledge.get_evaluator(semiring=None,
                                                  evidence=None,
                                                  weights=evidence)
        return [(query, evaluator.evaluate(queries[query]))
                for query in sorted(queries, key=str)]
コード例 #8
0
def sample_object(pl, N=1):
    engine = DefaultEngine()
    db = engine.prepare(pl)
    result = [engine.ground_all(db, target=SampledFormula()) for i in range(N)]
    return result, db
コード例 #9
0
class GDLIIIProblogRep(object):
    def __init__(self, program, fformat):
        #GlobalEngine
        self._engine = DefaultEngine()
        gdl_parser = GDLIIIParser()
        self._model = gdl_parser.output_model(program, fformat)
        self._baseModelFile = self._model.as_problog()
        self._playerList = []
        self._randomIdentifier = Constant(0) #Hardcoded to give the random player a specific constant id as we apply some special rules to the random player
        worlds = self._initialiseKB()
        self._cur_node = GDLNode(worlds, GameData(self._playerList, self._randomIdentifier))
        self._moveList = dict([(i,None) for i in self._playerList])
        self.terminal = False

    def getMoveList(self):
        return self._moveList

    def undo(self, increment=1):
        for _ in range(increment):
            self._cur_node = self._cur_node.parent
        self._moveList = dict([(i,None) for i in self._playerList])
        if self.terminal:
            self.terminal = False

    def getLegalMovesForPlayer(self, player):
        return self._cur_node.legal_moves[player]

    def _resetKnowledgeBase(self):
        self._kb = self._engine.prepare(self._baseModelFile)

    def getPlayersPossibleWorlds(self, player):
        return self._cur_node.worlds[player]


    #Assumption, term contains a single argument
    def extractSingleArg(self,nArg, term):
        return term.args[nArg]

    #Recommended to never call this function with step > 1 as it will likely take a long time, exponential time complexity for values of step > 0.
    def query(self, player, query, step=0):
        if step == 0:
            return self._cur_node.raw_query(player, Term('thinks', player, query))
        else:
            world_set = set([self._cur_node])
            for _ in range(step):
                #Create set of all possible move sequences from perspective of player
                action_set = set()
                for world in world_set:
                    action_set = action_set.union(set([i for i in world.get_legal_moves()[player].keys()]))
                legal_moves_seqs = [{k:(None if k != player else a) for k in self._playerList} for a in action_set]
                #Generate possible (but not always valid) successor worlds
                new_set = set()
                for world in world_set:
                    for actions in legal_moves_seqs:
                        new_set.add(world.generate_speculative_worlds(player, actions))
                world_set = new_set

            query_dict = {}
            size = len(world_set)
            for w in world_set:
                for (item,val) in w.raw_query(player, Term('thinks', player, query)).items():
                    if item in query_dict.keys():
                        query_dict[item] += val/size
                    else:
                        query_dict[item] = val/size
            return query_dict

    #Private
    def _initialiseKB(self):
        self._kb = self._engine.prepare(self._baseModelFile)
        initialState = \
            set(map(lambda a: Term('ptrue', a[0].args[0]), self._engine.ground_all(self._kb, queries=[Term('init',Var('_'))]).get_names()))
        players = \
            set(map(lambda a: a[0], self._engine.ground_all(self._kb, queries=[Term('role',Var('_'))]).get_names()))
        #Not needed, but dont care to remove right now
        self._step = 0
        playerWorldState = {}

        for playerNum in map(lambda a: a.args[0], players):
            knowledge = map(lambda a: Term('thinks', playerNum, a.args[0]), initialState)
            playerPreds = initialState.union(set(knowledge))
            self._playerList.append(playerNum)
            #Each player starts with a single initial world
            if playerNum == self._randomIdentifier:
                #Random Specific world has no thinks predicates
                playerWorldState[playerNum] = [RandomWorld(self._engine, self._baseModelFile, self._step, 1, initialState, playerNum)]
            else:
                playerWorldState[playerNum] = [World(self._engine, self._baseModelFile, self._step, 1, playerPreds, playerNum)]
        return playerWorldState

    def applyActionsToModelAndUpdate(self):
        if (None in self._moveList.values()):
            raise Exception("Error: Must have submitted moves for all players before proceeding")
        self._cur_node = self._cur_node.get_next_node(self._moveList)
        #Assume, there is at least one player
        if len([(k,v) for (k,v) in self._cur_node.raw_query(\
             self._playerList[0], Term('terminal')).items() if v > 0]) > 0:
            self.terminal = True

        self._step += 1
        self._moveList = dict([(i,None) for i in self._playerList])

    def submitAction(self, action, player):
        if ( action not in self.getLegalMovesForPlayer(player)):
            raise Exception("{} is not a legal action".format(action))
        self._moveList[player] = action
コード例 #10
0
ファイル: prolog_ipynotebook.py プロジェクト: sircanist/tilde
db = engine.prepare(pl)


print(db)
query_term = sibling(tom, sally)
res = engine.query(db, query_term)
print ('%s? %s' % (query_term, bool(res)))

query_term = sibling(sally, erica)
res = engine.query(db, query_term)
print(res)
print ('%s? %s' % (query_term, bool(res)))

# NOTE: variables can be replaced by None of a negative number
# the difference is that each None is a different variable,
# while each variable with the same negative number is the same variable
query_term = sibling(None, None)
res = engine.query(db, query_term)

for args in res:
    print(query_term(*args))

print('siblings of sally:')
query_term = Term('sibling', Term('sally'), None)
res = engine.query(db, query_term)

for args in res:
    print(query_term(*args))

print(engine.ground_all(db, queries=[query_term]))
コード例 #11
0
ファイル: engine.py プロジェクト: thiagopbueno/mdp-problog
class Engine(object):
    """
    Adapter class to Problog grounding and query engine.

    :param program: a valid MDP-ProbLog program
    :type program: str
    """

    def __init__(self, program):
        self._engine = DefaultEngine()
        self._db = self._engine.prepare(PrologString(program))
        self._gp = None
        self._knowledge = None

    def declarations(self, declaration_type):
        """
        Return a list of all terms of type `declaration_type`.

        :param declaration_type: declaration type.
        :type declaration_type: str
        :rtype: list of problog.logic.Term
        """
        return [t[0] for t in self._engine.query(self._db, Term(declaration_type, None))]

    def assignments(self, assignment_type):
        """
        Return a dictionary of assignments of type `assignment_type`.

        :param assignment_type: assignment type.
        :type assignment_type: str
        :rtype: dict of (problog.logic.Term, problog.logic.Constant) items.
        """
        return dict(self._engine.query(self._db, Term(assignment_type, None, None)))

    def get_instructions_table(self):
        """
        Return the table of instructions separated by instruction type
        as described in problog.engine.ClauseDB.

        :rtype: dict of (str, list of (node,namedtuple))
        """
        instructions = {}
        for node, instruction in enumerate(self._db._ClauseDB__nodes):
            instruction_type = str(instruction)
            instruction_type = instruction_type[:instruction_type.find('(')]
            if instruction_type not in instructions:
                instructions[instruction_type] = []
            assert(self._db.get_node(node) == instruction)  # sanity check
            instructions[instruction_type].append((node, instruction))
        return instructions

    def add_fact(self, term, probability=None):
        """
        Add a new `term` with a given `probability` to the program database.
        Return the corresponding node number.

        :param term: a predicate
        :type term: problog.logic.Term
        :param probability: a number in [0,1]
        :type probability: float
        :rtype: int
        """
        return self._db.add_fact(term.with_probability(Constant(probability)))

    def get_fact(self, node):
        """
        Return the fact in the table of instructions corresponding to `node`.

        :param node: identifier of fact in table of instructions
        :type node: int
        :rtype: problog.engine.fact
        """
        fact = self._db.get_node(node)
        if not str(fact).startswith('fact'):
            raise IndexError('Node `%d` is not a fact.' % node)
        return fact

    def add_rule(self, head, body):
        """
        Add a new rule defined by a `head` and `body` arguments
        to the program database. Return the corresponding node number.

        :param head: a predicate
        :type head: problog.logic.Term
        :param body: a list of literals
        :type body: list of problog.logic.Term or problog.logic.Not
        :rtype: int
        """
        b = body[0]
        for term in body[1:]:
            b = b & term
        rule = head << b
        return self._db.add_clause(rule)

    def get_rule(self, node):
        """
        Return the rule in the table of instructions corresponding to `node`.

        :param node: identifier of rule in table of instructions
        :type node: int
        :rtype: problog.engine.clause
        """
        rule = self._db.get_node(node)
        if not str(rule).startswith('clause'):
            raise IndexError('Node `%d` is not a rule.' % node)
        return rule

    def add_assignment(self, term, value):
        """
        Add a new utility assignment of `value` to `term` in the program database.
        Return the corresponding node number.

        :param term: a predicate
        :type term: problog.logic.Term
        :param value: a numeric value
        :type value: float
        :rtype: int
        """
        args = (term.with_probability(None), Constant(1.0 * value))
        utility = Term('utility', *args)
        return self._db.add_fact(utility)

    def get_assignment(self, node):
        """
        Return the assignment in the table of instructions corresponding to `node`.

        :param node: identifier of assignment in table of instructions
        :type node: int
        :rtype: pair of (problog.logic.Term, problog.logic.Constant)
        """
        fact = self._db.get_node(node)
        if not (str(fact).startswith('fact') and fact.functor == 'utility'):
            raise IndexError('Node `%d` is not an assignment.' % node)
        return (fact.args[0], fact.args[1])

    def add_annotated_disjunction(self, facts, probabilities):
        """
        Add a new annotated disjunction to the program database from
        a list of `facts` and its `probabilities`.
        Return a list of choice nodes.

        :param facts: list of probabilistic facts
        :type  facts: list of problog.logic.Term
        :param probabilities: list of valid individual probabilities
                              such that the total probability is less
                              than or equal to 1.0
        :type probabilities: list of float in [0.0, 1.0]
        :rtype: list of int
        """
        disjunction = [ f.with_probability(Constant(p)) for f, p in zip(facts, probabilities) ]
        self._db += AnnotatedDisjunction(heads=disjunction, body=Constant('true'))

        choices = []
        for node, term in enumerate(self._db._ClauseDB__nodes):
            if str(term).startswith('choice'):
                choices.append((term, node))

        nodes = []
        for term in disjunction:
            term = term.with_probability(None)
            for choice, node in choices:
                if term in choice.functor.args:
                    nodes.append(node)
        return nodes

    def get_annotated_disjunction(self, nodes):
        """
        Return the list of choice nodes in the table of instructions
        corresponding to `nodes`.

        :param nodes: list of node identifiers
        :type nodes: list of int
        :rtype: list of problog.engine.choice
        """
        choices = [ self._db.get_node(node) for node in nodes ]
        for choice in choices:
            if not str(choice).startswith('choice'):
                raise IndexError('Node `%d` is not a choice node.' % choice)
        return choices

    def relevant_ground(self, queries):
        """
        Create ground program with respect to `queries`.

        :param queries: list of predicates
        :type queries: list of problog.logic.Term
        """
        self._gp = self._engine.ground_all(self._db, queries=queries)

    def compile(self, terms=[]):
        """
        Create compiled knowledge database from ground program.
        Return mapping of `terms` to nodes in the compiled knowledge database.

        :param terms: list of predicates
        :type terms: list of problog.logic.Term
        :rtype: dict of (problog.logic.Term, int)
        """
        self._knowledge = get_evaluatable(None).create_from(self._gp)
        term2node = {}
        for term in terms:
            term2node[term] = self._knowledge.get_node_by_name(term)
        return term2node

    def evaluate(self, queries, evidence):
        """
        Compute probabilities of `queries` given `evidence`.

        :param queries: mapping of predicates to nodes
        :type queries: dict of (problog.logic.Term, int)
        :param evidence: mapping of predicate and evidence weight
        :type evidence: dictionary of (problog.logic.Term, {0, 1})
        :rtype: list of (problog.logic.Term, [0.0, 1.0])
        """
        evaluator = self._knowledge.get_evaluator(semiring=None, evidence=None, weights=evidence)
        return [ (query, evaluator.evaluate(queries[query])) for query in sorted(queries, key=str) ]