Skip to main content

A Numpy and Numba based Python library for solving Constraint Satifaction Problems

Project description

NUCS

TLDR

NUCS is a Python library for solving Constraint Satisfaction and Optimization Problems. Because it is 100% written in Python, NUCS is easy to install and use. NUCS is also very fast because it is powered by Numpy and Numba.

With NUCS, in a few seconds you can ...

Compute the 92 solutions to the BIBD(8,14,7,4,3) problem:

{
    'OPTIMIZER_SOLUTION_NB': 0,
    'PROBLEM_FILTER_NB': 2797,
    'PROBLEM_PROPAGATOR_NB': 462,
    'PROBLEM_VARIABLE_NB': 504,
    'PROPAGATOR_ENTAILMENT_NB': 36977,
    'PROPAGATOR_FILTER_NB': 564122,
    'PROPAGATOR_FILTER_NO_CHANGE_NB': 534436,
    'PROPAGATOR_INCONSISTENCY_NB': 1307,
    'SOLVER_BACKTRACK_NB': 1398,
    'SOLVER_CHOICE_DEPTH': 41,
    'SOLVER_CHOICE_NB': 1398,
    'SOLVER_SOLUTION_NB': 92
}

Demonstrate that the optimal 10-marks Golomb ruler length is 55:

{
    'OPTIMIZER_SOLUTION_NB': 10,
    'PROBLEM_FILTER_NB': 22204,
    'PROBLEM_PROPAGATOR_NB': 82,
    'PROBLEM_VARIABLE_NB': 45,
    'PROPAGATOR_ENTAILMENT_NB': 416934,
    'PROPAGATOR_FILTER_NB': 2145268,
    'PROPAGATOR_FILTER_NO_CHANGE_NB': 1129818,
    'PROPAGATOR_INCONSISTENCY_NB': 11065,
    'SOLVER_BACKTRACK_NB': 11064,
    'SOLVER_CHOICE_DEPTH': 9,
    'SOLVER_CHOICE_NB': 11129,
    'SOLVER_SOLUTION_NB': 10
 }

Find all 14200 solutions to the 12-queens problem:

{
    'OPTIMIZER_SOLUTION_NB': 0,
    'PROBLEM_FILTER_NB': 262011,
    'PROBLEM_PROPAGATOR_NB': 3,
    'PROBLEM_VARIABLE_NB': 36,
    'PROPAGATOR_ENTAILMENT_NB': 0,
    'PROPAGATOR_FILTER_NB': 1910609,
    'PROPAGATOR_FILTER_NO_CHANGE_NB': 631079,
    'PROPAGATOR_INCONSISTENCY_NB': 116806,
    'SOLVER_BACKTRACK_NB': 131005,
    'SOLVER_CHOICE_DEPTH': 10,
    'SOLVER_CHOICE_NB': 131005,
    'SOLVER_SOLUTION_NB': 14200
}

How to use NUCS ?

It is very simple to get started with NUCS. You can either install the Pip package or install NUCS from the sources.

Install the NUCS package

Let's install the Pip package for NUCS:

pip install nucs

Now we can write the following queens.py program (please refer to the technical documentation to better understand how NUCS works under the hood):

from nucs.problems.problem import Problem
from nucs.solvers.backtrack_solver import BacktrackSolver
from nucs.propagators.propagators import ALG_ALLDIFFERENT

n = 8  # the number of queens
problem = Problem(
    [(0, n - 1)] * n,  # these n domains are shared between 3n variables with different offsets
    list(range(n)) * 3,  # for each variable, its domain
    [0] * n + list(range(n)) + list(range(0, -n, -1))  # for each variable, its offset
)
problem.add_propagator((list(range(n)), ALG_ALLDIFFERENT, []))
problem.add_propagator((list(range(n, 2 * n)), ALG_ALLDIFFERENT, []))
problem.add_propagator((list(range(2 * n, 3 * n)), ALG_ALLDIFFERENT, []))
print(BacktrackSolver(problem).solve_one()[:n])

Let's run this model with the following command:

NUMBA_CACHE_DIR=.numba/cache PYTHONPATH=. python queens.py

The first solution found is:

[0, 4, 7, 5, 2, 6, 1, 3]

[!TIP] Note that the second run will always be much faster since the Python code will already have been compiled and cached by Numba.

Install NUCS from the sources

Let's install NUCS from the sources by cloning the NUCS Github repository:

git clone https://github.com/yangeorget/nucs.git
pip install -r requirements.txt

From there, we will launch some NUCS examples.

Run some examples

Some of the examples come with a command line interface and can be run directly.

Let's find all solutions to the 12-queens problem:

NUMBA_CACHE_DIR=.numba/cache PYTHONPATH=. python tests/examples/test_queens.py -n 12

Let's find the optimal solution to the Golomb ruler problem with 10 marks:

NUMBA_CACHE_DIR=.numba/cache PYTHONPATH=. python tests/examples/test_golomb.py -n 10

Other constraint solvers in Python

CCPMpy

CPMpy is a Constraint Programming and Modeling library in Python, based on numpy, with direct solver access.

Numberjack

Numberjack is a modelling package written in Python for combinatorial optimisation.

python-constraint

The Python constraint module offers solvers for Constraint Solving Problems (CSPs) over finite domains in simple and pure Python.

PyCSP

PyCSP3 is a Python library for developping models of combinatorial constrained problems in a declarative manner; you can write models of constraint satisfaction (CSP) and constraint optimization (COP) problems.

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

nucs-0.2.0.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

NUCS-0.2.0-py3-none-any.whl (29.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nucs-0.2.0.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for nucs-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0177c8d44b5789e4d47f3bd200108e6d8bab544f94fa2b3aca8da087ddf1e94b
MD5 11eb29576e267a37ab10ffb4a37265a2
BLAKE2b-256 a78faa73b49c2da72ed1f2e0510d32be01a8f2094e69926f19bdf0683b78b6f9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: NUCS-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 29.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for NUCS-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f176aa09c6e4895cf2bbf067ab7a0cc4b7f4aa14992956660ac329c32c5c2333
MD5 a82b76732e3dd7606a2b3e1997bb19f5
BLAKE2b-256 0803028b549aa54f3f52882d4b45eef64f74b9284aab7cd9122a8e18ce3329f2

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