clingo.backend
Functions and classes to observe or add ground statements.
Examples
The first example shows how to add a fact to a program:
>>> from clingo.core import Library
>>> from clingo.symbol import Function
>>> from clingo.control import Control
...
>>> lib = Library()
>>> ctl = Control(lib)
>>> sym = Function(lib, "a")
>>> with ctl.backend as bck:
... atm_a = bck.atom(sym)
... bck.rule([atm_a])
>>> ctl.base.is_fact(ctl.base[sym].literal)
True
>>> with ctl.solve(on_model=print) as hnd:
... hnd.get()
a
SAT
The next example shows how to add theory atoms to a program:
>>> from clingo.core import Library
>>> from clingo.symbol import Function
>>> from clingo.control import Control
...
>>> lib = Library()
>>> ctl = Control(lib)
...
>>> with ctl.backend as bck:
... n = Function(lib, "p")
... a = bck.theory_string("a")
... o = bck.theory_number(1)
... f = bck.theory_function("f", [a, o])
... e = bck.theory_element([f], [])
... bck.theory_atom(0, n, [e])
...
>>> print(ctl.base.theory[0])
&p { f(a,1) }
Provides an interface to extend a logic program.
This class offers methods to add various types of rules, set external atoms, specify heuristics, define optimization statements, and extend the underlying theory. It allows for low-level manipulation of logic programs.
See Also:
Add an assumption directive to the solver.
Specifies literals to be assumed true or false for the next solving call. Positive literals are treated as true, while negative literals are treated as false. These assumptions apply only to the next solve call.
Arguments:
- literals: Sequence of program literals to assume.
Return a fresh program atom or the atom associated with the given symbol.
If the given symbol does not exist in the atom base, it is added first. These atoms will be used in subsequent calls to ground for instantiation.
Arguments:
- symbol: The symbol associated with the atom.
Returns:
The program atom representing the atom.
Add an edge directive.
Adds an edge from node_u
to node_v
to the graph. The edge is subject to the
specified condition.
Arguments:
- node_u: The start node of the edge.
- node_v: The end node of the edge.
- condition: Sequence of literals representing the edge condition.
Add an external directive.
Declares an atom as external and sets its truth value according to the
specified type. External atoms can be used as assumptions or for incremental
solving. The special value ExternalType.Release
can be used to permanently
set an external atom to false.
Arguments:
- atom: The external atom (must be a positive literal).
- type: The type determining the truth value of the atom.
Add a heuristic directive for an atom.
Adds a heuristic directive for the given atom, influencing the solver's search process. The directive's effect depends on its type, weight, and priority. The condition, if provided, determines when the heuristic should be applied.
Arguments:
- atom: The atom to which the heuristic applies.
- type: The type of the heuristic.
- weight: The weight of the heuristic.
- priority: The priority of the heuristic (default: 0).
- condition: Sequence of literals representing the condition (default: []).
Add a minimize constraint to the program.
Adds a minimize constraint, which instructs the solver to minimize the sum of weights of true literals in the given sequence. Multiple minimize constraints with different priorities can be added, with lower priority values considered more important.
Arguments:
- literals: Sequence of (literal, weight) tuples to minimize.
- priority: Priority of the constraint (default: 0).
Add a projection directive to the program.
Specifies which atoms should be considered in stable models. When projection is used, stable models are treated as equivalent if they agree on the truth values of projected atoms, regardless of other atoms in the model.
Arguments:
- atoms: Sequence of atoms to project on.
Add a rule to the program.
Creates a disjunctive or choice rule based on the choice
parameter. The
head and body are specified as sequences of literals.
Rule types based on head length (when choice is False):
- Empty head: Integrity constraint
- Single literal head: Normal rule
- Multiple literals head: Disjunctive rule
Arguments:
- head: Sequence of literals in the rule head.
- body: Sequence of literals in the rule body (default: []).
- choice: If True, adds a choice rule; otherwise, a disjunctive rule (default: False).
Create a theory atom and return its associated program atom.
If the theory atom does not exist yet, the method assigns a program atom based on these rules:
- If a specific atom value is provided, that value is used.
- If None is given, a fresh program atom is introduced.
- For program directives, value zero is used.
Arguments:
- atom: The program atom to assign, or None to assign a fresh one.
- name: The name of the theory atom.
- elements: A sequence of ids representing theory elements.
- guard: A tuple containing the guard operator and the id of the guard term, or None if there is no guard.
Returns:
The program atom associated with the created theory atom.
Create a theory element.
The method creates a theory element consisting of a tuple and a condition, and returns its unique identifier. This id can be used in other theory-related methods to reference this element.
Arguments:
- tuple: A sequence of ids representing theory terms.
- condition: A sequence of program literals.
Returns:
The unique id of the created theory element.
Create a function theory term.
The method creates a function theory term with the given name and arguments, and returns its unique identifier. This id can be used in other theory-related methods to reference this term.
Arguments:
- name: The name of the function.
- arguments: A sequence of ids of theory terms.
Returns:
The unique id of the created function theory term.
Create a numeric theory term.
The function creates a numeric theory term with the given value and return its unique identifier. This id can be used in other theory-related methods to reference this term.
Arguments:
- number: The numeric value of the theory term to create.
Returns:
The unique id of the created theory term.
Create a sequence theory term.
The method creates a sequence theory term of the specified type, containing the given elements, and returns its unique identifier. This id can be used in other theory-related methods to reference this term.
Arguments:
- type: The type of the sequence (e.g., tuple, list, set).
- elements: A sequence of ids representing theory terms.
Returns:
The unique id of the created sequence theory term.
Create a string theory term.
The method creates a string theory term with the given value and returns its unique identifier. This id can be used in other theory-related methods to reference this term.
Arguments:
- string: The string value of the theory term to create.
Returns:
The unique id of the created string theory term.
Convert a symbol into a theory term.
The method converts the given symbol into a theory term and returns its unique identifier. This id can be used in other theory-related methods to reference this term.
Arguments:
- symbol: The symbol to create the theory term from.
Returns:
The unique id of the created theory term.
Add a weight rule to the program.
Adds a weight rule, where the body is a weight constraint. The head is either a disjunction ro choice (see Backend.rule for more details).
A weight constraint is satisfied if the sum of weights of the true literals meets or exceeds the lower bound.
Arguments:
- head: Sequence of literals in the rule head.
- lower_bound: The lower bound of the weight constraint.
- body: Sequence of (literal, weight) tuples forming the weight constraint.
- choice: If True, adds a choice rule; otherwise, a disjunctive rule (default: False).
A context manager to initialize and finalize a backend.
Available external types.
Available heuristic types.
ABC to inspect aspif directives.
Not all functions of the interface have to be implemented and can be omitted if not needed.
See Also:
Called for assumptions in the solver.
See also Backend.assume
.
Arguments:
- literals: Sequence of program literals to assume.
Called for edge directives in the program.
See also Backend.edge
.
Arguments:
- node_u: The start node of the edge.
- node_v: The end node of the edge.
- condition: Sequence of literals representing the edge condition.
Called for external directives in the program.
See also Backend.external
.
Arguments:
- atom: The external atom (must be a positive literal).
- type: The type determining the truth value of the atom.
Called for heuristic directives in the program.
See also Backend.heuristic
.
Arguments:
- atom: The atom to which the heuristic applies.
- type: The type of the heuristic.
- weight: The weight of the heuristic.
- priority: The priority of the heuristic (default: 0).
- condition: Sequence of literals representing the condition (default: []).
The first directive in a program.
The parameter incremental
indicates whether the program can consist of more
than one step.
Arguments:
- incremental: Whether the program is incremental.
Called for minimize constraints in the program.
See also Backend.minimize
.
Arguments:
- literals: Sequence of (literal, weight) tuples to minimize.
- priority: Priority of the constraint (default: 0).
Called for projection directives in the program.
See also Backend.project
.
Arguments:
- atoms: Sequence of atoms to project on.
Called for rules in the program.
See also Backend.rule
.
Arguments:
- head: Sequence of literals in the rule head.
- body: Sequence of literals in the rule body (default: []).
- choice: If True, adds a choice rule; otherwise, a disjunctive rule (default: False).
Called for weight rules in the program.
See also Backend.weight_rule
.
Arguments:
- head: Sequence of literals in the rule head.
- lower_bound: The lower bound of the weight constraint.
- body: Sequence of (literal, weight) tuples forming the weight constraint.
- choice: If True, adds a choice rule; otherwise, a disjunctive rule (default: False).
Available theory sequence types.