clingo.control
Module containing the Control class responsible for grounding and solving.
Examples
The first example shows the most straightforward way to ground and solve a small test program:
>>> from clingo.core import Library
>>> from clingo.control import Control
>>>
>>> lib = Library()
>>> ctl = Control(lib)
>>> ctl.parse_string("1 { a; b }.")
>>> ctl.ground()
>>> with ctl.solve(on_model=print) as hnd:
... hnd.get()
a
The second example shows how to call functions from within a program:
>>> from clingo.core import Library
>>> from clingo.symbol import Number
>>> from clingo.control import Control
>>>
>>> class Context:
... def __init__(self, lib):
... self.lib = lib
... def inc(self, x):
... return Number(self.lib, x.number + 1)
... def seq(self, x, y):
... return [x, y]
...
>>> lib = Library()
>>> ctl = Control(lib)
>>> ctl.parse_string("""
... p(@inc(10)).
... q(@seq(1,2)).
... """)
>>> ctl.ground(context=Context(lib))
>>> with ctl.solve(on_model=print) as hnd:
... print(hnd.get())
p(11) q(1) q(2)
SAT
A control object for grounding and solving.
Construct a control object.
Arguments:
- lib: The library storing symbols and scripts.
- options: The command line options to initialize the control object.
Discard statements of the selected types.
Arguments:
- minimize: Discard all minimize and weak constraints (default: False).
- project: Discard all previously added project statements (default: False).
Ground the given program parts.
Non-ground logic programs must be added before calling this function. Programs
can define named sections using #program.
directives. These sections can be
selectively grounded by specifying their name and binding parameters to
symbols.
Arguments:
- parts: A sequence of tuples, each containing a section name and a sequence of
symbols. The name identifies the program section to ground, and the
symbols bind its parameters. If
None
, the implicit base section without arguments is grounded. - context: An optional object providing functions that can be called during grounding.
Interrupt the active solve call.
This function is thread-safe. Prefer using clingo.solve.SolveHandle.cancel
if
possible.
Ground and solve a logic program based on the current control mode.
This function serves as a high-level entry point for the default
ground-and-solve process. It considers the current ControlMode
and can
dispatch execution to a script's main
function if defined.
If solving, the function proceeds as follows:
- Each set of program parts in
parts
is grounded sequentially. - After grounding each set, solving is performed immediately.
Before calling main()
, the control object can be prepared by parsing
programs, registering propagators, or performing other setup steps.
Inspect the ground program of the current step.
Arguments:
- observer: The program observer to inspect the program. preprocess: Whether the program should be preprocessed first (default: True).
Parses the logic programs in the given files
Arguments:
- files: A list of file paths to parse.
Parses a logic program given as a string.
Arguments:
- program: The logic program as a string.
Register the given propagator for theory propagation.
See the clingo.propagate
module for an example.
Arguments:
- propagator: The propagator.
Solve the current ground program.
This function runs the solver on the current ground program, optionally using
assumptions, callbacks, or asynchronous execution. It returns a SolveHandle
,
allowing interaction with the solving process.
If asynchronous solving (async_
) is enabled, the function returns
immediately, and solving runs in the background. Otherwise, the function
blocks until solving is complete.
Arguments:
- assumptions: A list of assumptions that constrain this search. Each assumption is
either a
tuple[clingo.symbol.Symbol, bool]
indicating an atom's truth value or a program literal (seeclingo.base.Atom.literal
). For example, using[(clingo.symbol.Function(lib, "a"), True)]
only admits answer sets that contain atoma
. - on_model: Optional callback that receives a
clingo.solve.Model
object when a model is found. ReturningFalse
from the callback stops solving. - on_unsat: Optional callback to intercept lower bounds during optimization.
- on_stats: Optional callback that receives statistics updates after each step.
Two
clingo.stats.Stats
objects are passed: step-specific and accumulated stats. - on_finish: Optional callback called once search has finished. A
clingo.solve.SolveResult
is passed to the callback. - yield_: If
True
, the returnedclingo.solve.SolveHandle
is iterable, yieldingclingo.solve.Model
objects during solving. - async_: If
True
, solving runs asynchronously in a separate thread. Note: Callbacks (on_model
,on_stats
, etc.) will also be called from a separate thread.
Returns:
A
clingo.solve.SolveHandle
to control the search.
Notes:
Asynchronous solving requires compiling clingo with thread support. Blocking methods on
SolveHandle
release the GIL but are not thread-safe.
See Also:
clingo.solve: Contains examples on using this function.
Write the current logic programs to the given file.
If append is true, a file will be created if none exists yet. If preamble is None, then the aspif preamble is written for newly created files and omitted for existing files.
Arguments:
- path: The path to write the program to.
- append: Whether to append to an existing file.
- preamble: Whether to write the aspif preamble.
- preprocess: Whether to preprocess the program before writing.
Get the map of constants defined by #const
directives.
Get/set the program parts to ground.
Available control modes.