Make sample_xy an instance method

master
Alinson S. Xavier 5 years ago
parent 54c20382c9
commit cfb17551f1
No known key found for this signature in database
GPG Key ID: DCA0DAD4D2F58624

@ -171,8 +171,8 @@ class StaticLazyConstraintsComponent(Component):
enforced_cids += [category_to_cids[category][i]] enforced_cids += [category_to_cids[category][i]]
return enforced_cids return enforced_cids
@staticmethod
def sample_xy( def sample_xy(
self,
instance: "Instance", instance: "Instance",
sample: TrainingSample, sample: TrainingSample,
) -> Tuple[Dict[Hashable, List[List[float]]], Dict[Hashable, List[List[float]]]]: ) -> Tuple[Dict[Hashable, List[List[float]]], Dict[Hashable, List[List[float]]]]:

@ -73,8 +73,8 @@ class ObjectiveValueComponent(Component):
logger.info(f"{c} regressor not fitted. Skipping.") logger.info(f"{c} regressor not fitted. Skipping.")
return pred return pred
@staticmethod
def sample_xy( def sample_xy(
self,
instance: Instance, instance: Instance,
sample: TrainingSample, sample: TrainingSample,
) -> Tuple[Dict[Hashable, List[List[float]]], Dict[Hashable, List[List[float]]]]: ) -> Tuple[Dict[Hashable, List[List[float]]], Dict[Hashable, List[List[float]]]]:

@ -245,7 +245,7 @@ def test_sample_xy(
"type-a": [[False, True], [False, True], [True, False]], "type-a": [[False, True], [False, True], [True, False]],
"type-b": [[False, True]], "type-b": [[False, True]],
} }
xy = StaticLazyConstraintsComponent.sample_xy(instance, sample) xy = StaticLazyConstraintsComponent().sample_xy(instance, sample)
assert xy is not None assert xy is not None
x_actual, y_actual = xy x_actual, y_actual = xy
assert x_actual == x_expected assert x_actual == x_expected

@ -68,7 +68,7 @@ def test_sample_xy(
"Lower bound": [[1.0]], "Lower bound": [[1.0]],
"Upper bound": [[2.0]], "Upper bound": [[2.0]],
} }
xy = ObjectiveValueComponent.sample_xy(instance, sample) xy = ObjectiveValueComponent().sample_xy(instance, sample)
assert xy is not None assert xy is not None
x_actual, y_actual = xy x_actual, y_actual = xy
assert x_actual == x_expected assert x_actual == x_expected
@ -87,7 +87,7 @@ def test_sample_xy_without_lp(
"Lower bound": [[1.0]], "Lower bound": [[1.0]],
"Upper bound": [[2.0]], "Upper bound": [[2.0]],
} }
xy = ObjectiveValueComponent.sample_xy(instance, sample_without_lp) xy = ObjectiveValueComponent().sample_xy(instance, sample_without_lp)
assert xy is not None assert xy is not None
x_actual, y_actual = xy x_actual, y_actual = xy
assert x_actual == x_expected assert x_actual == x_expected
@ -103,7 +103,7 @@ def test_sample_xy_without_ub(
"Upper bound": [[1.0, 2.0, 3.0]], "Upper bound": [[1.0, 2.0, 3.0]],
} }
y_expected = {"Lower bound": [[1.0]]} y_expected = {"Lower bound": [[1.0]]}
xy = ObjectiveValueComponent.sample_xy(instance, sample_without_ub) xy = ObjectiveValueComponent().sample_xy(instance, sample_without_ub)
assert xy is not None assert xy is not None
x_actual, y_actual = xy x_actual, y_actual = xy
assert x_actual == x_expected assert x_actual == x_expected
@ -180,7 +180,7 @@ def test_sample_predict(
instance: Instance, instance: Instance,
sample: TrainingSample, sample: TrainingSample,
) -> None: ) -> None:
x, y = ObjectiveValueComponent.sample_xy(instance, sample) x, y = ObjectiveValueComponent().sample_xy(instance, sample)
comp = ObjectiveValueComponent() comp = ObjectiveValueComponent()
comp.regressors["Lower bound"] = Mock(spec=Regressor) comp.regressors["Lower bound"] = Mock(spec=Regressor)
comp.regressors["Upper bound"] = Mock(spec=Regressor) comp.regressors["Upper bound"] = Mock(spec=Regressor)
@ -209,7 +209,7 @@ def test_sample_predict_without_ub(
instance: Instance, instance: Instance,
sample_without_ub: TrainingSample, sample_without_ub: TrainingSample,
) -> None: ) -> None:
x, y = ObjectiveValueComponent.sample_xy(instance, sample_without_ub) x, y = ObjectiveValueComponent().sample_xy(instance, sample_without_ub)
comp = ObjectiveValueComponent() comp = ObjectiveValueComponent()
comp.regressors["Lower bound"] = Mock(spec=Regressor) comp.regressors["Lower bound"] = Mock(spec=Regressor)
comp.regressors["Lower bound"].predict = Mock( # type: ignore comp.regressors["Lower bound"].predict = Mock( # type: ignore

@ -0,0 +1,31 @@
# MIPLearn: Extensible Framework for Learning-Enhanced Mixed-Integer Optimization
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
# Released under the modified BSD license. See COPYING.md for more details.
from typing import Any, List
import pytest
from networkx import Graph
import networkx as nx
from scipy.stats import randint
from miplearn import Instance
from miplearn.problems.stab import MaxWeightStableSetGenerator
class GurobiStableSetProblem(Instance):
def __init__(self, graph: Graph) -> None:
super().__init__()
self.graph = graph
def to_model(self) -> Any:
pass
@pytest.fixture
def instance() -> Instance:
graph = nx.generators.random_graphs.binomial_graph(50, 0.5)
return GurobiStableSetProblem(graph)
def test_usage(instance: Instance) -> None:
pass
Loading…
Cancel
Save