mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-07 18:08:51 -06:00
GurobiSolver: Let user decide whether to use callbacks or not
This commit is contained in:
@@ -304,8 +304,19 @@ class InternalSolver(ABC):
|
|||||||
|
|
||||||
|
|
||||||
class GurobiSolver(InternalSolver):
|
class GurobiSolver(InternalSolver):
|
||||||
def __init__(self):
|
def __init__(self,
|
||||||
|
use_lazy_callbacks=False):
|
||||||
|
"""
|
||||||
|
Creates a new GurobiSolver.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
use_lazy_callbacks: bool
|
||||||
|
If true, lazy constraints will be enforced via lazy callbacks.
|
||||||
|
Otherwise, they will be enforced via a simple solve-check loop.
|
||||||
|
"""
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self._use_lazy_callbacks = use_lazy_callbacks
|
||||||
self._pyomo_solver = pe.SolverFactory('gurobi_persistent')
|
self._pyomo_solver = pe.SolverFactory('gurobi_persistent')
|
||||||
self._pyomo_solver.options["Seed"] = randint(low=0, high=1000).rvs()
|
self._pyomo_solver.options["Seed"] = randint(low=0, high=1000).rvs()
|
||||||
|
|
||||||
@@ -319,6 +330,12 @@ class GurobiSolver(InternalSolver):
|
|||||||
self._pyomo_solver.options["MIPGap"] = gap_tolerance
|
self._pyomo_solver.options["MIPGap"] = gap_tolerance
|
||||||
|
|
||||||
def solve(self, tee=False):
|
def solve(self, tee=False):
|
||||||
|
if self._use_lazy_callbacks:
|
||||||
|
return self._solve_with_callbacks(tee)
|
||||||
|
else:
|
||||||
|
return super().solve(tee)
|
||||||
|
|
||||||
|
def _solve_with_callbacks(self, tee):
|
||||||
from gurobipy import GRB
|
from gurobipy import GRB
|
||||||
|
|
||||||
def cb(cb_model, cb_opt, cb_where):
|
def cb(cb_model, cb_opt, cb_where):
|
||||||
@@ -337,7 +354,6 @@ class GurobiSolver(InternalSolver):
|
|||||||
self._pyomo_solver.set_callback(cb)
|
self._pyomo_solver.set_callback(cb)
|
||||||
self.instance.found_violations = []
|
self.instance.found_violations = []
|
||||||
print(self._is_warm_start_available)
|
print(self._is_warm_start_available)
|
||||||
|
|
||||||
streams = [StringIO()]
|
streams = [StringIO()]
|
||||||
if tee:
|
if tee:
|
||||||
streams += [sys.stdout]
|
streams += [sys.stdout]
|
||||||
@@ -346,7 +362,6 @@ class GurobiSolver(InternalSolver):
|
|||||||
warmstart=self._is_warm_start_available)
|
warmstart=self._is_warm_start_available)
|
||||||
self._pyomo_solver.set_callback(None)
|
self._pyomo_solver.set_callback(None)
|
||||||
node_count = int(self._pyomo_solver._solver_model.getAttr("NodeCount"))
|
node_count = int(self._pyomo_solver._solver_model.getAttr("NodeCount"))
|
||||||
|
|
||||||
log = streams[0].getvalue()
|
log = streams[0].getvalue()
|
||||||
return {
|
return {
|
||||||
"Lower bound": results["Problem"][0]["Lower bound"],
|
"Lower bound": results["Problem"][0]["Lower bound"],
|
||||||
|
|||||||
Reference in New Issue
Block a user