0

I was trying to solve this problem in Minizinc, taken from Puzzle taken from Gardner :

Ten cells numbered 0,...,9 inscribe a 10-digit number such that each cell, say i, indicates the total number of occurrences of the digit i in this number. Find this number. The answer is 6210001000.

I solved it, and the code is working fine with Gecode:

int: n=9;
set of int: N=0..n;
array[N] of var N: cell;

include "globals.mzn";
constraint global_cardinality(cell, N, cell);
solve satisfy;

output [show(cell), "\n", show(index_set(cell)), " -- ", show(index_set(N))];

Output from Gecode:

[6, 2, 1, 0, 0, 0, 1, 0, 0, 0]
0..9 -- 1..10
----------
==========

However, G12 solvers complain about a assertion failed in global_cardinality:

in call 'assert' Assertion failed: global_cardinality: cover and counts must have identical index sets

True, as the output from Gecode shows, N is 1..10 and cell is 0..9. So my questions are:

  • Why Gecode is working? Different implementation or my program is buggy but I was lucky?
  • How can I fix the program to work with G12 or to make it robust/correct?
Community
  • 1
  • 1
siritinga
  • 4,063
  • 25
  • 38

2 Answers2

2

The problem is that you start your array on 0. While it is technically correct to do so, it is preferred and recommended to start your arrays at 1 (standard in MiniZinc). As you can see there is still some solvers that do not fully support arrays that do not start at 1. There have also been a few bugs connected to the use of arrays that do not start at 0.

I get the same error on g12cpx as you do but modifying the array to

array[1..10] of var N: cell;

gives me the right result.

Kobbe
  • 809
  • 5
  • 16
  • Thank you, this works fine. I had a misunderstanding of how sets work. I assumed that it should start the array at 0 because of the definition of the problem, that is, it should start with cell[0] containing the number of zeros, but I see now the difference. – siritinga Feb 22 '16 at 08:34
1

You can fix this by adding array1d():

 global_cardinality(cell,array1d(0..n,[i | i in N]), cell);

The reason Gecode works but not G12/fd, is that Gecode has its own MiniZinc definition of the constraint which don't include the cardinality check.

hakank
  • 6,629
  • 1
  • 17
  • 27