Skip to content

Commit

Permalink
Python: add BooleanFunction.sat_count using arbitrary precision int…
Browse files Browse the repository at this point in the history
…egers
  • Loading branch information
nhusung committed Nov 20, 2024
1 parent 5e31352 commit 6f35ad5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion bindings/python/oxidd/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,24 @@ def valid(self, /) -> bool:
"""
raise NotImplementedError

@abstractmethod
def sat_count(self, /, vars: int) -> int:
"""Count the exact number of satisfying assignments.
Uses arbitrary precision integers.
Locking behavior: acquires the manager's lock for shared access.
Args:
vars: Assume that the function's domain has this many variables.
"""
raise NotImplementedError

@abstractmethod
def sat_count_float(self, /, vars: int) -> float:
"""Count the number of satisfying assignments.
"""Count the (approximate) number of satisfying assignments.
Uses floating point numbers.
Locking behavior: acquires the manager's lock for shared access.
Expand Down
5 changes: 4 additions & 1 deletion crates/oxidd-ffi-python/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ oxidd-core = { workspace = true }
oxidd-dump = { workspace = true, features = ["dddmp", "dot"] }
oxidd = { workspace = true, features = ["bdd", "bcdd", "zbdd"] }

pyo3 = { version = "0.23.1", features = ["extension-module", "abi3-py39"] }
pyo3 = { version = "0.23.1", features = ["extension-module", "abi3-py39", "num-bigint"] }

# big integers
num-bigint = "0.4"

# fast hash function
#
Expand Down
18 changes: 18 additions & 0 deletions crates/oxidd-ffi-python/src/bcdd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::hash::BuildHasherDefault;
use std::path::PathBuf;

use num_bigint::BigUint;
use pyo3::prelude::*;
use pyo3::types::{PyBool, PyList, PyNone, PyTuple, PyType};
use rustc_hash::FxHasher;
Expand Down Expand Up @@ -695,6 +696,23 @@ impl BCDDFunction {
self.0.valid()
}

/// Count the number of satisfying assignments.
///
/// Locking behavior: acquires the manager's lock for shared access.
///
/// Args:
/// vars (int): Assume that the function's domain has this many
/// variables.
///
/// Returns:
/// int: The exact number of satisfying assignments
fn sat_count(&self, py: Python, vars: LevelNo) -> BigUint {
py.allow_threads(move || {
self.0
.sat_count::<BigUint, BuildHasherDefault<FxHasher>>(vars, &mut Default::default())
})
}

/// Count the number of satisfying assignments.
///
/// Locking behavior: acquires the manager's lock for shared access.
Expand Down
18 changes: 18 additions & 0 deletions crates/oxidd-ffi-python/src/bdd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::hash::BuildHasherDefault;
use std::path::PathBuf;

use num_bigint::BigUint;
use pyo3::prelude::*;
use pyo3::types::{PyBool, PyList, PyNone, PyTuple, PyType};
use rustc_hash::FxHasher;
Expand Down Expand Up @@ -693,6 +694,23 @@ impl BDDFunction {
self.0.valid()
}

/// Count the number of satisfying assignments.
///
/// Locking behavior: acquires the manager's lock for shared access.
///
/// Args:
/// vars (int): Assume that the function's domain has this many
/// variables.
///
/// Returns:
/// int: The exact number of satisfying assignments
fn sat_count(&self, py: Python, vars: LevelNo) -> BigUint {
py.allow_threads(move || {
self.0
.sat_count::<BigUint, BuildHasherDefault<FxHasher>>(vars, &mut Default::default())
})
}

/// Count the number of satisfying assignments.
///
/// Locking behavior: acquires the manager's lock for shared access.
Expand Down
18 changes: 18 additions & 0 deletions crates/oxidd-ffi-python/src/zbdd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::hash::BuildHasherDefault;
use std::path::PathBuf;

use num_bigint::BigUint;
use pyo3::prelude::*;
use pyo3::types::{PyBool, PyList, PyNone, PyTuple};
use rustc_hash::FxHasher;
Expand Down Expand Up @@ -580,6 +581,23 @@ impl ZBDDFunction {
self.0.valid()
}

/// Count the number of satisfying assignments.
///
/// Locking behavior: acquires the manager's lock for shared access.
///
/// Args:
/// vars (int): Assume that the function's domain has this many
/// variables.
///
/// Returns:
/// int: The exact number of satisfying assignments
fn sat_count(&self, py: Python, vars: LevelNo) -> BigUint {
py.allow_threads(move || {
self.0
.sat_count::<BigUint, BuildHasherDefault<FxHasher>>(vars, &mut Default::default())
})
}

/// Count the number of satisfying assignments.
///
/// Locking behavior: acquires the manager's lock for shared access.
Expand Down

0 comments on commit 6f35ad5

Please sign in to comment.