mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Make Sample abstract; create MemorySample
This commit is contained in:
@@ -2,10 +2,25 @@
|
|||||||
# Copyright (C) 2020-2021, UChicago Argonne, LLC. All rights reserved.
|
# Copyright (C) 2020-2021, 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 abc import ABC, abstractmethod
|
||||||
from typing import Dict, Optional, Any
|
from typing import Dict, Optional, Any
|
||||||
|
|
||||||
|
|
||||||
class Sample:
|
class Sample(ABC):
|
||||||
|
"""Abstract dictionary-like class that stores training data."""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get(self, key: str) -> Optional[Any]:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def put(self, key: str, value: Any) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MemorySample(Sample):
|
||||||
|
"""Dictionary-like class that stores training data in-memory."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
data: Optional[Dict[str, Any]] = None,
|
data: Optional[Dict[str, Any]] = None,
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ from miplearn.components.dynamic_user_cuts import UserCutsComponent
|
|||||||
from miplearn.components.objective import ObjectiveValueComponent
|
from miplearn.components.objective import ObjectiveValueComponent
|
||||||
from miplearn.components.primal import PrimalSolutionComponent
|
from miplearn.components.primal import PrimalSolutionComponent
|
||||||
from miplearn.features.extractor import FeaturesExtractor
|
from miplearn.features.extractor import FeaturesExtractor
|
||||||
from miplearn.features.sample import Sample
|
from miplearn.features.sample import Sample, MemorySample
|
||||||
from miplearn.instance.base import Instance
|
from miplearn.instance.base import Instance
|
||||||
from miplearn.instance.picklegz import PickleGzInstance
|
from miplearn.instance.picklegz import PickleGzInstance
|
||||||
from miplearn.solvers import _RedirectOutput
|
from miplearn.solvers import _RedirectOutput
|
||||||
@@ -150,7 +150,7 @@ class LearningSolver:
|
|||||||
|
|
||||||
# Initialize training sample
|
# Initialize training sample
|
||||||
# -------------------------------------------------------
|
# -------------------------------------------------------
|
||||||
sample = Sample()
|
sample = MemorySample()
|
||||||
instance.push_sample(sample)
|
instance.push_sample(sample)
|
||||||
|
|
||||||
# Initialize stats
|
# Initialize stats
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from miplearn.classifiers import Classifier
|
|||||||
from miplearn.classifiers.threshold import MinProbabilityThreshold
|
from miplearn.classifiers.threshold import MinProbabilityThreshold
|
||||||
from miplearn.components import classifier_evaluation_dict
|
from miplearn.components import classifier_evaluation_dict
|
||||||
from miplearn.components.dynamic_lazy import DynamicLazyConstraintsComponent
|
from miplearn.components.dynamic_lazy import DynamicLazyConstraintsComponent
|
||||||
from miplearn.features.sample import Sample
|
from miplearn.features.sample import Sample, MemorySample
|
||||||
from miplearn.instance.base import Instance
|
from miplearn.instance.base import Instance
|
||||||
from miplearn.solvers.tests import assert_equals
|
from miplearn.solvers.tests import assert_equals
|
||||||
|
|
||||||
@@ -22,13 +22,13 @@ E = 0.1
|
|||||||
def training_instances() -> List[Instance]:
|
def training_instances() -> List[Instance]:
|
||||||
instances = [cast(Instance, Mock(spec=Instance)) for _ in range(2)]
|
instances = [cast(Instance, Mock(spec=Instance)) for _ in range(2)]
|
||||||
samples_0 = [
|
samples_0 = [
|
||||||
Sample(
|
MemorySample(
|
||||||
{
|
{
|
||||||
"lazy_enforced": {"c1", "c2"},
|
"lazy_enforced": {"c1", "c2"},
|
||||||
"instance_features_user": [5.0],
|
"instance_features_user": [5.0],
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
Sample(
|
MemorySample(
|
||||||
{
|
{
|
||||||
"lazy_enforced": {"c2", "c3"},
|
"lazy_enforced": {"c2", "c3"},
|
||||||
"instance_features_user": [5.0],
|
"instance_features_user": [5.0],
|
||||||
@@ -53,7 +53,7 @@ def training_instances() -> List[Instance]:
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
samples_1 = [
|
samples_1 = [
|
||||||
Sample(
|
MemorySample(
|
||||||
{
|
{
|
||||||
"lazy_enforced": {"c3", "c4"},
|
"lazy_enforced": {"c3", "c4"},
|
||||||
"instance_features_user": [8.0],
|
"instance_features_user": [8.0],
|
||||||
|
|||||||
@@ -10,14 +10,14 @@ from numpy.testing import assert_array_equal
|
|||||||
|
|
||||||
from miplearn.classifiers import Regressor
|
from miplearn.classifiers import Regressor
|
||||||
from miplearn.components.objective import ObjectiveValueComponent
|
from miplearn.components.objective import ObjectiveValueComponent
|
||||||
from miplearn.features.sample import Sample
|
from miplearn.features.sample import Sample, MemorySample
|
||||||
from miplearn.solvers.learning import LearningSolver
|
from miplearn.solvers.learning import LearningSolver
|
||||||
from miplearn.solvers.pyomo.gurobi import GurobiPyomoSolver
|
from miplearn.solvers.pyomo.gurobi import GurobiPyomoSolver
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def sample() -> Sample:
|
def sample() -> Sample:
|
||||||
sample = Sample(
|
sample = MemorySample(
|
||||||
{
|
{
|
||||||
"mip_lower_bound": 1.0,
|
"mip_lower_bound": 1.0,
|
||||||
"mip_upper_bound": 2.0,
|
"mip_upper_bound": 2.0,
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from miplearn.classifiers import Classifier
|
|||||||
from miplearn.classifiers.threshold import Threshold
|
from miplearn.classifiers.threshold import Threshold
|
||||||
from miplearn.components import classifier_evaluation_dict
|
from miplearn.components import classifier_evaluation_dict
|
||||||
from miplearn.components.primal import PrimalSolutionComponent
|
from miplearn.components.primal import PrimalSolutionComponent
|
||||||
from miplearn.features.sample import Sample
|
from miplearn.features.sample import Sample, MemorySample
|
||||||
from miplearn.problems.tsp import TravelingSalesmanGenerator
|
from miplearn.problems.tsp import TravelingSalesmanGenerator
|
||||||
from miplearn.solvers.learning import LearningSolver
|
from miplearn.solvers.learning import LearningSolver
|
||||||
from miplearn.solvers.tests import assert_equals
|
from miplearn.solvers.tests import assert_equals
|
||||||
@@ -20,7 +20,7 @@ from miplearn.solvers.tests import assert_equals
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def sample() -> Sample:
|
def sample() -> Sample:
|
||||||
sample = Sample(
|
sample = MemorySample(
|
||||||
{
|
{
|
||||||
"var_names": ["x[0]", "x[1]", "x[2]", "x[3]"],
|
"var_names": ["x[0]", "x[1]", "x[2]", "x[3]"],
|
||||||
"var_categories": ["default", None, "default", "default"],
|
"var_categories": ["default", None, "default", "default"],
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from numpy.testing import assert_array_equal
|
|||||||
from miplearn.classifiers import Classifier
|
from miplearn.classifiers import Classifier
|
||||||
from miplearn.classifiers.threshold import Threshold, MinProbabilityThreshold
|
from miplearn.classifiers.threshold import Threshold, MinProbabilityThreshold
|
||||||
from miplearn.components.static_lazy import StaticLazyConstraintsComponent
|
from miplearn.components.static_lazy import StaticLazyConstraintsComponent
|
||||||
from miplearn.features.sample import Sample
|
from miplearn.features.sample import Sample, MemorySample
|
||||||
from miplearn.instance.base import Instance
|
from miplearn.instance.base import Instance
|
||||||
from miplearn.solvers.internal import InternalSolver, Constraints
|
from miplearn.solvers.internal import InternalSolver, Constraints
|
||||||
from miplearn.solvers.learning import LearningSolver
|
from miplearn.solvers.learning import LearningSolver
|
||||||
@@ -22,7 +22,7 @@ from miplearn.types import (
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def sample() -> Sample:
|
def sample() -> Sample:
|
||||||
sample = Sample(
|
sample = MemorySample(
|
||||||
{
|
{
|
||||||
"constr_categories": [
|
"constr_categories": [
|
||||||
"type-a",
|
"type-a",
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from miplearn.features.extractor import FeaturesExtractor
|
from miplearn.features.extractor import FeaturesExtractor
|
||||||
from miplearn.features.sample import Sample
|
from miplearn.features.sample import Sample, MemorySample
|
||||||
from miplearn.solvers.internal import Variables, Constraints
|
from miplearn.solvers.internal import Variables, Constraints
|
||||||
from miplearn.solvers.gurobi import GurobiSolver
|
from miplearn.solvers.gurobi import GurobiSolver
|
||||||
from miplearn.solvers.tests import assert_equals
|
from miplearn.solvers.tests import assert_equals
|
||||||
@@ -19,7 +19,7 @@ def test_knapsack() -> None:
|
|||||||
model = instance.to_model()
|
model = instance.to_model()
|
||||||
solver.set_instance(instance, model)
|
solver.set_instance(instance, model)
|
||||||
extractor = FeaturesExtractor()
|
extractor = FeaturesExtractor()
|
||||||
sample = Sample()
|
sample = MemorySample()
|
||||||
|
|
||||||
# after-load
|
# after-load
|
||||||
# -------------------------------------------------------
|
# -------------------------------------------------------
|
||||||
|
|||||||
Reference in New Issue
Block a user