mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Move feature classes to features.py
This commit is contained in:
@@ -7,7 +7,8 @@ from typing import Any, List, TYPE_CHECKING, Tuple, Dict, Hashable
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from miplearn.instance import Instance
|
from miplearn.instance import Instance
|
||||||
from miplearn.types import LearningSolveStats, TrainingSample, Features
|
from miplearn.types import LearningSolveStats
|
||||||
|
from miplearn.features import TrainingSample, Features
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from miplearn.solvers.learning import LearningSolver
|
from miplearn.solvers.learning import LearningSolver
|
||||||
@@ -49,7 +50,7 @@ class Component:
|
|||||||
their own statistics here. For example, PrimalSolutionComponent adds
|
their own statistics here. For example, PrimalSolutionComponent adds
|
||||||
statistics regarding the number of predicted variables. All statistics in
|
statistics regarding the number of predicted variables. All statistics in
|
||||||
this dictionary are exported to the benchmark CSV file.
|
this dictionary are exported to the benchmark CSV file.
|
||||||
features: Features
|
features: miplearn.features.Features
|
||||||
Features describing the model.
|
Features describing the model.
|
||||||
training_data: TrainingSample
|
training_data: TrainingSample
|
||||||
A dictionary containing data that may be useful for training machine
|
A dictionary containing data that may be useful for training machine
|
||||||
|
|||||||
@@ -11,7 +11,8 @@ from miplearn import Classifier
|
|||||||
from miplearn.classifiers.counting import CountingClassifier
|
from miplearn.classifiers.counting import CountingClassifier
|
||||||
from miplearn.classifiers.threshold import MinProbabilityThreshold, Threshold
|
from miplearn.classifiers.threshold import MinProbabilityThreshold, Threshold
|
||||||
from miplearn.components.component import Component
|
from miplearn.components.component import Component
|
||||||
from miplearn.types import TrainingSample, Features, LearningSolveStats
|
from miplearn.types import LearningSolveStats
|
||||||
|
from miplearn.features import TrainingSample, Features
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ from miplearn.classifiers import Regressor
|
|||||||
from miplearn.classifiers.sklearn import ScikitLearnRegressor
|
from miplearn.classifiers.sklearn import ScikitLearnRegressor
|
||||||
from miplearn.components.component import Component
|
from miplearn.components.component import Component
|
||||||
from miplearn.instance import Instance
|
from miplearn.instance import Instance
|
||||||
from miplearn.types import TrainingSample, LearningSolveStats, Features
|
from miplearn.types import LearningSolveStats
|
||||||
|
from miplearn.features import TrainingSample, Features
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from miplearn.solvers.learning import LearningSolver
|
from miplearn.solvers.learning import LearningSolver
|
||||||
|
|||||||
@@ -22,11 +22,10 @@ from miplearn.components import classifier_evaluation_dict
|
|||||||
from miplearn.components.component import Component
|
from miplearn.components.component import Component
|
||||||
from miplearn.instance import Instance
|
from miplearn.instance import Instance
|
||||||
from miplearn.types import (
|
from miplearn.types import (
|
||||||
TrainingSample,
|
|
||||||
Solution,
|
Solution,
|
||||||
LearningSolveStats,
|
LearningSolveStats,
|
||||||
Features,
|
|
||||||
)
|
)
|
||||||
|
from miplearn.features import TrainingSample, Features
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|||||||
@@ -4,20 +4,57 @@
|
|||||||
|
|
||||||
import collections
|
import collections
|
||||||
import numbers
|
import numbers
|
||||||
from typing import TYPE_CHECKING, Dict
|
from dataclasses import dataclass
|
||||||
|
from typing import TYPE_CHECKING, Dict, Optional, Set, List, Hashable
|
||||||
|
|
||||||
from miplearn.types import (
|
from miplearn.types import VarIndex, Solution
|
||||||
Features,
|
|
||||||
ConstraintFeatures,
|
|
||||||
InstanceFeatures,
|
|
||||||
VariableFeatures,
|
|
||||||
VarIndex,
|
|
||||||
)
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from miplearn import InternalSolver, Instance
|
from miplearn import InternalSolver, Instance
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class TrainingSample:
|
||||||
|
lp_log: Optional[str] = None
|
||||||
|
lp_solution: Optional[Solution] = None
|
||||||
|
lp_value: Optional[float] = None
|
||||||
|
lazy_enforced: Optional[Set[str]] = None
|
||||||
|
lower_bound: Optional[float] = None
|
||||||
|
mip_log: Optional[str] = None
|
||||||
|
solution: Optional[Solution] = None
|
||||||
|
upper_bound: Optional[float] = None
|
||||||
|
slacks: Optional[Dict[str, float]] = None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class InstanceFeatures:
|
||||||
|
user_features: Optional[List[float]] = None
|
||||||
|
lazy_constraint_count: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class VariableFeatures:
|
||||||
|
category: Optional[Hashable] = None
|
||||||
|
user_features: Optional[List[float]] = None
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ConstraintFeatures:
|
||||||
|
rhs: Optional[float] = None
|
||||||
|
lhs: Optional[Dict[str, float]] = None
|
||||||
|
sense: Optional[str] = None
|
||||||
|
category: Optional[Hashable] = None
|
||||||
|
user_features: Optional[List[float]] = None
|
||||||
|
lazy: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Features:
|
||||||
|
instance: Optional[InstanceFeatures] = None
|
||||||
|
variables: Optional[Dict[str, Dict[VarIndex, VariableFeatures]]] = None
|
||||||
|
constraints: Optional[Dict[str, ConstraintFeatures]] = None
|
||||||
|
|
||||||
|
|
||||||
class FeaturesExtractor:
|
class FeaturesExtractor:
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ import pickle
|
|||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from typing import Any, List, Optional, Hashable, IO, cast
|
from typing import Any, List, Optional, Hashable, IO, cast
|
||||||
|
|
||||||
from miplearn.types import TrainingSample, VarIndex, Features
|
from miplearn.types import VarIndex
|
||||||
|
from miplearn.features import TrainingSample, Features
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|||||||
@@ -13,12 +13,12 @@ from miplearn.components.cuts import UserCutsComponent
|
|||||||
from miplearn.components.lazy_dynamic import DynamicLazyConstraintsComponent
|
from miplearn.components.lazy_dynamic import DynamicLazyConstraintsComponent
|
||||||
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 import FeaturesExtractor
|
from miplearn.features import FeaturesExtractor, TrainingSample
|
||||||
from miplearn.instance import Instance, PickleGzInstance
|
from miplearn.instance import Instance, PickleGzInstance
|
||||||
from miplearn.solvers import _RedirectOutput
|
from miplearn.solvers import _RedirectOutput
|
||||||
from miplearn.solvers.internal import InternalSolver
|
from miplearn.solvers.internal import InternalSolver
|
||||||
from miplearn.solvers.pyomo.gurobi import GurobiPyomoSolver
|
from miplearn.solvers.pyomo.gurobi import GurobiPyomoSolver
|
||||||
from miplearn.types import TrainingSample, LearningSolveStats
|
from miplearn.types import LearningSolveStats
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,7 @@
|
|||||||
# 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 Optional, Dict, Callable, Any, Union, Tuple, List, Set, Hashable
|
from typing import Optional, Dict, Callable, Any, Union, Tuple
|
||||||
from dataclasses import dataclass
|
|
||||||
|
|
||||||
from mypy_extensions import TypedDict
|
from mypy_extensions import TypedDict
|
||||||
|
|
||||||
@@ -11,20 +10,6 @@ VarIndex = Union[str, int, Tuple[Union[str, int]]]
|
|||||||
|
|
||||||
Solution = Dict[str, Dict[VarIndex, Optional[float]]]
|
Solution = Dict[str, Dict[VarIndex, Optional[float]]]
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class TrainingSample:
|
|
||||||
lp_log: Optional[str] = None
|
|
||||||
lp_solution: Optional[Solution] = None
|
|
||||||
lp_value: Optional[float] = None
|
|
||||||
lazy_enforced: Optional[Set[str]] = None
|
|
||||||
lower_bound: Optional[float] = None
|
|
||||||
mip_log: Optional[str] = None
|
|
||||||
solution: Optional[Solution] = None
|
|
||||||
upper_bound: Optional[float] = None
|
|
||||||
slacks: Optional[Dict[str, float]] = None
|
|
||||||
|
|
||||||
|
|
||||||
LPSolveStats = TypedDict(
|
LPSolveStats = TypedDict(
|
||||||
"LPSolveStats",
|
"LPSolveStats",
|
||||||
{
|
{
|
||||||
@@ -75,36 +60,6 @@ LearningSolveStats = TypedDict(
|
|||||||
total=False,
|
total=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class InstanceFeatures:
|
|
||||||
user_features: Optional[List[float]] = None
|
|
||||||
lazy_constraint_count: int = 0
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class VariableFeatures:
|
|
||||||
category: Optional[Hashable] = None
|
|
||||||
user_features: Optional[List[float]] = None
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class ConstraintFeatures:
|
|
||||||
rhs: Optional[float] = None
|
|
||||||
lhs: Optional[Dict[str, float]] = None
|
|
||||||
sense: Optional[str] = None
|
|
||||||
category: Optional[Hashable] = None
|
|
||||||
user_features: Optional[List[float]] = None
|
|
||||||
lazy: bool = False
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Features:
|
|
||||||
instance: Optional[InstanceFeatures] = None
|
|
||||||
variables: Optional[Dict[str, Dict[VarIndex, VariableFeatures]]] = None
|
|
||||||
constraints: Optional[Dict[str, ConstraintFeatures]] = None
|
|
||||||
|
|
||||||
|
|
||||||
IterationCallback = Callable[[], bool]
|
IterationCallback = Callable[[], bool]
|
||||||
|
|
||||||
LazyCallback = Callable[[Any, Any], None]
|
LazyCallback = Callable[[Any, Any], None]
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from miplearn.components.steps.drop_redundant import DropRedundantInequalitiesSt
|
|||||||
from miplearn.instance import Instance
|
from miplearn.instance import Instance
|
||||||
from miplearn.solvers.internal import InternalSolver
|
from miplearn.solvers.internal import InternalSolver
|
||||||
from miplearn.solvers.learning import LearningSolver
|
from miplearn.solvers.learning import LearningSolver
|
||||||
from miplearn.types import TrainingSample, Features
|
from miplearn.features import TrainingSample, Features
|
||||||
from tests.fixtures.infeasible import get_infeasible_instance
|
from tests.fixtures.infeasible import get_infeasible_instance
|
||||||
from tests.fixtures.redundant import get_instance_with_redundancy
|
from tests.fixtures.redundant import get_instance_with_redundancy
|
||||||
|
|
||||||
|
|||||||
@@ -13,11 +13,13 @@ from miplearn.classifiers import Classifier
|
|||||||
from miplearn.classifiers.threshold import Threshold, MinProbabilityThreshold
|
from miplearn.classifiers.threshold import Threshold, MinProbabilityThreshold
|
||||||
from miplearn.components.lazy_static import StaticLazyConstraintsComponent
|
from miplearn.components.lazy_static import StaticLazyConstraintsComponent
|
||||||
from miplearn.types import (
|
from miplearn.types import (
|
||||||
TrainingSample,
|
|
||||||
Features,
|
|
||||||
LearningSolveStats,
|
LearningSolveStats,
|
||||||
|
)
|
||||||
|
from miplearn.features import (
|
||||||
|
TrainingSample,
|
||||||
InstanceFeatures,
|
InstanceFeatures,
|
||||||
ConstraintFeatures,
|
ConstraintFeatures,
|
||||||
|
Features,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from numpy.testing import assert_array_equal
|
|||||||
|
|
||||||
from miplearn import GurobiPyomoSolver, LearningSolver, Regressor
|
from miplearn import GurobiPyomoSolver, LearningSolver, Regressor
|
||||||
from miplearn.components.objective import ObjectiveValueComponent
|
from miplearn.components.objective import ObjectiveValueComponent
|
||||||
from miplearn.types import TrainingSample, Features, InstanceFeatures
|
from miplearn.features import TrainingSample, InstanceFeatures, Features
|
||||||
from tests.fixtures.knapsack import get_knapsack_instance
|
from tests.fixtures.knapsack import get_knapsack_instance
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ 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.problems.tsp import TravelingSalesmanGenerator
|
from miplearn.problems.tsp import TravelingSalesmanGenerator
|
||||||
from miplearn.types import TrainingSample, Features, VariableFeatures
|
from miplearn.features import TrainingSample, VariableFeatures, Features
|
||||||
|
|
||||||
|
|
||||||
def test_xy() -> None:
|
def test_xy() -> None:
|
||||||
|
|||||||
@@ -3,8 +3,12 @@
|
|||||||
# 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 miplearn import GurobiSolver
|
from miplearn import GurobiSolver
|
||||||
from miplearn.features import FeaturesExtractor
|
from miplearn.features import (
|
||||||
from miplearn.types import VariableFeatures, InstanceFeatures, ConstraintFeatures
|
FeaturesExtractor,
|
||||||
|
InstanceFeatures,
|
||||||
|
VariableFeatures,
|
||||||
|
ConstraintFeatures,
|
||||||
|
)
|
||||||
from tests.fixtures.knapsack import get_knapsack_instance
|
from tests.fixtures.knapsack import get_knapsack_instance
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user