2 unstable releases

0.2.0 Apr 11, 2024
0.1.0 Jan 19, 2024

#1637 in Parser implementations

Download history 6/week @ 2024-01-15 11/week @ 2024-02-19 9/week @ 2024-02-26 5/week @ 2024-03-11 62/week @ 2024-04-01 169/week @ 2024-04-08 29/week @ 2024-04-15 50/week @ 2024-04-22

310 downloads per month

MPL-2.0 license

33KB
714 lines

Serialization of the FlatZinc data format

FlatZinc is the language in which data and solver specific constraint models are produced by the MiniZinc compiler. This crate implements the FlatZinc serialization format as described in the Interfacing Solvers to FlatZinc section of the MiniZinc reference manual. Although the serde specification in this crate could be used with a range of data formats, MiniZinc currently only outputs this formulation using the JSON data format. We suggest using serde_json with the specification in this crate to parse the FlatZinc JSON files produced by the MiniZinc compiler.

Getting Started

Install flatzinc_serde and serde_json for your package:

cargo add flatzinc_serde serde_json

Once these dependencies have been installed to your crate, you could deserialize a FlatZinc JSON file as follows:

// let path = Path::new("/lorem/ipsum/model.fzn.json");
let rdr = BufReader::new(File::open(path).unwrap());
let fzn: FlatZinc = serde_json::from_reader(rdr).unwrap();
// ... process FlatZinc ...

If, however, you want to serialize a FlatZinc format you could follow the following fragment:

let fzn = FlatZinc::<String>::default();
// ... creat solver constraint model ...
let json_str = serde_json::to_string(&fzn).unwrap();

Note that serde_json::to_writer, using a buffered file writer, would be preferred when writing larger FlatZinc files.

Register your solver with MiniZinc

If your goal is to deserialize FlatZinc to implement a MiniZinc solver, then the next step is to register your solver executable with MiniZinc. This can be done by creating a MiniZinc Solver Configuration (.msc) file, and adding it to a folder on the MZN_SOLVER_PATH or a standardized path, like ~/.minizinc/solvers/. A basic solver configuration for a solver that accepts JSON input would look as follows:

{
  "name" : "My Solver",
  "version": "0.0.1",
  "id": "my.organisation.mysolver",
  "inputType": "JSON",
  "executable": "../../../bin/fzn-my-solver",
  "mznlib": "../mysolver"
  "stdFlags": [],
  "extraFlags": []
}

Once you have placed your configuration file on the correct path, then you solver will be listed by minizinc --solvers. Calling minizinc --solver mysolver model.mzn data.dzn, assuming a valid MiniZinc instance, will (after compilation) invoke the registered executable with a path of a FlatZinc JSON file, and potentially any registered standard and extra flags (e.g., ../../../bin/fzn-my-solver model.fzn.json).

Dependencies

~0.4–1MB
~22K SLoC