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.1-cp313-cp313-manylinux_2_34_x86_64.whl (5.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

pyganak-2.6.1-cp313-cp313-manylinux_2_34_aarch64.whl (4.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ ARM64

pyganak-2.6.1-cp313-cp313-macosx_11_0_arm64.whl (4.0 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyganak-2.6.1-cp312-cp312-manylinux_2_34_x86_64.whl (5.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

pyganak-2.6.1-cp312-cp312-manylinux_2_34_aarch64.whl (4.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ ARM64

pyganak-2.6.1-cp312-cp312-macosx_11_0_arm64.whl (4.0 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pyganak-2.6.1-cp311-cp311-manylinux_2_34_x86_64.whl (5.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

pyganak-2.6.1-cp311-cp311-manylinux_2_34_aarch64.whl (4.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ ARM64

pyganak-2.6.1-cp311-cp311-macosx_11_0_arm64.whl (4.0 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

pyganak-2.6.1-cp310-cp310-manylinux_2_34_x86_64.whl (5.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

pyganak-2.6.1-cp310-cp310-manylinux_2_34_aarch64.whl (4.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ ARM64

pyganak-2.6.1-cp310-cp310-macosx_11_0_arm64.whl (4.0 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

pyganak-2.6.1-cp39-cp39-manylinux_2_34_x86_64.whl (5.0 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ x86-64

pyganak-2.6.1-cp39-cp39-manylinux_2_34_aarch64.whl (4.6 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ ARM64

pyganak-2.6.1-cp39-cp39-macosx_11_0_arm64.whl (4.0 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

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

File metadata

File hashes

Hashes for pyganak-2.6.1-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 0aa38a3c842b8ec0d14ecf700ebe6dde1b8ae46be7c256a2a9066efc9b4745e0
MD5 f82cb86bfcad38dc363ae8c9f00256ba
BLAKE2b-256 8f42b484c9fd66b5aeda5dcf42ef3e98320386329739bc5917273c497ed0c412

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp313-cp313-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp313-cp313-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 959c33715d65a10cd99c08b5b3a1af173722f57c1a3cff8cef2d15fa90234f36
MD5 182191f1e8895b8e71fe1511d5958038
BLAKE2b-256 a3b53c4ab1d2748abc294d6161a40fa227d2a424527570bc215438f2e5b3f90c

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 73770c26af1bf5e070ddf70e7f46344092b00005e1bf8a0b15009f2bc59a2cfe
MD5 9ef72b4f48d1588395fc4e4af317ffae
BLAKE2b-256 45f5e894c1dbcf4ae576894c6bdf10218e5d8f9b13742e2d2588ec7eb23744a0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp312-cp312-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 dce8ed4c5b931148e69ec8f043ab8b3685b17555ef2aecd1378180edf5a13189
MD5 97f0927cf6ff4de8f2ff8a9c4a290b97
BLAKE2b-256 96f64075dfe44a41669ae6402ab8b027ef0faca3ec90a6819fdeca7c669e3b15

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp312-cp312-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp312-cp312-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 2170428c9e9af068fd0adb6f72dd04eea4f09be1655d501c9d029a9590521cb2
MD5 3acc6d96f3c23b0f71d917be12e71be7
BLAKE2b-256 49ee81bbeef7dd350983aa858ab5e8d4f5c7bd148218ae0ae02d881ba2b14b0a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 054ea5932cbb7033c4683872b40b95dc09b2321cbe29ba485e6b6a7c0b97209f
MD5 26f0ebc832b0fae6da7c19de42ed2bec
BLAKE2b-256 0ef4a913260e98e9a6060555f370fd1646165fff556c14d2c3fd9491cf226326

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp311-cp311-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 77975f64fd758fe6fc9b2d137a62fb447b3cd66787bc7f94d2e587efce3b109e
MD5 40f265557832923566fd11b7c31d27ce
BLAKE2b-256 cd6f2488bd7a8152d73dfa245033a288be33af94a4706d4d06e95390152e7329

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp311-cp311-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp311-cp311-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 dc82f5ccff110c76abc658bbcaa98ed01781388f1c90b15a7e20b8102f12e5f8
MD5 0d1c6b622061336f4057893f7b3c1829
BLAKE2b-256 d76644e93117ac08a7e2c3d2e937c454fa5391e37c48ead7809f3b5c615d761a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 dc6a4f91e6beccdc9ddeb556fcc709d13357701084bcf748aebb04b03be731e1
MD5 73db48f2f9cf2d032ac0b660a030fd65
BLAKE2b-256 32ccdfa5c6017ea71a9a981346eca24026a225d7007e653ca29b0886bf9afdf0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp310-cp310-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 09591bd6f260b9bd6ccb8d33142156a6138ce31788ef4cda0b4c5f0f889c7663
MD5 79ebadb36cafec7dc4adef6abd9feba6
BLAKE2b-256 da3242ba6ca8c9bc6ddab29c0fb0d08f38540612bb2930a6093890055e4a462b

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp310-cp310-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp310-cp310-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 61f1a776fd2558ea755d6ee388cc51cfa58845b73118586fd6cfeca37a1db9a9
MD5 93c0913a3c20aa5effaae390469930e0
BLAKE2b-256 c17eca5f7c18ebcb2129918a696c68f71a7df28e96982329dfe7c7573ed6c79f

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 840502cc979f39f47e4a757f70fd53c97640533a1e68d79decc041b36c1519fc
MD5 96fc65ac922ccd64e596d7b802c2680a
BLAKE2b-256 6976a2f0ce98ff5d80bf82275cac90cc53f14ad2ed9466fda0b4a2a3be7c542d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp39-cp39-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 fb289fd90a44232b6544eb1a80ff0217df285ceb06601fd3a0dc7b107322891e
MD5 5247dc45b436288e43219cc137deb75b
BLAKE2b-256 e084caea1190f4df4016636870e17b5995d927d155b27600bfb42db90f9c55d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp39-cp39-manylinux_2_34_aarch64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp39-cp39-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 083b791c7c410003e5900467e57f24ad7b8a9fd23372973815f4d0ae4d73bb4b
MD5 30d95bd387d512683419e0e8f6cf320e
BLAKE2b-256 faa8613ac458e48a999718343f5a6eea5beddb78397247eb69136096c551e6b4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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.1-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyganak-2.6.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3f649e658628de3d2137c866538f1cd024795935797814ab09b85329886111ea
MD5 ba8f7e74303ae6b888ab99ec49cf3514
BLAKE2b-256 65b331062285fcc18118045af67d210958f0aedd5012f73301772ff98b9aafd8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyganak-2.6.1-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