Solver([globals_dir]) Abstract solver class.
Gecode([mzn_path, fzn_path, globals_dir]) Interface to the Gecode solver.
Chuffed([mzn_path, fzn_path, globals_dir]) Interface to the Chuffed solver.
Optimathsat([path, globals_dir]) Interface to the Optimathsat solver.
Opturion([path, globals_dir]) Interface to the Opturion CPX solver.
MIPSolver([path, globals_dir]) Interface to the MIP solver.
Gurobi([path, globals_dir]) Interface to the Gurobi solver.
CBC([path, globals_dir]) Interface to the COIN-OR CBC solver.
G12Solver([mzn_path, fzn_path, globals_dir, …]) Interface to a generic G12 solver.
G12Fd([mzn_path, fzn_path, globals_dir]) Interface to the G12Fd solver.
G12Lazy([mzn_path, fzn_path, globals_dir]) Interface to the G12Lazy solver.
G12MIP([mzn_path, fzn_path, globals_dir]) Interface to the G12MIP solver.
gecode Default Gecode instance.
chuffed Default Chuffed instance.
optimathsat Default Optimathsat instance.
opturion Default Opturion instance.
gurobi Default Gurobi instance.
cbc Default CBC instance.
g12fd Default G12Fd instance.
g12lazy Default G12Lazy instance.
g12mip Default G12Lazy instance.

Provides classes to interface solvers with PyMzn.

PyMzn interfaces with solvers through the Solver class. This class includes the necessary infomation for PyMzn to setup the solver. This class also includes the solve method which takes care of the actual solving of a MiniZinc/FlatZinc model. The solvers classes are subclasses of the Solver class, providing implementations of the solve method. PyMzn provides a number of solver implementations out-of-the-box. PyMzn’s default solver is Gecode, which class is pymzn.Gecode and the default instance is pymzn.gecode.

To use a different solver or to exend an existing one, one has to subclass the Solver class and implement the solve method.

For instance:

from pymzn import Solver
from pymzn.utils import run

class MySolver(Solver):
    def __init__(self, path='/path/to/solver'):
        self.cmd = path

    def support_mzn(self):
        return False

    def support_dzn(self):
        return True

    def support_json(self):
        return False

    def support_item(self):
        return False

    def support_dict(self):
        return False

    def support_all(self):
        return False

    def support_timeout(self):
        return False

    # You can ignore the dzn_files, data and include if the solver does not
    # support mzn inputs. Similarly, you can ignore timeout and
    # all_solutions if the solver does not support the timeout and returning
    # all solutions respectively. Check out the Gecode implementation for
    # an example of how to handle these parameters if needed.
    def solve(self, mzn_file, *dzn_files, data=None, include=None,
              timeout=None, all_solutions=False, output_mode='dzn',
              arg1=def_val1, arg2=def_val2, **kwargs):
    # mzn_file contains a fzn if the solver does not support mzn inputs
    args = [self.path, '-arg1', arg1, '-arg2', arg2, mzn_file]
    process = run(args)
    return process.stdout    # assuming the solver returns dzn solutions

Then one can run the minizinc function with the custom solver:

my_solver = MySolver()
pymzn.minizinc('test.mzn', solver=my_solver(), arg1=val1, arg2=val2)