pyepo.model.bases¶
Problem-level base classes for optimization models.
Each base captures the problem-specific state (data, indices, derived attrs)
and methods that are independent of the underlying solver. Concrete classes
combine a problem base with a backend base via multiple inheritance, with the
problem base listed first so its __init__ runs before the backend’s:
class shortestPathModel(shortestPathBase, optGrbModel):
def _getModel(self):
... # solver-specific build
Classes¶
Problem-level base for grid shortest path. |
|
Problem-level base for the multi-dimensional knapsack. |
|
Problem-level base for Markowitz mean-variance portfolio optimization. |
|
Problem-level base for the symmetric traveling-salesperson problem. |
Module Contents¶
- class pyepo.model.bases.shortestPathBase(grid: tuple[int, int], *args, **kwargs)¶
Bases:
pyepo.model.opt.optModelProblem-level base for grid shortest path.
Finds the minimum-cost path from the northwest corner to the southeast corner of an
(h, w)grid network. The problem is formulated as a minimum-cost flow LP with arc-incidence equality constraints; arcs are enumerated automatically from the grid dimensions and stored onself.arcs.- Variables:
- grid¶
- arcs = []¶
- class pyepo.model.bases.knapsackBase(weights: numpy.ndarray | list, capacity: numpy.ndarray | list, *args, **kwargs)¶
Bases:
pyepo.model.opt.optModelProblem-level base for the multi-dimensional knapsack.
Selects a subset of items that maximizes total value subject to per-resource capacity constraints. Items, dimensions, and capacities are inferred from the shapes of
weightsandcapacity:weightshas shape(dim, n_items)andcapacityhas lengthdim. Item values are the predicted cost coefficientscset viasetObj.- Variables:
weights (np.ndarray) – item weights, shape
(dim, n_items)capacity (np.ndarray) – per-dimension capacity, shape
(dim,)items (list) – item indices
0 .. n_items - 1
- modelSense¶
- weights¶
- capacity¶
- items¶
- class pyepo.model.bases.portfolioBase(num_assets: int, covariance: numpy.ndarray, gamma: float = 2.25, *args, **kwargs)¶
Bases:
pyepo.model.opt.optModelProblem-level base for Markowitz mean-variance portfolio optimization.
Allocates a unit budget across
num_assetsto maximize predicted expected return subject to a quadratic risk budget \(\mathbf{x}^\top \boldsymbol{\Sigma} \mathbf{x} \le \gamma\, \overline{\boldsymbol{\Sigma}}\), where \(\overline{\boldsymbol{\Sigma}}\) is the mean covariance entry. Predicted asset returns are the cost coefficientscset viasetObj; the covariance and risk budget are fixed across instances.- Variables:
- modelSense¶
- num_assets¶
- covariance¶
- risk_level¶
- class pyepo.model.bases.tspABBase(num_nodes: int, *args, **kwargs)¶
Bases:
pyepo.model.opt.optModelProblem-level base for the symmetric traveling-salesperson problem.
Finds the shortest tour visiting each of
num_nodescities exactly once and returning to the origin. Three concrete ILP formulations are supplied per backend:DFJ (Dantzig-Fulkerson-Johnson) – lazy subtour-elimination constraints via solver callbacks (no LP relaxation).
GG (Gavish-Graves) – flow-based formulation with auxiliary flow variables.
MTZ (Miller-Tucker-Zemlin) – compact formulation with per-node potential auxiliaries.
The base only manages formulation-independent state (nodes, edges, extra-constraint replay) and the
getTourhelper. Concrete formulations implement_getModel(build the solver model) and_addExtraConstr(add a single linear extra constraint).- Variables:
- num_nodes¶
- nodes¶
- edges¶
- getTour(sol: numpy.ndarray | torch.Tensor | list) list[int]¶
Reconstruct a TSP tour from an undirected edge-selection vector.
- Raises:
ValueError – if the solution does not form a single connected tour.
- copy() Self¶
Return a fresh model with all extra constraints replayed onto it.
- addConstr(coefs: numpy.ndarray | torch.Tensor | list, rhs: float) Self¶
Return a new model with one extra linear constraint added.
- Parameters:
coefs – per-edge coefficients aligned with
self.edgesrhs – right-hand side