clingo.solve
Functions and classes related to solving.
Examples
The examples below show various ways to intercept models. The asynchronous
variants leave room for additional computation before calling blocking functions
like SolveHandle.get
or SolveHandle.model
.
The following example shows how to intercept models with a callback:
>>> from clingo.core import Library >>> from clingo.control import Control >>> >>> lib = Library() >>> ctl = Control(lib, ["0"]) >>> ctl.parse_string("1 { a; b } 1.") >>> ctl.ground() >>> with ctl.solve(on_model=print) as hnd: ... print(hnd.get()) ... a b SAT
The following example shows how to yield models:
>>> from clingo.core import Library >>> from clingo.control import Control >>> >>> lib = Library() >>> ctl = Control(lib, ["0"]) >>> ctl.parse_string("1 { a; b } 1.") >>> ctl.ground() >>> with ctl.solve(yield_=True) as hnd: ... for mdl in hnd: ... print(mdl) ... print(hnd.get()) ... a b SAT
The following example shows how to solve asynchronously:
>>> from clingo.core import Library >>> from clingo.control import Control >>> >>> lib = Library() >>> ctl = Control(lib, ["0"]) >>> ctl.parse_string("1 { a; b } 1.") >>> ctl.ground() >>> with ctl.solve(on_model=print, async_=True) as hnd: ... print(hnd.get()) ... a b SAT
This example shows how to solve both iteratively and asynchronously:
>>> from clingo.core import Library >>> from clingo.control import Control >>> >>> lib = Library() >>> ctl = Control(lib, ["0"]) >>> ctl.parse_string("1 { a; b } 1.") >>> ctl.ground() >>> with ctl.solve(yield_=True, async_=True) as hnd: ... while mdl := hnd.model(): ... print(mdl) ... hnd.resume() ... print(hnd.get()) ... b a SAT
A view on the solver's current solution.
Check if the model contains the given atom.
Arguments:
- atom: The atom to look up.
Returns:
Whether the atom is contained.
Extend a model with the given symbols.
This only has an effect if there is an underlying clingo application, which will print the added symbols.
Arguments:
- symbols: The symbols to add to the model.
Check if the given program literal is a consequence.
The function returns True
, False
, or None
if the literal is a
consequence, not a consequence, or it is not yet known whether it is a
consequence, respectively.
While enumerating cautious or brave consequences, there is partial information about which literals are consequences. The current state of a literal can be requested using this function. If this function is used during normal model enumeration, it simply returns whether the literal is true or false in the current model.
Arguments:
- literal: The given program literal.
Returns:
Whether the given program literal is a consequence.
Check if the given program literal is true.
Arguments:
- literal: The given program literal.
Returns:
Whether the given program literal is true.
Get the symbols in the model.
Arguments:
- shown: Include shown atoms and terms.
- atoms: Include all true atoms, including hidden ones.
- terms: Include shown terms.
- theory: Include terms added by external theories.
Returns:
A sequence of symbols present in the model.
Enumeration of model types.
A control object to add clauses while solving.
Add a clause that applies to the current solving step during the search.
Arguments:
- clause: The literals of the clause.
An object to interact with a running search.
It can be used to control solving, like, retrieving models or cancelling a search.
A SolveHandle is a context manager and must be used with Python's with statement.
Blocking functions in this object release the GIL. They are not thread-safe though.
See also: clingo.control.Control.solve
Get the solve result.
This is always the last function to be called on a handle to ensure that the search is properly terminated. It might be preceded by a call to cancel to stop the search.
Get the last computed model, if any.
If the search is not completed yet or the problem is unsatisfiable, the
function returns None
.
Discards the last model and starts searching for the next one.
If the search has been started asynchronously, this function starts the search in the background.
Wait for the solve call to finish or the next result with an optional timeout.
If a timeout is provided, the function blocks for the given duration or until a result is ready. A positive timeout blocks for that amount of time. A negative timeout blocks until a result is available, and a zero timeout allows polling for a result.
Arguments:
- timeout: The maximum time to block in seconds.
Returns:
Whether the solve call has finished or the next result is ready.
A solve result captures information about a solve call.