New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON-output of two-dimensional arrays isn't usable as input. #156

Closed
einarjohan opened this Issue Mar 28, 2017 · 0 comments

Comments

Projects
None yet
1 participant
@einarjohan

einarjohan commented Mar 28, 2017

Using current HEAD on develop ( eb53665 ):

If I pass the sudoku.mzn model from the MiniZinc tutorial to mzn2fzn with --output-mode json, I get JSON-output that I can't then use as input to mzn2fzn. (If I do the same with --output-mode dzn, the resulting output IS usable as input).

Note that I manually had to remove the trailing right parenthesis, as mentioned in #154.

sudoku.mzn (from the tutorial)

include "alldifferent.mzn";                                                                                                                    

int: S;
int: N = S * S;
int: digs = ceil(log(10.0,int2float(N))); % digits for output

set of int: PuzzleRange = 1..N;
set of int: SubSquareRange = 1..S;

array[1..N,1..N] of 0..N: start; %% initial board 0 = empty
array[1..N,1..N] of var PuzzleRange: puzzle;

% fill initial board
constraint forall(i,j in PuzzleRange)(
    if start[i,j] > 0 then puzzle[i,j] = start[i,j] else true endif );

% All different in rows
constraint forall (i in PuzzleRange) (
                   alldifferent( [ puzzle[i,j] | j in PuzzleRange ]) );

% All different in columns.
constraint forall (j in PuzzleRange) (
                   alldifferent( [ puzzle[i,j] | i in PuzzleRange ]) );

% All different in sub-squares:
constraint
        forall (a, o in SubSquareRange)(
                alldifferent( [ puzzle[(a-1) *S + a1, (o-1)*S + o1] |
                                        a1, o1 in SubSquareRange ] ) );

solve satisfy;

output  [ show_int(digs,puzzle[i,j]) ++ " " ++
         if j mod S == 0 then " " else "" endif ++
         if j == N /\ i != N then
             if i mod S == 0 then "\n\n" else "\n" endif
         else "" endif
         | i,j in PuzzleRange ] ++ ["\n"];

sudoku.dzn (also from tutorial):

S=3;
start=[|
0, 0, 0, 0, 0, 0, 0, 0, 0|
0, 6, 8, 4, 0, 1, 0, 7, 0|
0, 0, 0, 0, 8, 5, 0, 3, 0|
0, 2, 6, 8, 0, 9, 0, 4, 0|
0, 0, 7, 0, 0, 0, 9, 0, 0|
0, 5, 0, 1, 0, 6, 3, 2, 0|
0, 4, 0, 6, 1, 0, 0, 0, 0|
0, 3, 0, 2, 0, 7, 6, 9, 0|
0, 0, 0, 0, 0, 0, 0, 0, 0|];

Result from running:

mzn2fzn --output-mode json sudoku.mzn sudoku.dzn
fzn-gecode sudoku.fzn | solns2out sudoku.ozn > sudoku.json

sudoku.json

{
  "puzzle" : [5, 9, 3, 7, 6, 2, 8, 1, 4, 2, 6, 8, 4, 3, 1, 5, 7, 9, 7, 1, 4, 9, 8, 5, 2, 3, 6, 3, 2, 6, 8, 5, 9, 1, 4, 7, 1, 8, 7, 3, 2, 4, 9, 6, 5, 4, 5, 9, 1, 7, 6, 3, 2, 8, 9, 4, 2, 6, 1, 8, 7, 5, 3, 8, 3, 5, 2, 4, 7, 6, 9, 1, 6, 7, 1, 5, 9, 3, 4, 8, 2])
}
----------

If I trim away the ---------- line and the right parenthesis, and then attempt to run:

mzn2fzn --output-mode json sudoku.mzn sudoku.dzn sudoku.json

I get

unknown file:
MiniZinc: type error: initialisation value for `puzzle' has invalid type-inst: expected `array[int,int] of var int', actual `array[int] of int'

While if I do the same with --output-mode dzn, I get a result like:

puzzle = array2d(1..9 ,1..9 ,[5, 9, 3, 7, 6, 2, 8, 1, 4, 2, 6, 8, 4, 3, 1, 5, 7, 9, 7, 1, 4, 9, 8, 5, 2, 3, 6, 3, 2, 6, 8, 5, 9, 1, 4, 7, 1, 8, 7, 3, 2, 4, 9, 6, 5, 4, 5, 9, 1, 7, 6, 3, 2, 8, 9, 4, 2, 6, 1, 8, 7, 5, 3, 8, 3, 5, 2, 4, 7, 6, 9, 1, 6, 7, 1, 5, 9, 3, 4, 8, 2]);
----------

Which after trimming away the ---------- line works fine as input to mzn2fzn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment