mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Only include static features in after-load
This commit is contained in:
@@ -84,6 +84,7 @@ class GurobiSolver(InternalSolver):
|
||||
@overrides
|
||||
def add_constraint(self, constr: Constraint, name: str) -> None:
|
||||
assert self.model is not None
|
||||
assert constr.lhs is not None
|
||||
lhs = self.gp.quicksum(
|
||||
self._varname_to_var[varname] * coeff
|
||||
for (varname, coeff) in constr.lhs.items()
|
||||
@@ -153,23 +154,36 @@ class GurobiSolver(InternalSolver):
|
||||
]
|
||||
|
||||
@overrides
|
||||
def get_constraints(self) -> Dict[str, Constraint]:
|
||||
def get_constraints(self, with_static: bool = True) -> Dict[str, Constraint]:
|
||||
model = self.model
|
||||
assert model is not None
|
||||
self._raise_if_callback()
|
||||
if self._dirty:
|
||||
model.update()
|
||||
self._dirty = False
|
||||
|
||||
gp_constrs = model.getConstrs()
|
||||
var_names = model.getAttr("varName", model.getVars())
|
||||
constr_names = model.getAttr("constrName", gp_constrs)
|
||||
rhs = model.getAttr("rhs", gp_constrs)
|
||||
sense = model.getAttr("sense", gp_constrs)
|
||||
lhs: Optional[List[Dict]] = None
|
||||
rhs = None
|
||||
sense = None
|
||||
dual_value = None
|
||||
sa_rhs_up = None
|
||||
sa_rhs_down = None
|
||||
slack = None
|
||||
basis_status = None
|
||||
|
||||
if with_static:
|
||||
var_names = model.getAttr("varName", model.getVars())
|
||||
rhs = model.getAttr("rhs", gp_constrs)
|
||||
sense = model.getAttr("sense", gp_constrs)
|
||||
lhs = []
|
||||
for (i, gp_constr) in enumerate(gp_constrs):
|
||||
expr = model.getRow(gp_constr)
|
||||
lhsi = {}
|
||||
for j in range(expr.size()):
|
||||
lhsi[var_names[expr.getVar(j).index]] = expr.getCoeff(j)
|
||||
lhs.append(lhsi)
|
||||
if self._has_lp_solution:
|
||||
dual_value = model.getAttr("pi", gp_constrs)
|
||||
sa_rhs_up = model.getAttr("saRhsUp", gp_constrs)
|
||||
@@ -177,16 +191,20 @@ class GurobiSolver(InternalSolver):
|
||||
basis_status = model.getAttr("cbasis", gp_constrs)
|
||||
if self._has_lp_solution or self._has_mip_solution:
|
||||
slack = model.getAttr("slack", gp_constrs)
|
||||
|
||||
constraints: Dict[str, Constraint] = {}
|
||||
for (i, gp_constr) in enumerate(gp_constrs):
|
||||
expr = model.getRow(gp_constr)
|
||||
lhs = {}
|
||||
for j in range(expr.size()):
|
||||
lhs[var_names[expr.getVar(j).index]] = expr.getCoeff(j)
|
||||
assert (
|
||||
constr_names[i] not in constraints
|
||||
), f"Duplicated constraint name detected: {constr_names[i]}"
|
||||
constraint = Constraint(lhs=lhs, rhs=rhs[i], sense=sense[i])
|
||||
constraint = Constraint()
|
||||
if with_static:
|
||||
assert lhs is not None
|
||||
assert rhs is not None
|
||||
assert sense is not None
|
||||
constraint.lhs = lhs[i]
|
||||
constraint.rhs = rhs[i]
|
||||
constraint.sense = sense[i]
|
||||
if dual_value is not None:
|
||||
assert sa_rhs_up is not None
|
||||
assert sa_rhs_down is not None
|
||||
@@ -247,13 +265,10 @@ class GurobiSolver(InternalSolver):
|
||||
]
|
||||
|
||||
@overrides
|
||||
def get_variables(self) -> Dict[str, Variable]:
|
||||
def get_variables(self, with_static: bool = True) -> Dict[str, Variable]:
|
||||
assert self.model is not None
|
||||
variables = {}
|
||||
gp_vars = self.model.getVars()
|
||||
lb = self.model.getAttr("lb", gp_vars)
|
||||
ub = self.model.getAttr("ub", gp_vars)
|
||||
obj_coeff = self.model.getAttr("obj", gp_vars)
|
||||
names = self.model.getAttr("varName", gp_vars)
|
||||
values = None
|
||||
rc = None
|
||||
@@ -264,6 +279,13 @@ class GurobiSolver(InternalSolver):
|
||||
sa_lb_up = None
|
||||
sa_lb_down = None
|
||||
vbasis = None
|
||||
ub = None
|
||||
lb = None
|
||||
obj_coeff = None
|
||||
if with_static:
|
||||
lb = self.model.getAttr("lb", gp_vars)
|
||||
ub = self.model.getAttr("ub", gp_vars)
|
||||
obj_coeff = self.model.getAttr("obj", gp_vars)
|
||||
if self.model.solCount > 0:
|
||||
values = self.model.getAttr("x", gp_vars)
|
||||
if self._has_lp_solution:
|
||||
@@ -281,12 +303,15 @@ class GurobiSolver(InternalSolver):
|
||||
assert (
|
||||
names[i] not in variables
|
||||
), f"Duplicated variable name detected: {names[i]}"
|
||||
var = Variable(
|
||||
lower_bound=lb[i],
|
||||
upper_bound=ub[i],
|
||||
obj_coeff=obj_coeff[i],
|
||||
type=self._original_vtype[gp_var],
|
||||
)
|
||||
var = Variable()
|
||||
if with_static:
|
||||
assert lb is not None
|
||||
assert ub is not None
|
||||
assert obj_coeff is not None
|
||||
var.lower_bound = lb[i]
|
||||
var.upper_bound = ub[i]
|
||||
var.obj_coeff = obj_coeff[i]
|
||||
var.type = self._original_vtype[gp_var]
|
||||
if values is not None:
|
||||
var.value = values[i]
|
||||
if rc is not None:
|
||||
@@ -319,6 +344,7 @@ class GurobiSolver(InternalSolver):
|
||||
|
||||
@overrides
|
||||
def is_constraint_satisfied(self, constr: Constraint, tol: float = 1e-6) -> bool:
|
||||
assert constr.lhs is not None
|
||||
lhs = 0.0
|
||||
for (varname, coeff) in constr.lhs.items():
|
||||
var = self._varname_to_var[varname]
|
||||
|
||||
@@ -170,7 +170,7 @@ class InternalSolver(ABC, EnforceOverrides):
|
||||
raise NotImplementedError()
|
||||
|
||||
@abstractmethod
|
||||
def get_constraints(self) -> Dict[str, Constraint]:
|
||||
def get_constraints(self, with_static: bool = True) -> Dict[str, Constraint]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
@@ -237,7 +237,7 @@ class InternalSolver(ABC, EnforceOverrides):
|
||||
return False
|
||||
|
||||
@abstractmethod
|
||||
def get_variables(self) -> Dict[str, Variable]:
|
||||
def get_variables(self, with_static: bool = True) -> Dict[str, Variable]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
|
||||
@@ -187,7 +187,10 @@ class LearningSolver:
|
||||
# Extract features (after-lp)
|
||||
# -------------------------------------------------------
|
||||
logger.info("Extracting features (after-lp)...")
|
||||
features = FeaturesExtractor(self.internal_solver).extract(instance)
|
||||
features = FeaturesExtractor(self.internal_solver).extract(
|
||||
instance,
|
||||
with_static=False,
|
||||
)
|
||||
features.extra = {}
|
||||
features.lp_solve = lp_stats
|
||||
sample.after_lp = features
|
||||
@@ -249,7 +252,10 @@ class LearningSolver:
|
||||
# Extract features (after-mip)
|
||||
# -------------------------------------------------------
|
||||
logger.info("Extracting features (after-mip)...")
|
||||
features = FeaturesExtractor(self.internal_solver).extract(instance)
|
||||
features = FeaturesExtractor(self.internal_solver).extract(
|
||||
instance,
|
||||
with_static=False,
|
||||
)
|
||||
features.mip_solve = mip_stats
|
||||
features.extra = {}
|
||||
sample.after_mip = features
|
||||
|
||||
@@ -77,6 +77,7 @@ class BasePyomoSolver(InternalSolver):
|
||||
) -> None:
|
||||
assert self.model is not None
|
||||
if isinstance(constr, Constraint):
|
||||
assert constr.lhs is not None
|
||||
lhs = 0.0
|
||||
for (varname, coeff) in constr.lhs.items():
|
||||
var = self._varname_to_var[varname]
|
||||
@@ -127,7 +128,7 @@ class BasePyomoSolver(InternalSolver):
|
||||
self._pyomo_solver.update_var(var)
|
||||
|
||||
@overrides
|
||||
def get_constraints(self) -> Dict[str, Constraint]:
|
||||
def get_constraints(self, with_static: bool = True) -> Dict[str, Constraint]:
|
||||
assert self.model is not None
|
||||
|
||||
constraints = {}
|
||||
@@ -136,11 +137,17 @@ class BasePyomoSolver(InternalSolver):
|
||||
for idx in constr:
|
||||
name = f"{constr.name}[{idx}]"
|
||||
assert name not in constraints
|
||||
constraints[name] = self._parse_pyomo_constraint(constr[idx])
|
||||
constraints[name] = self._parse_pyomo_constraint(
|
||||
constr[idx],
|
||||
with_static=with_static,
|
||||
)
|
||||
else:
|
||||
name = constr.name
|
||||
assert name not in constraints
|
||||
constraints[name] = self._parse_pyomo_constraint(constr)
|
||||
constraints[name] = self._parse_pyomo_constraint(
|
||||
constr,
|
||||
with_static=with_static,
|
||||
)
|
||||
|
||||
return constraints
|
||||
|
||||
@@ -169,7 +176,7 @@ class BasePyomoSolver(InternalSolver):
|
||||
return solution
|
||||
|
||||
@overrides
|
||||
def get_variables(self) -> Dict[str, Variable]:
|
||||
def get_variables(self, with_static: bool = True) -> Dict[str, Variable]:
|
||||
assert self.model is not None
|
||||
variables = {}
|
||||
for var in self.model.component_objects(pyomo.core.Var):
|
||||
@@ -177,7 +184,10 @@ class BasePyomoSolver(InternalSolver):
|
||||
varname = f"{var}[{idx}]"
|
||||
if idx is None:
|
||||
varname = str(var)
|
||||
variables[varname] = self._parse_pyomo_variable(var[idx])
|
||||
variables[varname] = self._parse_pyomo_variable(
|
||||
var[idx],
|
||||
with_static=with_static,
|
||||
)
|
||||
return variables
|
||||
|
||||
@overrides
|
||||
@@ -201,6 +211,7 @@ class BasePyomoSolver(InternalSolver):
|
||||
@overrides
|
||||
def is_constraint_satisfied(self, constr: Constraint, tol: float = 1e-6) -> bool:
|
||||
lhs = 0.0
|
||||
assert constr.lhs is not None
|
||||
for (varname, coeff) in constr.lhs.items():
|
||||
var = self._varname_to_var[varname]
|
||||
lhs += var.value * coeff
|
||||
@@ -378,71 +389,78 @@ class BasePyomoSolver(InternalSolver):
|
||||
def _get_warm_start_regexp(self) -> Optional[str]:
|
||||
return None
|
||||
|
||||
def _parse_pyomo_variable(self, var: pyomo.core.Var) -> Variable:
|
||||
def _parse_pyomo_variable(
|
||||
self,
|
||||
pyomo_var: pyomo.core.Var,
|
||||
with_static: bool = True,
|
||||
) -> Variable:
|
||||
assert self.model is not None
|
||||
variable = Variable()
|
||||
|
||||
# Variable type
|
||||
vtype: Optional[str] = None
|
||||
if var.domain == pyomo.core.Binary:
|
||||
vtype = "B"
|
||||
elif var.domain in [
|
||||
pyomo.core.Reals,
|
||||
pyomo.core.NonNegativeReals,
|
||||
pyomo.core.NonPositiveReals,
|
||||
pyomo.core.NegativeReals,
|
||||
pyomo.core.PositiveReals,
|
||||
]:
|
||||
vtype = "C"
|
||||
if vtype is None:
|
||||
raise Exception(f"unknown variable domain: {var.domain}")
|
||||
if with_static:
|
||||
# Variable type
|
||||
vtype: Optional[str] = None
|
||||
if pyomo_var.domain == pyomo.core.Binary:
|
||||
vtype = "B"
|
||||
elif pyomo_var.domain in [
|
||||
pyomo.core.Reals,
|
||||
pyomo.core.NonNegativeReals,
|
||||
pyomo.core.NonPositiveReals,
|
||||
pyomo.core.NegativeReals,
|
||||
pyomo.core.PositiveReals,
|
||||
]:
|
||||
vtype = "C"
|
||||
if vtype is None:
|
||||
raise Exception(f"unknown variable domain: {pyomo_var.domain}")
|
||||
variable.type = vtype
|
||||
|
||||
# Bounds
|
||||
lb, ub = var.bounds
|
||||
# Bounds
|
||||
lb, ub = pyomo_var.bounds
|
||||
variable.upper_bound = float(ub)
|
||||
variable.lower_bound = float(lb)
|
||||
|
||||
# Objective coefficient
|
||||
obj_coeff = 0.0
|
||||
if pyomo_var.name in self._obj:
|
||||
obj_coeff = self._obj[pyomo_var.name]
|
||||
variable.obj_coeff = obj_coeff
|
||||
|
||||
# Reduced costs
|
||||
rc = None
|
||||
if var in self.model.rc:
|
||||
rc = self.model.rc[var]
|
||||
if pyomo_var in self.model.rc:
|
||||
variable.reduced_cost = self.model.rc[pyomo_var]
|
||||
|
||||
# Objective coefficient
|
||||
obj_coeff = 0.0
|
||||
if var.name in self._obj:
|
||||
obj_coeff = self._obj[var.name]
|
||||
|
||||
return Variable(
|
||||
value=var.value,
|
||||
type=vtype,
|
||||
lower_bound=float(lb),
|
||||
upper_bound=float(ub),
|
||||
obj_coeff=obj_coeff,
|
||||
reduced_cost=rc,
|
||||
)
|
||||
variable.value = pyomo_var.value
|
||||
return variable
|
||||
|
||||
def _parse_pyomo_constraint(
|
||||
self,
|
||||
pyomo_constr: pyomo.core.Constraint,
|
||||
with_static: bool = True,
|
||||
) -> Constraint:
|
||||
assert self.model is not None
|
||||
constr = Constraint()
|
||||
|
||||
# Extract RHS and sense
|
||||
has_ub = pyomo_constr.has_ub()
|
||||
has_lb = pyomo_constr.has_lb()
|
||||
assert (
|
||||
(not has_lb) or (not has_ub) or pyomo_constr.upper() == pyomo_constr.lower()
|
||||
), "range constraints not supported"
|
||||
if not has_ub:
|
||||
constr.sense = ">"
|
||||
constr.rhs = pyomo_constr.lower()
|
||||
elif not has_lb:
|
||||
constr.sense = "<"
|
||||
constr.rhs = pyomo_constr.upper()
|
||||
else:
|
||||
constr.sense = "="
|
||||
constr.rhs = pyomo_constr.upper()
|
||||
if with_static:
|
||||
# Extract RHS and sense
|
||||
has_ub = pyomo_constr.has_ub()
|
||||
has_lb = pyomo_constr.has_lb()
|
||||
assert (
|
||||
(not has_lb)
|
||||
or (not has_ub)
|
||||
or pyomo_constr.upper() == pyomo_constr.lower()
|
||||
), "range constraints not supported"
|
||||
if not has_ub:
|
||||
constr.sense = ">"
|
||||
constr.rhs = pyomo_constr.lower()
|
||||
elif not has_lb:
|
||||
constr.sense = "<"
|
||||
constr.rhs = pyomo_constr.upper()
|
||||
else:
|
||||
constr.sense = "="
|
||||
constr.rhs = pyomo_constr.upper()
|
||||
|
||||
# Extract LHS
|
||||
constr.lhs = self._parse_pyomo_expr(pyomo_constr.body)
|
||||
# Extract LHS
|
||||
constr.lhs = self._parse_pyomo_expr(pyomo_constr.body)
|
||||
|
||||
# Extract solution attributes
|
||||
if self._has_lp_solution:
|
||||
|
||||
@@ -55,37 +55,6 @@ class GurobiPyomoSolver(BasePyomoSolver):
|
||||
gvar = self._pyomo_solver._pyomo_var_to_solver_var_map[var]
|
||||
gvar.setAttr(GRB.Attr.BranchPriority, int(round(priority)))
|
||||
|
||||
@overrides
|
||||
def get_variables(self) -> Dict[str, Variable]:
|
||||
variables = super().get_variables()
|
||||
if self._has_lp_solution:
|
||||
for (varname, var) in variables.items():
|
||||
pvar = self._varname_to_var[varname]
|
||||
gvar = self._pyomo_solver._pyomo_var_to_solver_var_map[pvar]
|
||||
GurobiSolver._parse_gurobi_var_lp(gvar, var)
|
||||
|
||||
return variables
|
||||
|
||||
@overrides
|
||||
def get_variable_attrs(self) -> List[str]:
|
||||
return [
|
||||
"basis_status",
|
||||
"category",
|
||||
"lower_bound",
|
||||
"obj_coeff",
|
||||
"reduced_cost",
|
||||
"sa_lb_down",
|
||||
"sa_lb_up",
|
||||
"sa_obj_down",
|
||||
"sa_obj_up",
|
||||
"sa_ub_down",
|
||||
"sa_ub_up",
|
||||
"type",
|
||||
"upper_bound",
|
||||
"user_features",
|
||||
"value",
|
||||
]
|
||||
|
||||
@overrides
|
||||
def _extract_node_count(self, log: str) -> int:
|
||||
return max(1, int(self._pyomo_solver._solver_model.getAttr("NodeCount")))
|
||||
|
||||
@@ -279,63 +279,25 @@ def run_basic_usage_tests(solver: InternalSolver) -> None:
|
||||
assert isinstance(mip_stats.mip_wallclock_time, float)
|
||||
assert mip_stats.mip_wallclock_time > 0
|
||||
|
||||
# Fetch variables (after-load)
|
||||
# Fetch variables (after-mip)
|
||||
assert_equals(
|
||||
_round_variables(solver.get_variables()),
|
||||
_round_variables(solver.get_variables(with_static=False)),
|
||||
_remove_unsupported_var_attrs(
|
||||
solver,
|
||||
{
|
||||
"x[0]": Variable(
|
||||
lower_bound=0.0,
|
||||
obj_coeff=505.0,
|
||||
type="B",
|
||||
upper_bound=1.0,
|
||||
value=1.0,
|
||||
),
|
||||
"x[1]": Variable(
|
||||
lower_bound=0.0,
|
||||
obj_coeff=352.0,
|
||||
type="B",
|
||||
upper_bound=1.0,
|
||||
value=0.0,
|
||||
),
|
||||
"x[2]": Variable(
|
||||
lower_bound=0.0,
|
||||
obj_coeff=458.0,
|
||||
type="B",
|
||||
upper_bound=1.0,
|
||||
value=1.0,
|
||||
),
|
||||
"x[3]": Variable(
|
||||
lower_bound=0.0,
|
||||
obj_coeff=220.0,
|
||||
type="B",
|
||||
upper_bound=1.0,
|
||||
value=1.0,
|
||||
),
|
||||
"z": Variable(
|
||||
lower_bound=0.0,
|
||||
obj_coeff=0.0,
|
||||
type="C",
|
||||
upper_bound=67.0,
|
||||
value=61.0,
|
||||
),
|
||||
"x[0]": Variable(value=1.0),
|
||||
"x[1]": Variable(value=0.0),
|
||||
"x[2]": Variable(value=1.0),
|
||||
"x[3]": Variable(value=1.0),
|
||||
"z": Variable(value=61.0),
|
||||
},
|
||||
),
|
||||
)
|
||||
|
||||
# Fetch constraints (after-mip)
|
||||
assert_equals(
|
||||
_round_constraints(solver.get_constraints()),
|
||||
{
|
||||
"eq_capacity": Constraint(
|
||||
lazy=False,
|
||||
lhs={"x[0]": 23.0, "x[1]": 26.0, "x[2]": 20.0, "x[3]": 18.0, "z": -1.0},
|
||||
rhs=0.0,
|
||||
sense="=",
|
||||
slack=0.0,
|
||||
)
|
||||
},
|
||||
_round_constraints(solver.get_constraints(with_static=False)),
|
||||
{"eq_capacity": Constraint(slack=0.0)},
|
||||
)
|
||||
|
||||
# Build a new constraint
|
||||
|
||||
Reference in New Issue
Block a user