Add ObjectiveValueComponent.xy

This commit is contained in:
2021-03-30 17:17:29 -05:00
parent 9266743940
commit e8adeb28a3
5 changed files with 46 additions and 4 deletions

View File

@@ -11,9 +11,33 @@ from numpy.testing import assert_array_equal
from miplearn.instance import Instance
from miplearn.classifiers import Regressor
from miplearn.components.objective import ObjectiveValueComponent
from miplearn.types import TrainingSample
from tests.fixtures.knapsack import get_test_pyomo_instances
def test_xy() -> None:
instance = cast(Instance, Mock(spec=Instance))
instance.get_instance_features = Mock( # type: ignore
return_value=[1.0, 2.0],
)
sample: TrainingSample = {
"Lower bound": 1.0,
"Upper bound": 2.0,
"LP value": 3.0,
}
x_expected = {
"Lower bound": [[1.0, 2.0, 3.0]],
"Upper bound": [[1.0, 2.0, 3.0]],
}
y_expected = {
"Lower bound": [[1.0]],
"Upper bound": [[2.0]],
}
x_actual, y_actual = ObjectiveValueComponent.xy(instance, sample)
assert x_actual == x_expected
assert y_actual == y_expected
def test_x_y_predict() -> None:
# Construct instance
instance = cast(Instance, Mock(spec=Instance))

View File

@@ -69,7 +69,7 @@ def test_xy_with_lp_solution() -> None:
]
)
}
x_actual, y_actual = comp.xy(instance, sample)
x_actual, y_actual = PrimalSolutionComponent.xy(instance, sample)
assert len(x_actual.keys()) == 1
assert len(y_actual.keys()) == 1
assert_array_equal(x_actual["default"], x_expected["default"])