Formula#

class pybsp_rust.Formula#

Immutable normalized CNF formula with one-based signed literals.

Parameters:
  • clauses (typing.Sequence of typing.Sequence of int) – Each inner sequence is a disjunction. Positive i means x_i, negative -i means not x_i. Zero is invalid here (it is a DIMACS terminator only).

  • n (int or None, default None) – Declared variable count. None infers the largest absolute literal, or zero for an empty formula. Explicit n retains unused variables.

Notes

Repeated literals collapse, literals sort by variable/sign, and tautological clauses disappear. Repeated whole clauses and empty clauses remain. Copies share immutable Rust topology. Input errors raise ValueError; integer conversion outside native ranges can raise OverflowError.

classmethod __new__(*args, **kwargs)#
clauses#

Owned list of normalized clauses, each containing signed one-based literals.

Returns:

list of list of int – Owned normalized clauses in native incidence order.

edges#

Number of literal incidences after normalization.

Returns:

int – Number of normalized literal incidences.

static feistel(k, alpha, n, seed=1, ensemble='fluctuating')#

Construct an experimental implicit Feistel k-SAT formula.

Parameters:
  • k (int) – Clause width, between 1 and min(n, 64).

  • alpha (float) – Clause density; the clause count is floor(alpha * n). Fluctuating accepts zero; partitioned requires at least one clause.

  • n (int) – Variable count, at most i32::MAX.

  • seed (int, default 1) – Unsigned 64-bit generation seed, independent of the solver seed.

  • ensemble ({'fluctuating', 'partitioned'}, default 'fluctuating') – Fluctuating targets random k-SAT; partitioned uses balanced degrees and fixed variable groups, producing a different ensemble.

Returns:

Formula – Implicit topology supported by CPU, CPU-parallel, and Metal solvers.

Raises:
  • ValueError – If the ensemble or formula parameters are invalid.

  • OverflowError – If an integer exceeds the native range.

Notes

Construction releases the GIL. Fluctuating scans clauses for repairs. Solver state still scales with the instance and has u32 capacity limits. Accessing clauses or DIMACS materializes the output.

static from_dimacs(text)#

Parse DIMACS text with one p cnf N M header and zero-terminated clauses.

The header counts input clauses before normalization. Comments beginning with c and blank lines are ignored. Invalid text raises ValueError.

Parameters:

text (str) – DIMACS input with one p cnf N M header and zero-terminated clauses.

Returns:

Formula – Normalized immutable formula retaining declared variable IDs.

Raises:

ValueError – If header counts, literal bounds, or clause termination are invalid.

is_satisfied(assignment)#

Return whether a complete 0/1 assignment satisfies every original clause.

Requires exactly n values. A wrong length, -1 entries, or values other than 0/1 return False. An empty clause is always unsatisfied.

Parameters:

assignment (typing.Sequence of int) – Complete assignment in variable order, encoded as 0/1.

Returns:

bool – True exactly when the length is n, values are 0/1, and all clauses are satisfied. Other lengths or int8 values return False.

Raises:
  • OverflowError – If a value cannot be converted to the native signed 8-bit type.

  • TypeError – If the input cannot be converted to a sequence of integers.

m#

Number of clauses after normalization.

Returns:

int – Number of clauses after normalization.

n#

Declared number of variables, including unused ones.

Returns:

int – Declared variable count, including unused variables.

static random(k, alpha, n, seed=1)#

Generate random k-SAT with floor(alpha*n) independent clauses.

Each clause has k distinct variables sampled uniformly, with independent unbiased signs. Requires 1 <= k <= n, finite alpha >= 0, and capacity within packed u32 incidence limits. The unsigned 64-bit seed controls formula generation, independently of Options.seed. Releases the GIL.

Parameters:
  • k (int) – Distinct variables per clause, in [1, n].

  • alpha (float) – Finite nonnegative clause density; m is floor(alpha * n).

  • n (int) – Declared variable count, at least k and within packed graph limits.

  • seed (int, default 1) – Unsigned 64-bit formula-generation seed, independent of the solver seed.

Returns:

Formula – Random k-SAT instance with independently sampled clauses and signs.

Raises:
  • ValueError – If dimensions, density, or packed graph capacity are invalid.

  • OverflowError – If integer conversion exceeds the native range.

to_dimacs()#

Serialize normalized clauses as DIMACS, preserving the declared n.

Returns:

str – DIMACS text including the original declared variable count.

topology#

Feistel generator metadata, or None for an explicit formula.

The dictionary contains generator, ensemble, k, n, m, edges, seed, repaired_clauses and topology_bytes. The byte count excludes allocator overhead and solver state.

See Formulas, indices, and arrays for normalization examples, literal/array index conventions, capacity limits, and the normalized incidence order.