Live data from Hacker News

Constraint Programming

en.wikipedia.org

31–40 of 63 posts

Re: Constraint Programming

#31
post #26

I feel like this is something I may understand the solution to but the problem it solves leaves me dumbfounded. Any recommendations on a "this but for dribbling primates" introduction?

Constraint Programming is "on the same complexity" as any of your NP-completeness set of problems. IE: Knapsack problem, Traveling Salesman, etc. etc. Today, it seems more popular to use "3-SAT Solvers". But constraint programming is "more logical" for some problems to get converted into rather than 3-SAT. Consider 3-SAT and Constraint Programming to be two different "assembly languages" that you can compile your pro…

SAT solvers are used a lot for NP-complete problems, but they don't use the 3-SAT representation of clauses. 3-SAT is only interesting from a theoretical POV. Nobody uses it in practice.

There are also intermediate representations. For instance, pseudo-boolean retains most of the power of SAT solvers, while at the same time making problems much easier to express in terms of number of constraints needed. For instance, your Alldifferent(X11, X12, X13, X14, X15, X16, X17, X18, X19) constraint would be written as "x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 = 1". Same expressive power, on this problem.

Re: Constraint Programming

#32
post #31

Earlier quoted context omitted.

Constraint Programming is "on the same complexity" as any of your NP-completeness set of problems. IE: Knapsack problem, Traveling Salesman, etc. etc. Today, it seems more popular to use "3-SAT Solvers". But constraint programming is "more logical" for some problems to get converted into rather than 3-SAT. Consider 3-SAT and Constraint Programming to be two different "assembly languages" that you can compile your pro…

SAT solvers are used a lot for NP-complete problems, but they don't use the 3-SAT representation of clauses. 3-SAT is only interesting from a theoretical POV. Nobody uses it in practice. There are also intermediate representations. For instance, pseudo-boolean retains most of the power of SAT solvers, while at the same time making problems much easier to express in terms of number of constraints needed. For instance,…

> For instance, your Alldifferent(X11, X12, X13, X14, X15, X16, X17, X18, X19) constraint would be written as "x11 + x12 + x13 + x14 + x15 + x16 + x17 + x18 + x19 = 1". Same expressive power, on this problem.

I'm more comfortable with constraint-programming than 3SAT / SMT / etc. etc. So I'm probably getting something wrong here.

I don't think you're correct on this issue? "x11 + x12 = 1" means that "x11 or x12 are 1, and the other is zero".

EDIT: That is, the 9-set of solutions you described is:

    {1, 0, 0, 0, 0, 0, 0, 0, 0}
    {0, 1, 0, 0, 0, 0, 0, 0, 0}
    {0, 0, 1, 0, 0, 0, 0, 0, 0}
    {0, 0, 0, 1, 0, 0, 0, 0, 0}
    {0, 0, 0, 0, 1, 0, 0, 0, 0}
    {0, 0, 0, 0, 0, 1, 0, 0, 0}
    {0, 0, 0, 0, 0, 0, 1, 0, 0}
    {0, 0, 0, 0, 0, 0, 0, 1, 0}
    {0, 0, 0, 0, 0, 0, 0, 0, 1}
Meanwhile, Alldifferent(X11, X12, X13, X14, X15, X16, X17, X18, X19) represents the following 9-factorial set of solutions:

    {1, 2, 3, 4, 5, 6, 7, 8, 9}
    {1, 2, 3, 4, 5, 6, 7, 9, 8}
    {1, 2, 3, 4, 5, 6, 9, 8, 7}
    {1, 2, 3, 4, 5, 6, 9, 7, 8}
    {1, 2, 3, 4, 5, 9, 6, 7, 8}
    {1, 2, 3, 4, 5, 9, 6, 8, 7}
    {1, 2, 3, 4, 5, 9, 7, 6, 8}
    {1, 2, 3, 4, 5, 9, 7, 8, 6}
    {1, 2, 3, 4, 5, 9, 8, 6, 7}
    {1, 2, 3, 4, 5, 9, 8, 7, 6}
    ....
    {9, 8, 7, 6, 5, 4, 3, 2, 1}
That is, the entire set of permutations of the domain of all 9 variables participating in the constraint. What I've listed above is a *complete* Sudoku solver within the constraint-programming mindset.

--------

In most of the 3SAT / SMT solvers for Sudoku I've seen, you set up the variables as: "X111, X112, X113, X114...", which means "X11 == 1", and "X11 == 2", etc. etc. So you then do "X111 + X112 + X113... X119 == 1", meaning "X11 is either 1, 2, 3, 4, 5... 9".

Meanwhile, this entire process is simply "Domain of X11 = {1, 2, 3, 4, 5, 6, 7, 8, 9}" in the constraint-processing world, because variables can have arbitrarily sized (though finite) domains in constraint programming.

That is to say, I'm pretty sure you need 999 == 729 variables to brute-force represent the problem in 3SAT / SMT? Meanwhile, Constraint Solver trivially represents it with 81-variables (but each of these 81 variables, X11 through X99, have a domain of size 9: {1, 2, 3, 4, 5, 6, 7, 8, 9}).

There might be an easier way to represent Sudoku in 3SAT/SMT world, but again, I'm not very good with that world. I "just" know that 3SAT / SMT is a "similar but different" kind of solver and barely have used them.

--------

One can argue that "constraint programming" is simply 3SAT / SMT-solving except with arbitrarily sized domains. Rather than solely using 0 and 1 or boolean functions.

I've been told that "because everything is {0, 1}, more optimizations / inferences can be discovered" in 3SAT/SMT solvers compared to constraint programmers. Meanwhile, constraint programmers benefit from generic constraints that have a polynomial class subsolution (ie: using maximum flow, or other simpler algorithms to solve the subproblem).

It'd take a rather well-studied expert to know whether 3SAT/SMT is better for a particular problem vs constraint-programming.

Re: Constraint Programming

#33
post #27

Earlier quoted context omitted.

I think this can be applied to much the same domains as simplex or similar algorithms. So optimization problems/OR. See https://en.wikipedia.org/wiki/Operations_research

Constraint Programming is NP-complete (non-polynomial bounds, likely exponential complexity). Simplex is provably within the polynomial bounds of complexity, ie: simplex is a so called "easy" problem in complexity theory. -------- You're right that simplex is applied to optimization problems. Constraint programming could be seen as a "more generic" optimizer, in that it can handle integer-optimization (simplex cannot…

Not quite. Simplex is an algorithm to solve linear programming problems. Linear programming is (weakly) polynomial. The simplex algorithm itself has exponential worst case [0]. Whether there exist a polynomial-time variant of the simplex algorithm is an open problem.

Constraint programming is very broad — it encompasses many types of problems. Some of them are in P, some (most?) are in NP. In principle you can use the simplex algorithm for solving certain constraint programming problems, but mostly when people talk about constraint programming they refer to different techniques.

[0] https://en.wikipedia.org/wiki/Klee%E2%80%93Minty_cube

Re: Constraint Programming

#35
A very interesting application of constraint programming is the Unison compiler https://unison-code.github.io/, which uses constraint models to solve compiler backend problems for llvm. As a simple example, register allocation can be modeled as a graph coloring problem for which there is an edge between every variable which must be live at the same time and colors represent registers, but he unison model is sophisticated beyond this. We use a simpler related model in our project VIBES https://github.com/draperlaboratory/VIBES which is a micropatching compiler (it uses the constraints to compile code in such a way it can fit in place, has the right stuff in the right registers, etc.)

Re: Constraint Programming

#36
I used this to solve some complicated calendar scheduling routines for an app I wrote in Kotlin. Originally, I used some brute force/search methods to handle this, but the approach outlined here provided far better results, and it took less time too.

Re: Constraint Programming

#37
post #6
post #2

These techniques are usable in every language, and not just in special ones like Prolog. For example https://norvig.com/sudoku.html uses constraint propagation to solve sudoku puzzles in Python.

In the sense that Turing-complete languages are all equivalent, yes. But in most imperative and functional languages, you'll end up implementing your own solver (as Norvig did), and a DSL to model your problem. Why reinvent the wheel?

Why reinvent the wheel?

If you give me a choice between implementing a solver in an imperative language or a website in Prolog, I'll choose writing the solver. Every time.

Problems in the context of a programming environment where you're already using other languages. Introducing a specialized one does not necessarily make much sense.

Re: Constraint Programming

#38
post #11

Earlier quoted context omitted.

isn't or-tools just a wrapper library for a bunch of solvers? I must be missing something.

Yes, OR-tools has a lot of solvers. The solver I talked about is the CP-SAT solver. It's described ( https://www.minizinc.org/challenge2022/description_or-tools_... ) as: """ CP-SAT is a discrete optimization solver built on top of a SAT engine. It is available within the OR-Tools open-source repository (website: https://developers.google.com/optimization , github repository: https://github.com/google/or-tools ). It…

wow. that is impressive.. but surely this CP-SAT solver can't compete with the commercial offerings like Gurobi and CPLEX right? it'd be huge news if so... but I haven't seen it rank at all on the LP-focused benchmarks I've been looking at.

Re: Constraint Programming

#39
post #7

Earlier quoted context omitted.

as a note, I've tried MiniZinc for modeling a problem in the past, and the language/ecosystem simply wasn't up to my task. iirc, function calls in MiniZinc are expanded/unrolled when the model is generated, rather than the function definition and its callers being symbolically translated into the model. this is no fault of MiniZinc, I simply thought that an SMT solver would be sufficient and it wasn't. I need a mixed…

What are you using to build your GUROBI models? I think your only options are to feed it a .MPS or .LP file if you don't use the APIs for C#, C, C++, Python, Matlab, R...etc. Or did you use something like Mathematica to go from CAS to .MPS?

Mathematica, correct. But fun fact: Mathematica has no MPS exporter. So I had to write my own, which is somehow the perf bottleneck in my whole setup now. Even after wrestling with it for several days. Apparently string operations are not its forté.

Re: Constraint Programming

#40
post #37
post #6

Earlier quoted context omitted.

In the sense that Turing-complete languages are all equivalent, yes. But in most imperative and functional languages, you'll end up implementing your own solver (as Norvig did), and a DSL to model your problem. Why reinvent the wheel?

Why reinvent the wheel? If you give me a choice between implementing a solver in an imperative language or a website in Prolog, I'll choose writing the solver. Every time. Problems in the context of a programming environment where you're already using other languages. Introducing a specialized one does not necessarily make much sense.

well of course, but why not write the solver in Prolog and the website in Python? why are people so allergic to mixing languages?

if the problem is simple to describe, then sure, write a solver or use a binding. I'm just used to the problems being pretty intense and sophisticated.

I've found having a REPL in a symbolic (term-rewriting) language to be invaluable, and can't imagine going back to for-loops and endless maps of symbol tables chugging out text files for debugging.

Post reply on HN