Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

A Python wrapper for the MiniZinc tool pipeline.

Project Description

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 parse the solutions into Python objects.

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
s = pymzn.minizinc('knapsack01.mzn', 'knapsack01.dzn', data={'capacity': 20})
print(s)

The result will be:

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

The returned object is a lazy solution stream, which can however be directly referenced as a list. The minizinc function takes care of flattening the MiniZinc model, launching the solver, and parsing the solutions into Python dictionaries.

PyMzn is also able to:

  • Convert python objects to dzn format and back;
  • Interface with many different solvers;
  • Perform dynamic modelling through a Python interface or by embedding code from the Jinja2 templating language;
  • Safely parallelize several instances of the same problem;
  • and more …

For a follow-up of the previous example, read the Quick Start guide.

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

Currently, PyMzn is developed and maintained in Python 3.5 with a porting to Python 2.7 at every release (the python2 branch does not always contain the most recent changes).

Requirements

PyMzn requires the MiniZinc toolkit to be installed on your machine, along with at least one solver. 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 by either 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.

Contribute

If you find a bug or think of a useful feature, please submit an issue on the GitHub page of PyMzn.

Pull requests are very welcome too. If you are interested in contributing to the PyMzn source code, read about its implementation details.

Author

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

Release History

Release History

This version
History Node

0.16.5

History Node

0.16.4

History Node

0.16.3

History Node

0.16.2

History Node

0.16.1

History Node

0.14.9

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
pymzn-0.16.5.linux-x86_64.tar.gz (91.7 kB) Copy SHA256 Checksum SHA256 any Dumb Binary Dec 1, 2017
pymzn-0.16.5.tar.gz (28.1 kB) Copy SHA256 Checksum SHA256 Source Dec 1, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting