mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 01:18:52 -06:00
Implement some constraint methods in Pyomo
This commit is contained in:
@@ -18,10 +18,6 @@ from miplearn.types import (
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class ExtractedConstraint(ABC):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class Constraint:
|
class Constraint:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -270,12 +270,6 @@ class BasePyomoSolver(InternalSolver):
|
|||||||
def _get_node_count_regexp(self) -> Optional[str]:
|
def _get_node_count_regexp(self) -> Optional[str]:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def extract_constraint(self, cid):
|
|
||||||
raise Exception("Not implemented")
|
|
||||||
|
|
||||||
def is_constraint_satisfied(self, cobj):
|
|
||||||
raise Exception("Not implemented")
|
|
||||||
|
|
||||||
def relax(self) -> None:
|
def relax(self) -> None:
|
||||||
for var in self._bin_vars:
|
for var in self._bin_vars:
|
||||||
lb, ub = var.bounds
|
lb, ub = var.bounds
|
||||||
@@ -285,15 +279,35 @@ class BasePyomoSolver(InternalSolver):
|
|||||||
self._pyomo_solver.update_var(var)
|
self._pyomo_solver.update_var(var)
|
||||||
|
|
||||||
def get_inequality_slacks(self) -> Dict[str, float]:
|
def get_inequality_slacks(self) -> Dict[str, float]:
|
||||||
raise Exception("not implemented")
|
result: Dict[str, float] = {}
|
||||||
|
for (cname, cobj) in self._cname_to_constr.items():
|
||||||
|
if cobj.equality:
|
||||||
|
continue
|
||||||
|
result[cname] = cobj.slack()
|
||||||
|
return result
|
||||||
|
|
||||||
def set_constraint_sense(self, cid, sense):
|
def get_constraint_sense(self, cid: str) -> str:
|
||||||
|
cobj = self._cname_to_constr[cid]
|
||||||
|
has_ub = cobj.has_ub()
|
||||||
|
has_lb = cobj.has_lb()
|
||||||
|
assert (not has_lb) or (not has_ub), "range constraints not supported"
|
||||||
|
if has_lb:
|
||||||
|
return ">"
|
||||||
|
elif has_ub:
|
||||||
|
return "<"
|
||||||
|
else:
|
||||||
|
return "="
|
||||||
|
|
||||||
|
def set_constraint_sense(self, cid: str, sense: str) -> None:
|
||||||
raise Exception("Not implemented")
|
raise Exception("Not implemented")
|
||||||
|
|
||||||
def get_constraint_sense(self, cid):
|
def extract_constraint(self, cid: str) -> Constraint:
|
||||||
raise Exception("Not implemented")
|
raise Exception("Not implemented")
|
||||||
|
|
||||||
def set_constraint_rhs(self, cid, rhs):
|
def is_constraint_satisfied(self, cobj: Constraint) -> bool:
|
||||||
|
raise Exception("Not implemented")
|
||||||
|
|
||||||
|
def set_constraint_rhs(self, cid: str, rhs: float) -> None:
|
||||||
raise Exception("Not implemented")
|
raise Exception("Not implemented")
|
||||||
|
|
||||||
def is_infeasible(self) -> bool:
|
def is_infeasible(self) -> bool:
|
||||||
@@ -302,5 +316,5 @@ class BasePyomoSolver(InternalSolver):
|
|||||||
def get_dual(self, cid):
|
def get_dual(self, cid):
|
||||||
raise Exception("Not implemented")
|
raise Exception("Not implemented")
|
||||||
|
|
||||||
def get_sense(self):
|
def get_sense(self) -> str:
|
||||||
raise Exception("Not implemented")
|
return self._obj_sense
|
||||||
|
|||||||
@@ -133,8 +133,6 @@ def test_internal_solver():
|
|||||||
stats = solver.solve()
|
stats = solver.solve()
|
||||||
assert stats["Lower bound"] == 1030.0
|
assert stats["Lower bound"] == 1030.0
|
||||||
|
|
||||||
if isinstance(solver, GurobiSolver):
|
|
||||||
|
|
||||||
assert solver.get_sense() == "max"
|
assert solver.get_sense() == "max"
|
||||||
assert solver.get_constraint_sense("cut") == "<"
|
assert solver.get_constraint_sense("cut") == "<"
|
||||||
assert solver.get_constraint_sense("eq_capacity") == "<"
|
assert solver.get_constraint_sense("eq_capacity") == "<"
|
||||||
@@ -145,6 +143,7 @@ def test_internal_solver():
|
|||||||
"eq_capacity": 3.0,
|
"eq_capacity": 3.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isinstance(solver, GurobiSolver):
|
||||||
# Extract the new constraint
|
# Extract the new constraint
|
||||||
cobj = solver.extract_constraint("cut")
|
cobj = solver.extract_constraint("cut")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user