|
|
@ -1,22 +1,22 @@
|
|
|
|
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
|
|
|
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
|
|
|
|
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
|
|
|
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
|
|
|
# Released under the modified BSD license. See COPYING.md for more details.
|
|
|
|
# Released under the modified BSD license. See COPYING.md for more details.
|
|
|
|
from typing import cast, List
|
|
|
|
|
|
|
|
from unittest.mock import Mock, call
|
|
|
|
from typing import cast
|
|
|
|
|
|
|
|
from unittest.mock import Mock
|
|
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import numpy as np
|
|
|
|
from numpy.testing import assert_array_equal
|
|
|
|
from numpy.testing import assert_array_equal
|
|
|
|
|
|
|
|
|
|
|
|
from miplearn import Classifier
|
|
|
|
from miplearn import Classifier
|
|
|
|
from miplearn.classifiers.threshold import Threshold, MinPrecisionThreshold
|
|
|
|
from miplearn.classifiers.threshold import Threshold
|
|
|
|
from miplearn.components.primal import PrimalSolutionComponent
|
|
|
|
from miplearn.components.primal import PrimalSolutionComponent
|
|
|
|
from miplearn.instance import Instance
|
|
|
|
from miplearn.instance import Instance
|
|
|
|
from miplearn.types import TrainingSample
|
|
|
|
from miplearn.types import TrainingSample, Features
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_xy_sample_with_lp_solution() -> None:
|
|
|
|
def test_xy_sample_with_lp_solution() -> None:
|
|
|
|
instance = cast(Instance, Mock(spec=Instance))
|
|
|
|
features: Features = {
|
|
|
|
instance.features = {
|
|
|
|
|
|
|
|
"Variables": {
|
|
|
|
"Variables": {
|
|
|
|
"x": {
|
|
|
|
"x": {
|
|
|
|
0: {
|
|
|
|
0: {
|
|
|
@ -56,34 +56,28 @@ def test_xy_sample_with_lp_solution() -> None:
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x_expected = {
|
|
|
|
x_expected = {
|
|
|
|
"default": np.array(
|
|
|
|
"default": [
|
|
|
|
[
|
|
|
|
|
|
|
|
[0.0, 0.0, 0.1],
|
|
|
|
[0.0, 0.0, 0.1],
|
|
|
|
[1.0, 0.0, 0.1],
|
|
|
|
[1.0, 0.0, 0.1],
|
|
|
|
[1.0, 1.0, 0.1],
|
|
|
|
[1.0, 1.0, 0.1],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
y_expected = {
|
|
|
|
y_expected = {
|
|
|
|
"default": np.array(
|
|
|
|
"default": [
|
|
|
|
[
|
|
|
|
|
|
|
|
[True, False],
|
|
|
|
[True, False],
|
|
|
|
[False, True],
|
|
|
|
[False, True],
|
|
|
|
[True, False],
|
|
|
|
[True, False],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x_actual, y_actual = PrimalSolutionComponent.xy_sample(instance, sample)
|
|
|
|
xy = PrimalSolutionComponent.xy_sample(features, sample)
|
|
|
|
assert len(x_actual.keys()) == 1
|
|
|
|
assert xy is not None
|
|
|
|
assert len(y_actual.keys()) == 1
|
|
|
|
x_actual, y_actual = xy
|
|
|
|
assert_array_equal(x_actual["default"], x_expected["default"])
|
|
|
|
assert x_actual == x_expected
|
|
|
|
assert_array_equal(y_actual["default"], y_expected["default"])
|
|
|
|
assert y_actual == y_expected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_xy_sample_without_lp_solution() -> None:
|
|
|
|
def test_xy_sample_without_lp_solution() -> None:
|
|
|
|
comp = PrimalSolutionComponent()
|
|
|
|
features: Features = {
|
|
|
|
instance = cast(Instance, Mock(spec=Instance))
|
|
|
|
|
|
|
|
instance.features = {
|
|
|
|
|
|
|
|
"Variables": {
|
|
|
|
"Variables": {
|
|
|
|
"x": {
|
|
|
|
"x": {
|
|
|
|
0: {
|
|
|
|
0: {
|
|
|
@ -115,28 +109,24 @@ def test_xy_sample_without_lp_solution() -> None:
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x_expected = {
|
|
|
|
x_expected = {
|
|
|
|
"default": np.array(
|
|
|
|
"default": [
|
|
|
|
[
|
|
|
|
|
|
|
|
[0.0, 0.0],
|
|
|
|
[0.0, 0.0],
|
|
|
|
[1.0, 0.0],
|
|
|
|
[1.0, 0.0],
|
|
|
|
[1.0, 1.0],
|
|
|
|
[1.0, 1.0],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
y_expected = {
|
|
|
|
y_expected = {
|
|
|
|
"default": np.array(
|
|
|
|
"default": [
|
|
|
|
[
|
|
|
|
|
|
|
|
[True, False],
|
|
|
|
[True, False],
|
|
|
|
[False, True],
|
|
|
|
[False, True],
|
|
|
|
[True, False],
|
|
|
|
[True, False],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x_actual, y_actual = comp.xy_sample(instance, sample)
|
|
|
|
xy = PrimalSolutionComponent.xy_sample(features, sample)
|
|
|
|
assert len(x_actual.keys()) == 1
|
|
|
|
assert xy is not None
|
|
|
|
assert len(y_actual.keys()) == 1
|
|
|
|
x_actual, y_actual = xy
|
|
|
|
assert_array_equal(x_actual["default"], x_expected["default"])
|
|
|
|
assert x_actual == x_expected
|
|
|
|
assert_array_equal(y_actual["default"], y_expected["default"])
|
|
|
|
assert y_actual == y_expected
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_predict() -> None:
|
|
|
|
def test_predict() -> None:
|
|
|
|