Skip to main content
Author:

Guido Tack <guido.tack@monash.edu>

homepage:

https://github.com/minizinc/iminizinc

This module provides a cell magic extension for IPython / Jupyter notebooks that lets you solve MiniZinc models.

The module requires an existing installation of MiniZinc.

Installation

You can install or upgrade this module via pip

pip install -U iminizinc

Consult your Python documentation to find out if you need any extra options (e.g. you may want to use the –user flag to install only for the current user, or you may want to use virtual environments).

Make sure that the mzn2fzn binary as well as solver binaries (currently only fzn-gecode and mzn-cbc are supported) are on the PATH when you start the notebook server. The easiest way to do that is to get the “bundled installation” that includes the MiniZinc IDE and a few solvers, available from GitHub here: https://github.com/MiniZinc/MiniZincIDE/releases/latest You then need to change your PATH environment variable to include the MiniZinc installation.

Basic usage

After installing the module, you have to load the extension using %load_ext iminizinc. This will enable the cell magic %%minizinc, which lets you solve MiniZinc models. Here is a simple example:

In[1]:  %load_ext iminizinc

In[2]:  n=8

In[3]:  %%minizinc

        include "globals.mzn";
        int: n;
        array[1..n] of var 1..n: queens;
        constraint all_different(queens);
        constraint all_different([queens[i]+i | i in 1..n]);
        constraint all_different([queens[i]-i | i in 1..n]);
        solve satisfy;
Out[3]: {u'queens': [4, 2, 7, 3, 6, 8, 5, 1]}

As you can see, the model binds variables in the environment (in this case, n) to MiniZinc parameters, and returns an object with fields for all declared decision variables.

Alternatively, you can bind the decision variables to Python variables:

In[1]:  %load_ext iminizinc

In[2]:  n=8

In[3]:  %%minizinc -m bind

        include "globals.mzn";
        int: n;
        array[1..n] of var 1..n: queens;
        constraint all_different(queens);
        constraint all_different([queens[i]+i | i in 1..n]);
        constraint all_different([queens[i]-i | i in 1..n]);
        solve satisfy;

In[4]:  queens

Out[4]: [4, 2, 7, 3, 6, 8, 5, 1]

If you want to find all solutions of a satisfaction problem, or all intermediate solutions of an optimisation problem, you can use the -a flag:

In[1]:  %load_ext iminizinc

In[2]:  n=6

In[3]:  %%minizinc -a

        include "globals.mzn";
        int: n;
        array[1..n] of var 1..n: queens;
        constraint all_different(queens);
        constraint all_different([queens[i]+i | i in 1..n]);
        constraint all_different([queens[i]-i | i in 1..n]);
        solve satisfy;

Out[3]: [{u'queens': [5, 3, 1, 6, 4, 2]},
         {u'queens': [4, 1, 5, 2, 6, 3]},
         {u'queens': [3, 6, 2, 5, 1, 4]},
         {u'queens': [2, 4, 6, 1, 3, 5]}]

You can also store your model to use the model iteratively:

In[1]:  %load_ext iminizinc

In[2]:  %%mzn_model queens

        include "globals.mzn";
        int: n;
        array[1..n] of var 1..n: queens;
        constraint all_different(queens);
        constraint all_different([queens[i]+i | i in 1..n]);
        constraint all_different([queens[i]-i | i in 1..n]);
        solve satisfy;

In[3]:  for n in range(5,9):
            x = %minizinc queens
            print(x)

Out[3]: {'queens': [4, 2, 5, 3, 1]}
        {'queens': [5, 3, 1, 6, 4, 2]}
        {'queens': [6, 4, 2, 7, 5, 3, 1]}
        {'queens': [4, 2, 7, 3, 6, 8, 5, 1]}

The magic supports a number of additional options, in particular loading MiniZinc models and data from files. Some of these may only work with the development version of MiniZinc (i.e., not the one that comes with the bundled binary releases). You can take a look at the help using

In[1]:  %%minizinc?

Release files for iminizinc 0.4

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

Source distribution (sdist)

Source distribution for iminizinc 0.4
File Size Uploaded
iminizinc-0.4.tar.gz 12.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iminizinc 0.4
File Interpreter ABI Platform
iminizinc-0.4-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 18.9 kB

Release files / iminizinc-0.4.tar.gz

Download URL iminizinc-0.4.tar.gz
Size 12.0 kB
Tags Source
SHA-256 checksum
How to use checksums
9ecbd6e486a24dbbcc4d505a752bb4e87f2ce712f41af68ad29affe38ec066cd
BLAKE2b-256 checksum
How to use checksums
18ffd493b2252f80297f5d25158e907cc08d54de833f9e286bd462294dbd5c31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.5

Release files / iminizinc-0.4-py2.py3-none-any.whl

Download URL iminizinc-0.4-py2.py3-none-any.whl
Size 6.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
38c77bda643550d4b2f4443aadd9136a50ae2755e16aeb97f0a7dd0ee6cf3527
BLAKE2b-256 checksum
How to use checksums
7e09f4e5e7de0f88a6b98964f6ed823640f036235eff3b0ef42e738ae9fd307c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.5

Release history Release notifications | RSS feed

This release

0.4 This release

2 release files

0.3

1 release file

0.2

1 release file

0.1

1 release file

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