Skip to content

plp

tuprolog.solve.plp

Probabilistic logic programming: probabilities of terms and solutions, probabilistic option.

In probabilistic logic programming (PLP), clauses are annotated with probabilities, and the solutions of queries come with the probability that they hold (see tuprolog.solve.problog). This package holds the vocabulary shared by probabilistic solvers, attached to the plain types of tuprolog.solve as tags (see tuprolog.utils.Taggable):

  • probability and set_probability: the probability of a term or solution (1.0 if unset);
  • is_probabilistic, set_probabilistic and probabilistic: the option asking solvers to compute probabilities, also set by solve_options(probabilistic=True);
  • binary_decision_diagram and siblings: the binary decision diagram (BDD) of a solution, i.e. the Boolean formula over probabilistic facts explaining it, from which its probability is computed.

Solvers not supporting PLP ignore the probabilistic option, so their solutions have probability 1.0.

Examples:

>>> from tuprolog.solve.problog import PROBLOG_OPERATORS, problog_solver
>>> from tuprolog.theory.parsing import parse_theory
>>> solver = problog_solver(static_kb=parse_theory("0.2::a.", PROBLOG_OPERATORS))
>>> solution = solver.solve_once("a", solve_options(probabilistic=True))
>>> probability(solution), has_binary_decision_diagram(solution)
(0.2, True)
See Also
  • tuprolog.solve.problog, the probabilistic solvers.

ProbExtensions module-attribute

ProbExtensions = _solve.ProbExtensions

The JVM class of 2P-Kt's PLP extension functions, wrapped by tuprolog.solve.plp's functions.

Its static methods (e.g. getProbability(taggable), setProbabilistic(options, value)) are those the functions of tuprolog.solve.plp call; it also has the constants DEFAULT_PROBABILITY (1.0) and DEFAULT_PROBABILISTIC_OPTION (False).

solve_options

solve_options(
    lazy: bool = True,
    timeout: int = MAX_TIMEOUT,
    limit: int = ALL_SOLUTIONS,
    custom: Mapping[str, Any] = {},
    *,
    probabilistic: bool = False,
    **kwargs: Any,
) -> SolveOptions

Creates the options of a resolution, for Solver.solve and Solver.solve_once.

Parameters:

Name Type Description Default
lazy bool

Whether solutions are computed one at a time, as the caller asks for them (lazy), or all at once before returning (eager, in which case Solver.solve returns a list: beware of queries with infinitely many solutions).

True
timeout int

The maximum duration of the resolution, in milliseconds; when it expires, the last solution is a halt with a tuprolog.solve.exception.TimeOutException. MAX_TIMEOUT (the default) means no timeout.

MAX_TIMEOUT
limit int

The maximum number of solutions; ALL_SOLUTIONS (the default) means no limit.

ALL_SOLUTIONS
custom Mapping[str, Any]

Custom options, by name, interpreted by specific solvers; they override the homonymous kwargs.

{}
probabilistic bool

Whether to compute the probabilities of solutions, for probabilistic solvers (see tuprolog.solve.plp and tuprolog.solve.problog).

False
**kwargs Any

More custom options.

{}

Returns:

Type Description
SolveOptions

The solve options.

Examples:

>>> from tuprolog.solve.prolog import prolog_solver
>>> solver = prolog_solver()
>>> options = solve_options(lazy=False, limit=2)
>>> options.is_eager, options.limit, options.timeout == MAX_TIMEOUT
(True, 2, True)
>>> [solution["X"] for solution in solver.solve("member(X, [a, b, c])", options)]
[a, b]
>>> solver.solve_once("repeat, fail", solve_options(timeout=50)).is_halt
True

probability

probability(taggable: Taggable) -> float

Returns the probability of a term or solution.

Parameters:

Name Type Description Default
taggable Taggable

A taggable object, e.g. a tuprolog.core.Term, or a Solution of a probabilistic solver solving with the probabilistic option.

required

Returns:

Type Description
float

The probability set with set_probability (or computed by a solver), or 1.0 if none was.

Examples:

>>> from tuprolog.core import atom
>>> probability(atom("a"))
1.0

set_probability

set_probability(taggable: _T, value: float) -> _T

Returns a copy of a term or solution with the given probability.

Parameters:

Name Type Description Default
taggable _T

A taggable object, e.g. a tuprolog.core.Term, left unchanged.

required
value float

The probability, clamped between 0.0 and 1.0.

required

Returns:

Type Description
_T

A copy of taggable whose probability is value.

Examples:

>>> from tuprolog.core import atom
>>> probability(set_probability(atom("a"), 0.3))
0.3
>>> probability(set_probability(atom("a"), 1.5))
1.0

is_probabilistic

is_probabilistic(solve_opts: SolveOptions) -> bool

Returns whether solve options ask for probabilities to be computed (False by default).

Parameters:

Name Type Description Default
solve_opts SolveOptions

The solve options.

required

Returns:

Type Description
bool

Whether the probabilistic option is on.

Examples:

>>> is_probabilistic(solve_options()), is_probabilistic(solve_options(probabilistic=True))
(False, True)

set_probabilistic

set_probabilistic(
    solve_opts: SolveOptions, value: bool
) -> SolveOptions

Returns a copy of solve options with the probabilistic option set to value.

Parameters:

Name Type Description Default
solve_opts SolveOptions

The solve options, left unchanged.

required
value bool

Whether solvers should compute the probabilities of solutions.

required

Returns:

Type Description
SolveOptions

The new solve options.

probabilistic

probabilistic(solve_opts: SolveOptions) -> SolveOptions

Returns a copy of solve options with the probabilistic option on.

Parameters:

Name Type Description Default
solve_opts SolveOptions

The solve options, left unchanged.

required

Returns:

Type Description
SolveOptions

The new solve options, like set_probabilistic(solve_opts, True).

Examples:

>>> is_probabilistic(probabilistic(solve_options(limit=1)))
True

has_binary_decision_diagram

has_binary_decision_diagram(solution: Solution) -> bool

Returns whether a solution has a binary decision diagram explaining its probability.

Only probabilistic solvers, solving with the probabilistic option, attach one.

Parameters:

Name Type Description Default
solution Solution

The solution.

required

Returns:

Type Description
bool

Whether binary_decision_diagram(solution) is not None.

binary_decision_diagram

binary_decision_diagram(solution: Solution) -> Any

Returns the binary decision diagram (BDD) explaining the probability of a solution.

A BDD is a compact representation of a Boolean formula: here, the formula over the probabilistic facts that holds exactly when the solution can be proven.

Parameters:

Name Type Description Default
solution Solution

The solution.

required

Returns:

Type Description
Any

The JVM it.unibo.tuprolog.bdd.BinaryDecisionDiagram, or None if the solution has none

Any

(see has_binary_decision_diagram).

set_binary_decision_diagram

set_binary_decision_diagram(
    solution: Solution, bdd: Any
) -> Solution

Returns a copy of a solution with the given binary decision diagram.

Parameters:

Name Type Description Default
solution Solution

The solution, left unchanged.

required
bdd Any

The JVM it.unibo.tuprolog.bdd.BinaryDecisionDiagram.

required

Returns:

Type Description
Solution

The new solution.