Add first model feature (constraint RHS)

This commit is contained in:
2021-03-02 17:21:05 -06:00
parent 31ca45036a
commit 1397937f03
17 changed files with 167 additions and 47 deletions

View File

@@ -335,6 +335,10 @@ class GurobiSolver(InternalSolver):
self.model.update()
return [c.ConstrName for c in self.model.getConstrs()]
def get_constraint_rhs(self, cid: str) -> float:
assert self.model is not None
return self.model.getConstrByName(cid).rhs
def extract_constraint(self, cid):
self._raise_if_callback()
constr = self.model.getConstrByName(cid)

View File

@@ -155,6 +155,13 @@ class InternalSolver(ABC):
"""
pass
@abstractmethod
def get_constraint_rhs(self, cid: str) -> float:
"""
Returns the right-hand side of a given constraint.
"""
pass
@abstractmethod
def add_constraint(self, cobj: Constraint) -> None:
"""

View File

@@ -16,11 +16,12 @@ from miplearn.components.cuts import UserCutsComponent
from miplearn.components.lazy_dynamic import DynamicLazyConstraintsComponent
from miplearn.components.objective import ObjectiveValueComponent
from miplearn.components.primal import PrimalSolutionComponent
from miplearn.features import ModelFeaturesExtractor
from miplearn.instance import Instance
from miplearn.solvers import _RedirectOutput
from miplearn.solvers.internal import InternalSolver
from miplearn.solvers.pyomo.gurobi import GurobiPyomoSolver
from miplearn.types import MIPSolveStats, TrainingSample, LearningSolveStats
from miplearn.types import TrainingSample, LearningSolveStats
logger = logging.getLogger(__name__)
@@ -164,6 +165,10 @@ class LearningSolver:
assert isinstance(self.internal_solver, InternalSolver)
self.internal_solver.set_instance(instance, model)
# Extract model features
extractor = ModelFeaturesExtractor(self.internal_solver)
instance.model_features = extractor.extract()
# Solve linear relaxation
if self.solve_lp_first:
logger.info("Solving LP relaxation...")

View File

@@ -212,7 +212,11 @@ class BasePyomoSolver(InternalSolver):
assert self.model is not None
self._cname_to_constr = {}
for constr in self.model.component_objects(Constraint):
self._cname_to_constr[constr.name] = constr
if isinstance(constr, pe.ConstraintList):
for idx in constr:
self._cname_to_constr[f"{constr.name}[{idx}]"] = constr[idx]
else:
self._cname_to_constr[constr.name] = constr
def fix(self, solution):
count_total, count_fixed = 0, 0
@@ -302,6 +306,13 @@ class BasePyomoSolver(InternalSolver):
else:
return "="
def get_constraint_rhs(self, cid: str) -> float:
cobj = self._cname_to_constr[cid]
if cobj.has_ub:
return cobj.upper()
else:
return cobj.lower()
def set_constraint_sense(self, cid: str, sense: str) -> None:
raise Exception("Not implemented")