" /> My Constraint Programming Blog: December 2010 Archives

## December 31, 2010

### A first look at Answer Set Programming

Almost exactly 2 years ago I started this blog with the following tag line: This is my blog about constraint programming and related paradigms. One of the "related paradigms" I then thought of was Answer Set Programming (ASP). From the Wikipedia article Answer Set Programming:
Answer set programming (ASP) is a form of declarative programming oriented towards difficult (primarily NP-hard) search problems. It is based on the stable model (answer set) semantics of logic programming. In ASP, search problems are reduced to computing stable models, and answer set solvers -- programs for generating stable models are used to perform search. The computational process employed in the design of many answer set solvers is an enhancement of the DPLL algorithm and, in principle, it always terminates (unlike Prolog query evaluation, which may lead to an infinite loop).
In 2001 I first read about Answer Set Programming as a byproduct from an interest in planning and automated theorem proving. I then implemented the ASP program who_killed_agatha.lp, but didn't pursue this interest much more. After that I have - in different times - been very fascinated by this paradigm, especially how succint and clear many of these programs are.

The last weeks I did a more systematic take on ASP and have implemented about 50 different ASP programs (see my Answer Set Programming Page), among them many of the learning problems I use as a stepping stone when learning a new CP system. A full list of the implemented programs and problem instances is shown below.

Answer Set Programming is a huge subject and I will barely scratch the surface here. The object of this blog post is to show how some standard CP problems (and not so standard problems) can be implemented in ASP. I realize that having this approach don't show off some very nice features/applications of ASP, such as the connection to planning, database query, etc. See this as ground for future projects. Also, I have been more concerned about understand how to program in ASP than finding the best heuristics (solver parameters) for each problem. That said, there are some benchmark comparisons below which may not be representative for what ASP systems can do.

## My system

All programs was run on this computer: Linux Ubuntu 10.4, 64-bit with 8 cores (Intel i7 930, 2.80GHz) and 12 Gb RAM.

## Grounders and solvers

The ASP systems I have worked with solves a problem in two steps. Given a file in apropriate format:
• first a grounder "grounds" the problem file(s) to an intermittent format. In principle this means that all possible values of the variables are generated including all the combinations that are forbidden by integrity constraints. Note: This can be very large files.
• then a solver reads this grounded file and - hopefully - solves the problem by generating answers sets (solutions).
After some considerations I decided to go with Potassco (the Potsdam Answer Set Solving Collection ) tools which includes the grounder gringo, the solver clasp and a program that combines these two: clingo, and it is Clingo that I tend to use. The versions I have been using are clasp version 1.3.6, gringo version 3.0.3, clingo version 3.0.3. For some tests with lparse/smodels: lparse version 1.1.2 and smodels 2.34.

Another option was to use the grounder/solver lparse/smodels but I decided to use Potassco since it seems to be much more active and it include a lot of other tools, for example clingcon (using constraint programming domains etc), which I will study more later.

Another reason to use the Potassco tools was the impressive results in The Second Answer Set Programming Competition (for year 2010). For more about this, see the report The Second Answer Set Programming Competition (PDF).

However, with some few problem I also compared with with lparse/smodels, e.g. send_more_money.lp.

Note that I have not tweaked very much with the large amount of flags for the clasp solver. In some problems (e.g. the alphanumeric) I tested some of them but didn't find any that speeded it up considerably.

### ASP Systems

There are many ASP systems. Here are those I have tested most.

## Some programs

Here is some examples how one can encode some problems with ASP (or rather in lparse/gringo format). Let's start with Sudoku.

#### Sudoku

Program: sudoku.lp

Here is one encoding of the problem (inspired by this). The hints part is described below.
% domains
val(1..9).
border(1;4;7).
% alldifferent rows, columns, values
1 { x(X,Y,N) : val(N) } 1 :- val(X;Y).
1 { x(X,Y,N) : val(X) } 1 :- val(N;Y).
1 { x(X,Y,N) : val(Y) } 1 :- val(N;X).
% alldifferent: boxes
1 { x(X,Y,N) : val(X;Y):
X1<=X:X<=X1+2:Y1<=Y:Y<=Y1+2 } 1 :- val(N), border(X1;Y1).

One really big difference with coding ASP and CP is that in CP the data is often in matrix/array form (except maybe for the Prolog based CP systems). In ASP there are no arrays or matrices, so instead one have to state the order and "coordinates" by enumerating the combinations. E.g. the following problem instance is normally coded in CP as something like (where "_" must replaced with "0" in some systems).
 _,_,6, _,_,_, _,9,_,
_,_,_, 5,_,1, 7,_,_,
2,_,_, 9,_,_, 3,_,_,

_,7,_, _,3,_, _,5,_,
_,2,_, _,9,_, _,6,_,
_,4,_, _,8,_, _,2,_,

_,_,1, _,_,3, _,_,4,
_,_,5, 2,_,7, _,_,_,
_,3,_, _,_,_, 8,_,_

In ASP encodings this is stated as predicates x(Row, Column, Value), e.g.:
x(1, 3, 6).
x(1, 8, 9).
x(2, 4, 5).
x(2, 6, 1).
...
x(8, 6, 7).
x(9, 2, 3).
x(9, 7, 8).

This lack of direct matrix representation (and no direct representation of arrays) has been one of the biggest hurdle when translating the MiniZinc models to ASP. (It takes a lot of time to manually convert the matrix representation to this predicate variant, so I've created a simple Perl program for this: matrix2predicate.pl)

OK, now some explanations of the ASP encoding above.
val and border is "domain predicates" which contains the different values (1..9, and 1,4,7 respectively) to use in the program. They are used to generate different values later on.

Next we have the constraints that all rows, columns, and boxes should contain different values. In Constraint Programming this is (normally) done with a bunch of alldifferent constraints, but in ASP we (normally) have to use another approach. (however there are some systems, e.g. clingcon mentioned above, that is a hybrid of ASP and CP which includes some global constraints.)

Instead one have to use some other features which can be described as.
• "generators"
• cardinality constraint
• integrity constraint
The first line: 1 { x(X,Y,N) : val(N) } 1 :- val(X;Y). is to ensure that the indices in the "matrix" (X and Y) are unique, i.e. that the we have exactly 1 occurrence of x(1,1,Val) (for some value Val), 1 occurence of x(1,2, Val) etc.

The left "1" around the curly braces is the lower bound, and the right "1" is the upper bound of the expression. This is the cardinality constraints (think atleast and atmost in CP). The part after the colon (:) - : val(N) - is a condition filter which here means that N must take the values of the domain val/1 (i.e. the values 1..9).

The right part (the body) val(X;Y) is used as a "generator" of the expression; I tend to think of this as a "for loop" over X and Y that "drives" the left part.

Continuing with the next two lines:  1 { x(X,Y,N) : val(X) } 1 :- val(N;Y). 1 { x(X,Y,N) : val(Y) } 1 :- val(N;X). 

These act as alldifferent constraints: for each combination of val(N) and val(Y) (val(X;Y) is a shorthand for val(X) and val(Y)) there must be exactly one occurrence of X (the rows). And the next line is for Y.

The last constraint
1 { x(X,Y,N) : val(X;Y):
X1<=X:X<=X1+2:Y1<=Y:Y<=Y1+2 } 1 :- val(N), border(X1;Y1).

is for the boxes, and we can here see how the filter conditions (the ":" parts) can be combined. Note that the "loop" is over X1 and Y1.

Note: There is an alternative version of the constraints for the rows, columns, values which seems to be slighly faster:
% alternative:
:- 2 { x(X,Y,N) : val(N) }, val(X;Y).
:- 2 { x(X,Y,N) : val(X) }, val(N;Y).
:- 2 { x(X,Y,N) : val(Y) }, val(N;X).

These "headless" expressions (starts with :-) are integrity constraints which are used to remove certain results: if the expression is true then it don't belong to a solution. They all states that the occurrences of each value (X;Y) cannot be 2 or larger.

### nqueens

Program: nqueens.lp

Let's continue with another standard CP problems and compare the performances between encoding in ASP and some CP systems. Here is a complete encoding for n-queens problem in ASP: nqueens.lp.
#const n = 8.
number(1..n).
% alldifferent
1 { q(X,Y) : number(Y) } 1 :- number(X).
1 { q(X,Y) : number(X) } 1 :- number(Y).
:- number(X1;X2;Y1;Y2), q(X1,Y1), q(X2,Y2), X1 < X2, Y1 == Y2.
:- number(X1;X2;Y1;Y2), q(X1,Y1), q(X2,Y2), X1 < X2, Y1 + X1 == Y2 + X2.
:- number(X1;X2;Y1;Y2), q(X1,Y1), q(X2,Y2), X1 < X2, Y1 - X1 == Y2 - X2.

The first line (#const n = 8.) is a constant, and can be redefined from the command line with different grounders/solvers:
$clingo -c n=12 nqueens.lp$ gringo -c n=12 nqueens.lp | clasp
$lparse -c n=12 nqueens.lp | smodels$ lparse -c n=12 nqueens.lp | clasp

The program use the same principles as for the Sudoku encoding. We represent the queens as a predicate q(X, Y) where X is the position in the "array" q, and Y is the value. (Or maybe as a 2-dimensional matrix of row X and column Y, but I tend to think about arrays with a unique index.)
% alldifferent
1 { q(X,Y) : number(Y) } 1 :- number(X).
1 { q(X,Y) : number(X) } 1 :- number(Y).

The first line ensures unicity of the X:s, and the second line ensure the unicity of the Y:s.

After that is the constraints (and integrity constraints) stating that the queens can not be placed on the same line, same row or column.

Since it is a standard benchmark problem in CP, I thought it might be interesting to see how well this ASP program do.

Below are some statistics for generating the first solution for different values of n, with two different grounders (gringo and lparse) and two solvers (clasp and smodels). I had a tiny time limit for these problems, 2 minutes (except for N=200). Here smodels didn't do very well so I skipped it for N larger than 50.

In this table the times for the grounders and the solvers are separared. As we can see there is quite a difference of the times for the two grounders (gringo, lparse) and the solvers (clasp, smodels).
 N   Grounder Time    Solver  Time   Choices Conflicts Restarts
----------------------------------------------------------------
8  gringo   0.006s  clasp    0.004s     5      2      0
8  gringo   0.006s  smodels  0.008s     5      -      -
8  lparse   0.023s  clasp    0.02s      4      1      0
8  lparse   0.023s  smodels  0.02s      3      -      -

50  gringo   1.14s   clasp    0.424s    366    252     2
50  gringo   1.14s   smodels  > 2min      -      -     -
50  lparse   3.34s   clasp    0.37s     211    137     1
50  lparse   3.34s   smodels  > 2min      -      -     -

100  gringo  15.5s    clasp    3.81s     839    505     3
100  lparse  33.1s    clasp    3.4s      599    332     2

200! gringo 3:38min   clasp    45.3s    9782   7747     9
200  lparse 6:30min   clasp    38.5s    3669   2347     6

Note: The results above was obtained by the following:
 $time gringo -c n=200 nqueens.lp > xxx$ time clasp xxx

However, it seems that piping the results directly to the solver is faster. The following $time gringo -c n=200 nqueens.lp | clasp takes less time than running the programs separately: 4:01 minutes (compare with 3:38min + 45s = 4.23min). Using lparse, $ time lparse -c n=200 nqueens.lp | clasp: 6:28min (compare with 6:30min + 38.5s = 7:08min).

Compare these numbers with the MiniZinc model using the same modelling approach: queens.mzn with Gecode/fz as solver. These times are much better except maybe for small N. Or rather, the ASP solvers seems to be quite competitive, it is the ASP grounders that take long time. MiniZinc uses the same principle: first the MiniZinc model is converted to a FlatZinc file, and then the CP solvers works on the FlatZinc file. The times below includes both these steps.
  N    Time Failures
------------------
8   0.08s     3
50   0.2s      2
100   0.5s     60
200   2.9s      8
500  21.0s     25
1000 1:44min     0

With the Google CP Solver program nqueens2.py (Python interface) we have even better results (for larger N times also without output):
  N    Time Failures Time without output
-----------------------------------
8    0.04s     3
50   0.08s    16
100   0.2s     12   0.1s
200   0.6s      1   0.5s
500   4.0s     25   2.9s
1000  16.1s     0   12.1s

Clingcon
As mentioned above, there is a hybrid of ASP and CP (clingcon which seems to be much faster. However, the example of n-queens in the distribution (queens.lp) don't give correct result. For n=8 it should be 92 solutions, but it generates 1066 solutions. There is an alldifferent predicate in the program but it is commented which probably is the cause of this; removing the comment generates a segmentation fault.

### magic square

Program: magic_square.lp

Magic squares is another standard CP problem.
#const n = 3.               % the size
#const s = n*(n*n + 1) / 2. % the num
% domains
size(1..n).
val(1..n*n).
% unique index of x
1 { x(Row, Col, N) : val(N) } 1 :- size(Row;Col).
% alldifferent values of x
1 { x(Row, Col, N) : size(Row;Col) } 1 :- val(N).
% sum rows
:- not s #sum[ x(Row, Col, Val) : size(Col) : val(Val) = Val ] s, size(Row).
% sum columns
:- not s #sum[ x(Row, Col, Val) : size(Row) : val(Val) = Val ] s, size(Col).
% Sum diagonals 1 and 2
:- not s #sum[ x(I, I, Val) : size(I) : val(Val) = Val ] s.
:- not s #sum[ x(I, n-I+1, Val) : size(I) : val(Val) = Val ] s.
#hide.
#show x(Row, Col, N).

Note here how the constraints of sums are encoded. We use an integrity constraint  :- not s #sum[ x(Row, Col, Val) : size(Col) : val(Val) = Val ] s, size(Row).  stating that if the sum is not s, then it should be removed. This is kind of awkward sometimes but it is often required.

Here are some statistics for 1 solution of different n:s. Running with > \$ time gringo -c n=N magic_square.lp | clasp --stat 
 N  gringo     clasp   Choices   Conflicts Restarts
--------------------------------------------------
3   0.004s    0.009s     244       230       1
4   0.007s    0.041s    1764      1545       5
5   0.011s    0.061s    1946      1618       5
6   0.014s    8.509s  184347    172478      16
7   0.035s    6.007s   78131     72744      14
8   0.042s   1:56min  925330    858613      20
9   0.046s  2:32:37h  36674602 32898767     29

In contrast to the n-queens problem the grounding step is quite fast, it is the solver step that takes time. This also means that it may be room for a lot of improvements via different parameter settings of clasp.

Compare this with MiniZinc model (magic_square.mzn) that use about the same approach using Gecode/fz as the solver:
 N  time   failures
------------------
3  0.077s         3
4  0.076s         7
5  0.077s       678
6  0.083s       810
7  14:52m  99401166

Here the gringo/clasp implementation manage to handle larger N than the MiniZinc+Gecode/fz approach. However the plain Gecode version (magic-square) using standard settings is faster on some of the problem instances. It's quite interesting that the ASP program is faster than Gecode for N=8 (and assuminly also for N=9).
 N  time   failures
------------------
3  0.008s        6
4  0.013s       892
5  0.427s     72227
6  0.009s        27
7  2.628s     481301
8  > 1hour

Note: the Gecode mode has some symmetry breaking: x(1,1) > x(1,n) and x(1,1) > x(n,1). When I tried these in the ASP program it seems to be slower, not faster.

At last the mandatory declaimer: It's perhaps misleading to compare times on different systems/approaches like this. But it might give a good feeling of these systems/models.

### Papers on Comparisons ASP vs CP

There have been some more systematic comparisons of ASP and CP, which - as I understand - is written by ASP researchers:
These two papers are interesting since they show ASP in many areas where it do quite well: graph related problems. Many of the grid puzzles in the second paper use ASP's ability to encode transitive closure (see below) rather easy. This is not so easy (or "natural") to code in most CP systems.

### Other programs

Here is another samples of ASP programs together with some code and comments.

#### Who killed Agatha

Program: who_killed_agatha.lp As mentioned above, this was actually my first program in ASP, written when I first read about ASP, about 2002. (And that's why this problem has been a standard problem since.)

Here we see more of the declarative aspect of ASP and its use of defined predicates (hates/2, richer/2) which are then used in integrity constraints.
% "Someone who lives in Dreadsbury Mansion, killed aunt Agatha. Agatha,
% the butler, and Charles live in Dreadsbury Mansion, and are the only
% people who live therein."

% The people in this drama
person(agatha;butler;charles).

% Exactly one person killed agatha
1 {killed(agatha,agatha), killed(butler,agatha), killed(charles,agatha)} 1.

% A killer always hates her victim, and is never richer than her victim
hates(Killer,Victim) :- person(Killer), person(Victim), killed(Killer,Victim).
:- richer(Killer,Victim), person(Killer), person(Victim), killed(Killer,Victim).

% a person cannot be richer than him/herself
richer(X,Y) :- person(X), person(Y), X != Y.

% Charles hates no one that aunt Agatha hates
0 { hates(charles,X) } 0 :- person(X), hates(agatha,X).

% Agatha hates everyone except the butler, ...
hates(agatha,X) :- person(X), X != butler.
% ... and no one the butler does not hate.
:- hates(agatha,X), person(X), hates(butler,X).

% the butler hates everyone not richer than aunt Agatha
:- hates(butler, X),  person(X), richer(X,agatha).

% No one [in Dreadsbury Mansion] hates everyone
{hates(X,Y):person(Y)} 2 :- person(X).

% Who killed aunt Agatha?
{killed(X,agatha):person(X)}.

#hide.
#show killed(X,Y).


#### Diet

Program: diet1.lp

This is a simple optmization problem where the object is to minimize the cost of the products given that we must exceed some limits of the ingredients
#const n = 10.
amount(0..n). % max amount of each product

%    food                  calories  chocolate   sugar     fat    price
%                          (ounces)    (ounces) (ounces) (ounces)
food_a(chocolate_cake,       400,      3,          2,        2,   50).
food_a(chocolate_ice_cream,  200,      2,          2,        4,   20).
food_a(cola,                 150,      0,          4,        1,   30).
food_a(pineapple_cheesecake, 500,      0,          4,        5,   80).

% Minimum limits
limits(calories, 500).
limits(chocolate, 6).
limits(sugar, 10).
limits(fat, 8).

% extract the different nutrition types
calories(Food, Amount)  :- food_a(Food, Amount, _, _, _, _).
chocolate(Food, Amount) :- food_a(Food, _, Amount, _, _, _).
sugar(Food, Amount)     :- food_a(Food, _, _, Amount, _, _).
fat(Food, Amount)       :- food_a(Food, _, _, _, Amount, _).

% extrace the price
price(Food, Price) :- food_a(Food, _, _, _, _, Price).
prices(Price) :- price(Food, Price).

% food(chocolate_cake;chocolate_ice_cream;cola;pineapple_cheesecake).
food(F) :- food_a(F, _,_, _, _, _).

% each food has exactly one Price and one Amount
1 { food_price_amount(Food,Price,Amount) : amount(Amount) } 1 :-
food(Food),
price(Food, Price).

:- #sum[food_price_amount(F, _, A)  : calories(F, C)  = C*A] L-1, limits(calories, L).
:- #sum[food_price_amount(F, _, A)  : chocolate(F, C) = C*A] L-1, limits(chocolate, L).
:- #sum[food_price_amount(F, _, A)  : sugar(F, C)     = C*A] L-1, limits(sugar, L).
:- #sum[food_price_amount(F, _, A)  : fat(F, C)       = C*A] L-1, limits(fat, L).

#minimize [food_price_amount(F,Price,Amount) = Price*Amount ].

The way I have encoded it is rather complex but most of it is to extract different values from the predicate food_a. I have a feeling that there is a much better way...

A comment on the #sum construct. This code
:- #sum[food_price_amount(F, _, A)  : calories(F, C)  = C*A] L-1, limits(calories, L).

means that the sum of the products of the amount of food (A) and the calories in the food (C) should be larger than the limit (L) of this food. Since it's a integrity constrain we must negate the expression.

The objective is to minimize (#minimize) the cost of all selected products.

For me this problem is easier to encode with a traditional "array/matrix" approach in CP, and it is also easier to generalize (by just adding a dimension or two to the arrays). Compare for example with the MiniZinc model diet1.mzn

However, I like that it is possible to use symbolic domains directly instead of using integers (and enums).

### SEND+MORE=MONEY and other alphametic problems

Program: send_more_money.lp
Program: send_more_money_any_base.lp
Program: send_most_money.lp
Program: least_diff.lp

Here is my encoding of SEND+MORE=MONEY (send_more_money.lp). Note the use of the domain predicate letter) to define the "slots" in the "array" x(Letter, Value which are then extracted in the main predicate smm. Also note that we have to use :- not smm in order to remove the combinations we don't want (i.e. remove everything that is not satified by the rules in smm).
letter(s;e;n;d;m;o;r;y).
values(0..9).
% exact 1 occurrence of each letter
1 { x(L,Val) : values(Val) } 1 :- letter(L).
% 0..1 occurrences of each value
{ x(L,Val) : letter(L) } 1 :- values(Val).
% no digit can be given to two different symbols
% :- letter(L), letter(L1), x(L,V1), x(L1,V1), L != L1.
smm :-
values(S;E;N;D;M;O;R;Y),
x(s,S), x(e,E), x(n,N), x(d,D),
x(m,M), x(o,O), x(r,R), x(y,Y),
M > 0,  S > 0,
S*1000+E*100+N*10+D + M*1000+O*100+R*10+E ==
M*10000+O*1000+N*100+E*10+Y.
:- not smm.

I was surprised - and somewhat disappointed - that it took so long to solve these kind of alphametic problems with standard ASP tools. It took about 45 seconds to solve this with clingo (gringo + clasp). lparse accept this encoding with just some warnings and it takes 25s.

After a while I realized that it is the grounding (gringo) that takes so long, almost all of the 45 seconds. Given this grounding as a file, then the solvers - clasp and smodel - solves this in no time, about 0.004s.

clingcon:
There is also an example of this problem in clingcon's distribution (sendmoremoney1.lp). It works and is very fast (0.001s). However, I have not fully understood how to encode other similar problems with optimizations, different domains, etc (e.g. SEND + MOST + MONEY) etc, so understanding this is a further project. Also, it seems to have some bugs in the current version, especially regarding global constraints.

send_most_money.lp
This solves the problem SEND+MOST=MONEY and maximizes MONEY. It seems to works since it show the correct maximum value of MONEY (10768). Almost, since the value of Optimization is not this value but a much larger number (4949944124). I read in the Potassco documentation that maximization is converting to minimization with the weights inversed. Maybe the weird Optimization value is a consequence of that?
Answer: 1
x(e,6) x(n,7) x(d,3) x(m,1) x(o,0) x(t,5) x(y,8) x(s,9) y(money,10768)
Optimization: 4949944232
x(e,7) x(n,8) x(d,4) x(m,1) x(o,0) x(t,2) x(y,6) x(s,9) y(money,10876)
Optimization: 4949944124
OPTIMUM FOUND

As with send_more_money.pl it takes a long time to solve it: 2:24 minutes with clingo (gringo/clasp). With lparse/smodels it takes much longer.
gringo/clasp: 2:24 minutes
gringo/smodels: over 7 minutes and then some "strange" result is shown from
smodels (e.g. not y(money,98979)) and I stopped.
lparse/smodels: over 10 minutes for lparse
lparse/clasp:  over 10 minutes for lparse

send_more_money_any_base.lp: SEND+MORE=MONEY for "any" base
For n=10 it takes the same time as for send_more_money.lp. For n=11 it takes 1:37 minutes to show all 3 solutions.

least_diff.lp
I have also implemented the least diff problem, but I stopped the program after 30 minutes without any answer.

Well, maybe my approach in these problems is not a good example of how to use ASP. However, they are standard examples in CP.

### Other standard CP/OR problems

Here are some other standard CP or OR problems.

all_interval.lp: All interval.

langford.lp: Langford numbers
This follows quite nicely the MiniZinc version in langford.mzn
#const k = 4.
val(1..k).   % for solution
pos(1..2*k). % for position
% alldifferent position
1 { position(I, N) : pos(N) } 1 :- pos(I).
1 { position(I, N) : pos(I) } 1 :- pos(N).

% The difference in position between the two I's is I.
:- position(I+k, N1), position(I, N2), N1 != N2 + I+1, val(I).

% solution: unique index
1 { solution(I, N) : val(N) } 1 :- pos(I).

% exactly two occurrences of 1..k
2 { solution(I, N) : pos(I) } 2 :- val(N).

% channel solution <-> position
:- position(I, P1), solution(P1, I2), I != I2, val(I;I2).
:- position(I+k, P2), solution(P2, I2), I != I2, val(I;I2).

alldifferent_except_0.lp: All different except 0
Since there is no concept of global constraints in ASP, one has to code these kind of constraints directly:
#const n = 6.
#const m = 9.
values(0..m).
ix(1..n).
% unique indices of x, 1..n
1 { x(I, Val) : values(Val) } 1 :- ix(I).
% alldifferent except 0
% If Val > 0 then there must be 0..1
% occurrences of Val in x.
{ x(I, Val) : ix(I) } 1 :- values(Val), Val > 0.

% Additional constraint: There must be exactly 2 zeros.
% 2 #sum [ x(I, 0) : ix(I) = 1 ] 2.
% Alternative:
:- not 2 #sum [ x(I, 0) : ix(I) = 1 ] 2.

bus_scheduling.lp: Simple scheduling problem

organize_day.lp: Another simple scheduling problem: How to organize a day
The no overlap requirement are done with the following. A task is defined as the predicate task(TaskId, StartTime, EndTime). The predicate no_overlap is very much Prolog, apart from that the order of rules and the order of predicates don't matter in ASP.
% No overlap of tasks
End1 <= Start2.

End2 <= Start1.


photo.lp: Photo problem.
This is an example from Mozart/Oz tutorial on FD. The object is to maximize the number of satisfied preferences. I like this direct approach.
persons(betty;chris;donald;fred;gary;mary;paul).
positions(1..7).
% Preferences:
pref(betty, gary;mary).
pref(chris, betty;gary).
pref(fred, mary;donald).
pref(paul, fred;donald).
% alldifferent positions
1 { position(Person, Pos) : positions(Pos) } 1 :- persons(Person).
1 { position(Person, Pos) : persons(Person) } 1 :- positions(Pos).
next_to(P1,P2) :-
pref(P1,P2),
position(P1,Pos1),
position(P2,Pos2),
|Pos1-Pos2| == 1.
% maximize the number of satisfied preferences
#maximize [ next_to(P1,P2) ].

post_office_problem2.lp: Post office problem

coloring.lp: Simple map coloring problem
countries(belgium;denmark;france;germany;netherlands;luxembourg).
colors(red;green;blue;white).
arc(france,belgium;luxembourg;germany).
arc(luxembourg,germany;belgium).
arc(netherlands,belgium).
arc(germany,belgium;netherlands;denmark).
neighbour(X,Y) :- arc(X,Y).
neighbour(Y,X) :- arc(X,Y).
1 {color(X, C) : colors(C) } 1 :- countries(X).
:- color(X1, C), color(X2, C), neighbour(X1,X2).
:- color(germany, red).

A variant to the generator line (line 9) is to explicit state the different alternative with disjunction using '|' (bar):
color(X, red) | color(X, green) | color(X, blue) | color(X, white) :- countries(X).

However, then one has to use the parameter --shift to gringo/clingo for this to work.

xkcd.lp: xkcd problem
This is a subset sum (or knapsack depending on the definition) problem from xkcd. The object is to select dishes so they give a total of 15.05 (as 1505 in the encoding).
#const total = 1505.
#const n = 10.
amount(0..n).
price(mixed_fruit,215).
price(french_fries,275).
price(host_wings,355).
price(mozzarella_sticks,420).
price(samples_place,580).
prices(P) :- price(_, P).
% each food has exactly one amount
1 { food_amount(Food, Amount) : amount(Amount) } 1 :- food(Food).
% sum to the exact amount
total [ food_amount(F, Amount) : price(F, Price) : prices(Price) : amount(Amount) = Price*Amount ] total.

One can easily change the total (e.g. to 1506) with clingo -c total=1506 xkcd.lp. Also, here is an (not uncommon) example that the data part is larger than the actual program.

subset_sum.lp: Another subset sum problem

3_jugs.lp: 3 jugs problem (as a graph problem)
Here is a graph approach to the 3 jugs problem which shows another use of an adjacency predicate (adj). Here we also add the edge visited with an index to get the order in the graph traversal. The graph is of the states of the different ways of pouring from the jugs. The object is to get the shortest path from node 1 to node 15. Note that we select the edges and then with the predicate selected_nodes calculates which node was visited at what time.
% g(EdgeId, From, To).
g(1, 1, 2).
g(2, 1, 9).
g(3, 2, 3).
g(4, 3, 4).
g(5, 3, 9).
g(6, 4, 5).
g(7, 5, 6).
g(8, 5, 9).
g(9, 6, 7).
g(10, 7, 8).
g(11, 7, 9).
g(12, 8, 15).
g(13, 9, 10).
g(14, 10, 2).
g(15, 10, 11).
g(16, 11, 12).
g(17, 12, 2).
g(18, 12, 13).
g(19, 13, 14).
g(20, 14, 2).
g(21, 14, 15).

#const start = 1.
#const end = 15.
edges(1..21).
ix(1..21).

% ensure that the index is unique
{ selected(Ix, Edge) : edges(Edge) } 1 :- ix(Ix).
% ensure that we visit an edge atmost once
{ selected(Ix, Edge) : ix(Ix) } 1 :- edges(Edge).

adj(X, Y, I) :- g(Edge, X, Y), selected(I, Edge).
adj(X, Y, I) :- g(Edge, X, Z), adj(Z, Y, I+1), selected(I, Edge).

% init the problem: from start to end
% with index 1)

% Here we check which nodes that was involved
% in the selected edges.
selected_nodes(Ix, X, Y) :- selected(Ix, Edge), g(Edge, X, Y).

% minimize the number of edges
#minimize [ selected(Ix, Edge) : edges(Edge) : ix(Ix) ].

The solution is (edited). The first number is the order index.
selected edges:
1,2
2,13
3,15
4,16
5,18
6,19
7,21

selected_nodes
1,1,9
2,9,10
3,10,11
4,11,12
5,12,13
6,13,14
7,14,15

I.e. the nodes are visited in the following order (as it should): 1,9,10,11,12,13,14,15 via the edges 2,13,15,16,18,19,21.

The second definition of adj:
adj(X, Y, I) :- g(Edge, X, Z), adj(Z, Y, I+1), selected(I, Edge).

could as well be written with g/3 and adj/3 swapped:
adj(X, Y, I) :- adj(Z, Y, I+1), g(Edge, X, Z), selected(I, Edge).

ASP don't care about the order of rules in the predicates, in contrast to Prolog where order can matter very much.

### Grid puzzles

There are quite a lot of grid puzzles in my MiniZinc collection and I have implemented some of them in ASP. The problem instances are shown below in the full list of files.

minesweeper.lp: Minesweeper. This encoding was inspired by the "Phase1" encoding from Nitisha Desai's (?) .
nums(0..8).
rows(1..r).
cols(1..c).
%a cell can be a number or a mine
1 {number(R,C,Z) : nums(Z), mine(R, C)} 1 :- rows(R), cols(C).
adj(R,C,R1,C1) :- rows(R;R1), cols(C;C1), |R-R1| + |C-C1|==1.
adj(R,C,R1,C1) :- rows(R;R1), cols(C;C1), |R-R1|==1, |C-C1|==1.
% N mines around a number N
N {mine(R2, C2) : adj(R2,C2,R1,C1)} N :- number(R1,C1,N).

quasigroup_completion.lp: Quasigroup completion
This is pure Latin square conditions that we saw from the Sudoku encoding:
values(1..n).
% all different rows, columns, and ensure that we use
% distinct values
1 { cell(Row, Col, Val) : values(Row) } 1 :- values(Col;Val).
1 { cell(Row, Col, Val) : values(Col) } 1 :- values(Row;Val).
1 { cell(Row, Col, Val) : values(Val) } 1 :- values(Row;Col).

survo puzzle.lp: Survo puzzle.
The lines that handle the row and column sums are the following. Note how we extract the lower/upper bounds of the sum from the predicates rowsum (and colsum).
% Row sums
:- not RowSum #sum [matrix(Row, Col, Val) : values(Val) : cols(Col) = Val] RowSum,
rows(Row), rowsums(Row, RowSum).
% Col sums
:- not ColSum #sum [matrix(Row, Col, Val) : values(Val) : rows(Row) = Val] ColSum,
cols(Col), colsums(Col, ColSum).

discrete_tomography.lp: Discrete tomography

hidato.lp: Hidato puzzle

strimko2.lp: Strimko puzzle

futoshiki.lp: Futoshiki puzzle

fill_a_pix.lp: Fill-a-pix puzzle
This is a Minesweeper variant where the object is to fill a picture. It's almost as the Minesweeper encoding (see above) except that "this" cell is also counted in the hints (which I tend to forget) so the sum around and including a cell can be 9 instead of 8 (as in Minesweeper). Also, the third variant of adj/4 must be added to regard that each cell is its own adjacent.
nums(0..9).
size(1..n).
adj(R,C,R1,C1) :- size(R;C;R1;C1), |R-R1| + |C-C1|==1.
adj(R,C,R1,C1) :- size(R;C;R1;C1), size(C1), |R-R1|==1, |C-C1|==1.
N { x(R2,C2) : adj(R2,C2,R1,C1) } N :- hint(R1,C1,N).

seseman.lp: Seseman convent problem

coins_grid.lp: Coins grid problem
The object is to minimize the sum of the quadratic distances from the main diagonal. As noted before this is a problem that is very easy for MIP solvers, but harder for traditional CP solvers.
#const n = 10. % 31.
#const c = 4. % 14.
values(0..1).
size(1..n).
1 { x(Row, Col, Val) : values(Val) } 1 :- size(Row;Col).
c [ x(Row, Col, Val) : size(Row) : values(Val) = Val ] c :- size(Col).
c [ x(Row, Col, Val) : size(Col) : values(Val) = Val ] c :- size(Row).
#minimize [ x(Row, Col, Val) : values(Val) = Val*|Row-Col|*|Row-Col| ].

It's interesting to see that for a small problem (n=10, c=4) it is much faster than for example the MiniZinc model coins_grid.mzn and Gecode/fz as solver (I have tried to get a better heuristics but not succeeded). It takes 0.93 seconds to solve this with gringo/clasp, and more than 5 minutes with Gecode/fz. Even with the solution is assigned (z = 98) Gecode/fz takes 7 second.

A MIP solver solves the full problem (n=31, c=14) in 0.1 second, but it takes much longer for the ASP solver (I haven't waited for it to end).

### Set covering/set parition

For some reason I like set covering and set partition problems. Most are from OR books or other sources (Winston, Taha, etc). They share a core of constraints but most have some specific twist.

set_covering3.lp: Assigning senators to committee (Katta G Murty)
Here is a version where the senator is represented as integers from 1..10. As we have seen before, the problem instance is represented not as a matrix but as the predicate senator/2.
senator(1, southern;conservative;republicans).
senator(2, southern;liberals;republicans).
senator(3, southern;liberals;democrats).
senator(4, southern;democrats).
senator(5, southern;conservative;democrats).
senator(6, northern;conservative;democrats).
senator(7, northern;conservative;democrats).
senator(8, northern;liberals;republicans).
senator(9, northern;liberals;democrats).
senator(10, northern;liberals;republicans).
senators(1..num_senators).
groups(southern;northern;liberals;conservative;democrats;republicans).
1 { selected(Senator) : senators(Senator) : senator(Senator, Group) } :- groups(Group).
#minimize [selected(Senator) : senators(Senator) ].

The ";"'s in the predicates are enumerating shortcuts, so first line for senator 1 is really a shortcut for these three lines:
senator(1, southern).
senator(1, conservative).
senator(1, republicans).

set_covering3_b.lp: Assigning senators to committee (Katta G Murty)
This is the "symbolic" version of the above.

set_covering.lp: Placing firestations (Winston: "Operations Research")

set_covering_b.lp: Placing firestations, alternative approach

set_covering2.lp: Number of security telephones on campus (Taha: "Operations Research")

set_covering4.lp: Set convering (and set partition) problem (Lundgren, Rönnqvist, Värbrand "Optimeringslära")

set_covering_opl.lp: Selecting workers to build a house (from an OPL model)

set_covering_skiena.lp: Set covering problem from Skiena
Here I use a constant set_covering which is default 1 which mean to run it as a set covering problem. Setting this to 0, (clingo -c set_covering=0 set_covering_skiena.lp) it will be a set partition problem.

combinatorial_auction.lp: Combinatorial auction

bus_scheduling_csplib.lp: Bus driver scheduling from CSPLib. The problem instances (see below) are converted from CSPLib #22. I have not really tried more than the simplest problem instance (t1) with my ASP encoding.

assignment.lp: Assignment Problems from Winston "Operations Research"

ski_assignment.lp: Ski assignment problem

### Asorted puzzles

And last some problems from the worlds of recreational mathematics/logic .

safe_cracking.lp
This is an alphametic problem, but in contrast to send_more_money etc it is solved fast. The first version also used x(5,C5) in the safe_cracking predicate and it took almost 2 seconds to solve. After commenting out this variables, it took 0.2 second.
values(1..9).
% all different
1 { x(I, Val) : values(Val) } 1 :- values(I).
1 { x(I, Val) : values(I) } 1 :- values(Val).
safe_cracking :-
x(1,C1), x(2,C2),x(3,C3),x(4,C4), % x(5,C5),
x(6,C6),x(7,C7),x(8,C8), x(9,C9),
C4 - C6 == C7,
C1 * C2 * C3 == C8 + C9,
C2 + C3 + C6 < C8,
C9 < C8.
:- not safe_cracking.
% no fix points
:- x(I,I), values(I).

marathon2.lp: Marathon puzzle (XPress)

mr_smith.lp: Smith family problem (IF Prolog), a logic problem
Here we can see how the ASP constructs can be used to express different boolean expressions.
persons(mr_smith;mrs_smith;matt;john;tim).
value(go;stay).
% unique indices of person
1 { action(P, T) : value(T) } 1 :- persons(P).

% If Mr Smith comes, his wife will come too.
action(mrs_smith, go) :- action(mr_smith, go).

% At least one of their two sons Matt and John will come.
1 { action(matt, go), action(john, go) }.

% Either Mrs Smith or Tim will come but not both.
1 { action(mrs_smith, go), action(tim, go) } 1.

% Either Tim and John will come or neither will come.
:- action(tim, T1), action(john, T2), T1 != T2.

% If Matt comes then John and his father will also come.
2 { action(john, go), action(mr_smith, go) } 2 :- action(matt, go).

just_forgotten.lp: Just forgotten puzzle (Enigma 1517)

place_number.lp: Place number problem

a_round_of_golf.lp: A round of gold (Dell Logic Puzzles)
The hardest part to get right in this encoding was the requirement that in MiniZinc is quite simple to state:
(
(score[Frank] = score[Sands] + 4 /\ score[caddy] = score[Sands] + 7 )
\/
(score[Frank] = score[Sands] + 7 /\ score[caddy] = score[Sands] + 4 )
)

I have translated it as the following. Note the integrity constraint to ensure that we don't want both of them to be false.
p3d1 :-
score(frank, FrankScore),
last_name(Sands, sands),
score(Sands, SandsScore),
FrankScore == SandsScore + 4,

p3d2 :-
score(frank, FrankScore),
last_name(Sands, sands),
score(Sands, SandsScore),
FrankScore = SandsScore + 7,

:- not p3d1, not p3d2. % not both false


### Transitive closure

One thing that should also be mentioned is transitive closure which is some kind of trademark for Answer Set Programming (as well as planning). I have not used this much I have implemented mostly my traditional CP "learning problems" that does not use this type of structure. Here are two examples of transitive closures, and it is the succintness of these encodings that I made me so fascinated by ASP.

#### Hamiltonian cycle

For example Hamiltonian cycle can be encoded like this in ASP. The predicate e(X,Y) is the edge between two nodes, and v(X) is the vertices. The cycle is hardcoded to start from node 0.
{in(X,Y)} :- e(X,Y).

:- 2 {in(X,Y) : e(X,Y)}, v(X).
:- 2 {in(X,Y) : e(X,Y)}, v(Y).

r(X) :- in(0,X), v(X). % start from node 0
r(Y) :- r(X), in(X,Y), e(X,Y).

:- not r(X), v(X).


#### Clique

Here is an encoding of the problem finding maximum cliques, from Wikipedia Answer_set_programming#Large_clique:
v(X) :- e(X,Y).

n {in(X) : v(X)}.
% n {in(X) : v(X)} n. % exact size of clique

% Clique criteria
:- in(X), in(Y), v(X), v(Y), X!=Y, not e(X,Y), not e(Y,X).

% Find maximum clique
#maximize [in(X) : v(X)].

I have implemented an example of the maximum clique problem in the ASP program clique.lp with the problem instances clique_data1.lp, clique_data2.lp, clique_data2.lp.

## Future projects

As mentioned above, Answer Set Programming is a large subject and this blog post has only showed a small part. Here are some of my possible future projects regarding Answer Set Programming:
• learn how to tweak clasp etc.
• look more into clingcon, the hybrid of ASP and CP
• look more into iclingo (another Potassco tool), an incremental grounder/solver which can be used to incrementally step different variables for example in planning problems
• planning: I first read about ASP when I was interested in automated planning so it might be time to continue this track
• transitive closures: There are a lot of fun problems that use transitive closures. One example that I have in mind is the Rogo puzzle that Mike Trick wrote about some weeks ago in Operations Research, Sudoko, Rogo, and Puzzles. ASP might be good and natural approach to this. (I did a first try with ASP on Rogo but didn't get it right...)
• DLV and other ASP grounders/solvers. There is a lot of more or less experimental tools that use different techniques and encoding schemes.

## My Answer Set Programming programs

Here are my ASP programs collected for easy reference together with their problem instances. Almost all of of them have been implemented in some Constraint Programming system before, for example MiniZinc. See Common constraint programming problems for a list of different CP implementations.

All are written to comply with the Potassco tools (gringo/clasp/clingo), but should be quite easy to use with to other ASP systems, e.g. Lparse/Smodels.

## December 13, 2010

### Christmas Company Competition Problem: Mixing teams

This blog post is my entry in December Blog Challenge: O.R. and the Holidays. (Note: since I'm not a INFORMS member, this entry might get disqualified.)

This week my company (the local office) is having the annually Christmas gathering and we will - after eating some good Brazilian food - go bowling.

Mixing the teams as good as possible in these kind of gatherings can be quite important and I have here created a MiniZinc model (company_competition.mzn) for this.

### Problem statement

In this problem I have decided that the teams should be picked (mixed) according to the following requirements (see below for other considerations):
• We are in total 18 contestants and there should be 4 or 5 persons in each team, which gives 4 teams. (Different teams sizes are discussed below.)
• There should be as even distribution of sexes in each team as possible. There are 12 males and 6 females.
• There are 3 departments (IT, Custom relations 1, Custom relations 2) and these should be mixed as much as possible. As it happens, all 3 departments consists of 6 persons each.
• The managers for each department should be in different teams, if possible.
The number of violations of these requirements is then minimized (the variable z in the model).

### MiniZinc model

The MiniZinc model used is company_competition.mzn.

This model is slightly simplified and includes our first names and departments for realism (I'm "hakan" as you may have guessed). For general use of this model - e.g. our the next Christmas competition, or by some other company - the problem instance should have been in a separate data file (this is easy to fix), but for clarity I've kept everything is the same file.

The hardest part in modeling this problem was the following which required a lot of experimenting.
• The way to measure the violations is very important to get a good (fair) mixing, and took quite a time. Some of the rejected measurements has been kept (commented) in the model. See below for a comparison of two different measurements.
• To no surprise it took quite a time to get the labeling as good as possible. It may - of course - be a better labeling, but I have not found any.
• Testing different symmetry breaking constraints.

### Results

For Gecode/fz, MiniZinc/fd, and JaCoP/fz the optimal value of z (14) was found almost immediately (< 1 second). However, after that it took quite a while to prove that this was the optimal value. Here are the times (including generating the FlatZinc file) and the number of failures for each solver:
• FzTini: 52 seconds (no failures reported)
• Gecode/fz: 1:37 minutes, 6213161 failures
• JaCoP/fz : 4:02 minutes, 6591510 failures
• MiniZinc/fd: 4:30 minutes: 3726 choice points explored (it don't report # of failures)
• SMT : 11:49 minutes (no failures is reported)
• ECLiPSe/ic: 14:50 minutes (don't have support for set_search so I simply commented it)
• ECLiPSe/fd: 16:32 minutes (don't have support for set_search so I simply commented it)
• LazyFD : > 1 hour
• Choco/fz : error (the solver didn't like the way I use set variables)
• SICStus/fz: error (ibid)
• SCIP: error (don't handle sets)
Later update
Thanks to Joachim Schimpf (ECLiPSe team) I found a small bug in the manager constraint. This caused some solvers to behave badly: ECLiPSe/ic, ECLiPSe/fd), and FzTini. After the fix, FzTini solves the problem in 52 seconds which is the fastest. ECLiPSe don't have support for the set_search so I just comment it when running its solvers, and it may degrade their performance.
End of update

For the presentation of the results, however, the MiniZinc helper program mzn was used since it is the only way to show the output statements. This additional constraint was also added:
/\ z = 14

Also, please note that for getting a "nice" mix of sex and departments I actually did this is two steps: 1) Running with minimize z to obtain the minimum value (as described above), and (in principle) ignored the specific mixing. 2) Before running mzn I changed the labeling somewhat by adding team_sex first in the labeling list (which was not used in the labeling for optimization) since the distribution of sexes tend to be somewhat off. This approach seemed to be easier than looking through many thousands of solutions (with optimal value of z).

#### One solution

Below is one solution (of many) which seems to have a quite fair mixing: the departments and sexes are mixed very good. Since the number of competitors (18) don't divide evenly with t_size (4), we allow team sizes of either t_size (4) or t_size+1 (5).
z (#violations): 14

Teams: [{1, 7, 8, 13, 17}, {2, 5, 9, 10, 18}, {3, 6, 11, 14}, {4, 12, 15, 16}]

Team Departments:
1 2 2
2 2 1
2 1 1
1 1 2

Team Sexes:
3 2
3 2
3 1
3 1

Team_size: [5, 5, 4, 4]

which_team: [1, 2, 3, 4, 2, 3, 1, 1, 2, 2, 3, 4, 1, 3, 4, 4, 1, 2]
1: hakan	1
2: andersj	2
3: robert	3
4: markus	4
5: johan	2
6: micke	3
7: alex	        1
8: andersh	1
9: jennyk	2
10: kenneth	2
11: sara	3
12: cecilia	4
13: stefan	1
14: jacob	3
15: roger	4
16: henrik	4
17: line	1
18: hanna	2

The teams:
Team 1: hakan(M,it) alex(F,cr1) andersh(M,cr1) stefan(M,cr2) line(F,cr2)
Team 2: andersj(M,it) johan(M,it) jennyk(F,cr1) kenneth(M,cr1) hanna(F,cr2)
Team 3: robert(M,it) micke(M,it) sara(F,cr1) jacob(M,cr2)
Team 4: markus(M,it) cecilia(F,cr1) roger(M,cr2) henrik(M,cr2)

Managers:
johan(it) belongs to team 2
cecilia(cr1) belongs to team 4
stefan(cr2) belongs to team 1


#### Some explanations

The mixing of departments ("Team Departments"):
1 2 2  (team 1)
2 2 1  (team 2)
2 1 1  (team 3)
1 1 2  (team 4)

means that the first team consists of 1 person from department 1 (it), and 2 persons from departments 2 (cr1) and 3 (cr2) respectively. And so on.
Team Sexes:
3 2
3 2
3 1
3 1

shows the number of males and females for each team.

It took Gecode/fz 6:20 minutes (and 6006337 failures) to generate all the 467424 optimal solutions (where z = 14).

### Different violation measurements: departments

As stated above, the measurements of the violations was one of the hardest part. The solution I selected to be the best for measuring department mixing was the following:
sum(t in 1..num_teams, d1,d2 in 1..num_departments where d1 < d2) (abs(team_departments[t,d1] - team_departments[t,d2]))

One alternative version is to measure the department "mixedness" against some ideal value: the team size divided by the number of departments:
sum(t in 1..num_teams, d in 1..num_departments) (abs(team_departments[t,d] - (team_size[t] div num_departments)))

Using this latter version we get the following as the first solution from mzn, but it don't look as fair as the first variant shown above: both for the mixing of the departments and the sexes could be better. Note: I realize that there are many solutions with z = 12 and there may be some other optimal solution that looks more fair.
z (#violations): 12

Teams: [{1, 7, 8, 9, 13}, {2, 5, 10, 14, 17}, {3, 6, 11, 15}, {4, 12, 16, 18}]

Team Departments:
1 3 1
2 1 2
2 1 1
1 1 2

Team Sexes:
3 2
4 1
3 1
2 2

Here are the times to solve this optimization problem (to prove that z = 12 is the optimal value) for the three fastest solvers above:
• Gecode/fz: 1:30 minutes, 6502528 failures
• JaCoP/fz : 3:45 minutes, 6885666 failures
• MiniZinc/fd: 4:33 minutes, 21 choice points explored
Well, it seems that the time and the number of failures are about the same for Gecode/fz and MiniZinc/fd. For JaCoP/fz it's slightly faster.

### Different team sizes

Given the same problem instance, the same constraints and search labeling, how do different team sizes change the time to solve the problem? By changing t_size we see that team size of 4 was the hardest one.

For t_size = 3 it took Gecode/fz 15 seconds (742196 failures) to realize that it is quite easy to pick mixed teams. This seems to be an optimal mixing.
team_departments:
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1
1 1 1

team_sex:
2 1
2 1
2 1
2 1
2 1
2 1

team_size: 3 3 3 3 3 3

which_team: 1 2 3 4 5 6 1 2 3 4 5 6 1 3 5 6 2 4
z = 6;


For t_size = 5 if took Gecode/fz 2.2 seconds (122300 failures) to get the following optimal solution (z = 6). However, a better mixing of sexes must be sought in another optimal solution.
team_departments:
2 2 2
2 2 2
2 2 2

team_sex:
5 1
4 2
3 3

z = 6;

For t_size = 6 we get the same solution as for 5 but it took Gecode/fz slightly longer, 3.5 seconds (202517 failures).

### Final notes

The mixing model presented above can - of course - be used for competitions other than Christmas company competitions.

Also, other mixing requirements could have been taken into considerations, such as:
• different offices: people from different offices (say different cities) should be mixed
• ages: a fair mixing of age groups may be of some point.
• time of employment.
• experience in the target activity of competition: If some are very experienced in the target activity (e.g. former bowling pros), they should be put in different teams. Some kind of handicap system might also be used, e.g. that these pros counts as two persons etc.
• there might also be that some persons cannot stand each other. Depending on the management principle these should either be in the same team (to learn to cooperate) or in different teams (so they don't ruin a team). The opposite case, i.e. where two person are together as couple (or family, etc) may be handled in the same way.

### Christmas related

Related to this (Christmas and OR) is the two Secret Santa models I wrote about a year ago: Merry Christmas: Secret Santas Problem and 1 year anniversary and Secret Santa problem II

## December 09, 2010

### Global Constraint Catalog has been updated

The great Global Constraint Catalog has been updated.

The PDF version (about 20Mb) of the catalog is: Nicolas Beldiceanu, Mats Carlsson, Jean-Xavier Rampon: Global Constraint Catalog 2nd Edition, Working version of SICS Technical Report T2010:07, ISSN: 1100-3154, ISRN: SICS-T–2010/07-SE (November 22, 2010).

Abstract: This report presents a catalogue of global constraints where each constraint is explicitly described in terms of graph properties and/or automata and/or first order logical formulae with arithmetic. When available, it also presents some typical usage as well as some pointers to existing filtering algorithms.

The website (managed by Sophie Demassey): Global Constraint Catalog.

Here is the changelog:

2010-11-18 working version update: 354 constraints

Great work, and very useful additions. I especially like the new exercices of modelisation with global constraints.

## December 07, 2010

### MiniZinc version 1.2.2 released

From NEWS file:

G12 MiniZinc Distribution 1.2.2
-------------------------------

Changes to the MiniZinc language:

* We have added a new built-in function trace/2 that can be used to
print debugging output during flattening, for example the following
MiniZinc fragment:

constraint forall (i in 1 .. 5) (
trace("Processing i = " ++ show(i) ++ "\n",
x[i] < x[i + 1]
)
);

will cause mzn2fzn to print the following as the above constraint
is flattened:

Processing i = 1
Processing i = 2
Processing i = 3
Processing i = 4
Processing i = 5

Other changes in this release:

* The FlatZinc interpreter's -s option is now a synonym for the --solver-statistics option instead of the --solver-backend option.

* The FlatZinc interpreter's LazyFD backend can now print out the number of search nodes explored after each solution is generated.

* The mzn script has been extended so that comments in the FlatZinc output stream, such as those containing solver statistics, are printed after the output produced by processing the output item.

Bugs fixed in this release:

* A bug that caused the mzn script to abort if the model contained a large array literal has been fixed.