JSON Output

248 views
Skip to first unread message

Helmut Simonis

unread,
Feb 11, 2021, 5:07:38 PM2/11/21
to MiniZinc
The documentation states that MiniZinc can produce JSON output instead of its usual output, but does not state how this can be turned on. Can somebody please explain how to do this? Is it linked to the format of the input data?

guido.tack

unread,
Feb 11, 2021, 5:10:37 PM2/11/21
to MiniZinc
Hi Helmut,

you can use the command line option --output-mode json:

Cheers,
Guido

Helmut Simonis

unread,
Feb 21, 2021, 11:04:48 PM2/21/21
to MiniZinc
Thanks Guido, much appreciated.
May I suggest to add this in the usage help of the minizinc tool itself, and perhaps a link in Section 4.1.3 of the Reference Manual.

Helmut Simonis

unread,
Feb 21, 2021, 11:04:48 PM2/21/21
to MiniZinc
Hi,
I'm still a bit puzzled about the JSON output. In my optimization problem, I run into a time-out, so to get the best solution I need to use the

-a 

option, I think. This means that all intermediate solutions are printed as well. I'm ok with that, but now I have to read all these solutions together, as I use a file based interface. I want to parse the result as an JSONArray, with each solution found as a JSONObject structure describing the results. So I'm using the flags

--output-mode json --solution-separator "" error-msg "]" --time-limit 60000 --solution-comma "," --unsat-msg "]" --unbounded-msg "]" --search-complete-msg "]" --no-output-comments

This works, but the resulting output is missing the starting "[" , which is a bit weird. Is there a way of adding this by minizinc, so that the result file is valid JSON? Alternatively can I get only the best solution within a time-out, skipping all intermediate solutions?

Also, using 

--output-time 

does not work in this setting, it is printed as a comment "% ..." which is not supported by JSON. As a feature request, it would be nice if it was added to the JSONObject as an extra field, like the "_objective" field added by 

--output-objective. 

which I don't use, as the objective already is a decision variable in my model.
The complete command line I currently use (with different solvers and timeout values) is

 minizinc --output-mode json --no-output-comments --output-time --solver Chuffed --solution-separator "" --solution-comma "," --error-msg "]" --unsat-msg "]" --unbounded-msg "]" --search-complete-msg "]" --output-objective -a --time-limit 30000 -o result.json program.mzn

I hope this is not too much information, but it might serve as an (bad) example.

Helmut
On Thursday, 11 February 2021 at 22:10:37 UTC guido.tack wrote:

Jip J. Dekker

unread,
Feb 21, 2021, 11:15:14 PM2/21/21
to MiniZinc
Hi Helmut,

Although MiniZinc supports outputting a solution as JSON, it currently does not have a mode that allows you to directly use its output as a JSON file. I once started on a seperate output mode like this for MiniZinc Python, but found that the amount of different types of output in MiniZinc makes the JSON spec very unwieldy. Instead it felt nicer to preprocess the output of MiniZinc and correctly identify the solutions before parsing them as JSON. Similarly, you can then process "% mzn-stat:" comments to acquire the statistics. Currently the other output is ignored by MiniZinc Python. You can find my processing function (that works after splitting by the solution separator) here: https://github.com/MiniZinc/minizinc-python/blob/develop/src/minizinc/result.py#L291-L346

It might be interesting to look at using MiniZinc Python directly. Although it would mean you would have to use Python, all this code to correctly interface with the MiniZinc executable is already in place.

Kind regards,
Jip

guido.tack

unread,
Feb 22, 2021, 12:06:38 AM2/22/21
to MiniZinc
Hi Helmut,

further to Jip's comments, we're currently working on a new output framework for MiniZinc 2.6 that should solve this problem. We'll also make sure that --output-time and all the statistics outputs will work in the new framework.

As a (slightly ugly) workaround to get the opening bracket, you can create a file called open_bracket.mzn with the following one-liner:

constraint trace_stdout("[");

and then give that as an additional model argument to minizinc.

Cheers,
Guido

Reply all
Reply to author
Forward
0 new messages