this repo has no description
1Using MiniZinc in Jupyter Notebooks
2===================================
3
4You can use MiniZinc inside a Jupyter / IPython notebook using the ``iminizinc`` Python module. The module provides a "cell magic" extension that lets you solve MiniZinc models.
5
6The module requires an existing installation of MiniZinc.
7
8Installation
9------------
10
11You can install or upgrade this module via ``pip``:
12
13
14.. code-block:: bash
15
16 pip install -U iminizinc
17
18Consult 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).
19
20Make sure that the ``minizinc`` binary are on the ``PATH`` environment variable 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
21You then need to change your ``PATH`` environment variable to include the MiniZinc installation.
22
23Basic usage
24-----------
25
26After 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:
27
28.. code::
29
30 In[1]: %load_ext iminizinc
31
32 In[2]: n=8
33
34 In[3]: %%minizinc
35
36 include "globals.mzn";
37 int: n;
38 array[1..n] of var 1..n: queens;
39 constraint all_different(queens);
40 constraint all_different([queens[i]+i | i in 1..n]);
41 constraint all_different([queens[i]-i | i in 1..n]);
42 solve satisfy;
43 Out[3]: {u'queens': [4, 2, 7, 3, 6, 8, 5, 1]}
44
45As 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.
46
47Alternatively, you can bind the decision variables to Python variables:
48
49.. code::
50
51 In[1]: %load_ext iminizinc
52
53 In[2]: n=8
54
55 In[3]: %%minizinc -m bind
56
57 include "globals.mzn";
58 int: n;
59 array[1..n] of var 1..n: queens;
60 constraint all_different(queens);
61 constraint all_different([queens[i]+i | i in 1..n]);
62 constraint all_different([queens[i]-i | i in 1..n]);
63 solve satisfy;
64
65 In[4]: queens
66
67 Out[4]: [4, 2, 7, 3, 6, 8, 5, 1]
68
69If you want to find all solutions of a satisfaction problem, or all intermediate solutions of an optimisation problem, you can use the ``-a`` flag:
70
71.. code::
72
73 In[1]: %load_ext iminizinc
74
75 In[2]: n=6
76
77 In[3]: %%minizinc -a
78
79 include "globals.mzn";
80 int: n;
81 array[1..n] of var 1..n: queens;
82 constraint all_different(queens);
83 constraint all_different([queens[i]+i | i in 1..n]);
84 constraint all_different([queens[i]-i | i in 1..n]);
85 solve satisfy;
86
87 Out[3]: [{u'queens': [5, 3, 1, 6, 4, 2]},
88 {u'queens': [4, 1, 5, 2, 6, 3]},
89 {u'queens': [3, 6, 2, 5, 1, 4]},
90 {u'queens': [2, 4, 6, 1, 3, 5]}]
91
92The 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
93
94.. code::
95
96 In[1]: %%minizinc?