Skip to main content

tididi-cnf

PyPI Python Run in the browser arXiv

tididi-cnf for Python

A knowledge compiler and model counter for CNF, in Python.

tididi-cnf compiles a CNF formula into a Tree Decision Diagram (TDD) and counts its models exactly: the model count, the weighted count and their projections onto a set of variables. On the 1,400 entries of the 2020–2026 Model Counting Competitions it compiles more formulas than any BDD, SDD or logic-circuit compiler we compared, and counts more than every exact counter of the 2026 competition but one (evaluation). This package runs the same Rust implementation as the tididi command line: each call compiles or counts a formula as the binary does, and returns the answer instead of printing it.

Install

pip install tididi-cnf

The wheel covers Python 3.10 and later on x86-64 Linux with glibc 2.34 or later (Ubuntu 22.04, Debian 12, RHEL 9 and newer), and carries the GMP library it needs. Elsewhere on Linux, build it from a checkout, as Develop says.

Getting started

This example counts the models of a three-clause formula, then the probability that the formula holds when x1 is true with probability 0.3 and x2 with probability 0.4, whatever x3 does:

import tididi_cnf

# (x1 ∨ x2) ∧ (¬x1 ∨ x3) ∧ (¬x2 ∨ ¬x3)
clauses = [[1, 2], [-1, 3], [-2, -3]]
print("Models:", tididi_cnf.count(clauses).count)

answer = tididi_cnf.count(clauses, mode="pwmc", show=[1, 2], weights={1: "0.3", 2: "0.4"})
print("Probability:", answer.count)

Output:

Models: 2
Probability: 23/50

The second call is a projected weighted count: it adds up the weights of the assignments to x1 and x2 that some value of x3 completes to a model. Counts are exact: an int, or a fractions.Fraction for weighted projected counts. A weighted count without projection is a float.

tididi_cnf.count takes DIMACS text as str or bytes, clauses of nonzero int literals, or an object with clauses and nv such as a PySAT CNF. show and weights stand for a DIMACS file's c p show and c p weight lines. A weight is an int, a Fraction, a decimal string read exactly, or a float read as its shortest decimal; a lone weight for x leaves 1 - w for ¬x. tididi_cnf.count_file reads a DIMACS file, whose header can name the task:

answer = tididi_cnf.count_file("formula.cnf", time_limit_ms=60_000)

answer.mode         # "mc", "wmc", "pmc", "pwmc", or "compile" without a task line
answer.satisfiable  # whether the formula has a model
answer.count        # the count
answer.stats        # the run's record, the object `tididi --stats` writes

Both calls take mode, which must agree with the formula's c t and c p show lines and names the task for a formula without them, and the limits time_limit_ms and mem_limit_mb. A run that fails raises a subclass of tididi_cnf.Error; help(tididi_cnf.count) lists them. The user guide's input, tasks and limits pages describe the formula, the counts and the limits, and examples/count.py counts a file from the command line.

A run belongs to the process:

  • A nonzero mem_limit_mb lowers the interpreter's address-space limit (RLIMIT_AS) while the run lasts, and the run prints its progress to the process's stderr, not to sys.stderr.
  • The interpreter lock is released while it runs, and calls from several threads run one at a time.
  • The time limit is checked as the run goes, so a step in progress can finish after it. For a hard wall-clock limit, make the call in a separate process and stop that process at the limit.

Develop

Building the package needs the Rust and C++ toolchains and the system packages that docs/building.md lists. From the repository root, build it in place, then build the command line and run the tests. They also compare the version and the counts of the instances bindings/parity.txt lists with the binary's:

python3 -m venv .venv
. .venv/bin/activate
python -m pip install 'maturin>=1.9.4,<2' pytest
maturin develop --release --locked --manifest-path bindings/python/Cargo.toml
cargo build --release --locked --bin tididi
python -m pytest bindings/python/tests --tididi-cli target/release/tididi

Citing

Please cite A Canonical Generalization of OBDD, which introduced TDDs.

Acknowledgements

This material is based upon work supported by the Defense Advanced Research Projects Agency (DARPA) under Agreement No. HR0011269E224.

Metadata

Release files for tididi-cnf 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for tididi-cnf 0.1.1
File Interpreter ABI Platform Actions
tididi_cnf-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl CPython 3.10 abi3 Linux glibc 2.34+ x86-64 Details for tididi_cnf-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl

Release files / tididi_cnf-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl

Download URL tididi_cnf-0.1.1-cp310-abi3-manylinux_2_34_x86_64.whl
Size 5.5 MB
Tags CPython 3.10 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
f6e11816e93d10bcf6a397f3699f3d27a37951a5b85c6823f187846cfed1226b
BLAKE2b-256 checksum
How to use checksums
b96c53710fd6ffb1c312027accec6934a4e6465ed71b429334ec66a8ea1c9b61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for tididi-cnf 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

1 release file No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page