How to use the problog.logic.Term function in problog

To help you get started, we’ve selected a few problog examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github thiagopbueno / mdp-problog / tests / test_engine.py View on Github external
r2 = Fluent.create_fluent(running(c2), 1)
        r3 = Fluent.create_fluent(running(c3), 1)

        head = Term('__s0__')
        body = ~r1 & ~r2 & ~r3
        rule = head << body
        node = engine.add_rule(head, [~r1, ~r2, ~r3])
        self.assertTrue(engine.get_rule(node), rule)

        head = Term('__s3__')
        body = r1 & r2 & ~r3
        node = engine.add_rule(head, [r1, r2, ~r3])
        rule = engine.get_rule(node)
        self.assertTrue(rule, head << body)

        head = Term('__s7__')
        body = r1 & r2 & r3
        rule = head << body
        node = engine.add_rule(head, [r1, r2, r3])
        self.assertTrue(engine.get_rule(node), rule)
github thiagopbueno / mdp-problog / tests / test_engine.py View on Github external
running = Term('running')
        c1 = Constant('c1')
        c2 = Constant('c2')
        c3 = Constant('c3')
        r1 = Fluent.create_fluent(running(c1), 1)
        r2 = Fluent.create_fluent(running(c2), 1)
        r3 = Fluent.create_fluent(running(c3), 1)

        head = Term('__s0__')
        body = ~r1 & ~r2 & ~r3
        rule = head << body
        node = engine.add_rule(head, [~r1, ~r2, ~r3])
        self.assertTrue(engine.get_rule(node), rule)

        head = Term('__s3__')
        body = r1 & r2 & ~r3
        node = engine.add_rule(head, [r1, r2, ~r3])
        rule = engine.get_rule(node)
        self.assertTrue(rule, head << body)

        head = Term('__s7__')
        body = r1 & r2 & r3
        rule = head << body
        node = engine.add_rule(head, [r1, r2, r3])
        self.assertTrue(engine.get_rule(node), rule)
github thiagopbueno / mdp-problog / mdp-problog.py View on Github external
def _get_decision_facts(self):
		self._action_decision_facts = []
		self._state_decision_facts = []
		for i, n, t in self._gp:
			if t == 'atom' and n.probability == Term('?'):
				functor = n.name.functor
				if functor in self._state_functors:
					self._state_decision_facts.append(n.name)
				else:
					self._action_decision_facts.append(n.name)
		self._state_decision_facts = sorted(self._state_decision_facts, key=Term.__repr__)
github thiagopbueno / mdp-problog / mdp-problog.py View on Github external
def __init__(self, model, gamma, epsilon):
		self._model = model
		self._gamma = gamma
		self._epsilon = epsilon

		print("  >> Preprocessing program ...", end=" ")
		start = time.clock()
		self._db = self._eng.prepare(PrologString(model))
		self._build_state_atoms()
		self._get_action_atoms()
		self._build_action_rules()
		self._build_value_function_rules()
		self._utilities = dict(self._eng.query(self._db, Term('utility', None, None)))
		end = time.clock()
		uptime = end-start
		print("Done in {0:.3f}sec.".format(uptime))

		print("  >> Relevant grounding ...", end=" ")
		start = time.clock()
		self._gp = self._eng.ground_all(self._db, target=None, queries=self._utilities.keys())
		end = time.clock()
		uptime = end-start
		print("Done in {0:.3f}sec.".format(uptime))

		print("  >> Compilation ...", end=" ")
		start = time.clock()
		self._knowledge = get_evaluatable(None).create_from(self._gp)
		end = time.clock()
		uptime = end-start
github thiagopbueno / mdp-problog / mdp-problog.py View on Github external
def _build_state_atoms(self):
		state_vars = [p[0] for p in self._eng.query(self._db, Term('state_fluent', None))]
		self._state_functors = set()
		self._next_state_atoms = []
		self._current_state_atoms = []
		for t in state_vars:
			self._state_functors.add(t.functor)

			args = t.args + (Constant(1),)
			self._next_state_atoms.append(t.with_args(*args))

			args = t.args + (Constant(0),)
			curr_state_atom = t.with_args(*args)
			self._current_state_atoms.append(curr_state_atom)
			self._db.add_fact(curr_state_atom.with_probability(Term('?')))
github thiagopbueno / mdp-problog / mdp-problog.py View on Github external
def _get_action_atoms(self):
		self._action_atoms = [p[0] for p in self._eng.query(self._db, Term('action', None))]
github thiagopbueno / mdp-problog / mdpproblog / engine.py View on Github external
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)))
github thiagopbueno / mdp-problog / mdpproblog / engine.py View on Github external
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)
github thiagopbueno / mdp-problog / mdp-problog.py View on Github external
def _build_value_function_rules(self):
		self._value_function_atoms = []
		n = len(self._next_state_atoms)
		valuation = [0]*n
		for i in range(2**n):
			body_atoms = []
			for pos in range(n):
				if valuation[pos] == 1:
					body_atoms.append(self._next_state_atoms[pos])
				else:
					body_atoms.append(~self._next_state_atoms[pos])
			body = And.from_list(body_atoms)
			head = Term('__s{}__'.format(i))
			self._value_function_atoms.append(head)
			rule = head << body
			self._db.add_clause(rule)

			value = Term('utility', head.with_probability(None), Constant(0.0))
			self._db.add_fact(value)

			MDPProbLog.next_valuation(valuation)