Remove sample.{get,set}

This commit is contained in:
2021-07-27 09:00:04 -05:00
parent ef9c48d79a
commit 4224586d10
15 changed files with 184 additions and 211 deletions

View File

@@ -81,7 +81,7 @@ def test_usage(
) -> None:
stats_before = solver.solve(stab_instance)
sample = stab_instance.get_samples()[0]
user_cuts_enforced = sample.get("user_cuts_enforced")
user_cuts_enforced = sample.get_set("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

@@ -93,7 +93,7 @@ def test_usage_with_solver(instance: Instance) -> None:
stats: LearningSolveStats = {}
sample = instance.get_samples()[0]
assert sample.get("lazy_enforced") is not None
assert sample.get_set("lazy_enforced") is not None
# LearningSolver calls before_solve_mip
component.before_solve_mip(
@@ -142,7 +142,7 @@ def test_usage_with_solver(instance: Instance) -> None:
)
# Should update training sample
assert sample.get("lazy_enforced") == {"c1", "c2", "c3", "c4"}
assert sample.get_set("lazy_enforced") == {"c1", "c2", "c3", "c4"}
#
# Should update stats
assert stats["LazyStatic: Removed"] == 1

View File

@@ -24,21 +24,23 @@ def test_knapsack() -> None:
# after-load
# -------------------------------------------------------
extractor.extract_after_load_features(instance, solver, sample)
assert_equals(sample.get("var_names"), ["x[0]", "x[1]", "x[2]", "x[3]", "z"])
assert_equals(sample.get("var_lower_bounds"), [0.0, 0.0, 0.0, 0.0, 0.0])
assert_equals(sample.get("var_obj_coeffs"), [505.0, 352.0, 458.0, 220.0, 0.0])
assert_equals(sample.get("var_types"), ["B", "B", "B", "B", "C"])
assert_equals(sample.get("var_upper_bounds"), [1.0, 1.0, 1.0, 1.0, 67.0])
assert_equals(sample.get_vector("var_names"), ["x[0]", "x[1]", "x[2]", "x[3]", "z"])
assert_equals(sample.get_vector("var_lower_bounds"), [0.0, 0.0, 0.0, 0.0, 0.0])
assert_equals(
sample.get("var_categories"),
sample.get_vector("var_obj_coeffs"), [505.0, 352.0, 458.0, 220.0, 0.0]
)
assert_equals(sample.get_vector("var_types"), ["B", "B", "B", "B", "C"])
assert_equals(sample.get_vector("var_upper_bounds"), [1.0, 1.0, 1.0, 1.0, 67.0])
assert_equals(
sample.get_vector("var_categories"),
["default", "default", "default", "default", None],
)
assert_equals(
sample.get("var_features_user"),
sample.get_vector_list("var_features_user"),
[[23.0, 505.0], [26.0, 352.0], [20.0, 458.0], [18.0, 220.0], None],
)
assert_equals(
sample.get("var_features_AlvLouWeh2017"),
sample.get_vector_list("var_features_AlvLouWeh2017"),
[
[1.0, 0.32899, 0.0],
[1.0, 0.229316, 0.0],
@@ -47,61 +49,63 @@ def test_knapsack() -> None:
[0.0, 0.0, 0.0],
],
)
assert sample.get("var_features") is not None
assert_equals(sample.get("constr_names"), ["eq_capacity"])
assert_equals(
sample.get("constr_lhs"),
[
[
("x[0]", 23.0),
("x[1]", 26.0),
("x[2]", 20.0),
("x[3]", 18.0),
("z", -1.0),
],
],
)
assert_equals(sample.get("constr_rhs"), [0.0])
assert_equals(sample.get("constr_senses"), ["="])
assert_equals(sample.get("constr_features_user"), [None])
assert_equals(sample.get("constr_categories"), ["eq_capacity"])
assert_equals(sample.get("constr_lazy"), [False])
assert_equals(sample.get("instance_features_user"), [67.0, 21.75])
assert_equals(sample.get("static_lazy_count"), 0)
assert sample.get_vector_list("var_features") is not None
assert_equals(sample.get_vector("constr_names"), ["eq_capacity"])
# assert_equals(
# sample.get_vector("constr_lhs"),
# [
# [
# ("x[0]", 23.0),
# ("x[1]", 26.0),
# ("x[2]", 20.0),
# ("x[3]", 18.0),
# ("z", -1.0),
# ],
# ],
# )
assert_equals(sample.get_vector("constr_rhs"), [0.0])
assert_equals(sample.get_vector("constr_senses"), ["="])
assert_equals(sample.get_vector("constr_features_user"), [None])
assert_equals(sample.get_vector("constr_categories"), ["eq_capacity"])
assert_equals(sample.get_vector("constr_lazy"), [False])
assert_equals(sample.get_vector("instance_features_user"), [67.0, 21.75])
assert_equals(sample.get_scalar("static_lazy_count"), 0)
# after-lp
# -------------------------------------------------------
solver.solve_lp()
extractor.extract_after_lp_features(solver, sample)
assert_equals(
sample.get("lp_var_basis_status"),
sample.get_vector("lp_var_basis_status"),
["U", "B", "U", "L", "U"],
)
assert_equals(
sample.get("lp_var_reduced_costs"),
sample.get_vector("lp_var_reduced_costs"),
[193.615385, 0.0, 187.230769, -23.692308, 13.538462],
)
assert_equals(
sample.get("lp_var_sa_lb_down"),
sample.get_vector("lp_var_sa_lb_down"),
[-inf, -inf, -inf, -0.111111, -inf],
)
assert_equals(
sample.get("lp_var_sa_lb_up"),
sample.get_vector("lp_var_sa_lb_up"),
[1.0, 0.923077, 1.0, 1.0, 67.0],
)
assert_equals(
sample.get("lp_var_sa_obj_down"),
sample.get_vector("lp_var_sa_obj_down"),
[311.384615, 317.777778, 270.769231, -inf, -13.538462],
)
assert_equals(
sample.get("lp_var_sa_obj_up"),
sample.get_vector("lp_var_sa_obj_up"),
[inf, 570.869565, inf, 243.692308, inf],
)
assert_equals(sample.get("lp_var_sa_ub_down"), [0.913043, 0.923077, 0.9, 0.0, 43.0])
assert_equals(sample.get("lp_var_sa_ub_up"), [2.043478, inf, 2.2, inf, 69.0])
assert_equals(sample.get("lp_var_values"), [1.0, 0.923077, 1.0, 0.0, 67.0])
assert_equals(
sample.get("lp_var_features_AlvLouWeh2017"),
sample.get_vector("lp_var_sa_ub_down"), [0.913043, 0.923077, 0.9, 0.0, 43.0]
)
assert_equals(sample.get_vector("lp_var_sa_ub_up"), [2.043478, inf, 2.2, inf, 69.0])
assert_equals(sample.get_vector("lp_var_values"), [1.0, 0.923077, 1.0, 0.0, 67.0])
assert_equals(
sample.get_vector_list("lp_var_features_AlvLouWeh2017"),
[
[1.0, 0.32899, 0.0, 0.0, 1.0, 1.0, 5.265874, 46.051702],
[1.0, 0.229316, 0.0, 0.076923, 1.0, 1.0, 3.532875, 5.388476],
@@ -110,19 +114,19 @@ def test_knapsack() -> None:
[0.0, 0.0, 0.0, 0.0, 1.0, -1.0, 0.0, 0.0],
],
)
assert sample.get("lp_var_features") is not None
assert_equals(sample.get("lp_constr_basis_status"), ["N"])
assert_equals(sample.get("lp_constr_dual_values"), [13.538462])
assert_equals(sample.get("lp_constr_sa_rhs_down"), [-24.0])
assert_equals(sample.get("lp_constr_sa_rhs_up"), [2.0])
assert_equals(sample.get("lp_constr_slacks"), [0.0])
assert sample.get_vector_list("lp_var_features") is not None
assert_equals(sample.get_vector("lp_constr_basis_status"), ["N"])
assert_equals(sample.get_vector("lp_constr_dual_values"), [13.538462])
assert_equals(sample.get_vector("lp_constr_sa_rhs_down"), [-24.0])
assert_equals(sample.get_vector("lp_constr_sa_rhs_up"), [2.0])
assert_equals(sample.get_vector("lp_constr_slacks"), [0.0])
# after-mip
# -------------------------------------------------------
solver.solve()
extractor.extract_after_mip_features(solver, sample)
assert_equals(sample.get("mip_var_values"), [1.0, 0.0, 1.0, 1.0, 61.0])
assert_equals(sample.get("mip_constr_slacks"), [0.0])
assert_equals(sample.get_vector("mip_var_values"), [1.0, 0.0, 1.0, 1.0, 61.0])
assert_equals(sample.get_vector("mip_constr_slacks"), [0.0])
def test_constraint_getindex() -> None:

View File

@@ -41,9 +41,9 @@ def test_instance() -> None:
solver.solve(instance)
assert len(instance.get_samples()) == 1
sample = instance.get_samples()[0]
assert sample.get("mip_var_values") == [1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
assert sample.get("mip_lower_bound") == 4.0
assert sample.get("mip_upper_bound") == 4.0
assert sample.get_vector("mip_var_values") == [1.0, 0.0, 1.0, 1.0, 0.0, 1.0]
assert sample.get_scalar("mip_lower_bound") == 4.0
assert sample.get_scalar("mip_upper_bound") == 4.0
def test_subtour() -> None:
@@ -65,10 +65,10 @@ def test_subtour() -> None:
samples = instance.get_samples()
assert len(samples) == 1
sample = samples[0]
lazy_enforced = sample.get("lazy_enforced")
lazy_enforced = sample.get_set("lazy_enforced")
assert lazy_enforced is not None
assert len(lazy_enforced) > 0
assert sample.get("mip_var_values") == [
assert sample.get_vector("mip_var_values") == [
1.0,
0.0,
0.0,

View File

@@ -38,16 +38,18 @@ def test_learning_solver(
assert len(instance.get_samples()) > 0
sample = instance.get_samples()[0]
assert sample.get("mip_var_values") == [1.0, 0.0, 1.0, 1.0, 61.0]
assert sample.get("mip_lower_bound") == 1183.0
assert sample.get("mip_upper_bound") == 1183.0
mip_log = sample.get("mip_log")
assert sample.get_vector("mip_var_values") == [1.0, 0.0, 1.0, 1.0, 61.0]
assert sample.get_scalar("mip_lower_bound") == 1183.0
assert sample.get_scalar("mip_upper_bound") == 1183.0
mip_log = sample.get_scalar("mip_log")
assert mip_log is not None
assert len(mip_log) > 100
assert_equals(sample.get("lp_var_values"), [1.0, 0.923077, 1.0, 0.0, 67.0])
assert_equals(sample.get("lp_value"), 1287.923077)
lp_log = sample.get("lp_log")
assert_equals(
sample.get_vector("lp_var_values"), [1.0, 0.923077, 1.0, 0.0, 67.0]
)
assert_equals(sample.get_scalar("lp_value"), 1287.923077)
lp_log = sample.get_scalar("lp_log")
assert lp_log is not None
assert len(lp_log) > 100