CPMpy HiGHS interface (cpmpy.solvers.highs)

Interface to the HiGHS highspy Python API

HiGHS is a high-performance open-source linear/mixed-integer programming solver.

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

Installation

Install the Python bindings from PyPI:

$ pip install highspy

Detailed installation instructions available at: https://ergo-code.github.io/HiGHS/dev/interfaces/python/

The rest of this documentation is for advanced users.

List of classes

CPM_highs

Interface to HiGHS' Python API (highspy).

Module details

Supports FloatSum objectives.

class cpmpy.solvers.highs.CPM_highs(cpm_model=None, subsolver=None)[source]

Interface to HiGHS’ Python API (highspy).

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

  • highs: object, HiGHS Highs instance

  • _inf: numeric, HiGHS’ infinity constant (highspy.kHighsInf)

  • _obj_cols: Optional[npt.NDArray[np.int32]], columns with nonzero cost in the previous objective; None means no objective posted yet

Documentation of the solver’s own Python API: https://ergo-code.github.io/HiGHS/dev/interfaces/python/model-py/

add(cpm_expr: Expression | bool | bool | Sequence[Expression | bool | bool | Sequence[NestedBoolExprLike] | ndarray] | ndarray) CPM_highs[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.

Parameters:

cpm_expr (NestedBoolExprLike) – CPMpy expression, or list thereof

Returns:

self

get_core()

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.

maximize(expr: Expression | FloatSum) None[source]

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 | FloatSum) None[source]

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=[])

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 (HiGHS Highs instance).

objective(expr: Expression | FloatSum, minimize: bool = True) None[source]

Post the given expression to the solver as objective to minimize/maximize. Any constraints created during conversion are permanently posted.

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])

For warmstarting the solver with a variable assignment

Typically implemented in SAT-based solvers

Parameters:
  • cpm_vars – list of CPMpy variables

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

solve(time_limit=None, display: Expression | Sequence[Expression] | ndarray | Callable[[], None] | None = None, **kwargs)[source]

Call the HiGHS solver.

Arguments: - time_limit: maximum solve time in seconds (float, optional) - display: callback function to call after each solution is found

either a list of CPMpy expressions, OR a callback function which gets called after the variable-value mapping of the intermediate solution. default/None: nothing is displayed

  • kwargs: any keyword argument, mapped to HiGHS options via setOptionValue.

    Unknown/invalid options are ignored with a warning.

Notable HiGHS options:

  • threads (int): number of threads; default 0 means automatic (parallelism enabled according to HiGHS defaults).

HiGHS option reference: https://ergo-code.github.io/HiGHS/dev/options/definitions/

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)

Compute all solutions and optionally display the solutions.

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

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() bool[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({})
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.

Follows the ILP-style pipeline with linearize-friendly decompositions and treatment of reified variables.

Parameters:

cpm_expr (NestedBoolExprLike) – CPMpy expression, or list thereof

Returns:

transformed constraints

Return type:

list[Expression]

classmethod version() str | None[source]

Returns the installed version of the solver’s Python API (highspy).