Search code examples
minizinc

Minizinc program complains of failed assertion in global_cardinality


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?

Solution

  • 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.