Update ObjectiveValueComponent

This commit is contained in:
2021-04-13 07:53:23 -05:00
parent a9dcdb8e4e
commit b5411b8950
2 changed files with 23 additions and 183 deletions

View File

@@ -10,38 +10,12 @@ from numpy.testing import assert_array_equal
from miplearn.classifiers import Regressor
from miplearn.components.objective import ObjectiveValueComponent
from miplearn.features import TrainingSample, InstanceFeatures, Features, Sample
from miplearn.instance.base import Instance
from miplearn.features import InstanceFeatures, Features, Sample
from miplearn.solvers.internal import MIPSolveStats, LPSolveStats
from miplearn.solvers.learning import LearningSolver
from miplearn.solvers.pyomo.gurobi import GurobiPyomoSolver
@pytest.fixture
def instance_old(features_old: Features) -> Instance:
instance = Mock(spec=Instance)
instance.features = features_old
return instance
@pytest.fixture
def features_old() -> Features:
return Features(
instance=InstanceFeatures(
user_features=[1.0, 2.0],
)
)
@pytest.fixture
def sample_old() -> TrainingSample:
return TrainingSample(
lower_bound=1.0,
upper_bound=2.0,
lp_value=3.0,
)
@pytest.fixture
def sample() -> Sample:
sample = Sample(
@@ -63,22 +37,6 @@ def sample() -> Sample:
return sample
@pytest.fixture
def sample_without_lp() -> TrainingSample:
return TrainingSample(
lower_bound=1.0,
upper_bound=2.0,
)
@pytest.fixture
def sample_without_ub_old() -> TrainingSample:
return TrainingSample(
lower_bound=1.0,
lp_value=3.0,
)
def test_sample_xy(sample: Sample) -> None:
x_expected = {
"Lower bound": [[1.0, 2.0, 3.0]],
@@ -95,41 +53,6 @@ def test_sample_xy(sample: Sample) -> None:
assert y_actual == y_expected
def test_sample_xy_without_lp_old(
instance_old: Instance,
sample_without_lp: TrainingSample,
) -> None:
x_expected = {
"Lower bound": [[1.0, 2.0]],
"Upper bound": [[1.0, 2.0]],
}
y_expected = {
"Lower bound": [[1.0]],
"Upper bound": [[2.0]],
}
xy = ObjectiveValueComponent().sample_xy_old(instance_old, sample_without_lp)
assert xy is not None
x_actual, y_actual = xy
assert x_actual == x_expected
assert y_actual == y_expected
def test_sample_xy_without_ub_old(
instance_old: Instance,
sample_without_ub_old: TrainingSample,
) -> None:
x_expected = {
"Lower bound": [[1.0, 2.0, 3.0]],
"Upper bound": [[1.0, 2.0, 3.0]],
}
y_expected = {"Lower bound": [[1.0]]}
xy = ObjectiveValueComponent().sample_xy_old(instance_old, sample_without_ub_old)
assert xy is not None
x_actual, y_actual = xy
assert x_actual == x_expected
assert y_actual == y_expected
def test_fit_xy() -> None:
x: Dict[Hashable, np.ndarray] = {
"Lower bound": np.array([[0.0, 0.0], [1.0, 2.0]]),
@@ -168,39 +91,8 @@ def test_fit_xy() -> None:
)
def test_fit_xy_without_ub() -> None:
x: Dict[Hashable, np.ndarray] = {
"Lower bound": np.array([[0.0, 0.0], [1.0, 2.0]]),
"Upper bound": np.array([[0.0, 0.0], [1.0, 2.0]]),
}
y: Dict[Hashable, np.ndarray] = {
"Lower bound": np.array([[100.0]]),
}
reg = Mock(spec=Regressor)
reg.clone = Mock(side_effect=lambda: Mock(spec=Regressor))
comp = ObjectiveValueComponent(regressor=reg)
assert "Upper bound" not in comp.regressors
assert "Lower bound" not in comp.regressors
comp.fit_xy(x, y)
assert reg.clone.call_count == 1
assert "Upper bound" not in comp.regressors
assert "Lower bound" in comp.regressors
assert comp.regressors["Lower bound"].fit.call_count == 1 # type: ignore
assert_array_equal(
comp.regressors["Lower bound"].fit.call_args[0][0], # type: ignore
x["Lower bound"],
)
assert_array_equal(
comp.regressors["Lower bound"].fit.call_args[0][1], # type: ignore
y["Lower bound"],
)
def test_sample_predict(
instance_old: Instance,
sample_old: TrainingSample,
) -> None:
x, y = ObjectiveValueComponent().sample_xy_old(instance_old, sample_old)
def test_sample_predict(sample: Sample) -> None:
x, y = ObjectiveValueComponent().sample_xy(None, sample)
comp = ObjectiveValueComponent()
comp.regressors["Lower bound"] = Mock(spec=Regressor)
comp.regressors["Upper bound"] = Mock(spec=Regressor)
@@ -210,7 +102,7 @@ def test_sample_predict(
comp.regressors["Upper bound"].predict = Mock( # type: ignore
side_effect=lambda _: np.array([[60.0]])
)
pred = comp.sample_predict_old(instance_old, sample_old)
pred = comp.sample_predict(sample)
assert pred == {
"Lower bound": 50.0,
"Upper bound": 60.0,
@@ -225,36 +117,13 @@ def test_sample_predict(
)
def test_sample_predict_without_ub_old(
instance_old: Instance,
sample_without_ub_old: TrainingSample,
) -> None:
x, y = ObjectiveValueComponent().sample_xy_old(instance_old, sample_without_ub_old)
comp = ObjectiveValueComponent()
comp.regressors["Lower bound"] = Mock(spec=Regressor)
comp.regressors["Lower bound"].predict = Mock( # type: ignore
side_effect=lambda _: np.array([[50.0]])
)
pred = comp.sample_predict_old(instance_old, sample_without_ub_old)
assert pred == {
"Lower bound": 50.0,
}
assert_array_equal(
comp.regressors["Lower bound"].predict.call_args[0][0], # type: ignore
x["Lower bound"],
)
def test_sample_evaluate_old(
instance_old: Instance,
sample_old: TrainingSample,
) -> None:
def test_sample_evaluate(sample: Sample) -> None:
comp = ObjectiveValueComponent()
comp.regressors["Lower bound"] = Mock(spec=Regressor)
comp.regressors["Lower bound"].predict = lambda _: np.array([[1.05]]) # type: ignore
comp.regressors["Upper bound"] = Mock(spec=Regressor)
comp.regressors["Upper bound"].predict = lambda _: np.array([[2.50]]) # type: ignore
ev = comp.sample_evaluate_old(instance_old, sample_old)
ev = comp.sample_evaluate(None, sample)
assert ev == {
"Lower bound": {
"Actual value": 1.0,