NuCS: A Constraint Solver for Analysis, Educating, and Manufacturing Functions | by Yan Georget | Nov, 2024
NuCS is a Python library for fixing Constraint Satisfaction and Optimisation Issues (CSP and COP) that I’m creating as a aspect challenge. As a result of it’s 100% written in Python, NuCS is simple to put in and permits to mannequin advanced issues in just a few traces of code. The NuCS solver can also be very quick as a result of it’s powered by Numpy and Numba.
Many issues will be formulated as CSPs. Because of this a constraint library corresponding to NuCS can profit a number of builders or information scientists.
Let’s take into account the well-known N-queens drawback which consists in putting N queens on a N x N chessboard such that the queens do not threaten one another.
The 14200 options to the 12-queens issues are present in lower than 2s on a MacBook Professional M2 working:
- Python 3.11,
- Numpy 2.0.1,
- Numba 0.60.0 and
- NuCS 3.0.0.
(venv) ➜ nucs git:(principal) time NUMBA_CACHE_DIR=.numba/cache python -m nucs.examples.queens -n 12 --log_level=ERROR --processors=6
{
'ALG_BC_NB': 262006,
'ALG_BC_WITH_SHAVING_NB': 0,
'ALG_SHAVING_NB': 0,
'ALG_SHAVING_CHANGE_NB': 0,
'ALG_SHAVING_NO_CHANGE_NB': 0,
'PROPAGATOR_ENTAILMENT_NB': 0,
'PROPAGATOR_FILTER_NB': 2269965,
'PROPAGATOR_FILTER_NO_CHANGE_NB': 990435,
'PROPAGATOR_INCONSISTENCY_NB': 116806,
'SOLVER_BACKTRACK_NB': 131000,
'SOLVER_CHOICE_NB': 131000,
'SOLVER_CHOICE_DEPTH': 10,
'SOLVER_SOLUTION_NB': 14200
}
NUMBA_CACHE_DIR=.numba/cache python -m nucs.examples.queens -n 12 6.65s person 0.53s system 422% cpu 1.699 whole
Constraint programming is a paradigm for fixing combinatorial issues. In constraint programming, customers declaratively state the constraints on the possible options for a set of determination variables. Constraints specify the properties of an answer to be discovered. The solver combines constraint propagation and backtracking to search out the options.
For instance, here’s a mannequin for the Magic Sequence Problem (discover a sequence x_0, … x_n-1 such that, for every i in [0, n-1], x_i is the variety of occurrences of i within the sequence) utilizing NuCS:
class MagicSequenceProblem(Drawback):
def __init__(self, n: int):
tremendous().__init__([(0, n)] * n)
for i in vary(n):
self.add_propagator((checklist(vary(n)) + [i], ALG_COUNT_EQ, [i]))
# redundant constraints
self.add_propagator((checklist(vary(n)), ALG_AFFINE_EQ, [1] * n + [n]))
self.add_propagator((checklist(vary(n)), ALG_AFFINE_EQ, checklist(vary(n)) + [n]))
In NuCS, a constraint is called a propagator.
The propagator (right here ALG_COUNT_EQ) merely states that x_i is the variety of occurrences of i within the sequence. The next two ALG_AFFINE_EQ propagators are redundant, which means that they don’t seem to be essential for NuCS to search out the answer however they velocity up the decision course of.
See the documentation for an entire checklist of propagator supported by NuCS. Word that almost all propagators in NuCS are international (aka n-ary) and implement state-of-art propagation algorithms.
Python is the language of alternative for information scientists: it has a easy syntax, a rising neighborhood and a large number of information science and machine studying libraries.
However then again, Python is thought to be a gradual language : possibly 50 to 100 instances slower than C relying on the benchmarks.
The selection of Python for creating a excessive efficiency constraint programming library was not so apparent however we are going to see that the mixed use of Numpy (excessive efficiency computing package deal) and Numba (Simply-In-Time compilation for Python) helps lots.
Many makes an attempt have been made to write down constraint solvers in Python, however these are both gradual or are solely wrappers and depend upon exterior solvers written in Java or C/C++.
NumPy brings the computational energy of languages like C and Fortran to Python.
In NuCS, every thing is a Numpy array.
This permits to leverage Numpy’s indexing and broadcasting capabilities and to write down compact propagators corresponding to Max_i x_i <= y
def compute_domains_max_leq(domains: NDArray, parameters: NDArray) -> int:
x = domains[:-1]
y = domains[-1]
if np.max(x[:, MAX]) <= y[MIN]:
return PROP_ENTAILMENT
y[MIN] = max(y[MIN], np.max(x[:, MIN]))
if y[MIN] > y[MAX]:
return PROP_INCONSISTENCY
for i in vary(len(x)):
x[i, MAX] = min(x[i, MAX], y[MAX])
if x[i, MAX] < x[i, MIN]:
return PROP_INCONSISTENCY
return PROP_CONSISTENCY
Numba is an open supply Simply-In-Time compiler that interprets a subset of Python and NumPy code into quick machine code.
Within the following instance, we discover the 14200 options to the 12-queens issues (notice that we use a single processor right here).
NUMBA_DISABLE_JIT=1 python -m nucs.examples.queens -n 12 --log_level=ERROR 179.89s person 0.31s system 99% cpu 3:00.57 whole
We obtain a x60 speed-up by enabling Simply-In-Time compilation:
NUMBA_CACHE_DIR=.numba/cache python -m nucs.examples.queens -n 12 3.03s person 0.06s system 99% cpu 3.095 whole
So as to let Numba JIT-compile your code, you need to :
- keep away from OOP,
- use supported varieties or Numpy arrays,
- use a subset of the Python language,
- use a subset of Numpy’s features.
In NuCS, these pointers have been efficiently carried out for :
Because of Numpy and Numba, NuCS achieves efficiency much like that of solvers written in Java or C/C++.
Word that, for the reason that Python code is compiled and the outcome cached, efficiency will all the time be considerably higher whenever you run your program a second time.
NuCS comes with many models for traditional constraint programming issues corresponding to:
- some crypto-arithmetic puzzles: Alpha, Donald,
- the Balanced Incomplete Block Design drawback,
- the Golomb ruler drawback,
- the knapsack drawback,
- the magic sequence drawback,
- the magic sq. drawback,
- the quasigroup drawback,
- the n-queens drawback,
- the Schur lemma drawback,
- the sports activities match scheduling drawback,
- the Sudoku drawback.
A few of these examples require some superior strategies:
- redundant constraints,
- customized heuristics,
- customized consistency algorithms
Most of those fashions are additionally out there in CSPLib, the bible for something CSP associated.
When options are looked for, NuCS additionally aggregates some statistics:
{
'ALG_BC_NB': 262006,
'ALG_BC_WITH_SHAVING_NB': 0,
'ALG_SHAVING_NB': 0,
'ALG_SHAVING_CHANGE_NB': 0,
'ALG_SHAVING_NO_CHANGE_NB': 0,
'PROPAGATOR_ENTAILMENT_NB': 0,
'PROPAGATOR_FILTER_NB': 2269965,
'PROPAGATOR_FILTER_NO_CHANGE_NB': 990435,
'PROPAGATOR_INCONSISTENCY_NB': 116806,
'SOLVER_BACKTRACK_NB': 131000,
'SOLVER_CHOICE_NB': 131000,
'SOLVER_CHOICE_DEPTH': 10,
'SOLVER_SOLUTION_NB': 14200
}
Right here we will see that:
- certain consistency was computed 262006 instances,
- 2268895 propagators had been utilized however with out impact 990435 instances whereas inconsistencies had been detected 116806 instances,
- they had been 131000 decisions and backtracks, with a most alternative depth of 10,
- lastly, 14200 options had been discovered.
Taking part in with the mannequin and understanding the way it impacts the statistics has confirmed to be a really helpful train in getting probably the most out of NuCS.
NuCS additionally comes with some fundamental logging capabilities.
NUMBA_CACHE_DIR=.numba/cache python -m nucs.examples.golomb -n 10 --symmetry_breaking --log_level=INFO
2024-11-12 17:27:45,110 - INFO - nucs.solvers.solver - Drawback has 82 propagators
2024-11-12 17:27:45,110 - INFO - nucs.solvers.solver - Drawback has 45 variables
2024-11-12 17:27:45,110 - INFO - nucs.solvers.backtrack_solver - BacktrackSolver makes use of variable heuristic 0
2024-11-12 17:27:45,110 - INFO - nucs.solvers.backtrack_solver - BacktrackSolver makes use of area heuristic 0
2024-11-12 17:27:45,110 - INFO - nucs.solvers.backtrack_solver - BacktrackSolver makes use of consistency algorithm 2
2024-11-12 17:27:45,110 - INFO - nucs.solvers.backtrack_solver - Alternative factors stack has a maximal peak of 128
2024-11-12 17:27:45,172 - INFO - nucs.solvers.backtrack_solver - Minimizing variable 8
2024-11-12 17:27:45,644 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) answer: 80
2024-11-12 17:27:45,677 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) answer: 75
2024-11-12 17:27:45,677 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) answer: 73
2024-11-12 17:27:45,678 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) answer: 72
2024-11-12 17:27:45,679 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) answer: 70
2024-11-12 17:27:45,682 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) answer: 68
2024-11-12 17:27:45,687 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) answer: 66
2024-11-12 17:27:45,693 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) answer: 62
2024-11-12 17:27:45,717 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) answer: 60
2024-11-12 17:27:45,977 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) answer: 55
{
'ALG_BC_NB': 22652,
'ALG_BC_WITH_SHAVING_NB': 0,
'ALG_SHAVING_NB': 0,
'ALG_SHAVING_CHANGE_NB': 0,
'ALG_SHAVING_NO_CHANGE_NB': 0,
'PROPAGATOR_ENTAILMENT_NB': 107911,
'PROPAGATOR_FILTER_NB': 2813035,
'PROPAGATOR_FILTER_NO_CHANGE_NB': 1745836,
'PROPAGATOR_INCONSISTENCY_NB': 11289,
'SOLVER_BACKTRACK_NB': 11288,
'SOLVER_CHOICE_NB': 11353,
'SOLVER_CHOICE_DEPTH': 9,
'SOLVER_SOLUTION_NB': 10
}
[ 1 6 10 23 26 34 41 53 55]
Lastly, NuCS is a really open platform had been virtually something will be custom-made:
- propagators,
- consistency algorithms,
- heuristics,
- solvers.
Within the following Golomb ruler instance, a customized consistency algorithm is registered earlier than getting used:
consistency_alg_golomb = register_consistency_algorithm(golomb_consistency_algorithm)
solver = BacktrackSolver(drawback, consistency_alg_idx=consistency_alg_golomb)
In conclusion, NuCS is a constraint solver library with a number of options. Though it’s written solely in Python, it is extremely quick and can be utilized for a variety of purposes: analysis, instructing and manufacturing.
Don’t hesitate to contact me on Github if you happen to’d like to participate in NuCS growth!
Some helpful hyperlinks to go additional:
In the event you loved this text about NuCS, please clap 50 instances !