Search code examples
pythonminizinc

Minizinc Python - using .dzn module instead of instance module


Imagine I have .mzn file called abc.mzn and it is as follows.

array[1..3] of int:a;
output[show(a)];

Now I have a .dzn file called cde.dzn and it is as follows.

 a=[1,2,3];

I will run minizinc python package as below,

import minizinc as minizinc
from minizinc import Instance,Model,Solver

x=Solver.lookup("geocode")
M1=Model("./abc.mzn")

instance1=Instance(x,M1)
instance1("a")=[1,2,3]
result = instance1.solve()
print(result)

Above code works fine and no problem with that.I am keen to use dzn module in this python code instead of Instance module and to get rid of manually assigning below line. As you can see we need to manually assign the values for all parameters using instance1=..

 instance1("a")=[1,2,3]

Is there any way that we can use .dzn file to assign values(using dzn module).I noted that in the package itself we have the dzn module already there.

can we do in below manner or how to get results.

import minizinc as minizinc
from minizinc import dzn,Model,Solver

M1=Model("./abc.mzn")
D1=dzn("./cde.dzn") etc..

Solution

  • The DZN module in MiniZinc Python is meant to be used through the .add_file method of Instance/Model. Using this method you can add data files (.dzn/.json) or additional model files .mzn to your MiniZinc model or instance.

    So for your example would become:

    from minizinc import Model
    
    M1 = Model("./abc.mzn")
    M1.add_file("./cde.dzn")