Skip to main content

A Python wrapper for the MiniZinc tool pipeline.

Project description

PyMzn is a Python library that wraps and enhances the MiniZinc tools for CSP modelling and solving. It is built on top of the minizinc toolkit and provides a number of off-the-shelf functions to readily solve problems encoded in MiniZinc and parse the solutions into Python objects.

Usage

First, you need a MiniZinc model encoding the problem you want to solve. Here is a simple 0-1 knapsack problem encoded with MiniZinc::

%% test.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;


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

The result will be:

SolnStream(solns=[{'x': {3, 5}}], complete=True)

The returned object represent a solution stream, which can be directly referenced and iterated as a list. The minizinc function automatically flattens the MiniZinc model, using the provided mzn and dzn files. It executes the solver on the flattened model and parses the solution stream to get the solutions as Python dictionaries.

PyMzn is also able to:

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

For more information on the PyMzn functions read the reference manual.

Install

PyMzn can be installed via Pip:

pip3 install pymzn

or from the source code available on GitHub:

python3 setup.py install

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

Requirements

PyMzn requires some additional software to be installed on your system before you can use it properly, namely:

You can use any solver you like, but the default one for PyMzn is Gecode. If you use the Gecode solver, PyMzn will work out-of-the-box. PyMzn also supports most of the solvers included in the MiniZinc toolkit. If the solver you are looking for is not supported by PyMzn you can implement your own interface and use it with little configuration (see the Solvers section).

Detailed instructions on how to install MiniZinc and Gecode can be found in the Install section of 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. Some things that would be very useful are:

  • Implement specific interfaces for not yet supported solvers;

  • Enhance existing ones.

Heads up on future changes

Be aware that this project is still currently under development and thus it is not in a stable version yet. Things in the future will certainly change. This is especially due to recent changes in the minizinc library, which are introducing lots of new features that could make some of PyMzn’s features obsolete. At any rate, PyMzn will stay updated to the most recent changes in MiniZinc and keep enhancing its python interface.

Author

Paolo Dragone, PhD student at the University of Trento.

Project details


Download files

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

Source Distribution

pymzn-0.14.9.tar.gz (25.2 kB view hashes)

Uploaded Source

Built Distribution

pymzn-0.14.9.linux-x86_64.tar.gz (48.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page