• Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
Thursday, October 23, 2025
newsaiworld
  • Home
  • Artificial Intelligence
  • ChatGPT
  • Data Science
  • Machine Learning
  • Crypto Coins
  • Contact Us
No Result
View All Result
  • Home
  • Artificial Intelligence
  • ChatGPT
  • Data Science
  • Machine Learning
  • Crypto Coins
  • Contact Us
No Result
View All Result
Morning News
No Result
View All Result
Home Artificial Intelligence

NuCS: A Constraint Solver for Analysis, Instructing, and Manufacturing Functions | by Yan Georget | Nov, 2024

Admin by Admin
November 26, 2024
in Artificial Intelligence
0
04ixpwildlry S7hn.jpeg
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


Photograph by Eric Prouzet on Unsplash

Blazing-fast constraint fixing in pure Python

Yan Georget

Towards Data Science

NuCS is a Python library for fixing Constraint Satisfaction and Optimisation Issues (CSP and COP) that I’m growing as a facet challenge. As a result of it’s 100% written in Python, NuCS is simple to put in and permits to mannequin complicated issues in just a few strains of code. The NuCS solver can be very quick as a result of it’s powered by Numpy and Numba.

Many issues might be formulated as CSPs. That is why a constraint library reminiscent of NuCS can profit loads of builders or information scientists.

Let’s think about the well-known N-queens downside which consists in putting N queens on a N x N chessboard such that the queens do not threaten one another.

An answer to the 8-queens downside. Supply: Yue Guo

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:(primary) 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 complete

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 choice 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 Downside (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(Downside):
def __init__(self, n: int):
tremendous().__init__([(0, n)] * n)
for i in vary(n):
self.add_propagator((record(vary(n)) + [i], ALG_COUNT_EQ, [i]))
# redundant constraints
self.add_propagator((record(vary(n)), ALG_AFFINE_EQ, [1] * n + [n]))
self.add_propagator((record(vary(n)), ALG_AFFINE_EQ, record(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, that means that they aren’t obligatory for NuCS to search out the answer however they velocity up the decision course of.

See the documentation for an entire record of propagator supported by NuCS. Be aware 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 alternatively, Python is thought to be a sluggish language : perhaps 50 to 100 occasions slower than C relying on the benchmarks.

The selection of Python for growing 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 bundle) and Numba (Simply-In-Time compilation for Python) helps loads.

Many makes an attempt have been made to write down constraint solvers in Python, however these are both sluggish 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 part is a Numpy array.

This permits to leverage Numpy’s indexing and broadcasting capabilities and to write down compact propagators reminiscent of 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 (observe 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 complete

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 complete

With a purpose to let Numba JIT-compile your code, it’s best to :

  • keep away from OOP,
  • use supported sorts or Numpy arrays,
  • use a subset of the Python language,
  • use a subset of Numpy’s features.

In NuCS, these tips have been efficiently applied for :

Because of Numpy and Numba, NuCS achieves efficiency much like that of solvers written in Java or C/C++.

Be aware that, because the Python code is compiled and the outcome cached, efficiency will at all times be considerably higher once you run your program a second time.

NuCS comes with many fashions for traditional constraint programming issues reminiscent of:

  • some crypto-arithmetic puzzles: Alpha, Donald,
  • the Balanced Incomplete Block Design downside,
  • the Golomb ruler downside,
  • the knapsack downside,
  • the magic sequence downside,
  • the magic sq. downside,
  • the quasigroup downside,
  • the n-queens downside,
  • the Schur lemma downside,
  • the sports activities match scheduling downside,
  • the Sudoku downside.

A few of these examples require some superior methods:

  • redundant constraints,
  • customized heuristics,
  • customized consistency algorithms

Most of those fashions are additionally accessible 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 are able to see that:

  • sure consistency was computed 262006 occasions,
  • 2268895 propagators have been utilized however with out impact 990435 occasions whereas inconsistencies have been detected 116806 occasions,
  • they have been 131000 selections and backtracks, with a most alternative depth of 10,
  • lastly, 14200 options have 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 - Downside has 82 propagators
2024-11-12 17:27:45,110 - INFO - nucs.solvers.solver - Downside 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 - Selection 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) resolution: 80
2024-11-12 17:27:45,677 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) resolution: 75
2024-11-12 17:27:45,677 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) resolution: 73
2024-11-12 17:27:45,678 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) resolution: 72
2024-11-12 17:27:45,679 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) resolution: 70
2024-11-12 17:27:45,682 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) resolution: 68
2024-11-12 17:27:45,687 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) resolution: 66
2024-11-12 17:27:45,693 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) resolution: 62
2024-11-12 17:27:45,717 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) resolution: 60
2024-11-12 17:27:45,977 - INFO - nucs.solvers.backtrack_solver - Discovered a (new) resolution: 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 have been virtually something might be custom-made:

  • propagators,
  • consistency algorithms,
  • heuristics,
  • solvers.

Within the following Golomb ruler instance, a customized consistency algorithm is registered earlier than getting used:

READ ALSO

7 NumPy Methods to Vectorize Your Code

Why Ought to We Hassle with Quantum Computing in ML?

    consistency_alg_golomb = register_consistency_algorithm(golomb_consistency_algorithm)
solver = BacktrackSolver(downside, consistency_alg_idx=consistency_alg_golomb)

In conclusion, NuCS is a constraint solver library with loads of options. Though it’s written totally in Python, it is rather quick and can be utilized for a variety of functions: analysis, educating and manufacturing.

Don’t hesitate to contact me on Github in the event you’d like to participate in NuCS improvement!

Some helpful hyperlinks to go additional:

If you happen to loved this text about NuCS, please clap 50 occasions !

Tags: ApplicationsConstraintGeorgetNovNuCSproductionResearchSolverTeachingYan

Related Posts

Mlm bala numpy vectorization tricks.png
Artificial Intelligence

7 NumPy Methods to Vectorize Your Code

October 23, 2025
9ca5bc8c a164 42c3 9dd0 e157dd5617a4 scaled 1.jpg
Artificial Intelligence

Why Ought to We Hassle with Quantum Computing in ML?

October 22, 2025
Mlm chugani machine learning practitioners guide agentic ai systems feature png 1024x683.png
Artificial Intelligence

The Machine Studying Practitioner’s Information to Agentic AI Programs

October 22, 2025
Chatgpt image oct 21 2025 05 49 10 am.jpg
Artificial Intelligence

Is RAG Useless? The Rise of Context Engineering and Semantic Layers for Agentic AI

October 22, 2025
Caleb jack juxmsnzzcj8 unsplash scaled.jpg
Artificial Intelligence

Constructing Transformer Fashions from Scratch with PyTorch (10-day Mini-Course)

October 21, 2025
Chatgpt image 14 oct. 2025 08 10 18.jpg
Artificial Intelligence

Implementing the Fourier Rework Numerically in Python: A Step-by-Step Information

October 21, 2025
Next Post
Solana Based Pump.fun Disables Livestream.webp.webp

Solana-based Pump.enjoyable Disables 'Livestreams' Amid Outrage

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

POPULAR NEWS

0 3.png

College endowments be a part of crypto rush, boosting meme cash like Meme Index

February 10, 2025
Blog.png

XMN is accessible for buying and selling!

October 10, 2025
Gemini 2.0 Fash Vs Gpt 4o.webp.webp

Gemini 2.0 Flash vs GPT 4o: Which is Higher?

January 19, 2025
1da3lz S3h Cujupuolbtvw.png

Scaling Statistics: Incremental Customary Deviation in SQL with dbt | by Yuval Gorchover | Jan, 2025

January 2, 2025
0khns0 Djocjfzxyr.jpeg

Constructing Data Graphs with LLM Graph Transformer | by Tomaz Bratanic | Nov, 2024

November 5, 2024

EDITOR'S PICK

Bitcoin Mining Difficulty.jpg

Bitcoin mining complexity surges as miners’ reserves slip 4.74% YoY

December 17, 2024
Ai Shutterstock 2285020313 Special.png

RAND AI Governance Sequence: How U.S. policymakers can study from the EU AI Act

August 25, 2024
Gpu.jpg

Harnessing idle GPU energy can drive a greener tech revolution

December 21, 2024
Writer Logo 2 1 0325.png

Author Survey: 42% of C-Suite Say Gen AI Is Tearing Their Corporations Aside

March 20, 2025

About Us

Welcome to News AI World, your go-to source for the latest in artificial intelligence news and developments. Our mission is to deliver comprehensive and insightful coverage of the rapidly evolving AI landscape, keeping you informed about breakthroughs, trends, and the transformative impact of AI technologies across industries.

Categories

  • Artificial Intelligence
  • ChatGPT
  • Crypto Coins
  • Data Science
  • Machine Learning

Recent Posts

  • 7 NumPy Methods to Vectorize Your Code
  • The Psychology of Dangerous Knowledge Storytelling: Why Individuals Misinterpret Your Knowledge
  • Construct an Inference Cache to Save Prices in Excessive-Site visitors LLM Apps
  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy

© 2024 Newsaiworld.com. All rights reserved.

No Result
View All Result
  • Home
  • Artificial Intelligence
  • ChatGPT
  • Data Science
  • Machine Learning
  • Crypto Coins
  • Contact Us

© 2024 Newsaiworld.com. All rights reserved.

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?