mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Remove EnforceOverrides; automatically convert np.ndarray features
This commit is contained in:
@@ -28,8 +28,6 @@
|
|||||||
def get_variable_category(self, var_name: str) -> Optional[Hashable]:
|
def get_variable_category(self, var_name: str) -> Optional[Hashable]:
|
||||||
pass
|
pass
|
||||||
````
|
````
|
||||||
- Features are now represented as a list of floating point numbers, as indicated in the snippet above. This change was required for performance reasons. Returning numpy arrays is no longer supported, and raises an error.
|
|
||||||
|
|
||||||
- Internal solvers must now be specified as objects, instead of strings. For example,
|
- Internal solvers must now be specified as objects, instead of strings. For example,
|
||||||
```python
|
```python
|
||||||
solver = LearningSolver(
|
solver = LearningSolver(
|
||||||
|
|||||||
@@ -13,6 +13,13 @@ from .components.dynamic_user_cuts import UserCutsComponent
|
|||||||
from .components.objective import ObjectiveValueComponent
|
from .components.objective import ObjectiveValueComponent
|
||||||
from .components.primal import PrimalSolutionComponent
|
from .components.primal import PrimalSolutionComponent
|
||||||
from .components.static_lazy import StaticLazyConstraintsComponent
|
from .components.static_lazy import StaticLazyConstraintsComponent
|
||||||
|
from .features import (
|
||||||
|
Features,
|
||||||
|
TrainingSample,
|
||||||
|
ConstraintFeatures,
|
||||||
|
VariableFeatures,
|
||||||
|
InstanceFeatures,
|
||||||
|
)
|
||||||
from .instance.base import Instance
|
from .instance.base import Instance
|
||||||
from .instance.picklegz import (
|
from .instance.picklegz import (
|
||||||
PickleGzInstance,
|
PickleGzInstance,
|
||||||
@@ -27,6 +34,3 @@ from .solvers.learning import LearningSolver
|
|||||||
from .solvers.pyomo.base import BasePyomoSolver
|
from .solvers.pyomo.base import BasePyomoSolver
|
||||||
from .solvers.pyomo.cplex import CplexPyomoSolver
|
from .solvers.pyomo.cplex import CplexPyomoSolver
|
||||||
from .solvers.pyomo.gurobi import GurobiPyomoSolver
|
from .solvers.pyomo.gurobi import GurobiPyomoSolver
|
||||||
|
|
||||||
# noinspection PyUnresolvedReferences
|
|
||||||
from overrides import overrides
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ if TYPE_CHECKING:
|
|||||||
|
|
||||||
|
|
||||||
# noinspection PyMethodMayBeStatic
|
# noinspection PyMethodMayBeStatic
|
||||||
class Component(EnforceOverrides):
|
class Component:
|
||||||
"""
|
"""
|
||||||
A Component is an object which adds functionality to a LearningSolver.
|
A Component is an object which adds functionality to a LearningSolver.
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from dataclasses import dataclass
|
|||||||
from typing import TYPE_CHECKING, Dict, Optional, Set, List, Hashable
|
from typing import TYPE_CHECKING, Dict, Optional, Set, List, Hashable
|
||||||
|
|
||||||
from miplearn.types import Solution, VariableName, Category
|
from miplearn.types import Solution, VariableName, Category
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from miplearn.solvers.internal import InternalSolver
|
from miplearn.solvers.internal import InternalSolver
|
||||||
@@ -83,6 +84,8 @@ class FeaturesExtractor:
|
|||||||
f"Found {type(category).__name__} instead for var={var_name}."
|
f"Found {type(category).__name__} instead for var={var_name}."
|
||||||
)
|
)
|
||||||
user_features = instance.get_variable_features(var_name)
|
user_features = instance.get_variable_features(var_name)
|
||||||
|
if isinstance(user_features, np.ndarray):
|
||||||
|
user_features = user_features.tolist()
|
||||||
assert isinstance(user_features, list), (
|
assert isinstance(user_features, list), (
|
||||||
f"Variable features must be a list. "
|
f"Variable features must be a list. "
|
||||||
f"Found {type(user_features).__name__} instead for "
|
f"Found {type(user_features).__name__} instead for "
|
||||||
@@ -115,6 +118,8 @@ class FeaturesExtractor:
|
|||||||
f"Found {type(category).__name__} instead for cid={cid}.",
|
f"Found {type(category).__name__} instead for cid={cid}.",
|
||||||
)
|
)
|
||||||
user_features = instance.get_constraint_features(cid)
|
user_features = instance.get_constraint_features(cid)
|
||||||
|
if isinstance(user_features, np.ndarray):
|
||||||
|
user_features = user_features.tolist()
|
||||||
assert isinstance(user_features, list), (
|
assert isinstance(user_features, list), (
|
||||||
f"Constraint features must be a list. "
|
f"Constraint features must be a list. "
|
||||||
f"Found {type(user_features).__name__} instead for cid={cid}."
|
f"Found {type(user_features).__name__} instead for cid={cid}."
|
||||||
@@ -141,6 +146,8 @@ class FeaturesExtractor:
|
|||||||
) -> InstanceFeatures:
|
) -> InstanceFeatures:
|
||||||
assert features.constraints is not None
|
assert features.constraints is not None
|
||||||
user_features = instance.get_instance_features()
|
user_features = instance.get_instance_features()
|
||||||
|
if isinstance(user_features, np.ndarray):
|
||||||
|
user_features = user_features.tolist()
|
||||||
assert isinstance(user_features, list), (
|
assert isinstance(user_features, list), (
|
||||||
f"Instance features must be a list. "
|
f"Instance features must be a list. "
|
||||||
f"Found {type(user_features).__name__} instead."
|
f"Found {type(user_features).__name__} instead."
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ logger = logging.getLogger(__name__)
|
|||||||
|
|
||||||
|
|
||||||
# noinspection PyMethodMayBeStatic
|
# noinspection PyMethodMayBeStatic
|
||||||
class Instance(ABC, EnforceOverrides):
|
class Instance(ABC):
|
||||||
"""
|
"""
|
||||||
Abstract class holding all the data necessary to generate a concrete model of the
|
Abstract class holding all the data necessary to generate a concrete model of the
|
||||||
proble.
|
proble.
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ from miplearn.types import (
|
|||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class InternalSolver(ABC, EnforceOverrides):
|
class InternalSolver(ABC):
|
||||||
"""
|
"""
|
||||||
Abstract class representing the MIP solver used internally by LearningSolver.
|
Abstract class representing the MIP solver used internally by LearningSolver.
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user