Solvers

Solver(solver_id)

Abstract solver class.

Gecode([solver_id])

Interface to the Gecode solver.

Chuffed([solver_id])

Interface to the Chuffed solver.

Optimathsat([solver_id])

Interface to the Optimathsat solver.

Opturion([solver_id])

Interface to the Opturion CPX solver.

MIPSolver(solver_id)

Generic interface to MIP solvers.

Gurobi([solver_id, dll])

Interface to the Gurobi solver.

CBC([solver_id])

Interface to the COIN-OR CBC solver.

OscarCBLS([solver_id])

Interface to the Oscar/CBLS solver.

ORTools([solver_id])

Interface to the OR-tools solver.

gecode

Interface to the Gecode solver.

chuffed

Interface to the Chuffed solver.

optimathsat

Interface to the Optimathsat solver.

opturion

Interface to the Opturion CPX solver.

gurobi

Interface to the Gurobi solver.

cbc

Interface to the COIN-OR CBC solver.

oscar_cbls

Interface to the Oscar/CBLS solver.

or_tools

Interface to the OR-tools solver.

PyMzn interfaces with solvers through the Solver base class. This class includes the necessary infomation for PyMzn to setup the solver, and provides two main functions to support custom solver arguments and parsing the solution stream of the solver.

PyMzn provides a number of solver implementations out-of-the-box. PyMzn’s default solver is pymzn.gecode, an instance of pymzn.Gecode.

To use a solver that is not provided by PyMzn or to extend an existing one, one has to subclass the Solver class and implement the args method, which returns a list of command line arguments for executing the process. The command line arguments supported by this function have to be paired with proper extraFlags in the solver configuration file (see the solver configuration files page from the MiniZinc reference manual for additional details).

For instance, suppose you have the following configuration file for an external solver, my_solver.msc:

{
  "name" : "My Solver",
  "version": "1.0",
  "id": "org.myorg.my_solver",
  "executable": "fzn-mysolver"
}

You want to support the command line argument --solve-twice-as-fast. First you need to add the flag into the solver configuration file:

{
  "name" : "My Solver",
  "version": "1.0",
  "id": "org.myorg.my_solver",
  "executable": "fzn-mysolver",
  "extraFlags": [
      ["--solve-twice-as-fast", "provides twofold speedup", "bool", "false"]
  ]
}

This will make the argument available to the minizinc executable when using the solver my_solver. Next, to add this option to PyMzn, you need to subclass the Solver class and override the args function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
 from pymzn import Solver

 class MySolver(Solver):
     def __init__(self):
         super().__init__(solver_id='my_solver')

     def args(self, solve_twice_as_fast=False, **kwargs):
         args = super().args(**kwargs)
         if solve_twice_as_fast:
             args.append('--solve-twice-as-fast')
         return args

It is then possible to run the minizinc function with the custom solver:

my_solver = MySolver()
pymzn.minizinc('test.mzn', solver=my_solver, solve_twice_as_fast=True)