mirror of
https://github.com/ANL-CEEESA/MIPLearn.git
synced 2025-12-06 09:28:51 -06:00
Add type annotations to components
This commit is contained in:
@@ -2,8 +2,16 @@
|
||||
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Any, List, Union, TYPE_CHECKING
|
||||
|
||||
from miplearn.instance import Instance
|
||||
from miplearn.types import MIPSolveStats, TrainingSample
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from miplearn.solvers.learning import LearningSolver
|
||||
|
||||
|
||||
class Component(ABC):
|
||||
@@ -15,18 +23,35 @@ class Component(ABC):
|
||||
strategy.
|
||||
"""
|
||||
|
||||
def before_solve(self, solver, instance, model):
|
||||
def before_solve(
|
||||
self,
|
||||
solver: LearningSolver,
|
||||
instance: Instance,
|
||||
model: Any,
|
||||
) -> None:
|
||||
"""
|
||||
Method called by LearningSolver before the problem is solved.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
solver
|
||||
The solver calling this method.
|
||||
instance
|
||||
The instance being solved.
|
||||
model
|
||||
The concrete optimization model being solved.
|
||||
"""
|
||||
return
|
||||
|
||||
@abstractmethod
|
||||
def after_solve(
|
||||
self,
|
||||
solver,
|
||||
instance,
|
||||
model,
|
||||
stats,
|
||||
training_data,
|
||||
):
|
||||
solver: LearningSolver,
|
||||
instance: Instance,
|
||||
model: Any,
|
||||
stats: MIPSolveStats,
|
||||
training_data: TrainingSample,
|
||||
) -> None:
|
||||
"""
|
||||
Method called by LearningSolver after the problem is solved to optimality.
|
||||
|
||||
@@ -40,19 +65,23 @@ class Component(ABC):
|
||||
The concrete optimization model being solved.
|
||||
stats: dict
|
||||
A dictionary containing statistics about the solution process, such as
|
||||
number of nodes explored and running time. Components are free to add their own
|
||||
statistics here. For example, PrimalSolutionComponent adds statistics regarding
|
||||
the number of predicted variables. All statistics in this dictionary are exported
|
||||
to the benchmark CSV file.
|
||||
number of nodes explored and running time. Components are free to add
|
||||
their own statistics here. For example, PrimalSolutionComponent adds
|
||||
statistics regarding the number of predicted variables. All statistics in
|
||||
this dictionary are exported to the benchmark CSV file.
|
||||
training_data: dict
|
||||
A dictionary containing data that may be useful for training machine learning
|
||||
models and accelerating the solution process. Components are free to add their
|
||||
own training data here. For example, PrimalSolutionComponent adds the current
|
||||
primal solution. The data must be pickable.
|
||||
A dictionary containing data that may be useful for training machine
|
||||
learning models and accelerating the solution process. Components are
|
||||
free to add their own training data here. For example,
|
||||
PrimalSolutionComponent adds the current primal solution. The data must
|
||||
be pickable.
|
||||
"""
|
||||
pass
|
||||
|
||||
def fit(self, training_instances):
|
||||
def fit(
|
||||
self,
|
||||
training_instances: Union[List[str], List[Instance]],
|
||||
) -> None:
|
||||
return
|
||||
|
||||
def iteration_cb(self, solver, instance, model):
|
||||
|
||||
@@ -5,10 +5,12 @@
|
||||
import logging
|
||||
import sys
|
||||
from copy import deepcopy
|
||||
from typing import Any, Dict
|
||||
|
||||
import numpy as np
|
||||
from tqdm.auto import tqdm
|
||||
|
||||
from miplearn.classifiers import Classifier
|
||||
from miplearn.classifiers.counting import CountingClassifier
|
||||
from miplearn.components import classifier_evaluation_dict
|
||||
from miplearn.components.component import Component
|
||||
@@ -24,15 +26,12 @@ class UserCutsComponent(Component):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
classifier=CountingClassifier(),
|
||||
threshold=0.05,
|
||||
classifier: Classifier = CountingClassifier(),
|
||||
threshold: float = 0.05,
|
||||
):
|
||||
self.violations = set()
|
||||
self.count = {}
|
||||
self.n_samples = 0
|
||||
self.threshold = threshold
|
||||
self.classifier_prototype = classifier
|
||||
self.classifiers = {}
|
||||
self.threshold: float = threshold
|
||||
self.classifier_prototype: Classifier = classifier
|
||||
self.classifiers: Dict[Any, Classifier] = {}
|
||||
|
||||
def before_solve(self, solver, instance, model):
|
||||
instance.found_violated_user_cuts = []
|
||||
|
||||
@@ -5,10 +5,12 @@
|
||||
import logging
|
||||
import sys
|
||||
from copy import deepcopy
|
||||
from typing import Any, Dict
|
||||
|
||||
import numpy as np
|
||||
from tqdm.auto import tqdm
|
||||
|
||||
from miplearn.classifiers import Classifier
|
||||
from miplearn.classifiers.counting import CountingClassifier
|
||||
from miplearn.components import classifier_evaluation_dict
|
||||
from miplearn.components.component import Component
|
||||
@@ -24,15 +26,12 @@ class DynamicLazyConstraintsComponent(Component):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
classifier=CountingClassifier(),
|
||||
threshold=0.05,
|
||||
classifier: Classifier = CountingClassifier(),
|
||||
threshold: float = 0.05,
|
||||
):
|
||||
self.violations = set()
|
||||
self.count = {}
|
||||
self.n_samples = 0
|
||||
self.threshold = threshold
|
||||
self.classifier_prototype = classifier
|
||||
self.classifiers = {}
|
||||
self.threshold: float = threshold
|
||||
self.classifier_prototype: Classifier = classifier
|
||||
self.classifiers: Dict[Any, Classifier] = {}
|
||||
|
||||
def before_solve(self, solver, instance, model):
|
||||
instance.found_violated_lazy_constraints = []
|
||||
|
||||
@@ -15,6 +15,7 @@ from sklearn.metrics import (
|
||||
r2_score,
|
||||
)
|
||||
|
||||
from miplearn.classifiers import Regressor
|
||||
from miplearn.components.component import Component
|
||||
from miplearn.extractors import InstanceFeaturesExtractor, ObjectiveValueExtractor
|
||||
|
||||
@@ -26,7 +27,10 @@ class ObjectiveValueComponent(Component):
|
||||
A Component which predicts the optimal objective value of the problem.
|
||||
"""
|
||||
|
||||
def __init__(self, regressor=LinearRegression()):
|
||||
def __init__(
|
||||
self,
|
||||
regressor: Regressor = LinearRegression(),
|
||||
) -> None:
|
||||
self.ub_regressor = None
|
||||
self.lb_regressor = None
|
||||
self.regressor_prototype = regressor
|
||||
|
||||
@@ -2,14 +2,19 @@
|
||||
# Copyright (C) 2020, UChicago Argonne, LLC. All rights reserved.
|
||||
# Released under the modified BSD license. See COPYING.md for more details.
|
||||
|
||||
import logging
|
||||
from copy import deepcopy
|
||||
import sys
|
||||
from typing import Union, Dict, Any
|
||||
|
||||
from .component import Component
|
||||
from ..classifiers.adaptive import AdaptiveClassifier
|
||||
from ..classifiers.threshold import MinPrecisionThreshold, DynamicThreshold
|
||||
from ..components import classifier_evaluation_dict
|
||||
from ..extractors import *
|
||||
import numpy as np
|
||||
from tqdm.auto import tqdm
|
||||
|
||||
from miplearn.classifiers import Classifier
|
||||
from miplearn.classifiers.adaptive import AdaptiveClassifier
|
||||
from miplearn.classifiers.threshold import MinPrecisionThreshold, DynamicThreshold
|
||||
from miplearn.components import classifier_evaluation_dict
|
||||
from miplearn.components.component import Component
|
||||
from miplearn.extractors import VariableFeaturesExtractor, SolutionExtractor, Extractor
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -21,13 +26,13 @@ class PrimalSolutionComponent(Component):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
classifier=AdaptiveClassifier(),
|
||||
mode="exact",
|
||||
threshold=MinPrecisionThreshold(0.98),
|
||||
):
|
||||
classifier: Classifier = AdaptiveClassifier(),
|
||||
mode: str = "exact",
|
||||
threshold: Union[float, DynamicThreshold] = MinPrecisionThreshold(0.98),
|
||||
) -> None:
|
||||
self.mode = mode
|
||||
self.classifiers = {}
|
||||
self.thresholds = {}
|
||||
self.classifiers: Dict[Any, Classifier] = {}
|
||||
self.thresholds: Dict[Any, Union[float, DynamicThreshold]] = {}
|
||||
self.threshold_prototype = threshold
|
||||
self.classifier_prototype = classifier
|
||||
|
||||
|
||||
Reference in New Issue
Block a user