pymzn.minizinc

pymzn.minizinc(mzn, *dzn_files, args=None, data=None, include=None, stdlib_dir=None, globals_dir=None, declare_enums=True, allow_multiple_assignments=False, keep=False, output_vars=None, output_base=None, output_mode='dict', solver=None, timeout=None, two_pass=None, pre_passes=None, output_objective=False, non_unique=False, all_solutions=False, num_solutions=None, free_search=False, parallel=None, seed=None, rebase_arrays=True, keep_solutions=True, return_enums=False, **kwargs)

Implements the workflow for solving a CSP problem encoded with MiniZinc.

Parameters
  • mzn (str) – The minizinc model. This can be either the path to the .mzn file or the content of the model itself.

  • *dzn_files – A list of paths to dzn files to attach to the minizinc execution, provided as positional arguments; by default no data file is attached.

  • args (dict) – Arguments for the template engine.

  • data (dict) – Additional data as a dictionary of variables assignments to supply to the minizinc executable. The dictionary is automatically converted to dzn format by the pymzn.dict2dzn function.

  • include (str or list) – One or more additional paths to search for included .mzn files.

  • stdlib_dir (str) – The path to the MiniZinc standard library. Provide it only if it is different from the default one.

  • globals_dir (str) – The path to the MiniZinc globals directory. Provide it only if it is different from the default one.

  • declare_enums (bool) – Whether to declare enum types when converting inline data into dzn format. If the enum types are declared elsewhere this option should be False. Default is True.

  • allow_multiple_assignments (bool) – Whether to allow multiple assignments of variables. Sometimes is convenient to simply let the data file override the value already assigned in the minizinc file. Default is False.

  • keep (bool) – Whether to keep the generated .mzn, .dzn, .fzn and .ozn files or not. If False, the generated files are created as temporary files which will be deleted right after the problem is solved. Though files generated by PyMzn are not intended to be kept, this property can be used for debugging purpose. Note that in case of error the files are not deleted even if this parameter is False. Default is False.

  • output_vars (list of str) – A list of output variables. These variables will be the ones included in the output dictionary. Only available if ouptut_mode='dict'.

  • output_base (str) – Output directory for the files generated by PyMzn. The default (None) is the temporary directory of your OS (if keep=False) or the current working directory (if keep=True).

  • output_mode ({'dict', 'item', 'dzn', 'json', 'raw'}) – The desired output format. The default is 'dict' which returns a stream of solutions decoded as python dictionaries. The 'item' format outputs a stream of strings as returned by the solns2out tool, formatted according to the output statement of the MiniZinc model. The 'dzn' and 'json' formats output a stream of strings formatted in dzn of json respectively. The 'raw' format, instead returns the whole solution stream, without parsing.

  • solver (Solver) – The Solver instance to use. The default solver is gecode.

  • timeout (int) – The timeout in seconds for the flattening + solving process.

  • two_pass (bool or int) – If two_pass is True, then it is equivalent to the --two-pass option for the minizinc executable. If two_pass is an integer <n>, instead, it is equivalent to the -O<n> option for the minizinc executable.

  • pre_passes (int) – Equivalent to the --pre-passes option for the minizinc executable.

  • output_objective (bool) – Equivalent to the --output-objective option for the minizinc executable. Adds a field _objective to all solutions.

  • non_unique (bool) – Equivalent to the --non-unique option for the minizinc executable.

  • all_solutions (bool) – Whether all the solutions must be returned. This option might not work if the solver does not support it. Default is False.

  • num_solutions (int) – The upper bound on the number of solutions to be returned. This option might not work if the solver does not support it. Default is 1.

  • free_search (bool) – If True, instruct the solver to perform free search.

  • parallel (int) – The number of parallel threads the solver can utilize for the solving.

  • seed (int) – The random number generator seed to pass to the solver.

  • rebase_arrays (bool) – Whether to “rebase” parsed arrays (see the Dzn files section). Default is True.

  • keep_solutions (bool) – Whether to store the solutions in memory after solving is done. If keep_solutions is False, the returned solution stream can only be iterated once and cannot be addressed as a list.

  • return_enums (bool) – Wheter to return enum types along with the variable assignments in the solutions. Only used if output_mode='dict'. Default is False.

  • **kwargs – Additional arguments to pass to the solver, provided as additional keyword arguments to this function. Check the solver documentation for the available arguments.

Returns

If output_mode is not 'raw', returns a list-like object containing the solutions found by the solver. The format of the solution depends on the specified output_mode. If keep_solutions=False, the returned object cannot be addressed as a list and can only be iterated once. If output_mode='raw', the function returns the whole solution stream as a single string.

Return type

Solutions or str