Remove {get,put}_set and deprecated functions

This commit is contained in:
2021-08-10 17:27:06 -05:00
parent ed58242b5c
commit 9cfb31bacb
11 changed files with 56 additions and 124 deletions

View File

@@ -24,13 +24,13 @@ def training_instances() -> List[Instance]:
samples_0 = [
MemorySample(
{
"mip_constr_lazy_enforced": {b"c1", b"c2"},
"mip_constr_lazy_enforced": np.array(["c1", "c2"], dtype="S"),
"static_instance_features": np.array([5.0]),
},
),
MemorySample(
{
"mip_constr_lazy_enforced": {b"c2", b"c3"},
"mip_constr_lazy_enforced": np.array(["c2", "c3"], dtype="S"),
"static_instance_features": np.array([5.0]),
},
),
@@ -55,7 +55,7 @@ def training_instances() -> List[Instance]:
samples_1 = [
MemorySample(
{
"mip_constr_lazy_enforced": {b"c3", b"c4"},
"mip_constr_lazy_enforced": np.array(["c3", "c4"], dtype="S"),
"static_instance_features": np.array([8.0]),
},
)
@@ -81,7 +81,12 @@ def training_instances() -> List[Instance]:
def test_sample_xy(training_instances: List[Instance]) -> None:
comp = DynamicLazyConstraintsComponent()
comp.pre_fit([{b"c1", b"c2", b"c3", b"c4"}])
comp.pre_fit(
[
np.array(["c1", "c3", "c4"], dtype="S"),
np.array(["c1", "c2", "c4"], dtype="S"),
]
)
x_expected = {
b"type-a": np.array([[5.0, 1.0, 2.0, 3.0], [5.0, 4.0, 5.0, 6.0]]),
b"type-b": np.array([[5.0, 1.0, 2.0, 0.0], [5.0, 3.0, 4.0, 0.0]]),

View File

@@ -82,7 +82,7 @@ def test_usage(
) -> None:
stats_before = solver.solve(stab_instance)
sample = stab_instance.get_samples()[0]
user_cuts_enforced = sample.get_set("mip_user_cuts_enforced")
user_cuts_enforced = sample.get_array("mip_user_cuts_enforced")
assert user_cuts_enforced is not None
assert len(user_cuts_enforced) > 0
assert stats_before["UserCuts: Added ahead-of-time"] == 0

View File

@@ -19,6 +19,7 @@ from miplearn.types import (
LearningSolveStats,
ConstraintCategory,
)
from miplearn.solvers.tests import assert_equals
@pytest.fixture
@@ -35,7 +36,7 @@ def sample() -> Sample:
"static_constr_lazy": np.array([True, True, True, True, False]),
"static_constr_names": np.array(["c1", "c2", "c3", "c4", "c5"], dtype="S"),
"static_instance_features": [5.0],
"mip_constr_lazy_enforced": {b"c1", b"c2", b"c4"},
"mip_constr_lazy_enforced": np.array(["c1", "c2", "c4"], dtype="S"),
"lp_constr_features": np.array(
[
[1.0, 1.0, 0.0],
@@ -96,7 +97,7 @@ def test_usage_with_solver(instance: Instance) -> None:
stats: LearningSolveStats = {}
sample = instance.get_samples()[0]
assert sample.get_set("mip_constr_lazy_enforced") is not None
assert sample.get_array("mip_constr_lazy_enforced") is not None
# LearningSolver calls before_solve_mip
component.before_solve_mip(
@@ -145,8 +146,13 @@ def test_usage_with_solver(instance: Instance) -> None:
)
# Should update training sample
assert sample.get_set("mip_constr_lazy_enforced") == {b"c1", b"c2", b"c3", b"c4"}
#
mip_constr_lazy_enforced = sample.get_array("mip_constr_lazy_enforced")
assert mip_constr_lazy_enforced is not None
assert_equals(
sorted(mip_constr_lazy_enforced),
np.array(["c1", "c2", "c3", "c4"], dtype="S"),
)
# Should update stats
assert stats["LazyStatic: Removed"] == 1
assert stats["LazyStatic: Kept"] == 3

View File

@@ -77,7 +77,7 @@ def test_knapsack() -> None:
np.array(["eq_capacity"], dtype="S"),
)
# assert_equals(
# sample.get_vector("static_constr_lhs"),
# sample.get_array("static_constr_lhs"),
# [
# [
# ("x[0]", 23.0),
@@ -89,7 +89,7 @@ def test_knapsack() -> None:
# ],
# )
assert_equals(
sample.get_vector("static_constr_rhs"),
sample.get_array("static_constr_rhs"),
np.array([0.0]),
)
assert_equals(
@@ -97,11 +97,11 @@ def test_knapsack() -> None:
np.array(["="], dtype="S"),
)
assert_equals(
sample.get_vector("static_constr_features"),
sample.get_array("static_constr_features"),
np.array([[0.0]]),
)
assert_equals(
sample.get_vector("static_constr_categories"),
sample.get_array("static_constr_categories"),
np.array(["eq_capacity"], dtype="S"),
)
assert_equals(
@@ -109,7 +109,7 @@ def test_knapsack() -> None:
np.array([False]),
)
assert_equals(
sample.get_vector("static_instance_features"),
sample.get_array("static_instance_features"),
np.array([67.0, 21.75]),
)
assert_equals(sample.get_scalar("static_constr_lazy_count"), 0)

View File

@@ -17,7 +17,7 @@ def test_usage() -> None:
# Save instance to disk
filename = tempfile.mktemp()
FileInstance.save(original, filename)
sample = Hdf5Sample(filename, check_data=True)
sample = Hdf5Sample(filename)
assert len(sample.get_array("pickled")) > 0
# Solve instance from disk

View File

@@ -66,7 +66,7 @@ def test_subtour() -> None:
samples = instance.get_samples()
assert len(samples) == 1
sample = samples[0]
lazy_enforced = sample.get_set("mip_constr_lazy_enforced")
lazy_enforced = sample.get_array("mip_constr_lazy_enforced")
assert lazy_enforced is not None
assert len(lazy_enforced) > 0
assert_equals(