July 3rd, 2024

A Practical Introduction to Constraint Programming Using CP-SAT and Python

Constraint programming (CP) is a declarative paradigm for solving discrete optimization problems using variables and constraints. CP-SAT, an open-source solver by Google, can handle complex problems efficiently, like fair contribution distribution and employee scheduling.

Read original articleLink Icon
A Practical Introduction to Constraint Programming Using CP-SAT and Python

Constraint programming (CP) is a declarative paradigm used to solve discrete optimization problems by describing the desired result through variables and constraints. Variables represent what is sought, with associated domains, while constraints define relationships between variables. Solutions are assignments of values to variables satisfying constraints. CP can handle complex concepts with global constraints, like ensuring variables have different values. A solver interprets the model and returns a valid solution. Adding objectives allows maximizing or minimizing expressions without compromising constraints. An example illustrates distributing contributions fairly among individuals. CP-SAT, an open-source solver by Google, can be used with Python to solve real-world problems. A practical example involves scheduling employees for shifts in a store, ensuring coverage, role assignments, and shift constraints. By defining variables, constraints, and objectives, CP can efficiently solve intricate optimization challenges.

Related

Solving puzzles faster than humanly possible

Solving puzzles faster than humanly possible

The Opus Magnum challenge tasks players with automating puzzle-solving to optimize Cost, Cycles, and Area metrics. Participants submit solutions for evaluation, exploring automated vs. human strategies, hybrid approaches, scoring systems, mods, and bots.

ICFP Contest 2024

ICFP Contest 2024

The ICFP Programming Contest 2024 is an open online competition sponsored by ACM SIGPLAN. It allows participants to form teams of any size, using any programming language or platform. The event runs from June 28 to July 1, 2024.

PARI/GP computer algebra system

PARI/GP computer algebra system

PARI/GP is a versatile open-source computer algebra system for number theory and mathematical computations. It offers a wide range of functions, a C library for speed, an interactive shell, and a GP scripting language. Recent updates include versions 2.15.5 and 2.16.1 (alpha). The system received the 2021 Richard D. Jenks Memorial Prize and a new book on numerical algorithms was released, showcasing its ongoing development.

A Survey of General-Purpose Polyhedral Compilers

A Survey of General-Purpose Polyhedral Compilers

A survey of polyhedral compilers available in 2024 compares schedulers and implementations based on robustness and performance using PolyBench/C benchmarks. Key tools like Candl, Tiramisu, Polly, and PolyMage are referenced.

Solving a math problem with planner programming

Solving a math problem with planner programming

The article explores solving a math problem by optimizing select, copy, and paste functions using C++ and Picat. It demonstrates reducing steps to reach 100,000 characters and enhancing efficiency through planning.

Link Icon 8 comments
By @0cf8612b2e1e - 7 months
I have used constraint solvers in the past, and they are truly magical in what they can do. The problem is that there are not many available resources for the novice. Most of the material you can find is how to solve sudoku (the hello world of the space) or highly technical primary research literate meant exclusively for domain experts. Which is a shame, because I think huge swaths of problems could be solved by these tools if they were more accessible. “Accessible” still meaning it requires a programmer, because shaping a problem into the constraints DSL is not going to be in the wheelhouse of most.
By @taeric - 7 months
Core to a lot of this, is learning how to model things in such a way that you can send them to a solver. After that, how to take a solution and present it in a way that can be understood.

It is a shame, as most programs work against the ideas here by trying to have a singular representation of their data. This is just not reasonable for most things and leads to a lot of contortions to get the algorithms to work on a new representation.

This article touches on it with the brief touch of declarative at the top. I always regret that more of my code is not translating between representations more often. You can wind up with very concise representations when you do this, and then you can get a double bonus by having things run faster by virtue of being concise.

(And, yes, I realize I'm basically describing many data pipelines. Where you spend most of your time translating and fanning out data to places for more compute to be done on it.)

By @mark_l_watson - 7 months
I have a short chapter on using MiniZinc with Python in one of my old books that I am currently rewriting https://leanpub.com/pythonai/read#constraint-programming-wit... (link directly to this chapter online)

MiniZinc is a constraint programming system. There is a good Coursera class using MiniZinc.

By @bartkappenburg - 7 months
I used a lot of solvers in the early 2000s in my Operations Research master after my econometrics study. While now working on software (web) that uses python I’m thrilled to see these deep dives on this subject!

I love the subject and reading this brought back a lot of memories. Also the realization that translating constraints to a model (variables, structure etc) is 90% of the work and the most difficult part.

By @d_burfoot - 7 months
I have a client that runs a sports camp for kids. The kids get to request what sports they want to play, and what friends they want to be in class with. This creates a scheduling problem that's hard for a human, and previously they spent several man-weeks per year dealing with it. I built them a simple system that connects their data to an optimizer based on OR-Tools, now their scheduling is done with a few clicks.
By @Elucalidavah - 7 months
Is there a parametric CAD that works primarily as a constraint solver?

It so often bothers me that I have to guesstimate some values for parameters I don't initially care about, instead of constraining the parameters I care about and then optimizing the rest.

By @richard___ - 7 months
How does this compare with mixed integer programming? For problems in physics