Add types to remaining files; activate mypy's disallow_untyped_defs

This commit is contained in:
2021-04-07 21:25:30 -05:00
parent f5606efb72
commit e9cd6d1715
21 changed files with 102 additions and 64 deletions

View File

@@ -16,7 +16,7 @@ from . import _get_knapsack_instance, get_internal_solvers
logger = logging.getLogger(__name__)
def test_learning_solver():
def test_learning_solver() -> None:
for mode in ["exact", "heuristic"]:
for internal_solver in get_internal_solvers():
logger.info("Solver: %s" % internal_solver)
@@ -30,17 +30,21 @@ def test_learning_solver():
assert hasattr(instance, "features")
sample = instance.training_data[0]
assert sample.solution is not None
assert sample.solution["x[0]"] == 1.0
assert sample.solution["x[1]"] == 0.0
assert sample.solution["x[2]"] == 1.0
assert sample.solution["x[3]"] == 1.0
assert sample.lower_bound == 1183.0
assert sample.upper_bound == 1183.0
assert sample.lp_solution is not None
assert round(sample.lp_solution["x[0]"], 3) == 1.000
assert round(sample.lp_solution["x[1]"], 3) == 0.923
assert round(sample.lp_solution["x[2]"], 3) == 1.000
assert round(sample.lp_solution["x[3]"], 3) == 0.000
assert sample.lp_value is not None
assert round(sample.lp_value, 3) == 1287.923
assert sample.mip_log is not None
assert len(sample.mip_log) > 100
solver.fit([instance])
@@ -51,7 +55,7 @@ def test_learning_solver():
dill.dump(solver, file)
def test_solve_without_lp():
def test_solve_without_lp() -> None:
for internal_solver in get_internal_solvers():
logger.info("Solver: %s" % internal_solver)
instance = _get_knapsack_instance(internal_solver)
@@ -64,7 +68,7 @@ def test_solve_without_lp():
solver.solve(instance)
def test_parallel_solve():
def test_parallel_solve() -> None:
for internal_solver in get_internal_solvers():
instances = [_get_knapsack_instance(internal_solver) for _ in range(10)]
solver = LearningSolver(solver=internal_solver)
@@ -72,10 +76,11 @@ def test_parallel_solve():
assert len(results) == 10
for instance in instances:
data = instance.training_data[0]
assert data.solution is not None
assert len(data.solution.keys()) == 4
def test_solve_fit_from_disk():
def test_solve_fit_from_disk() -> None:
for internal_solver in get_internal_solvers():
# Create instances and pickle them
instances = []
@@ -108,7 +113,7 @@ def test_solve_fit_from_disk():
os.remove(instance.filename)
def test_simulate_perfect():
def test_simulate_perfect() -> None:
internal_solver = GurobiSolver()
instance = _get_knapsack_instance(internal_solver)
with tempfile.NamedTemporaryFile(suffix=".pkl", delete=False) as tmp:
@@ -121,7 +126,7 @@ def test_simulate_perfect():
assert stats["Lower bound"] == stats["Objective: Predicted lower bound"]
def test_gap():
def test_gap() -> None:
assert LearningSolver._compute_gap(ub=0.0, lb=0.0) == 0.0
assert LearningSolver._compute_gap(ub=1.0, lb=0.5) == 0.5
assert LearningSolver._compute_gap(ub=1.0, lb=1.0) == 0.0