clingo
raw JSON → 5.8.0 verified Mon Apr 27 auth: no python
CFFI-based Python bindings to the clingo Answer Set Programming (ASP) solver. Version 5.8.0, released 2024-04-06. Cadence: irregular, roughly annual.
pip install clingo Common errors
error ModuleNotFoundError: No module named 'clingo' ↓
cause Clingo installed from PyPI may not work on systems without a compatible C compiler or prebuilt wheel (e.g., ARM macOS).
fix
Install via conda:
conda install -c potassco clingo, or install from source with proper dependencies. error clingo.core.SolveError: undefined arithmetic operation ↓
cause Using arithmetic expressions on ungrounded variables (e.g., `X = Y + 1`) without proper grounding.
fix
Use
#const directives, or ground the program properly, or avoid arithmetic on variables without domain bounds. Warnings
breaking From clingo 5.5 to 5.6, the `solve` method changed from returning a `SolveResult` to a `SolveHandle` with a context manager. In 5.4 and earlier, `ctl.solve()` returned a `SolveResult` directly. ↓
fix Use the context manager: `with ctl.solve(yield_=True) as handle:` instead of `result = ctl.solve()`.
breaking The `clingo.solving.SolveResult` class was removed in 5.6. `solve()` now returns a `SolveHandle`, and the satisfiability is obtained via `solve()`'s return value after the context manager exits. ↓
fix After the `with` block, use `handle.get()` to get satisfiability (e.g., `print(handle.get().satisfiable)`).
deprecated The `clingo.Control.load()` method is deprecated in 5.8 in favor of `Control.add()` + `Control.ground()`. ↓
fix Replace `ctl.load('file.lp')` with `with open('file.lp') as f: ctl.add('base', [], f.read()); ctl.ground([('base', [])])`.
gotcha Calling `Control.solve()` without `yield_=True` will not return models; you must iterate over the `SolveHandle` or use the context manager with `yield_=True`. ↓
fix Always use `with ctl.solve(yield_=True) as handle: for model in handle: print(model)`.
Imports
- Control wrong
import clingo; ctl = clingo.Control()correctfrom clingo import Control - Function wrong
from clingo.symbol import Functioncorrectfrom clingo import Function
Quickstart
from clingo import Control
ctl = Control()
ctl.add("base", [], "a :- not b. b :- not a.")
ctl.ground([("base", [])])
with ctl.solve(yield_=True) as handle:
for model in handle:
print(model)