Search code examples
jupyter-notebookcompiler-flagsminizinc

Is there a way to bind all the solutions of Minizinc solver to a python variable (when used in jupyter notebook via cell magic)?


I wish to use minizinc to solve an optimisation problem. I'm using it on Jupyter notebook via cell magic, as follows.

For the simple quadratic equation below, when I use the -a flag on its own, I'm getting all the possible solutions for x in the output as desired.

%load_ext iminizinc
a=1
b=4
c=0
%%minizinc -a

include "gecode.mzn";
var -100..100: x;
int: a; int: b; int: c;
constraint a*(x*x) + b*x = c;
solve satisfy;

[Out]: [{'x': -4}, {'x': 0}]

But, when I try to bind all possible solutions to a python variable, it only binds one of the solutions, not all:

%%minizinc -a -m bind

include "gecode.mzn";
var -100..100: x;
int: a; int: b; int: c;
constraint a*(x*x) + b*x = c;
solve satisfy;
x

[Out]:0

Likewise, when I use only the -m bind flag without the -a flag, it binds only one solution to the variable as well:

%%minizinc -m bind
x

[Out]:-4

If there is a way to bind all possible solutions to the python variable, please let me know. (Do I need to declare a variable of different data type?)


Solution

  • The idea behind the bind option is that you bind the solution values of the MiniZinc variables to variables with the same name in Python. It is currently not defined how that would work when you use the -a flag, since it would mean the Python variables somehow need multiple values. Currently it seems that the behaviour is that only the last solution is stored in the Python variables.

    If you have ideas about how to change this, then you can submit an issue to the iMiniZinc repository.

    The workaround is to just use your first example, where the results are returned as a list of dictionaries that contain the solutions.