Skip to main content

Bindings to Ganak, a high-performance exact model counter

Project description

pyganak — Python bindings for Ganak

pyganak provides Python bindings for Ganak, a high-performance exact model counter for CNF formulas. It exposes two classes:

  • Counter — unweighted (and projected) model counting. Returns an arbitrary-precision Python int.
  • WeightedCounter — weighted model counting with per-literal float weights. Uses MPFR internally; returns a Python float.

Arjun preprocessing is applied automatically before each count, just as the command-line tool does.

Installation

pip install pyganak

Pre-built wheels are available for Linux (x86-64, ARM64) and macOS (Apple Silicon, Intel).

Quick start

Unweighted counting

from pyganak import Counter

c = Counter()
c.add_clause([1, 2])      # x1 OR x2
c.add_clause([-1, 2])     # NOT x1 OR x2
print(c.count())          # → 2  (exact Python int)

Weighted counting

from pyganak import WeightedCounter

c = WeightedCounter()
c.add_clause([1, 2])           # x1 OR x2

# Set weights for both polarities of each variable.
c.set_lit_weight( 1, 0.3)     # P(x1 = True)  = 0.3
c.set_lit_weight(-1, 0.7)     # P(x1 = False) = 0.7
c.set_lit_weight( 2, 0.4)     # P(x2 = True)  = 0.4
c.set_lit_weight(-2, 0.6)     # P(x2 = False) = 0.6

# Models: (T,T)=0.12  (T,F)=0.18  (F,T)=0.28  → total=0.58
print(c.count())               # → 0.58  (Python float)

Counter API

Counter(verbose=0, seed=0)

Parameter Type Default Description
verbose int 0 Verbosity level (0 = silent)
seed int 0 Random seed for the solver

add_clause(clause)

Add a single clause — iterable of nonzero signed integers (1-indexed variables; positive = positive literal, negative = negated literal).

c.add_clause([1, -2, 3])   # x1 OR NOT x2 OR x3

add_clauses(clauses)

Add multiple clauses at once.

c.add_clauses([[1, 2], [-1, 3]])

set_sampling_set(vars)

Set the projection set (independent support). Only the given variables are counted; all others are existentially quantified out. If never called, all variables are included.

c.add_clause([1, 2, 3])
c.set_sampling_set([1, 2])
print(c.count())   # → 4

count() → int

Run Arjun preprocessing + Ganak and return the exact model count as a Python int. May only be called once per instance.

new_vars(n) / nof_vars() → int / nof_clauses() → int

Declare extra variables, or query the current variable / clause count.


WeightedCounter API

WeightedCounter(verbose=0, seed=0, prec=128)

Parameter Type Default Description
verbose int 0 Verbosity level
seed int 0 Random seed
prec int 128 MPFR precision in bits (≥ 2)

The internal arithmetic is performed with MPFR at prec bits of precision (default 128 ≈ 38 significant decimal digits). The final result is returned as a Python float (double).

Floating-point caveat: Because floating-point arithmetic is not associative, the result is a high-precision approximation rather than an exact value — the order in which partial sums are accumulated can affect the last few bits. For exact rational weighted counting, use the command-line tool with --mode 1 (GMP mpq rationals).

set_lit_weight(lit, weight)

Set the weight of a literal.

  • lit — nonzero signed integer (1-indexed; positive = positive literal).
  • weight — Python float (double). Stored internally as an MPFR value at the precision given to the constructor.

Always set weights for both a literal and its negation to get well-defined weighted counting. Literals whose weights are not set default to weight 1.0.

c.set_lit_weight( 1, 0.3)   # weight of  x1 = 0.3
c.set_lit_weight(-1, 0.7)   # weight of ¬x1 = 0.7

add_clause(clause) / add_clauses(clauses) / set_sampling_set(vars) / new_vars(n) / nof_vars() / nof_clauses()

Identical to the Counter versions.

count() → float

Run Arjun preprocessing + Ganak weighted model counting. Returns the weighted count as a Python float. The internal computation uses MPFR at the configured precision. May only be called once per instance.


Examples

Plain model counting

from pyganak import Counter

# (x1 XOR x2): 2 models
c = Counter()
c.add_clause([1, 2])
c.add_clause([-1, -2])
print(c.count())   # 2

Projected model counting

from pyganak import Counter

# (x1 OR x2 OR x3) projected onto {x1, x2}: 4 models
c = Counter()
c.add_clause([1, 2, 3])
c.set_sampling_set([1, 2])
print(c.count())   # 4

Large count (arbitrary precision)

from pyganak import Counter

# 30 unconstrained variables → 2^30 models
c = Counter()
c.new_vars(30)
print(c.count())   # 1073741824

Weighted counting

from pyganak import WeightedCounter

# (x1 OR x2) with independent Bernoulli weights
c = WeightedCounter()
c.add_clause([1, 2])
c.set_lit_weight( 1, 0.3);  c.set_lit_weight(-1, 0.7)
c.set_lit_weight( 2, 0.4);  c.set_lit_weight(-2, 0.6)
print(c.count())   # 0.58

Weighted counting with higher MPFR precision

from pyganak import WeightedCounter

# Use 256-bit MPFR precision for the internal computation.
c = WeightedCounter(prec=256)
c.add_clause([1, 2, 3])
c.set_lit_weight( 1, 0.1);  c.set_lit_weight(-1, 0.9)
c.set_lit_weight( 2, 0.2);  c.set_lit_weight(-2, 0.8)
c.set_lit_weight( 3, 0.5);  c.set_lit_weight(-3, 0.5)
print(c.count())

Building from source (venv)

If you have already built Ganak with CMake (see the top-level README.md for instructions), the extension is in build/lib/pyganak*.so. Test it without a full pip install using a venv:

python3 -m venv venv
venv/bin/pip install pytest
PYTHONPATH=build/lib venv/bin/pytest python/tests/ -v

To rebuild the extension after editing python/src/pyganak.cpp:

# If the build directory already exists:
cmake -DBUILD_PYTHON_EXTENSION=ON build   # or cmake .. from inside build/
cmake --build build --target pyganak -j$(nproc)

# Then re-run tests as above.

To do a full pip install (builds everything from scratch):

pip install .

Requires: GMP ≥ 5, MPFR ≥ 3, FLINT ≥ 2. On Ubuntu/Debian:

sudo apt install libgmp-dev libmpfr-dev libflint-dev

On macOS:

brew install gmp mpfr flint

License

MIT — see LICENSE.txt in the repository root.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyganak-2.6.3-cp313-cp313-manylinux_2_34_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

pyganak-2.6.3-cp313-cp313-manylinux_2_34_aarch64.whl (4.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ ARM64

pyganak-2.6.3-cp313-cp313-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyganak-2.6.3-cp312-cp312-manylinux_2_34_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

pyganak-2.6.3-cp312-cp312-manylinux_2_34_aarch64.whl (4.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ ARM64

pyganak-2.6.3-cp312-cp312-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pyganak-2.6.3-cp311-cp311-manylinux_2_34_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

pyganak-2.6.3-cp311-cp311-manylinux_2_34_aarch64.whl (4.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ ARM64

pyganak-2.6.3-cp311-cp311-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

pyganak-2.6.3-cp310-cp310-manylinux_2_34_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

pyganak-2.6.3-cp310-cp310-manylinux_2_34_aarch64.whl (4.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ ARM64

pyganak-2.6.3-cp310-cp310-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

pyganak-2.6.3-cp39-cp39-manylinux_2_34_x86_64.whl (5.3 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ x86-64

pyganak-2.6.3-cp39-cp39-manylinux_2_34_aarch64.whl (4.9 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ ARM64

pyganak-2.6.3-cp39-cp39-macosx_11_0_arm64.whl (4.3 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

Details for the file pyganak-2.6.3-cp313-cp313-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 d8c852eaf6a7ae9603d5a0a71921ce597bba84cdcf8f4a84abbe8620969b5aee
MD5 e2c32f2d3bc9239f2073a71dca260ebf
BLAKE2b-256 9bf1bc71204887c6a4347be73b15b7642586bde1a1ebb4553b4717a6cb1f5935

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp313-cp313-manylinux_2_34_x86_64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp313-cp313-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp313-cp313-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 03879e6f0cb5c33dfe3b20274aef829e29e6346bc7fa1b3d965baa283519f5b0
MD5 74c10a73a759f0bfe178931e84f1eeed
BLAKE2b-256 128387ea3ec925e7f483dbdf7bf319cc3fae5f8013be85f16215da7b4d512f24

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp313-cp313-manylinux_2_34_aarch64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f96578564694d987fa6e0274f0b9fa7912667e8b3c335f527583252ae72ffa94
MD5 ab4a853ef2908939e90771dc1179109b
BLAKE2b-256 92913fe9ab0d7567f9a262cb3dab9874be06bd8fd044f0c2d8d1c5d9154157da

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 3be99f94c28d5968b914c2c576bd0c6464c33c5f214e43ef6e8e43c6e17fa974
MD5 e77006e8b38b81c212f21057f994c420
BLAKE2b-256 d643d3c6b8fd3e75bde2eaf1f1d380703f96f643d1ec25351fe5e309b495f210

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp312-cp312-manylinux_2_34_x86_64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp312-cp312-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp312-cp312-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 3f9781e753ca771e23f3c424c0bfc3ea180b01946457101af176d24e665e4dec
MD5 a5097773fee48fdc15f93b39af056cb9
BLAKE2b-256 26ace61261f0e85735388b7bd0b92dce732b2ddfea3c6b3e07be3aa7e9f833ae

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp312-cp312-manylinux_2_34_aarch64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e9688f158b76f08439c236e8cb07e6de7cf52515b6ec75bd5095acbd317cdb39
MD5 7d836dc3693a7c6ce408200162b682ba
BLAKE2b-256 4a9061cfa2c99766948ded7c3b6c37b103f9565f0a5e3442d5f368c59d69594b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 4aa46ee246d74d1e26aba8ab7175e2dbbfee48ccdd96a8b238ba85a3428a7035
MD5 a05ce9d5d254c4f828a59aaa9d30c540
BLAKE2b-256 2d820f7237d629b3c82843e078b6e975a1e2019cb731210e9aa5e51132f12bf0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp311-cp311-manylinux_2_34_x86_64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp311-cp311-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp311-cp311-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 a2e990ea2fe213e6abc23b6c409bef921fd988310b439918e2fd9eadc6ed2736
MD5 7810409c96e307f7c4c1b0cc62b9c89e
BLAKE2b-256 959489a51d66220fb44f90bf758f81345a2b7990bc7eb39cc6d1210a82365a41

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp311-cp311-manylinux_2_34_aarch64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6fc1a56ec9ca87df735ccc193677b5b01f6ae2276983fc41ed6d006cf2ae0d62
MD5 c2e19238abc1c531e8a3607bf99ade6e
BLAKE2b-256 a2e2e43b6a1a977364fd0df2e845596f62858c70f1a71fccbdb3f8b4afa55a03

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 e921b771af7cee974eb86800df641f43dc71ec457e364980092ce47c4960e4eb
MD5 bd47c947d4dec3b72edc70bcce50dac9
BLAKE2b-256 7c7d96b3d598c6b2a97a05173f22f6a378f16e443c786486c120a7d1252bb190

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp310-cp310-manylinux_2_34_x86_64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp310-cp310-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp310-cp310-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 6917153921ea6ebcba53fc7eb4191ca0d9f2d616266046d3c42e5efd72f34da0
MD5 ea3d963f68506d90ab7017d0d09a76a7
BLAKE2b-256 ddf8452478b5d5a956f1bb02153ae85682a64961a642e9a02157cea538f1a600

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp310-cp310-manylinux_2_34_aarch64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 70d38173186afa3bb5d3424a122d36aa6ca3c4e8a15658f6bdd308f6a759f00b
MD5 31ec5b5d696dc6dc1e8114f3e89cc3e1
BLAKE2b-256 ee367b61c2c002a8b379be3d7172b07f78badf84481ce037e286917870517768

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp310-cp310-macosx_11_0_arm64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp39-cp39-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 778a5683ead03c62c8056526f854dfe30f4a83f94499271bd6a98c3c9b002385
MD5 81d8a1f68f389a1fa92e216dbcf342f2
BLAKE2b-256 51072c16892eea6671b17aab68226fb5135468ecda37b70240ede8a7e405425b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp39-cp39-manylinux_2_34_x86_64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp39-cp39-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp39-cp39-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 844423ac4e406f821646808a95df6166a230a65175133ec7872129efda5f2c65
MD5 3886d85f0cf32cb200540809387b4555
BLAKE2b-256 82d3a4383313475bc108ff27a683a71ccd657c45966f73d2fed0ca12e48feb28

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp39-cp39-manylinux_2_34_aarch64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyganak-2.6.3-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.3-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b91e756cb089bcbd3ae2ea7d4cafe55ea1d59c47245ffcfb04f45368e1b6e769
MD5 b5f9209e11d277896cdd4ad311252512
BLAKE2b-256 8476f108c41405fbcb9d1349c39256d92bc3cf7c408ef03c22424bcc6d01daca

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.3-cp39-cp39-macosx_11_0_arm64.whl:

Publisher: python-wheels.yml on meelgroup/ganak

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page