Make sample_ method accept instance

This commit is contained in:
2021-04-06 06:48:47 -05:00
parent bb91c83187
commit c6aee4f90d
8 changed files with 91 additions and 72 deletions

View File

@@ -7,7 +7,7 @@ from unittest.mock import Mock
import pytest
from numpy.testing import assert_array_equal
from miplearn import GurobiPyomoSolver, LearningSolver, Regressor
from miplearn import GurobiPyomoSolver, LearningSolver, Regressor, Instance
from miplearn.components.objective import ObjectiveValueComponent
from miplearn.features import TrainingSample, InstanceFeatures, Features
from tests.fixtures.knapsack import get_knapsack_instance
@@ -15,6 +15,13 @@ from tests.fixtures.knapsack import get_knapsack_instance
import numpy as np
@pytest.fixture
def instance(features: Features) -> Instance:
instance = Mock(spec=Instance)
instance.features = features
return instance
@pytest.fixture
def features() -> Features:
return Features(
@@ -50,7 +57,7 @@ def sample_without_ub() -> TrainingSample:
def test_sample_xy(
features: Features,
instance: Instance,
sample: TrainingSample,
) -> None:
x_expected = {
@@ -61,7 +68,7 @@ def test_sample_xy(
"Lower bound": [[1.0]],
"Upper bound": [[2.0]],
}
xy = ObjectiveValueComponent.sample_xy(features, sample)
xy = ObjectiveValueComponent.sample_xy(instance, sample)
assert xy is not None
x_actual, y_actual = xy
assert x_actual == x_expected
@@ -69,7 +76,7 @@ def test_sample_xy(
def test_sample_xy_without_lp(
features: Features,
instance: Instance,
sample_without_lp: TrainingSample,
) -> None:
x_expected = {
@@ -80,7 +87,7 @@ def test_sample_xy_without_lp(
"Lower bound": [[1.0]],
"Upper bound": [[2.0]],
}
xy = ObjectiveValueComponent.sample_xy(features, sample_without_lp)
xy = ObjectiveValueComponent.sample_xy(instance, sample_without_lp)
assert xy is not None
x_actual, y_actual = xy
assert x_actual == x_expected
@@ -88,7 +95,7 @@ def test_sample_xy_without_lp(
def test_sample_xy_without_ub(
features: Features,
instance: Instance,
sample_without_ub: TrainingSample,
) -> None:
x_expected = {
@@ -96,7 +103,7 @@ def test_sample_xy_without_ub(
"Upper bound": [[1.0, 2.0, 3.0]],
}
y_expected = {"Lower bound": [[1.0]]}
xy = ObjectiveValueComponent.sample_xy(features, sample_without_ub)
xy = ObjectiveValueComponent.sample_xy(instance, sample_without_ub)
assert xy is not None
x_actual, y_actual = xy
assert x_actual == x_expected
@@ -170,10 +177,10 @@ def test_fit_xy_without_ub() -> None:
def test_sample_predict(
features: Features,
instance: Instance,
sample: TrainingSample,
) -> None:
x, y = ObjectiveValueComponent.sample_xy(features, sample)
x, y = ObjectiveValueComponent.sample_xy(instance, sample)
comp = ObjectiveValueComponent()
comp.regressors["Lower bound"] = Mock(spec=Regressor)
comp.regressors["Upper bound"] = Mock(spec=Regressor)
@@ -183,7 +190,7 @@ def test_sample_predict(
comp.regressors["Upper bound"].predict = Mock( # type: ignore
side_effect=lambda _: np.array([[60.0]])
)
pred = comp.sample_predict(features, sample)
pred = comp.sample_predict(instance, sample)
assert pred == {
"Lower bound": 50.0,
"Upper bound": 60.0,
@@ -199,16 +206,16 @@ def test_sample_predict(
def test_sample_predict_without_ub(
features: Features,
instance: Instance,
sample_without_ub: TrainingSample,
) -> None:
x, y = ObjectiveValueComponent.sample_xy(features, sample_without_ub)
x, y = ObjectiveValueComponent.sample_xy(instance, sample_without_ub)
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(features, sample_without_ub)
pred = comp.sample_predict(instance, sample_without_ub)
assert pred == {
"Lower bound": 50.0,
}
@@ -218,13 +225,13 @@ def test_sample_predict_without_ub(
)
def test_sample_evaluate(features: Features, sample: TrainingSample) -> None:
def test_sample_evaluate(instance: Instance, sample: TrainingSample) -> 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(features, sample)
ev = comp.sample_evaluate(instance, sample)
assert ev == {
"Lower bound": {
"Actual value": 1.0,