mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 01:18:52 -06:00
InternalSolver: set_constraint_sense, set_constraint_rhs
This commit is contained in:
@@ -274,6 +274,14 @@ class GurobiSolver(InternalSolver):
|
||||
def get_constraint_slacks(self):
|
||||
return {c.ConstrName: c.Slack for c in self.model.getConstrs()}
|
||||
|
||||
def set_constraint_sense(self, cid, sense):
|
||||
c = self.model.getConstrByName(cid)
|
||||
c.Sense = sense
|
||||
|
||||
def set_constraint_rhs(self, cid, rhs):
|
||||
c = self.model.getConstrByName(cid)
|
||||
c.RHS = rhs
|
||||
|
||||
def relax(self):
|
||||
self.model = self.model.relax()
|
||||
self._update_vars()
|
||||
|
||||
@@ -195,6 +195,14 @@ class InternalSolver(ABC):
|
||||
def is_constraint_satisfied(self, cobj):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def set_constraint_sense(self, cid, sense):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def set_constraint_rhs(self, cid, rhs):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def set_threads(self, threads):
|
||||
pass
|
||||
|
||||
@@ -219,12 +219,6 @@ class BasePyomoSolver(InternalSolver):
|
||||
def get_constraint_ids(self):
|
||||
return list(self._cname_to_constr.keys())
|
||||
|
||||
def extract_constraint(self, cid):
|
||||
raise Exception("Not implemented")
|
||||
|
||||
def is_constraint_satisfied(self, cobj):
|
||||
raise Exception("Not implemented")
|
||||
|
||||
@abstractmethod
|
||||
def _get_warm_start_regexp(self):
|
||||
pass
|
||||
@@ -249,8 +243,20 @@ class BasePyomoSolver(InternalSolver):
|
||||
def _get_gap_tolerance_option_name(self):
|
||||
pass
|
||||
|
||||
def extract_constraint(self, cid):
|
||||
raise Exception("Not implemented")
|
||||
|
||||
def is_constraint_satisfied(self, cobj):
|
||||
raise Exception("Not implemented")
|
||||
|
||||
def relax(self):
|
||||
raise Exception("not implemented")
|
||||
|
||||
def get_constraint_slacks(self):
|
||||
raise Exception("not implemented")
|
||||
|
||||
def set_constraint_sense(self, cid, sense):
|
||||
raise Exception("Not implemented")
|
||||
|
||||
def set_constraint_rhs(self, cid, rhs):
|
||||
raise Exception("Not implemented")
|
||||
|
||||
@@ -122,7 +122,7 @@ def test_internal_solver():
|
||||
assert stats["Lower bound"] == 1030.0
|
||||
|
||||
if isinstance(solver, GurobiSolver):
|
||||
# Extract new constraint
|
||||
# Extract the new constraint
|
||||
cobj = solver.extract_constraint("cut")
|
||||
|
||||
# New constraint should no longer affect solution and should no longer
|
||||
@@ -145,6 +145,13 @@ def test_internal_solver():
|
||||
# New constraint should now be satisfied
|
||||
assert solver.is_constraint_satisfied(cobj)
|
||||
|
||||
# Relax problem and make cut into an equality constraint
|
||||
solver.relax()
|
||||
solver.set_constraint_rhs("cut", 0.5)
|
||||
solver.set_constraint_sense("cut", "=")
|
||||
stats = solver.solve()
|
||||
assert round(stats["Lower bound"]) == 1179.0
|
||||
|
||||
|
||||
def test_iteration_cb():
|
||||
for solver_class in _get_internal_solvers():
|
||||
|
||||
Reference in New Issue
Block a user