CPMpy gurobi interface (cpmpy.solvers.gurobi)

Interface to the python ‘gurobi’ package

Requires that the ‘gurobipy’ python package is installed:

$ pip install gurobipy

as well as the Gurobi bundled binary packages, downloadable from: https://www.gurobi.com/

In contrast to other solvers in this package, Gurobi is not free to use and requires an active licence You can read more about available licences at https://www.gurobi.com/downloads/

List of classes


Interface to Gurobi's API

Module details

class cpmpy.solvers.gurobi.CPM_gurobi(cpm_model=None, subsolver=None)[source]

Interface to Gurobi’s API

Requires that the ‘gurobipy’ python package is installed: $ pip install gurobipy

See detailed installation instructions at: https://support.gurobi.com/hc/en-us/articles/360044290292-How-do-I-install-Gurobi-for-Python-

Creates the following attributes: user_vars: set(), variables in the original (non-transformed) model,

for reverse mapping the values after solve()

cpm_status: SolverStatus(), the CPMpy status after a solve() tpl_model: object, TEMPLATE’s model object _varmap: dict(), maps cpmpy variables to native solver variables


For use with s.solve(assumptions=[…]). Only meaningful if the solver returned UNSAT.

Typically implemented in SAT-based solvers

Returns a small subset of assumption literals that are unsat together. (a literal is either a _BoolVarImpl or a NegBoolView in case of its negation, e.g. x or ~x) Setting these literals to True makes the model UNSAT, setting any to False makes it SAT


Post the given expression to the solver as objective to maximize

maximize() can be called multiple times, only the last one is stored


Post the given expression to the solver as objective to minimize

minimize() can be called multiple times, only the last one is stored

objective(expr, minimize=True)[source]

Post the expression to optimize to the solver.

‘objective()’ can be called multiple times, onlu the last one is used.

(technical side note: any constraints created during conversion of the objective

are premanently posted to the solver)


Returns the value of the objective function of the latest solver run on this model


an integer or ‘None’ if it is not run, or a satisfaction problem

solution_hint(cpm_vars, vals)

For warmstarting the solver with a variable assignment

Typically implemented in SAT-based solvers

  • cpm_vars – list of CPMpy variables

  • vals – list of (corresponding) values for the variables

solve(time_limit=None, solution_callback=None, **kwargs)[source]

Call the gurobi solver

Arguments: - time_limit: maximum solve time in seconds (float, optional) - kwargs: any keyword argument, sets parameters of solver object

Arguments that correspond to solver parameters: Examples of gurobi supported arguments include:

  • Threads : int

  • MIPFocus: int

  • ImproveStartTime : bool

  • FlowCoverCuts: int

For a full list of gurobi parameters, please visit https://www.gurobi.com/documentation/9.5/refman/parameters.html#sec:Parameters

solveAll(display=None, time_limit=None, solution_limit=None, **kwargs)[source]

Compute all solutions and optionally display the solutions.

This is the generic implementation, solvers can overwrite this with a more efficient native implementation

  • display: either a list of CPMpy expressions, OR a callback function, called with the variables after value-mapping

    default/None: nothing displayed

  • time_limit: stop after this many seconds (default: None)

  • solution_limit: stop after this many solutions (default: None)

  • any other keyword argument

Returns: number of solutions found


Creates solver variable for cpmpy variable or returns from cache if previously created


Like solver_var() but for arbitrary shaped lists/tensors

static supported()[source]

Check for support in current system setup. Return True if the system has package installed or supports solver, else returns False.


[bool]: Solver support by current system setup.