mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 01:18:52 -06:00
Refer to variables by varname instead of (vname, index)
This commit is contained in:
@@ -10,6 +10,7 @@ import networkx as nx
|
||||
import pytest
|
||||
from gurobipy import GRB
|
||||
from networkx import Graph
|
||||
from overrides import overrides
|
||||
|
||||
from miplearn.components.dynamic_user_cuts import UserCutsComponent
|
||||
from miplearn.instance.base import Instance
|
||||
@@ -24,6 +25,7 @@ class GurobiStableSetProblem(Instance):
|
||||
super().__init__()
|
||||
self.graph: Graph = graph
|
||||
|
||||
@overrides
|
||||
def to_model(self) -> Any:
|
||||
model = gp.Model()
|
||||
x = [model.addVar(vtype=GRB.BINARY) for _ in range(len(self.graph.nodes))]
|
||||
@@ -32,9 +34,11 @@ class GurobiStableSetProblem(Instance):
|
||||
model.addConstr(x[e[0]] + x[e[1]] <= 1)
|
||||
return model
|
||||
|
||||
@overrides
|
||||
def has_user_cuts(self) -> bool:
|
||||
return True
|
||||
|
||||
@overrides
|
||||
def find_violated_user_cuts(self, model):
|
||||
assert isinstance(model, gp.Model)
|
||||
vals = model.cbGetNodeRel(model.getVars())
|
||||
@@ -44,6 +48,7 @@ class GurobiStableSetProblem(Instance):
|
||||
violations += [frozenset(clique)]
|
||||
return violations
|
||||
|
||||
@overrides
|
||||
def build_user_cut(self, model: Any, cid: Hashable) -> Any:
|
||||
assert isinstance(cid, FrozenSet)
|
||||
x = model.getVars()
|
||||
|
||||
@@ -20,43 +20,37 @@ from miplearn.solvers.learning import LearningSolver
|
||||
def test_xy() -> None:
|
||||
features = Features(
|
||||
variables={
|
||||
"x": {
|
||||
0: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[0.0, 0.0],
|
||||
),
|
||||
1: VariableFeatures(
|
||||
category=None,
|
||||
),
|
||||
2: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[1.0, 0.0],
|
||||
),
|
||||
3: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[1.0, 1.0],
|
||||
),
|
||||
}
|
||||
"x[0]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[0.0, 0.0],
|
||||
),
|
||||
"x[1]": VariableFeatures(
|
||||
category=None,
|
||||
),
|
||||
"x[2]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[1.0, 0.0],
|
||||
),
|
||||
"x[3]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[1.0, 1.0],
|
||||
),
|
||||
}
|
||||
)
|
||||
instance = Mock(spec=Instance)
|
||||
instance.features = features
|
||||
sample = TrainingSample(
|
||||
solution={
|
||||
"x": {
|
||||
0: 0.0,
|
||||
1: 1.0,
|
||||
2: 1.0,
|
||||
3: 0.0,
|
||||
}
|
||||
"x[0]": 0.0,
|
||||
"x[1]": 1.0,
|
||||
"x[2]": 1.0,
|
||||
"x[3]": 0.0,
|
||||
},
|
||||
lp_solution={
|
||||
"x": {
|
||||
0: 0.1,
|
||||
1: 0.1,
|
||||
2: 0.1,
|
||||
3: 0.1,
|
||||
}
|
||||
"x[0]": 0.1,
|
||||
"x[1]": 0.1,
|
||||
"x[2]": 0.1,
|
||||
"x[3]": 0.1,
|
||||
},
|
||||
)
|
||||
x_expected = {
|
||||
@@ -73,7 +67,7 @@ def test_xy() -> None:
|
||||
[True, False],
|
||||
]
|
||||
}
|
||||
xy = PrimalSolutionComponent.sample_xy(instance, sample)
|
||||
xy = PrimalSolutionComponent().sample_xy(instance, sample)
|
||||
assert xy is not None
|
||||
x_actual, y_actual = xy
|
||||
assert x_actual == x_expected
|
||||
@@ -83,35 +77,31 @@ def test_xy() -> None:
|
||||
def test_xy_without_lp_solution() -> None:
|
||||
features = Features(
|
||||
variables={
|
||||
"x": {
|
||||
0: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[0.0, 0.0],
|
||||
),
|
||||
1: VariableFeatures(
|
||||
category=None,
|
||||
),
|
||||
2: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[1.0, 0.0],
|
||||
),
|
||||
3: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[1.0, 1.0],
|
||||
),
|
||||
}
|
||||
"x[0]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[0.0, 0.0],
|
||||
),
|
||||
"x[1]": VariableFeatures(
|
||||
category=None,
|
||||
),
|
||||
"x[2]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[1.0, 0.0],
|
||||
),
|
||||
"x[3]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[1.0, 1.0],
|
||||
),
|
||||
}
|
||||
)
|
||||
instance = Mock(spec=Instance)
|
||||
instance.features = features
|
||||
sample = TrainingSample(
|
||||
solution={
|
||||
"x": {
|
||||
0: 0.0,
|
||||
1: 1.0,
|
||||
2: 1.0,
|
||||
3: 0.0,
|
||||
}
|
||||
"x[0]": 0.0,
|
||||
"x[1]": 1.0,
|
||||
"x[2]": 1.0,
|
||||
"x[3]": 0.0,
|
||||
},
|
||||
)
|
||||
x_expected = {
|
||||
@@ -128,7 +118,7 @@ def test_xy_without_lp_solution() -> None:
|
||||
[True, False],
|
||||
]
|
||||
}
|
||||
xy = PrimalSolutionComponent.sample_xy(instance, sample)
|
||||
xy = PrimalSolutionComponent().sample_xy(instance, sample)
|
||||
assert xy is not None
|
||||
x_actual, y_actual = xy
|
||||
assert x_actual == x_expected
|
||||
@@ -150,48 +140,42 @@ def test_predict() -> None:
|
||||
thr.predict = Mock(return_value=[0.75, 0.75])
|
||||
features = Features(
|
||||
variables={
|
||||
"x": {
|
||||
0: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[0.0, 0.0],
|
||||
),
|
||||
1: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[0.0, 2.0],
|
||||
),
|
||||
2: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[2.0, 0.0],
|
||||
),
|
||||
}
|
||||
"x[0]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[0.0, 0.0],
|
||||
),
|
||||
"x[1]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[0.0, 2.0],
|
||||
),
|
||||
"x[2]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[2.0, 0.0],
|
||||
),
|
||||
}
|
||||
)
|
||||
instance = Mock(spec=Instance)
|
||||
instance.features = features
|
||||
sample = TrainingSample(
|
||||
lp_solution={
|
||||
"x": {
|
||||
0: 0.1,
|
||||
1: 0.5,
|
||||
2: 0.9,
|
||||
}
|
||||
"x[0]": 0.1,
|
||||
"x[1]": 0.5,
|
||||
"x[2]": 0.9,
|
||||
}
|
||||
)
|
||||
x, _ = PrimalSolutionComponent.sample_xy(instance, sample)
|
||||
x, _ = PrimalSolutionComponent().sample_xy(instance, sample)
|
||||
comp = PrimalSolutionComponent()
|
||||
comp.classifiers = {"default": clf}
|
||||
comp.thresholds = {"default": thr}
|
||||
solution_actual = comp.sample_predict(instance, sample)
|
||||
pred = comp.sample_predict(instance, sample)
|
||||
clf.predict_proba.assert_called_once()
|
||||
assert_array_equal(x["default"], clf.predict_proba.call_args[0][0])
|
||||
thr.predict.assert_called_once()
|
||||
assert_array_equal(x["default"], thr.predict.call_args[0][0])
|
||||
assert solution_actual == {
|
||||
"x": {
|
||||
0: 0.0,
|
||||
1: None,
|
||||
2: 1.0,
|
||||
}
|
||||
assert pred == {
|
||||
"x[0]": 0.0,
|
||||
"x[1]": None,
|
||||
"x[2]": 1.0,
|
||||
}
|
||||
|
||||
|
||||
@@ -242,36 +226,30 @@ def test_usage():
|
||||
def test_evaluate() -> None:
|
||||
comp = PrimalSolutionComponent()
|
||||
comp.sample_predict = lambda _, __: { # type: ignore
|
||||
"x": {
|
||||
0: 1.0,
|
||||
1: 0.0,
|
||||
2: 0.0,
|
||||
3: None,
|
||||
4: 1.0,
|
||||
}
|
||||
"x[0]": 1.0,
|
||||
"x[1]": 0.0,
|
||||
"x[2]": 0.0,
|
||||
"x[3]": None,
|
||||
"x[4]": 1.0,
|
||||
}
|
||||
features: Features = Features(
|
||||
variables={
|
||||
"x": {
|
||||
0: VariableFeatures(),
|
||||
1: VariableFeatures(),
|
||||
2: VariableFeatures(),
|
||||
3: VariableFeatures(),
|
||||
4: VariableFeatures(),
|
||||
}
|
||||
"x[0]": VariableFeatures(),
|
||||
"x[1]": VariableFeatures(),
|
||||
"x[2]": VariableFeatures(),
|
||||
"x[3]": VariableFeatures(),
|
||||
"x[4]": VariableFeatures(),
|
||||
}
|
||||
)
|
||||
instance = Mock(spec=Instance)
|
||||
instance.features = features
|
||||
sample: TrainingSample = TrainingSample(
|
||||
solution={
|
||||
"x": {
|
||||
0: 1.0,
|
||||
1: 1.0,
|
||||
2: 0.0,
|
||||
3: 1.0,
|
||||
4: 1.0,
|
||||
}
|
||||
"x[0]": 1.0,
|
||||
"x[1]": 1.0,
|
||||
"x[2]": 0.0,
|
||||
"x[3]": 1.0,
|
||||
"x[4]": 1.0,
|
||||
}
|
||||
)
|
||||
ev = comp.sample_evaluate(instance, sample)
|
||||
|
||||
3
tests/fixtures/infeasible.py
vendored
3
tests/fixtures/infeasible.py
vendored
@@ -4,6 +4,7 @@
|
||||
|
||||
from typing import Any
|
||||
|
||||
from overrides import overrides
|
||||
from pyomo import environ as pe
|
||||
|
||||
from miplearn.instance.base import Instance
|
||||
@@ -13,6 +14,7 @@ from tests.solvers import _is_subclass_or_instance
|
||||
|
||||
|
||||
class InfeasiblePyomoInstance(Instance):
|
||||
@overrides
|
||||
def to_model(self) -> pe.ConcreteModel:
|
||||
model = pe.ConcreteModel()
|
||||
model.x = pe.Var([0], domain=pe.Binary)
|
||||
@@ -22,6 +24,7 @@ class InfeasiblePyomoInstance(Instance):
|
||||
|
||||
|
||||
class InfeasibleGurobiInstance(Instance):
|
||||
@overrides
|
||||
def to_model(self) -> Any:
|
||||
import gurobipy as gp
|
||||
from gurobipy import GRB
|
||||
|
||||
@@ -39,13 +39,13 @@ def test_instance():
|
||||
instance = TravelingSalesmanInstance(n_cities, distances)
|
||||
solver = LearningSolver()
|
||||
stats = solver.solve(instance)
|
||||
x = instance.training_data[0].solution["x"]
|
||||
assert x[0, 1] == 1.0
|
||||
assert x[0, 2] == 0.0
|
||||
assert x[0, 3] == 1.0
|
||||
assert x[1, 2] == 1.0
|
||||
assert x[1, 3] == 0.0
|
||||
assert x[2, 3] == 1.0
|
||||
solution = instance.training_data[0].solution
|
||||
assert solution["x[(0, 1)]"] == 1.0
|
||||
assert solution["x[(0, 2)]"] == 0.0
|
||||
assert solution["x[(0, 3)]"] == 1.0
|
||||
assert solution["x[(1, 2)]"] == 1.0
|
||||
assert solution["x[(1, 3)]"] == 0.0
|
||||
assert solution["x[(2, 3)]"] == 1.0
|
||||
assert stats["Lower bound"] == 4.0
|
||||
assert stats["Upper bound"] == 4.0
|
||||
|
||||
@@ -67,12 +67,12 @@ def test_subtour():
|
||||
solver = LearningSolver()
|
||||
solver.solve(instance)
|
||||
assert len(instance.training_data[0].lazy_enforced) > 0
|
||||
x = instance.training_data[0].solution["x"]
|
||||
assert x[0, 1] == 1.0
|
||||
assert x[0, 4] == 1.0
|
||||
assert x[1, 2] == 1.0
|
||||
assert x[2, 3] == 1.0
|
||||
assert x[3, 5] == 1.0
|
||||
assert x[4, 5] == 1.0
|
||||
solution = instance.training_data[0].solution
|
||||
assert solution["x[(0, 1)]"] == 1.0
|
||||
assert solution["x[(0, 4)]"] == 1.0
|
||||
assert solution["x[(1, 2)]"] == 1.0
|
||||
assert solution["x[(2, 3)]"] == 1.0
|
||||
assert solution["x[(3, 5)]"] == 1.0
|
||||
assert solution["x[(4, 5)]"] == 1.0
|
||||
solver.fit([instance])
|
||||
solver.solve(instance)
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -20,24 +20,22 @@ def test_knapsack() -> None:
|
||||
solver.set_instance(instance, model)
|
||||
FeaturesExtractor(solver).extract(instance)
|
||||
assert instance.features.variables == {
|
||||
"x": {
|
||||
0: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[23.0, 505.0],
|
||||
),
|
||||
1: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[26.0, 352.0],
|
||||
),
|
||||
2: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[20.0, 458.0],
|
||||
),
|
||||
3: VariableFeatures(
|
||||
category="default",
|
||||
user_features=[18.0, 220.0],
|
||||
),
|
||||
}
|
||||
"x[0]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[23.0, 505.0],
|
||||
),
|
||||
"x[1]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[26.0, 352.0],
|
||||
),
|
||||
"x[2]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[20.0, 458.0],
|
||||
),
|
||||
"x[3]": VariableFeatures(
|
||||
category="default",
|
||||
user_features=[18.0, 220.0],
|
||||
),
|
||||
}
|
||||
assert instance.features.constraints == {
|
||||
"eq_capacity": ConstraintFeatures(
|
||||
|
||||
Reference in New Issue
Block a user