CPMpy exact interface (cpmpy.solvers.exact)

Interface to Exact’s Python API

Exact solves decision and optimization problems formulated as integer linear programs. Under the hood, it converts integer variables to binary (0-1) variables and applies highly efficient propagation routines and strong cutting-planes / pseudo-Boolean conflict analysis.

The solver’s git repository: https://gitlab.com/nonfiction-software/exact

Always use cp.SolverLookup.get("exact") to instantiate the solver object.

Installation

Requires that the ‘exact’ python package is installed:

$ pip install exact

Warning

Exact requires Python 3.10 or higher and the pip install only works on Linux and Windows. On MacOS, you have to install the package from source.

See https://pypi.org/project/exact for more information.

The rest of this documentation is for advanced users.

List of classes

CPM_exact

Interface to Exact's Python API

Module details

class cpmpy.solvers.exact.CPM_exact(cpm_model=None, subsolver=None, **kwargs)[source]

Interface to Exact’s Python API

Creates the following attributes (see parent constructor for more):

  • xct_solver : the Exact instance used in solve() and solveAll()

  • assumption_dict : maps Exact variables to (Exact value, CPM assumption expression)

Documentation of the solver’s own Python API is sparse, but example usage can be found at: https://gitlab.com/nonfiction-software/exact/-/tree/main/python_examples

add(cpm_expr: Expression | bool | bool | Sequence[Expression | bool | bool | Sequence[NestedBoolExprLike] | ndarray] | ndarray) CPM_exact[source]

Eagerly add a constraint to the underlying solver.

Any CPMpy expression given is immediately transformed (through transform()) and then posted to the solver in this function.

This can raise ‘NotImplementedError’ for any constraint not supported after transformation

The variables used in expressions given to add are stored as ‘user variables’. Those are the only ones the user knows and cares about (and will be populated with a value after solve). All other variables are auxiliary variables created by transformations.

Parameters:

cpm_expr (NestedBoolExprLike) – CPMpy expression, or list thereof

Returns:

self

static fix(o)[source]
get_core()[source]

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

has_objective()[source]

Returns whether the solver has an objective function or not.

static is_multiplication(cpm_expr)[source]
maximize(expr: Expression) None

Post the given expression to the solver as objective to maximize

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

minimize(expr: Expression) None

Post the given expression to the solver as objective to minimize

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

classmethod mus_native(soft, hard=[])[source]

For using the solver’s internal MUS extractor

Parameters:
  • soft – List of soft constraints over which a MUS needs to be found

  • hard – List of hard constraints that always need to be satisfied

Returns a MUS.

property native_model

Returns the solver’s underlying native model (for direct solver access).

objective(expr, minimize)[source]

Post the given expression to the solver as objective to minimize/maximize.

Parameters:
  • expr – Expression, the CPMpy expression that represents the objective function

  • minimize – Bool, whether it is a minimization problem (True) or maximization problem (False)

objective_value() int | None

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

Returns:

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

objective_value_: int | None
print_display(display: Expression | Sequence[Expression] | ndarray | Callable[[], None] | None) None

Helper function for printing the display argument used in solveAll().

Parameters:

display – either a CPMpy Expression, OR a list of expressions, OR a callback function (no-arg) to call.

solution_hint(cpm_vars: List[_NumVarImpl], vals: List[int | bool])[source]

Exact supports warmstarting the solver with a partial feasible assignment.

Parameters:
  • cpm_vars – list of CPMpy variables

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

solve(time_limit: float | None = None, assumptions: Iterable[_BoolVarImpl] | None = None, **kwargs)[source]

Call Exact

Overwrites self.cpm_status

Parameters:
  • assumptions (iterable (e.g. list, set, tuple) of CPMpy Boolean variables) – CPMpy Boolean variables (or their negation) that are assumed to be true. For repeated solving, and/or for use with s.get_core(): if the model is UNSAT, get_core() returns a small subset of assumption variables that are unsat together.

  • time_limit (int or float) – optional, time limit in seconds

Returns:

Bool: - True if a solution is found (not necessarily optimal, e.g. could be after timeout) - False if no solution is found

solveAll(display: Expression | Sequence[Expression] | ndarray | Callable[[], None] | None = None, time_limit: float | None = None, solution_limit: int | None = None, call_from_model=False, **kwargs)[source]

Compute all solutions and optionally, display the solutions.

Parameters:
  • 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)

  • call_from_model – whether the method is called from a CPMpy Model instance or not

  • argument (any other keyword)

Returns: number of solutions found

solver_var(cpm_var)[source]

Creates solver variable for cpmpy variable or returns from cache if previously created or returns a constant if the variable is a constant

solver_vars(cpm_vars: Iterable[Expression | int | integer | bool]) list[Any]

Like solver_var() but for arbitrary shaped lists/tensors

status()
static supported()[source]

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

Returns:

Solver support by current system setup.

Return type:

[bool]

supported_global_constraints: frozenset[str] = frozenset({'mul'})
supported_reified_global_constraints: frozenset[str] = frozenset({})
transform(cpm_expr: Expression | bool | bool | Sequence[Expression | bool | bool | Sequence[NestedBoolExprLike] | ndarray] | ndarray) list[Expression][source]

Transform arbitrary CPMpy expressions to constraints the solver supports

Implemented through chaining multiple solver-independent transformation functions from the cpmpy/transformations/ directory.

See the Adding a new solver docs on readthedocs for more information.

Parameters:

cpm_expr (NestedBoolExprLike) – CPMpy expression, or list thereof

Returns:

transformed constraints

Return type:

list[Expression]

static version() str | None[source]

Returns the installed version of the solver’s Python API.