Skip to main content

A DIMACS CNF compiler

Project description

cnfc

A Python package that generates DIMACS CNF files.

A CNF compiler that generates compact encodings from higher-level primitives that are commonly used for solving combinatorial problems.

Instead of providing an integrated encoder-solver environment, this package generates DIMACS CNF files only. This is a useful intermediate step for difficult problems that might take hours or days to solve, since having a DIMACS CNF input allows one to move to a more powerful machine or cluster and experiment with different preprocessors and solvers.

Example:

from cnfc import *

f = Formula()
x, y, z, w = f.AddVars('x y z w')

f.AddClause(~x,y)  # Equivalent to f.Add(Or(~x,y)).
f.AddClause(~y,z)
f.AddClause(~z,x)

# Equivalent to f.Add(Or(Implies(x,y),Not(And(z,w)))).
f.Add(Implies(x,y) | ~(z & w))

# Assert that at least one of x,y, or z is true.
f.Add(NumTrue(x,y,z) >= 1)

# Assert that the tuple (x,y) is lexicographically less than (z,w).
f.Add(Tuple(x,y) < Tuple(z,w))

# Write the resulting CNF file to /tmp/output.cnf.
with open('/tmp/output.cnf', 'w') as fd:
    f.WriteCNF(fd)

The above script will generate:

p cnf 11 28
-1 2 0
-2 3 0
-3 1 0
-1 2 -5 0
5 1 0
5 -2 0
-3 -4 6 0
-6 3 0
-6 4 0
5 -6 0
-8 1 2 0
-1 8 0
-2 8 0
7 -1 -2 0
1 -7 0
2 -7 0
-10 7 3 0
-7 10 0
-3 10 0
9 -7 -3 0
7 -9 0
3 -9 0
8 10 0
-1 3 0
-1 11 0
3 11 0
-2 -11 0
4 -11 0

Status

Basic functionality (boolean operations, cardinality tests, lexicographic tuple comparisons) is implemented.

Next up: finish supporting all more complicated compound statements (an or-of-cardinality-constraints, etc.)

Installation

pip install cnfc

Development

Install poetry and run poetry install. Then you can bring up a shell, etc. Run tests with:

poetry run python3 -m unittest discover

Project details


Download files

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

Source Distribution

cnfc-0.2.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

cnfc-0.2.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file cnfc-0.2.0.tar.gz.

File metadata

  • Download URL: cnfc-0.2.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.9.2 Linux/5.15.108-18910-gab0e1cb584e1

File hashes

Hashes for cnfc-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1fb62aa23b421d7adc7f564e92779e5618c6659d1477322645739ca32de0cbf6
MD5 9a7cc8e590dfd6647f0ac25e26084471
BLAKE2b-256 aa68ef6890a028c7f54166137af8f9a4f3d68df211e1f505a039b71167b39c15

See more details on using hashes here.

File details

Details for the file cnfc-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: cnfc-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.9.2 Linux/5.15.108-18910-gab0e1cb584e1

File hashes

Hashes for cnfc-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a7c4630bd8ff611785b020a01ce5fe0bca07ead3844359a277dd9eb8e57e9015
MD5 787658da503cb333a04d94a841dc27c3
BLAKE2b-256 5de54c2ef78085c1b37cf3a84fd12665d82bcdb830f7bc44b05fadae1dc07077

See more details on using hashes here.

Supported by

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