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):
probabilityandset_probability: the probability of a term or solution (1.0 if unset);is_probabilistic,set_probabilisticandprobabilistic: the option asking solvers to compute probabilities, also set bysolve_options(probabilistic=True);binary_decision_diagramand 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 |
True
|
timeout
|
int
|
The maximum duration of the resolution, in milliseconds; when it expires, the
last solution is a halt with a |
MAX_TIMEOUT
|
limit
|
int
|
The maximum number of solutions; |
ALL_SOLUTIONS
|
custom
|
Mapping[str, Any]
|
Custom options, by name, interpreted by specific solvers; they override the
homonymous |
{}
|
probabilistic
|
bool
|
Whether to compute the probabilities of solutions, for probabilistic
solvers (see |
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
¶
Returns the probability of a term or solution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
taggable
|
Taggable
|
A taggable object, e.g. a |
required |
Returns:
| Type | Description |
|---|---|
float
|
The probability set with |
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 |
required |
value
|
float
|
The probability, clamped between 0.0 and 1.0. |
required |
Returns:
| Type | Description |
|---|---|
_T
|
A copy of |
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 |
Examples:
>>> is_probabilistic(probabilistic(solve_options(limit=1)))
True
has_binary_decision_diagram
¶
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
¶
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 |
Any
|
(see |