Make all before/solve callbacks receive same parameters

This commit is contained in:
2021-04-02 07:05:16 -05:00
parent 8eb2b63a85
commit 0c687692f7
17 changed files with 201 additions and 189 deletions

View File

@@ -80,7 +80,14 @@ def test_drop_redundant():
component.classifiers = classifiers
# LearningSolver calls before_solve
component.before_solve_mip(solver, instance, None)
component.before_solve_mip(
solver=solver,
instance=instance,
model=None,
stats={},
features=None,
training_data=None,
)
# Should query list of constraints
internal.get_constraint_ids.assert_called_once()
@@ -123,7 +130,14 @@ def test_drop_redundant():
# LearningSolver calls after_solve
training_data = {}
component.after_solve_mip(solver, instance, None, {}, training_data)
component.after_solve_mip(
solver=solver,
instance=instance,
model=None,
stats={},
features=None,
training_data=training_data,
)
# Should query slack for all inequalities
internal.get_inequality_slacks.assert_called_once()
@@ -147,7 +161,14 @@ def test_drop_redundant_with_check_feasibility():
component.classifiers = classifiers
# LearningSolver call before_solve
component.before_solve_mip(solver, instance, None)
component.before_solve_mip(
solver=solver,
instance=instance,
model=None,
stats={},
features=None,
training_data=None,
)
# Assert constraints are extracted
assert internal.extract_constraint.call_count == 2

View File

@@ -86,7 +86,14 @@ def test_lazy_before():
component.classifiers["a"].predict_proba = Mock(return_value=[[0.95, 0.05]])
component.classifiers["b"].predict_proba = Mock(return_value=[[0.02, 0.80]])
component.before_solve_mip(solver, instances[0], models[0])
component.before_solve_mip(
solver=solver,
instance=instances[0],
model=models[0],
stats=None,
features=None,
training_data=None,
)
# Should ask classifier likelihood of each constraint being violated
expected_x_test_a = np.array([[67.0, 21.75, 1287.92]])

View File

@@ -69,7 +69,14 @@ def test_usage_with_solver():
)
# LearningSolver calls before_solve
component.before_solve_mip(solver, instance, None)
component.before_solve_mip(
solver=solver,
instance=instance,
model=None,
stats=None,
features=None,
training_data=None,
)
# Should ask if instance has static lazy constraints
instance.has_static_lazy_constraints.assert_called_once()

View File

@@ -157,11 +157,11 @@ def test_xy_sample_without_lp() -> None:
assert y_actual == y_expected
def test_usage():
def test_usage() -> None:
solver = LearningSolver(components=[ObjectiveValueComponent()])
instance = get_knapsack_instance(GurobiPyomoSolver())
solver.solve(instance)
solver.fit([instance])
stats = solver.solve(instance)
assert stats["Lower bound"] == stats["Objective: predicted LB"]
assert stats["Upper bound"] == stats["Objective: predicted UB"]
assert stats["Lower bound"] == stats["Objective: Predicted LB"]
assert stats["Upper bound"] == stats["Objective: Predicted UB"]

View File

@@ -226,6 +226,6 @@ def test_usage():
solver.solve(instance)
solver.fit([instance])
stats = solver.solve(instance)
assert stats["Primal: free"] == 0
assert stats["Primal: one"] + stats["Primal: zero"] == 10
assert stats["Primal: Free"] == 0
assert stats["Primal: One"] + stats["Primal: Zero"] == 10
assert stats["Lower bound"] == stats["Warm start value"]