Skip to content

problog

tuprolog.solve.problog

ProbLog solvers: probabilistic logic programming.

Probabilistic logic programming (PLP) attaches probabilities to facts and clauses: with ProbLog's :: operator, 0.6::heads(C). states that each coin C lands heads with probability 0.6, independently of the others. The probability of a query is then the probability that it can be proven, i.e. that the facts used by at least one of its proofs hold: with the clauses win :- heads(c1). and win :- heads(c2)., win has probability 1 - 0.4 * 0.4 = 0.84. ProbLog solvers compute it by compiling the proofs into a binary decision diagram (BDD).

ProbLog solvers extend Prolog solvers: knowledge bases with probabilistic clauses must be parsed with PROBLOG_OPERATORS, which add :: to Prolog's operators, and probabilities are computed only when solving with the probabilistic option (tuprolog.solve.solve_options(probabilistic=True)); otherwise, solvers behave as Prolog ones and ignore probabilities. Probabilities are read with tuprolog.solve.plp.probability. Annotations follow ProbLog's syntax, including evidence/2 for facts known to be true or false.

Main functions and constants:

  • problog_solver: creates a ProbLog solver;
  • PROBLOG_SOLVER_FACTORY: the factory of ProbLog solvers;
  • PROBLOG_OPERATORS, PROBLOG_SPECIFIC_OPERATORS, ANNOTATION_OPERATOR: ProbLog's operators.

Examples:

>>> from tuprolog.solve import solve_options
>>> from tuprolog.solve.plp import probability
>>> from tuprolog.theory.parsing import parse_theory
>>> kb = parse_theory(
...     "0.6::heads(C). win :- heads(c1). win :- heads(c2).", PROBLOG_OPERATORS
... )
>>> solver = problog_solver(static_kb=kb)
>>> [round(probability(s), 2) for s in solver.solve("win", solve_options(probabilistic=True))]
[0.84]
See Also
  • tuprolog.solve.plp, for the probabilities of solutions and the probabilistic option.
  • tuprolog.solve.prolog, the non-probabilistic solvers.
  • ProbLog, the language these solvers implement.

PROBLOG_SOLVER_FACTORY module-attribute

PROBLOG_SOLVER_FACTORY: SolverFactory = Solver.problog()

The factory of ProbLog solvers (2P-Kt's Solver.problog), see tuprolog.solve.SolverFactory.

Its solvers always load ProbLog's library, and turn the track_variables flag on, as computing probabilities requires it. tuprolog.solve.problog.problog_solver is a simpler way to create solvers.

Operators module-attribute

Operators = _problog.Operators

The JVM class holding ProbLog's operators, also re-exported as module constants.

Its static fields are ANNOTATION_FUNCTOR (::), ANNOTATION_OPERATOR, PROBLOG_SPECIFIC_OPERATORS and PROBLOG_OPERATORS.

ANNOTATION_OPERATOR module-attribute

ANNOTATION_OPERATOR = Operators.ANNOTATION_OPERATOR

ProbLog's probability annotation, ::: an infix, right-associative (xfy) operator, priority 900.

Its priority is lower than the one of :-, so that 0.3::a :- b. reads as (0.3::a) :- b.

Examples:

>>> ANNOTATION_OPERATOR
Operator(900, XFY, '::')

PROBLOG_SPECIFIC_OPERATORS module-attribute

PROBLOG_SPECIFIC_OPERATORS = (
    Operators.PROBLOG_SPECIFIC_OPERATORS
)

The operators ProbLog adds to Prolog's, i.e. only ANNOTATION_OPERATOR (an OperatorSet).

PROBLOG_OPERATORS module-attribute

PROBLOG_OPERATORS = Operators.PROBLOG_OPERATORS

ProbLog's operators: Prolog's standard ones, plus ANNOTATION_OPERATOR (an OperatorSet).

Parse probabilistic knowledge bases and queries with them, e.g. tuprolog.theory.parsing.parse_theory(text, PROBLOG_OPERATORS).

problog_solver

problog_solver(
    unificator: Unificator | None = None,
    libraries: Runtime | Library | None = None,
    flags: FlagStore = DEFAULT_FLAG_STORE,
    static_kb: Theory | None = None,
    dynamic_kb: Theory | None = None,
    std_in: InputChannel | IOBase | None = None,
    std_out: OutputChannel | IOBase | None = None,
    std_err: OutputChannel | IOBase | None = None,
    warning: OutputChannel | IOBase | None = None,
    mutable: bool = True,
) -> Solver

Creates a ProbLog solver, i.e. a Prolog solver also computing probabilities.

Probabilities are computed only when solving with the probabilistic option (see tuprolog.solve.solve_options and tuprolog.solve.plp); otherwise, the solver behaves as a Prolog one, finding one solution per proof, and ignoring probabilities.

Parameters:

Name Type Description Default
unificator Unificator | None

The unification algorithm; by default, the standard one (see tuprolog.unify).

None
libraries Runtime | Library | None

Further libraries of predicates and functions (a Library, or a Runtime of several ones, see tuprolog.solve.library), loaded on top of the standard ones.

None
flags FlagStore

The flags (see tuprolog.solve.flags); by default, DEFAULT_FLAG_STORE.

DEFAULT_FLAG_STORE
static_kb Theory | None

The static knowledge base, i.e. the clauses queries cannot change, parsed with PROBLOG_OPERATORS if probabilistic; empty by default.

None
dynamic_kb Theory | None

The dynamic knowledge base, i.e. the clauses that predicates like assertz/1 and retract/1 change; empty by default.

None
std_in InputChannel | IOBase | None

The standard input: an InputChannel, a Python stream, or a callable returning a string per call (None at the end); by default, Python's sys.stdin.

None
std_out OutputChannel | IOBase | None

The standard output, where write/1 writes: an OutputChannel, a Python stream, or a callable taking each string; by default, Python's sys.stdout.

None
std_err OutputChannel | IOBase | None

The standard error, like std_out; by default, Python's sys.stderr.

None
warning OutputChannel | IOBase | None

Where warnings go (e.g. about calls to unknown predicates): an OutputChannel, a Python stream (receiving their messages), or a callable taking each tuprolog.solve.exception.Warning; by default, Python's sys.stderr.

None
mutable bool

Whether to create a tuprolog.solve.MutableSolver, whose state Python code can change between queries (the default), or a plain tuprolog.solve.Solver.

True

Returns:

Type Description
Solver

The ProbLog solver.

Examples:

>>> from tuprolog.solve import solve_options
>>> from tuprolog.solve.plp import probability
>>> from tuprolog.solve.problog import PROBLOG_OPERATORS
>>> from tuprolog.theory.parsing import parse_theory
>>> kb = parse_theory(
...     "0.3::rain. 0.5::sprinkler. wet :- rain. wet :- sprinkler.", PROBLOG_OPERATORS
... )
>>> solver = problog_solver(static_kb=kb)
>>> solution = next(iter(solver.solve("wet", solve_options(probabilistic=True))))
>>> round(probability(solution), 2)
0.65
>>> list(solver.query("wet"))  # not probabilistic: one solution per proof
[{}, {}]
Note

solve_once stops at the first proof, so the probability of its solution only accounts for that proof: use solve to get the probability of a query.