From cfb17551f12900966ff8cf3fb8014e63a4b07d10 Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Tue, 6 Apr 2021 08:27:37 -0500 Subject: [PATCH] Make sample_xy an instance method --- miplearn/components/lazy_static.py | 2 +- miplearn/components/objective.py | 2 +- tests/components/test_lazy_static.py | 2 +- tests/components/test_objective.py | 10 ++++----- tests/components/test_user_cuts.py | 31 ++++++++++++++++++++++++++++ 5 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 tests/components/test_user_cuts.py diff --git a/miplearn/components/lazy_static.py b/miplearn/components/lazy_static.py index e56d5aa..c0cf04f 100644 --- a/miplearn/components/lazy_static.py +++ b/miplearn/components/lazy_static.py @@ -171,8 +171,8 @@ class StaticLazyConstraintsComponent(Component): enforced_cids += [category_to_cids[category][i]] return enforced_cids - @staticmethod def sample_xy( + self, instance: "Instance", sample: TrainingSample, ) -> Tuple[Dict[Hashable, List[List[float]]], Dict[Hashable, List[List[float]]]]: diff --git a/miplearn/components/objective.py b/miplearn/components/objective.py index 522ea2b..b54cde6 100644 --- a/miplearn/components/objective.py +++ b/miplearn/components/objective.py @@ -73,8 +73,8 @@ class ObjectiveValueComponent(Component): logger.info(f"{c} regressor not fitted. Skipping.") return pred - @staticmethod def sample_xy( + self, instance: Instance, sample: TrainingSample, ) -> Tuple[Dict[Hashable, List[List[float]]], Dict[Hashable, List[List[float]]]]: diff --git a/tests/components/test_lazy_static.py b/tests/components/test_lazy_static.py index 61d7751..4d62b5c 100644 --- a/tests/components/test_lazy_static.py +++ b/tests/components/test_lazy_static.py @@ -245,7 +245,7 @@ def test_sample_xy( "type-a": [[False, True], [False, True], [True, False]], "type-b": [[False, True]], } - xy = StaticLazyConstraintsComponent.sample_xy(instance, sample) + xy = StaticLazyConstraintsComponent().sample_xy(instance, sample) assert xy is not None x_actual, y_actual = xy assert x_actual == x_expected diff --git a/tests/components/test_objective.py b/tests/components/test_objective.py index 442de2e..24ee154 100644 --- a/tests/components/test_objective.py +++ b/tests/components/test_objective.py @@ -68,7 +68,7 @@ def test_sample_xy( "Lower bound": [[1.0]], "Upper bound": [[2.0]], } - xy = ObjectiveValueComponent.sample_xy(instance, sample) + xy = ObjectiveValueComponent().sample_xy(instance, sample) assert xy is not None x_actual, y_actual = xy assert x_actual == x_expected @@ -87,7 +87,7 @@ def test_sample_xy_without_lp( "Lower bound": [[1.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 x_actual, y_actual = xy assert x_actual == x_expected @@ -103,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(instance, 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 @@ -180,7 +180,7 @@ def test_sample_predict( instance: Instance, sample: TrainingSample, ) -> None: - x, y = ObjectiveValueComponent.sample_xy(instance, sample) + x, y = ObjectiveValueComponent().sample_xy(instance, sample) comp = ObjectiveValueComponent() comp.regressors["Lower bound"] = Mock(spec=Regressor) comp.regressors["Upper bound"] = Mock(spec=Regressor) @@ -209,7 +209,7 @@ def test_sample_predict_without_ub( instance: Instance, sample_without_ub: TrainingSample, ) -> None: - x, y = ObjectiveValueComponent.sample_xy(instance, 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 diff --git a/tests/components/test_user_cuts.py b/tests/components/test_user_cuts.py new file mode 100644 index 0000000..885e510 --- /dev/null +++ b/tests/components/test_user_cuts.py @@ -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