mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 01:18:52 -06:00
Use np.ndarray for constraint names
This commit is contained in:
@@ -211,7 +211,7 @@ class GurobiSolver(InternalSolver):
|
||||
raise Exception(f"unknown cbasis: {v}")
|
||||
|
||||
gp_constrs = model.getConstrs()
|
||||
constr_names = model.getAttr("constrName", gp_constrs)
|
||||
constr_names = np.array(model.getAttr("constrName", gp_constrs), dtype="S")
|
||||
lhs: Optional[List] = None
|
||||
rhs, senses, slacks, basis_status = None, None, None, None
|
||||
dual_value, basis_status, sa_rhs_up, sa_rhs_down = None, None, None, None
|
||||
|
||||
@@ -72,7 +72,7 @@ class Constraints:
|
||||
dual_values: Optional[np.ndarray] = None
|
||||
lazy: Optional[List[bool]] = None
|
||||
lhs: Optional[List[List[Tuple[bytes, float]]]] = None
|
||||
names: Optional[List[str]] = None
|
||||
names: Optional[np.ndarray] = None
|
||||
rhs: Optional[np.ndarray] = None
|
||||
sa_rhs_down: Optional[np.ndarray] = None
|
||||
sa_rhs_up: Optional[np.ndarray] = None
|
||||
@@ -86,7 +86,7 @@ class Constraints:
|
||||
dual_values=sample.get_vector("lp_constr_dual_values"),
|
||||
lazy=sample.get_vector("static_constr_lazy"),
|
||||
# lhs=sample.get_vector("static_constr_lhs"),
|
||||
names=sample.get_vector("static_constr_names"),
|
||||
names=sample.get_array("static_constr_names"),
|
||||
rhs=sample.get_vector("static_constr_rhs"),
|
||||
sa_rhs_down=sample.get_vector("lp_constr_sa_rhs_down"),
|
||||
sa_rhs_up=sample.get_vector("lp_constr_sa_rhs_up"),
|
||||
@@ -100,7 +100,7 @@ class Constraints:
|
||||
dual_values=(
|
||||
None if self.dual_values is None else self.dual_values[selected]
|
||||
),
|
||||
names=self._filter(self.names, selected),
|
||||
names=(None if self.names is None else self.names[selected]),
|
||||
lazy=self._filter(self.lazy, selected),
|
||||
lhs=self._filter(self.lhs, selected),
|
||||
rhs=(None if self.rhs is None else self.rhs[selected]),
|
||||
@@ -254,7 +254,7 @@ class InternalSolver(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def remove_constraints(self, names: List[str]) -> None:
|
||||
def remove_constraints(self, names: np.ndarray) -> None:
|
||||
"""
|
||||
Removes the given constraints from the model.
|
||||
"""
|
||||
|
||||
@@ -96,7 +96,7 @@ class BasePyomoSolver(InternalSolver):
|
||||
else:
|
||||
expr = lhs >= cf.rhs[i]
|
||||
cl = pe.Constraint(expr=expr, name=name)
|
||||
self.model.add_component(name, cl)
|
||||
self.model.add_component(name.decode(), cl)
|
||||
self._pyomo_solver.add_constraint(cl)
|
||||
self._cname_to_constr[name] = cl
|
||||
self._termination_condition = ""
|
||||
@@ -233,7 +233,7 @@ class BasePyomoSolver(InternalSolver):
|
||||
_parse_constraint(constr)
|
||||
|
||||
return Constraints(
|
||||
names=_none_if_empty(names),
|
||||
names=_none_if_empty(np.array(names, dtype="S")),
|
||||
rhs=_none_if_empty(np.array(rhs, dtype=float)),
|
||||
senses=_none_if_empty(senses),
|
||||
lhs=_none_if_empty(lhs),
|
||||
|
||||
@@ -53,7 +53,7 @@ def run_basic_usage_tests(solver: InternalSolver) -> None:
|
||||
assert_equals(
|
||||
solver.get_constraints(),
|
||||
Constraints(
|
||||
names=["eq_capacity"],
|
||||
names=np.array(["eq_capacity"], dtype="S"),
|
||||
rhs=np.array([0.0]),
|
||||
lhs=[
|
||||
[
|
||||
@@ -110,7 +110,7 @@ def run_basic_usage_tests(solver: InternalSolver) -> None:
|
||||
Constraints(
|
||||
basis_status=["N"],
|
||||
dual_values=np.array([13.538462]),
|
||||
names=["eq_capacity"],
|
||||
names=np.array(["eq_capacity"], dtype="S"),
|
||||
sa_rhs_down=np.array([-24.0]),
|
||||
sa_rhs_up=np.array([2.0]),
|
||||
slacks=np.array([0.0]),
|
||||
@@ -153,7 +153,7 @@ def run_basic_usage_tests(solver: InternalSolver) -> None:
|
||||
_filter_attrs(
|
||||
solver.get_constraint_attrs(),
|
||||
Constraints(
|
||||
names=["eq_capacity"],
|
||||
names=np.array(["eq_capacity"], dtype="S"),
|
||||
slacks=np.array([0.0]),
|
||||
),
|
||||
),
|
||||
@@ -161,7 +161,7 @@ def run_basic_usage_tests(solver: InternalSolver) -> None:
|
||||
|
||||
# Build new constraint and verify that it is violated
|
||||
cf = Constraints(
|
||||
names=["cut"],
|
||||
names=np.array(["cut"], dtype="S"),
|
||||
lhs=[[(b"x[0]", 1.0)]],
|
||||
rhs=np.array([0.0]),
|
||||
senses=["<"],
|
||||
@@ -175,7 +175,7 @@ def run_basic_usage_tests(solver: InternalSolver) -> None:
|
||||
_filter_attrs(
|
||||
solver.get_constraint_attrs(),
|
||||
Constraints(
|
||||
names=["eq_capacity", "cut"],
|
||||
names=np.array(["eq_capacity", "cut"], dtype="S"),
|
||||
rhs=np.array([0.0, 0.0]),
|
||||
lhs=[
|
||||
[
|
||||
@@ -198,7 +198,7 @@ def run_basic_usage_tests(solver: InternalSolver) -> None:
|
||||
assert_equals(solver.are_constraints_satisfied(cf), [True])
|
||||
|
||||
# Remove the new constraint
|
||||
solver.remove_constraints(["cut"])
|
||||
solver.remove_constraints(np.array(["cut"], dtype="S"))
|
||||
|
||||
# New constraint should no longer affect solution
|
||||
stats = solver.solve()
|
||||
|
||||
Reference in New Issue
Block a user