JSON input

458 views
Skip to first unread message

dcatteeu

unread,
Dec 10, 2017, 5:48:25 PM12/10/17
to MiniZinc
Hi,

The MiniZinc spec (http://www.minizinc.org/doc-lib/minizinc-spec.pdf) mentions support for input in JSON format (Chapter D).

How do I enable this?

It does not seem to work out of the box. Running
mzn-gecode --all-solutions model.mzn data.dzn
works fine, if data.dzn contains
n = 4;
but not when it contains
{ "n": 4 }
Instead, I get
{"n": 4}
    ^
Error: syntax error, unexpected ':', expecting '}'

model.mzn:
int: n;
var 1..n: x;
solve satisfy;

Versions:
$ mzn-gecode --version
G12 MiniZinc evaluation driver, version 2.1.x
Copyright (C) 2011-2015 The University of Melbourne and NICTA
$ uname -a
Darwin neptune.local 17.2.0 Darwin Kernel Version 17.2.0: Fri Sep 29 18:27:05 PDT 2017; root:xnu-4570.20.62~3/RELEASE_X86_64 x86_64

Any help is much appreciated.

Thanks,
david. 

Guido Tack

unread,
Dec 10, 2017, 6:03:36 PM12/10/17
to mini...@googlegroups.com
The data file needs to have the extension .json, the parser currently doesn’t try to auto-detect that format.

Cheers,
Guido

--
You received this message because you are subscribed to the Google Groups "MiniZinc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to minizinc+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/minizinc/ee861246-5b6a-4ff0-9acd-90cba3c3a081%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
GUIDO TACK
Senior Lecturer

Information Technology
Monash University
Level 6, Room 6.40, Building H, Caulfield Campus
900 Dandenong Road
Caulfield East VIC 3145
Australia

dcatteeu

unread,
Dec 11, 2017, 1:52:34 AM12/11/17
to MiniZinc
Thanks Guido. Seems reasonable to implement it that way.

Unfortunately, I bump into another error:

$ mzn-gecode --all-solutions json-input-test.mzn json-input-test.json
minizinc: more than one model file specified.
minizinc: use --help for more information.
$ cat json-input-test.mzn
int: n;
var 1..n: x;
solve satisfy;

$ cat json-input-test.json
{ "n": 4 }

I see no difference when switching solvers. I tried mzn-chuffed and mzn-g12fd.

Using -D to specify the data on the command line, doesn't help either. Then I get the same syntax error as before.

Do I need to build the latest version myself? 

Thanks,
david.


guido.tack

unread,
Dec 11, 2017, 3:51:45 AM12/11/17
to mini...@googlegroups.com
Ah, this is because the minizinc driver executable doesn't recognise json input yet. You have to split up the call into mzn2fzn (for flattening) and fzn-gecode (for solving), piping the output through solns2out, e.g.:

mzn2fzn -Ggecode json-input-test.mzn json-input-test.json
fzn-gecode -a json-input-test.fzn | solns2out json-input-test.ozn

Cheers,
Guido

dcatteeu

unread,
Dec 11, 2017, 7:17:01 AM12/11/17
to MiniZinc
Great, that works!

Claudio Cesar de Sa

unread,
Nov 25, 2019, 9:22:30 AM11/25/19
to MiniZinc
Hi Guido

Any improvement about JSON inputs since this thread (2017) or this workaround
Is the scheme to use JSON inputs?


Thanks


CC

guido.tack

unread,
Nov 26, 2019, 12:51:24 AM11/26/19
to MiniZinc
You can use JSON files (with extension .json) directly with the minizinc driver now. There are still a few limitations (such as array index sets that don't start at 1, and some restrictions around enums) so you may not be able to use JSON with existing models that were written with dzn input in mind, but with any model you write yourself there should be no problem.

Cheers,
Guido

Claudio Cesar de Sa

unread,
Nov 26, 2019, 2:26:36 AM11/26/19
to mini...@googlegroups.com
Thanks Guido


It works perfectly. Later I will be sending the code with arrays ...
The test was with a simple code.


Claudio

--
You received this message because you are subscribed to the Google Groups "MiniZinc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to minizinc+u...@googlegroups.com.

David Wagstaff

unread,
Dec 10, 2019, 5:13:35 PM12/10/19
to MiniZinc

JSON file still has enums issue (as you noted) when using an array of strings in minizinc 3.2.1. 

But the same .mzn and .json work fine in the IDE. I infer that the IDE is simply spawning minizinc on the command line. However, I can't manually get it to work. I can get it to work with a dzn, but the equivalent json is a no go.

Shuai Wang

unread,
Dec 10, 2019, 5:24:02 PM12/10/19
to mini...@googlegroups.com
Hi David,
I am using https://github.com/paolodragone/pymzn to parse the json.


On Tue, Dec 10, 2019 at 5:13 PM David Wagstaff <david.w...@gmail.com> wrote:

JSON file still has enums issue (as you noted) when using an array of strings in minizinc 3.2.1. 

But the same .mzn and .json work fine in the IDE. I infer that the IDE is simply spawning minizinc on the command line. However, I can't manually get it to work. I can get it to work with a dzn, but the equivalent json is a no go.

--
You received this message because you are subscribed to the Google Groups "MiniZinc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to minizinc+u...@googlegroups.com.


--
Thanks,

Shuai Wang, Ph.D.

guido.tack

unread,
Dec 10, 2019, 5:33:24 PM12/10/19
to MiniZinc
If you send me the files that don't work I can have a look. It really shouldn't behave differently in the IDE. In order for MiniZinc to be able to read enums from JSON files, they need to be encoded into objects rather than passed as lists of strings. For example, value A of an enum type is represented as {"e":"A"} in JSON. This is currently undocumented (but the documentation will be available later today).

Cheers,
Guido
Reply all
Reply to author
Forward
0 new messages