mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Temporarily remove native solver callbacks; add iteration_cb
This commit is contained in:
@@ -9,7 +9,7 @@ import pyomo
|
||||
from abc import abstractmethod
|
||||
from io import StringIO
|
||||
from pyomo import environ as pe
|
||||
from pyomo.core import Var
|
||||
from pyomo.core import Var, Constraint
|
||||
|
||||
from .. import RedirectOutput
|
||||
from ..internal import InternalSolver
|
||||
@@ -32,6 +32,7 @@ class BasePyomoSolver(InternalSolver):
|
||||
self._pyomo_solver = None
|
||||
self._obj_sense = None
|
||||
self._varname_to_var = {}
|
||||
self._cname_to_constr = {}
|
||||
|
||||
def solve_lp(self, tee=False):
|
||||
for var in self._bin_vars:
|
||||
@@ -93,23 +94,31 @@ class BasePyomoSolver(InternalSolver):
|
||||
self.instance = instance
|
||||
self.model = model
|
||||
self._pyomo_solver.set_instance(model)
|
||||
self._update_obj()
|
||||
self._update_vars()
|
||||
self._update_constrs()
|
||||
|
||||
# Update objective sense
|
||||
def _update_obj(self):
|
||||
self._obj_sense = "max"
|
||||
if self._pyomo_solver._objective.sense == pyomo.core.kernel.objective.minimize:
|
||||
self._obj_sense = "min"
|
||||
|
||||
# Update variables
|
||||
def _update_vars(self):
|
||||
self._all_vars = []
|
||||
self._bin_vars = []
|
||||
self._varname_to_var = {}
|
||||
for var in model.component_objects(Var):
|
||||
for var in self.model.component_objects(Var):
|
||||
self._varname_to_var[var.name] = var
|
||||
for idx in var:
|
||||
self._all_vars += [var[idx]]
|
||||
if var[idx].domain == pyomo.core.base.set_types.Binary:
|
||||
self._bin_vars += [var[idx]]
|
||||
|
||||
def _update_constrs(self):
|
||||
self._cname_to_constr = {}
|
||||
for constr in self.model.component_objects(Constraint):
|
||||
self._cname_to_constr[constr.name] = constr
|
||||
|
||||
def fix(self, solution):
|
||||
count_total, count_fixed = 0, 0
|
||||
for varname in solution:
|
||||
@@ -126,12 +135,15 @@ class BasePyomoSolver(InternalSolver):
|
||||
|
||||
def add_constraint(self, constraint):
|
||||
self._pyomo_solver.add_constraint(constraint)
|
||||
self._update_constrs()
|
||||
|
||||
def solve(self, tee=False):
|
||||
def solve(self, tee=False, iteration_cb=None):
|
||||
total_wallclock_time = 0
|
||||
streams = [StringIO()]
|
||||
if tee:
|
||||
streams += [sys.stdout]
|
||||
if iteration_cb is None:
|
||||
iteration_cb = lambda: False
|
||||
self.instance.found_violated_lazy_constraints = []
|
||||
self.instance.found_violated_user_cuts = []
|
||||
while True:
|
||||
@@ -140,16 +152,9 @@ class BasePyomoSolver(InternalSolver):
|
||||
results = self._pyomo_solver.solve(tee=True,
|
||||
warmstart=self._is_warm_start_available)
|
||||
total_wallclock_time += results["Solver"][0]["Wallclock time"]
|
||||
logger.debug("Finding violated constraints...")
|
||||
violations = self.instance.find_violated_lazy_constraints(self.model)
|
||||
if len(violations) == 0:
|
||||
should_repeat = iteration_cb()
|
||||
if not should_repeat:
|
||||
break
|
||||
self.instance.found_violated_lazy_constraints += violations
|
||||
logger.debug(" %d violations found" % len(violations))
|
||||
for v in violations:
|
||||
cut = self.instance.build_lazy_constraint(self.model, v)
|
||||
self.add_constraint(cut)
|
||||
|
||||
log = streams[0].getvalue()
|
||||
return {
|
||||
"Lower bound": results["Problem"][0]["Lower bound"],
|
||||
@@ -198,6 +203,15 @@ class BasePyomoSolver(InternalSolver):
|
||||
key = self._get_gap_tolerance_option_name()
|
||||
self._pyomo_solver.options[key] = gap_tolerance
|
||||
|
||||
def get_constraints_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
|
||||
|
||||
@@ -16,89 +16,23 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
class GurobiPyomoSolver(BasePyomoSolver):
|
||||
def __init__(self,
|
||||
use_lazy_callbacks=True,
|
||||
options=None):
|
||||
"""
|
||||
Creates a new Gurobi solver, accessed through Pyomo.
|
||||
|
||||
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.
|
||||
options: dict
|
||||
Dictionary of options to pass to the Pyomo solver. For example,
|
||||
{"Threads": 4} to set the number of threads.
|
||||
"""
|
||||
super().__init__()
|
||||
self._use_lazy_callbacks = use_lazy_callbacks
|
||||
self._pyomo_solver = pe.SolverFactory('gurobi_persistent')
|
||||
self._pyomo_solver.options["Seed"] = randint(low=0, high=1000).rvs()
|
||||
if options is not None:
|
||||
for (key, value) in options.items():
|
||||
self._pyomo_solver.options[key] = value
|
||||
|
||||
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
|
||||
|
||||
def cb(cb_model, cb_opt, cb_where):
|
||||
try:
|
||||
# User cuts
|
||||
if cb_where == GRB.Callback.MIPNODE:
|
||||
logger.debug("Finding violated cutting planes...")
|
||||
cb_opt.cbGetNodeRel(self._all_vars)
|
||||
violations = self.instance.find_violated_user_cuts(cb_model)
|
||||
self.instance.found_violated_user_cuts += violations
|
||||
logger.debug(" %d found" % len(violations))
|
||||
for v in violations:
|
||||
cut = self.instance.build_user_cut(cb_model, v)
|
||||
cb_opt.cbCut(cut)
|
||||
|
||||
# Lazy constraints
|
||||
if cb_where == GRB.Callback.MIPSOL:
|
||||
cb_opt.cbGetSolution(self._all_vars)
|
||||
logger.debug("Finding violated lazy constraints...")
|
||||
violations = self.instance.find_violated_lazy_constraints(cb_model)
|
||||
self.instance.found_violated_lazy_constraints += violations
|
||||
logger.debug(" %d found" % len(violations))
|
||||
for v in violations:
|
||||
cut = self.instance.build_lazy_constraint(cb_model, v)
|
||||
cb_opt.cbLazy(cut)
|
||||
except Exception as e:
|
||||
logger.error(e)
|
||||
|
||||
self._pyomo_solver.options["LazyConstraints"] = 1
|
||||
self._pyomo_solver.options["PreCrush"] = 1
|
||||
self._pyomo_solver.set_callback(cb)
|
||||
|
||||
self.instance.found_violated_lazy_constraints = []
|
||||
self.instance.found_violated_user_cuts = []
|
||||
|
||||
streams = [StringIO()]
|
||||
if tee:
|
||||
streams += [sys.stdout]
|
||||
with RedirectOutput(streams):
|
||||
results = self._pyomo_solver.solve(tee=True,
|
||||
warmstart=self._is_warm_start_available)
|
||||
|
||||
self._pyomo_solver.set_callback(None)
|
||||
log = streams[0].getvalue()
|
||||
return {
|
||||
"Lower bound": results["Problem"][0]["Lower bound"],
|
||||
"Upper bound": results["Problem"][0]["Upper bound"],
|
||||
"Wallclock time": results["Solver"][0]["Wallclock time"],
|
||||
"Nodes": self._extract_node_count(log),
|
||||
"Sense": self._obj_sense,
|
||||
"Log": log,
|
||||
"Warm start value": self._extract_warm_start_value(log),
|
||||
}
|
||||
|
||||
def _extract_node_count(self, log):
|
||||
return max(1, int(self._pyomo_solver._solver_model.getAttr("NodeCount")))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user