1

I'm trying to use Minizinc in Jupyter. I've installed all the packages, but when I run the model it's run an error. I tried with the examples on MiniZinc Python pdf, but it runs the same error. The code is:

from minizinc import Instance, Model, Solver

gecode = Solver.lookup("gecode")

model = Model()
model.add_string(
    """
    include "all_different.mzn";
    set of int: A;
    set of int: B;
    array[A] of var B: arr;
    var set of B: X;
    var set of B: Y;

    constraint all_different(arr);
    constraint forall (i in index_set(arr)) ( arr[i] in X );
    constraint forall (i in index_set(arr)) ( (arr[i] mod 2 = 0) <-> arr[i] in Y );
    """
)

instance = Instance(gecode, model)
instance["A"] = range(3, 8)  # MiniZinc: 3..8
instance["B"] = {4, 3, 2, 1, 0}  # MiniZinc: {4, 3, 2, 1, 0}

result = instance.solve()
print(result["X"])  # range(0, 5)
assert isinstance(result["X"], range)
print(result["Y"])  # {0, 2, 4}
assert isinstance(result["Y"], set)

and Jupyther returns:

RuntimeError: asyncio.run() cannot be called from a running event loop
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • 1
    Does this answer your question? [RuntimeError: asyncio.run() cannot be called from a running event loop](https://stackoverflow.com/questions/56154176/runtimeerror-asyncio-run-cannot-be-called-from-a-running-event-loop) – Dekker1 Jan 14 '21 at 02:09
  • The nest_asyncio solve this problem well in normal Jupyter usage. In my case, I am using VSCode for juptyer to run minizinc code. Calling nest_asyncio fails with this error : UnboundLocalError: local variable 'proc' referenced before assignment Same solution works fine in normal jupyter. I am not sure what kind of diffrence with vscode one. – omdit May 19 '22 at 09:18

1 Answers1

1

Follow the installation steps, as they say, it is magic! Using MiniZinc in Jupyter Notebooks.

Just run pip install -U iminizinc and then your cells will be like:

%load_ext iminizinc

(^ this can cause a white output with a strange cyan error, in case, just ignore it. if you want to be sure you successfully loaded minizinc, run the cell again and somehing like The iminizinc extension is already loaded. should appear)

# regular python assigments.
# Note that set and range are not JSON serializable, you will need
# always to convert your iterables to list

A = list(range(3, 8))
B = [4, 3, 2, 1, 0]
%%minizinc

include "all_different.mzn";
set of int: A;
set of int: B;
array[A] of var B: arr;
var set of B: X;
var set of B: Y;

constraint all_different(arr);
constraint forall (i in index_set(arr)) ( arr[i] in X );
constraint forall (i in index_set(arr))
    ( (arr[i] mod 2 = 0) <-> arr[i] in Y );

This last cell is actually minizinc code, executed within the cell, and the output will be your solution.

If you are looking for all the possible solutions, just add the flag -a to the magic: %%minizinc -a

You can also bind the output to a python variable using the flag -m bind: %%minizinc -m bind. In your case, you can check your output variable in another cell like this:

arr # => [0, 1, 2, 3, 4]

For getting all the possible flags available use the magic: %%minizinc?

DDomen
  • 1,808
  • 1
  • 7
  • 17