pyepo.model.opt

Abstract optimization model

Classes

ModelSpec

Serializable recipe for building a fresh optimization model.

optModel

Abstract base class for predict-then-optimize models.

Module Contents

class pyepo.model.opt.ModelSpec(model_type: type[optModel], config: dict, args: tuple = ())

Serializable recipe for building a fresh optimization model.

model_type: type[optModel]
property args: tuple

Return an independent copy of positional constructor arguments.

property config: dict

Return an independent copy of keyword constructor arguments.

build() optModel

Build a fresh model without sharing mutable configuration values.

class pyepo.model.opt.optModel

Bases: abc.ABC

Abstract base class for predict-then-optimize models.

Subclasses wrap an optimization solver or algorithm with a unified _getModel / setObj / solve / num_cost interface that pyepo.func modules call during training. Concrete backends are provided for GurobiPy (optGrbModel), Pyomo (optOmoModel), COPT (optCoptModel), OR-Tools (optOrtModel / optOrtCpModel), and MPAX (optMpaxModel); subclass optModel directly to integrate any other solver or algorithm.

The default objective sense is minimization; set self.modelSense = EPO.MAXIMIZE in _getModel or __init__ for maximization problems (some backends, e.g. Gurobi and COPT, detect this automatically from the underlying solver model).

Variables:
  • _model (optimization model) – underlying solver model object

  • modelSense (ModelSense) – EPO.MINIMIZE (default) or EPO.MAXIMIZE

modelSense: pyepo.EPO.ModelSense
arcs: list
get_config() dict

Return the constructor configuration for this model.

classmethod from_config(config: dict, args: tuple = ()) Self

Build a model from a configuration produced by get_config.

to_spec() ModelSpec

Return a serializable, immutable-snapshot rebuild recipe.

rebuild() Self

Build a structurally equivalent model with clean runtime state.

property num_cost: int

number of costs to be predicted

abstractmethod setObj(c: numpy.ndarray | torch.Tensor | list) None

An abstract method to set the objective function

Parameters:

c – cost of objective function

abstractmethod solve() tuple[numpy.ndarray | torch.Tensor | list, float]

An abstract method to solve the model

Returns:

optimal solution (list) and objective value (float)

Return type:

tuple

property c_pred_index: numpy.ndarray | None

Variable positions the predicted cost lands on, or None when every variable is predicted (the default).

copy() Self

A method to copy the model

Returns:

new copied model

Return type:

optModel

abstractmethod addConstr(coefs: numpy.ndarray | torch.Tensor | list, rhs: float) Self

A method to add a new constraint. Subclasses should override.

Parameters:
  • coefs – coefficients of the new constraint

  • rhs – right-hand side of new constraint

Returns:

new model with the added constraint

Return type:

optModel

abstractmethod relax() optModel

A method to relax the MIP model. Subclasses should override.