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

Uploaded CPython 3.13manylinux: glibc 2.34+ x86-64

pyganak-2.6.2-cp313-cp313-manylinux_2_34_aarch64.whl (5.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.34+ ARM64

pyganak-2.6.2-cp313-cp313-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

pyganak-2.6.2-cp312-cp312-manylinux_2_34_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ x86-64

pyganak-2.6.2-cp312-cp312-manylinux_2_34_aarch64.whl (5.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.34+ ARM64

pyganak-2.6.2-cp312-cp312-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

pyganak-2.6.2-cp311-cp311-manylinux_2_34_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ x86-64

pyganak-2.6.2-cp311-cp311-manylinux_2_34_aarch64.whl (5.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.34+ ARM64

pyganak-2.6.2-cp311-cp311-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

pyganak-2.6.2-cp310-cp310-manylinux_2_34_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ x86-64

pyganak-2.6.2-cp310-cp310-manylinux_2_34_aarch64.whl (5.0 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.34+ ARM64

pyganak-2.6.2-cp310-cp310-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

pyganak-2.6.2-cp39-cp39-manylinux_2_34_x86_64.whl (5.4 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ x86-64

pyganak-2.6.2-cp39-cp39-manylinux_2_34_aarch64.whl (5.0 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.34+ ARM64

pyganak-2.6.2-cp39-cp39-macosx_11_0_arm64.whl (4.4 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

File details

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp313-cp313-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 900f963462558708bef50e80b37640646d0a87c3302df91effa1f78a502f91b8
MD5 d99c7c933f06fa7c7e527060f0ce5759
BLAKE2b-256 61e698fc2a9c2b0491c168c707121b224c066017e4632aaf96a518d3d109a910

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp313-cp313-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 5426499ee73758109f35933672b10b4d3860aca902498a8d8813b9efe446b26d
MD5 0be2c4791b1eb754ff3a9264506759b6
BLAKE2b-256 83da0762e6b5fb36eeb731706b189a5da24edfee53960717dfc8bad539fd6987

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bf58c64d92779998f1598f4b0854b0c23e56581d0a07fa27c03479f85b28ab70
MD5 cfaa2c4cdfbcb2354e7f604fa913b158
BLAKE2b-256 7af25149ef2ea45af616389d48838990d0b01150730313224beb51f7cf2edcd0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp312-cp312-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 911606412d183c61a62939e4f842a3b4b4c42916cb90e1028120cf48f0d15f91
MD5 43136f671017b19868418a7b653a3dab
BLAKE2b-256 bfe0d49393bb3eea5a76b860166b878f43b2686b00b2ea2b4efedd7172c29518

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp312-cp312-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 67ce556b2d62978b42f8b47cca5a1f8c4fe294020c284e34dfed0bfb48976598
MD5 2c410c2b973347e9f88f28d2a31bf574
BLAKE2b-256 8522f2cfe198eb75210e2894de5aee8ba6f4add2a9fa61d2330990eaade5c6f7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 be30f9b12e1d1e9cebed789be6c92dccb861624a3f89b3b26ace610531c6397a
MD5 ed0359291b412540446c6730dfa95e30
BLAKE2b-256 5e0d2476dfeadd2cc44580987c1b6b4ffb54c15e588526fcdcd9550f6d4fe2f9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp311-cp311-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 9041f0af1d29784ea3d6704776758e79333462c3347f439b882c5cc44f4bd0dc
MD5 48e085ab76f09396bbfc8e79b63e4172
BLAKE2b-256 3501debd094bba4b476b4577df256ddfaba40bdf710b0a017c316fe4ca8fc672

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp311-cp311-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 7664d93b4783fd3866a50325ec0605057f98531bc23522715c92e1a6c5ab8ab3
MD5 2aaa38cef5e98b2bf4195a36d12f4358
BLAKE2b-256 f51d62c2edc56733bf263bd7a3428fbc61bb19ad8f807c9431b81c2c061799ec

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6d470fc8a753f4aa6df8ed07b9c85f65591eb0896b0e9d56801297ce78b15ced
MD5 02ab62cde001cb34a59c4d4262454935
BLAKE2b-256 ebdbead8b42e2355a8107c9584c8a2a237a63ae85fae76196fac8b12f28fae25

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp310-cp310-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 a5eb8da1845b36fb6a5f294ba74acf11e01c1883d0a666ac04ac6f20039e9240
MD5 220a802d4ea22cb42766d96ee0cab302
BLAKE2b-256 5cd8f547b0dad9e460d4b89dcad4b5cc0dd4ef051bdd838491d167341f9436bc

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp310-cp310-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 5675b83ef716d714f29b3f4e9f4830fae3d0f3f7c65211aa3beb13362fb1c34b
MD5 29a40924997b38901643f33e4014b17b
BLAKE2b-256 aba633f64274468eacead54617962d07bae06aadfb7015663d3e1f0733e4ebcf

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 200951ad72bae20a2451da5e13c1ff46b315e4f9eb2a61364f4baa06a0042fed
MD5 be9267c13b52c5f216544fa38fe572fe
BLAKE2b-256 e1c0142b4385331111e61e5fa3fe798d8eba42ef9f48c7f2cf4d9bfc9ec2dcd9

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp39-cp39-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 58665d2e0563bf600f5efea2f6cc49ac13bd0745786154506a4b9cafad33d0cb
MD5 902fab18db955bdedc19ec623ccbfe97
BLAKE2b-256 f6a0afe6a221f8b4736c050211febf6a8ac48853689b1539b189935b20f11a7d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp39-cp39-manylinux_2_34_aarch64.whl
Algorithm Hash digest
SHA256 0c603e57d27d8db5bd7e6ea7b43f68c5d0569c75326cc4f1c7800f929b688860
MD5 4b148010d2e4b1ef56aea5906b72f77b
BLAKE2b-256 be8912601edb84a02de17d331a2a7cf4758331b5da8fc48400a3f3b95bf8dea7

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for pyganak-2.6.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 12e82c7419f3a83298e126683c1a2ab3deb2c133cb967df8d13488a1db9cc9db
MD5 37535c7c2b436e8dcd6137aac1c9f9fd
BLAKE2b-256 e9e3e2b298aea2b0e6f159fa5b9da9515caaa6e8b1718f7980b017d7c69b77c8

See more details on using hashes here.

Provenance

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