Skip to main content

PyMzn

PyMzn is a Python library that wraps and enhances the MiniZinc tools for constraint programming. PyMzn is built on top of the minizinc toolkit and provides a number of off-the-shelf functions to readily solve problems encoded with the MiniZinc language and return solutions as Python dictionaries.

Usage

First, we need to define a constraint program via MiniZinc. Here is a simple 0-1 knapsack problem encoded with MiniZinc:

    %% knapsack01.mzn %%
    int: n;                     % number of objects
    set of int: OBJ = 1..n;
    int: capacity;              % the capacity of the knapsack
    array[OBJ] of int: profit;  % the profit of each object
    array[OBJ] of int: size;    % the size of each object

    var set of OBJ: x;
    constraint sum(i in x)(size[i]) <= capacity;
    var int: obj = sum(i in x)(profit[i]);
    solve maximize obj;


    %% knapsack01.dzn %%
    n = 5;
    profit = [10, 3, 9, 4, 8];
    size = [14, 4, 10, 6, 9];

You can solve the above problem using the pymzn.minizinc function:

    import pymzn
    solns = pymzn.minizinc('knapsack01.mzn', 'knapsack01.dzn', data={'capacity': 20})
    print(solns)

The result will be:

    [{'x': {3, 5}}]

The returned object is a lazy solution stream, which can either be iterated or directly indexed as a list. The pymzn.minizinc function takes care of all the preprocessing, the communication with the minizinc executable, and the parsing of the solutions stream into Python dictionaries.

PyMzn is also able to:

  • Convert Python dictionaries to dzn format and back (e.g. when passing data to the pymzn.minizinc function);
  • Interface with many different solvers;
  • Preprocess MiniZinc models by embedding code from the Jinja2 templating language;
  • Perform concurrent MiniZinc execution using Python coroutines.

For a follow-up of the previous example, read the PyMzn tutorial.

For more information on the PyMzn classes and functions refer to the reference manual.

Install

PyMzn can be installed via Pip:

    pip install pymzn

or from the source code available on GitHub:

    python setup.py install

Requirements

PyMzn is developed and maintained in Python 3.5. Starting from version 0.18.0, support for Python 2 and versions previous to 3.5 has been dropped (its just too much work mainintaining them). Using the package pymzn.aio for concurrent execution requires Python 3.6 (though it is optional).

PyMzn requires the MiniZinc toolkit to be installed on your machine. Starting from PyMzn 0.18.0, the minimum MiniZinc version required is the 2.2.0. If you need to work with previous versions of MiniZinc, PyMzn 0.17.1 should work fine.

The easiest way to install MiniZinc is to download the MiniZincIDE package, which contains both the MiniZinc binaries and several solvers. After downloading the package, make sure the executables are visible to PyMzn either by setting the PATH environment variable or by configuring it using the pymzn.config module.

For more details take a look at the Install section in the documentation.

Optional dependencies include:

  • Jinja2, for preprocessing through Jinja templating language;
  • PyYAML and appdirs, for loading and saving configuration files.

Author

Paolo Dragone, PhD student at the University of Trento (Italy).

Metadata

Release files for pymzn 0.18.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymzn 0.18.3
File Size Uploaded
pymzn-0.18.3.tar.gz 36.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pymzn 0.18.3
File Interpreter ABI Platform
pymzn-0.18.3-py3-none-any.whl Python 3 none any Details

Total release size: 78.9 kB

Release files / pymzn-0.18.3.tar.gz

Download URL pymzn-0.18.3.tar.gz
Size 36.0 kB
Tags Source
SHA-256 checksum
How to use checksums
de886a906e91335bb4ae3025f65cb8de04646e544df765b4c00c717978315e69
BLAKE2b-256 checksum
How to use checksums
6624aebdfb7867b9aefe1edf926287aae36166f2775b2a34be41d8ee127f704f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Release files / pymzn-0.18.3-py3-none-any.whl

Download URL pymzn-0.18.3-py3-none-any.whl
Size 42.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c4674e6ca87313e3005aa1a406c9a1aa1bdac0afeb0a0ff8316f0f2d89179dcb
BLAKE2b-256 checksum
How to use checksums
dee6429b77f1deae4acbff48b89f12242333ea7731bd704f2a36759ace0d9f90
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.18.3 This release

2 release files

0.18.2

2 release files

0.18.1

2 release files

0.17.1

2 release files

0.16.9

2 release files

0.16.7

2 release files

0.16.3

2 release files

0.16.2

2 release files

0.16.1

2 release files

0.14.9

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page