Refer to variables by varname instead of (vname, index)

This commit is contained in:
2021-04-07 10:56:31 -05:00
parent 856b595d5e
commit 1cf6124757
22 changed files with 467 additions and 516 deletions

View File

@@ -38,45 +38,18 @@ def test_internal_solver_warm_starts():
model = instance.to_model()
solver = solver_class()
solver.set_instance(instance, model)
solver.set_warm_start(
{
"x": {
0: 1.0,
1: 0.0,
2: 0.0,
3: 1.0,
}
}
)
solver.set_warm_start({"x[0]": 1.0, "x[1]": 0.0, "x[2]": 0.0, "x[3]": 1.0})
stats = solver.solve(tee=True)
if stats["Warm start value"] is not None:
assert stats["Warm start value"] == 725.0
else:
warn(f"{solver_class.__name__} should set warm start value")
solver.set_warm_start(
{
"x": {
0: 1.0,
1: 1.0,
2: 1.0,
3: 1.0,
}
}
)
solver.set_warm_start({"x[0]": 1.0, "x[1]": 1.0, "x[2]": 1.0, "x[3]": 1.0})
stats = solver.solve(tee=True)
assert stats["Warm start value"] is None
solver.fix(
{
"x": {
0: 1.0,
1: 0.0,
2: 0.0,
3: 1.0,
}
}
)
solver.fix({"x[0]": 1.0, "x[1]": 0.0, "x[2]": 0.0, "x[3]": 1.0})
stats = solver.solve(tee=True)
assert stats["Lower bound"] == 725.0
assert stats["Upper bound"] == 725.0
@@ -91,16 +64,18 @@ def test_internal_solver():
solver = solver_class()
solver.set_instance(instance, model)
assert solver.get_variable_names() == ["x[0]", "x[1]", "x[2]", "x[3]"]
stats = solver.solve_lp()
assert not solver.is_infeasible()
assert round(stats["LP value"], 3) == 1287.923
assert len(stats["LP log"]) > 100
solution = solver.get_solution()
assert round(solution["x"][0], 3) == 1.000
assert round(solution["x"][1], 3) == 0.923
assert round(solution["x"][2], 3) == 1.000
assert round(solution["x"][3], 3) == 0.000
assert round(solution["x[0]"], 3) == 1.000
assert round(solution["x[1]"], 3) == 0.923
assert round(solution["x[2]"], 3) == 1.000
assert round(solution["x[3]"], 3) == 0.000
stats = solver.solve(tee=True)
assert not solver.is_infeasible()
@@ -111,10 +86,10 @@ def test_internal_solver():
assert isinstance(stats["Wallclock time"], float)
solution = solver.get_solution()
assert solution["x"][0] == 1.0
assert solution["x"][1] == 0.0
assert solution["x"][2] == 1.0
assert solution["x"][3] == 1.0
assert solution["x[0]"] == 1.0
assert solution["x[1]"] == 0.0
assert solution["x[2]"] == 1.0
assert solution["x[3]"] == 1.0
# Add a brand new constraint
if isinstance(solver, BasePyomoSolver):
@@ -199,7 +174,6 @@ def test_infeasible_instance():
stats = solver.solve_lp()
assert solver.get_solution() is None
assert stats["LP value"] is None
assert solver.get_value("x", 0) is None
def test_iteration_cb():

View File

@@ -16,7 +16,6 @@ def test_lazy_cb():
model = instance.to_model()
def lazy_cb(cb_solver, cb_model):
logger.info("x[0] = %.f" % cb_solver.get_value("x", 0))
cobj = (cb_model.getVarByName("x[0]") * 1.0, "<", 0.0, "cut")
if not cb_solver.is_constraint_satisfied(cobj):
cb_solver.add_constraint(cobj)
@@ -24,4 +23,4 @@ def test_lazy_cb():
solver.set_instance(instance, model)
solver.solve(lazy_cb=lazy_cb)
solution = solver.get_solution()
assert solution["x"][0] == 0.0
assert solution["x[0]"] == 0.0

View File

@@ -30,16 +30,16 @@ def test_learning_solver():
assert hasattr(instance, "features")
sample = instance.training_data[0]
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.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 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 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 round(sample.lp_value, 3) == 1287.923
assert len(sample.mip_log) > 100
@@ -72,7 +72,7 @@ def test_parallel_solve():
assert len(results) == 10
for instance in instances:
data = instance.training_data[0]
assert len(data.solution["x"].keys()) == 4
assert len(data.solution.keys()) == 4
def test_solve_fit_from_disk():