
MiniZinc: free and open-source constraint modeling language - bjourne
http://www.minizinc.org/
======
Macuyiko
Every time some OR or constraint programming language comes up, Håkan
Kjellerstrand is an obligatory mention. He has a whole page dedicated to
problems and examples for those, including MiniZinc:
[http://www.hakank.org/minizinc/index.html](http://www.hakank.org/minizinc/index.html)

~~~
placebo
Yes, an impressive set of problems solved with an impressive set of solvers.
Quite a bit of experience stored on that site.

One thing I'm interested to know (as someone with less experience in
constraint programming), is when does one employ constraint programming
engines like Choco, Gecode, MiniZinc etc, vs. when metaheuristics (such as
simulated annealing, genetic algorithms, etc.) are a more practical solution.
Is there a rule of thumb regarding the size search space, type of problem etc.
?

~~~
jahewson
Constraint satisfaction is NP-complete and for optimization it’s NP-hard. In
theory that means it’s going to be equally hard to determine how many steps
solving the problem takes as it is to simply solve the problem.

Local search is always going to outperform global search but there’s no
guarantee of a solution or an optimal one. So it really depends on your needs
and how long you’re prepared to wait. Modern CSP solvers can handle
impressively large problems with multiple thousands of clauses (1m+ for SAT),
but it really depends on what your constraints look like.

------
jsjolen
A constraint solver typically works by:

1\. Having a finite set of variables with a finite set of possible values

2\. Having a finite set of propagators which are monotonic on variables (that
is, it either reduces the amount of possible values for a variable or does not
(it never adds).

3\. A space describing a full set of variables and propagators

4\. A way of reducing the amount of values when all propagators have hit a
fixpoint for some space.

5\. A way of rewinding state when some variable runs out of possible values

And then it basically (BASICALLY) runs this loop:

while(!solved(space)) {

if(failed(space)) {

    
    
    		rewind(space); 
    
    	} 
    
    	while(!fixpoint(space)) { 
    
    		for(p in propagators(space)) 
    
    		{ p(space); 
    
    		} 
    
    
    	} 
    
    	make_choice(space);
    

}

It's mainly used for solving NP-hard problems by being smart when attacking
the full search tree.

~~~
sevensor
So what differentiates the solvers then? Is it in how they apply the
propagators? I'm interested because I naively wrote the exact loop you just
posted, trying to solve a very hard problem. I spent ages trying different
heuristics, but I knew I was floundering. Then I discovered Z3, which solves
the problem in a tiny fraction of the time. It seems like pure magic, and I'm
keen to understand how it works.

~~~
afpx
I haven’t used all the different solver engines, and I haven’t kept up with
the field, but many years ago I had a license to IBM ILOG CPLEX, and it was
interesting how much faster it was than some others. Gurobi is also very fast
(I think they were started by the people who sold ILOG CPLEX to IBM), but
haven’t benchmarked it vs CPLEX.

~~~
sevensor
So I haven't looked deeply into the capabilities of Gurobi and CPLEX, but I
was under the impression that they focus on integer linear programming. How do
they do with problems for which finding a feasible solution is very difficult?

------
dfan
There's a good MOOC on Coursera on applied discrete optimization that uses
MiniZinc: [https://www.coursera.org/learn/basic-
modeling](https://www.coursera.org/learn/basic-modeling)

------
Fiahil
Fun fact: We're using MiniZinc and Gecode in production code to solve
volume/product puzzles on oil tankers.

------
trex987
Tried to use MiniZinc to write a model checker [1] for c programs a while back
[2]. Essentially checking the equivalence of c functions that use only bit
operations. It basically translates the programs into minizinc, then attempts
to derive a case where the same input results in differing output. Was a fun
way to experiment using the constraint solver for a practical application,
though unfortunately didn't work as well on larger more complicated functions.

1)
[https://en.wikipedia.org/wiki/Model_checking](https://en.wikipedia.org/wiki/Model_checking)

2)
[https://github.com/dbunker/ArchStatMzn](https://github.com/dbunker/ArchStatMzn)

~~~
hairtuq
I'd expect a SMT (Satisfiability modulo theory) solver like z3 to work better
for this use case, might be worth giving it a try. E.g.

    
    
      x = z3.BitVec('x', 32)
      y = z3.BitVec('y', 32)
      i1 = x | y
      i2 = ~(~x | ~y)
      s = z3.Solver()
      s.add(i1 != i2)
      if s.check() == z3.sat:
          print "found counterexample"
          print s.model()
      else:
          print "always equal"

------
phkahler
People need to specify what type of constraint their solver handles. It's a
rather large space, and to not even mention what type of constraints on the
page is a major oversight.

~~~
Jeff_Brown
Hear, hear. Is this good for optimization problems over the reals, or only
finite-domain problems? If the former, can it solve nonlinear problems?

~~~
hakank
Some of the FlatZinc solvers can handle (nonlinear) finite-domain problems,
and some just linear MIP problems with floats.

Some solvers, e.g. Gecode and JaCoP, can handle nonlinear problems with floats
as well as finite-domain problems.

~~~
nurettin
_the_ hakank?

~~~
hakank
:-) Yes.

------
udkl
Whenever I come across a post about constraint solvers, I refer back to this
article for a great introduction to the domain - complete with examples and
different progressive approaches - [http://www.jasq.org/just-another-scala-
quant/new-agey-interv...](http://www.jasq.org/just-another-scala-quant/new-
agey-interviews-at-the-grocery-startup)

------
verifex
I've never heard of constraint programming languages before, and I kind of
wish I had, because I recently had a problem where I needed to sequence a
bunch of elements based on a lot of arbitrary rules. Sequencing based on one
rule was easy, but applying all simultaneously was a huge PITA, and resulted
in much headaches; this tool looks like it could have solved the problem
quickly. Definitely putting this in my bookmark tool-set for future use.

------
forkandwait
I would love to hear other applications of optimization techniques to HN
readers.

(I used constrained quadratic optimization to fit transition matrices to
population data for my dissertation, and for some other demographic estimation
projects.)

------
crimsonalucard
What is a constraint modeling language exactly? I briefly looked at the first
example in the documentation, and it looks like you specify a problem and some
type of solver solves the problem automatically? What algorithm is it running
exactly?

~~~
jahewson
“Constraint Programming” refers, for the most part, to the solving of finite-
domain problems via backtracking and constraint propagation and is typically
concerned with performing a complete, global search of the solution space.

[https://en.m.wikipedia.org/wiki/Constraint_satisfaction](https://en.m.wikipedia.org/wiki/Constraint_satisfaction)

~~~
crimsonalucard
I like this answer. However, by solution space do you mean domain space?

~~~
jahewson
Yes, sorry, the search space.

